[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / System / System.Diagnostics / PerformanceCounter.cs
blobe388d063edd13c35b71e5914e9c22570662bca6f
1 //
2 // System.Diagnostics.PerformanceCounter.cs
3 //
4 // Authors:
5 // Jonathan Pryor (jonpryor@vt.edu)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) 2002
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:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
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.
33 using System;
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;
52 IntPtr impl;
53 PerformanceCounterType type;
54 CounterSample old_sample;
55 private bool readOnly;
56 bool valid_old;
57 bool changed;
58 bool is_custom;
59 private PerformanceCounterInstanceLifetime lifetime;
61 [Obsolete]
62 public static int DefaultFileMappingSize = 524288;
64 // set catname, countname, instname to "", machname to "."
65 public PerformanceCounter ()
67 categoryName = counterName = instanceName = "";
68 machineName = ".";
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,
75 string counterName)
76 : this (categoryName, counterName, false)
80 public PerformanceCounter (string categoryName,
81 string counterName,
82 bool readOnly)
83 : this (categoryName, counterName, "", readOnly)
87 public PerformanceCounter (string categoryName,
88 string counterName,
89 string instanceName)
90 : this (categoryName, counterName, instanceName, false)
94 public PerformanceCounter (string categoryName,
95 string counterName,
96 string instanceName,
97 bool readOnly)
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;
116 changed = true;
119 public PerformanceCounter (string categoryName,
120 string counterName,
121 string instanceName,
122 string machineName)
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
163 void UpdateInfo ()
165 // need to free the previous info
166 if (impl != IntPtr.Zero)
167 Close ();
169 if (IsValidMachine (machineName))
170 impl = GetImpl (categoryName, counterName, instanceName, out type, out is_custom);
171 // system counters are always readonly
172 if (!is_custom)
173 readOnly = true;
174 // invalid counter, need to handle out of mem
176 // TODO: reenable this
177 //if (impl == IntPtr.Zero)
178 // throw new InvalidOperationException ();
179 changed = false;
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;}
188 set {
189 if (value == null)
190 throw new ArgumentNullException ("categoryName");
191 categoryName = value;
192 changed = true;
196 // may throw InvalidOperationException
197 [MonoTODO]
198 [ReadOnly (true), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
199 [MonitoringDescription ("A description describing the counter.")]
200 public string CounterHelp {
201 get {return "";}
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;}
211 set {
212 if (value == null)
213 throw new ArgumentNullException ("counterName");
214 counterName = value;
215 changed = true;
219 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
220 [MonitoringDescription ("The type of the counter.")]
221 public PerformanceCounterType CounterType {
222 get {
223 if (changed)
224 UpdateInfo ();
225 return type;
229 [MonoTODO]
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;}
241 set {
242 if (value == null)
243 throw new ArgumentNullException ("value");
244 instanceName = value;
245 changed = true;
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;}
255 set {
256 if (value == null)
257 throw new ArgumentNullException ("value");
258 if (value == "" || value == ".") {
259 machineName = ".";
260 changed = true;
261 return;
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 {
271 get {
272 CounterSample sample;
273 if (changed)
274 UpdateInfo ();
275 GetSample (impl, true, out sample);
276 // should this update old_sample as well?
277 return sample.RawValue;
279 set {
280 if (changed)
281 UpdateInfo ();
282 if (readOnly)
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
305 public void Close ()
307 IntPtr p = impl;
308 impl = IntPtr.Zero;
309 if (p != IntPtr.Zero)
310 FreeData (p);
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)
326 Close ();
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)
339 if (changed)
340 UpdateInfo ();
341 if (readOnly) {
342 // FIXME: This should really throw, but by now set this workaround in place.
343 //throw new InvalidOperationException ();
344 return 0;
346 return UpdateValue (impl, true, value);
349 // may throw InvalidOperationException, Win32Exception
350 public CounterSample NextSample ()
352 CounterSample sample;
353 if (changed)
354 UpdateInfo ();
355 GetSample (impl, false, out sample);
356 valid_old = true;
357 old_sample = sample;
358 return sample;
361 // may throw InvalidOperationException, Win32Exception
362 public float NextValue ()
364 CounterSample sample;
365 if (changed)
366 UpdateInfo ();
367 GetSample (impl, false, out sample);
368 float val;
369 if (valid_old)
370 val = CounterSampleCalculator.ComputeCounterValue (old_sample, sample);
371 else
372 val = CounterSampleCalculator.ComputeCounterValue (sample);
373 valid_old = true;
374 old_sample = sample;
375 return val;
378 // may throw InvalidOperationException, Win32Exception
379 [MonoTODO]
380 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
381 public void RemoveInstance ()
383 throw new NotImplementedException ();