MaintenanceThread.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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;
  29. using System.Reflection;
  30. using System.Threading;
  31. using log4net;
  32. using OpenSim.Framework;
  33. namespace OpenSim.Region.ScriptEngine.DotNetEngine
  34. {
  35. /// <summary>
  36. /// This class does maintenance on script engine.
  37. /// </summary>
  38. public class MaintenanceThread
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. //public ScriptEngine m_ScriptEngine;
  42. private int MaintenanceLoopms;
  43. private int MaintenanceLoopTicks_ScriptLoadUnload;
  44. private int MaintenanceLoopTicks_Other;
  45. public MaintenanceThread()
  46. {
  47. //m_ScriptEngine = _ScriptEngine;
  48. ReadConfig();
  49. // Start maintenance thread
  50. StartMaintenanceThread();
  51. }
  52. ~MaintenanceThread()
  53. {
  54. StopMaintenanceThread();
  55. }
  56. public void ReadConfig()
  57. {
  58. // Bad hack, but we need a m_ScriptEngine :)
  59. lock (ScriptEngine.ScriptEngines)
  60. {
  61. foreach (ScriptEngine m_ScriptEngine in ScriptEngine.ScriptEngines)
  62. {
  63. MaintenanceLoopms = m_ScriptEngine.ScriptConfigSource.GetInt("MaintenanceLoopms", 50);
  64. MaintenanceLoopTicks_ScriptLoadUnload =
  65. m_ScriptEngine.ScriptConfigSource.GetInt("MaintenanceLoopTicks_ScriptLoadUnload", 1);
  66. MaintenanceLoopTicks_Other =
  67. m_ScriptEngine.ScriptConfigSource.GetInt("MaintenanceLoopTicks_Other", 10);
  68. return;
  69. }
  70. }
  71. }
  72. #region " Maintenance thread "
  73. /// <summary>
  74. /// Maintenance thread. Enforcing max execution time for example.
  75. /// </summary>
  76. public Thread MaintenanceThreadThread;
  77. /// <summary>
  78. /// Starts maintenance thread
  79. /// </summary>
  80. private void StartMaintenanceThread()
  81. {
  82. if (MaintenanceThreadThread == null)
  83. {
  84. MaintenanceThreadThread = new Thread(MaintenanceLoop);
  85. MaintenanceThreadThread.Name = "ScriptMaintenanceThread";
  86. MaintenanceThreadThread.IsBackground = true;
  87. MaintenanceThreadThread.Start();
  88. ThreadTracker.Add(MaintenanceThreadThread);
  89. }
  90. }
  91. /// <summary>
  92. /// Stops maintenance thread
  93. /// </summary>
  94. private void StopMaintenanceThread()
  95. {
  96. #if DEBUG
  97. //m_log.Debug("[" + m_ScriptEngine.ScriptEngineName + "]: StopMaintenanceThread() called");
  98. #endif
  99. //PleaseShutdown = true;
  100. Thread.Sleep(100);
  101. try
  102. {
  103. if (MaintenanceThreadThread != null && MaintenanceThreadThread.IsAlive)
  104. {
  105. MaintenanceThreadThread.Abort();
  106. }
  107. }
  108. catch (Exception)
  109. {
  110. //m_log.Error("[" + m_ScriptEngine.ScriptEngineName + "]: Exception stopping maintenence thread: " + ex.ToString());
  111. }
  112. }
  113. // private ScriptEngine lastScriptEngine; // Keep track of what ScriptEngine instance we are at so we can give exception
  114. /// <summary>
  115. /// A thread should run in this loop and check all running scripts
  116. /// </summary>
  117. public void MaintenanceLoop()
  118. {
  119. //if (m_ScriptEngine.m_EventQueueManager.maxFunctionExecutionTimens < MaintenanceLoopms)
  120. // m_log.Warn("[" + m_ScriptEngine.ScriptEngineName + "]: " +
  121. // "Configuration error: MaxEventExecutionTimeMs is less than MaintenanceLoopms. The Maintenance Loop will only check scripts once per run.");
  122. long Last_maxFunctionExecutionTimens = 0; // DateTime.Now.Ticks;
  123. long Last_ReReadConfigFilens = DateTime.Now.Ticks;
  124. int MaintenanceLoopTicks_ScriptLoadUnload_Count = 0;
  125. int MaintenanceLoopTicks_Other_Count = 0;
  126. bool MaintenanceLoopTicks_ScriptLoadUnload_ResetCount = false;
  127. bool MaintenanceLoopTicks_Other_ResetCount = false;
  128. while (true)
  129. {
  130. try
  131. {
  132. while (true)
  133. {
  134. Thread.Sleep(MaintenanceLoopms); // Sleep before next pass
  135. // Reset counters?
  136. if (MaintenanceLoopTicks_ScriptLoadUnload_ResetCount)
  137. {
  138. MaintenanceLoopTicks_ScriptLoadUnload_ResetCount = false;
  139. MaintenanceLoopTicks_ScriptLoadUnload_Count = 0;
  140. }
  141. if (MaintenanceLoopTicks_Other_ResetCount)
  142. {
  143. MaintenanceLoopTicks_Other_ResetCount = false;
  144. MaintenanceLoopTicks_Other_Count = 0;
  145. }
  146. // Increase our counters
  147. MaintenanceLoopTicks_ScriptLoadUnload_Count++;
  148. MaintenanceLoopTicks_Other_Count++;
  149. //lock (ScriptEngine.ScriptEngines)
  150. //{
  151. foreach (ScriptEngine m_ScriptEngine in new ArrayList(ScriptEngine.ScriptEngines))
  152. {
  153. // lastScriptEngine = m_ScriptEngine;
  154. // Re-reading config every x seconds
  155. if (MaintenanceLoopTicks_Other_Count >= MaintenanceLoopTicks_Other)
  156. {
  157. MaintenanceLoopTicks_Other_ResetCount = true;
  158. if (m_ScriptEngine.RefreshConfigFilens > 0)
  159. {
  160. // Check if its time to re-read config
  161. if (DateTime.Now.Ticks - Last_ReReadConfigFilens >
  162. m_ScriptEngine.RefreshConfigFilens)
  163. {
  164. //m_log.Debug("Time passed: " + (DateTime.Now.Ticks - Last_ReReadConfigFilens) + ">" + m_ScriptEngine.RefreshConfigFilens);
  165. // Its time to re-read config file
  166. m_ScriptEngine.ReadConfig();
  167. Last_ReReadConfigFilens = DateTime.Now.Ticks; // Reset time
  168. }
  169. // Adjust number of running script threads if not correct
  170. if (m_ScriptEngine.m_EventQueueManager != null)
  171. m_ScriptEngine.m_EventQueueManager.AdjustNumberOfScriptThreads();
  172. // Check if any script has exceeded its max execution time
  173. if (EventQueueManager.EnforceMaxExecutionTime)
  174. {
  175. // We are enforcing execution time
  176. if (DateTime.Now.Ticks - Last_maxFunctionExecutionTimens >
  177. EventQueueManager.maxFunctionExecutionTimens)
  178. {
  179. // Its time to check again
  180. m_ScriptEngine.m_EventQueueManager.CheckScriptMaxExecTime(); // Do check
  181. Last_maxFunctionExecutionTimens = DateTime.Now.Ticks; // Reset time
  182. }
  183. }
  184. }
  185. }
  186. if (MaintenanceLoopTicks_ScriptLoadUnload_Count >= MaintenanceLoopTicks_ScriptLoadUnload)
  187. {
  188. MaintenanceLoopTicks_ScriptLoadUnload_ResetCount = true;
  189. // LOAD / UNLOAD SCRIPTS
  190. if (m_ScriptEngine.m_ScriptManager != null)
  191. m_ScriptEngine.m_ScriptManager.DoScriptLoadUnload();
  192. }
  193. }
  194. //}
  195. }
  196. }
  197. catch(ThreadAbortException)
  198. {
  199. m_log.Error("Thread aborted in MaintenanceLoopThread. If this is during shutdown, please ignore");
  200. }
  201. catch (Exception ex)
  202. {
  203. m_log.ErrorFormat("Exception in MaintenanceLoopThread. Thread will recover after 5 sec throttle. Exception: {0}", ex.ToString());
  204. }
  205. }
  206. }
  207. #endregion
  208. ///// <summary>
  209. ///// If set to true then threads and stuff should try to make a graceful exit
  210. ///// </summary>
  211. //public bool PleaseShutdown
  212. //{
  213. // get { return _PleaseShutdown; }
  214. // set { _PleaseShutdown = value; }
  215. //}
  216. //private bool _PleaseShutdown = false;
  217. }
  218. }