diff options
Diffstat (limited to '')
-rw-r--r-- | ThirdParty/SmartThreadPool/STPPerformanceCounter.cs | 352 |
1 files changed, 352 insertions, 0 deletions
diff --git a/ThirdParty/SmartThreadPool/STPPerformanceCounter.cs b/ThirdParty/SmartThreadPool/STPPerformanceCounter.cs new file mode 100644 index 0000000..be70aea --- /dev/null +++ b/ThirdParty/SmartThreadPool/STPPerformanceCounter.cs | |||
@@ -0,0 +1,352 @@ | |||
1 | using System; | ||
2 | using System.Diagnostics; | ||
3 | |||
4 | namespace Amib.Threading.Internal | ||
5 | { | ||
6 | internal enum STPPerformanceCounterType | ||
7 | { | ||
8 | // Fields | ||
9 | ActiveThreads = 0, | ||
10 | InUseThreads = 1, | ||
11 | OverheadThreads = 2, | ||
12 | OverheadThreadsPercent = 3, | ||
13 | OverheadThreadsPercentBase = 4, | ||
14 | |||
15 | WorkItems = 5, | ||
16 | WorkItemsInQueue = 6, | ||
17 | WorkItemsProcessed = 7, | ||
18 | |||
19 | WorkItemsQueuedPerSecond = 8, | ||
20 | WorkItemsProcessedPerSecond = 9, | ||
21 | |||
22 | AvgWorkItemWaitTime = 10, | ||
23 | AvgWorkItemWaitTimeBase = 11, | ||
24 | |||
25 | AvgWorkItemProcessTime = 12, | ||
26 | AvgWorkItemProcessTimeBase = 13, | ||
27 | |||
28 | WorkItemsGroups = 14, | ||
29 | |||
30 | LastCounter = 14, | ||
31 | } | ||
32 | |||
33 | |||
34 | /// <summary> | ||
35 | /// Summary description for STPPerformanceCounter. | ||
36 | /// </summary> | ||
37 | internal class STPPerformanceCounter | ||
38 | { | ||
39 | // Fields | ||
40 | private PerformanceCounterType _pcType; | ||
41 | protected string _counterHelp; | ||
42 | protected string _counterName; | ||
43 | |||
44 | // Methods | ||
45 | public STPPerformanceCounter( | ||
46 | string counterName, | ||
47 | string counterHelp, | ||
48 | PerformanceCounterType pcType) | ||
49 | { | ||
50 | this._counterName = counterName; | ||
51 | this._counterHelp = counterHelp; | ||
52 | this._pcType = pcType; | ||
53 | } | ||
54 | |||
55 | public void AddCounterToCollection(CounterCreationDataCollection counterData) | ||
56 | { | ||
57 | CounterCreationData counterCreationData = new CounterCreationData( | ||
58 | _counterName, | ||
59 | _counterHelp, | ||
60 | _pcType); | ||
61 | |||
62 | counterData.Add(counterCreationData); | ||
63 | } | ||
64 | |||
65 | // Properties | ||
66 | public string Name | ||
67 | { | ||
68 | get | ||
69 | { | ||
70 | return _counterName; | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | |||
75 | internal class STPPerformanceCounters | ||
76 | { | ||
77 | // Fields | ||
78 | internal STPPerformanceCounter[] _stpPerformanceCounters; | ||
79 | private static STPPerformanceCounters _instance; | ||
80 | internal const string _stpCategoryHelp = "SmartThreadPool performance counters"; | ||
81 | internal const string _stpCategoryName = "SmartThreadPool"; | ||
82 | |||
83 | // Methods | ||
84 | static STPPerformanceCounters() | ||
85 | { | ||
86 | _instance = new STPPerformanceCounters(); | ||
87 | } | ||
88 | |||
89 | private STPPerformanceCounters() | ||
90 | { | ||
91 | STPPerformanceCounter[] stpPerformanceCounters = new STPPerformanceCounter[] | ||
92 | { | ||
93 | new STPPerformanceCounter("Active threads", "The current number of available in the thread pool.", PerformanceCounterType.NumberOfItems32), | ||
94 | new STPPerformanceCounter("In use threads", "The current number of threads that execute a work item.", PerformanceCounterType.NumberOfItems32), | ||
95 | new STPPerformanceCounter("Overhead threads", "The current number of threads that are active, but are not in use.", PerformanceCounterType.NumberOfItems32), | ||
96 | new STPPerformanceCounter("% overhead threads", "The current number of threads that are active, but are not in use in percents.", PerformanceCounterType.RawFraction), | ||
97 | new STPPerformanceCounter("% overhead threads base", "The current number of threads that are active, but are not in use in percents.", PerformanceCounterType.RawBase), | ||
98 | |||
99 | new STPPerformanceCounter("Work Items", "The number of work items in the Smart Thread Pool. Both queued and processed.", PerformanceCounterType.NumberOfItems32), | ||
100 | new STPPerformanceCounter("Work Items in queue", "The current number of work items in the queue", PerformanceCounterType.NumberOfItems32), | ||
101 | new STPPerformanceCounter("Work Items processed", "The number of work items already processed", PerformanceCounterType.NumberOfItems32), | ||
102 | |||
103 | new STPPerformanceCounter("Work Items queued/sec", "The number of work items queued per second", PerformanceCounterType.RateOfCountsPerSecond32), | ||
104 | new STPPerformanceCounter("Work Items processed/sec", "The number of work items processed per second", PerformanceCounterType.RateOfCountsPerSecond32), | ||
105 | |||
106 | new STPPerformanceCounter("Avg. Work Item wait time/sec", "The average time a work item supends in the queue waiting for its turn to execute.", PerformanceCounterType.AverageCount64), | ||
107 | new STPPerformanceCounter("Avg. Work Item wait time base", "The average time a work item supends in the queue waiting for its turn to execute.", PerformanceCounterType.AverageBase), | ||
108 | |||
109 | new STPPerformanceCounter("Avg. Work Item process time/sec", "The average time it takes to process a work item.", PerformanceCounterType.AverageCount64), | ||
110 | new STPPerformanceCounter("Avg. Work Item process time base", "The average time it takes to process a work item.", PerformanceCounterType.AverageBase), | ||
111 | |||
112 | new STPPerformanceCounter("Work Items Groups", "The current number of work item groups associated with the Smart Thread Pool.", PerformanceCounterType.NumberOfItems32), | ||
113 | }; | ||
114 | |||
115 | _stpPerformanceCounters = stpPerformanceCounters; | ||
116 | SetupCategory(); | ||
117 | } | ||
118 | |||
119 | private void SetupCategory() | ||
120 | { | ||
121 | if (!PerformanceCounterCategory.Exists(_stpCategoryName)) | ||
122 | { | ||
123 | CounterCreationDataCollection counters = new CounterCreationDataCollection(); | ||
124 | |||
125 | for (int i = 0; i < _stpPerformanceCounters.Length; i++) | ||
126 | { | ||
127 | _stpPerformanceCounters[i].AddCounterToCollection(counters); | ||
128 | } | ||
129 | |||
130 | |||
131 | // *********** Remark for .NET 2.0 *********** | ||
132 | // If you are here, it means you got the warning that this overload | ||
133 | // of the method is deprecated in .NET 2.0. To use the correct | ||
134 | // method overload, uncomment the third argument of the method. | ||
135 | PerformanceCounterCategory.Create( | ||
136 | _stpCategoryName, | ||
137 | _stpCategoryHelp, | ||
138 | //PerformanceCounterCategoryType.MultiInstance, | ||
139 | counters); | ||
140 | |||
141 | } | ||
142 | } | ||
143 | |||
144 | // Properties | ||
145 | public static STPPerformanceCounters Instance | ||
146 | { | ||
147 | get | ||
148 | { | ||
149 | return _instance; | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | |||
154 | internal class STPInstancePerformanceCounter : IDisposable | ||
155 | { | ||
156 | // Fields | ||
157 | private PerformanceCounter _pcs; | ||
158 | |||
159 | // Methods | ||
160 | protected STPInstancePerformanceCounter() | ||
161 | { | ||
162 | } | ||
163 | |||
164 | public STPInstancePerformanceCounter( | ||
165 | string instance, | ||
166 | STPPerformanceCounterType spcType) | ||
167 | { | ||
168 | STPPerformanceCounters counters = STPPerformanceCounters.Instance; | ||
169 | _pcs = new PerformanceCounter( | ||
170 | STPPerformanceCounters._stpCategoryName, | ||
171 | counters._stpPerformanceCounters[(int) spcType].Name, | ||
172 | instance, | ||
173 | false); | ||
174 | _pcs.RawValue = _pcs.RawValue; | ||
175 | } | ||
176 | |||
177 | ~STPInstancePerformanceCounter() | ||
178 | { | ||
179 | Close(); | ||
180 | } | ||
181 | |||
182 | public void Close() | ||
183 | { | ||
184 | if (_pcs != null) | ||
185 | { | ||
186 | _pcs.RemoveInstance(); | ||
187 | _pcs.Close(); | ||
188 | _pcs = null; | ||
189 | } | ||
190 | } | ||
191 | |||
192 | public void Dispose() | ||
193 | { | ||
194 | Close(); | ||
195 | GC.SuppressFinalize(this); | ||
196 | } | ||
197 | |||
198 | public virtual void Increment() | ||
199 | { | ||
200 | _pcs.Increment(); | ||
201 | } | ||
202 | |||
203 | public virtual void IncrementBy(long val) | ||
204 | { | ||
205 | _pcs.IncrementBy(val); | ||
206 | } | ||
207 | |||
208 | public virtual void Set(long val) | ||
209 | { | ||
210 | _pcs.RawValue = val; | ||
211 | } | ||
212 | } | ||
213 | |||
214 | internal class STPInstanceNullPerformanceCounter : STPInstancePerformanceCounter | ||
215 | { | ||
216 | // Methods | ||
217 | public STPInstanceNullPerformanceCounter() {} | ||
218 | public override void Increment() {} | ||
219 | public override void IncrementBy(long value) {} | ||
220 | public override void Set(long val) {} | ||
221 | } | ||
222 | |||
223 | internal interface ISTPInstancePerformanceCounters : IDisposable | ||
224 | { | ||
225 | void Close(); | ||
226 | void SampleThreads(long activeThreads, long inUseThreads); | ||
227 | void SampleWorkItems(long workItemsQueued, long workItemsProcessed); | ||
228 | void SampleWorkItemsWaitTime(TimeSpan workItemWaitTime); | ||
229 | void SampleWorkItemsProcessTime(TimeSpan workItemProcessTime); | ||
230 | } | ||
231 | |||
232 | |||
233 | internal class STPInstancePerformanceCounters : ISTPInstancePerformanceCounters, IDisposable | ||
234 | { | ||
235 | // Fields | ||
236 | private STPInstancePerformanceCounter[] _pcs; | ||
237 | private static STPInstancePerformanceCounter _stpInstanceNullPerformanceCounter; | ||
238 | |||
239 | // Methods | ||
240 | static STPInstancePerformanceCounters() | ||
241 | { | ||
242 | _stpInstanceNullPerformanceCounter = new STPInstanceNullPerformanceCounter(); | ||
243 | } | ||
244 | |||
245 | public STPInstancePerformanceCounters(string instance) | ||
246 | { | ||
247 | _pcs = new STPInstancePerformanceCounter[(int)STPPerformanceCounterType.LastCounter]; | ||
248 | STPPerformanceCounters counters = STPPerformanceCounters.Instance; | ||
249 | for (int i = 0; i < _pcs.Length; i++) | ||
250 | { | ||
251 | if (instance != null) | ||
252 | { | ||
253 | _pcs[i] = new STPInstancePerformanceCounter( | ||
254 | instance, | ||
255 | (STPPerformanceCounterType) i); | ||
256 | } | ||
257 | else | ||
258 | { | ||
259 | _pcs[i] = _stpInstanceNullPerformanceCounter; | ||
260 | } | ||
261 | } | ||
262 | } | ||
263 | |||
264 | |||
265 | public void Close() | ||
266 | { | ||
267 | if (null != _pcs) | ||
268 | { | ||
269 | for (int i = 0; i < _pcs.Length; i++) | ||
270 | { | ||
271 | if (null != _pcs[i]) | ||
272 | { | ||
273 | _pcs[i].Close(); | ||
274 | } | ||
275 | } | ||
276 | _pcs = null; | ||
277 | } | ||
278 | } | ||
279 | |||
280 | ~STPInstancePerformanceCounters() | ||
281 | { | ||
282 | Close(); | ||
283 | } | ||
284 | |||
285 | public void Dispose() | ||
286 | { | ||
287 | Close(); | ||
288 | GC.SuppressFinalize(this); | ||
289 | } | ||
290 | |||
291 | private STPInstancePerformanceCounter GetCounter(STPPerformanceCounterType spcType) | ||
292 | { | ||
293 | return _pcs[(int) spcType]; | ||
294 | } | ||
295 | |||
296 | public void SampleThreads(long activeThreads, long inUseThreads) | ||
297 | { | ||
298 | GetCounter(STPPerformanceCounterType.ActiveThreads).Set(activeThreads); | ||
299 | GetCounter(STPPerformanceCounterType.InUseThreads).Set(inUseThreads); | ||
300 | GetCounter(STPPerformanceCounterType.OverheadThreads).Set(activeThreads-inUseThreads); | ||
301 | |||
302 | GetCounter(STPPerformanceCounterType.OverheadThreadsPercentBase).Set(activeThreads-inUseThreads); | ||
303 | GetCounter(STPPerformanceCounterType.OverheadThreadsPercent).Set(inUseThreads); | ||
304 | } | ||
305 | |||
306 | public void SampleWorkItems(long workItemsQueued, long workItemsProcessed) | ||
307 | { | ||
308 | GetCounter(STPPerformanceCounterType.WorkItems).Set(workItemsQueued+workItemsProcessed); | ||
309 | GetCounter(STPPerformanceCounterType.WorkItemsInQueue).Set(workItemsQueued); | ||
310 | GetCounter(STPPerformanceCounterType.WorkItemsProcessed).Set(workItemsProcessed); | ||
311 | |||
312 | GetCounter(STPPerformanceCounterType.WorkItemsQueuedPerSecond).Set(workItemsQueued); | ||
313 | GetCounter(STPPerformanceCounterType.WorkItemsProcessedPerSecond).Set(workItemsProcessed); | ||
314 | } | ||
315 | |||
316 | public void SampleWorkItemsWaitTime(TimeSpan workItemWaitTime) | ||
317 | { | ||
318 | GetCounter(STPPerformanceCounterType.AvgWorkItemWaitTime).IncrementBy((long)workItemWaitTime.TotalMilliseconds); | ||
319 | GetCounter(STPPerformanceCounterType.AvgWorkItemWaitTimeBase).Increment(); | ||
320 | } | ||
321 | |||
322 | public void SampleWorkItemsProcessTime(TimeSpan workItemProcessTime) | ||
323 | { | ||
324 | GetCounter(STPPerformanceCounterType.AvgWorkItemProcessTime).IncrementBy((long)workItemProcessTime.TotalMilliseconds); | ||
325 | GetCounter(STPPerformanceCounterType.AvgWorkItemProcessTimeBase).Increment(); | ||
326 | } | ||
327 | } | ||
328 | |||
329 | internal class NullSTPInstancePerformanceCounters : ISTPInstancePerformanceCounters, IDisposable | ||
330 | { | ||
331 | static NullSTPInstancePerformanceCounters() | ||
332 | { | ||
333 | } | ||
334 | |||
335 | private static NullSTPInstancePerformanceCounters _instance = new NullSTPInstancePerformanceCounters(null); | ||
336 | |||
337 | public static NullSTPInstancePerformanceCounters Instance | ||
338 | { | ||
339 | get { return _instance; } | ||
340 | } | ||
341 | |||
342 | public NullSTPInstancePerformanceCounters(string instance) {} | ||
343 | public void Close() {} | ||
344 | public void Dispose() {} | ||
345 | |||
346 | public void SampleThreads(long activeThreads, long inUseThreads) {} | ||
347 | public void SampleWorkItems(long workItemsQueued, long workItemsProcessed) {} | ||
348 | public void SampleWorkItemsWaitTime(TimeSpan workItemWaitTime) {} | ||
349 | public void SampleWorkItemsProcessTime(TimeSpan workItemProcessTime) {} | ||
350 | } | ||
351 | |||
352 | } | ||