Hardware/CPU/IntelCPU.cs
author moel.mich
Mon, 08 Mar 2010 20:05:51 +0000
changeset 77 c71d4cd5f29c
parent 63 1a7c13ac7348
child 79 9cdbe1d8d12a
permissions -rw-r--r--
Fixed Issue 12.
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@69
    90
    private float[] Floats(float f) {
moel@69
    91
      float[] result = new float[coreCount];
moel@69
    92
      for (int i = 0; i < coreCount; i++)
moel@69
    93
        result[i] = f;
moel@69
    94
      return result;
moel@69
    95
    }
moel@69
    96
moel@1
    97
    public IntelCPU(string name, uint family, uint model, uint stepping, 
moel@1
    98
      uint[,] cpuidData, uint[,] cpuidExtData) {
moel@1
    99
      
moel@1
   100
      this.name = name;
moel@1
   101
      this.icon = Utilities.EmbeddedResources.GetImage("cpu.png");
moel@46
   102
moel@46
   103
      this.family = family;
moel@46
   104
      this.model = model;
moel@46
   105
      this.stepping = stepping;
moel@46
   106
moel@23
   107
      logicalProcessors = 0;
moel@22
   108
      if (cpuidData.GetLength(0) > 0x0B) {
moel@22
   109
        uint eax, ebx, ecx, edx;
moel@22
   110
        WinRing0.CpuidEx(0x0B, 0, out eax, out ebx, out ecx, out edx);
moel@23
   111
        logicalProcessorsPerCore = ebx & 0xFF;
moel@23
   112
        if (logicalProcessorsPerCore > 0) {
moel@23
   113
          WinRing0.CpuidEx(0x0B, 1, out eax, out ebx, out ecx, out edx);
moel@23
   114
          logicalProcessors = ebx & 0xFF;
moel@23
   115
        }   
moel@23
   116
      }
moel@23
   117
      if (logicalProcessors <= 0 && cpuidData.GetLength(0) > 0x04) {
moel@49
   118
        uint coresPerPackage = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
moel@49
   119
        uint logicalPerPackage = (cpuidData[1, 1] >> 16) & 0xFF;        
moel@49
   120
        logicalProcessorsPerCore = logicalPerPackage / coresPerPackage;
moel@49
   121
        logicalProcessors = logicalPerPackage;
moel@49
   122
      }
moel@49
   123
      if (logicalProcessors <= 0 && cpuidData.GetLength(0) > 0x01) {
moel@49
   124
        uint logicalPerPackage = (cpuidData[1, 1] >> 16) & 0xFF;
moel@49
   125
        logicalProcessorsPerCore = logicalPerPackage;
moel@49
   126
        logicalProcessors = logicalPerPackage;
moel@23
   127
      }
moel@23
   128
      if (logicalProcessors <= 0) {
moel@22
   129
        logicalProcessors = 1;
moel@22
   130
        logicalProcessorsPerCore = 1;
moel@22
   131
      }
moel@4
   132
moel@63
   133
      IntPtr processMask, systemMask;
moel@63
   134
      GetProcessAffinityMask(Process.GetCurrentProcess().Handle,
moel@63
   135
        out processMask, out systemMask);
moel@63
   136
      affinityMask = (ulong)systemMask;
moel@63
   137
moel@63
   138
      // correct values in case HypeThreading is disabled
moel@63
   139
      if (logicalProcessorsPerCore > 1) {
moel@63
   140
        ulong affinity = affinityMask;
moel@63
   141
        int availableLogicalProcessors = 0;
moel@63
   142
        while (affinity != 0) {
moel@63
   143
          if ((affinity & 0x1) > 0)
moel@63
   144
            availableLogicalProcessors++;
moel@63
   145
          affinity >>= 1;
moel@63
   146
        }
moel@63
   147
        while (logicalProcessorsPerCore > 1 &&
moel@63
   148
          availableLogicalProcessors < logicalProcessors) {
moel@63
   149
          logicalProcessors >>= 1;
moel@63
   150
          logicalProcessorsPerCore >>= 1;
moel@63
   151
        }
moel@63
   152
      }
moel@63
   153
moel@22
   154
      coreCount = logicalProcessors / logicalProcessorsPerCore;
moel@63
   155
moel@69
   156
      float[] tjMax;
moel@49
   157
      switch (family) {
moel@49
   158
        case 0x06: {
moel@49
   159
            switch (model) {
moel@49
   160
              case 0x0F: // Intel Core (65nm)
moel@49
   161
                switch (stepping) {
moel@49
   162
                  case 0x06: // B2
moel@49
   163
                    switch (coreCount) {
moel@49
   164
                      case 2:
moel@69
   165
                        tjMax = Floats(80 + 10); break;
moel@49
   166
                      case 4:
moel@69
   167
                        tjMax = Floats(90 + 10); break;
moel@49
   168
                      default:
moel@69
   169
                        tjMax = Floats(85 + 10); break;
moel@49
   170
                    }
moel@69
   171
                    tjMax = Floats(80 + 10); break;
moel@49
   172
                  case 0x0B: // G0
moel@69
   173
                    tjMax = Floats(90 + 10); break;
moel@49
   174
                  case 0x0D: // M0
moel@69
   175
                    tjMax = Floats(85 + 10); break;
moel@49
   176
                  default:
moel@69
   177
                    tjMax = Floats(85 + 10); break;
moel@49
   178
                } break;
moel@49
   179
              case 0x17: // Intel Core (45nm)
moel@69
   180
                tjMax = Floats(100); break;
moel@49
   181
              case 0x1C: // Intel Atom 
moel@69
   182
                tjMax = Floats(90); break;
moel@49
   183
              case 0x1A: // Intel Core i7 LGA1366 (45nm)
moel@49
   184
              case 0x1E: // Intel Core i5, i7 LGA1156 (45nm)
moel@49
   185
              case 0x25: // Intel Core i3, i5, i7 LGA1156 (32nm)
moel@49
   186
                uint eax, edx;
moel@69
   187
                tjMax = new float[coreCount];
moel@69
   188
                for (int i = 0; i < coreCount; i++) {
moel@69
   189
                  if (WinRing0.RdmsrTx(IA32_TEMPERATURE_TARGET, out eax,
moel@69
   190
                    out edx, (UIntPtr)(
moel@69
   191
                    1 << (int)(logicalProcessorsPerCore * i)))) 
moel@69
   192
                  {
moel@69
   193
                    tjMax[i] = (eax >> 16) & 0xFF;
moel@69
   194
                  } else {
moel@69
   195
                    tjMax[i] = 100;
moel@69
   196
                  }
moel@49
   197
                }
moel@49
   198
                if (WinRing0.Rdmsr(MSR_PLATFORM_INFO, out eax, out edx)) {
moel@49
   199
                  maxNehalemMultiplier = (eax >> 8) & 0xff;
moel@49
   200
                }
moel@49
   201
                break;
moel@49
   202
              default:
moel@69
   203
                tjMax = Floats(100); break;
moel@49
   204
            }
moel@49
   205
          } break;
moel@69
   206
        default: tjMax = Floats(100); break;
moel@49
   207
      }
moel@1
   208
moel@44
   209
      // check if processor supports a digital thermal sensor
moel@44
   210
      if (cpuidData.GetLength(0) > 6 && (cpuidData[6, 0] & 1) != 0) {
moel@44
   211
        coreTemperatures = new Sensor[coreCount];
moel@44
   212
        for (int i = 0; i < coreTemperatures.Length; i++) {
moel@69
   213
          coreTemperatures[i] = new Sensor(CoreString(i), i, tjMax[i],
moel@63
   214
            SensorType.Temperature, this, new ParameterDescription[] { 
moel@63
   215
              new ParameterDescription(
moel@63
   216
                "TjMax", "TjMax temperature of the core.\n" + 
moel@69
   217
                "Temperature = TjMax - TSlope * Value.", tjMax[i]), 
moel@63
   218
              new ParameterDescription(
moel@63
   219
                "TSlope", "Temperature slope of the digital thermal sensor.\n" + 
moel@63
   220
                "Temperature = TjMax - TSlope * Value.", 1)});
moel@44
   221
        }
moel@44
   222
      } else {
moel@44
   223
        coreTemperatures = new Sensor[0];
moel@1
   224
      }
moel@49
   225
moel@49
   226
      if (coreCount > 1)
moel@49
   227
        totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);
moel@49
   228
      else
moel@49
   229
        totalLoad = null;
moel@24
   230
      coreLoads = new Sensor[coreCount];
moel@49
   231
      for (int i = 0; i < coreLoads.Length; i++)
moel@49
   232
        coreLoads[i] = new Sensor(CoreString(i), i + 1,
moel@44
   233
          SensorType.Load, this);     
moel@26
   234
      cpuLoad = new CPULoad(coreCount, logicalProcessorsPerCore);
moel@26
   235
      if (cpuLoad.IsAvailable) {
moel@26
   236
        foreach (Sensor sensor in coreLoads)
moel@26
   237
          ActivateSensor(sensor);
moel@49
   238
        if (totalLoad != null)
moel@49
   239
          ActivateSensor(totalLoad);
moel@26
   240
      }
moel@26
   241
moel@44
   242
      lastCount = 0;
moel@44
   243
      lastTime = 0;
moel@44
   244
      busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this);      
moel@44
   245
      coreClocks = new Sensor[coreCount];
moel@44
   246
      for (int i = 0; i < coreClocks.Length; i++) {
moel@49
   247
        coreClocks[i] =
moel@49
   248
          new Sensor(CoreString(i), i + 1, SensorType.Clock, this);
moel@44
   249
        ActivateSensor(coreClocks[i]);
moel@44
   250
      }
moel@44
   251
      
moel@1
   252
      Update();                   
moel@1
   253
    }
moel@1
   254
moel@1
   255
    public string Name {
moel@1
   256
      get { return name; }
moel@1
   257
    }
moel@1
   258
moel@1
   259
    public string Identifier {
moel@1
   260
      get { return "/intelcpu/0"; }
moel@1
   261
    }
moel@1
   262
moel@1
   263
    public Image Icon {
moel@1
   264
      get { return icon; }
moel@1
   265
    }
moel@1
   266
moel@49
   267
    private void AppendMSRData(StringBuilder r, uint msr, int core) {
moel@49
   268
      uint eax, edx;
moel@49
   269
      if (WinRing0.RdmsrTx(msr, out eax, out edx,
moel@49
   270
         (UIntPtr)(1 << (int)(logicalProcessorsPerCore * core)))) {
moel@49
   271
        r.Append(" ");
moel@49
   272
        r.Append((msr).ToString("X8"));
moel@49
   273
        r.Append("  ");
moel@49
   274
        r.Append((edx).ToString("X8"));
moel@49
   275
        r.Append("  ");
moel@49
   276
        r.Append((eax).ToString("X8"));
moel@49
   277
        r.AppendLine();
moel@49
   278
      }
moel@49
   279
    }
moel@49
   280
moel@1
   281
    public string GetReport() {
moel@5
   282
      StringBuilder r = new StringBuilder();
moel@5
   283
moel@5
   284
      r.AppendLine("Intel CPU");
moel@5
   285
      r.AppendLine();
moel@5
   286
      r.AppendFormat("Name: {0}{1}", name, Environment.NewLine);
moel@63
   287
      r.AppendFormat("Number of Cores: {0}{1}", coreCount, 
moel@22
   288
        Environment.NewLine);
moel@63
   289
      r.AppendFormat("Threads per Core: {0}{1}", logicalProcessorsPerCore,
moel@5
   290
        Environment.NewLine);
moel@63
   291
      r.AppendFormat("Affinity Mask: 0x{0}{1}", affinityMask.ToString("X"),
moel@63
   292
        Environment.NewLine);  
moel@5
   293
      r.AppendLine();
moel@5
   294
moel@49
   295
      for (int i = 0; i < coreCount; i++) {
moel@49
   296
        r.AppendLine("MSR Core #" + (i + 1));
moel@49
   297
        r.AppendLine();
moel@49
   298
        r.AppendLine(" MSR       EDX       EAX");
moel@49
   299
        AppendMSRData(r, MSR_PLATFORM_INFO, i);
moel@49
   300
        AppendMSRData(r, IA32_PERF_STATUS, i);
moel@49
   301
        AppendMSRData(r, IA32_THERM_STATUS_MSR, i);
moel@49
   302
        AppendMSRData(r, IA32_TEMPERATURE_TARGET, i);
moel@49
   303
        r.AppendLine();
moel@49
   304
      }
moel@49
   305
moel@5
   306
      return r.ToString();
moel@1
   307
    }
moel@1
   308
moel@1
   309
    public void Update() {
moel@44
   310
            
moel@1
   311
      for (int i = 0; i < coreTemperatures.Length; i++) {
moel@46
   312
        uint eax, edx;
moel@46
   313
        if (WinRing0.RdmsrTx(
moel@46
   314
          IA32_THERM_STATUS_MSR, out eax, out edx, 
moel@13
   315
            (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) 
moel@1
   316
        {
moel@1
   317
          // if reading is valid
moel@1
   318
          if ((eax & 0x80000000) != 0) {
moel@1
   319
            // get the dist from tjMax from bits 22:16
moel@63
   320
            float deltaT = ((eax & 0x007F0000) >> 16);
moel@63
   321
            float tjMax = coreTemperatures[i].Parameters[0].Value;
moel@63
   322
            float tSlope = coreTemperatures[i].Parameters[1].Value;
moel@63
   323
            coreTemperatures[i].Value = tjMax - tSlope * deltaT;
moel@24
   324
            ActivateSensor(coreTemperatures[i]);
moel@24
   325
          } else {
moel@24
   326
            DeactivateSensor(coreTemperatures[i]);
moel@1
   327
          }
moel@1
   328
        }        
moel@24
   329
      }
moel@24
   330
moel@26
   331
      if (cpuLoad.IsAvailable) {
moel@26
   332
        cpuLoad.Update();
moel@26
   333
        for (int i = 0; i < coreLoads.Length; i++)
moel@26
   334
          coreLoads[i].Value = cpuLoad.GetCoreLoad(i);
moel@49
   335
        if (totalLoad != null)
moel@49
   336
          totalLoad.Value = cpuLoad.GetTotalLoad();
moel@24
   337
      }
moel@44
   338
     
moel@46
   339
      uint lsb, msb;
moel@46
   340
      bool valid = WinRing0.RdtscTx(out lsb, out msb, (UIntPtr)1);
moel@44
   341
      long time = Stopwatch.GetTimestamp();
moel@44
   342
      ulong count = ((ulong)msb << 32) | lsb;
moel@44
   343
      double delta = ((double)(time - lastTime)) / Stopwatch.Frequency;
moel@44
   344
      if (valid && delta > 0.5) {
moel@46
   345
        double maxClock = (count - lastCount) / (1e6 * delta);
moel@44
   346
        double busClock = 0;
moel@46
   347
        uint eax, edx;
moel@44
   348
        for (int i = 0; i < coreClocks.Length; i++) {
moel@44
   349
          System.Threading.Thread.Sleep(1);
moel@46
   350
          if (WinRing0.RdmsrTx(IA32_PERF_STATUS, out eax, out edx,
moel@44
   351
            (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) {
moel@49
   352
            if (maxNehalemMultiplier > 0) { // Core i3, i5, i7
moel@49
   353
              uint nehalemMultiplier = eax & 0xff;
moel@49
   354
              coreClocks[i].Value =
moel@49
   355
                (float)(nehalemMultiplier * maxClock / maxNehalemMultiplier);
moel@49
   356
              busClock = (float)(maxClock / maxNehalemMultiplier);
moel@49
   357
            } else { // Core 2
moel@46
   358
              uint multiplier = (eax >> 8) & 0x1f;
moel@46
   359
              uint maxMultiplier = (edx >> 8) & 0x1f;
moel@46
   360
              // factor = multiplier * 2 to handle non integer multipliers 
moel@46
   361
              uint factor = (multiplier << 1) | ((eax >> 14) & 1);
moel@46
   362
              uint maxFactor = (maxMultiplier << 1) | ((edx >> 14) & 1);
moel@46
   363
              if (maxFactor > 0) {
moel@46
   364
                coreClocks[i].Value = (float)(factor * maxClock / maxFactor);
moel@46
   365
                busClock = (float)(2 * maxClock / maxFactor);
moel@46
   366
              }
moel@49
   367
            }  
moel@46
   368
          } else { // Intel Pentium 4
moel@44
   369
            // if IA32_PERF_STATUS is not available, assume maxClock
moel@44
   370
            coreClocks[i].Value = (float)maxClock;
moel@46
   371
          }
moel@44
   372
        }
moel@44
   373
        if (busClock > 0) {
moel@44
   374
          this.busClock.Value = (float)busClock;
moel@44
   375
          ActivateSensor(this.busClock);
moel@44
   376
        }
moel@44
   377
      }
moel@44
   378
      lastCount = count;
moel@44
   379
      lastTime = time;
moel@46
   380
    }
moel@46
   381
  }  
moel@1
   382
}