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
;
40 using System
.Runtime
.ConstrainedExecution
;
43 namespace System
.Diagnostics
{
45 // must be safe for multithreaded operations
47 [Designer ("Microsoft.VisualStudio.Install.PerformanceCounterDesigner, " + Consts
.AssemblyMicrosoft_VisualStudio
)]
49 [InstallerType (typeof (PerformanceCounterInstaller
))]
50 public sealed class PerformanceCounter
: Component
, ISupportInitialize
53 private string categoryName
;
54 private string counterName
;
55 private string instanceName
;
56 private string machineName
;
58 PerformanceCounterType type
;
59 CounterSample old_sample
;
60 private bool readOnly
;
65 private PerformanceCounterInstanceLifetime lifetime
;
71 public static int DefaultFileMappingSize
= 524288;
73 // set catname, countname, instname to "", machname to "."
74 public PerformanceCounter ()
76 categoryName
= counterName
= instanceName
= "";
80 // throws: InvalidOperationException (if catName or countName
81 // is ""); ArgumentNullException if either is null
82 // sets instName to "", machname to "."
83 public PerformanceCounter (String categoryName
,
85 : this (categoryName
, counterName
, false)
89 public PerformanceCounter (string categoryName
,
92 : this (categoryName
, counterName
, "", readOnly
)
96 public PerformanceCounter (string categoryName
,
99 : this (categoryName
, counterName
, instanceName
, false)
103 public PerformanceCounter (string categoryName
,
109 if (categoryName
== null)
110 throw new ArgumentNullException ("categoryName");
111 if (counterName
== null)
112 throw new ArgumentNullException ("counterName");
113 if (instanceName
== null)
114 throw new ArgumentNullException ("instanceName");
115 CategoryName
= categoryName
;
116 CounterName
= counterName
;
118 if (categoryName
== "" || counterName
== "")
119 throw new InvalidOperationException ();
121 InstanceName
= instanceName
;
122 this.instanceName
= instanceName
;
123 this.machineName
= ".";
124 this.readOnly
= readOnly
;
128 public PerformanceCounter (string categoryName
,
132 : this (categoryName
, counterName
, instanceName
, false)
134 this.machineName
= machineName
;
137 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
138 static extern IntPtr
GetImpl (string category
, string counter
,
139 string instance
, string machine
, out PerformanceCounterType ctype
, out bool custom
);
141 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
142 static extern bool GetSample (IntPtr impl
, bool only_value
, out CounterSample sample
);
144 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
145 static extern long UpdateValue (IntPtr impl
, bool do_incr
, long value);
147 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
148 static extern void FreeData (IntPtr impl
);
150 /* the perf counter has changed, ensure it's valid and setup it to
151 * be able to collect/update data
155 // need to free the previous info
156 if (impl
!= IntPtr
.Zero
)
158 impl
= GetImpl (categoryName
, counterName
, instanceName
, machineName
, out type
, out is_custom
);
159 // system counters are always readonly
162 // invalid counter, need to handle out of mem
164 // TODO: reenable this
165 //if (impl == IntPtr.Zero)
166 // throw new InvalidOperationException ();
170 // may throw ArgumentNullException
171 [DefaultValue (""), ReadOnly (true), RecommendedAsConfigurable (true)]
172 [TypeConverter ("System.Diagnostics.Design.CategoryValueConverter, " + Consts
.AssemblySystem_Design
)]
173 [SRDescription ("The category name for this performance counter.")]
174 public string CategoryName
{
175 get {return categoryName;}
178 throw new ArgumentNullException ("categoryName");
179 categoryName
= value;
184 // may throw InvalidOperationException
186 [ReadOnly (true), DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
187 [MonitoringDescription ("A description describing the counter.")]
188 public string CounterHelp
{
192 // may throw ArgumentNullException
193 [DefaultValue (""), ReadOnly (true), RecommendedAsConfigurable (true)]
194 [TypeConverter ("System.Diagnostics.Design.CounterNameConverter, " + Consts
.AssemblySystem_Design
)]
195 [SRDescription ("The name of this performance counter.")]
196 public string CounterName
198 get {return counterName;}
201 throw new ArgumentNullException ("counterName");
207 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
208 [MonitoringDescription ("The type of the counter.")]
209 public PerformanceCounterType CounterType
{
219 [DefaultValue (PerformanceCounterInstanceLifetime
.Global
)]
220 public PerformanceCounterInstanceLifetime InstanceLifetime
{
221 get { return lifetime; }
222 set { lifetime = value; }
226 [DefaultValue (""), ReadOnly (true), RecommendedAsConfigurable (true)]
227 [TypeConverter ("System.Diagnostics.Design.InstanceNameConverter, " + Consts
.AssemblySystem_Design
)]
228 [SRDescription ("The instance name for this performance counter.")]
229 public string InstanceName
{
230 get {return instanceName;}
233 throw new ArgumentNullException ("value");
234 instanceName
= value;
239 // may throw ArgumentException if machine name format is wrong
240 [MonoTODO("What's the machine name format?")]
241 [DefaultValue ("."), Browsable (false), RecommendedAsConfigurable (true)]
242 [SRDescription ("The machine where this performance counter resides.")]
243 public string MachineName
{
244 get {return machineName;}
247 throw new ArgumentNullException ("value");
248 if (value == "" || value == ".") {
253 throw new PlatformNotSupportedException ();
257 // may throw InvalidOperationException, Win32Exception
258 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
259 [MonitoringDescription ("The raw value of the counter.")]
260 public long RawValue
{
262 CounterSample sample
;
265 GetSample (impl
, true, out sample
);
266 // should this update old_sample as well?
267 return sample
.RawValue
;
273 throw new InvalidOperationException ();
274 UpdateValue (impl
, false, value);
278 [Browsable (false), DefaultValue (true)]
279 [MonitoringDescription ("The accessability level of the counter.")]
280 public bool ReadOnly
{
281 get {return readOnly;}
282 set {readOnly = value;}
285 public void BeginInit ()
287 // we likely don't need to do anything significant here
290 public void EndInit ()
292 // we likely don't need to do anything significant here
299 if (p
!= IntPtr
.Zero
)
303 public static void CloseSharedResources ()
305 // we likely don't need to do anything significant here
308 // may throw InvalidOperationException, Win32Exception
309 public long Decrement ()
311 return IncrementBy (-1);
314 protected override void Dispose (bool disposing
)
319 // may throw InvalidOperationException, Win32Exception
320 public long Increment ()
322 return IncrementBy (1);
325 // may throw InvalidOperationException, Win32Exception
327 [ReliabilityContract (Consistency
.WillNotCorruptState
, Cer
.MayFail
)]
329 public long IncrementBy (long value)
334 // FIXME: This should really throw, but by now set this workaround in place.
335 //throw new InvalidOperationException ();
338 return UpdateValue (impl
, true, value);
341 // may throw InvalidOperationException, Win32Exception
342 public CounterSample
NextSample ()
344 CounterSample sample
;
347 GetSample (impl
, false, out sample
);
353 // may throw InvalidOperationException, Win32Exception
354 public float NextValue ()
356 CounterSample sample
;
359 GetSample (impl
, false, out sample
);
362 val
= CounterSampleCalculator
.ComputeCounterValue (old_sample
, sample
);
364 val
= CounterSampleCalculator
.ComputeCounterValue (sample
);
370 // may throw InvalidOperationException, Win32Exception
373 [ReliabilityContract (Consistency
.WillNotCorruptState
, Cer
.MayFail
)]
375 public void RemoveInstance ()
377 throw new NotImplementedException ();