Hardware/CPU/AMD10CPU.cs
changeset 26 0e01b63e1fdc
parent 25 ff3e6edc7113
child 31 c4d1fb76a9e1
     1.1 --- a/Hardware/CPU/AMD10CPU.cs	Wed Feb 03 22:02:58 2010 +0000
     1.2 +++ b/Hardware/CPU/AMD10CPU.cs	Thu Feb 04 21:19:27 2010 +0000
     1.3 @@ -55,8 +55,7 @@
     1.4  
     1.5      private List<ISensor> active = new List<ISensor>();
     1.6  
     1.7 -    private PerformanceCounter totalLoadCounter;
     1.8 -    private PerformanceCounter[] coreLoadCounters;
     1.9 +    private CPULoad cpuLoad;
    1.10  
    1.11      private const ushort PCI_AMD_VENDOR_ID = 0x1022;
    1.12      private const ushort PCI_AMD_10H_MISCELLANEOUS_DEVICE_ID = 0x1203;
    1.13 @@ -72,32 +71,19 @@
    1.14        if (cpuidExtData.GetLength(0) > 8)
    1.15          coreCount = (cpuidExtData[8, 2] & 0xFF) + 1;
    1.16  
    1.17 -      try {
    1.18 -        totalLoadCounter = new PerformanceCounter();
    1.19 -        totalLoadCounter.CategoryName = "Processor";
    1.20 -        totalLoadCounter.CounterName = "% Processor Time";
    1.21 -        totalLoadCounter.InstanceName = "_Total";
    1.22 -        totalLoadCounter.NextValue();
    1.23 -      } catch (Exception) {
    1.24 -        totalLoadCounter = null;
    1.25 -      }
    1.26        totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);
    1.27  
    1.28 -      coreLoadCounters = new PerformanceCounter[coreCount];
    1.29        coreLoads = new Sensor[coreCount];
    1.30 -      for (int i = 0; i < coreLoadCounters.Length; i++) {
    1.31 -        try {
    1.32 -          coreLoadCounters[i] = new PerformanceCounter();
    1.33 -          coreLoadCounters[i].CategoryName = "Processor";
    1.34 -          coreLoadCounters[i].CounterName = "% Processor Time";
    1.35 -          coreLoadCounters[i].InstanceName = i.ToString();
    1.36 -          coreLoadCounters[i].NextValue();
    1.37 -        } catch (Exception) {
    1.38 -          coreLoadCounters[i] = null;
    1.39 -        }
    1.40 +      for (int i = 0; i < coreCount; i++) 
    1.41          coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1,
    1.42            SensorType.Load, this);
    1.43 -      }        
    1.44 +
    1.45 +      cpuLoad = new CPULoad(coreCount, 1);
    1.46 +      if (cpuLoad.IsAvailable) {
    1.47 +        foreach (Sensor sensor in coreLoads)
    1.48 +          ActivateSensor(sensor);
    1.49 +        ActivateSensor(totalLoad);
    1.50 +      }    
    1.51        
    1.52        // AMD family 10h processors support only one temperature sensor
    1.53        coreTemperature = new Sensor(
    1.54 @@ -142,16 +128,12 @@
    1.55          DeactivateSensor(coreTemperature);
    1.56        }
    1.57  
    1.58 -      if (totalLoadCounter != null) {
    1.59 -        totalLoad.Value = totalLoadCounter.NextValue();
    1.60 -        ActivateSensor(totalLoad);
    1.61 +      if (cpuLoad.IsAvailable) {
    1.62 +        cpuLoad.Update();
    1.63 +        for (int i = 0; i < coreLoads.Length; i++)
    1.64 +          coreLoads[i].Value = cpuLoad.GetCoreLoad(i);
    1.65 +        totalLoad.Value = cpuLoad.GetTotalLoad();
    1.66        }
    1.67 -
    1.68 -      for (int i = 0; i < coreLoads.Length; i++)
    1.69 -        if (coreLoadCounters[i] != null) {
    1.70 -          coreLoads[i].Value = coreLoadCounters[i].NextValue();
    1.71 -          ActivateSensor(coreLoads[i]);
    1.72 -        }
    1.73      }
    1.74  
    1.75      private void ActivateSensor(Sensor sensor) {