1
0

STPPerformanceCounter.cs 15 KB

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