From 182908d216f27861d033f69398abff996caf8053 Mon Sep 17 00:00:00 2001 From: "Justin Clark-Casey (justincc)" Date: Tue, 25 Oct 2011 23:16:01 +0100 Subject: [PATCH] In Watchdog, add ability to specific timeout for a thread. This also changes the point of registration to the StartThread() call rather than the first Update() --- OpenSim/Framework/Watchdog.cs | 72 +++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/OpenSim/Framework/Watchdog.cs b/OpenSim/Framework/Watchdog.cs index 3ef9ccbba0..1374518b6e 100644 --- a/OpenSim/Framework/Watchdog.cs +++ b/OpenSim/Framework/Watchdog.cs @@ -40,18 +40,30 @@ namespace OpenSim.Framework { /// Timer interval in milliseconds for the watchdog timer const double WATCHDOG_INTERVAL_MS = 2500.0d; + /// Maximum timeout in milliseconds before a thread is considered dead const int WATCHDOG_TIMEOUT_MS = 5000; [System.Diagnostics.DebuggerDisplay("{Thread.Name}")] public class ThreadWatchdogInfo { - public Thread Thread; - public int LastTick; + public Thread Thread { get; private set; } + public int LastTick { get; set; } - public ThreadWatchdogInfo(Thread thread) + /// + /// Number of seconds before we notify that the thread is having a problem. + /// + public int Timeout { get; set; } + + /// + /// Is this thread considered timed out? + /// + public bool IsTimedOut { get; set; } + + public ThreadWatchdogInfo(Thread thread, int timeout) { Thread = thread; + Timeout = timeout; LastTick = Environment.TickCount & Int32.MaxValue; } } @@ -82,7 +94,7 @@ namespace OpenSim.Framework } /// - /// Start a new thread that is tracked by the watchdog timer + /// Start a new thread that is tracked by the watchdog timer. /// /// The method that will be executed in a new thread /// A name to give to the new thread @@ -91,6 +103,24 @@ namespace OpenSim.Framework /// thread, otherwise false /// The newly created Thread object public static Thread StartThread(ThreadStart start, string name, ThreadPriority priority, bool isBackground) + { + return StartThread(start, name, priority, isBackground, WATCHDOG_TIMEOUT_MS); + } + + /// + /// Start a new thread that is tracked by the watchdog timer + /// + /// The method that will be executed in a new thread + /// A name to give to the new thread + /// Priority to run the thread at + /// True to run this thread as a background + /// thread, otherwise false + /// + /// Number of seconds to wait until we issue a warning about timeout. + /// + /// The newly created Thread object + public static Thread StartThread( + ThreadStart start, string name, ThreadPriority priority, bool isBackground, int timeout) { Thread thread = new Thread(start); thread.Name = name; @@ -98,6 +128,13 @@ namespace OpenSim.Framework thread.IsBackground = isBackground; thread.Start(); + 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); + return thread; } @@ -121,14 +158,6 @@ namespace OpenSim.Framework return RemoveThread(Thread.CurrentThread.ManagedThreadId); } - 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); - } - private static bool RemoveThread(int threadID) { lock (m_threads) @@ -165,9 +194,14 @@ namespace OpenSim.Framework try { if (m_threads.TryGetValue(threadID, out threadInfo)) + { threadInfo.LastTick = Environment.TickCount & Int32.MaxValue; + threadInfo.IsTimedOut = false; + } else - AddThread(new ThreadWatchdogInfo(Thread.CurrentThread)); + { + m_log.WarnFormat("[WATCHDOG]: Asked to update thread {0} which is not being monitored", threadID); + } } catch { } } @@ -196,10 +230,16 @@ namespace OpenSim.Framework foreach (ThreadWatchdogInfo threadInfo in m_threads.Values) { - if (threadInfo.Thread.ThreadState == ThreadState.Stopped || now - threadInfo.LastTick >= WATCHDOG_TIMEOUT_MS) + if (threadInfo.Thread.ThreadState == ThreadState.Stopped) { timedOut = threadInfo; - m_threads.Remove(threadInfo.Thread.ManagedThreadId); + RemoveThread(threadInfo.Thread.ManagedThreadId); + break; + } + else if (!threadInfo.IsTimedOut && now - threadInfo.LastTick >= threadInfo.Timeout) + { + threadInfo.IsTimedOut = true; + timedOut = threadInfo; break; } } @@ -212,4 +252,4 @@ namespace OpenSim.Framework m_watchdogTimer.Start(); } } -} +} \ No newline at end of file