aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Monitoring/Watchdog.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/Monitoring/Watchdog.cs106
1 files changed, 49 insertions, 57 deletions
diff --git a/OpenSim/Framework/Monitoring/Watchdog.cs b/OpenSim/Framework/Monitoring/Watchdog.cs
index 3f992b1..a644fa5 100644
--- a/OpenSim/Framework/Monitoring/Watchdog.cs
+++ b/OpenSim/Framework/Monitoring/Watchdog.cs
@@ -38,6 +38,8 @@ namespace OpenSim.Framework.Monitoring
38 /// </summary> 38 /// </summary>
39 public static class Watchdog 39 public static class Watchdog
40 { 40 {
41 private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
42
41 /// <summary>Timer interval in milliseconds for the watchdog timer</summary> 43 /// <summary>Timer interval in milliseconds for the watchdog timer</summary>
42 public const double WATCHDOG_INTERVAL_MS = 2500.0d; 44 public const double WATCHDOG_INTERVAL_MS = 2500.0d;
43 45
@@ -82,12 +84,32 @@ namespace OpenSim.Framework.Monitoring
82 /// </summary> 84 /// </summary>
83 public Func<string> AlarmMethod { get; set; } 85 public Func<string> AlarmMethod { get; set; }
84 86
85 public ThreadWatchdogInfo(Thread thread, int timeout) 87 /// <summary>
88 /// Stat structure associated with this thread.
89 /// </summary>
90 public Stat Stat { get; set; }
91
92 public ThreadWatchdogInfo(Thread thread, int timeout, string name)
86 { 93 {
87 Thread = thread; 94 Thread = thread;
88 Timeout = timeout; 95 Timeout = timeout;
89 FirstTick = Environment.TickCount & Int32.MaxValue; 96 FirstTick = Environment.TickCount & Int32.MaxValue;
90 LastTick = FirstTick; 97 LastTick = FirstTick;
98
99 Stat
100 = new Stat(
101 name,
102 string.Format("Last update of thread {0}", name),
103 "",
104 "ms",
105 "server",
106 "thread",
107 StatType.Pull,
108 MeasuresOfInterest.None,
109 stat => stat.Value = Environment.TickCount & Int32.MaxValue - LastTick,
110 StatVerbosity.Debug);
111
112 StatsManager.RegisterStat(Stat);
91 } 113 }
92 114
93 public ThreadWatchdogInfo(ThreadWatchdogInfo previousTwi) 115 public ThreadWatchdogInfo(ThreadWatchdogInfo previousTwi)
@@ -100,6 +122,11 @@ namespace OpenSim.Framework.Monitoring
100 AlarmIfTimeout = previousTwi.AlarmIfTimeout; 122 AlarmIfTimeout = previousTwi.AlarmIfTimeout;
101 AlarmMethod = previousTwi.AlarmMethod; 123 AlarmMethod = previousTwi.AlarmMethod;
102 } 124 }
125
126 public void Cleanup()
127 {
128 StatsManager.DeregisterStat(Stat);
129 }
103 } 130 }
104 131
105 /// <summary> 132 /// <summary>
@@ -116,7 +143,7 @@ namespace OpenSim.Framework.Monitoring
116 get { return m_enabled; } 143 get { return m_enabled; }
117 set 144 set
118 { 145 {
119// m_log.DebugFormat("[MEMORY WATCHDOG]: Setting MemoryWatchdog.Enabled to {0}", value); 146 // m_log.DebugFormat("[MEMORY WATCHDOG]: Setting MemoryWatchdog.Enabled to {0}", value);
120 147
121 if (value == m_enabled) 148 if (value == m_enabled)
122 return; 149 return;
@@ -132,9 +159,8 @@ namespace OpenSim.Framework.Monitoring
132 m_watchdogTimer.Enabled = m_enabled; 159 m_watchdogTimer.Enabled = m_enabled;
133 } 160 }
134 } 161 }
135 private static bool m_enabled;
136 162
137 private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 163 private static bool m_enabled;
138 private static Dictionary<int, ThreadWatchdogInfo> m_threads; 164 private static Dictionary<int, ThreadWatchdogInfo> m_threads;
139 private static System.Timers.Timer m_watchdogTimer; 165 private static System.Timers.Timer m_watchdogTimer;
140 166
@@ -155,57 +181,19 @@ namespace OpenSim.Framework.Monitoring
155 } 181 }
156 182
157 /// <summary> 183 /// <summary>
158 /// Start a new thread that is tracked by the watchdog timer. 184 /// Add a thread to the watchdog tracker.
159 /// </summary>
160 /// <param name="start">The method that will be executed in a new thread</param>
161 /// <param name="name">A name to give to the new thread</param>
162 /// <param name="priority">Priority to run the thread at</param>
163 /// <param name="isBackground">True to run this thread as a background thread, otherwise false</param>
164 /// <param name="alarmIfTimeout">Trigger an alarm function is we have timed out</param>
165 /// <returns>The newly created Thread object</returns>
166 public static Thread StartThread(
167 ThreadStart start, string name, ThreadPriority priority, bool isBackground, bool alarmIfTimeout)
168 {
169 return StartThread(start, name, priority, isBackground, alarmIfTimeout, null, DEFAULT_WATCHDOG_TIMEOUT_MS);
170 }
171
172 /// <summary>
173 /// Start a new thread that is tracked by the watchdog timer
174 /// </summary> 185 /// </summary>
175 /// <param name="start">The method that will be executed in a new thread</param> 186 /// <param name="info">Information about the thread.</info>
176 /// <param name="name">A name to give to the new thread</param> 187 /// <param name="info">Name of the thread.</info>
177 /// <param name="priority">Priority to run the thread at</param> 188 /// <param name="log">If true then creation of thread is logged.</param>
178 /// <param name="isBackground">True to run this thread as a background 189 public static void AddThread(ThreadWatchdogInfo info, string name, bool log = true)
179 /// thread, otherwise false</param>
180 /// <param name="alarmIfTimeout">Trigger an alarm function is we have timed out</param>
181 /// <param name="alarmMethod">
182 /// Alarm method to call if alarmIfTimeout is true and there is a timeout.
183 /// Normally, this will just return some useful debugging information.
184 /// </param>
185 /// <param name="timeout">Number of milliseconds to wait until we issue a warning about timeout.</param>
186 /// <returns>The newly created Thread object</returns>
187 public static Thread StartThread(
188 ThreadStart start, string name, ThreadPriority priority, bool isBackground,
189 bool alarmIfTimeout, Func<string> alarmMethod, int timeout)
190 { 190 {
191 Thread thread = new Thread(start); 191 if (log)
192 thread.Name = name; 192 m_log.DebugFormat(
193 thread.Priority = priority; 193 "[WATCHDOG]: Started tracking thread {0}, ID {1}", name, info.Thread.ManagedThreadId);
194 thread.IsBackground = isBackground;
195
196 ThreadWatchdogInfo twi
197 = new ThreadWatchdogInfo(thread, timeout)
198 { AlarmIfTimeout = alarmIfTimeout, AlarmMethod = alarmMethod };
199
200 m_log.DebugFormat(
201 "[WATCHDOG]: Started tracking thread {0}, ID {1}", twi.Thread.Name, twi.Thread.ManagedThreadId);
202 194
203 lock (m_threads) 195 lock (m_threads)
204 m_threads.Add(twi.Thread.ManagedThreadId, twi); 196 m_threads.Add(info.Thread.ManagedThreadId, info);
205
206 thread.Start();
207
208 return thread;
209 } 197 }
210 198
211 /// <summary> 199 /// <summary>
@@ -219,25 +207,28 @@ namespace OpenSim.Framework.Monitoring
219 /// <summary> 207 /// <summary>
220 /// Stops watchdog tracking on the current thread 208 /// Stops watchdog tracking on the current thread
221 /// </summary> 209 /// </summary>
210 /// <param name="log">If true then normal events in thread removal are not logged.</param>
222 /// <returns> 211 /// <returns>
223 /// True if the thread was removed from the list of tracked 212 /// True if the thread was removed from the list of tracked
224 /// threads, otherwise false 213 /// threads, otherwise false
225 /// </returns> 214 /// </returns>
226 public static bool RemoveThread() 215 public static bool RemoveThread(bool log = true)
227 { 216 {
228 return RemoveThread(Thread.CurrentThread.ManagedThreadId); 217 return RemoveThread(Thread.CurrentThread.ManagedThreadId, log);
229 } 218 }
230 219
231 private static bool RemoveThread(int threadID) 220 private static bool RemoveThread(int threadID, bool log = true)
232 { 221 {
233 lock (m_threads) 222 lock (m_threads)
234 { 223 {
235 ThreadWatchdogInfo twi; 224 ThreadWatchdogInfo twi;
236 if (m_threads.TryGetValue(threadID, out twi)) 225 if (m_threads.TryGetValue(threadID, out twi))
237 { 226 {
238 m_log.DebugFormat( 227 if (log)
239 "[WATCHDOG]: Removing thread {0}, ID {1}", twi.Thread.Name, twi.Thread.ManagedThreadId); 228 m_log.DebugFormat(
229 "[WATCHDOG]: Removing thread {0}, ID {1}", twi.Thread.Name, twi.Thread.ManagedThreadId);
240 230
231 twi.Cleanup();
241 m_threads.Remove(threadID); 232 m_threads.Remove(threadID);
242 233
243 return true; 234 return true;
@@ -293,7 +284,7 @@ namespace OpenSim.Framework.Monitoring
293 } 284 }
294 catch { } 285 catch { }
295 } 286 }
296 287
297 /// <summary> 288 /// <summary>
298 /// Get currently watched threads for diagnostic purposes 289 /// Get currently watched threads for diagnostic purposes
299 /// </summary> 290 /// </summary>
@@ -380,6 +371,7 @@ namespace OpenSim.Framework.Monitoring
380 if (MemoryWatchdog.Enabled) 371 if (MemoryWatchdog.Enabled)
381 MemoryWatchdog.Update(); 372 MemoryWatchdog.Update();
382 373
374 ChecksManager.CheckChecks();
383 StatsManager.RecordStats(); 375 StatsManager.RecordStats();
384 376
385 m_watchdogTimer.Start(); 377 m_watchdogTimer.Start();