Ejemplo n.º 1
0
        /// <summary>
        /// Start a new thread that is tracked by the watchdog timer
        /// </summary>
        /// <param name="start">The method that will be executed in a new thread</param>
        /// <param name="name">A name to give to the new thread</param>
        /// <param name="priority">Priority to run the thread at</param>
        /// <param name="isBackground">True to run this thread as a background
        /// thread, otherwise false</param>
        /// <param name="alarmIfTimeout">Trigger an alarm function is we have timed out</param>
        /// <param name="alarmMethod">
        /// Alarm method to call if alarmIfTimeout is true and there is a timeout.
        /// Normally, this will just return some useful debugging information.
        /// </param>
        /// <param name="timeout">Number of milliseconds to wait until we issue a warning about timeout.</param>
        /// <returns>The newly created Thread object</returns>
        public static Thread StartThread(
            ThreadStart start, string name, ThreadPriority priority, bool isBackground,
            bool alarmIfTimeout, Func <string> alarmMethod, int timeout)
        {
            Thread thread = new Thread(start);

            thread.Name         = name;
            thread.Priority     = priority;
            thread.IsBackground = isBackground;

            ThreadWatchdogInfo twi
                = new ThreadWatchdogInfo(thread, timeout)
                {
                AlarmIfTimeout = alarmIfTimeout, AlarmMethod = alarmMethod
                };

            m_log.DebugFormat(
                "[WATCHDOG]: Started tracking thread {0}, ID {1}", twi.Thread.Name, twi.Thread.ManagedThreadId);

            lock (m_threads)
                m_threads.Add(twi.Thread.ManagedThreadId, twi);

            thread.Start();

            return(thread);
        }
Ejemplo n.º 2
0
        private void WatchdogTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            EventHandler <WatchdogTimeoutArgs> callback = OnWatchdogTimeout;

            if (callback != null)
            {
                ThreadWatchdogInfo timedOut = null;

                lock (m_threads)
                {
                    int now = Util.TickCount();

                    foreach (ThreadWatchdogInfo threadInfo in m_threads.Values)
                    {
                        if (threadInfo.Thread.ThreadState == ThreadState.Stopped || now - threadInfo.LastTick >= WATCHDOG_TIMEOUT_MS)
                        {
                            timedOut = threadInfo;
                            m_threads.Remove(threadInfo.Thread.ManagedThreadId);
                            break;
                        }
                    }
                }

                if (timedOut != null)
                {
                    callback(this, new WatchdogTimeoutArgs {
                        Thread = timedOut.Thread, LastTick = timedOut.LastTick
                    });
                }
            }

            m_watchdogTimer.Start();
        }
Ejemplo n.º 3
0
        private void AddThread(ThreadWatchdogInfo threadInfo)
        {
            //m_log.Debug("[WATCHDOG]: Started tracking thread \"" + threadInfo.Thread.Name + "\" (ID " + threadInfo.Thread.ManagedThreadId + ")");

            lock (m_threads)
                m_threads.Add(threadInfo.Thread.ManagedThreadId, threadInfo);
        }
Ejemplo n.º 4
0
        private static void WatchdogTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            WatchdogTimeout callback = OnWatchdogTimeout;

            if (callback != null)
            {
                ThreadWatchdogInfo timedOut = null;

                lock (m_threads)
                {
                    int now = Environment.TickCount & Int32.MaxValue;

                    foreach (ThreadWatchdogInfo threadInfo in m_threads.Values)
                    {
                        if (threadInfo.Thread.ThreadState == ThreadState.Stopped || now - threadInfo.LastTick >= WATCHDOG_TIMEOUT_MS)
                        {
                            timedOut = threadInfo;
                            m_threads.Remove(threadInfo.Thread.ManagedThreadId);
                            break;
                        }
                    }
                }

                if (timedOut != null)
                {
                    callback(timedOut.Thread, timedOut.LastTick);
                }
            }

            m_watchdogTimer.Start();
        }
Ejemplo n.º 5
0
        private static void AddThread(ThreadWatchdogInfo threadInfo)
        {
            m_log.Debug("[Watch Dog]: Started tracking thread \"" + threadInfo.Thread.Name + "\" (ID " + threadInfo.Thread.ManagedThreadId + ")");

            lock (m_threads)
            {
                m_threads.Add(threadInfo.Thread.ManagedThreadId, threadInfo);
            }
        }
Ejemplo n.º 6
0
 public ThreadWatchdogInfo(ThreadWatchdogInfo previousTwi)
 {
     Thread         = previousTwi.Thread;
     FirstTick      = previousTwi.FirstTick;
     LastTick       = previousTwi.LastTick;
     Timeout        = previousTwi.Timeout;
     IsTimedOut     = previousTwi.IsTimedOut;
     AlarmIfTimeout = previousTwi.AlarmIfTimeout;
     AlarmMethod    = previousTwi.AlarmMethod;
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Add a thread to the watchdog tracker.
        /// </summary>
        /// <param name="info">Information about the thread.</info>
        /// <param name="info">Name of the thread.</info>
        /// <param name="log">If true then creation of thread is logged.</param>
        public static void AddThread(ThreadWatchdogInfo info, string name, bool log = true)
        {
            if (log)
            {
                m_log.DebugFormat(
                    "[WATCHDOG]: Started tracking thread {0}, ID {1}", name, info.Thread.ManagedThreadId);
            }

            lock (m_threads)
                m_threads.Add(info.Thread.ManagedThreadId, info);
        }
Ejemplo n.º 8
0
        public static bool AbortThread(int threadID)
        {
            lock (m_threads)
            {
                if (m_threads.ContainsKey(threadID))
                {
                    ThreadWatchdogInfo twi = m_threads[threadID];
                    twi.Thread.Abort();
                    RemoveThread(threadID);

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Start a new thread that is tracked by the watchdog timer
        /// </summary>
        /// <param name="start">The method that will be executed in a new thread</param>
        /// <param name="name">A name to give to the new thread</param>
        /// <param name="priority">Priority to run the thread at</param>
        /// <param name="isBackground">True to run this thread as a background
        /// thread, otherwise false</param>
        /// <param name="timeout">
        /// Number of milliseconds to wait until we issue a warning about timeout.
        /// </para>
        /// <returns>The newly created Thread object</returns>
        public static Thread StartThread(
            ThreadStart start, string name, ThreadPriority priority, bool isBackground, int timeout)
        {
            Thread thread = new Thread(start);

            thread.Name         = name;
            thread.Priority     = priority;
            thread.IsBackground = isBackground;

            ThreadWatchdogInfo twi = new ThreadWatchdogInfo(thread, timeout);

            m_log.Debug("[WATCHDOG]: Started tracking thread \"" + twi.Thread.Name + "\" (ID " + twi.Thread.ManagedThreadId + ")");

            lock (m_threads)
                m_threads.Add(twi.Thread.ManagedThreadId, twi);

            thread.Start();

            return(thread);
        }
Ejemplo n.º 10
0
 public ThreadWatchdogInfo(ThreadWatchdogInfo previousTwi)
 {
     Thread = previousTwi.Thread;
     FirstTick = previousTwi.FirstTick;
     LastTick = previousTwi.LastTick;
     Timeout = previousTwi.Timeout;
     IsTimedOut = previousTwi.IsTimedOut;
     AlarmIfTimeout = previousTwi.AlarmIfTimeout;
     AlarmMethod = previousTwi.AlarmMethod;
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Start a new thread that is tracked by the watchdog timer
        /// </summary>
        /// <param name="start">The method that will be executed in a new thread</param>
        /// <param name="name">A name to give to the new thread</param>
        /// <param name="priority">Priority to run the thread at</param>
        /// <param name="isBackground">True to run this thread as a background
        /// thread, otherwise false</param>
        /// <param name="alarmIfTimeout">Trigger an alarm function is we have timed out</param>
        /// <param name="alarmMethod">
        /// Alarm method to call if alarmIfTimeout is true and there is a timeout.
        /// Normally, this will just return some useful debugging information.
        /// </param>
        /// <param name="timeout">Number of milliseconds to wait until we issue a warning about timeout.</param>
        /// <returns>The newly created Thread object</returns>
        public static Thread StartThread(
            ThreadStart start, string name, ThreadPriority priority, bool isBackground,
            bool alarmIfTimeout, Func<string> alarmMethod, int timeout)
        {
            Thread thread = new Thread(start);
            thread.Name = name;
            thread.Priority = priority;
            thread.IsBackground = isBackground;
            
            ThreadWatchdogInfo twi
                = new ThreadWatchdogInfo(thread, timeout)
                    { AlarmIfTimeout = alarmIfTimeout, AlarmMethod = alarmMethod };

            m_log.DebugFormat(
                "[WATCHDOG]: Started tracking thread {0}, ID {1}", twi.Thread.Name, twi.Thread.ManagedThreadId);

            lock (m_threads)
                m_threads.Add(twi.Thread.ManagedThreadId, twi);

            thread.Start();

            return thread;
        }
Ejemplo n.º 12
0
        private static void AddThread(ThreadWatchdogInfo threadInfo)
        {
            m_log.Debug("[WATCHDOG]: Started tracking thread \"" + threadInfo.Thread.Name + "\" (ID " + threadInfo.Thread.ManagedThreadId + ")");

            lock (m_threads)
                m_threads.Add(threadInfo.Thread.ManagedThreadId, threadInfo);
        }
Ejemplo n.º 13
0
        private static void WatchdogTimerElapsed()
        {
            int now       = Environment.TickCount;
            int msElapsed = now - LastWatchdogThreadTick;

            LastWatchdogThreadTick = now;

            if (!m_enabled)
            {
                return;
            }

            if (msElapsed > WATCHDOG_INTERVAL_MS * 2)
            {
                m_log.WarnFormat(
                    "[WATCHDOG]: {0} ms since Watchdog last ran.  Interval should be approximately {1} ms",
                    msElapsed, WATCHDOG_INTERVAL_MS);
            }

            Action <ThreadWatchdogInfo> callback = OnWatchdogTimeout;

            if (callback != null)
            {
                List <ThreadWatchdogInfo> callbackInfos = null;

                lock (m_threads)
                {
scan:
                    foreach (ThreadWatchdogInfo threadInfo in m_threads.Values)
                    {
                        if ((threadInfo.Thread.ThreadState & ThreadState.Stopped) != 0)
                        {
                            RemoveThread(threadInfo.Thread.ManagedThreadId);

                            if (callbackInfos == null)
                            {
                                callbackInfos = new List <ThreadWatchdogInfo>();
                            }

                            callbackInfos.Add(threadInfo);
                            goto scan;
                        }

                        if (!threadInfo.IsTimedOut)
                        {
                            int lastTick = threadInfo.LastTick;
                            int delta    = now - lastTick; // .lt. 0 means LastTick updated after now was set

                            int timeout = threadInfo.Timeout;
                            if (delta >= timeout)
                            {
                                threadInfo.IsTimedOut = true;

                                if (threadInfo.AlarmIfTimeout)
                                {
                                    if (callbackInfos == null)
                                    {
                                        callbackInfos = new List <ThreadWatchdogInfo>();
                                    }

                                    // Send a copy of the watchdog info to prevent race conditions where the watchdog
                                    // thread updates the monitoring info after an alarm has been sent out.
                                    ThreadWatchdogInfo twicopy = new ThreadWatchdogInfo(threadInfo);
                                    twicopy.LastTick = lastTick;
                                    callbackInfos.Add(twicopy);
                                }
                            }
                        }
                    }
                }

                if (callbackInfos != null)
                {
                    foreach (ThreadWatchdogInfo callbackInfo in callbackInfos)
                    {
                        callback(callbackInfo);
                    }
                }
            }

            if (MemoryWatchdog.Enabled)
            {
                MemoryWatchdog.Update();
            }

            StatsManager.RecordStats();
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Add a thread to the watchdog tracker.
        /// </summary>
        /// <param name="info">Information about the thread.</info>
        /// <param name="info">Name of the thread.</info>
        /// <param name="log">If true then creation of thread is logged.</param>
        public static void AddThread(ThreadWatchdogInfo info, string name, bool log = true)
        {
            if (log)
                m_log.DebugFormat(
                    "[WATCHDOG]: Started tracking thread {0}, ID {1}", name, info.Thread.ManagedThreadId);

            lock (m_threads)
                m_threads.Add(info.Thread.ManagedThreadId, info);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Start a new thread that is tracked by the watchdog timer
        /// </summary>
        /// <param name="start">The method that will be executed in a new thread</param>
        /// <param name="name">A name to give to the new thread</param>
        /// <param name="priority">Priority to run the thread at</param>
        /// <param name="isBackground">True to run this thread as a background
        /// thread, otherwise false</param>
        /// <param name="timeout">
        /// Number of milliseconds to wait until we issue a warning about timeout.
        /// </para>
        /// <returns>The newly created Thread object</returns>
        public static Thread StartThread(
            ThreadStart start, string name, ThreadPriority priority, bool isBackground, int timeout)
        {
            Thread thread = new Thread(start);
            thread.Name = name;
            thread.Priority = priority;
            thread.IsBackground = isBackground;
            
            ThreadWatchdogInfo twi = new ThreadWatchdogInfo(thread, timeout);

            m_log.Debug("[WATCHDOG]: Started tracking thread \"" + twi.Thread.Name + "\" (ID " + twi.Thread.ManagedThreadId + ")");

            lock (m_threads)
                m_threads.Add(twi.Thread.ManagedThreadId, twi);

            thread.Start();

            return thread;
        }