2 // System.Diagnostics.PerformanceCounter.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.ComponentModel
;
35 using System
.ComponentModel
.Design
;
36 using System
.Diagnostics
;
37 using System
.Runtime
.InteropServices
;
38 using System
.Runtime
.CompilerServices
;
39 using System
.Runtime
.ConstrainedExecution
;
41 namespace System
.Diagnostics
{
43 // must be safe for multithreaded operations
44 [InstallerType (typeof (PerformanceCounterInstaller
))]
45 public sealed class PerformanceCounter
: Component
, ISupportInitialize
48 private string categoryName
;
49 private string counterName
;
50 private string instanceName
;
51 private string machineName
;
53 PerformanceCounterType type
;
54 CounterSample old_sample
;
55 private bool readOnly
;
59 private PerformanceCounterInstanceLifetime lifetime
;
62 public static int DefaultFileMappingSize
= 524288;
64 // set catname, countname, instname to "", machname to "."
65 public PerformanceCounter ()
67 categoryName
= counterName
= instanceName
= "";
71 // throws: InvalidOperationException (if catName or countName
72 // is ""); ArgumentNullException if either is null
73 // sets instName to "", machname to "."
74 public PerformanceCounter (String categoryName
,
76 : this (categoryName
, counterName
, false)
80 public PerformanceCounter (string categoryName
,
83 : this (categoryName
, counterName
, "", readOnly
)
87 public PerformanceCounter (string categoryName
,
90 : this (categoryName
, counterName
, instanceName
, false)
94 public PerformanceCounter (string categoryName
,
100 if (categoryName
== null)
101 throw new ArgumentNullException ("categoryName");
102 if (counterName
== null)
103 throw new ArgumentNullException ("counterName");
104 if (instanceName
== null)
105 throw new ArgumentNullException ("instanceName");
106 CategoryName
= categoryName
;
107 CounterName
= counterName
;
109 if (categoryName
== "" || counterName
== "")
110 throw new InvalidOperationException ();
112 InstanceName
= instanceName
;
113 this.instanceName
= instanceName
;
114 this.machineName
= ".";
115 this.readOnly
= readOnly
;
119 public PerformanceCounter (string categoryName
,
123 : this (categoryName
, counterName
, instanceName
, false)
125 this.machineName
= machineName
;
128 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
129 static private unsafe extern IntPtr
GetImpl_icall (char *category
, int category_length
,
130 char *counter
, int counter_length
, char *instance
, int instance_length
,
131 out PerformanceCounterType ctype
, out bool custom
);
133 static unsafe IntPtr
GetImpl (string category
, string counter
,
134 string instance
, out PerformanceCounterType ctype
, out bool custom
)
136 fixed (char* fixed_category
= category
,
137 fixed_counter
= counter
,
138 fixed_instance
= instance
) {
139 return GetImpl_icall (fixed_category
, category
?.Length
?? 0,
140 fixed_counter
, counter
?.Length
?? 0,
141 fixed_instance
, instance
?.Length
?? 0,
142 out ctype
, out custom
);
146 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
147 static extern bool GetSample (IntPtr impl
, bool only_value
, out CounterSample sample
);
149 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
150 static extern long UpdateValue (IntPtr impl
, bool do_incr
, long value);
152 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
153 static extern void FreeData (IntPtr impl
);
155 static bool IsValidMachine (string machine
)
156 { // no support for counters on other machines
157 return machine
== ".";
160 /* the perf counter has changed, ensure it's valid and setup it to
161 * be able to collect/update data
165 // need to free the previous info
166 if (impl
!= IntPtr
.Zero
)
169 if (IsValidMachine (machineName
))
170 impl
= GetImpl (categoryName
, counterName
, instanceName
, out type
, out is_custom
);
171 // system counters are always readonly
174 // invalid counter, need to handle out of mem
176 // TODO: reenable this
177 //if (impl == IntPtr.Zero)
178 // throw new InvalidOperationException ();
182 // may throw ArgumentNullException
183 [DefaultValue (""), ReadOnly (true), SettingsBindable (true)]
184 [TypeConverter ("System.Diagnostics.Design.CategoryValueConverter, " + Consts
.AssemblySystem_Design
)]
185 [SRDescription ("The category name for this performance counter.")]
186 public string CategoryName
{
187 get {return categoryName;}
190 throw new ArgumentNullException ("categoryName");
191 categoryName
= value;
196 // may throw InvalidOperationException
198 [ReadOnly (true), DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
199 [MonitoringDescription ("A description describing the counter.")]
200 public string CounterHelp
{
204 // may throw ArgumentNullException
205 [DefaultValue (""), ReadOnly (true), SettingsBindable (true)]
206 [TypeConverter ("System.Diagnostics.Design.CounterNameConverter, " + Consts
.AssemblySystem_Design
)]
207 [SRDescription ("The name of this performance counter.")]
208 public string CounterName
210 get {return counterName;}
213 throw new ArgumentNullException ("counterName");
219 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
220 [MonitoringDescription ("The type of the counter.")]
221 public PerformanceCounterType CounterType
{
230 [DefaultValue (PerformanceCounterInstanceLifetime
.Global
)]
231 public PerformanceCounterInstanceLifetime InstanceLifetime
{
232 get { return lifetime; }
233 set { lifetime = value; }
236 [DefaultValue (""), ReadOnly (true), SettingsBindable (true)]
237 [TypeConverter ("System.Diagnostics.Design.InstanceNameConverter, " + Consts
.AssemblySystem_Design
)]
238 [SRDescription ("The instance name for this performance counter.")]
239 public string InstanceName
{
240 get {return instanceName;}
243 throw new ArgumentNullException ("value");
244 instanceName
= value;
249 // may throw ArgumentException if machine name format is wrong
250 [MonoTODO("What's the machine name format?")]
251 [DefaultValue ("."), Browsable (false), SettingsBindable (true)]
252 [SRDescription ("The machine where this performance counter resides.")]
253 public string MachineName
{
254 get {return machineName;}
257 throw new ArgumentNullException ("value");
258 if (value == "" || value == ".") {
263 throw new PlatformNotSupportedException ();
267 // may throw InvalidOperationException, Win32Exception
268 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
269 [MonitoringDescription ("The raw value of the counter.")]
270 public long RawValue
{
272 CounterSample sample
;
275 GetSample (impl
, true, out sample
);
276 // should this update old_sample as well?
277 return sample
.RawValue
;
283 throw new InvalidOperationException ();
284 UpdateValue (impl
, false, value);
288 [Browsable (false), DefaultValue (true)]
289 [MonitoringDescription ("The accessability level of the counter.")]
290 public bool ReadOnly
{
291 get {return readOnly;}
292 set {readOnly = value;}
295 public void BeginInit ()
297 // we likely don't need to do anything significant here
300 public void EndInit ()
302 // we likely don't need to do anything significant here
309 if (p
!= IntPtr
.Zero
)
313 public static void CloseSharedResources ()
315 // we likely don't need to do anything significant here
318 // may throw InvalidOperationException, Win32Exception
319 public long Decrement ()
321 return IncrementBy (-1);
324 protected override void Dispose (bool disposing
)
329 // may throw InvalidOperationException, Win32Exception
330 public long Increment ()
332 return IncrementBy (1);
335 // may throw InvalidOperationException, Win32Exception
336 [ReliabilityContract (Consistency
.WillNotCorruptState
, Cer
.MayFail
)]
337 public long IncrementBy (long value)
342 // FIXME: This should really throw, but by now set this workaround in place.
343 //throw new InvalidOperationException ();
346 return UpdateValue (impl
, true, value);
349 // may throw InvalidOperationException, Win32Exception
350 public CounterSample
NextSample ()
352 CounterSample sample
;
355 GetSample (impl
, false, out sample
);
361 // may throw InvalidOperationException, Win32Exception
362 public float NextValue ()
364 CounterSample sample
;
367 GetSample (impl
, false, out sample
);
370 val
= CounterSampleCalculator
.ComputeCounterValue (old_sample
, sample
);
372 val
= CounterSampleCalculator
.ComputeCounterValue (sample
);
378 // may throw InvalidOperationException, Win32Exception
380 [ReliabilityContract (Consistency
.WillNotCorruptState
, Cer
.MayFail
)]
381 public void RemoveInstance ()
383 throw new NotImplementedException ();