Hardware/CPU/IntelCPU.cs
changeset 63 1a7c13ac7348
parent 52 8495c0ee29ac
child 69 5f539c00e925
     1.1 --- a/Hardware/CPU/IntelCPU.cs	Tue Feb 23 19:45:40 2010 +0000
     1.2 +++ b/Hardware/CPU/IntelCPU.cs	Sat Feb 27 15:55:17 2010 +0000
     1.3 @@ -40,6 +40,8 @@
     1.4  using System.Drawing;
     1.5  using System.Diagnostics;
     1.6  using System.Reflection;
     1.7 +using System.Runtime.InteropServices;
     1.8 +using System.Threading;
     1.9  using System.Text;
    1.10  
    1.11  namespace OpenHardwareMonitor.Hardware.CPU {
    1.12 @@ -53,15 +55,15 @@
    1.13      private uint stepping;
    1.14  
    1.15      private Sensor[] coreTemperatures;
    1.16 +
    1.17      private Sensor totalLoad;
    1.18      private Sensor[] coreLoads;
    1.19      private Sensor[] coreClocks;
    1.20      private Sensor busClock;
    1.21 -
    1.22 -    private float tjMax = 0;
    1.23      private uint logicalProcessors;
    1.24      private uint logicalProcessorsPerCore;
    1.25      private uint coreCount;
    1.26 +    private ulong affinityMask;
    1.27  
    1.28      private CPULoad cpuLoad;
    1.29  
    1.30 @@ -81,6 +83,10 @@
    1.31          return "CPU Core #" + (i + 1);
    1.32      }
    1.33  
    1.34 +    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    1.35 +    private static extern bool GetProcessAffinityMask(IntPtr handle, 
    1.36 +      out IntPtr processMask, out IntPtr systemMask);
    1.37 +
    1.38      public IntelCPU(string name, uint family, uint model, uint stepping, 
    1.39        uint[,] cpuidData, uint[,] cpuidExtData) {
    1.40        
    1.41 @@ -117,8 +123,30 @@
    1.42          logicalProcessorsPerCore = 1;
    1.43        }
    1.44  
    1.45 +      IntPtr processMask, systemMask;
    1.46 +      GetProcessAffinityMask(Process.GetCurrentProcess().Handle,
    1.47 +        out processMask, out systemMask);
    1.48 +      affinityMask = (ulong)systemMask;
    1.49 +
    1.50 +      // correct values in case HypeThreading is disabled
    1.51 +      if (logicalProcessorsPerCore > 1) {
    1.52 +        ulong affinity = affinityMask;
    1.53 +        int availableLogicalProcessors = 0;
    1.54 +        while (affinity != 0) {
    1.55 +          if ((affinity & 0x1) > 0)
    1.56 +            availableLogicalProcessors++;
    1.57 +          affinity >>= 1;
    1.58 +        }
    1.59 +        while (logicalProcessorsPerCore > 1 &&
    1.60 +          availableLogicalProcessors < logicalProcessors) {
    1.61 +          logicalProcessors >>= 1;
    1.62 +          logicalProcessorsPerCore >>= 1;
    1.63 +        }
    1.64 +      }
    1.65 +
    1.66        coreCount = logicalProcessors / logicalProcessorsPerCore;
    1.67 -      
    1.68 +
    1.69 +      float tjMax;
    1.70        switch (family) {
    1.71          case 0x06: {
    1.72              switch (model) {
    1.73 @@ -170,7 +198,13 @@
    1.74          coreTemperatures = new Sensor[coreCount];
    1.75          for (int i = 0; i < coreTemperatures.Length; i++) {
    1.76            coreTemperatures[i] = new Sensor(CoreString(i), i, tjMax,
    1.77 -            SensorType.Temperature, this);
    1.78 +            SensorType.Temperature, this, new ParameterDescription[] { 
    1.79 +              new ParameterDescription(
    1.80 +                "TjMax", "TjMax temperature of the core.\n" + 
    1.81 +                "Temperature = TjMax - TSlope * Value.", tjMax), 
    1.82 +              new ParameterDescription(
    1.83 +                "TSlope", "Temperature slope of the digital thermal sensor.\n" + 
    1.84 +                "Temperature = TjMax - TSlope * Value.", 1)});
    1.85          }
    1.86        } else {
    1.87          coreTemperatures = new Sensor[0];
    1.88 @@ -237,11 +271,12 @@
    1.89        r.AppendLine("Intel CPU");
    1.90        r.AppendLine();
    1.91        r.AppendFormat("Name: {0}{1}", name, Environment.NewLine);
    1.92 -      r.AppendFormat("Number of cores: {0}{1}", coreCount, 
    1.93 +      r.AppendFormat("Number of Cores: {0}{1}", coreCount, 
    1.94          Environment.NewLine);
    1.95 -      r.AppendFormat("Threads per core: {0}{1}", logicalProcessorsPerCore,
    1.96 +      r.AppendFormat("Threads per Core: {0}{1}", logicalProcessorsPerCore,
    1.97          Environment.NewLine);
    1.98 -      r.AppendFormat("TjMax: {0}{1}", tjMax, Environment.NewLine);
    1.99 +      r.AppendFormat("Affinity Mask: 0x{0}{1}", affinityMask.ToString("X"),
   1.100 +        Environment.NewLine);  
   1.101        r.AppendLine();
   1.102  
   1.103        for (int i = 0; i < coreCount; i++) {
   1.104 @@ -269,7 +304,10 @@
   1.105            // if reading is valid
   1.106            if ((eax & 0x80000000) != 0) {
   1.107              // get the dist from tjMax from bits 22:16
   1.108 -            coreTemperatures[i].Value = tjMax - ((eax & 0x007F0000) >> 16);
   1.109 +            float deltaT = ((eax & 0x007F0000) >> 16);
   1.110 +            float tjMax = coreTemperatures[i].Parameters[0].Value;
   1.111 +            float tSlope = coreTemperatures[i].Parameters[1].Value;
   1.112 +            coreTemperatures[i].Value = tjMax - tSlope * deltaT;
   1.113              ActivateSensor(coreTemperatures[i]);
   1.114            } else {
   1.115              DeactivateSensor(coreTemperatures[i]);