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 private PerformanceCounterCategoryType type = PerformanceCounterCategoryType.Unknown; 42 43 [MethodImplAttribute (MethodImplOptions.InternalCall)] CategoryDelete(string name)44 static extern bool CategoryDelete (string name); 45 46 [MethodImplAttribute (MethodImplOptions.InternalCall)] CategoryHelpInternal(string category, string machine)47 static extern string CategoryHelpInternal (string category, string machine); 48 49 /* this icall allows a null counter and it will just search for the category */ 50 [MethodImplAttribute (MethodImplOptions.InternalCall)] CounterCategoryExists(string counter, string category, string machine)51 static extern bool CounterCategoryExists (string counter, string category, string machine); 52 53 [MethodImplAttribute (MethodImplOptions.InternalCall)] Create(string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationData[] items)54 static extern bool Create (string categoryName, string categoryHelp, 55 PerformanceCounterCategoryType categoryType, CounterCreationData[] items); 56 57 [MethodImplAttribute (MethodImplOptions.InternalCall)] InstanceExistsInternal(string instance, string category, string machine)58 static extern int InstanceExistsInternal (string instance, string category, string machine); 59 60 [MethodImplAttribute (MethodImplOptions.InternalCall)] GetCategoryNames(string machine)61 static extern string[] GetCategoryNames (string machine); 62 63 [MethodImplAttribute (MethodImplOptions.InternalCall)] GetCounterNames(string category, string machine)64 static extern string[] GetCounterNames (string category, string machine); 65 66 [MethodImplAttribute (MethodImplOptions.InternalCall)] GetInstanceNames(string category, string machine)67 static extern string[] GetInstanceNames (string category, string machine); 68 CheckCategory(string categoryName)69 static void CheckCategory (string categoryName) { 70 if (categoryName == null) 71 throw new ArgumentNullException ("categoryName"); 72 if (categoryName == "") 73 throw new ArgumentException ("categoryName"); 74 } 75 PerformanceCounterCategory()76 public PerformanceCounterCategory () 77 : this ("", ".") 78 { 79 } 80 81 // may throw ArgumentException (""), ArgumentNullException PerformanceCounterCategory(string categoryName)82 public PerformanceCounterCategory (string categoryName) 83 : this (categoryName, ".") 84 { 85 } 86 87 // may throw ArgumentException (""), ArgumentNullException PerformanceCounterCategory(string categoryName, string machineName)88 public PerformanceCounterCategory (string categoryName, string machineName) 89 { 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; 96 } 97 98 // may throw InvalidOperationException, Win32Exception 99 public string CategoryHelp { 100 get { 101 string res = CategoryHelpInternal (categoryName, machineName); 102 if (res != null) 103 return res; 104 throw new InvalidOperationException (); 105 } 106 } 107 108 // may throw ArgumentException (""), ArgumentNullException 109 public string CategoryName { 110 get {return categoryName;} 111 set { 112 if (value == null) 113 throw new ArgumentNullException ("value"); 114 if (value == "") 115 throw new ArgumentException ("value"); 116 categoryName = value; 117 } 118 } 119 120 // may throw ArgumentException 121 public string MachineName { 122 get {return machineName;} 123 set { 124 if (value == null) 125 throw new ArgumentNullException ("value"); 126 if (value == "") 127 throw new ArgumentException ("value"); 128 machineName = value; 129 } 130 } 131 132 public PerformanceCounterCategoryType CategoryType { 133 get { 134 return type; 135 } 136 } 137 CounterExists(string counterName)138 public bool CounterExists (string counterName) 139 { 140 return CounterExists (counterName, categoryName, machineName); 141 } 142 CounterExists(string counterName, string categoryName)143 public static bool CounterExists (string counterName, string categoryName) 144 { 145 return CounterExists (counterName, categoryName, "."); 146 } 147 148 // may throw ArgumentNullException, InvalidOperationException 149 // (categoryName is "", machine name is bad), Win32Exception CounterExists(string counterName, string categoryName, string machineName)150 public static bool CounterExists (string counterName, string categoryName, string machineName) 151 { 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); 158 } 159 160 [Obsolete ("Use another overload that uses PerformanceCounterCategoryType instead")] Create( string categoryName, string categoryHelp, CounterCreationDataCollection counterData)161 public static PerformanceCounterCategory Create ( 162 string categoryName, 163 string categoryHelp, 164 CounterCreationDataCollection counterData) 165 { 166 return Create (categoryName, categoryHelp, 167 PerformanceCounterCategoryType.Unknown, counterData); 168 } 169 170 [Obsolete ("Use another overload that uses PerformanceCounterCategoryType instead")] Create( string categoryName, string categoryHelp, string counterName, string counterHelp)171 public static PerformanceCounterCategory Create ( 172 string categoryName, 173 string categoryHelp, 174 string counterName, 175 string counterHelp) 176 { 177 return Create (categoryName, categoryHelp, 178 PerformanceCounterCategoryType.Unknown, counterName, counterHelp); 179 } 180 Create( string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData)181 public static PerformanceCounterCategory Create ( 182 string categoryName, 183 string categoryHelp, 184 PerformanceCounterCategoryType categoryType, 185 CounterCreationDataCollection counterData) 186 { 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); 197 } 198 Create( string categoryName, string categoryHelp, PerformanceCounterCategoryType categoryType, string counterName, string counterHelp)199 public static PerformanceCounterCategory Create ( 200 string categoryName, 201 string categoryHelp, 202 PerformanceCounterCategoryType categoryType, 203 string counterName, 204 string counterHelp) 205 { 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); 213 } 214 Delete(string categoryName)215 public static void Delete (string categoryName) 216 { 217 CheckCategory (categoryName); 218 if (!CategoryDelete (categoryName)) 219 throw new InvalidOperationException (); 220 } 221 Exists(string categoryName)222 public static bool Exists (string categoryName) 223 { 224 return Exists (categoryName, "."); 225 } 226 Exists(string categoryName, string machineName)227 public static bool Exists (string categoryName, string machineName) 228 { 229 CheckCategory (categoryName); 230 return CounterCategoryExists (null, categoryName, machineName); 231 } 232 GetCategories()233 public static PerformanceCounterCategory[] GetCategories () 234 { 235 return GetCategories ("."); 236 } 237 GetCategories(string machineName)238 public static PerformanceCounterCategory[] GetCategories (string machineName) 239 { 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); 246 return cats; 247 } 248 GetCounters()249 public PerformanceCounter[] GetCounters () 250 { 251 return GetCounters (""); 252 } 253 GetCounters(string instanceName)254 public PerformanceCounter[] GetCounters (string instanceName) 255 { 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); 260 } 261 return counters; 262 } 263 GetInstanceNames()264 public string[] GetInstanceNames () 265 { 266 return GetInstanceNames (categoryName, machineName); 267 } 268 InstanceExists(string instanceName)269 public bool InstanceExists (string instanceName) 270 { 271 return InstanceExists (instanceName, categoryName, machineName); 272 } 273 InstanceExists(string instanceName, string categoryName)274 public static bool InstanceExists (string instanceName, string categoryName) 275 { 276 return InstanceExists (instanceName, categoryName, "."); 277 } 278 InstanceExists(string instanceName, string categoryName, string machineName)279 public static bool InstanceExists (string instanceName, string categoryName, string machineName) 280 { 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); 287 if (val == 0) 288 return false; 289 if (val == 1) 290 return true; 291 throw new InvalidOperationException (); 292 } 293 294 [MonoTODO] ReadCategory()295 public InstanceDataCollectionCollection ReadCategory () 296 { 297 throw new NotImplementedException (); 298 } 299 } 300 } 301 302