Hardware/CPU/AMD0FCPU.cs
changeset 26 0e01b63e1fdc
parent 25 ff3e6edc7113
child 31 c4d1fb76a9e1
     1.1 --- a/Hardware/CPU/AMD0FCPU.cs	Wed Feb 03 22:02:58 2010 +0000
     1.2 +++ b/Hardware/CPU/AMD0FCPU.cs	Thu Feb 04 21:19:27 2010 +0000
     1.3 @@ -58,8 +58,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_0FH_MISCELLANEOUS_DEVICE_ID = 0x1103;
    1.13 @@ -80,37 +79,23 @@
    1.14        // max two cores
    1.15        coreCount = coreCount > 2 ? 2 : coreCount;
    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 +        
    1.30 +      coreTemperatures = new Sensor[coreCount];
    1.31        coreLoads = new Sensor[coreCount];
    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 +      for (int i = 0; i < coreCount; i++) {
    1.43 +        coreTemperatures[i] =
    1.44 +          new Sensor("Core #" + (i + 1), i, SensorType.Temperature, this);
    1.45          coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1,
    1.46            SensorType.Load, this);
    1.47 -      }        
    1.48 +      }
    1.49  
    1.50 -      coreTemperatures = new Sensor[coreCount];
    1.51 -      for (int i = 0; i < coreCount; i++)
    1.52 -        coreTemperatures[i] = 
    1.53 -          new Sensor("Core #" + (i + 1), i, SensorType.Temperature, this);
    1.54 +      cpuLoad = new CPULoad(coreCount, 1);
    1.55 +      if (cpuLoad.IsAvailable) {
    1.56 +        foreach (Sensor sensor in coreLoads)
    1.57 +          ActivateSensor(sensor);
    1.58 +        ActivateSensor(totalLoad);
    1.59 +      }
    1.60  
    1.61        pciAddress = WinRing0.FindPciDeviceById(PCI_AMD_VENDOR_ID,
    1.62          PCI_AMD_0FH_MISCELLANEOUS_DEVICE_ID, 0);
    1.63 @@ -163,16 +148,12 @@
    1.64          }
    1.65        }
    1.66  
    1.67 -      if (totalLoadCounter != null) {
    1.68 -        totalLoad.Value = totalLoadCounter.NextValue();
    1.69 -        ActivateSensor(totalLoad);
    1.70 +      if (cpuLoad.IsAvailable) {
    1.71 +        cpuLoad.Update();
    1.72 +        for (int i = 0; i < coreLoads.Length; i++)
    1.73 +          coreLoads[i].Value = cpuLoad.GetCoreLoad(i);
    1.74 +        totalLoad.Value = cpuLoad.GetTotalLoad();
    1.75        }
    1.76 -
    1.77 -      for (int i = 0; i < coreLoads.Length; i++)
    1.78 -        if (coreLoadCounters[i] != null) {
    1.79 -          coreLoads[i].Value = coreLoadCounters[i].NextValue();
    1.80 -          ActivateSensor(coreLoads[i]);
    1.81 -        }
    1.82      }
    1.83  
    1.84      private void ActivateSensor(Sensor sensor) {