mscorlib(4.0.0.0) API with additions
CounterSampleCalculator.cs
1 using Microsoft.Win32;
4 using System.IO;
7 
8 namespace System.Diagnostics
9 {
11  public static class CounterSampleCalculator
12  {
13  private static volatile bool perfCounterDllLoaded;
14 
15  private static float GetElapsedTime(CounterSample oldSample, CounterSample newSample)
16  {
17  if (newSample.RawValue == 0L)
18  {
19  return 0f;
20  }
21  float num = (float)(double)(ulong)oldSample.CounterFrequency;
22  if (oldSample.UnsignedRawValue >= (ulong)newSample.CounterTimeStamp || num <= 0f)
23  {
24  return 0f;
25  }
26  float num2 = (float)(double)(ulong)(newSample.CounterTimeStamp - (long)oldSample.UnsignedRawValue);
27  return num2 / num;
28  }
29 
33  public static float ComputeCounterValue(CounterSample newSample)
34  {
35  return ComputeCounterValue(CounterSample.Empty, newSample);
36  }
37 
46  public static float ComputeCounterValue(CounterSample oldSample, CounterSample newSample)
47  {
48  int counterType = (int)newSample.CounterType;
49  if (oldSample.SystemFrequency == 0L)
50  {
51  if (counterType != 537003008 && counterType != 65536 && counterType != 0 && counterType != 65792 && counterType != 256 && counterType != 1107494144)
52  {
53  return 0f;
54  }
55  }
56  else if (oldSample.CounterType != newSample.CounterType)
57  {
58  throw new InvalidOperationException(SR.GetString("MismatchedCounterTypes"));
59  }
60  if (counterType == 807666944)
61  {
62  return GetElapsedTime(oldSample, newSample);
63  }
64  Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER pDH_RAW_COUNTER = new Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER();
65  Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER pDH_RAW_COUNTER2 = new Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER();
66  FillInValues(oldSample, newSample, pDH_RAW_COUNTER2, pDH_RAW_COUNTER);
67  LoadPerfCounterDll();
68  Microsoft.Win32.NativeMethods.PDH_FMT_COUNTERVALUE pDH_FMT_COUNTERVALUE = new Microsoft.Win32.NativeMethods.PDH_FMT_COUNTERVALUE();
69  long pTimeBase = newSample.SystemFrequency;
70  int num = Microsoft.Win32.SafeNativeMethods.FormatFromRawValue((uint)counterType, 37376u, ref pTimeBase, pDH_RAW_COUNTER, pDH_RAW_COUNTER2, pDH_FMT_COUNTERVALUE);
71  switch (num)
72  {
73  case -2147481643:
74  case -2147481642:
75  case -2147481640:
76  return 0f;
77  default:
78  throw new Win32Exception(num, SR.GetString("PerfCounterPdhError", num.ToString("x", CultureInfo.InvariantCulture)));
79  case 0:
80  return (float)pDH_FMT_COUNTERVALUE.data;
81  }
82  }
83 
84  private static void FillInValues(CounterSample oldSample, CounterSample newSample, Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER oldPdhValue, Microsoft.Win32.NativeMethods.PDH_RAW_COUNTER newPdhValue)
85  {
86  int counterType = (int)newSample.CounterType;
87  switch (counterType)
88  {
89  case 4260864:
90  case 4523008:
91  case 6620416:
92  case 272696320:
93  case 543229184:
94  newPdhValue.FirstValue = newSample.RawValue;
95  newPdhValue.SecondValue = newSample.TimeStamp;
96  oldPdhValue.FirstValue = oldSample.RawValue;
97  oldPdhValue.SecondValue = oldSample.TimeStamp;
98  break;
99  case 5571840:
100  newPdhValue.FirstValue = newSample.RawValue;
101  newPdhValue.SecondValue = newSample.TimeStamp100nSec;
102  oldPdhValue.FirstValue = oldSample.RawValue;
103  oldPdhValue.SecondValue = oldSample.TimeStamp100nSec;
104  break;
105  case 4523264:
106  case 272696576:
107  case 541132032:
108  case 557909248:
109  case 574686464:
110  case 591463680:
111  newPdhValue.FirstValue = newSample.RawValue;
112  newPdhValue.SecondValue = newSample.TimeStamp;
113  oldPdhValue.FirstValue = oldSample.RawValue;
114  oldPdhValue.SecondValue = oldSample.TimeStamp;
115  if (counterType == 574686464 || counterType == 591463680)
116  {
117  newPdhValue.FirstValue *= (uint)newSample.CounterFrequency;
118  if (oldSample.CounterFrequency != 0L)
119  {
120  oldPdhValue.FirstValue *= (uint)oldSample.CounterFrequency;
121  }
122  }
123  if ((counterType & 0x2000000) == 33554432)
124  {
125  newPdhValue.MultiCount = (int)newSample.BaseValue;
126  oldPdhValue.MultiCount = (int)oldSample.BaseValue;
127  }
128  break;
129  case 0:
130  case 256:
131  case 65536:
132  case 65792:
133  case 4195328:
134  case 4195584:
135  newPdhValue.FirstValue = newSample.RawValue;
136  newPdhValue.SecondValue = 0L;
137  oldPdhValue.FirstValue = oldSample.RawValue;
138  oldPdhValue.SecondValue = 0L;
139  break;
140  case 542180608:
141  case 558957824:
142  case 575735040:
143  case 592512256:
144  newPdhValue.FirstValue = newSample.RawValue;
145  newPdhValue.SecondValue = newSample.TimeStamp100nSec;
146  oldPdhValue.FirstValue = oldSample.RawValue;
147  oldPdhValue.SecondValue = oldSample.TimeStamp100nSec;
148  if ((counterType & 0x2000000) == 33554432)
149  {
150  newPdhValue.MultiCount = (int)newSample.BaseValue;
151  oldPdhValue.MultiCount = (int)oldSample.BaseValue;
152  }
153  break;
154  case 537003008:
155  case 537003264:
156  case 541525248:
157  case 542573824:
158  case 543622400:
159  case 549585920:
160  case 805438464:
161  case 1073874176:
162  newPdhValue.FirstValue = newSample.RawValue;
163  newPdhValue.SecondValue = newSample.BaseValue;
164  oldPdhValue.FirstValue = oldSample.RawValue;
165  oldPdhValue.SecondValue = oldSample.BaseValue;
166  break;
167  default:
168  newPdhValue.FirstValue = 0L;
169  newPdhValue.SecondValue = 0L;
170  oldPdhValue.FirstValue = 0L;
171  oldPdhValue.SecondValue = 0L;
172  break;
173  }
174  }
175 
176  private static void LoadPerfCounterDll()
177  {
178  if (!perfCounterDllLoaded)
179  {
180  new FileIOPermission(PermissionState.Unrestricted).Assert();
181  string runtimeDirectory = RuntimeEnvironment.GetRuntimeDirectory();
182  string libFilename = Path.Combine(runtimeDirectory, "perfcounter.dll");
183  if (Microsoft.Win32.SafeNativeMethods.LoadLibrary(libFilename) == IntPtr.Zero)
184  {
186  }
187  perfCounterDllLoaded = true;
188  }
189  }
190  }
191 }
long BaseValue
Gets an optional, base raw value for the counter.
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
long TimeStamp
Gets the raw time stamp.
long RawValue
Gets the raw value of the counter.
static string Combine(string path1, string path2)
Combines two strings into a path.
Definition: Path.cs:1107
long CounterTimeStamp
Gets the counter's time stamp.
static float ComputeCounterValue(CounterSample newSample)
Computes the calculated value of a single raw counter sample.
Definition: __Canon.cs:3
Provides a collection of static methods that return information about the common language runtime env...
static string GetRuntimeDirectory()
Returns the directory where the common language runtime is installed.
Provides a set of utility functions for interpreting performance counter data.
Defines a structure that holds the raw data for a performance counter.
Definition: CounterSample.cs:4
static float ComputeCounterValue(CounterSample oldSample, CounterSample newSample)
Computes the calculated value of two raw counter samples.
Throws an exception for a Win32 error code.
PerformanceCounterType CounterType
Gets the performance counter type.
static CounterSample Empty
Defines an empty, uninitialized performance counter sample of type NumberOfItems32.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
long TimeStamp100nSec
Gets the raw, high-fidelity time stamp.
long CounterFrequency
Gets the raw counter frequency.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
long SystemFrequency
Gets the raw system frequency.
The exception that is thrown when a method call is invalid for the object's current state.
void Assert()
Declares that the calling code can access the resource protected by a permission demand through the c...
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Controls the ability to access files and folders. This class cannot be inherited.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13