System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System / System.Diagnostics / PerformanceCounterCategory.cs
1 //
2 // System.Diagnostics.PerformanceCounterCategory.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2002
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Security.Permissions;
32 using System.Runtime.CompilerServices;
33
34 namespace System.Diagnostics 
35 {
36         [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
37         public sealed class PerformanceCounterCategory 
38         {
39                 private string categoryName;
40                 private string machineName;
41 #if NET_2_0
42                 private PerformanceCounterCategoryType type = PerformanceCounterCategoryType.Unknown;
43 #endif          
44
45                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
46                 static extern bool CategoryDelete (string name);
47
48                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
49                 static extern string CategoryHelpInternal (string category, string machine);
50
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);
54
55                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
56                 static extern bool Create (string categoryName, string categoryHelp,
57                         PerformanceCounterCategoryType categoryType, CounterCreationData[] items);
58
59                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
60                 static extern int InstanceExistsInternal (string instance, string category, string machine);
61
62                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
63                 static extern string[] GetCategoryNames (string machine);
64
65                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
66                 static extern string[] GetCounterNames (string category, string machine);
67
68                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
69                 static extern string[] GetInstanceNames (string category, string machine);
70
71                 static void CheckCategory (string categoryName) {
72                         if (categoryName == null)
73                                 throw new ArgumentNullException ("categoryName");
74                         if (categoryName == "")
75                                 throw new ArgumentException ("categoryName");
76                 }
77
78                 public PerformanceCounterCategory ()
79                         : this ("", ".")
80                 {
81                 }
82
83                 // may throw ArgumentException (""), ArgumentNullException
84                 public PerformanceCounterCategory (string categoryName)
85                         : this (categoryName, ".")
86                 {
87                 }
88
89                 // may throw ArgumentException (""), ArgumentNullException
90                 public PerformanceCounterCategory (string categoryName, string machineName)
91                 {
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;
98                 }
99
100                 // may throw InvalidOperationException, Win32Exception
101                 public string CategoryHelp {
102                         get {
103                                 string res = CategoryHelpInternal (categoryName, machineName);
104                                 if (res != null)
105                                         return res;
106                                 throw new InvalidOperationException ();
107                         }
108                 }
109
110                 // may throw ArgumentException (""), ArgumentNullException
111                 public string CategoryName {
112                         get {return categoryName;}
113                         set {
114                                 if (value == null)
115                                         throw new ArgumentNullException ("value");
116                                 if (value == "")
117                                         throw new ArgumentException ("value");
118                                 categoryName = value;
119                         }
120                 }
121
122                 // may throw ArgumentException
123                 public string MachineName {
124                         get {return machineName;}
125                         set {
126                                 if (value == null)
127                                         throw new ArgumentNullException ("value");
128                                 if (value == "")
129                                         throw new ArgumentException ("value");
130                                 machineName = value;
131                         }
132                 }
133
134 #if NET_2_0
135                 public PerformanceCounterCategoryType CategoryType {
136                         get {
137                                 return type;
138                         }
139                 }
140 #endif
141
142                 public bool CounterExists (string counterName)
143                 {
144                         return CounterExists (counterName, categoryName, machineName);
145                 }
146
147                 public static bool CounterExists (string counterName, string categoryName)
148                 {
149                         return CounterExists (counterName, categoryName, ".");
150                 }
151
152                 // may throw ArgumentNullException, InvalidOperationException
153                 // (categoryName is "", machine name is bad), Win32Exception
154                 public static bool CounterExists (string counterName, string categoryName, string machineName)
155                 {
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);
162                 }
163
164 #if NET_2_0
165                 [Obsolete ("Use another overload that uses PerformanceCounterCategoryType instead")]
166 #endif
167                 public static PerformanceCounterCategory Create (
168                         string categoryName,
169                         string categoryHelp,
170                         CounterCreationDataCollection counterData)
171                 {
172                         return Create (categoryName, categoryHelp,
173                                 PerformanceCounterCategoryType.Unknown, counterData);
174                 }
175
176 #if NET_2_0
177                 [Obsolete ("Use another overload that uses PerformanceCounterCategoryType instead")]
178 #endif
179                 public static PerformanceCounterCategory Create (
180                         string categoryName,
181                         string categoryHelp,
182                         string counterName,
183                         string counterHelp)
184                 {
185                         return Create (categoryName, categoryHelp,
186                                 PerformanceCounterCategoryType.Unknown, counterName, counterHelp);
187                 }
188
189 #if NET_2_0
190                 public
191 #endif
192                 static PerformanceCounterCategory Create (
193                         string categoryName,
194                         string categoryHelp,
195                         PerformanceCounterCategoryType categoryType,
196                         CounterCreationDataCollection counterData)
197                 {
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);
208                 }
209
210 #if NET_2_0
211                 public
212 #endif
213                 static PerformanceCounterCategory Create (
214                         string categoryName,
215                         string categoryHelp,
216                         PerformanceCounterCategoryType categoryType,
217                         string counterName,
218                         string counterHelp)
219                 {
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);
227                 }
228
229                 public static void Delete (string categoryName)
230                 {
231                         CheckCategory (categoryName);
232                         if (!CategoryDelete (categoryName))
233                                 throw new InvalidOperationException ();
234                 }
235
236                 public static bool Exists (string categoryName)
237                 {
238                         return Exists (categoryName, ".");
239                 }
240
241                 public static bool Exists (string categoryName, string machineName)
242                 {
243                         CheckCategory (categoryName);
244                         return CounterCategoryExists (null, categoryName, machineName);
245                 }
246
247                 public static PerformanceCounterCategory[] GetCategories ()
248                 {
249                         return GetCategories (".");
250                 }
251
252                 public static PerformanceCounterCategory[] GetCategories (string machineName)
253                 {
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);
260                         return cats;
261                 }
262
263                 public PerformanceCounter[] GetCounters ()
264                 {
265                         return GetCounters ("");
266                 }
267
268                 public PerformanceCounter[] GetCounters (string instanceName)
269                 {
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);
274                         }
275                         return counters;
276                 }
277
278                 public string[] GetInstanceNames ()
279                 {
280                         return GetInstanceNames (categoryName, machineName);
281                 }
282
283                 public bool InstanceExists (string instanceName)
284                 {
285                         return InstanceExists (instanceName, categoryName, machineName);
286                 }
287
288                 public static bool InstanceExists (string instanceName, string categoryName)
289                 {
290                         return InstanceExists (instanceName, categoryName, ".");
291                 }
292
293                 public static bool InstanceExists (string instanceName, string categoryName, string machineName)
294                 {
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);
301                         if (val == 0)
302                                 return false;
303                         if (val == 1)
304                                 return true;
305                         throw new InvalidOperationException ();
306                 }
307
308                 [MonoTODO]
309                 public InstanceDataCollectionCollection ReadCategory ()
310                 {
311                         throw new NotImplementedException ();
312                 }
313         }
314 }
315