5 // Marek Habersack <mhabersack@novell.com>
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Diagnostics;
34 // Counters in the ".NET Memory Cache 4.0" are not documented on MSDN. They were discovered using
35 // perfmon there their definition may change without any notice
37 namespace System.Runtime.Caching
39 sealed class MemoryCachePerformanceCounters : IDisposable
41 const string dotNetCategoryName = ".NET Memory Cache 4.0";
43 public const int CACHE_ENTRIES = 0;
44 public const int CACHE_HIT_RATIO = 1;
45 public const int CACHE_HITS = 2;
46 public const int CACHE_MISSES = 3;
47 public const int CACHE_TRIMS = 4;
48 public const int CACHE_TURNOVER_RATE = 5;
49 const int COUNTERS_LAST = CACHE_TURNOVER_RATE;
51 PerformanceCounter[] perfCounters;
53 public MemoryCachePerformanceCounters (string instanceName, bool noCounters)
55 var collection = new CounterCreationDataCollection ();
58 if (!PerformanceCounterCategory.Exists (dotNetCategoryName)) {
61 // - types of all the counters
63 CreateCounter ("Cache Entries", PerformanceCounterType.NumberOfItems64, collection);
64 CreateCounter ("Cache Hit Ratio", PerformanceCounterType.RawFraction, collection);
65 CreateCounter ("Cache Hits", PerformanceCounterType.NumberOfItems64, collection);
66 CreateCounter ("Cache Misses", PerformanceCounterType.NumberOfItems64, collection);
67 CreateCounter ("Cache Trims", PerformanceCounterType.NumberOfItems64, collection);
68 CreateCounter ("Cache Turnover Rate", PerformanceCounterType.RateOfCountsPerSecond64, collection);
70 PerformanceCounterCategory.Create (dotNetCategoryName, "System.Runtime.Caching.MemoryCache Performance Counters",
71 PerformanceCounterCategoryType.MultiInstance, collection);
74 perfCounters = new PerformanceCounter [COUNTERS_LAST + 1];
75 perfCounters [CACHE_ENTRIES] = new PerformanceCounter (dotNetCategoryName, "Cache Entries", instanceName, false);
76 perfCounters [CACHE_ENTRIES].RawValue = 0;
77 perfCounters [CACHE_HIT_RATIO] = new PerformanceCounter (dotNetCategoryName, "Cache Hit Ratio", instanceName, false);
78 perfCounters [CACHE_HIT_RATIO].RawValue = 0;
79 perfCounters [CACHE_HITS] = new PerformanceCounter (dotNetCategoryName, "Cache Hits", instanceName, false);
80 perfCounters [CACHE_HITS].RawValue = 0;
81 perfCounters [CACHE_MISSES] = new PerformanceCounter (dotNetCategoryName, "Cache Misses", instanceName, false);
82 perfCounters [CACHE_MISSES].RawValue = 0;
83 perfCounters [CACHE_TRIMS] = new PerformanceCounter (dotNetCategoryName, "Cache Trims", instanceName, false);
84 perfCounters [CACHE_TRIMS].RawValue = 0;
85 perfCounters [CACHE_TURNOVER_RATE] = new PerformanceCounter (dotNetCategoryName, "Cache Turnover Rate", instanceName, false);
86 perfCounters [CACHE_TURNOVER_RATE].RawValue = 0;
90 public void Dispose ()
92 foreach (PerformanceCounter counter in perfCounters) {
100 public void Decrement (int counteridx)
102 if (perfCounters == null || counteridx < 0 || counteridx > COUNTERS_LAST)
105 perfCounters [counteridx].Decrement ();
108 public void Increment (int counteridx)
110 if (perfCounters == null || counteridx < 0 || counteridx > COUNTERS_LAST)
113 perfCounters [counteridx].Increment ();
116 void CreateCounter (string name, PerformanceCounterType type, CounterCreationDataCollection collection)
118 var ccd = new CounterCreationData ();
120 ccd.CounterName = name;
121 ccd.CounterType = type;
122 collection.Add (ccd);