1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections.Generic;
6
using System.Diagnostics;
9
namespace ICSharpCode.Profiler.Controller.Data
12
/// Wraps <see cref="System.Diagnostics.PerformanceCounter" /> to support lazy-loading and easy value collection.
13
/// Stores additonal meta data such as min/max allowed value or unit of the values.
16
public class PerformanceCounterDescriptor
19
/// Gets the category of the performance counter.
21
public string Category { get; private set; }
24
/// Gets the name of the performance counter.
26
public string Name { get; private set; }
29
/// Gets the instance (perfmon process id) of the performance counter.
31
public string Instance { get; private set; }
34
/// Gets the computer the performance counter is executed on.
36
public string Computer { get; private set; }
39
/// Gets a list of values collected by this performance counter.
41
public IList<float> Values { get; private set; }
44
/// Gets the minimum allowed value collected by the performance counter.
45
/// Returns null if there is no lower bound.
47
public float? MinValue { get; private set; }
50
/// Gets the maximum allowed value collected by the performance counter.
51
/// Returns null if there is no upper bound.
53
public float? MaxValue { get; private set; }
56
/// Gets a string representation of the unit of the values collected.
58
public string Unit { get; private set; }
61
/// Gets the format string for display of the collected values.
63
public string Format { get; private set; }
66
PerformanceCounter counter;
69
/// Creates a new PerformanceCounterDescriptor.
71
public PerformanceCounterDescriptor(string category, string name, string instance, string computer,
72
float defaultValue, float? minValue, float? maxValue, string unit, string format)
78
Values = new List<float>();
79
this.defaultValue = defaultValue;
87
/// Creates a new PerformanceCounterDescriptor.
89
public PerformanceCounterDescriptor(string name, float? minValue, float? maxValue, string unit, string format)
90
: this(null, name, null, null, 0, minValue, maxValue, unit, format)
95
/// Returns the perfmon process identifier for a process Id.
96
/// If the process is not available (e. g. not running anymore) null is returned.
98
public static string GetProcessInstanceName(int pid)
100
PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
102
string[] instances = cat.GetInstanceNames();
103
foreach (string instance in instances) {
104
using (PerformanceCounter procIdCounter = new PerformanceCounter("Process", "ID Process", instance, true)) {
105
int val = (int)procIdCounter.RawValue;
115
/// Deletes all collected information.
123
/// Collects a new value. The default value is recorded if any error occurs, while attempting to collect a value.
125
/// <param name="instanceName"></param>
126
public void Collect(string instanceName)
128
if (counter == null && Instance != null)
129
counter = new PerformanceCounter(Category, Name, instanceName ?? Instance, Computer);
132
this.Values.Add(counter.NextValue());
133
} catch (Exception e) {
135
Console.WriteLine(e.ToString());
137
this.Values.Add(defaultValue);
142
/// Returns the name of the performance counter.
144
public override string ToString()