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