/
PerformanceCounter.cs
596 lines (517 loc) · 20.7 KB
/
PerformanceCounter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Threading;
namespace System.Diagnostics
{
/// <summary>
/// Performance Counter component.
/// This class provides support for NT Performance counters.
/// It handles both the existing counters (accessible by Perf Registry Interface)
/// and user defined (extensible) counters.
/// This class is a part of a larger framework, that includes the perf dll object and
/// perf service.
/// </summary>
public sealed class PerformanceCounter : Component, ISupportInitialize
{
private string _machineName;
private string _categoryName;
private string _counterName;
private string _instanceName;
private PerformanceCounterInstanceLifetime _instanceLifetime = PerformanceCounterInstanceLifetime.Global;
private bool _isReadOnly;
private bool _initialized;
private string _helpMsg;
private int _counterType = -1;
// Cached old sample
private CounterSample _oldSample = CounterSample.Empty;
// Cached IP Shared Performanco counter
private SharedPerformanceCounter _sharedCounter;
[Obsolete("PerformanceCounter.DefaultFileMappingSize has been deprecated and is not used. Use machine.config or an application configuration file to set the size of the PerformanceCounter file mapping instead.")]
public static int DefaultFileMappingSize = 524288;
private object _instanceLockObject;
private object InstanceLockObject
{
get
{
if (_instanceLockObject == null)
{
object o = new object();
Interlocked.CompareExchange(ref _instanceLockObject, o, null);
}
return _instanceLockObject;
}
}
/// <summary>
/// The defaut constructor. Creates the perf counter object
/// </summary>
public PerformanceCounter()
{
_machineName = ".";
_categoryName = string.Empty;
_counterName = string.Empty;
_instanceName = string.Empty;
_isReadOnly = true;
GC.SuppressFinalize(this);
}
/// <summary>
/// Creates the Performance Counter Object
/// </summary>
public PerformanceCounter(string categoryName, string counterName, string instanceName, string machineName)
{
MachineName = machineName;
CategoryName = categoryName;
CounterName = counterName;
InstanceName = instanceName;
_isReadOnly = true;
Initialize();
GC.SuppressFinalize(this);
}
internal PerformanceCounter(string categoryName, string counterName, string instanceName, string machineName, bool skipInit)
{
MachineName = machineName;
CategoryName = categoryName;
CounterName = counterName;
InstanceName = instanceName;
_isReadOnly = true;
_initialized = true;
GC.SuppressFinalize(this);
}
/// <summary>
/// Creates the Performance Counter Object on local machine.
/// </summary>
public PerformanceCounter(string categoryName, string counterName, string instanceName) :
this(categoryName, counterName, instanceName, true)
{
}
/// <summary>
/// Creates the Performance Counter Object on local machine.
/// </summary>
public PerformanceCounter(string categoryName, string counterName, string instanceName, bool readOnly)
{
if (!readOnly)
{
VerifyWriteableCounterAllowed();
}
MachineName = ".";
CategoryName = categoryName;
CounterName = counterName;
InstanceName = instanceName;
_isReadOnly = readOnly;
Initialize();
GC.SuppressFinalize(this);
}
/// <summary>
/// Creates the Performance Counter Object, assumes that it's a single instance
/// </summary>
public PerformanceCounter(string categoryName, string counterName) :
this(categoryName, counterName, true)
{
}
/// <summary>
/// Creates the Performance Counter Object, assumes that it's a single instance
/// </summary>
public PerformanceCounter(string categoryName, string counterName, bool readOnly) :
this(categoryName, counterName, "", readOnly)
{
}
/// <summary>
/// Returns the performance category name for this performance counter
/// </summary>
public string CategoryName
{
get
{
return _categoryName;
}
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (_categoryName == null || !string.Equals(_categoryName, value, StringComparison.OrdinalIgnoreCase))
{
_categoryName = value;
Close();
}
}
}
/// <summary>
/// Returns the description message for this performance counter
/// </summary>
public string CounterHelp
{
get
{
string currentCategoryName = _categoryName;
string currentMachineName = _machineName;
Initialize();
_helpMsg ??= PerformanceCounterLib.GetCounterHelp(currentMachineName, currentCategoryName, _counterName);
return _helpMsg;
}
}
/// <summary>
/// Sets/returns the performance counter name for this performance counter
/// </summary>
public string CounterName
{
get
{
return _counterName;
}
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (_counterName == null || !string.Equals(_counterName, value, StringComparison.OrdinalIgnoreCase))
{
_counterName = value;
Close();
}
}
}
/// <summary>
/// Sets/Returns the counter type for this performance counter
/// </summary>
public PerformanceCounterType CounterType
{
get
{
if (_counterType == -1)
{
string currentCategoryName = _categoryName;
string currentMachineName = _machineName;
// This is the same thing that NextSample does, except that it doesn't try to get the actual counter
// value. If we wanted the counter value, we would need to have an instance name.
Initialize();
using (CategorySample categorySample = PerformanceCounterLib.GetCategorySample(currentMachineName, currentCategoryName))
{
CounterDefinitionSample counterSample = categorySample.GetCounterDefinitionSample(_counterName);
_counterType = counterSample._counterType;
}
}
return (PerformanceCounterType)_counterType;
}
}
public PerformanceCounterInstanceLifetime InstanceLifetime
{
get { return _instanceLifetime; }
set
{
if (value > PerformanceCounterInstanceLifetime.Process || value < PerformanceCounterInstanceLifetime.Global)
throw new ArgumentOutOfRangeException(nameof(value));
if (_initialized)
throw new InvalidOperationException(SR.CantSetLifetimeAfterInitialized);
_instanceLifetime = value;
}
}
/// <summary>
/// Sets/returns an instance name for this performance counter
/// </summary>
public string InstanceName
{
get
{
return _instanceName;
}
set
{
if (value == null && _instanceName == null)
return;
if ((value == null && _instanceName != null) ||
(value != null && _instanceName == null) ||
!string.Equals(_instanceName, value, StringComparison.OrdinalIgnoreCase))
{
_instanceName = value;
Close();
}
}
}
/// <summary>
/// Returns true if counter is read only (system counter, foreign extensible counter or remote counter)
/// </summary>
public bool ReadOnly
{
get
{
return _isReadOnly;
}
set
{
if (value != _isReadOnly)
{
if (value == false)
{
VerifyWriteableCounterAllowed();
}
_isReadOnly = value;
Close();
}
}
}
/// <summary>
/// Set/returns the machine name for this performance counter
/// </summary>
public string MachineName
{
get
{
return _machineName;
}
set
{
if (!SyntaxCheck.CheckMachineName(value))
throw new ArgumentException(SR.Format(SR.InvalidProperty, nameof(MachineName), value), nameof(value));
if (_machineName != value)
{
_machineName = value;
Close();
}
}
}
/// <summary>
/// Directly accesses the raw value of this counter. If counter type is of a 32-bit size, it will truncate
/// the value given to 32 bits. This can be significantly more performant for scenarios where
/// the raw value is sufficient. Note that this only works for custom counters created using
/// this component, non-custom counters will throw an exception if this property is accessed.
/// </summary>
public long RawValue
{
get
{
if (ReadOnly)
{
//No need to initialize or Demand, since NextSample already does.
return NextSample().RawValue;
}
else
{
Initialize();
return _sharedCounter.Value;
}
}
set
{
if (ReadOnly)
ThrowReadOnly();
Initialize();
_sharedCounter.Value = value;
}
}
/// <summary>
/// </summary>
public void BeginInit()
{
Close();
}
/// <summary>
/// Frees all the resources allocated by this counter
/// </summary>
public void Close()
{
_helpMsg = null;
_oldSample = CounterSample.Empty;
_sharedCounter = null;
_initialized = false;
_counterType = -1;
}
/// <summary>
/// Frees all the resources allocated for all performance
/// counters, frees File Mapping used by extensible counters,
/// unloads dll's used to read counters.
/// </summary>
public static void CloseSharedResources()
{
PerformanceCounterLib.CloseAllLibraries();
}
/// <internalonly/>
/// <summary>
/// </summary>
protected override void Dispose(bool disposing)
{
// safe to call while finalizing or disposing
if (disposing)
{
//Dispose managed and unmanaged resources
Close();
}
base.Dispose(disposing);
}
/// <summary>
/// Decrements counter by one using an efficient atomic operation.
/// </summary>
public long Decrement()
{
if (ReadOnly)
ThrowReadOnly();
Initialize();
return _sharedCounter.Decrement();
}
/// <summary>
/// </summary>
public void EndInit()
{
Initialize();
}
/// <summary>
/// Increments the value of this counter. If counter type is of a 32-bit size, it'll truncate
/// the value given to 32 bits. This method uses a mutex to guarantee correctness of
/// the operation in case of multiple writers. This method should be used with caution because of the negative
/// impact on performance due to creation of the mutex.
/// </summary>
public long IncrementBy(long value)
{
if (_isReadOnly)
ThrowReadOnly();
Initialize();
return _sharedCounter.IncrementBy(value);
}
/// <summary>
/// Increments counter by one using an efficient atomic operation.
/// </summary>
public long Increment()
{
if (_isReadOnly)
ThrowReadOnly();
Initialize();
return _sharedCounter.Increment();
}
private static void ThrowReadOnly()
{
throw new InvalidOperationException(SR.ReadOnlyCounter);
}
private static void VerifyWriteableCounterAllowed()
{
if (EnvironmentHelpers.IsAppContainerProcess)
{
throw new NotSupportedException(SR.PCNotSupportedUnderAppContainer);
}
}
private void Initialize()
{
// Keep this method small so the JIT will inline it.
if (!_initialized && !DesignMode)
{
InitializeImpl();
}
}
/// <summary>
/// Initializes required resources
/// </summary>
private void InitializeImpl()
{
bool tookLock = false;
try
{
Monitor.Enter(InstanceLockObject, ref tookLock);
if (!_initialized)
{
string currentCategoryName = _categoryName;
string currentMachineName = _machineName;
if (currentCategoryName.Length == 0)
throw new InvalidOperationException(SR.CategoryNameMissing);
if (_counterName.Length == 0)
throw new InvalidOperationException(SR.CounterNameMissing);
if (ReadOnly)
{
if (!PerformanceCounterLib.CounterExists(currentMachineName, currentCategoryName, _counterName))
throw new InvalidOperationException(SR.Format(SR.CounterExists, currentCategoryName, _counterName));
PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
if (categoryType == PerformanceCounterCategoryType.MultiInstance)
{
if (string.IsNullOrEmpty(_instanceName))
throw new InvalidOperationException(SR.Format(SR.MultiInstanceOnly, currentCategoryName));
}
else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
{
if (!string.IsNullOrEmpty(_instanceName))
throw new InvalidOperationException(SR.Format(SR.SingleInstanceOnly, currentCategoryName));
}
if (_instanceLifetime != PerformanceCounterInstanceLifetime.Global)
throw new InvalidOperationException(SR.InstanceLifetimeProcessonReadOnly);
_initialized = true;
}
else
{
if (currentMachineName != "." && !string.Equals(currentMachineName, PerformanceCounterLib.ComputerName, StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException(SR.RemoteWriting);
if (!PerformanceCounterLib.IsCustomCategory(currentMachineName, currentCategoryName))
throw new InvalidOperationException(SR.NotCustomCounter);
// check category type
PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
if (categoryType == PerformanceCounterCategoryType.MultiInstance)
{
if (string.IsNullOrEmpty(_instanceName))
throw new InvalidOperationException(SR.Format(SR.MultiInstanceOnly, currentCategoryName));
}
else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
{
if (!string.IsNullOrEmpty(_instanceName))
throw new InvalidOperationException(SR.Format(SR.SingleInstanceOnly, currentCategoryName));
}
if (string.IsNullOrEmpty(_instanceName) && InstanceLifetime == PerformanceCounterInstanceLifetime.Process)
throw new InvalidOperationException(SR.InstanceLifetimeProcessforSingleInstance);
_sharedCounter = new SharedPerformanceCounter(currentCategoryName.ToLowerInvariant(), _counterName.ToLowerInvariant(), _instanceName.ToLowerInvariant(), _instanceLifetime);
_initialized = true;
}
}
}
finally
{
if (tookLock)
Monitor.Exit(InstanceLockObject);
}
}
// Will cause an update, raw value
/// <summary>
/// Obtains a counter sample and returns the raw value for it.
/// </summary>
public CounterSample NextSample()
{
string currentCategoryName = _categoryName;
string currentMachineName = _machineName;
Initialize();
using (CategorySample categorySample = PerformanceCounterLib.GetCategorySample(currentMachineName, currentCategoryName))
{
CounterDefinitionSample counterSample = categorySample.GetCounterDefinitionSample(_counterName);
_counterType = counterSample._counterType;
if (!categorySample._isMultiInstance)
{
if (_instanceName != null && _instanceName.Length != 0)
throw new InvalidOperationException(SR.Format(SR.InstanceNameProhibited, _instanceName));
return counterSample.GetSingleValue();
}
else
{
if (_instanceName == null || _instanceName.Length == 0)
throw new InvalidOperationException(SR.InstanceNameRequired);
return counterSample.GetInstanceValue(_instanceName);
}
}
}
/// <summary>
/// Obtains a counter sample and returns the calculated value for it.
/// NOTE: For counters whose calculated value depend upon 2 counter reads,
/// the very first read will return 0.0.
/// </summary>
public float NextValue()
{
//No need to initialize or Demand, since NextSample already does.
CounterSample newSample = NextSample();
float retVal = CounterSample.Calculate(_oldSample, newSample);
_oldSample = newSample;
return retVal;
}
/// <summary>
/// Removes this counter instance from the shared memory
/// </summary>
public void RemoveInstance()
{
if (_isReadOnly)
throw new InvalidOperationException(SR.ReadOnlyRemoveInstance);
Initialize();
_sharedCounter.RemoveInstance(_instanceName.ToLowerInvariant(), _instanceLifetime);
}
}
}