2 // System.Diagnostics.PerformanceCounterCategory.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System
.Security
.Permissions
;
32 using System
.Runtime
.CompilerServices
;
34 namespace System
.Diagnostics
36 [PermissionSet (SecurityAction
.LinkDemand
, Unrestricted
= true)]
37 public sealed class PerformanceCounterCategory
39 private string categoryName
;
40 private string machineName
;
42 private PerformanceCounterCategoryType type
= PerformanceCounterCategoryType
.Unknown
;
45 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
46 static extern bool CategoryDelete (string name
);
48 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
49 static extern string CategoryHelpInternal (string category
, string machine
);
51 /* this icall allows a null counter and it will just search for the category */
52 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
53 static extern bool CounterCategoryExists (string counter
, string category
, string machine
);
55 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
56 static extern bool Create (string categoryName
, string categoryHelp
,
57 PerformanceCounterCategoryType categoryType
, CounterCreationData
[] items
);
59 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
60 static extern int InstanceExistsInternal (string instance
, string category
, string machine
);
62 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
63 static extern string[] GetCategoryNames (string machine
);
65 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
66 static extern string[] GetCounterNames (string category
, string machine
);
68 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
69 static extern string[] GetInstanceNames (string category
, string machine
);
71 static void CheckCategory (string categoryName
) {
72 if (categoryName
== null)
73 throw new ArgumentNullException ("categoryName");
74 if (categoryName
== "")
75 throw new ArgumentException ("categoryName");
78 public PerformanceCounterCategory ()
83 // may throw ArgumentException (""), ArgumentNullException
84 public PerformanceCounterCategory (string categoryName
)
85 : this (categoryName
, ".")
89 // may throw ArgumentException (""), ArgumentNullException
90 public PerformanceCounterCategory (string categoryName
, string machineName
)
92 CheckCategory (categoryName
);
93 if (machineName
== null)
94 throw new ArgumentNullException ("machineName");
95 // TODO checks and whatever else is needed
96 this.categoryName
= categoryName
;
97 this.machineName
= machineName
;
100 // may throw InvalidOperationException, Win32Exception
101 public string CategoryHelp
{
103 string res
= CategoryHelpInternal (categoryName
, machineName
);
106 throw new InvalidOperationException ();
110 // may throw ArgumentException (""), ArgumentNullException
111 public string CategoryName
{
112 get {return categoryName;}
115 throw new ArgumentNullException ("value");
117 throw new ArgumentException ("value");
118 categoryName
= value;
122 // may throw ArgumentException
123 public string MachineName
{
124 get {return machineName;}
127 throw new ArgumentNullException ("value");
129 throw new ArgumentException ("value");
135 public PerformanceCounterCategoryType CategoryType
{
142 public bool CounterExists (string counterName
)
144 return CounterExists (counterName
, categoryName
, machineName
);
147 public static bool CounterExists (string counterName
, string categoryName
)
149 return CounterExists (counterName
, categoryName
, ".");
152 // may throw ArgumentNullException, InvalidOperationException
153 // (categoryName is "", machine name is bad), Win32Exception
154 public static bool CounterExists (string counterName
, string categoryName
, string machineName
)
156 if (counterName
== null)
157 throw new ArgumentNullException ("counterName");
158 CheckCategory (categoryName
);
159 if (machineName
== null)
160 throw new ArgumentNullException ("machineName");
161 return CounterCategoryExists (counterName
, categoryName
, machineName
);
165 [Obsolete ("Use another overload that uses PerformanceCounterCategoryType instead")]
167 public static PerformanceCounterCategory
Create (
170 CounterCreationDataCollection counterData
)
172 return Create (categoryName
, categoryHelp
,
173 PerformanceCounterCategoryType
.Unknown
, counterData
);
177 [Obsolete ("Use another overload that uses PerformanceCounterCategoryType instead")]
179 public static PerformanceCounterCategory
Create (
185 return Create (categoryName
, categoryHelp
,
186 PerformanceCounterCategoryType
.Unknown
, counterName
, counterHelp
);
192 static PerformanceCounterCategory
Create (
195 PerformanceCounterCategoryType categoryType
,
196 CounterCreationDataCollection counterData
)
198 CheckCategory (categoryName
);
199 if (counterData
== null)
200 throw new ArgumentNullException ("counterData");
201 if (counterData
.Count
== 0)
202 throw new ArgumentException ("counterData");
203 CounterCreationData
[] items
= new CounterCreationData
[counterData
.Count
];
204 counterData
.CopyTo (items
, 0);
205 if (!Create (categoryName
, categoryHelp
, categoryType
, items
))
206 throw new InvalidOperationException ();
207 return new PerformanceCounterCategory (categoryName
, categoryHelp
);
213 static PerformanceCounterCategory
Create (
216 PerformanceCounterCategoryType categoryType
,
220 CheckCategory (categoryName
);
221 CounterCreationData
[] items
= new CounterCreationData
[1];
222 // we use PerformanceCounterType.NumberOfItems32 as the default type
223 items
[0] = new CounterCreationData (counterName
, counterHelp
, PerformanceCounterType
.NumberOfItems32
);
224 if (!Create (categoryName
, categoryHelp
, categoryType
, items
))
225 throw new InvalidOperationException ();
226 return new PerformanceCounterCategory (categoryName
, categoryHelp
);
229 public static void Delete (string categoryName
)
231 CheckCategory (categoryName
);
232 if (!CategoryDelete (categoryName
))
233 throw new InvalidOperationException ();
236 public static bool Exists (string categoryName
)
238 return Exists (categoryName
, ".");
241 public static bool Exists (string categoryName
, string machineName
)
243 CheckCategory (categoryName
);
244 return CounterCategoryExists (null, categoryName
, machineName
);
247 public static PerformanceCounterCategory
[] GetCategories ()
249 return GetCategories (".");
252 public static PerformanceCounterCategory
[] GetCategories (string machineName
)
254 if (machineName
== null)
255 throw new ArgumentNullException ("machineName");
256 string[] catnames
= GetCategoryNames (machineName
);
257 PerformanceCounterCategory
[] cats
= new PerformanceCounterCategory
[catnames
.Length
];
258 for (int i
= 0; i
< catnames
.Length
; ++i
)
259 cats
[i
] = new PerformanceCounterCategory (catnames
[i
], machineName
);
263 public PerformanceCounter
[] GetCounters ()
265 return GetCounters ("");
268 public PerformanceCounter
[] GetCounters (string instanceName
)
270 string[] countnames
= GetCounterNames (categoryName
, machineName
);
271 PerformanceCounter
[] counters
= new PerformanceCounter
[countnames
.Length
];
272 for (int i
= 0; i
< countnames
.Length
; ++i
) {
273 counters
[i
] = new PerformanceCounter (categoryName
, countnames
[i
], instanceName
, machineName
);
278 public string[] GetInstanceNames ()
280 return GetInstanceNames (categoryName
, machineName
);
283 public bool InstanceExists (string instanceName
)
285 return InstanceExists (instanceName
, categoryName
, machineName
);
288 public static bool InstanceExists (string instanceName
, string categoryName
)
290 return InstanceExists (instanceName
, categoryName
, ".");
293 public static bool InstanceExists (string instanceName
, string categoryName
, string machineName
)
295 if (instanceName
== null)
296 throw new ArgumentNullException ("instanceName");
297 CheckCategory (categoryName
);
298 if (machineName
== null)
299 throw new ArgumentNullException ("machineName");
300 int val
= InstanceExistsInternal (instanceName
, categoryName
, machineName
);
305 throw new InvalidOperationException ();
309 public InstanceDataCollectionCollection
ReadCategory ()
311 throw new NotImplementedException ();