-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPMMain.cs
402 lines (334 loc) · 13.8 KB
/
CPMMain.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Core_Performance_Monitor
{
public partial class CPMMain : Form
{
[DllImport("dwmapi.dll")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
internal static bool Enable_DWMWA_USE_IMMERSIVE_DARK_MODE(IntPtr handle)
{
int enableDarkMode = 1;
return DwmSetWindowAttribute(handle, DWMWA_USE_IMMERSIVE_DARK_MODE, ref enableDarkMode, sizeof(int)) == 0;
}
private const string APP_VERSION = "1.0.0";
SystemInfo systemInfo = new SystemInfo();
CacheInformation? caches;
MemoryInformation? memory;
ProcessorInformation? processorInformation;
DeviceInformation? deviceInformation;
ChronoInformation? chronoInformation;
List<GraphicsInformation> graphicsInformation = new List<GraphicsInformation>();
private const int BITMAP_WIDTH = 193;
private const int BITMAP_HEIGHT = 88;
private const int PEN_WIDTH = 1;
private const int ELLIPSE_SIZE = 1;
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private const string REFRESH_FREQUENCY_1 = "1 second";
private const string REFRESH_FREQUENCY_2 = "2 second";
private const string REFRESH_FREQUENCY_5 = "5 second";
private const string REFRESH_FREQUENCY_10 = "10 second";
List<int> memoryUsage = new List<int>();
float memoryUsagePct = 0.0f;
List<int> cpuUsage = new List<int>();
float cpuUsagePct = 0.0f;
private Color PERFORMANCE_GRAPH_PEN_COLOR = Color.LightCyan;
private Color MEMORY_GRAPH_PEN_COLOR = Color.Goldenrod;
private Pen performanceGraphPen;
private Pen memoryGraphPen;
Bitmap performanceGraphBitmap = new Bitmap(BITMAP_WIDTH, BITMAP_HEIGHT);
Bitmap memoryGraphBitmap = new Bitmap(BITMAP_WIDTH, BITMAP_HEIGHT);
List<Rectangle> rectsPerformance;
List<Rectangle> rectsMemory;
private const double GRAPH_SIZE_THRESHOLD = (BITMAP_WIDTH * 1.0 / PEN_WIDTH);
public CPMMain()
{
InitializeComponent();
// Set the application version
this.Text += " v" + APP_VERSION;
// Set the titlebar theme.
Enable_DWMWA_USE_IMMERSIVE_DARK_MODE(Handle);
// Set the refresh frequencies.
cbRefreshFrequency.Items.AddRange(new string[]
{
REFRESH_FREQUENCY_1,
REFRESH_FREQUENCY_2,
REFRESH_FREQUENCY_5,
REFRESH_FREQUENCY_10
});
cbRefreshFrequency.SelectedIndex = 0;
// Initialise pen and colour for graphs.
performanceGraphPen = new Pen(PERFORMANCE_GRAPH_PEN_COLOR, PEN_WIDTH);
memoryGraphPen = new Pen(MEMORY_GRAPH_PEN_COLOR, PEN_WIDTH);
// Update statistics every tick.
timer.Tick += new EventHandler(RefreshStatistics);
timer.Interval = 1000;
timer.Start();
pbPerformanceGraph.Image = performanceGraphBitmap;
pbMemoryGraph.Image = memoryGraphBitmap;
rectsPerformance = new List<Rectangle>();
rectsMemory = new List<Rectangle>();
Initialise();
}
private void Initialise()
{
Task t1 = Task.Run(() => RetrieveDeviceInformation());
Task t2 = Task.Run(() => RetrieveCacheInformation());
Task t3 = Task.Run(() => RetrieveProcessorInformation());
Task t4 = Task.Run(() => RetrieveGraphicsInformation());
Task tLoading = Task.WhenAll([t1, t2, t3, t4]);
try
{
tLoading.Wait();
} catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
UpdateDeviceInformation();
UpdateCacheInformation();
UpdateProcessorInformation();
UpdateGraphicsInformation();
}
private async void RefreshStatistics(object? sender, EventArgs e)
{
await Task.Run(() => RetrieveProcessorPerformanceCounter());
await Task.Run(() => RetrieveMemoryInformation());
await Task.Run(() => RetrieveChronoInformation());
UpdatePerformancePanelInformation();
UpdateMemoryInformation();
UpdateChronoInformation();
RenderPerformanceGraph();
RenderMemoryGraph();
GC.Collect();
}
private int ConvertPercentageToImageDimensions(int source)
{
int result = (int)(BITMAP_HEIGHT - (source / 100.0 * BITMAP_HEIGHT));
return result;
}
private void RenderGraph(Bitmap bitmap, PictureBox pictureBox, List<int> sourceList, List<Rectangle> rects, Pen pen)
{
Graphics g1 = Graphics.FromImage(bitmap);
g1.Clear(Color.FromArgb(0, 30, 71, 104));
pictureBox.Image = bitmap;
int NUMBER_OF_ENTRIES = sourceList.Count;
int STARTING_POINT = BITMAP_WIDTH - ELLIPSE_SIZE * NUMBER_OF_ENTRIES;
rects.Clear();
for (int i = 0; i < sourceList.Count; i++)
{
int y = ConvertPercentageToImageDimensions(sourceList[i]);
rects.Add(new Rectangle(STARTING_POINT + i * ELLIPSE_SIZE, y, ELLIPSE_SIZE, BITMAP_HEIGHT - y - ELLIPSE_SIZE));
}
using (Graphics g = Graphics.FromImage(bitmap))
{
foreach (Rectangle rectangle in rects)
{
g.DrawEllipse(pen, rectangle);
}
}
}
private void RenderPerformanceGraph()
{
RenderGraph(performanceGraphBitmap, pbPerformanceGraph, cpuUsage, rectsPerformance, performanceGraphPen);
}
private void RenderMemoryGraph()
{
RenderGraph(memoryGraphBitmap, pbMemoryGraph, memoryUsage, rectsMemory, memoryGraphPen);
}
private void RetrieveGraphicsInformation()
{
graphicsInformation = systemInfo.GetGraphicsInformation();
}
private void RetrieveCacheInformation()
{
caches = systemInfo.GetCacheInformation();
}
private void RetrieveMemoryInformation()
{
memory = systemInfo.GetMemoryInformation();
// Get information on the available system memory.
try
{
using(PerformanceCounter availableMemoryPerformanceCounter = new PerformanceCounter("Memory", "Available MBytes"))
{
ulong availableMemory = (ulong)availableMemoryPerformanceCounter.NextValue();
// Update the memory usage percentage.
if ((availableMemory > 0) && (memory.TotalMemory > 0))
{
memoryUsagePct = (((memory.TotalMemory - availableMemory) * 1.0f / memory.TotalMemory) * 100.0f);
memoryUsage.Add((int)memoryUsagePct);
// Pop element if greater than size threshold.
if (memoryUsage.Count > GRAPH_SIZE_THRESHOLD)
{
memoryUsage.RemoveAt(0);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void RetrieveDeviceInformation()
{
deviceInformation = systemInfo.GetDeviceInformation();
}
private void RetrieveProcessorInformation()
{
processorInformation = systemInfo.GetProcessorInformation();
}
private void RetrieveProcessorPerformanceCounter()
{
try
{
using (PerformanceCounter processorTimePerformanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"))
{
processorTimePerformanceCounter.NextValue();
Thread.Sleep(1000);
cpuUsagePct = processorTimePerformanceCounter.NextValue() * 1.0f;
cpuUsage.Add((int)cpuUsagePct);
// Pop element if greater than size threshold.
if (cpuUsage.Count > GRAPH_SIZE_THRESHOLD)
{
cpuUsage.RemoveAt(0);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void RetrieveChronoInformation()
{
var info = systemInfo.GetChronoInformation();
if (chronoInformation == null)
{
chronoInformation = info;
}
else
{
chronoInformation.LocalDateTime = info.LocalDateTime;
}
}
private void UpdateGraphicsInformation()
{
if(graphicsInformation == null)
{
return;
}
if(graphicsInformation.Count > 0)
{
lblGraphics1Value.Text = graphicsInformation[0].GPUName;
lblGraphics1Driver.Text = graphicsInformation[0].GPUDriverVersion;
if(graphicsInformation.Count > 1)
{
lblGraphics2Value.Text = graphicsInformation[1].GPUName;
lblGraphics2Driver.Text = graphicsInformation[1].GPUDriverVersion;
}
}
}
private void UpdateCacheInformation()
{
if(caches == null)
{
return;
}
lblL1CacheSize.Text = caches.L1CacheSize.ToString() + " " + caches.L1SizeUnits;
lblL2CacheSize.Text = caches.L2CacheSize.ToString() + " " + caches.L2SizeUnits;
lblL3CacheSize.Text = caches.L3CacheSize.ToString() + " " + caches.L3SizeUnits;
}
private void UpdateMemoryInformation()
{
if (memory == null)
{
return;
}
lblFreeMemorySize.Text = memory.FreeMemory.ToString() + " " + memory.FreeMemorySizeUnits;
lblTotalMemorySize.Text = memory.TotalMemory.ToString() + " " + memory.TotalMemorySizeUnits;
lblFreeVirtualMemorySize.Text = memory.FreeVirtualMemory.ToString() + " " + memory.FreeVirtualMemorySizeUnits;
lblTotalVirtualMemorySize.Text = memory.TotalVirtualMemory.ToString() + " " + memory.TotalVirtualMemorySizeUnits;
lblFreeSpacePagingSize.Text = memory.FreeSpacePaging.ToString() + " " + memory.FreeSpacePagingSizeUnits;
lblSizeStoredPagingSize.Text = memory.SizeStoredPaging.ToString() + " " + memory.SizeStoredPagingSizeUnits;
}
private void UpdateDeviceInformation()
{
if (deviceInformation == null)
{
return;
}
lblDeviceNameValue.Text = deviceInformation.DeviceName;
lblOperatingSystemValue.Text = deviceInformation.OperatingSystem;
lblOSVersionValue.Text = deviceInformation.OSVersion;
}
private void UpdateProcessorInformation()
{
if (processorInformation == null)
{
return;
}
lblProcessorTypeValue.Text = processorInformation.ProcessorType.ToString();
lblPhysicalCoresValue.Text = processorInformation.PhysicalCores.ToString();
lblLogicalCoresValue.Text = processorInformation.LogicalCores.ToString();
}
private void UpdateChronoInformation()
{
if (chronoInformation == null)
{
return;
}
lblLastBootTimeValue.Text = chronoInformation.LastBootUpTime;
lblLocalDateTimeValue.Text = chronoInformation.LocalDateTime;
}
private void UpdatePerformancePanelInformation()
{
lblCPUUsageValue.Text = "CPU: " + cpuUsagePct.ToString("#0.00") + "%";
lblMemoryUsageValue.Text = "Mem: " + memoryUsagePct.ToString("#0.00") + "%";
}
private void notifyIconCPM_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
Show();
this.WindowState = FormWindowState.Normal;
notifyIconCPM.Visible = false;
}
}
private void CPMMain_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
notifyIconCPM.Visible = true;
}
}
private void cbRefreshFrequency_SelectedIndexChanged(object sender, EventArgs e)
{
if(chronoInformation == null)
{
return;
}
if (cbRefreshFrequency.Text.Equals(REFRESH_FREQUENCY_1))
{
chronoInformation.RefreshFrequency = 1000;
}
else if (cbRefreshFrequency.Text.Equals(REFRESH_FREQUENCY_2))
{
chronoInformation.RefreshFrequency = 2000;
}
else if (cbRefreshFrequency.Text.Equals(REFRESH_FREQUENCY_5))
{
chronoInformation.RefreshFrequency = 5000;
}
else if (cbRefreshFrequency.Text.Equals(REFRESH_FREQUENCY_10))
{
chronoInformation.RefreshFrequency = 10000;
}
timer.Stop();
timer.Interval = chronoInformation.RefreshFrequency;
timer.Start();
}
}
}