Hardware/CPU/IntelCPU.cs
changeset 90 3333b29a1746
parent 86 b4f0f206173d
child 91 c0937b698b81
     1.1 --- a/Hardware/CPU/IntelCPU.cs	Mon Apr 05 21:31:21 2010 +0000
     1.2 +++ b/Hardware/CPU/IntelCPU.cs	Sat Apr 24 19:59:52 2010 +0000
     1.3 @@ -48,6 +48,9 @@
     1.4  namespace OpenHardwareMonitor.Hardware.CPU {
     1.5    public class IntelCPU : Hardware, IHardware {
     1.6  
     1.7 +    private CPUID[][] cpuid;
     1.8 +    private int coreCount;
     1.9 +    
    1.10      private string name;
    1.11      private Image icon;
    1.12  
    1.13 @@ -60,15 +63,11 @@
    1.14      private Sensor totalLoad;
    1.15      private Sensor[] coreLoads;
    1.16      private Sensor[] coreClocks;
    1.17 -    private Sensor busClock;
    1.18 -    private uint logicalProcessors;
    1.19 -    private uint logicalProcessorsPerCore;
    1.20 -    private uint coreCount;
    1.21 +    private Sensor busClock;    
    1.22      private bool hasTSC;
    1.23      private bool invariantTSC;    
    1.24      private double estimatedMaxClock;
    1.25  
    1.26 -    private ulong affinityMask;
    1.27      private CPULoad cpuLoad;
    1.28  
    1.29      private ulong lastTimeStampCount;    
    1.30 @@ -87,10 +86,6 @@
    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      private float[] Floats(float f) {
    1.39        float[] result = new float[coreCount];
    1.40        for (int i = 0; i < coreCount; i++)
    1.41 @@ -98,64 +93,16 @@
    1.42        return result;
    1.43      }
    1.44  
    1.45 -    public IntelCPU(string name, uint family, uint model, uint stepping, 
    1.46 -      uint[,] cpuidData, uint[,] cpuidExtData) {
    1.47 -      
    1.48 -      this.name = name;
    1.49 +    public IntelCPU(CPUID[][] cpuid) {
    1.50 +
    1.51 +      this.cpuid = cpuid;
    1.52 +      this.coreCount = cpuid.Length;
    1.53 +      this.name = cpuid[0][0].Name;
    1.54        this.icon = Utilities.EmbeddedResources.GetImage("cpu.png");
    1.55  
    1.56 -      this.family = family;
    1.57 -      this.model = model;
    1.58 -      this.stepping = stepping;
    1.59 -
    1.60 -      logicalProcessors = 0;
    1.61 -      if (cpuidData.GetLength(0) > 0x0B) {
    1.62 -        uint eax, ebx, ecx, edx;
    1.63 -        WinRing0.CpuidEx(0x0B, 0, out eax, out ebx, out ecx, out edx);
    1.64 -        logicalProcessorsPerCore = ebx & 0xFF;
    1.65 -        if (logicalProcessorsPerCore > 0) {
    1.66 -          WinRing0.CpuidEx(0x0B, 1, out eax, out ebx, out ecx, out edx);
    1.67 -          logicalProcessors = ebx & 0xFF;
    1.68 -        }   
    1.69 -      }
    1.70 -      if (logicalProcessors <= 0 && cpuidData.GetLength(0) > 0x04) {
    1.71 -        uint coresPerPackage = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
    1.72 -        uint logicalPerPackage = (cpuidData[1, 1] >> 16) & 0xFF;        
    1.73 -        logicalProcessorsPerCore = logicalPerPackage / coresPerPackage;
    1.74 -        logicalProcessors = logicalPerPackage;
    1.75 -      }
    1.76 -      if (logicalProcessors <= 0 && cpuidData.GetLength(0) > 0x01) {
    1.77 -        uint logicalPerPackage = (cpuidData[1, 1] >> 16) & 0xFF;
    1.78 -        logicalProcessorsPerCore = logicalPerPackage;
    1.79 -        logicalProcessors = logicalPerPackage;
    1.80 -      }
    1.81 -      if (logicalProcessors <= 0) {
    1.82 -        logicalProcessors = 1;
    1.83 -        logicalProcessorsPerCore = 1;
    1.84 -      }
    1.85 -
    1.86 -      IntPtr processMask, systemMask;
    1.87 -      GetProcessAffinityMask(Process.GetCurrentProcess().Handle,
    1.88 -        out processMask, out systemMask);
    1.89 -      affinityMask = (ulong)systemMask;
    1.90 -
    1.91 -      // correct values in case HypeThreading is disabled
    1.92 -      if (logicalProcessorsPerCore > 1) {
    1.93 -        ulong affinity = affinityMask;
    1.94 -        int availableLogicalProcessors = 0;
    1.95 -        while (affinity != 0) {
    1.96 -          if ((affinity & 0x1) > 0)
    1.97 -            availableLogicalProcessors++;
    1.98 -          affinity >>= 1;
    1.99 -        }
   1.100 -        while (logicalProcessorsPerCore > 1 &&
   1.101 -          availableLogicalProcessors < logicalProcessors) {
   1.102 -          logicalProcessors >>= 1;
   1.103 -          logicalProcessorsPerCore >>= 1;
   1.104 -        }
   1.105 -      }
   1.106 -
   1.107 -      coreCount = logicalProcessors / logicalProcessorsPerCore;
   1.108 +      this.family = cpuid[0][0].Family;
   1.109 +      this.model = cpuid[0][0].Model;
   1.110 +      this.stepping = cpuid[0][0].Stepping;
   1.111  
   1.112        float[] tjMax;
   1.113        switch (family) {
   1.114 @@ -191,8 +138,7 @@
   1.115                  tjMax = new float[coreCount];
   1.116                  for (int i = 0; i < coreCount; i++) {
   1.117                    if (WinRing0.RdmsrTx(IA32_TEMPERATURE_TARGET, out eax,
   1.118 -                    out edx, (UIntPtr)(
   1.119 -                    1 << (int)(logicalProcessorsPerCore * i)))) 
   1.120 +                    out edx, (UIntPtr)(1L << cpuid[i][0].Thread)))
   1.121                    {
   1.122                      tjMax[i] = (eax >> 16) & 0xFF;
   1.123                    } else {
   1.124 @@ -211,7 +157,9 @@
   1.125        }
   1.126  
   1.127        // check if processor supports a digital thermal sensor
   1.128 -      if (cpuidData.GetLength(0) > 6 && (cpuidData[6, 0] & 1) != 0) {
   1.129 +      if (cpuid[0][0].Data.GetLength(0) > 6 && 
   1.130 +        (cpuid[0][0].Data[6, 0] & 1) != 0) 
   1.131 +      {
   1.132          coreTemperatures = new Sensor[coreCount];
   1.133          for (int i = 0; i < coreTemperatures.Length; i++) {
   1.134            coreTemperatures[i] = new Sensor(CoreString(i), i, tjMax[i],
   1.135 @@ -235,7 +183,7 @@
   1.136        for (int i = 0; i < coreLoads.Length; i++)
   1.137          coreLoads[i] = new Sensor(CoreString(i), i + 1,
   1.138            SensorType.Load, this);     
   1.139 -      cpuLoad = new CPULoad(coreCount, logicalProcessorsPerCore);
   1.140 +      cpuLoad = new CPULoad(cpuid);
   1.141        if (cpuLoad.IsAvailable) {
   1.142          foreach (Sensor sensor in coreLoads)
   1.143            ActivateSensor(sensor);
   1.144 @@ -244,13 +192,15 @@
   1.145        }
   1.146  
   1.147        // check if processor has TSC
   1.148 -      if (cpuidData.GetLength(0) > 1 && (cpuidData[1, 3] & 0x10) != 0)
   1.149 +      if (cpuid[0][0].Data.GetLength(0) > 1 
   1.150 +        && (cpuid[0][0].Data[1, 3] & 0x10) != 0)
   1.151          hasTSC = true;
   1.152        else
   1.153          hasTSC = false; 
   1.154  
   1.155        // check if processor supports invariant TSC 
   1.156 -      if (cpuidExtData.GetLength(0) > 7 && (cpuidExtData[7, 3] & 0x100) != 0)
   1.157 +      if (cpuid[0][0].ExtData.GetLength(0) > 7 
   1.158 +        && (cpuid[0][0].ExtData[7, 3] & 0x100) != 0)
   1.159          invariantTSC = true;
   1.160        else
   1.161          invariantTSC = false;
   1.162 @@ -288,10 +238,9 @@
   1.163        get { return icon; }
   1.164      }
   1.165  
   1.166 -    private void AppendMSRData(StringBuilder r, uint msr, int core) {
   1.167 +    private void AppendMSRData(StringBuilder r, uint msr, int thread) {
   1.168        uint eax, edx;
   1.169 -      if (WinRing0.RdmsrTx(msr, out eax, out edx,
   1.170 -         (UIntPtr)(1 << (int)(logicalProcessorsPerCore * core)))) {
   1.171 +      if (WinRing0.RdmsrTx(msr, out eax, out edx, (UIntPtr)(1L << thread))) {
   1.172          r.Append(" ");
   1.173          r.Append((msr).ToString("X8"));
   1.174          r.Append("  ");
   1.175 @@ -310,10 +259,8 @@
   1.176        r.AppendFormat("Name: {0}{1}", name, Environment.NewLine);
   1.177        r.AppendFormat("Number of Cores: {0}{1}", coreCount, 
   1.178          Environment.NewLine);
   1.179 -      r.AppendFormat("Threads per Core: {0}{1}", logicalProcessorsPerCore,
   1.180 -        Environment.NewLine);
   1.181 -      r.AppendFormat("Affinity Mask: 0x{0}{1}", affinityMask.ToString("X"),
   1.182 -        Environment.NewLine);
   1.183 +      r.AppendFormat("Threads per Core: {0}{1}", cpuid[0].Length,
   1.184 +        Environment.NewLine);     
   1.185        r.AppendLine("TSC: " + 
   1.186          (hasTSC ? (invariantTSC ? "Invariant" : "Not Invariant") : "None"));
   1.187        r.AppendLine(string.Format(CultureInfo.InvariantCulture, 
   1.188 @@ -322,14 +269,14 @@
   1.189          "Max Clock: {0} MHz", Math.Round(estimatedMaxClock * 100) * 0.01));
   1.190        r.AppendLine();
   1.191  
   1.192 -      for (int i = 0; i < coreCount; i++) {
   1.193 +      for (int i = 0; i < cpuid.Length; i++) {
   1.194          r.AppendLine("MSR Core #" + (i + 1));
   1.195          r.AppendLine();
   1.196          r.AppendLine(" MSR       EDX       EAX");
   1.197 -        AppendMSRData(r, MSR_PLATFORM_INFO, i);
   1.198 -        AppendMSRData(r, IA32_PERF_STATUS, i);
   1.199 -        AppendMSRData(r, IA32_THERM_STATUS_MSR, i);
   1.200 -        AppendMSRData(r, IA32_TEMPERATURE_TARGET, i);
   1.201 +        AppendMSRData(r, MSR_PLATFORM_INFO, cpuid[i][0].Thread);
   1.202 +        AppendMSRData(r, IA32_PERF_STATUS, cpuid[i][0].Thread);
   1.203 +        AppendMSRData(r, IA32_THERM_STATUS_MSR, cpuid[i][0].Thread);
   1.204 +        AppendMSRData(r, IA32_TEMPERATURE_TARGET, cpuid[i][0].Thread);
   1.205          r.AppendLine();
   1.206        }
   1.207  
   1.208 @@ -360,8 +307,8 @@
   1.209        for (int i = 0; i < coreTemperatures.Length; i++) {
   1.210          uint eax, edx;
   1.211          if (WinRing0.RdmsrTx(
   1.212 -          IA32_THERM_STATUS_MSR, out eax, out edx,
   1.213 -            (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) {
   1.214 +          IA32_THERM_STATUS_MSR, out eax, out edx, 
   1.215 +            (UIntPtr)(1L << cpuid[i][0].Thread))) {
   1.216            // if reading is valid
   1.217            if ((eax & 0x80000000) != 0) {
   1.218              // get the dist from tjMax from bits 22:16
   1.219 @@ -402,7 +349,7 @@
   1.220            for (int i = 0; i < coreClocks.Length; i++) {
   1.221              System.Threading.Thread.Sleep(1);
   1.222              if (WinRing0.RdmsrTx(IA32_PERF_STATUS, out eax, out edx,
   1.223 -              (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) {
   1.224 +              (UIntPtr)(1L << cpuid[i][0].Thread))) {
   1.225                if (maxNehalemMultiplier > 0) { // Core i3, i5, i7
   1.226                  uint nehalemMultiplier = eax & 0xff;
   1.227                  coreClocks[i].Value =