Watchdog.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Threading;
  30. using log4net;
  31. namespace OpenSim.Framework
  32. {
  33. /// <summary>
  34. /// Manages launching threads and keeping watch over them for timeouts
  35. /// </summary>
  36. public static class Watchdog
  37. {
  38. /// <summary>Timer interval in milliseconds for the watchdog timer</summary>
  39. const double WATCHDOG_INTERVAL_MS = 2500.0d;
  40. /// <summary>Maximum timeout in milliseconds before a thread is considered dead</summary>
  41. const int WATCHDOG_TIMEOUT_MS = 5000;
  42. [System.Diagnostics.DebuggerDisplay("{Thread.Name}")]
  43. private class ThreadWatchdogInfo
  44. {
  45. public Thread Thread;
  46. public int LastTick;
  47. public ThreadWatchdogInfo(Thread thread)
  48. {
  49. Thread = thread;
  50. LastTick = Environment.TickCount & Int32.MaxValue;
  51. }
  52. }
  53. /// <summary>
  54. /// This event is called whenever a tracked thread is stopped or
  55. /// has not called UpdateThread() in time
  56. /// </summary>
  57. /// <param name="thread">The thread that has been identified as dead</param>
  58. /// <param name="lastTick">The last time this thread called UpdateThread()</param>
  59. public delegate void WatchdogTimeout(Thread thread, int lastTick);
  60. /// <summary>This event is called whenever a tracked thread is
  61. /// stopped or has not called UpdateThread() in time</summary>
  62. public static event WatchdogTimeout OnWatchdogTimeout;
  63. private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  64. private static Dictionary<int, ThreadWatchdogInfo> m_threads;
  65. private static System.Timers.Timer m_watchdogTimer;
  66. static Watchdog()
  67. {
  68. m_threads = new Dictionary<int, ThreadWatchdogInfo>();
  69. m_watchdogTimer = new System.Timers.Timer(WATCHDOG_INTERVAL_MS);
  70. m_watchdogTimer.AutoReset = false;
  71. m_watchdogTimer.Elapsed += WatchdogTimerElapsed;
  72. m_watchdogTimer.Start();
  73. }
  74. /// <summary>
  75. /// Start a new thread that is tracked by the watchdog timer
  76. /// </summary>
  77. /// <param name="start">The method that will be executed in a new thread</param>
  78. /// <param name="name">A name to give to the new thread</param>
  79. /// <param name="priority">Priority to run the thread at</param>
  80. /// <param name="isBackground">True to run this thread as a background
  81. /// thread, otherwise false</param>
  82. /// <returns>The newly created Thread object</returns>
  83. public static Thread StartThread(ThreadStart start, string name, ThreadPriority priority, bool isBackground)
  84. {
  85. Thread thread = new Thread(start);
  86. thread.Name = name;
  87. thread.Priority = priority;
  88. thread.IsBackground = isBackground;
  89. thread.Start();
  90. return thread;
  91. }
  92. /// <summary>
  93. /// Marks the current thread as alive
  94. /// </summary>
  95. public static void UpdateThread()
  96. {
  97. UpdateThread(Thread.CurrentThread.ManagedThreadId);
  98. }
  99. /// <summary>
  100. /// Stops watchdog tracking on the current thread
  101. /// </summary>
  102. /// <returns>True if the thread was removed from the list of tracked
  103. /// threads, otherwise false</returns>
  104. public static bool RemoveThread()
  105. {
  106. return RemoveThread(Thread.CurrentThread.ManagedThreadId);
  107. }
  108. private static void AddThread(ThreadWatchdogInfo threadInfo)
  109. {
  110. m_log.Debug("[WATCHDOG]: Started tracking thread \"" + threadInfo.Thread.Name + "\" (ID " + threadInfo.Thread.ManagedThreadId + ")");
  111. lock (m_threads)
  112. m_threads.Add(threadInfo.Thread.ManagedThreadId, threadInfo);
  113. }
  114. private static bool RemoveThread(int threadID)
  115. {
  116. lock (m_threads)
  117. return m_threads.Remove(threadID);
  118. }
  119. private static void UpdateThread(int threadID)
  120. {
  121. ThreadWatchdogInfo threadInfo;
  122. // Although TryGetValue is not a thread safe operation, we use a try/catch here instead
  123. // of a lock for speed. Adding/removing threads is a very rare operation compared to
  124. // UpdateThread(), and a single UpdateThread() failure here and there won't break
  125. // anything
  126. try
  127. {
  128. if (m_threads.TryGetValue(threadID, out threadInfo))
  129. threadInfo.LastTick = Environment.TickCount & Int32.MaxValue;
  130. else
  131. AddThread(new ThreadWatchdogInfo(Thread.CurrentThread));
  132. }
  133. catch { }
  134. }
  135. private static void WatchdogTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
  136. {
  137. WatchdogTimeout callback = OnWatchdogTimeout;
  138. if (callback != null)
  139. {
  140. ThreadWatchdogInfo timedOut = null;
  141. lock (m_threads)
  142. {
  143. int now = Environment.TickCount & Int32.MaxValue;
  144. foreach (ThreadWatchdogInfo threadInfo in m_threads.Values)
  145. {
  146. if (threadInfo.Thread.ThreadState == ThreadState.Stopped || now - threadInfo.LastTick >= WATCHDOG_TIMEOUT_MS)
  147. {
  148. timedOut = threadInfo;
  149. m_threads.Remove(threadInfo.Thread.ManagedThreadId);
  150. break;
  151. }
  152. }
  153. }
  154. if (timedOut != null)
  155. callback(timedOut.Thread, timedOut.LastTick);
  156. }
  157. m_watchdogTimer.Start();
  158. }
  159. }
  160. }