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;
41 private PerformanceCounterCategoryType type = PerformanceCounterCategoryType.Unknown;
43 [MethodImplAttribute (MethodImplOptions.InternalCall)]
44 static extern bool CategoryDelete (string name);
46 [MethodImplAttribute (MethodImplOptions.InternalCall)]
47 static extern string CategoryHelpInternal (string category, string machine);
49 /* this icall allows a null counter and it will just search for the category */
50 [MethodImplAttribute (MethodImplOptions.InternalCall)]
51 static extern bool CounterCategoryExists (string counter, string category, string machine);
53 [MethodImplAttribute (MethodImplOptions.InternalCall)]
54 static extern bool Create (string categoryName, string categoryHelp,
55 PerformanceCounterCategoryType categoryType, CounterCreationData[] items);
57 [MethodImplAttribute (MethodImplOptions.InternalCall)]
58 static extern int InstanceExistsInternal (string instance, string category, string machine);
60 [MethodImplAttribute (MethodImplOptions.InternalCall)]
61 static extern string[] GetCategoryNames (string machine);
63 [MethodImplAttribute (MethodImplOptions.InternalCall)]
64 static extern string[] GetCounterNames (string category, string machine);
66 [MethodImplAttribute (MethodImplOptions.InternalCall)]
67 static extern string[] GetInstanceNames (string category, string machine);
69 static void CheckCategory (string categoryName) {
70 if (categoryName == null)
71 throw new ArgumentNullException ("categoryName");
72 if (categoryName == "")
73 throw new ArgumentException ("categoryName");
76 public PerformanceCounterCategory ()
81 // may throw ArgumentException (""), ArgumentNullException
82 public PerformanceCounterCategory (string categoryName)
83 : this (categoryName, ".")
87 // may throw ArgumentException (""), ArgumentNullException
88 public PerformanceCounterCategory (string categoryName, string machineName)
90 CheckCategory (categoryName);
91 if (machineName == null)
92 throw new ArgumentNullException ("machineName");
93 // TODO checks and whatever else is needed
94 this.categoryName = categoryName;
95 this.machineName = machineName;
98 // may throw InvalidOperationException, Win32Exception
99 public string CategoryHelp {
101 string res = CategoryHelpInternal (categoryName, machineName);
104 throw new InvalidOperationException ();
108 // may throw ArgumentException (""), ArgumentNullException
109 public string CategoryName {
110 get {return categoryName;}
113 throw new ArgumentNullException ("value");
115 throw new ArgumentException ("value");
116 categoryName = value;
120 // may throw ArgumentException
121 public string MachineName {
122 get {return machineName;}
125 throw new ArgumentNullException ("value");
127 throw new ArgumentException ("value");
132 public PerformanceCounterCategoryType CategoryType {
138 public bool CounterExists (string counterName)
140 return CounterExists (counterName, categoryName, machineName);
143 public static bool CounterExists (string counterName, string categoryName)
145 return CounterExists (counterName, categoryName, ".");
148 // may throw ArgumentNullException, InvalidOperationException
149 // (categoryName is "", machine name is bad), Win32Exception
150 public static bool CounterExists (string counterName, string categoryName, string machineName)
152 if (counterName == null)
153 throw new ArgumentNullException ("counterName");
154 CheckCategory (categoryName);
155 if (machineName == null)
156 throw new ArgumentNullException ("machineName");
157 return CounterCategoryExists (counterName, categoryName, machineName);
160 [Obsolete ("Use another overload that uses PerformanceCounterCategoryType instead")]
161 public static PerformanceCounterCategory Create (
164 CounterCreationDataCollection counterData)
166 return Create (categoryName, categoryHelp,
167 PerformanceCounterCategoryType.Unknown, counterData);
170 [Obsolete ("Use another overload that uses PerformanceCounterCategoryType instead")]
171 public static PerformanceCounterCategory Create (
177 return Create (categoryName, categoryHelp,
178 PerformanceCounterCategoryType.Unknown, counterName, counterHelp);
181 public static PerformanceCounterCategory Create (
184 PerformanceCounterCategoryType categoryType,
185 CounterCreationDataCollection counterData)
187 CheckCategory (categoryName);
188 if (counterData == null)
189 throw new ArgumentNullException ("counterData");
190 if (counterData.Count == 0)
191 throw new ArgumentException ("counterData");
192 CounterCreationData[] items = new CounterCreationData [counterData.Count];
193 counterData.CopyTo (items, 0);
194 if (!Create (categoryName, categoryHelp, categoryType, items))
195 throw new InvalidOperationException ();
196 return new PerformanceCounterCategory (categoryName, categoryHelp);
199 public static PerformanceCounterCategory Create (
202 PerformanceCounterCategoryType categoryType,
206 CheckCategory (categoryName);
207 CounterCreationData[] items = new CounterCreationData [1];
208 // we use PerformanceCounterType.NumberOfItems32 as the default type
209 items [0] = new CounterCreationData (counterName, counterHelp, PerformanceCounterType.NumberOfItems32);
210 if (!Create (categoryName, categoryHelp, categoryType, items))
211 throw new InvalidOperationException ();
212 return new PerformanceCounterCategory (categoryName, categoryHelp);
215 public static void Delete (string categoryName)
217 CheckCategory (categoryName);
218 if (!CategoryDelete (categoryName))
219 throw new InvalidOperationException ();
222 public static bool Exists (string categoryName)
224 return Exists (categoryName, ".");
227 public static bool Exists (string categoryName, string machineName)
229 CheckCategory (categoryName);
230 return CounterCategoryExists (null, categoryName, machineName);
233 public static PerformanceCounterCategory[] GetCategories ()
235 return GetCategories (".");
238 public static PerformanceCounterCategory[] GetCategories (string machineName)
240 if (machineName == null)
241 throw new ArgumentNullException ("machineName");
242 string[] catnames = GetCategoryNames (machineName);
243 PerformanceCounterCategory[] cats = new PerformanceCounterCategory [catnames.Length];
244 for (int i = 0; i < catnames.Length; ++i)
245 cats [i] = new PerformanceCounterCategory (catnames [i], machineName);
249 public PerformanceCounter[] GetCounters ()
251 return GetCounters ("");
254 public PerformanceCounter[] GetCounters (string instanceName)
256 string[] countnames = GetCounterNames (categoryName, machineName);
257 PerformanceCounter[] counters = new PerformanceCounter [countnames.Length];
258 for (int i = 0; i < countnames.Length; ++i) {
259 counters [i] = new PerformanceCounter (categoryName, countnames [i], instanceName, machineName);
264 public string[] GetInstanceNames ()
266 return GetInstanceNames (categoryName, machineName);
269 public bool InstanceExists (string instanceName)
271 return InstanceExists (instanceName, categoryName, machineName);
274 public static bool InstanceExists (string instanceName, string categoryName)
276 return InstanceExists (instanceName, categoryName, ".");
279 public static bool InstanceExists (string instanceName, string categoryName, string machineName)
281 if (instanceName == null)
282 throw new ArgumentNullException ("instanceName");
283 CheckCategory (categoryName);
284 if (machineName == null)
285 throw new ArgumentNullException ("machineName");
286 int val = InstanceExistsInternal (instanceName, categoryName, machineName);
291 throw new InvalidOperationException ();
295 public InstanceDataCollectionCollection ReadCategory ()
297 throw new NotImplementedException ();