-
Notifications
You must be signed in to change notification settings - Fork 0
/
CpuMeter.cs
238 lines (194 loc) · 7.25 KB
/
CpuMeter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Linq;
using Ephemera.NBagOfTricks;
namespace Ephemera.NBagOfUis
{
public partial class CpuMeter : UserControl
{
#region Fields
/// <summary>Total usage.</summary>
PerformanceCounter? _cpuPerf = null;
/// <summary>Logical processes.</summary>
PerformanceCounter[]? _processesPerf = null;
/// <summary> </summary>
bool _inited = false;
/// <summary> </summary>
readonly Timer _timer = new();
/// <summary> </summary>
readonly int _min = 0;
/// <summary> </summary>
readonly int _max = 100;
/// <summary>Storage.</summary>
double[][]? _processesBuffs = null;
/// <summary>Storage.</summary>
double[]? _cpuBuff = null;
/// <summary>Storage.</summary>
int _buffIndex = 0;
///// <summary>CPU info.</summary>
//int _cores = 0;
///// <summary>CPU info.</summary>
//int _physicalProcessors = 0;
/// <summary>CPU info.</summary>
int _logicalProcessors = 0;
/// <summary>The pen.</summary>
readonly Pen _pen = new(Color.Black, 1);
/// <summary>For drawing text.</summary>
readonly StringFormat _format = new() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center };
#endregion
#region Properties
/// <summary>User can change.</summary>
public string Label { get; set; } = "cpu";
/// <summary> </summary>
public bool Enable { get; set; } = false;
/// <summary>Default is 500 msec. Change if you like.</summary>
public int UpdateFreq { set { _timer.Interval = value; } }
/// <summary>For styling.</summary>
public Color DrawColor { get { return _pen.Color; } set { _pen.Color = value; } }
#endregion
#region Lifecycle
/// <summary>
/// Constructor.
/// </summary>
public CpuMeter()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
Name = "CpuMeter";
_timer.Tick += Timer_Tick;
_timer.Interval = 500;
_timer.Start();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
_pen?.Dispose();
_cpuPerf?.Dispose();
_format?.Dispose();
_processesPerf?.ForEach(p => p?.Dispose());
}
base.Dispose(disposing);
}
#endregion
#region Private functions
/// <summary>
/// Paints the volume meter.
/// </summary>
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.Clear(BackColor);
// Draw data. FUTURE: for each process?
if(_cpuBuff is not null)
{
for (int i = 0; i < _cpuBuff.Length; i++)
{
int index = _buffIndex - i;
index = index < 0 ? index + _cpuBuff.Length : index;
double val = _cpuBuff[index];
// Draw data point.
double y = MathUtils.Map(val, _min, _max, Height, 0);
pe.Graphics.DrawLine(_pen, (float)i, (float)y, (float)i, Height);
}
}
Rectangle r = new(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height / 2);
pe.Graphics.DrawString(Label, Font, Brushes.Black, r, _format);
}
/// <summary>
/// Update drawing area.
/// </summary>
protected override void OnResize(EventArgs e)
{
if(_inited)
{
SetBuffs();
}
Invalidate();
base.OnResize(e);
}
/// <summary>
///
/// </summary>
void SetBuffs()
{
if(_processesBuffs is not null)
{
int size = Width;
for (int i = 0; i < _processesBuffs.Length; i++)
{
_processesBuffs[i] = new double[size];
}
_cpuBuff = new double[size];
_buffIndex = 0;
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Timer_Tick(object? sender, EventArgs e)
{
if(Enable)
{
if (_cpuPerf is null)
{
InitPerf();
}
else if (_cpuBuff is not null && _processesPerf is not null && _processesBuffs is not null)
{
_cpuBuff[_buffIndex] = 0;
for (int i = 0; i < _processesPerf.Length; i++)
{
float val = _processesPerf[i].NextValue();
_processesBuffs[i][_buffIndex] = val;
}
_cpuBuff[_buffIndex] = _cpuPerf.NextValue();
_buffIndex++;
if (_buffIndex >= _cpuBuff.Length)
{
_buffIndex = 0;
}
Invalidate();
}
}
}
/// <summary>
/// Defer init as they are slow processes.
/// </summary>
void InitPerf()
{
// The Processor (% Processor Time) counter will be out of 100 and will give the total usage across all
// processors /cores/etc in the computer. However, the Processor (% Process Time) is scaled by the number
// of logical processors. To get average usage across a computer, divide the result by Environment.ProcessorCount.
// There are several different pieces of information relating to processors that you could get:
// Number of physical processors
// Number of cores
// Number of logical processors.
// These can all be different; in the case of a machine with 2 dual-core hyper-threading-enabled processors,
// there are 2 physical processors, 4 cores, and 8 logical processors.
_logicalProcessors = Environment.ProcessorCount;
_processesPerf = new PerformanceCounter[_logicalProcessors];
_processesBuffs = new double[_logicalProcessors][];
for (int i = 0; i < _logicalProcessors; i++)
{
var pc = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
_processesPerf[i] = pc;
}
_cpuPerf = new PerformanceCounter("Processor", "% Processor Time", "_Total");
SetBuffs();
_inited = true;
}
#endregion
}
}