~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Analysis/Profiler/Controller/Data/PerformanceCounterDescriptor.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Diagnostics;
 
7
using System.Linq;
 
8
 
 
9
namespace ICSharpCode.Profiler.Controller.Data
 
10
{
 
11
        /// <summary>
 
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.
 
14
        /// </summary>
 
15
        [Serializable]
 
16
        public class PerformanceCounterDescriptor
 
17
        {
 
18
                /// <summary>
 
19
                /// Gets the category of the performance counter.
 
20
                /// </summary>
 
21
                public string Category { get; private set; }
 
22
                
 
23
                /// <summary>
 
24
                /// Gets the name of the performance counter.
 
25
                /// </summary>
 
26
                public string Name { get; private set; }
 
27
                
 
28
                /// <summary>
 
29
                /// Gets the instance (perfmon process id) of the performance counter.
 
30
                /// </summary>
 
31
                public string Instance { get; private set; }
 
32
                
 
33
                /// <summary>
 
34
                /// Gets the computer the performance counter is executed on.
 
35
                /// </summary>
 
36
                public string Computer { get; private set; }
 
37
                
 
38
                /// <summary>
 
39
                /// Gets a list of values collected by this performance counter.
 
40
                /// </summary>
 
41
                public IList<float> Values { get; private set; }
 
42
                
 
43
                /// <summary>
 
44
                /// Gets the minimum allowed value collected by the performance counter.
 
45
                /// Returns null if there is no lower bound.
 
46
                /// </summary>
 
47
                public float? MinValue { get; private set; }
 
48
                
 
49
                /// <summary>
 
50
                /// Gets the maximum allowed value collected by the performance counter.
 
51
                /// Returns null if there is no upper bound.
 
52
                /// </summary>
 
53
                public float? MaxValue { get; private set; }
 
54
                
 
55
                /// <summary>
 
56
                /// Gets a string representation of the unit of the values collected.
 
57
                /// </summary>
 
58
                public string Unit { get; private set; }
 
59
                
 
60
                /// <summary>
 
61
                /// Gets the format string for display of the collected values.
 
62
                /// </summary>
 
63
                public string Format { get; private set; }
 
64
                
 
65
                float defaultValue;
 
66
                PerformanceCounter counter;
 
67
                
 
68
                /// <summary>
 
69
                /// Creates a new PerformanceCounterDescriptor.
 
70
                /// </summary>
 
71
                public PerformanceCounterDescriptor(string category, string name, string instance, string computer,
 
72
                                                    float defaultValue, float? minValue, float? maxValue, string unit, string format)
 
73
                {
 
74
                        Category = category;
 
75
                        Name = name;
 
76
                        Instance = instance;
 
77
                        Computer = computer;
 
78
                        Values = new List<float>();
 
79
                        this.defaultValue = defaultValue;
 
80
                        MinValue = minValue;
 
81
                        MaxValue = maxValue;
 
82
                        Unit = unit;
 
83
                        Format = format;
 
84
                }
 
85
                
 
86
                /// <summary>
 
87
                /// Creates a new PerformanceCounterDescriptor.
 
88
                /// </summary>
 
89
                public PerformanceCounterDescriptor(string name, float? minValue, float? maxValue, string unit, string format)
 
90
                        : this(null, name, null, null, 0, minValue, maxValue, unit, format)
 
91
                {
 
92
                }
 
93
                
 
94
                /// <summary>
 
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.
 
97
                /// </summary>
 
98
                public static string GetProcessInstanceName(int pid)
 
99
                {
 
100
                        PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
 
101
 
 
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;
 
106
                                        if (val == pid)
 
107
                                                return instance;
 
108
                                }
 
109
                        }
 
110
                        
 
111
                        return null;
 
112
                }
 
113
                
 
114
                /// <summary>
 
115
                /// Deletes all collected information.
 
116
                /// </summary>
 
117
                public void Reset()
 
118
                {
 
119
                        this.Values.Clear();
 
120
                }
 
121
                
 
122
                /// <summary>
 
123
                /// Collects a new value. The default value is recorded if any error occurs, while attempting to collect a value.
 
124
                /// </summary>
 
125
                /// <param name="instanceName"></param>
 
126
                public void Collect(string instanceName)
 
127
                {
 
128
                        if (counter == null && Instance != null)
 
129
                                counter = new PerformanceCounter(Category, Name, instanceName ?? Instance, Computer);
 
130
                        
 
131
                        try {
 
132
                                this.Values.Add(counter.NextValue());
 
133
                        } catch (Exception e) {
 
134
                                #if DEBUG
 
135
                                Console.WriteLine(e.ToString());
 
136
                                #endif
 
137
                                this.Values.Add(defaultValue);
 
138
                        }
 
139
                }
 
140
                
 
141
                /// <summary>
 
142
                /// Returns the name of the performance counter.
 
143
                /// </summary>
 
144
                public override string ToString()
 
145
                {
 
146
                        return Name;
 
147
                }
 
148
        }
 
149
}