Hardware/CPU/IntelCPU.cs
author moel.mich
Mon, 05 Apr 2010 21:31:21 +0000
changeset 89 62e0b6011e8c
parent 79 9cdbe1d8d12a
child 90 3333b29a1746
permissions -rw-r--r--
Fixed Issue 25.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Drawing;
    41 using System.Diagnostics;
    42 using System.Globalization;
    43 using System.Reflection;
    44 using System.Runtime.InteropServices;
    45 using System.Threading;
    46 using System.Text;
    47 
    48 namespace OpenHardwareMonitor.Hardware.CPU {
    49   public class IntelCPU : Hardware, IHardware {
    50 
    51     private string name;
    52     private Image icon;
    53 
    54     private uint family;
    55     private uint model;
    56     private uint stepping;
    57 
    58     private Sensor[] coreTemperatures;
    59 
    60     private Sensor totalLoad;
    61     private Sensor[] coreLoads;
    62     private Sensor[] coreClocks;
    63     private Sensor busClock;
    64     private uint logicalProcessors;
    65     private uint logicalProcessorsPerCore;
    66     private uint coreCount;
    67     private bool hasTSC;
    68     private bool invariantTSC;    
    69     private double estimatedMaxClock;
    70 
    71     private ulong affinityMask;
    72     private CPULoad cpuLoad;
    73 
    74     private ulong lastTimeStampCount;    
    75     private long lastTime;
    76     private uint maxNehalemMultiplier = 0;    
    77     
    78     private const uint IA32_THERM_STATUS_MSR = 0x019C;
    79     private const uint IA32_TEMPERATURE_TARGET = 0x01A2;
    80     private const uint IA32_PERF_STATUS = 0x0198;
    81     private const uint MSR_PLATFORM_INFO = 0xCE;
    82 
    83     private string CoreString(int i) {
    84       if (coreCount == 1)
    85         return "CPU Core";
    86       else
    87         return "CPU Core #" + (i + 1);
    88     }
    89 
    90     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    91     private static extern bool GetProcessAffinityMask(IntPtr handle, 
    92       out IntPtr processMask, out IntPtr systemMask);
    93 
    94     private float[] Floats(float f) {
    95       float[] result = new float[coreCount];
    96       for (int i = 0; i < coreCount; i++)
    97         result[i] = f;
    98       return result;
    99     }
   100 
   101     public IntelCPU(string name, uint family, uint model, uint stepping, 
   102       uint[,] cpuidData, uint[,] cpuidExtData) {
   103       
   104       this.name = name;
   105       this.icon = Utilities.EmbeddedResources.GetImage("cpu.png");
   106 
   107       this.family = family;
   108       this.model = model;
   109       this.stepping = stepping;
   110 
   111       logicalProcessors = 0;
   112       if (cpuidData.GetLength(0) > 0x0B) {
   113         uint eax, ebx, ecx, edx;
   114         WinRing0.CpuidEx(0x0B, 0, out eax, out ebx, out ecx, out edx);
   115         logicalProcessorsPerCore = ebx & 0xFF;
   116         if (logicalProcessorsPerCore > 0) {
   117           WinRing0.CpuidEx(0x0B, 1, out eax, out ebx, out ecx, out edx);
   118           logicalProcessors = ebx & 0xFF;
   119         }   
   120       }
   121       if (logicalProcessors <= 0 && cpuidData.GetLength(0) > 0x04) {
   122         uint coresPerPackage = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
   123         uint logicalPerPackage = (cpuidData[1, 1] >> 16) & 0xFF;        
   124         logicalProcessorsPerCore = logicalPerPackage / coresPerPackage;
   125         logicalProcessors = logicalPerPackage;
   126       }
   127       if (logicalProcessors <= 0 && cpuidData.GetLength(0) > 0x01) {
   128         uint logicalPerPackage = (cpuidData[1, 1] >> 16) & 0xFF;
   129         logicalProcessorsPerCore = logicalPerPackage;
   130         logicalProcessors = logicalPerPackage;
   131       }
   132       if (logicalProcessors <= 0) {
   133         logicalProcessors = 1;
   134         logicalProcessorsPerCore = 1;
   135       }
   136 
   137       IntPtr processMask, systemMask;
   138       GetProcessAffinityMask(Process.GetCurrentProcess().Handle,
   139         out processMask, out systemMask);
   140       affinityMask = (ulong)systemMask;
   141 
   142       // correct values in case HypeThreading is disabled
   143       if (logicalProcessorsPerCore > 1) {
   144         ulong affinity = affinityMask;
   145         int availableLogicalProcessors = 0;
   146         while (affinity != 0) {
   147           if ((affinity & 0x1) > 0)
   148             availableLogicalProcessors++;
   149           affinity >>= 1;
   150         }
   151         while (logicalProcessorsPerCore > 1 &&
   152           availableLogicalProcessors < logicalProcessors) {
   153           logicalProcessors >>= 1;
   154           logicalProcessorsPerCore >>= 1;
   155         }
   156       }
   157 
   158       coreCount = logicalProcessors / logicalProcessorsPerCore;
   159 
   160       float[] tjMax;
   161       switch (family) {
   162         case 0x06: {
   163             switch (model) {
   164               case 0x0F: // Intel Core (65nm)
   165                 switch (stepping) {
   166                   case 0x06: // B2
   167                     switch (coreCount) {
   168                       case 2:
   169                         tjMax = Floats(80 + 10); break;
   170                       case 4:
   171                         tjMax = Floats(90 + 10); break;
   172                       default:
   173                         tjMax = Floats(85 + 10); break;
   174                     }
   175                     tjMax = Floats(80 + 10); break;
   176                   case 0x0B: // G0
   177                     tjMax = Floats(90 + 10); break;
   178                   case 0x0D: // M0
   179                     tjMax = Floats(85 + 10); break;
   180                   default:
   181                     tjMax = Floats(85 + 10); break;
   182                 } break;
   183               case 0x17: // Intel Core (45nm)
   184                 tjMax = Floats(100); break;
   185               case 0x1C: // Intel Atom 
   186                 tjMax = Floats(90); break;
   187               case 0x1A: // Intel Core i7 LGA1366 (45nm)
   188               case 0x1E: // Intel Core i5, i7 LGA1156 (45nm)
   189               case 0x25: // Intel Core i3, i5, i7 LGA1156 (32nm)
   190                 uint eax, edx;
   191                 tjMax = new float[coreCount];
   192                 for (int i = 0; i < coreCount; i++) {
   193                   if (WinRing0.RdmsrTx(IA32_TEMPERATURE_TARGET, out eax,
   194                     out edx, (UIntPtr)(
   195                     1 << (int)(logicalProcessorsPerCore * i)))) 
   196                   {
   197                     tjMax[i] = (eax >> 16) & 0xFF;
   198                   } else {
   199                     tjMax[i] = 100;
   200                   }
   201                 }
   202                 if (WinRing0.Rdmsr(MSR_PLATFORM_INFO, out eax, out edx)) {
   203                   maxNehalemMultiplier = (eax >> 8) & 0xff;
   204                 }
   205                 break;
   206               default:
   207                 tjMax = Floats(100); break;
   208             }
   209           } break;
   210         default: tjMax = Floats(100); break;
   211       }
   212 
   213       // check if processor supports a digital thermal sensor
   214       if (cpuidData.GetLength(0) > 6 && (cpuidData[6, 0] & 1) != 0) {
   215         coreTemperatures = new Sensor[coreCount];
   216         for (int i = 0; i < coreTemperatures.Length; i++) {
   217           coreTemperatures[i] = new Sensor(CoreString(i), i, tjMax[i],
   218             SensorType.Temperature, this, new ParameterDescription[] { 
   219               new ParameterDescription(
   220                 "TjMax", "TjMax temperature of the core.\n" + 
   221                 "Temperature = TjMax - TSlope * Value.", tjMax[i]), 
   222               new ParameterDescription(
   223                 "TSlope", "Temperature slope of the digital thermal sensor.\n" + 
   224                 "Temperature = TjMax - TSlope * Value.", 1)});
   225         }
   226       } else {
   227         coreTemperatures = new Sensor[0];
   228       }
   229 
   230       if (coreCount > 1)
   231         totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);
   232       else
   233         totalLoad = null;
   234       coreLoads = new Sensor[coreCount];
   235       for (int i = 0; i < coreLoads.Length; i++)
   236         coreLoads[i] = new Sensor(CoreString(i), i + 1,
   237           SensorType.Load, this);     
   238       cpuLoad = new CPULoad(coreCount, logicalProcessorsPerCore);
   239       if (cpuLoad.IsAvailable) {
   240         foreach (Sensor sensor in coreLoads)
   241           ActivateSensor(sensor);
   242         if (totalLoad != null)
   243           ActivateSensor(totalLoad);
   244       }
   245 
   246       // check if processor has TSC
   247       if (cpuidData.GetLength(0) > 1 && (cpuidData[1, 3] & 0x10) != 0)
   248         hasTSC = true;
   249       else
   250         hasTSC = false; 
   251 
   252       // check if processor supports invariant TSC 
   253       if (cpuidExtData.GetLength(0) > 7 && (cpuidExtData[7, 3] & 0x100) != 0)
   254         invariantTSC = true;
   255       else
   256         invariantTSC = false;
   257 
   258       // preload the function
   259       EstimateMaxClock(0); 
   260       EstimateMaxClock(0); 
   261 
   262       // estimate the max clock in MHz      
   263       estimatedMaxClock = 1e-6 * EstimateMaxClock(0.01);
   264 
   265       lastTimeStampCount = 0;
   266       lastTime = 0;
   267       busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this);      
   268       coreClocks = new Sensor[coreCount];
   269       for (int i = 0; i < coreClocks.Length; i++) {
   270         coreClocks[i] =
   271           new Sensor(CoreString(i), i + 1, SensorType.Clock, this);
   272         if (hasTSC)
   273           ActivateSensor(coreClocks[i]);
   274       }
   275       
   276       Update();                   
   277     }
   278 
   279     public string Name {
   280       get { return name; }
   281     }
   282 
   283     public string Identifier {
   284       get { return "/intelcpu/0"; }
   285     }
   286 
   287     public Image Icon {
   288       get { return icon; }
   289     }
   290 
   291     private void AppendMSRData(StringBuilder r, uint msr, int core) {
   292       uint eax, edx;
   293       if (WinRing0.RdmsrTx(msr, out eax, out edx,
   294          (UIntPtr)(1 << (int)(logicalProcessorsPerCore * core)))) {
   295         r.Append(" ");
   296         r.Append((msr).ToString("X8"));
   297         r.Append("  ");
   298         r.Append((edx).ToString("X8"));
   299         r.Append("  ");
   300         r.Append((eax).ToString("X8"));
   301         r.AppendLine();
   302       }
   303     }
   304 
   305     public string GetReport() {
   306       StringBuilder r = new StringBuilder();
   307 
   308       r.AppendLine("Intel CPU");
   309       r.AppendLine();
   310       r.AppendFormat("Name: {0}{1}", name, Environment.NewLine);
   311       r.AppendFormat("Number of Cores: {0}{1}", coreCount, 
   312         Environment.NewLine);
   313       r.AppendFormat("Threads per Core: {0}{1}", logicalProcessorsPerCore,
   314         Environment.NewLine);
   315       r.AppendFormat("Affinity Mask: 0x{0}{1}", affinityMask.ToString("X"),
   316         Environment.NewLine);
   317       r.AppendLine("TSC: " + 
   318         (hasTSC ? (invariantTSC ? "Invariant" : "Not Invariant") : "None"));
   319       r.AppendLine(string.Format(CultureInfo.InvariantCulture, 
   320         "Timer Frequency: {0} MHz", Stopwatch.Frequency * 1e-6));
   321       r.AppendLine(string.Format(CultureInfo.InvariantCulture,
   322         "Max Clock: {0} MHz", Math.Round(estimatedMaxClock * 100) * 0.01));
   323       r.AppendLine();
   324 
   325       for (int i = 0; i < coreCount; i++) {
   326         r.AppendLine("MSR Core #" + (i + 1));
   327         r.AppendLine();
   328         r.AppendLine(" MSR       EDX       EAX");
   329         AppendMSRData(r, MSR_PLATFORM_INFO, i);
   330         AppendMSRData(r, IA32_PERF_STATUS, i);
   331         AppendMSRData(r, IA32_THERM_STATUS_MSR, i);
   332         AppendMSRData(r, IA32_TEMPERATURE_TARGET, i);
   333         r.AppendLine();
   334       }
   335 
   336       return r.ToString();
   337     }
   338 
   339     private double EstimateMaxClock(double timeWindow) {
   340       long ticks = (long)(timeWindow * Stopwatch.Frequency);
   341       uint lsbBegin, msbBegin, lsbEnd, msbEnd; 
   342       
   343       Thread.BeginThreadAffinity();
   344       long timeBegin = Stopwatch.GetTimestamp() + 2;
   345       long timeEnd = timeBegin + ticks;      
   346       while (Stopwatch.GetTimestamp() < timeBegin) { }
   347       WinRing0.Rdtsc(out lsbBegin, out msbBegin);
   348       while (Stopwatch.GetTimestamp() < timeEnd) { }
   349       WinRing0.Rdtsc(out lsbEnd, out msbEnd);
   350       Thread.EndThreadAffinity();
   351 
   352       ulong countBegin = ((ulong)msbBegin << 32) | lsbBegin;
   353       ulong countEnd = ((ulong)msbEnd << 32) | lsbEnd;
   354 
   355       return (((double)(countEnd - countBegin)) * Stopwatch.Frequency) / 
   356         (timeEnd - timeBegin);
   357     }
   358 
   359     public void Update() {      
   360       for (int i = 0; i < coreTemperatures.Length; i++) {
   361         uint eax, edx;
   362         if (WinRing0.RdmsrTx(
   363           IA32_THERM_STATUS_MSR, out eax, out edx,
   364             (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) {
   365           // if reading is valid
   366           if ((eax & 0x80000000) != 0) {
   367             // get the dist from tjMax from bits 22:16
   368             float deltaT = ((eax & 0x007F0000) >> 16);
   369             float tjMax = coreTemperatures[i].Parameters[0].Value;
   370             float tSlope = coreTemperatures[i].Parameters[1].Value;
   371             coreTemperatures[i].Value = tjMax - tSlope * deltaT;
   372             ActivateSensor(coreTemperatures[i]);
   373           } else {
   374             DeactivateSensor(coreTemperatures[i]);
   375           }
   376         }
   377       }
   378 
   379       if (cpuLoad.IsAvailable) {
   380         cpuLoad.Update();
   381         for (int i = 0; i < coreLoads.Length; i++)
   382           coreLoads[i].Value = cpuLoad.GetCoreLoad(i);
   383         if (totalLoad != null)
   384           totalLoad.Value = cpuLoad.GetTotalLoad();
   385       }
   386 
   387       if (hasTSC) {
   388         uint lsb, msb;
   389         WinRing0.RdtscTx(out lsb, out msb, (UIntPtr)1);
   390         long time = Stopwatch.GetTimestamp();
   391         ulong timeStampCount = ((ulong)msb << 32) | lsb;
   392         double delta = ((double)(time - lastTime)) / Stopwatch.Frequency;
   393         if (delta > 0.5) {
   394           double maxClock;
   395           if (invariantTSC)
   396             maxClock = (timeStampCount - lastTimeStampCount) / (1e6 * delta);
   397           else
   398             maxClock = estimatedMaxClock;
   399 
   400           double busClock = 0;
   401           uint eax, edx;
   402           for (int i = 0; i < coreClocks.Length; i++) {
   403             System.Threading.Thread.Sleep(1);
   404             if (WinRing0.RdmsrTx(IA32_PERF_STATUS, out eax, out edx,
   405               (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) {
   406               if (maxNehalemMultiplier > 0) { // Core i3, i5, i7
   407                 uint nehalemMultiplier = eax & 0xff;
   408                 coreClocks[i].Value =
   409                   (float)(nehalemMultiplier * maxClock / maxNehalemMultiplier);
   410                 busClock = (float)(maxClock / maxNehalemMultiplier);
   411               } else { // Core 2
   412                 uint multiplier = (eax >> 8) & 0x1f;
   413                 uint maxMultiplier = (edx >> 8) & 0x1f;
   414                 // factor = multiplier * 2 to handle non integer multipliers 
   415                 uint factor = (multiplier << 1) | ((eax >> 14) & 1);
   416                 uint maxFactor = (maxMultiplier << 1) | ((edx >> 14) & 1);
   417                 if (maxFactor > 0) {
   418                   coreClocks[i].Value = (float)(factor * maxClock / maxFactor);
   419                   busClock = (float)(2 * maxClock / maxFactor);
   420                 }
   421               }
   422             } else { // Intel Pentium 4
   423               // if IA32_PERF_STATUS is not available, assume maxClock
   424               coreClocks[i].Value = (float)maxClock;
   425             }
   426           }
   427           if (busClock > 0) {
   428             this.busClock.Value = (float)busClock;
   429             ActivateSensor(this.busClock);
   430           }
   431         }
   432         lastTimeStampCount = timeStampCount;
   433         lastTime = time;
   434       }
   435     }
   436   }  
   437 }