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