Hardware/CPU/IntelCPU.cs
changeset 26 0e01b63e1fdc
parent 25 ff3e6edc7113
child 31 c4d1fb76a9e1
     1.1 --- a/Hardware/CPU/IntelCPU.cs	Wed Feb 03 22:02:58 2010 +0000
     1.2 +++ b/Hardware/CPU/IntelCPU.cs	Thu Feb 04 21:19:27 2010 +0000
     1.3 @@ -59,9 +59,8 @@
     1.4      private uint logicalProcessorsPerCore;
     1.5      private uint coreCount;
     1.6  
     1.7 -    private PerformanceCounter totalLoadCounter;
     1.8 -    private PerformanceCounter[] coreLoadCounters;
     1.9 -
    1.10 +    private CPULoad cpuLoad;
    1.11 +    
    1.12      private const uint IA32_THERM_STATUS_MSR = 0x019C;
    1.13      private const uint IA32_TEMPERATURE_TARGET = 0x01A2;
    1.14  
    1.15 @@ -133,30 +132,7 @@
    1.16          default: tjMax = 100; break;
    1.17        }
    1.18  
    1.19 -      try {
    1.20 -        totalLoadCounter = new PerformanceCounter();
    1.21 -        totalLoadCounter.CategoryName = "Processor";
    1.22 -        totalLoadCounter.CounterName = "% Processor Time";
    1.23 -        totalLoadCounter.InstanceName = "_Total";
    1.24 -        totalLoadCounter.NextValue();        
    1.25 -      } catch (Exception) {
    1.26 -        totalLoadCounter = null;
    1.27 -      }
    1.28 -      totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);
    1.29 -
    1.30 -      coreLoadCounters = new PerformanceCounter[
    1.31 -        coreCount * logicalProcessorsPerCore];
    1.32 -      for (int i = 0; i < coreLoadCounters.Length; i++) {
    1.33 -        try {
    1.34 -          coreLoadCounters[i] = new PerformanceCounter();
    1.35 -          coreLoadCounters[i].CategoryName = "Processor";
    1.36 -          coreLoadCounters[i].CounterName = "% Processor Time";
    1.37 -          coreLoadCounters[i].InstanceName = i.ToString();
    1.38 -          coreLoadCounters[i].NextValue();
    1.39 -        } catch (Exception) {
    1.40 -          coreLoadCounters[i] = null;
    1.41 -        }
    1.42 -      }
    1.43 +      totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);     
    1.44  
    1.45        coreTemperatures = new Sensor[coreCount];
    1.46        coreLoads = new Sensor[coreCount];
    1.47 @@ -167,6 +143,13 @@
    1.48            SensorType.Load, this);
    1.49        }
    1.50  
    1.51 +      cpuLoad = new CPULoad(coreCount, logicalProcessorsPerCore);
    1.52 +      if (cpuLoad.IsAvailable) {
    1.53 +        foreach (Sensor sensor in coreLoads)
    1.54 +          ActivateSensor(sensor);
    1.55 +        ActivateSensor(totalLoad);
    1.56 +      }
    1.57 +
    1.58        Update();                   
    1.59      }
    1.60  
    1.61 @@ -221,27 +204,11 @@
    1.62          }        
    1.63        }
    1.64  
    1.65 -      if (totalLoadCounter != null) {
    1.66 -        totalLoad.Value = totalLoadCounter.NextValue();
    1.67 -        ActivateSensor(totalLoad);
    1.68 -      }
    1.69 -
    1.70 -      for (int i = 0; i < coreLoads.Length; i++) {
    1.71 -        float value = 0;
    1.72 -        int count = 0;
    1.73 -        for (int j = 0; j < logicalProcessorsPerCore; j++) {
    1.74 -          PerformanceCounter counter =
    1.75 -            coreLoadCounters[logicalProcessorsPerCore * i + j];
    1.76 -          if (counter != null) {
    1.77 -            value += counter.NextValue();
    1.78 -            count++;
    1.79 -          }
    1.80 -        }
    1.81 -        if (count > 0) {
    1.82 -          value /= count;
    1.83 -          coreLoads[i].Value = value;
    1.84 -          ActivateSensor(coreLoads[i]);
    1.85 -        }
    1.86 +      if (cpuLoad.IsAvailable) {
    1.87 +        cpuLoad.Update();
    1.88 +        for (int i = 0; i < coreLoads.Length; i++)
    1.89 +          coreLoads[i].Value = cpuLoad.GetCoreLoad(i);
    1.90 +        totalLoad.Value = cpuLoad.GetTotalLoad();
    1.91        }
    1.92      }
    1.93