/
CounterSet.cs
272 lines (242 loc) · 13 KB
/
CounterSet.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
namespace System.Diagnostics.PerformanceData
{
/// <summary>
/// CounterSet is equivalent to "Counter Object" in native performance counter terminology,
/// or "Counter Category" in previous framework releases. It defines a abstract grouping of
/// counters, where each counter defines measurable matrix. In the new performance counter
/// infrastructure, CounterSet is defined by GUID called CounterSetGuid, and is hosted inside
/// provider application, which is also defined by another GUID called ProviderGuid.
/// </summary>
public class CounterSet : IDisposable
{
internal PerfProvider _provider;
internal Guid _providerGuid;
internal Guid _counterSet;
internal CounterSetInstanceType _instType;
private readonly object _lockObject;
private bool _instanceCreated;
internal Dictionary<string, int> _stringToId;
internal Dictionary<int, CounterType> _idToCounter;
/// <summary>
/// CounterSet constructor.
/// </summary>
/// <param name="providerGuid">ProviderGuid identifies the provider application. A provider identified by ProviderGuid could publish several CounterSets defined by different CounterSetGuids</param>
/// <param name="counterSetGuid">CounterSetGuid identifies the specific CounterSet. CounterSetGuid should be unique.</param>
/// <param name="instanceType">One of defined CounterSetInstanceType values</param>
public CounterSet(Guid providerGuid, Guid counterSetGuid, CounterSetInstanceType instanceType)
{
if (!PerfProviderCollection.ValidateCounterSetInstanceType(instanceType))
{
throw new ArgumentException(SR.Format(SR.Perflib_Argument_InvalidCounterSetInstanceType, instanceType), nameof(instanceType));
}
_providerGuid = providerGuid;
_counterSet = counterSetGuid;
_instType = instanceType;
PerfProviderCollection.RegisterCounterSet(_counterSet);
_provider = PerfProviderCollection.QueryProvider(_providerGuid);
_lockObject = new object();
_stringToId = new Dictionary<string, int>();
_idToCounter = new Dictionary<int, CounterType>();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~CounterSet()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
lock (this)
{
PerfProviderCollection.UnregisterCounterSet(_counterSet);
if (_instanceCreated && _provider != null)
{
lock (_lockObject)
{
if (_provider != null)
{
Interlocked.Decrement(ref _provider._counterSet);
if (_provider._counterSet <= 0)
{
PerfProviderCollection.RemoveProvider(_providerGuid);
}
_provider = null;
}
}
}
}
}
/// <summary>
/// Add non-displayable new counter to CounterSet; that is, perfmon would not display the counter.
/// </summary>
/// <param name="counterId">CounterId uniquely identifies the counter within CounterSet</param>
/// <param name="counterType">One of defined CounterType values</param>
public void AddCounter(int counterId, CounterType counterType)
{
if (_provider == null)
{
throw new InvalidOperationException(SR.Format(SR.Perflib_InvalidOperation_NoActiveProvider, _providerGuid));
}
if (!PerfProviderCollection.ValidateCounterType(counterType))
{
throw new ArgumentException(SR.Format(SR.Perflib_Argument_InvalidCounterType, counterType), nameof(counterType));
}
if (_instanceCreated)
{
throw new InvalidOperationException(SR.Format(SR.Perflib_InvalidOperation_AddCounterAfterInstance, _counterSet));
}
lock (_lockObject)
{
if (_instanceCreated)
{
throw new InvalidOperationException(SR.Format(SR.Perflib_InvalidOperation_AddCounterAfterInstance, _counterSet));
}
if (_idToCounter.ContainsKey(counterId))
{
throw new ArgumentException(SR.Format(SR.Perflib_Argument_CounterAlreadyExists, counterId, _counterSet), nameof(counterId));
}
_idToCounter.Add(counterId, counterType);
}
}
/// <summary>
/// Add named new counter to CounterSet.
/// </summary>
/// <param name="counterId">CounterId uniquely identifies the counter within CounterSet</param>
/// <param name="counterType">One of defined CounterType values</param>
/// <param name="counterName">This is friendly name to help provider developers as indexer. and it might not match what is displayed in counter consumption applications lie perfmon.</param>
public void AddCounter(int counterId, CounterType counterType, string counterName)
{
ArgumentNullException.ThrowIfNull(counterName);
if (counterName.Length == 0)
{
throw new ArgumentException(SR.Perflib_Argument_EmptyCounterName, nameof(counterName));
}
if (!PerfProviderCollection.ValidateCounterType(counterType))
{
throw new ArgumentException(SR.Format(SR.Perflib_Argument_InvalidCounterType, counterType), nameof(counterType));
}
if (_provider == null)
{
throw new InvalidOperationException(SR.Format(SR.Perflib_InvalidOperation_NoActiveProvider, _providerGuid));
}
if (_instanceCreated)
{
throw new InvalidOperationException(SR.Format(SR.Perflib_InvalidOperation_AddCounterAfterInstance, _counterSet));
}
lock (_lockObject)
{
if (_instanceCreated)
{
throw new InvalidOperationException(SR.Format(SR.Perflib_InvalidOperation_AddCounterAfterInstance, _counterSet));
}
if (_stringToId.ContainsKey(counterName))
{
throw new ArgumentException(SR.Format(SR.Perflib_Argument_CounterNameAlreadyExists, counterName, _counterSet), nameof(counterName));
}
if (_idToCounter.ContainsKey(counterId))
{
throw new ArgumentException(SR.Format(SR.Perflib_Argument_CounterAlreadyExists, counterId, _counterSet), nameof(counterId));
}
_stringToId.Add(counterName, counterId);
_idToCounter.Add(counterId, counterType);
}
}
/// <summary>
/// Create instances of the CounterSet. Created CounterSetInstance identifies active identity and tracks raw counter data for that identity.
/// </summary>
/// <param name="instanceName">Friendly name identifies the instance. InstanceName would be shown in counter consumption applications like perfmon.</param>
/// <returns>CounterSetInstance object</returns>
public CounterSetInstance CreateCounterSetInstance(string instanceName)
{
ArgumentNullException.ThrowIfNull(instanceName);
if (instanceName.Length == 0)
{
throw new ArgumentException(SR.Perflib_Argument_EmptyInstanceName, nameof(instanceName));
}
if (_provider == null)
{
throw new InvalidOperationException(SR.Format(SR.Perflib_InvalidOperation_NoActiveProvider, _providerGuid));
}
if (!_instanceCreated)
{
lock (_lockObject)
{
if (!_instanceCreated)
{
if (_provider == null)
{
throw new InvalidOperationException(SR.Format(SR.Perflib_InvalidOperation_NoActiveProvider, _providerGuid));
}
if (_provider._hProvider.IsInvalid)
{
throw new InvalidOperationException(SR.Format(SR.Perflib_InvalidOperation_NoActiveProvider, _providerGuid));
}
if (_idToCounter.Count == 0)
{
throw new InvalidOperationException(SR.Format(SR.Perflib_InvalidOperation_CounterSetContainsNoCounter, _counterSet));
}
uint Status = (uint)Interop.Errors.ERROR_SUCCESS;
unsafe
{
uint CounterSetInfoSize = (uint)sizeof(Interop.PerfCounter.PerfCounterSetInfoStruct)
+ (uint)_idToCounter.Count * (uint)sizeof(Interop.PerfCounter.PerfCounterInfoStruct);
uint CounterSetInfoUsed = 0;
byte* CounterSetBuffer = stackalloc byte[(int)CounterSetInfoSize];
Debug.Assert(sizeof(Interop.PerfCounter.PerfCounterSetInfoStruct) == 40);
Debug.Assert(sizeof(Interop.PerfCounter.PerfCounterInfoStruct) == 32);
Interop.PerfCounter.PerfCounterSetInfoStruct* CounterSetInfo;
Interop.PerfCounter.PerfCounterInfoStruct* CounterInfo;
uint CurrentCounter = 0;
uint CurrentOffset = 0;
CounterSetInfo = (Interop.PerfCounter.PerfCounterSetInfoStruct*)CounterSetBuffer;
CounterSetInfo->CounterSetGuid = _counterSet;
CounterSetInfo->ProviderGuid = _providerGuid;
CounterSetInfo->NumCounters = (uint)_idToCounter.Count;
CounterSetInfo->InstanceType = (uint)_instType;
foreach (KeyValuePair<int, CounterType> CounterDef in _idToCounter)
{
CounterSetInfoUsed = (uint)sizeof(Interop.PerfCounter.PerfCounterSetInfoStruct)
+ (uint)CurrentCounter * (uint)sizeof(Interop.PerfCounter.PerfCounterInfoStruct);
if (CounterSetInfoUsed < CounterSetInfoSize)
{
CounterInfo = (Interop.PerfCounter.PerfCounterInfoStruct*)(CounterSetBuffer + CounterSetInfoUsed);
CounterInfo->CounterId = (uint)CounterDef.Key;
CounterInfo->CounterType = (uint)CounterDef.Value;
CounterInfo->Attrib = 0x0000000000000001; // PERF_ATTRIB_BY_REFERENCE
CounterInfo->Size = (uint)sizeof(void*); // always use pointer size
CounterInfo->DetailLevel = 100; // PERF_DETAIL_NOVICE
CounterInfo->Scale = 0; // Default scale
CounterInfo->Offset = CurrentOffset;
CurrentOffset += CounterInfo->Size;
}
CurrentCounter++;
}
Status = Interop.PerfCounter.PerfSetCounterSetInfo(_provider._hProvider, CounterSetInfo, CounterSetInfoSize);
// ERROR_INVALID_PARAMETER, ERROR_ALREADY_EXISTS, ERROR_NOT_ENOUGH_MEMORY, ERROR_OUTOFMEMORY
if (Status != (uint)Interop.Errors.ERROR_SUCCESS)
{
throw Status switch
{
(uint)Interop.Errors.ERROR_ALREADY_EXISTS => new InvalidOperationException(SR.Format(SR.Perflib_Argument_CounterSetAlreadyRegister, _counterSet)),
_ => new Win32Exception((int)Status),
};
}
Interlocked.Increment(ref _provider._counterSet);
}
_instanceCreated = true;
}
}
}
CounterSetInstance thisInst = new CounterSetInstance(this, instanceName);
return thisInst;
}
}
}