Hardware/CPU/IntelCPU.cs
author moel.mich
Tue, 16 Sep 2014 20:35:33 +0000
changeset 424 427fc32e589f
parent 418 3bba187d41c2
child 425 520bca5f2a7d
permissions -rw-r--r--
Added support for Intel Xeon E5-2600/1600 v3, Core i7-59xx CPUs (Fixed Issue 617). Corrected an error in the Intel CPU power calculation (wrong bit mask for "Energy Status Units: bits 12:8").
moel@1
     1
/*
moel@1
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@1
     6
 
moel@424
     7
  Copyright (C) 2009-2014 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@1
     9
*/
moel@1
    10
moel@1
    11
using System;
moel@219
    12
using System.Globalization;
moel@219
    13
using System.Text;
moel@1
    14
moel@1
    15
namespace OpenHardwareMonitor.Hardware.CPU {
moel@191
    16
  internal sealed class IntelCPU : GenericCPU {
moel@46
    17
moel@219
    18
    private enum Microarchitecture {
moel@219
    19
      Unknown,
moel@264
    20
      NetBurst,
moel@219
    21
      Core,
moel@219
    22
      Atom,
moel@249
    23
      Nehalem,
moel@350
    24
      SandyBridge,
moel@396
    25
      IvyBridge,
moel@396
    26
      Haswell
moel@219
    27
    }
moel@219
    28
moel@195
    29
    private readonly Sensor[] coreTemperatures;
moel@306
    30
    private readonly Sensor packageTemperature;
moel@195
    31
    private readonly Sensor[] coreClocks;
moel@195
    32
    private readonly Sensor busClock;
moel@321
    33
    private readonly Sensor[] powerSensors;
moel@63
    34
moel@219
    35
    private readonly Microarchitecture microarchitecture;
moel@219
    36
    private readonly double timeStampCounterMultiplier;
moel@79
    37
moel@1
    38
    private const uint IA32_THERM_STATUS_MSR = 0x019C;
moel@4
    39
    private const uint IA32_TEMPERATURE_TARGET = 0x01A2;
moel@44
    40
    private const uint IA32_PERF_STATUS = 0x0198;
moel@46
    41
    private const uint MSR_PLATFORM_INFO = 0xCE;
moel@306
    42
    private const uint IA32_PACKAGE_THERM_STATUS = 0x1B1;
moel@317
    43
    private const uint MSR_RAPL_POWER_UNIT = 0x606;
moel@317
    44
    private const uint MSR_PKG_ENERY_STATUS = 0x611;
moel@321
    45
    private const uint MSR_DRAM_ENERGY_STATUS = 0x619;
moel@317
    46
    private const uint MSR_PP0_ENERY_STATUS = 0x639;
moel@320
    47
    private const uint MSR_PP1_ENERY_STATUS = 0x641;
moel@317
    48
moel@321
    49
    private readonly uint[] energyStatusMSRs = { MSR_PKG_ENERY_STATUS, 
moel@321
    50
      MSR_PP0_ENERY_STATUS, MSR_PP1_ENERY_STATUS, MSR_DRAM_ENERGY_STATUS };
moel@321
    51
    private readonly string[] powerSensorLabels = 
moel@321
    52
      { "CPU Package", "CPU Cores", "CPU Graphics", "CPU DRAM" };
moel@317
    53
    private float energyUnitMultiplier = 0;
moel@321
    54
    private DateTime[] lastEnergyTime;
moel@321
    55
    private uint[] lastEnergyConsumed;
moel@317
    56
moel@1
    57
moel@69
    58
    private float[] Floats(float f) {
moel@69
    59
      float[] result = new float[coreCount];
moel@69
    60
      for (int i = 0; i < coreCount; i++)
moel@69
    61
        result[i] = f;
moel@69
    62
      return result;
moel@69
    63
    }
moel@69
    64
moel@249
    65
    private float[] GetTjMaxFromMSR() {
moel@249
    66
      uint eax, edx;
moel@249
    67
      float[] result = new float[coreCount];
moel@249
    68
      for (int i = 0; i < coreCount; i++) {
moel@249
    69
        if (Ring0.RdmsrTx(IA32_TEMPERATURE_TARGET, out eax,
moel@249
    70
          out edx, 1UL << cpuid[i][0].Thread)) {
moel@249
    71
          result[i] = (eax >> 16) & 0xFF;
moel@249
    72
        } else {
moel@249
    73
          result[i] = 100;
moel@249
    74
        }
moel@249
    75
      }
moel@249
    76
      return result;
moel@249
    77
    }
moel@249
    78
moel@191
    79
    public IntelCPU(int processorIndex, CPUID[][] cpuid, ISettings settings)
moel@321
    80
      : base(processorIndex, cpuid, settings) {
moel@219
    81
      // set tjMax
moel@69
    82
      float[] tjMax;
moel@49
    83
      switch (family) {
moel@49
    84
        case 0x06: {
moel@49
    85
            switch (model) {
moel@219
    86
              case 0x0F: // Intel Core 2 (65nm)
moel@219
    87
                microarchitecture = Microarchitecture.Core;
moel@49
    88
                switch (stepping) {
moel@49
    89
                  case 0x06: // B2
moel@49
    90
                    switch (coreCount) {
moel@49
    91
                      case 2:
moel@69
    92
                        tjMax = Floats(80 + 10); break;
moel@49
    93
                      case 4:
moel@69
    94
                        tjMax = Floats(90 + 10); break;
moel@49
    95
                      default:
moel@69
    96
                        tjMax = Floats(85 + 10); break;
moel@49
    97
                    }
moel@69
    98
                    tjMax = Floats(80 + 10); break;
moel@49
    99
                  case 0x0B: // G0
moel@69
   100
                    tjMax = Floats(90 + 10); break;
moel@49
   101
                  case 0x0D: // M0
moel@69
   102
                    tjMax = Floats(85 + 10); break;
moel@49
   103
                  default:
moel@69
   104
                    tjMax = Floats(85 + 10); break;
moel@49
   105
                } break;
moel@219
   106
              case 0x17: // Intel Core 2 (45nm)
moel@219
   107
                microarchitecture = Microarchitecture.Core;
moel@69
   108
                tjMax = Floats(100); break;
moel@114
   109
              case 0x1C: // Intel Atom (45nm)
moel@219
   110
                microarchitecture = Microarchitecture.Atom;
moel@114
   111
                switch (stepping) {
moel@114
   112
                  case 0x02: // C0
moel@114
   113
                    tjMax = Floats(90); break;
moel@114
   114
                  case 0x0A: // A0, B0
moel@114
   115
                    tjMax = Floats(100); break;
moel@114
   116
                  default:
moel@114
   117
                    tjMax = Floats(90); break;
moel@191
   118
                } break;
moel@49
   119
              case 0x1A: // Intel Core i7 LGA1366 (45nm)
moel@49
   120
              case 0x1E: // Intel Core i5, i7 LGA1156 (45nm)
moel@249
   121
              case 0x1F: // Intel Core i5, i7 
moel@49
   122
              case 0x25: // Intel Core i3, i5, i7 LGA1156 (32nm)
moel@91
   123
              case 0x2C: // Intel Core i7 LGA1366 (32nm) 6 Core
moel@350
   124
              case 0x2E: // Intel Xeon Processor 7500 series (45nm)
moel@350
   125
              case 0x2F: // Intel Xeon Processor (32nm)
moel@219
   126
                microarchitecture = Microarchitecture.Nehalem;
moel@249
   127
                tjMax = GetTjMaxFromMSR();
moel@249
   128
                break;
moel@249
   129
              case 0x2A: // Intel Core i5, i7 2xxx LGA1155 (32nm)
moel@350
   130
              case 0x2D: // Next Generation Intel Xeon, i7 3xxx LGA2011 (32nm)
moel@249
   131
                microarchitecture = Microarchitecture.SandyBridge;
moel@249
   132
                tjMax = GetTjMaxFromMSR();
moel@49
   133
                break;
moel@350
   134
              case 0x3A: // Intel Core i5, i7 3xxx LGA1155 (22nm)
moel@417
   135
              case 0x3E: // Intel Core i7 4xxx LGA2011 (22nm)
moel@350
   136
                microarchitecture = Microarchitecture.IvyBridge;
moel@350
   137
                tjMax = GetTjMaxFromMSR();
moel@350
   138
                break;
moel@396
   139
              case 0x3C: // Intel Core i5, i7 4xxx LGA1150 (22nm)
moel@424
   140
              case 0x3F: // Intel Xeon E5-2600/1600 v3, Core i7-59xx
moel@424
   141
                         // LGA2011-v3, Haswell-E (22nm)
moel@396
   142
              case 0x45:
moel@424
   143
              case 0x46: 
moel@396
   144
                microarchitecture = Microarchitecture.Haswell;
moel@396
   145
                tjMax = GetTjMaxFromMSR();
moel@396
   146
                break;
moel@49
   147
              default:
moel@219
   148
                microarchitecture = Microarchitecture.Unknown;
moel@321
   149
                tjMax = Floats(100);
moel@219
   150
                break;
moel@49
   151
            }
moel@49
   152
          } break;
moel@264
   153
        case 0x0F: {
moel@264
   154
            switch (model) {
moel@264
   155
              case 0x00: // Pentium 4 (180nm)
moel@264
   156
              case 0x01: // Pentium 4 (130nm)
moel@264
   157
              case 0x02: // Pentium 4 (130nm)
moel@264
   158
              case 0x03: // Pentium 4, Celeron D (90nm)
moel@264
   159
              case 0x04: // Pentium 4, Pentium D, Celeron D (90nm)
moel@264
   160
              case 0x06: // Pentium 4, Pentium D, Celeron D (65nm)
moel@264
   161
                microarchitecture = Microarchitecture.NetBurst;
moel@321
   162
                tjMax = Floats(100);
moel@264
   163
                break;
moel@264
   164
              default:
moel@264
   165
                microarchitecture = Microarchitecture.Unknown;
moel@264
   166
                tjMax = Floats(100);
moel@264
   167
                break;
moel@264
   168
            }
moel@264
   169
          } break;
moel@219
   170
        default:
moel@219
   171
          microarchitecture = Microarchitecture.Unknown;
moel@321
   172
          tjMax = Floats(100);
moel@219
   173
          break;
moel@219
   174
      }
moel@219
   175
moel@219
   176
      // set timeStampCounterMultiplier
moel@219
   177
      switch (microarchitecture) {
moel@264
   178
        case Microarchitecture.NetBurst:
moel@219
   179
        case Microarchitecture.Atom:
moel@219
   180
        case Microarchitecture.Core: {
moel@219
   181
            uint eax, edx;
moel@236
   182
            if (Ring0.Rdmsr(IA32_PERF_STATUS, out eax, out edx)) {
moel@321
   183
              timeStampCounterMultiplier =
moel@219
   184
                ((edx >> 8) & 0x1f) + 0.5 * ((edx >> 14) & 1);
moel@219
   185
            }
moel@219
   186
          } break;
moel@321
   187
        case Microarchitecture.Nehalem:
moel@350
   188
        case Microarchitecture.SandyBridge:
moel@396
   189
        case Microarchitecture.IvyBridge:
moel@396
   190
        case Microarchitecture.Haswell: {
moel@219
   191
            uint eax, edx;
moel@236
   192
            if (Ring0.Rdmsr(MSR_PLATFORM_INFO, out eax, out edx)) {
moel@219
   193
              timeStampCounterMultiplier = (eax >> 8) & 0xff;
moel@219
   194
            }
moel@219
   195
          } break;
moel@350
   196
        default: 
moel@350
   197
          timeStampCounterMultiplier = 0;
moel@350
   198
          break;
moel@49
   199
      }
moel@1
   200
moel@306
   201
      // check if processor supports a digital thermal sensor at core level
moel@191
   202
      if (cpuid[0][0].Data.GetLength(0) > 6 &&
moel@350
   203
        (cpuid[0][0].Data[6, 0] & 1) != 0 && 
moel@350
   204
        microarchitecture != Microarchitecture.Unknown) 
moel@350
   205
      {
moel@44
   206
        coreTemperatures = new Sensor[coreCount];
moel@44
   207
        for (int i = 0; i < coreTemperatures.Length; i++) {
moel@134
   208
          coreTemperatures[i] = new Sensor(CoreString(i), i,
moel@321
   209
            SensorType.Temperature, this, new[] { 
moel@63
   210
              new ParameterDescription(
moel@306
   211
                "TjMax [°C]", "TjMax temperature of the core sensor.\n" + 
moel@69
   212
                "Temperature = TjMax - TSlope * Value.", tjMax[i]), 
moel@122
   213
              new ParameterDescription("TSlope [°C]", 
moel@122
   214
                "Temperature slope of the digital thermal sensor.\n" + 
moel@165
   215
                "Temperature = TjMax - TSlope * Value.", 1)}, settings);
moel@155
   216
          ActivateSensor(coreTemperatures[i]);
moel@44
   217
        }
moel@44
   218
      } else {
moel@44
   219
        coreTemperatures = new Sensor[0];
moel@1
   220
      }
moel@49
   221
moel@306
   222
      // check if processor supports a digital thermal sensor at package level
moel@306
   223
      if (cpuid[0][0].Data.GetLength(0) > 6 &&
moel@350
   224
        (cpuid[0][0].Data[6, 0] & 0x40) != 0 && 
moel@350
   225
        microarchitecture != Microarchitecture.Unknown) 
moel@350
   226
      {
moel@321
   227
        packageTemperature = new Sensor("CPU Package",
moel@321
   228
          coreTemperatures.Length, SensorType.Temperature, this, new[] { 
moel@306
   229
              new ParameterDescription(
moel@306
   230
                "TjMax [°C]", "TjMax temperature of the package sensor.\n" + 
moel@306
   231
                "Temperature = TjMax - TSlope * Value.", tjMax[0]), 
moel@306
   232
              new ParameterDescription("TSlope [°C]", 
moel@306
   233
                "Temperature slope of the digital thermal sensor.\n" + 
moel@306
   234
                "Temperature = TjMax - TSlope * Value.", 1)}, settings);
moel@306
   235
        ActivateSensor(packageTemperature);
moel@321
   236
      }
moel@306
   237
moel@191
   238
      busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this, settings);
moel@44
   239
      coreClocks = new Sensor[coreCount];
moel@44
   240
      for (int i = 0; i < coreClocks.Length; i++) {
moel@49
   241
        coreClocks[i] =
moel@165
   242
          new Sensor(CoreString(i), i + 1, SensorType.Clock, this, settings);
moel@350
   243
        if (HasTimeStampCounter && microarchitecture != Microarchitecture.Unknown)
moel@79
   244
          ActivateSensor(coreClocks[i]);
moel@44
   245
      }
moel@191
   246
moel@350
   247
      if (microarchitecture == Microarchitecture.SandyBridge ||
moel@396
   248
          microarchitecture == Microarchitecture.IvyBridge ||
moel@396
   249
          microarchitecture == Microarchitecture.Haswell) 
moel@350
   250
      {
moel@321
   251
        powerSensors = new Sensor[energyStatusMSRs.Length];
moel@321
   252
        lastEnergyTime = new DateTime[energyStatusMSRs.Length];
moel@321
   253
        lastEnergyConsumed = new uint[energyStatusMSRs.Length];
moel@321
   254
moel@317
   255
        uint eax, edx;
moel@317
   256
        if (Ring0.Rdmsr(MSR_RAPL_POWER_UNIT, out eax, out edx))
moel@424
   257
          energyUnitMultiplier = 1.0f / (1 << (int)((eax >> 8) & 0x1F));
moel@317
   258
moel@321
   259
        if (energyUnitMultiplier != 0) {
moel@321
   260
          for (int i = 0; i < energyStatusMSRs.Length; i++) {
moel@321
   261
            if (!Ring0.Rdmsr(energyStatusMSRs[i], out eax, out edx))
moel@321
   262
              continue;
moel@317
   263
moel@321
   264
            lastEnergyTime[i] = DateTime.UtcNow;
moel@321
   265
            lastEnergyConsumed[i] = eax;
moel@321
   266
            powerSensors[i] = new Sensor(powerSensorLabels[i], i,
moel@321
   267
              SensorType.Power, this, settings);
moel@321
   268
            ActivateSensor(powerSensors[i]);
moel@321
   269
          }
moel@317
   270
        }
moel@317
   271
      }
moel@317
   272
moel@191
   273
      Update();
moel@1
   274
    }
moel@1
   275
moel@191
   276
    protected override uint[] GetMSRs() {
moel@321
   277
      return new[] {
moel@191
   278
        MSR_PLATFORM_INFO,
moel@191
   279
        IA32_PERF_STATUS ,
moel@191
   280
        IA32_THERM_STATUS_MSR,
moel@306
   281
        IA32_TEMPERATURE_TARGET,
moel@317
   282
        IA32_PACKAGE_THERM_STATUS,
moel@317
   283
        MSR_RAPL_POWER_UNIT,
moel@317
   284
        MSR_PKG_ENERY_STATUS,
moel@321
   285
        MSR_DRAM_ENERGY_STATUS,
moel@320
   286
        MSR_PP0_ENERY_STATUS,
moel@320
   287
        MSR_PP1_ENERY_STATUS
moel@191
   288
      };
moel@1
   289
    }
moel@1
   290
moel@219
   291
    public override string GetReport() {
moel@219
   292
      StringBuilder r = new StringBuilder();
moel@219
   293
      r.Append(base.GetReport());
moel@219
   294
moel@264
   295
      r.Append("Microarchitecture: ");
moel@264
   296
      r.AppendLine(microarchitecture.ToString());
moel@219
   297
      r.Append("Time Stamp Counter Multiplier: ");
moel@219
   298
      r.AppendLine(timeStampCounterMultiplier.ToString(
moel@219
   299
        CultureInfo.InvariantCulture));
moel@219
   300
      r.AppendLine();
moel@219
   301
moel@219
   302
      return r.ToString();
moel@219
   303
    }
moel@219
   304
moel@191
   305
    public override void Update() {
moel@191
   306
      base.Update();
moel@1
   307
moel@1
   308
      for (int i = 0; i < coreTemperatures.Length; i++) {
moel@46
   309
        uint eax, edx;
moel@418
   310
        // if reading is valid
moel@418
   311
        if (Ring0.RdmsrTx(IA32_THERM_STATUS_MSR, out eax, out edx,
moel@418
   312
            1UL << cpuid[i][0].Thread) && (eax & 0x80000000) != 0) 
moel@418
   313
        {
moel@418
   314
          // get the dist from tjMax from bits 22:16
moel@418
   315
          float deltaT = ((eax & 0x007F0000) >> 16);
moel@418
   316
          float tjMax = coreTemperatures[i].Parameters[0].Value;
moel@418
   317
          float tSlope = coreTemperatures[i].Parameters[1].Value;
moel@418
   318
          coreTemperatures[i].Value = tjMax - tSlope * deltaT;
moel@418
   319
        } else {
moel@418
   320
          coreTemperatures[i].Value = null;
moel@79
   321
        }
moel@24
   322
      }
moel@24
   323
moel@306
   324
      if (packageTemperature != null) {
moel@306
   325
        uint eax, edx;
moel@418
   326
        // if reading is valid
moel@418
   327
        if (Ring0.RdmsrTx(IA32_PACKAGE_THERM_STATUS, out eax, out edx,
moel@418
   328
            1UL << cpuid[0][0].Thread) && (eax & 0x80000000) != 0) 
moel@418
   329
        {
moel@306
   330
          // get the dist from tjMax from bits 22:16
moel@306
   331
          float deltaT = ((eax & 0x007F0000) >> 16);
moel@306
   332
          float tjMax = packageTemperature.Parameters[0].Value;
moel@306
   333
          float tSlope = packageTemperature.Parameters[1].Value;
moel@306
   334
          packageTemperature.Value = tjMax - tSlope * deltaT;
moel@306
   335
        } else {
moel@306
   336
          packageTemperature.Value = null;
moel@306
   337
        }
moel@306
   338
      }
moel@306
   339
moel@350
   340
      if (HasTimeStampCounter && timeStampCounterMultiplier > 0) {
moel@191
   341
        double newBusClock = 0;
moel@191
   342
        uint eax, edx;
moel@191
   343
        for (int i = 0; i < coreClocks.Length; i++) {
moel@191
   344
          System.Threading.Thread.Sleep(1);
moel@236
   345
          if (Ring0.RdmsrTx(IA32_PERF_STATUS, out eax, out edx,
moel@321
   346
            1UL << cpuid[i][0].Thread)) {
moel@321
   347
            newBusClock =
moel@219
   348
              TimeStampCounterFrequency / timeStampCounterMultiplier;
moel@250
   349
            switch (microarchitecture) {
moel@250
   350
              case Microarchitecture.Nehalem: {
moel@250
   351
                  uint multiplier = eax & 0xff;
moel@250
   352
                  coreClocks[i].Value = (float)(multiplier * newBusClock);
moel@250
   353
                } break;
moel@350
   354
              case Microarchitecture.SandyBridge:
moel@396
   355
              case Microarchitecture.IvyBridge:
moel@396
   356
              case Microarchitecture.Haswell: {
moel@250
   357
                  uint multiplier = (eax >> 8) & 0xff;
moel@250
   358
                  coreClocks[i].Value = (float)(multiplier * newBusClock);
moel@250
   359
                } break;
moel@250
   360
              default: {
moel@321
   361
                  double multiplier =
moel@250
   362
                    ((eax >> 8) & 0x1f) + 0.5 * ((eax >> 14) & 1);
moel@250
   363
                  coreClocks[i].Value = (float)(multiplier * newBusClock);
moel@250
   364
                } break;
moel@321
   365
            }
moel@321
   366
          } else {
moel@201
   367
            // if IA32_PERF_STATUS is not available, assume TSC frequency
moel@201
   368
            coreClocks[i].Value = (float)TimeStampCounterFrequency;
moel@46
   369
          }
moel@44
   370
        }
moel@191
   371
        if (newBusClock > 0) {
moel@191
   372
          this.busClock.Value = (float)newBusClock;
moel@191
   373
          ActivateSensor(this.busClock);
moel@191
   374
        }
moel@44
   375
      }
moel@317
   376
moel@321
   377
      if (powerSensors != null) {
moel@321
   378
        foreach (Sensor sensor in powerSensors) {
moel@321
   379
          if (sensor == null)
moel@321
   380
            continue;
moel@317
   381
moel@321
   382
          uint eax, edx;
moel@321
   383
          if (!Ring0.Rdmsr(energyStatusMSRs[sensor.Index], out eax, out edx))
moel@321
   384
            continue;
moel@317
   385
moel@317
   386
          DateTime time = DateTime.UtcNow;
moel@317
   387
          uint energyConsumed = eax;
moel@321
   388
          float deltaTime =
moel@321
   389
            (float)(time - lastEnergyTime[sensor.Index]).TotalSeconds;
moel@321
   390
          if (deltaTime < 0.01)
moel@321
   391
            continue;
moel@321
   392
moel@321
   393
          sensor.Value = energyUnitMultiplier * unchecked(
moel@321
   394
            energyConsumed - lastEnergyConsumed[sensor.Index]) / deltaTime;
moel@321
   395
          lastEnergyTime[sensor.Index] = time;
moel@321
   396
          lastEnergyConsumed[sensor.Index] = energyConsumed;
moel@317
   397
        }
moel@317
   398
      }
moel@46
   399
    }
moel@191
   400
  }
moel@1
   401
}