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

Make assembling of message strings for CPU monitoring more consistent

parent ea6aa185
No related branches found
No related tags found
No related merge requests found
...@@ -48,6 +48,15 @@ namespace ATxCommon.Monitoring ...@@ -48,6 +48,15 @@ namespace ATxCommon.Monitoring
private int _behaving; private int _behaving;
private int _probation; private int _probation;
/// <summary>
/// Performance counter category name, <see cref="PerformanceCounter"/> for details.
/// </summary>
private const string Category = "Processor";
/// <summary>
/// Description string to be used in log messages.
/// </summary>
private readonly string _description;
#region properties #region properties
...@@ -70,7 +79,7 @@ namespace ATxCommon.Monitoring ...@@ -70,7 +79,7 @@ namespace ATxCommon.Monitoring
set { set {
_interval = value; _interval = value;
_monitoringTimer.Interval = value; _monitoringTimer.Interval = value;
Log.Debug("CPU monitoring interval: {0}", _interval); Log.Debug("{0} monitoring interval: {1}ms", _description, _interval);
} }
} }
...@@ -81,7 +90,7 @@ namespace ATxCommon.Monitoring ...@@ -81,7 +90,7 @@ namespace ATxCommon.Monitoring
get => _limit; get => _limit;
set { set {
_limit = value; _limit = value;
Log.Debug("CPU monitoring limit: {0}", _limit); Log.Debug("{0} monitoring limit: {1}", _description, _limit);
} }
} }
...@@ -93,7 +102,8 @@ namespace ATxCommon.Monitoring ...@@ -93,7 +102,8 @@ namespace ATxCommon.Monitoring
get => _probation; get => _probation;
set { set {
_probation = value; _probation = value;
Log.Debug("CPU monitoring probation cycles when violating limit: {0}", _probation); Log.Debug("{0} monitoring probation cycles when violating limit: {1}",
_description, _probation);
} }
} }
...@@ -103,7 +113,7 @@ namespace ATxCommon.Monitoring ...@@ -103,7 +113,7 @@ namespace ATxCommon.Monitoring
public bool Enabled { public bool Enabled {
get => _monitoringTimer.Enabled; get => _monitoringTimer.Enabled;
set { set {
Log.Debug("{0} CPU monitoring.", value ? "Enabling" : "Disabling"); Log.Debug("{0} - {1} monitoring.", _description, value ? "enabling" : "disabling");
_monitoringTimer.Enabled = value; _monitoringTimer.Enabled = value;
} }
} }
...@@ -120,29 +130,32 @@ namespace ATxCommon.Monitoring ...@@ -120,29 +130,32 @@ namespace ATxCommon.Monitoring
/// <summary> /// <summary>
/// Create performance counter and initialize it. /// Create performance counter and initialize it.
/// </summary> /// </summary>
public Cpu() { public Cpu(string counterName = "% Processor Time") {
_interval = 250; _interval = 250;
_limit = 25; _limit = 25;
_probation = 40; _probation = 40;
Log.Info("Initializing CPU load monitoring..."); Log.Info("Initializing {0} performance monitoring for [{1}].", Category, counterName);
// assemble the description string to be used in messages:
_description = $"{Category} {counterName}";
try { try {
Log.Debug("CPU monitoring initializing PerformanceCounter (takes one second)..."); Log.Debug("{0} monitoring initializing PerformanceCounter (takes one second)...", Category);
_cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); _cpuCounter = new PerformanceCounter(Category, counterName, "_Total");
_cpuCounter.NextValue();
Thread.Sleep(1000);
var curLoad = _cpuCounter.NextValue(); var curLoad = _cpuCounter.NextValue();
Log.Debug("CPU monitoring current load: {0:0.0}", curLoad); Log.Debug("{0} initial value: {1:0.0}", _description, curLoad);
Thread.Sleep(1000);
curLoad = _cpuCounter.NextValue();
Log.Debug("{0} current value: {1:0.0}", _description, curLoad);
// now initialize the load state: // now initialize the load state:
HighLoad = curLoad > _limit; HighLoad = curLoad > _limit;
_monitoringTimer = new Timer(_interval); _monitoringTimer = new Timer(_interval);
_monitoringTimer.Elapsed += UpdateCpuLoad; _monitoringTimer.Elapsed += UpdateCpuLoad;
} }
catch (Exception) { catch (Exception) {
Log.Error("Initializing CPU monitoring failed!"); Log.Error("{0} monitoring initialization failed!", Category);
throw; throw;
} }
Log.Debug("Initializing CPU monitoring completed."); Log.Debug("{0} monitoring initialization completed.", _description);
} }
/// <summary> /// <summary>
...@@ -160,33 +173,37 @@ namespace ATxCommon.Monitoring ...@@ -160,33 +173,37 @@ namespace ATxCommon.Monitoring
if (_behaving > _probation) { if (_behaving > _probation) {
// this means the load was considered as "low" before, so raise an event: // this means the load was considered as "low" before, so raise an event:
OnLoadAboveLimit(); OnLoadAboveLimit();
Log.Trace("CPU load ({0:0.0}) violating limit ({1})!", _loadReadings[3], _limit); Log.Trace("{0} ({1:0.0}) violating limit ({2})!",
_description, _loadReadings[3], _limit);
} else if (_behaving > 0) { } else if (_behaving > 0) {
// this means we were still in probation, so no need to trigger again... // this means we were still in probation, so no need to trigger again...
Log.Trace("Resetting behaving counter to 0 (was {0}).", _behaving); Log.Trace("{0}: resetting behaving counter (was {1}).",
_description, _behaving);
} }
_behaving = 0; _behaving = 0;
} else { } else {
_behaving++; _behaving++;
if (_behaving == _probation) { if (_behaving == _probation) {
Log.Trace("CPU load below limit for {0} cycles, passing probation!", _probation); Log.Trace("{0} below limit for {1} cycles, passing probation!",
_description, _probation);
OnLoadBelowLimit(); OnLoadBelowLimit();
} else if (_behaving > _probation) { } else if (_behaving > _probation) {
Log.Trace("CPU load behaving well since {0} cycles.", _behaving); Log.Trace("{0} behaving well since {1} cycles.",
_description, _behaving);
} else if (_behaving < 0) { } else if (_behaving < 0) {
Log.Info("CPU load monitoring: integer wrap around happened, " + Log.Info("{0}: integer wrap around happened, resetting probation " +
"resetting probation counter (no reason to worry)."); "counter (no reason to worry).", _description);
_behaving = _probation + 1; _behaving = _probation + 1;
} }
} }
} }
catch (Exception ex) { catch (Exception ex) {
Log.Error("UpdateCpuLoad failed: {0}", ex.Message); Log.Error("Updating {0} counters failed: {1}", _description, ex.Message);
} }
finally { finally {
_monitoringTimer.Enabled = true; _monitoringTimer.Enabled = true;
} }
Log.Log(LogPerformanceReadings, "CPU load: {0:0.0} {1}", Log.Log(LogPerformanceReadings, "{0}: {1:0.0} {2}", _description,
_loadReadings[3], _loadReadings[3] < Limit ? " [" + _behaving + "]" : ""); _loadReadings[3], _loadReadings[3] < Limit ? " [" + _behaving + "]" : "");
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment