STPPerformanceCounter.cs 15 KB

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