1
0

STPPerformanceCounter.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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
  109. // the method.
  110. #pragma warning disable 0618
  111. PerformanceCounterCategory.Create(
  112. _stpCategoryName,
  113. _stpCategoryHelp,
  114. //PerformanceCounterCategoryType.MultiInstance,
  115. counters);
  116. #pragma warning restore 0618
  117. }
  118. }
  119. // Properties
  120. public static STPPerformanceCounters Instance
  121. {
  122. get
  123. {
  124. return _instance;
  125. }
  126. }
  127. }
  128. internal class STPInstancePerformanceCounter : IDisposable
  129. {
  130. // Fields
  131. private PerformanceCounter _pcs;
  132. // Methods
  133. protected STPInstancePerformanceCounter()
  134. {
  135. }
  136. public STPInstancePerformanceCounter(
  137. string instance,
  138. STPPerformanceCounterType spcType)
  139. {
  140. STPPerformanceCounters counters = STPPerformanceCounters.Instance;
  141. _pcs = new PerformanceCounter(
  142. STPPerformanceCounters._stpCategoryName,
  143. counters._stpPerformanceCounters[(int) spcType].Name,
  144. instance,
  145. false);
  146. _pcs.RawValue = _pcs.RawValue;
  147. }
  148. ~STPInstancePerformanceCounter()
  149. {
  150. Close();
  151. }
  152. public void Close()
  153. {
  154. if (_pcs != null)
  155. {
  156. _pcs.RemoveInstance();
  157. _pcs.Close();
  158. _pcs = null;
  159. }
  160. }
  161. public void Dispose()
  162. {
  163. Close();
  164. GC.SuppressFinalize(this);
  165. }
  166. public virtual void Increment()
  167. {
  168. _pcs.Increment();
  169. }
  170. public virtual void IncrementBy(long val)
  171. {
  172. _pcs.IncrementBy(val);
  173. }
  174. public virtual void Set(long val)
  175. {
  176. _pcs.RawValue = val;
  177. }
  178. }
  179. internal class STPInstanceNullPerformanceCounter : STPInstancePerformanceCounter
  180. {
  181. // Methods
  182. public STPInstanceNullPerformanceCounter() {}
  183. public override void Increment() {}
  184. public override void IncrementBy(long value) {}
  185. public override void Set(long val) {}
  186. }
  187. internal interface ISTPInstancePerformanceCounters : IDisposable
  188. {
  189. void Close();
  190. void SampleThreads(long activeThreads, long inUseThreads);
  191. void SampleWorkItems(long workItemsQueued, long workItemsProcessed);
  192. void SampleWorkItemsWaitTime(TimeSpan workItemWaitTime);
  193. void SampleWorkItemsProcessTime(TimeSpan workItemProcessTime);
  194. }
  195. internal class STPInstancePerformanceCounters : ISTPInstancePerformanceCounters, IDisposable
  196. {
  197. // Fields
  198. private STPInstancePerformanceCounter[] _pcs;
  199. private static STPInstancePerformanceCounter _stpInstanceNullPerformanceCounter;
  200. // Methods
  201. static STPInstancePerformanceCounters()
  202. {
  203. _stpInstanceNullPerformanceCounter = new STPInstanceNullPerformanceCounter();
  204. }
  205. public STPInstancePerformanceCounters(string instance)
  206. {
  207. _pcs = new STPInstancePerformanceCounter[(int)STPPerformanceCounterType.LastCounter];
  208. // STPPerformanceCounters counters = STPPerformanceCounters.Instance;
  209. for (int i = 0; i < _pcs.Length; i++)
  210. {
  211. if (instance != null)
  212. {
  213. _pcs[i] = new STPInstancePerformanceCounter(
  214. instance,
  215. (STPPerformanceCounterType) i);
  216. }
  217. else
  218. {
  219. _pcs[i] = _stpInstanceNullPerformanceCounter;
  220. }
  221. }
  222. }
  223. public void Close()
  224. {
  225. if (null != _pcs)
  226. {
  227. for (int i = 0; i < _pcs.Length; i++)
  228. {
  229. if (null != _pcs[i])
  230. {
  231. _pcs[i].Close();
  232. }
  233. }
  234. _pcs = null;
  235. }
  236. }
  237. ~STPInstancePerformanceCounters()
  238. {
  239. Close();
  240. }
  241. public void Dispose()
  242. {
  243. Close();
  244. GC.SuppressFinalize(this);
  245. }
  246. private STPInstancePerformanceCounter GetCounter(STPPerformanceCounterType spcType)
  247. {
  248. return _pcs[(int) spcType];
  249. }
  250. public void SampleThreads(long activeThreads, long inUseThreads)
  251. {
  252. GetCounter(STPPerformanceCounterType.ActiveThreads).Set(activeThreads);
  253. GetCounter(STPPerformanceCounterType.InUseThreads).Set(inUseThreads);
  254. GetCounter(STPPerformanceCounterType.OverheadThreads).Set(activeThreads-inUseThreads);
  255. GetCounter(STPPerformanceCounterType.OverheadThreadsPercentBase).Set(activeThreads-inUseThreads);
  256. GetCounter(STPPerformanceCounterType.OverheadThreadsPercent).Set(inUseThreads);
  257. }
  258. public void SampleWorkItems(long workItemsQueued, long workItemsProcessed)
  259. {
  260. GetCounter(STPPerformanceCounterType.WorkItems).Set(workItemsQueued+workItemsProcessed);
  261. GetCounter(STPPerformanceCounterType.WorkItemsInQueue).Set(workItemsQueued);
  262. GetCounter(STPPerformanceCounterType.WorkItemsProcessed).Set(workItemsProcessed);
  263. GetCounter(STPPerformanceCounterType.WorkItemsQueuedPerSecond).Set(workItemsQueued);
  264. GetCounter(STPPerformanceCounterType.WorkItemsProcessedPerSecond).Set(workItemsProcessed);
  265. }
  266. public void SampleWorkItemsWaitTime(TimeSpan workItemWaitTime)
  267. {
  268. GetCounter(STPPerformanceCounterType.AvgWorkItemWaitTime).IncrementBy((long)workItemWaitTime.TotalMilliseconds);
  269. GetCounter(STPPerformanceCounterType.AvgWorkItemWaitTimeBase).Increment();
  270. }
  271. public void SampleWorkItemsProcessTime(TimeSpan workItemProcessTime)
  272. {
  273. GetCounter(STPPerformanceCounterType.AvgWorkItemProcessTime).IncrementBy((long)workItemProcessTime.TotalMilliseconds);
  274. GetCounter(STPPerformanceCounterType.AvgWorkItemProcessTimeBase).Increment();
  275. }
  276. }
  277. internal class NullSTPInstancePerformanceCounters : ISTPInstancePerformanceCounters, IDisposable
  278. {
  279. static NullSTPInstancePerformanceCounters()
  280. {
  281. }
  282. private static NullSTPInstancePerformanceCounters _instance = new NullSTPInstancePerformanceCounters(null);
  283. public static NullSTPInstancePerformanceCounters Instance
  284. {
  285. get { return _instance; }
  286. }
  287. public NullSTPInstancePerformanceCounters(string instance) {}
  288. public void Close() {}
  289. public void Dispose() {}
  290. public void SampleThreads(long activeThreads, long inUseThreads) {}
  291. public void SampleWorkItems(long workItemsQueued, long workItemsProcessed) {}
  292. public void SampleWorkItemsWaitTime(TimeSpan workItemWaitTime) {}
  293. public void SampleWorkItemsProcessTime(TimeSpan workItemProcessTime) {}
  294. }
  295. }