STPPerformanceCounter.cs 13 KB

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