Hardware/CPU/AMD10CPU.cs
changeset 24 09ab31bee6bd
parent 22 e6832d4b89d2
child 25 ff3e6edc7113
     1.1 --- a/Hardware/CPU/AMD10CPU.cs	Wed Feb 03 18:10:11 2010 +0000
     1.2 +++ b/Hardware/CPU/AMD10CPU.cs	Wed Feb 03 20:35:10 2010 +0000
     1.3 @@ -38,6 +38,7 @@
     1.4  using System;
     1.5  using System.Collections.Generic;
     1.6  using System.Drawing;
     1.7 +using System.Diagnostics;
     1.8  using System.Text;
     1.9  
    1.10  namespace OpenHardwareMonitor.Hardware.CPU {
    1.11 @@ -49,6 +50,13 @@
    1.12      private uint pciAddress;
    1.13  
    1.14      private Sensor coreTemperature;
    1.15 +    private Sensor totalLoad;
    1.16 +    private Sensor[] coreLoads;
    1.17 +
    1.18 +    private List<ISensor> active = new List<ISensor>();
    1.19 +
    1.20 +    private PerformanceCounter totalLoadCounter;
    1.21 +    private PerformanceCounter[] coreLoadCounters;
    1.22  
    1.23      private const ushort PCI_AMD_VENDOR_ID = 0x1022;
    1.24      private const ushort PCI_AMD_10H_MISCELLANEOUS_DEVICE_ID = 0x1203;
    1.25 @@ -63,6 +71,23 @@
    1.26        uint coreCount = 1;
    1.27        if (cpuidExtData.GetLength(0) > 8)
    1.28          coreCount = (cpuidExtData[8, 2] & 0xFF) + 1;
    1.29 +
    1.30 +      totalLoadCounter = new PerformanceCounter();
    1.31 +      totalLoadCounter.CategoryName = "Processor";
    1.32 +      totalLoadCounter.CounterName = "% Processor Time";
    1.33 +      totalLoadCounter.InstanceName = "_Total";
    1.34 +      totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);
    1.35 +
    1.36 +      coreLoadCounters = new PerformanceCounter[coreCount];
    1.37 +      coreLoads = new Sensor[coreCount];
    1.38 +      for (int i = 0; i < coreLoadCounters.Length; i++) {
    1.39 +        coreLoadCounters[i] = new PerformanceCounter();
    1.40 +        coreLoadCounters[i].CategoryName = "Processor";
    1.41 +        coreLoadCounters[i].CounterName = "% Processor Time";
    1.42 +        coreLoadCounters[i].InstanceName = i.ToString();
    1.43 +        coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1,
    1.44 +          SensorType.Load, this);
    1.45 +      }        
    1.46        
    1.47        // AMD family 10h processors support only one temperature sensor
    1.48        coreTemperature = new Sensor(
    1.49 @@ -87,9 +112,7 @@
    1.50      }
    1.51  
    1.52      public ISensor[] Sensors {
    1.53 -      get {
    1.54 -        return new ISensor[] { coreTemperature };
    1.55 -      }
    1.56 +      get { return active.ToArray(); }
    1.57      }
    1.58  
    1.59      public string GetReport() {
    1.60 @@ -102,14 +125,40 @@
    1.61  
    1.62        uint value;      
    1.63        if (WinRing0.ReadPciConfigDwordEx(pciAddress, 
    1.64 -        REPORTED_TEMPERATURE_CONTROL_REGISTER, out value)) 
    1.65 -        coreTemperature.Value = ((value >> 21) & 0x7FF) / 8.0f;      
    1.66 +        REPORTED_TEMPERATURE_CONTROL_REGISTER, out value)) {
    1.67 +        coreTemperature.Value = ((value >> 21) & 0x7FF) / 8.0f;
    1.68 +        ActivateSensor(coreTemperature);
    1.69 +      } else {
    1.70 +        DeactivateSensor(coreTemperature);
    1.71 +      }
    1.72 +
    1.73 +      totalLoad.Value = totalLoadCounter.NextValue();
    1.74 +      ActivateSensor(totalLoad);
    1.75 +
    1.76 +      for (int i = 0; i < coreLoads.Length; i++) {
    1.77 +        coreLoads[i].Value = coreLoadCounters[i].NextValue();       
    1.78 +        ActivateSensor(coreLoads[i]);
    1.79 +      }
    1.80      }
    1.81  
    1.82 -    #pragma warning disable 67
    1.83 +    private void ActivateSensor(Sensor sensor) {
    1.84 +      if (!active.Contains(sensor)) {
    1.85 +        active.Add(sensor);
    1.86 +        if (SensorAdded != null)
    1.87 +          SensorAdded(sensor);
    1.88 +      }
    1.89 +    }
    1.90 +
    1.91 +    private void DeactivateSensor(Sensor sensor) {
    1.92 +      if (active.Contains(sensor)) {
    1.93 +        active.Remove(sensor);
    1.94 +        if (SensorRemoved != null)
    1.95 +          SensorRemoved(sensor);
    1.96 +      }
    1.97 +    }
    1.98 +
    1.99      public event SensorEventHandler SensorAdded;
   1.100      public event SensorEventHandler SensorRemoved;
   1.101 -    #pragma warning restore 67
   1.102  
   1.103    }
   1.104  }