Skip to content
Snippets Groups Projects
Commit 4ca08524 authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Add class for CPU load monitoring.

Placed in namespace ATxCommon.Monitoring, using its own timer to update
the values.
parent a4ff897f
No related branches found
No related tags found
No related merge requests found
......@@ -47,6 +47,7 @@
<Compile Include="BuildDetails.cs" />
<Compile Include="Conv.cs" />
<Compile Include="FsUtils.cs" />
<Compile Include="Monitoring\Cpu.cs" />
<Compile Include="NLog\RateLimitWrapper.cs" />
<Compile Include="Serializables\DriveToCheck.cs" />
<Compile Include="Serializables\ServiceConfig.cs" />
......
using System;
using System.Diagnostics;
using System.Linq;
using System.Timers;
using NLog;
namespace ATxCommon.Monitoring
{
public class Cpu
{
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
private readonly Timer _monitoringTimer;
private readonly PerformanceCounter _cpuCounter;
private readonly float[] _loadReadings = {0F, 0F, 0F, 0F};
private float _load;
public float Load() => _load;
public Cpu() {
Log.Debug("Initializing CPU monitoring...");
try {
_cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
_monitoringTimer = new Timer(250);
_monitoringTimer.Elapsed += UpdateCpuLoad;
_monitoringTimer.Enabled = true;
}
catch (Exception ex) {
Log.Error("Initializing CPU monitoring completed ({1}): {0}", ex.Message, ex.GetType());
throw;
}
Log.Debug("Initializing CPU monitoring completed.");
}
private void UpdateCpuLoad(object sender, ElapsedEventArgs e) {
_monitoringTimer.Enabled = false;
try {
Log.Trace("load values: {0}, average: {1}", string.Join(", ", _loadReadings), _load);
// ConstrainedCopy seems to be the most efficient approach to shift the array:
Array.ConstrainedCopy(_loadReadings, 1, _loadReadings, 0, 3);
_loadReadings[3] = _cpuCounter.NextValue();
_load = _loadReadings.Average();
Log.Trace("load values: {0}, average: {1}", string.Join(", ", _loadReadings), _load);
}
catch (Exception ex) {
Log.Error("UpdateCpuLoad failed: {0}", ex.Message);
}
finally {
_monitoringTimer.Enabled = true;
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment