Ejemplo n.º 1
0
        /// <summary>
        /// Check watched threads.  Fire alarm if appropriate.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void WatchdogTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            int now       = Environment.TickCount & Int32.MaxValue;
            int msElapsed = now - LastWatchdogThreadTick;

            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);
            }

            LastWatchdogThreadTick = Environment.TickCount & Int32.MaxValue;

            Action <ThreadWatchdogInfo> callback = OnWatchdogTimeout;

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

                lock (m_threads)
                {
                    // get a copy since we may change m_threads
                    List <ThreadWatchdogInfo> threadsInfo = m_threads.Values.ToList();
                    foreach (ThreadWatchdogInfo threadInfo in threadsInfo)
                    {
                        if (threadInfo.Thread.ThreadState == ThreadState.Stopped)
                        {
                            RemoveThread(threadInfo.Thread.ManagedThreadId);

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

                            callbackInfos.Add(threadInfo);
                        }
                        else if (!threadInfo.IsTimedOut && now - threadInfo.LastTick >= threadInfo.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.
                                callbackInfos.Add(new ThreadWatchdogInfo(threadInfo));
                            }
                        }
                    }
                }

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

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

            ChecksManager.CheckChecks();
            StatsManager.RecordStats();

            m_watchdogTimer.Start();
        }
Ejemplo n.º 2
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.º 3
0
        /// <summary>
        /// Check watched threads.  Fire alarm if appropriate.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void WatchdogTimerElapsed(object sender)
        {
            if (!m_enabled)
            {
                return;
            }
            int now       = Environment.TickCount & Int32.MaxValue;
            int msElapsed = now - LastWatchdogThreadTick;

            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);
            }

            LastWatchdogThreadTick = Environment.TickCount & Int32.MaxValue;

            Action <ThreadWatchdogInfo> callback = OnWatchdogTimeout;

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

                const ThreadState thgone = ThreadState.Stopped;

                lock (m_threads)
                {
                    foreach (ThreadWatchdogInfo threadInfo in m_threads.Values)
                    {
                        if (!m_enabled)
                        {
                            return;
                        }
                        if ((threadInfo.Thread.ThreadState & thgone) != 0)
                        {
                            if (threadsToRemove == null)
                            {
                                threadsToRemove = new List <ThreadWatchdogInfo>();
                            }

                            threadsToRemove.Add(threadInfo);

                            /*
                             *                          if(callbackInfos == null)
                             *                              callbackInfos = new List<ThreadWatchdogInfo>();
                             *
                             *                          callbackInfos.Add(threadInfo);
                             */
                        }
                        else if (!threadInfo.IsTimedOut && now - threadInfo.LastTick >= threadInfo.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.
                                callbackInfos.Add(new ThreadWatchdogInfo(threadInfo));
                            }
                        }
                    }

                    if (threadsToRemove != null)
                    {
                        foreach (ThreadWatchdogInfo twi in threadsToRemove)
                        {
                            RemoveThread(twi.Thread.ManagedThreadId);
                        }
                    }
                }

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

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

            ChecksManager.CheckChecks();
            StatsManager.RecordStats();

            m_watchdogTimer.Change(WATCHDOG_INTERVAL_MS, Timeout.Infinite);
        }