New tests.
[mono.git] / mcs / class / System.Runtime.Caching / System.Runtime.Caching / MemoryCachePerformanceCounters.cs
1 //
2 // MemoryCache.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Diagnostics;
32
33 //
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
36 //
37 namespace System.Runtime.Caching
38 {
39         sealed class MemoryCachePerformanceCounters : IDisposable
40         {
41                 const string dotNetCategoryName = ".NET Memory Cache 4.0";
42
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;
50                 
51                 PerformanceCounter[] perfCounters;
52
53                 public MemoryCachePerformanceCounters (string instanceName, bool noCounters)
54                 {
55                         var collection = new CounterCreationDataCollection ();
56
57                         if (!noCounters) {
58                                 if (!PerformanceCounterCategory.Exists (dotNetCategoryName)) {
59                                         // TODO: check:
60                                         //
61                                         //  - types of all the counters
62                                         //
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);
69                         
70                                         PerformanceCounterCategory.Create (dotNetCategoryName, "System.Runtime.Caching.MemoryCache Performance Counters",
71                                                                            PerformanceCounterCategoryType.MultiInstance, collection);
72                                 }
73                                 
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;
87                         }
88                 }
89
90                 public void Dispose ()
91                 {
92                         foreach (PerformanceCounter counter in perfCounters) {
93                                 if (counter == null)
94                                         continue;
95
96                                 counter.Dispose ();
97                         }
98                 }
99                 
100                 public void Decrement (int counteridx)
101                 {
102                         if (perfCounters == null || counteridx < 0 || counteridx > COUNTERS_LAST)
103                                 return;
104
105                         perfCounters [counteridx].Decrement ();
106                 }
107                 
108                 public void Increment (int counteridx)
109                 {
110                         if (perfCounters == null || counteridx < 0 || counteridx > COUNTERS_LAST)
111                                 return;
112
113                         perfCounters [counteridx].Increment ();
114                 }
115                 
116                 void CreateCounter (string name, PerformanceCounterType type, CounterCreationDataCollection collection)
117                 {
118                         var ccd = new CounterCreationData ();
119
120                         ccd.CounterName = name;
121                         ccd.CounterType = type;
122                         collection.Add (ccd);
123                 }
124         }
125 }