Release version 0.1.12. Added error handling for PerformanceCounters (CPU load sensor).

This commit is contained in:
Michael Möller
2010-02-03 22:02:58 +00:00
parent 5249183290
commit de0c73471d
4 changed files with 92 additions and 46 deletions

View File

@@ -80,19 +80,29 @@ namespace OpenHardwareMonitor.Hardware.CPU {
// max two cores // max two cores
coreCount = coreCount > 2 ? 2 : coreCount; coreCount = coreCount > 2 ? 2 : coreCount;
try {
totalLoadCounter = new PerformanceCounter(); totalLoadCounter = new PerformanceCounter();
totalLoadCounter.CategoryName = "Processor"; totalLoadCounter.CategoryName = "Processor";
totalLoadCounter.CounterName = "% Processor Time"; totalLoadCounter.CounterName = "% Processor Time";
totalLoadCounter.InstanceName = "_Total"; totalLoadCounter.InstanceName = "_Total";
totalLoadCounter.NextValue();
} catch (Exception) {
totalLoadCounter = null;
}
totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this); totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);
coreLoadCounters = new PerformanceCounter[coreCount]; coreLoadCounters = new PerformanceCounter[coreCount];
coreLoads = new Sensor[coreCount]; coreLoads = new Sensor[coreCount];
for (int i = 0; i < coreLoadCounters.Length; i++) { for (int i = 0; i < coreLoadCounters.Length; i++) {
try {
coreLoadCounters[i] = new PerformanceCounter(); coreLoadCounters[i] = new PerformanceCounter();
coreLoadCounters[i].CategoryName = "Processor"; coreLoadCounters[i].CategoryName = "Processor";
coreLoadCounters[i].CounterName = "% Processor Time"; coreLoadCounters[i].CounterName = "% Processor Time";
coreLoadCounters[i].InstanceName = i.ToString(); coreLoadCounters[i].InstanceName = i.ToString();
coreLoadCounters[i].NextValue();
} catch (Exception) {
coreLoadCounters[i] = null;
}
coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1, coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1,
SensorType.Load, this); SensorType.Load, this);
} }
@@ -153,10 +163,13 @@ namespace OpenHardwareMonitor.Hardware.CPU {
} }
} }
if (totalLoadCounter != null) {
totalLoad.Value = totalLoadCounter.NextValue(); totalLoad.Value = totalLoadCounter.NextValue();
ActivateSensor(totalLoad); ActivateSensor(totalLoad);
}
for (int i = 0; i < coreLoads.Length; i++) { for (int i = 0; i < coreLoads.Length; i++)
if (coreLoadCounters[i] != null) {
coreLoads[i].Value = coreLoadCounters[i].NextValue(); coreLoads[i].Value = coreLoadCounters[i].NextValue();
ActivateSensor(coreLoads[i]); ActivateSensor(coreLoads[i]);
} }

View File

@@ -72,19 +72,29 @@ namespace OpenHardwareMonitor.Hardware.CPU {
if (cpuidExtData.GetLength(0) > 8) if (cpuidExtData.GetLength(0) > 8)
coreCount = (cpuidExtData[8, 2] & 0xFF) + 1; coreCount = (cpuidExtData[8, 2] & 0xFF) + 1;
try {
totalLoadCounter = new PerformanceCounter(); totalLoadCounter = new PerformanceCounter();
totalLoadCounter.CategoryName = "Processor"; totalLoadCounter.CategoryName = "Processor";
totalLoadCounter.CounterName = "% Processor Time"; totalLoadCounter.CounterName = "% Processor Time";
totalLoadCounter.InstanceName = "_Total"; totalLoadCounter.InstanceName = "_Total";
totalLoadCounter.NextValue();
} catch (Exception) {
totalLoadCounter = null;
}
totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this); totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);
coreLoadCounters = new PerformanceCounter[coreCount]; coreLoadCounters = new PerformanceCounter[coreCount];
coreLoads = new Sensor[coreCount]; coreLoads = new Sensor[coreCount];
for (int i = 0; i < coreLoadCounters.Length; i++) { for (int i = 0; i < coreLoadCounters.Length; i++) {
try {
coreLoadCounters[i] = new PerformanceCounter(); coreLoadCounters[i] = new PerformanceCounter();
coreLoadCounters[i].CategoryName = "Processor"; coreLoadCounters[i].CategoryName = "Processor";
coreLoadCounters[i].CounterName = "% Processor Time"; coreLoadCounters[i].CounterName = "% Processor Time";
coreLoadCounters[i].InstanceName = i.ToString(); coreLoadCounters[i].InstanceName = i.ToString();
coreLoadCounters[i].NextValue();
} catch (Exception) {
coreLoadCounters[i] = null;
}
coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1, coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1,
SensorType.Load, this); SensorType.Load, this);
} }
@@ -132,10 +142,13 @@ namespace OpenHardwareMonitor.Hardware.CPU {
DeactivateSensor(coreTemperature); DeactivateSensor(coreTemperature);
} }
if (totalLoadCounter != null) {
totalLoad.Value = totalLoadCounter.NextValue(); totalLoad.Value = totalLoadCounter.NextValue();
ActivateSensor(totalLoad); ActivateSensor(totalLoad);
}
for (int i = 0; i < coreLoads.Length; i++) { for (int i = 0; i < coreLoads.Length; i++)
if (coreLoadCounters[i] != null) {
coreLoads[i].Value = coreLoadCounters[i].NextValue(); coreLoads[i].Value = coreLoadCounters[i].NextValue();
ActivateSensor(coreLoads[i]); ActivateSensor(coreLoads[i]);
} }

View File

@@ -133,19 +133,29 @@ namespace OpenHardwareMonitor.Hardware.CPU {
default: tjMax = 100; break; default: tjMax = 100; break;
} }
try {
totalLoadCounter = new PerformanceCounter(); totalLoadCounter = new PerformanceCounter();
totalLoadCounter.CategoryName = "Processor"; totalLoadCounter.CategoryName = "Processor";
totalLoadCounter.CounterName = "% Processor Time"; totalLoadCounter.CounterName = "% Processor Time";
totalLoadCounter.InstanceName = "_Total"; totalLoadCounter.InstanceName = "_Total";
totalLoadCounter.NextValue();
} catch (Exception) {
totalLoadCounter = null;
}
totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this); totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);
coreLoadCounters = new PerformanceCounter[ coreLoadCounters = new PerformanceCounter[
coreCount * logicalProcessorsPerCore]; coreCount * logicalProcessorsPerCore];
for (int i = 0; i < coreLoadCounters.Length; i++) { for (int i = 0; i < coreLoadCounters.Length; i++) {
try {
coreLoadCounters[i] = new PerformanceCounter(); coreLoadCounters[i] = new PerformanceCounter();
coreLoadCounters[i].CategoryName = "Processor"; coreLoadCounters[i].CategoryName = "Processor";
coreLoadCounters[i].CounterName = "% Processor Time"; coreLoadCounters[i].CounterName = "% Processor Time";
coreLoadCounters[i].InstanceName = i.ToString(); coreLoadCounters[i].InstanceName = i.ToString();
coreLoadCounters[i].NextValue();
} catch (Exception) {
coreLoadCounters[i] = null;
}
} }
coreTemperatures = new Sensor[coreCount]; coreTemperatures = new Sensor[coreCount];
@@ -211,19 +221,29 @@ namespace OpenHardwareMonitor.Hardware.CPU {
} }
} }
if (totalLoadCounter != null) {
totalLoad.Value = totalLoadCounter.NextValue(); totalLoad.Value = totalLoadCounter.NextValue();
ActivateSensor(totalLoad); ActivateSensor(totalLoad);
}
for (int i = 0; i < coreLoads.Length; i++) { for (int i = 0; i < coreLoads.Length; i++) {
float value = 0; float value = 0;
for (int j = 0; j < logicalProcessorsPerCore; j++) int count = 0;
value += coreLoadCounters[ for (int j = 0; j < logicalProcessorsPerCore; j++) {
logicalProcessorsPerCore * i + j].NextValue(); PerformanceCounter counter =
value /= logicalProcessorsPerCore; coreLoadCounters[logicalProcessorsPerCore * i + j];
if (counter != null) {
value += counter.NextValue();
count++;
}
}
if (count > 0) {
value /= count;
coreLoads[i].Value = value; coreLoads[i].Value = value;
ActivateSensor(coreLoads[i]); ActivateSensor(coreLoads[i]);
} }
} }
}
private void ActivateSensor(Sensor sensor) { private void ActivateSensor(Sensor sensor) {
if (!active.Contains(sensor)) { if (!active.Contains(sensor)) {

View File

@@ -69,5 +69,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.11.0")] [assembly: AssemblyVersion("0.1.12.0")]
[assembly: AssemblyFileVersion("0.1.11.0")] [assembly: AssemblyFileVersion("0.1.12.0")]