diff options
Diffstat (limited to 'OpenSim/Framework/Monitoring/WorkManager.cs')
-rw-r--r-- | OpenSim/Framework/Monitoring/WorkManager.cs | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/OpenSim/Framework/Monitoring/WorkManager.cs b/OpenSim/Framework/Monitoring/WorkManager.cs new file mode 100644 index 0000000..9d0eefc --- /dev/null +++ b/OpenSim/Framework/Monitoring/WorkManager.cs | |||
@@ -0,0 +1,212 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Reflection; | ||
30 | using System.Threading; | ||
31 | using log4net; | ||
32 | |||
33 | namespace OpenSim.Framework.Monitoring | ||
34 | { | ||
35 | /// <summary> | ||
36 | /// Manages various work items in the simulator. | ||
37 | /// </summary> | ||
38 | /// <remarks> | ||
39 | /// Currently, here work can be started | ||
40 | /// * As a long-running and monitored thread. | ||
41 | /// * In a thread that will never timeout but where the job is expected to eventually complete. | ||
42 | /// * In a threadpool thread that will timeout if it takes a very long time to complete (> 10 mins). | ||
43 | /// * As a job which will be run in a single-threaded job engine. Such jobs must not incorporate delays (sleeps, | ||
44 | /// network waits, etc.). | ||
45 | /// | ||
46 | /// This is an evolving approach to better manage the work that OpenSimulator is asked to do from a very diverse | ||
47 | /// range of sources (client actions, incoming network, outgoing network calls, etc.). | ||
48 | /// | ||
49 | /// Util.FireAndForget is still available to insert jobs in the threadpool, though this is equivalent to | ||
50 | /// WorkManager.RunInThreadPool(). | ||
51 | /// </remarks> | ||
52 | public static class WorkManager | ||
53 | { | ||
54 | private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
55 | |||
56 | public static JobEngine JobEngine { get; private set; } | ||
57 | |||
58 | static WorkManager() | ||
59 | { | ||
60 | JobEngine = new JobEngine(); | ||
61 | } | ||
62 | |||
63 | /// <summary> | ||
64 | /// Start a new long-lived thread. | ||
65 | /// </summary> | ||
66 | /// <param name="start">The method that will be executed in a new thread</param> | ||
67 | /// <param name="name">A name to give to the new thread</param> | ||
68 | /// <param name="priority">Priority to run the thread at</param> | ||
69 | /// <param name="isBackground">True to run this thread as a background thread, otherwise false</param> | ||
70 | /// <param name="alarmIfTimeout">Trigger an alarm function is we have timed out</param> | ||
71 | /// <param name="log">If true then creation of thread is logged.</param> | ||
72 | /// <returns>The newly created Thread object</returns> | ||
73 | public static Thread StartThread( | ||
74 | ThreadStart start, string name, ThreadPriority priority, bool isBackground, bool alarmIfTimeout, bool log = true) | ||
75 | { | ||
76 | return StartThread(start, name, priority, isBackground, alarmIfTimeout, null, Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS, log); | ||
77 | } | ||
78 | |||
79 | /// <summary> | ||
80 | /// Start a new thread that is tracked by the watchdog | ||
81 | /// </summary> | ||
82 | /// <param name="start">The method that will be executed in a new thread</param> | ||
83 | /// <param name="name">A name to give to the new thread</param> | ||
84 | /// <param name="priority">Priority to run the thread at</param> | ||
85 | /// <param name="isBackground">True to run this thread as a background | ||
86 | /// thread, otherwise false</param> | ||
87 | /// <param name="alarmIfTimeout">Trigger an alarm function is we have timed out</param> | ||
88 | /// <param name="alarmMethod"> | ||
89 | /// Alarm method to call if alarmIfTimeout is true and there is a timeout. | ||
90 | /// Normally, this will just return some useful debugging information. | ||
91 | /// </param> | ||
92 | /// <param name="timeout">Number of milliseconds to wait until we issue a warning about timeout.</param> | ||
93 | /// <param name="log">If true then creation of thread is logged.</param> | ||
94 | /// <returns>The newly created Thread object</returns> | ||
95 | public static Thread StartThread( | ||
96 | ThreadStart start, string name, ThreadPriority priority, bool isBackground, | ||
97 | bool alarmIfTimeout, Func<string> alarmMethod, int timeout, bool log = true) | ||
98 | { | ||
99 | Thread thread = new Thread(start); | ||
100 | thread.Priority = priority; | ||
101 | thread.IsBackground = isBackground; | ||
102 | |||
103 | Watchdog.ThreadWatchdogInfo twi | ||
104 | = new Watchdog.ThreadWatchdogInfo(thread, timeout, name) | ||
105 | { AlarmIfTimeout = alarmIfTimeout, AlarmMethod = alarmMethod }; | ||
106 | |||
107 | Watchdog.AddThread(twi, name, log:log); | ||
108 | |||
109 | thread.Start(); | ||
110 | thread.Name = name; | ||
111 | |||
112 | return thread; | ||
113 | } | ||
114 | |||
115 | /// <summary> | ||
116 | /// Run the callback in a new thread immediately. If the thread exits with an exception log it but do | ||
117 | /// not propogate it. | ||
118 | /// </summary> | ||
119 | /// <param name="callback">Code for the thread to execute.</param> | ||
120 | /// <param name="obj">Object to pass to the thread.</param> | ||
121 | /// <param name="name">Name of the thread</param> | ||
122 | public static void RunInThread(WaitCallback callback, object obj, string name, bool log = false) | ||
123 | { | ||
124 | if (Util.FireAndForgetMethod == FireAndForgetMethod.RegressionTest) | ||
125 | { | ||
126 | Culture.SetCurrentCulture(); | ||
127 | callback(obj); | ||
128 | return; | ||
129 | } | ||
130 | |||
131 | ThreadStart ts = new ThreadStart(delegate() | ||
132 | { | ||
133 | try | ||
134 | { | ||
135 | Culture.SetCurrentCulture(); | ||
136 | callback(obj); | ||
137 | Watchdog.RemoveThread(log:false); | ||
138 | } | ||
139 | catch (Exception e) | ||
140 | { | ||
141 | m_log.Error(string.Format("[WATCHDOG]: Exception in thread {0}.", name), e); | ||
142 | } | ||
143 | }); | ||
144 | |||
145 | StartThread(ts, name, ThreadPriority.Normal, true, false, log:log); | ||
146 | } | ||
147 | |||
148 | /// <summary> | ||
149 | /// Run the callback via a threadpool thread. | ||
150 | /// </summary> | ||
151 | /// <remarks> | ||
152 | /// Such jobs may run after some delay but must always complete. | ||
153 | /// </remarks> | ||
154 | /// <param name="callback"></param> | ||
155 | /// <param name="obj"></param> | ||
156 | /// <param name="name">The name of the job. This is used in monitoring and debugging.</param> | ||
157 | public static void RunInThreadPool(System.Threading.WaitCallback callback, object obj, string name) | ||
158 | { | ||
159 | Util.FireAndForget(callback, obj, name); | ||
160 | } | ||
161 | |||
162 | /// <summary> | ||
163 | /// Run a job. | ||
164 | /// </summary> | ||
165 | /// <remarks> | ||
166 | /// This differs from direct scheduling (e.g. Util.FireAndForget) in that a job can be run in the job | ||
167 | /// engine if it is running, where all jobs are currently performed in sequence on a single thread. This is | ||
168 | /// to prevent observed overload and server freeze problems when there are hundreds of connections which all attempt to | ||
169 | /// perform work at once (e.g. in conference situations). With lower numbers of connections, the small | ||
170 | /// delay in performing jobs in sequence rather than concurrently has not been notiecable in testing, though a future more | ||
171 | /// sophisticated implementation could perform jobs concurrently when the server is under low load. | ||
172 | /// | ||
173 | /// However, be advised that some callers of this function rely on all jobs being performed in sequence if any | ||
174 | /// jobs are performed in sequence (i.e. if jobengine is active or not). Therefore, expanding the jobengine | ||
175 | /// beyond a single thread will require considerable thought. | ||
176 | /// | ||
177 | /// Also, any jobs submitted must be guaranteed to complete within a reasonable timeframe (e.g. they cannot | ||
178 | /// incorporate a network delay with a long timeout). At the moment, work that could suffer such issues | ||
179 | /// should still be run directly with RunInThread(), Util.FireAndForget(), etc. This is another area where | ||
180 | /// the job engine could be improved and so CPU utilization improved by better management of concurrency within | ||
181 | /// OpenSimulator. | ||
182 | /// </remarks> | ||
183 | /// <param name="jobType">General classification for the job (e.g. "RezAttachments").</param> | ||
184 | /// <param name="callback">Callback for job.</param> | ||
185 | /// <param name="obj">Object to pass to callback when run</param> | ||
186 | /// <param name="name">Specific name of job (e.g. "RezAttachments for Joe Bloggs"</param> | ||
187 | /// <param name="canRunInThisThread">If set to true then the job may be run in ths calling thread.</param> | ||
188 | /// <param name="mustNotTimeout">If the true then the job must never timeout.</param> | ||
189 | /// <param name="log">If set to true then extra logging is performed.</param> | ||
190 | public static void RunJob( | ||
191 | string jobType, WaitCallback callback, object obj, string name, | ||
192 | bool canRunInThisThread = false, bool mustNotTimeout = false, | ||
193 | bool log = false) | ||
194 | { | ||
195 | if (Util.FireAndForgetMethod == FireAndForgetMethod.RegressionTest) | ||
196 | { | ||
197 | Culture.SetCurrentCulture(); | ||
198 | callback(obj); | ||
199 | return; | ||
200 | } | ||
201 | |||
202 | if (JobEngine.IsRunning) | ||
203 | JobEngine.QueueRequest(name, callback, obj); | ||
204 | else if (canRunInThisThread) | ||
205 | callback(obj); | ||
206 | else if (mustNotTimeout) | ||
207 | RunInThread(callback, obj, name, log); | ||
208 | else | ||
209 | Util.FireAndForget(callback, obj, name); | ||
210 | } | ||
211 | } | ||
212 | } \ No newline at end of file | ||