moel@1: /* moel@1: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@1: moel@424: Copyright (C) 2009-2014 Michael Möller moel@344: moel@1: */ moel@1: moel@1: using System; moel@219: using System.Globalization; moel@219: using System.Text; moel@1: moel@1: namespace OpenHardwareMonitor.Hardware.CPU { moel@191: internal sealed class IntelCPU : GenericCPU { moel@46: moel@219: private enum Microarchitecture { moel@219: Unknown, moel@264: NetBurst, moel@219: Core, moel@219: Atom, moel@249: Nehalem, moel@350: SandyBridge, moel@396: IvyBridge, moel@425: Haswell, moel@425: Broadwell moel@219: } moel@219: moel@195: private readonly Sensor[] coreTemperatures; moel@306: private readonly Sensor packageTemperature; moel@195: private readonly Sensor[] coreClocks; moel@195: private readonly Sensor busClock; moel@321: private readonly Sensor[] powerSensors; moel@63: moel@219: private readonly Microarchitecture microarchitecture; moel@219: private readonly double timeStampCounterMultiplier; moel@79: moel@1: private const uint IA32_THERM_STATUS_MSR = 0x019C; moel@4: private const uint IA32_TEMPERATURE_TARGET = 0x01A2; moel@44: private const uint IA32_PERF_STATUS = 0x0198; moel@46: private const uint MSR_PLATFORM_INFO = 0xCE; moel@306: private const uint IA32_PACKAGE_THERM_STATUS = 0x1B1; moel@317: private const uint MSR_RAPL_POWER_UNIT = 0x606; moel@317: private const uint MSR_PKG_ENERY_STATUS = 0x611; moel@321: private const uint MSR_DRAM_ENERGY_STATUS = 0x619; moel@317: private const uint MSR_PP0_ENERY_STATUS = 0x639; moel@320: private const uint MSR_PP1_ENERY_STATUS = 0x641; moel@317: moel@321: private readonly uint[] energyStatusMSRs = { MSR_PKG_ENERY_STATUS, moel@321: MSR_PP0_ENERY_STATUS, MSR_PP1_ENERY_STATUS, MSR_DRAM_ENERGY_STATUS }; moel@321: private readonly string[] powerSensorLabels = moel@321: { "CPU Package", "CPU Cores", "CPU Graphics", "CPU DRAM" }; moel@317: private float energyUnitMultiplier = 0; moel@321: private DateTime[] lastEnergyTime; moel@321: private uint[] lastEnergyConsumed; moel@317: moel@1: moel@69: private float[] Floats(float f) { moel@69: float[] result = new float[coreCount]; moel@69: for (int i = 0; i < coreCount; i++) moel@69: result[i] = f; moel@69: return result; moel@69: } moel@69: moel@249: private float[] GetTjMaxFromMSR() { moel@249: uint eax, edx; moel@249: float[] result = new float[coreCount]; moel@249: for (int i = 0; i < coreCount; i++) { moel@249: if (Ring0.RdmsrTx(IA32_TEMPERATURE_TARGET, out eax, moel@249: out edx, 1UL << cpuid[i][0].Thread)) { moel@249: result[i] = (eax >> 16) & 0xFF; moel@249: } else { moel@249: result[i] = 100; moel@249: } moel@249: } moel@249: return result; moel@249: } moel@249: moel@191: public IntelCPU(int processorIndex, CPUID[][] cpuid, ISettings settings) moel@321: : base(processorIndex, cpuid, settings) { moel@219: // set tjMax moel@69: float[] tjMax; moel@49: switch (family) { moel@49: case 0x06: { moel@49: switch (model) { moel@219: case 0x0F: // Intel Core 2 (65nm) moel@219: microarchitecture = Microarchitecture.Core; moel@49: switch (stepping) { moel@49: case 0x06: // B2 moel@49: switch (coreCount) { moel@49: case 2: moel@69: tjMax = Floats(80 + 10); break; moel@49: case 4: moel@69: tjMax = Floats(90 + 10); break; moel@49: default: moel@69: tjMax = Floats(85 + 10); break; moel@49: } moel@69: tjMax = Floats(80 + 10); break; moel@49: case 0x0B: // G0 moel@69: tjMax = Floats(90 + 10); break; moel@49: case 0x0D: // M0 moel@69: tjMax = Floats(85 + 10); break; moel@49: default: moel@69: tjMax = Floats(85 + 10); break; moel@49: } break; moel@219: case 0x17: // Intel Core 2 (45nm) moel@219: microarchitecture = Microarchitecture.Core; moel@69: tjMax = Floats(100); break; moel@114: case 0x1C: // Intel Atom (45nm) moel@219: microarchitecture = Microarchitecture.Atom; moel@114: switch (stepping) { moel@114: case 0x02: // C0 moel@114: tjMax = Floats(90); break; moel@114: case 0x0A: // A0, B0 moel@114: tjMax = Floats(100); break; moel@114: default: moel@114: tjMax = Floats(90); break; moel@191: } break; moel@49: case 0x1A: // Intel Core i7 LGA1366 (45nm) moel@49: case 0x1E: // Intel Core i5, i7 LGA1156 (45nm) moel@249: case 0x1F: // Intel Core i5, i7 moel@49: case 0x25: // Intel Core i3, i5, i7 LGA1156 (32nm) moel@91: case 0x2C: // Intel Core i7 LGA1366 (32nm) 6 Core moel@350: case 0x2E: // Intel Xeon Processor 7500 series (45nm) moel@350: case 0x2F: // Intel Xeon Processor (32nm) moel@219: microarchitecture = Microarchitecture.Nehalem; moel@249: tjMax = GetTjMaxFromMSR(); moel@249: break; moel@249: case 0x2A: // Intel Core i5, i7 2xxx LGA1155 (32nm) moel@350: case 0x2D: // Next Generation Intel Xeon, i7 3xxx LGA2011 (32nm) moel@249: microarchitecture = Microarchitecture.SandyBridge; moel@249: tjMax = GetTjMaxFromMSR(); moel@49: break; moel@350: case 0x3A: // Intel Core i5, i7 3xxx LGA1155 (22nm) moel@417: case 0x3E: // Intel Core i7 4xxx LGA2011 (22nm) moel@350: microarchitecture = Microarchitecture.IvyBridge; moel@350: tjMax = GetTjMaxFromMSR(); moel@350: break; moel@425: case 0x3C: // Intel Core i5, i7 4xxx LGA1150 (22nm) moel@424: case 0x3F: // Intel Xeon E5-2600/1600 v3, Core i7-59xx moel@424: // LGA2011-v3, Haswell-E (22nm) moel@425: case 0x45: // Intel Core i5, i7 4xxxU (22nm) moel@424: case 0x46: moel@396: microarchitecture = Microarchitecture.Haswell; moel@396: tjMax = GetTjMaxFromMSR(); moel@396: break; moel@425: case 0x3D: // Intel Core M-5xxx (14nm) moel@425: microarchitecture = Microarchitecture.Broadwell; moel@425: tjMax = GetTjMaxFromMSR(); moel@425: break; moel@49: default: moel@219: microarchitecture = Microarchitecture.Unknown; moel@321: tjMax = Floats(100); moel@219: break; moel@49: } moel@49: } break; moel@264: case 0x0F: { moel@264: switch (model) { moel@264: case 0x00: // Pentium 4 (180nm) moel@264: case 0x01: // Pentium 4 (130nm) moel@264: case 0x02: // Pentium 4 (130nm) moel@264: case 0x03: // Pentium 4, Celeron D (90nm) moel@264: case 0x04: // Pentium 4, Pentium D, Celeron D (90nm) moel@264: case 0x06: // Pentium 4, Pentium D, Celeron D (65nm) moel@264: microarchitecture = Microarchitecture.NetBurst; moel@321: tjMax = Floats(100); moel@264: break; moel@264: default: moel@264: microarchitecture = Microarchitecture.Unknown; moel@264: tjMax = Floats(100); moel@264: break; moel@264: } moel@264: } break; moel@219: default: moel@219: microarchitecture = Microarchitecture.Unknown; moel@321: tjMax = Floats(100); moel@219: break; moel@219: } moel@219: moel@219: // set timeStampCounterMultiplier moel@219: switch (microarchitecture) { moel@264: case Microarchitecture.NetBurst: moel@219: case Microarchitecture.Atom: moel@219: case Microarchitecture.Core: { moel@219: uint eax, edx; moel@236: if (Ring0.Rdmsr(IA32_PERF_STATUS, out eax, out edx)) { moel@321: timeStampCounterMultiplier = moel@219: ((edx >> 8) & 0x1f) + 0.5 * ((edx >> 14) & 1); moel@219: } moel@219: } break; moel@321: case Microarchitecture.Nehalem: moel@350: case Microarchitecture.SandyBridge: moel@396: case Microarchitecture.IvyBridge: moel@425: case Microarchitecture.Haswell: moel@425: case Microarchitecture.Broadwell: { moel@219: uint eax, edx; moel@236: if (Ring0.Rdmsr(MSR_PLATFORM_INFO, out eax, out edx)) { moel@219: timeStampCounterMultiplier = (eax >> 8) & 0xff; moel@219: } moel@219: } break; moel@350: default: moel@350: timeStampCounterMultiplier = 0; moel@350: break; moel@49: } moel@1: moel@306: // check if processor supports a digital thermal sensor at core level moel@191: if (cpuid[0][0].Data.GetLength(0) > 6 && moel@350: (cpuid[0][0].Data[6, 0] & 1) != 0 && moel@350: microarchitecture != Microarchitecture.Unknown) moel@350: { moel@44: coreTemperatures = new Sensor[coreCount]; moel@44: for (int i = 0; i < coreTemperatures.Length; i++) { moel@134: coreTemperatures[i] = new Sensor(CoreString(i), i, moel@321: SensorType.Temperature, this, new[] { moel@63: new ParameterDescription( moel@306: "TjMax [°C]", "TjMax temperature of the core sensor.\n" + moel@69: "Temperature = TjMax - TSlope * Value.", tjMax[i]), moel@122: new ParameterDescription("TSlope [°C]", moel@122: "Temperature slope of the digital thermal sensor.\n" + moel@165: "Temperature = TjMax - TSlope * Value.", 1)}, settings); moel@155: ActivateSensor(coreTemperatures[i]); moel@44: } moel@44: } else { moel@44: coreTemperatures = new Sensor[0]; moel@1: } moel@49: moel@306: // check if processor supports a digital thermal sensor at package level moel@306: if (cpuid[0][0].Data.GetLength(0) > 6 && moel@350: (cpuid[0][0].Data[6, 0] & 0x40) != 0 && moel@350: microarchitecture != Microarchitecture.Unknown) moel@350: { moel@321: packageTemperature = new Sensor("CPU Package", moel@321: coreTemperatures.Length, SensorType.Temperature, this, new[] { moel@306: new ParameterDescription( moel@306: "TjMax [°C]", "TjMax temperature of the package sensor.\n" + moel@306: "Temperature = TjMax - TSlope * Value.", tjMax[0]), moel@306: new ParameterDescription("TSlope [°C]", moel@306: "Temperature slope of the digital thermal sensor.\n" + moel@306: "Temperature = TjMax - TSlope * Value.", 1)}, settings); moel@306: ActivateSensor(packageTemperature); moel@321: } moel@306: moel@191: busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this, settings); moel@44: coreClocks = new Sensor[coreCount]; moel@44: for (int i = 0; i < coreClocks.Length; i++) { moel@49: coreClocks[i] = moel@165: new Sensor(CoreString(i), i + 1, SensorType.Clock, this, settings); moel@350: if (HasTimeStampCounter && microarchitecture != Microarchitecture.Unknown) moel@79: ActivateSensor(coreClocks[i]); moel@44: } moel@191: moel@350: if (microarchitecture == Microarchitecture.SandyBridge || moel@396: microarchitecture == Microarchitecture.IvyBridge || moel@425: microarchitecture == Microarchitecture.Haswell || moel@425: microarchitecture == Microarchitecture.Broadwell) moel@350: { moel@321: powerSensors = new Sensor[energyStatusMSRs.Length]; moel@321: lastEnergyTime = new DateTime[energyStatusMSRs.Length]; moel@321: lastEnergyConsumed = new uint[energyStatusMSRs.Length]; moel@321: moel@317: uint eax, edx; moel@317: if (Ring0.Rdmsr(MSR_RAPL_POWER_UNIT, out eax, out edx)) moel@424: energyUnitMultiplier = 1.0f / (1 << (int)((eax >> 8) & 0x1F)); moel@317: moel@321: if (energyUnitMultiplier != 0) { moel@321: for (int i = 0; i < energyStatusMSRs.Length; i++) { moel@321: if (!Ring0.Rdmsr(energyStatusMSRs[i], out eax, out edx)) moel@321: continue; moel@317: moel@321: lastEnergyTime[i] = DateTime.UtcNow; moel@321: lastEnergyConsumed[i] = eax; moel@321: powerSensors[i] = new Sensor(powerSensorLabels[i], i, moel@321: SensorType.Power, this, settings); moel@321: ActivateSensor(powerSensors[i]); moel@321: } moel@317: } moel@317: } moel@317: moel@191: Update(); moel@1: } moel@1: moel@191: protected override uint[] GetMSRs() { moel@321: return new[] { moel@191: MSR_PLATFORM_INFO, moel@191: IA32_PERF_STATUS , moel@191: IA32_THERM_STATUS_MSR, moel@306: IA32_TEMPERATURE_TARGET, moel@317: IA32_PACKAGE_THERM_STATUS, moel@317: MSR_RAPL_POWER_UNIT, moel@317: MSR_PKG_ENERY_STATUS, moel@321: MSR_DRAM_ENERGY_STATUS, moel@320: MSR_PP0_ENERY_STATUS, moel@320: MSR_PP1_ENERY_STATUS moel@191: }; moel@1: } moel@1: moel@219: public override string GetReport() { moel@219: StringBuilder r = new StringBuilder(); moel@219: r.Append(base.GetReport()); moel@219: moel@264: r.Append("Microarchitecture: "); moel@264: r.AppendLine(microarchitecture.ToString()); moel@219: r.Append("Time Stamp Counter Multiplier: "); moel@219: r.AppendLine(timeStampCounterMultiplier.ToString( moel@219: CultureInfo.InvariantCulture)); moel@219: r.AppendLine(); moel@219: moel@219: return r.ToString(); moel@219: } moel@219: moel@191: public override void Update() { moel@191: base.Update(); moel@1: moel@1: for (int i = 0; i < coreTemperatures.Length; i++) { moel@46: uint eax, edx; moel@418: // if reading is valid moel@418: if (Ring0.RdmsrTx(IA32_THERM_STATUS_MSR, out eax, out edx, moel@418: 1UL << cpuid[i][0].Thread) && (eax & 0x80000000) != 0) moel@418: { moel@418: // get the dist from tjMax from bits 22:16 moel@418: float deltaT = ((eax & 0x007F0000) >> 16); moel@418: float tjMax = coreTemperatures[i].Parameters[0].Value; moel@418: float tSlope = coreTemperatures[i].Parameters[1].Value; moel@418: coreTemperatures[i].Value = tjMax - tSlope * deltaT; moel@418: } else { moel@418: coreTemperatures[i].Value = null; moel@79: } moel@24: } moel@24: moel@306: if (packageTemperature != null) { moel@306: uint eax, edx; moel@418: // if reading is valid moel@418: if (Ring0.RdmsrTx(IA32_PACKAGE_THERM_STATUS, out eax, out edx, moel@418: 1UL << cpuid[0][0].Thread) && (eax & 0x80000000) != 0) moel@418: { moel@306: // get the dist from tjMax from bits 22:16 moel@306: float deltaT = ((eax & 0x007F0000) >> 16); moel@306: float tjMax = packageTemperature.Parameters[0].Value; moel@306: float tSlope = packageTemperature.Parameters[1].Value; moel@306: packageTemperature.Value = tjMax - tSlope * deltaT; moel@306: } else { moel@306: packageTemperature.Value = null; moel@306: } moel@306: } moel@306: moel@350: if (HasTimeStampCounter && timeStampCounterMultiplier > 0) { moel@191: double newBusClock = 0; moel@191: uint eax, edx; moel@191: for (int i = 0; i < coreClocks.Length; i++) { moel@191: System.Threading.Thread.Sleep(1); moel@236: if (Ring0.RdmsrTx(IA32_PERF_STATUS, out eax, out edx, moel@321: 1UL << cpuid[i][0].Thread)) { moel@321: newBusClock = moel@219: TimeStampCounterFrequency / timeStampCounterMultiplier; moel@250: switch (microarchitecture) { moel@250: case Microarchitecture.Nehalem: { moel@250: uint multiplier = eax & 0xff; moel@250: coreClocks[i].Value = (float)(multiplier * newBusClock); moel@250: } break; moel@350: case Microarchitecture.SandyBridge: moel@396: case Microarchitecture.IvyBridge: moel@425: case Microarchitecture.Haswell: moel@425: case Microarchitecture.Broadwell: { moel@250: uint multiplier = (eax >> 8) & 0xff; moel@250: coreClocks[i].Value = (float)(multiplier * newBusClock); moel@250: } break; moel@250: default: { moel@321: double multiplier = moel@250: ((eax >> 8) & 0x1f) + 0.5 * ((eax >> 14) & 1); moel@250: coreClocks[i].Value = (float)(multiplier * newBusClock); moel@250: } break; moel@321: } moel@321: } else { moel@201: // if IA32_PERF_STATUS is not available, assume TSC frequency moel@201: coreClocks[i].Value = (float)TimeStampCounterFrequency; moel@46: } moel@44: } moel@191: if (newBusClock > 0) { moel@191: this.busClock.Value = (float)newBusClock; moel@191: ActivateSensor(this.busClock); moel@191: } moel@44: } moel@317: moel@321: if (powerSensors != null) { moel@321: foreach (Sensor sensor in powerSensors) { moel@321: if (sensor == null) moel@321: continue; moel@317: moel@321: uint eax, edx; moel@321: if (!Ring0.Rdmsr(energyStatusMSRs[sensor.Index], out eax, out edx)) moel@321: continue; moel@317: moel@317: DateTime time = DateTime.UtcNow; moel@317: uint energyConsumed = eax; moel@321: float deltaTime = moel@321: (float)(time - lastEnergyTime[sensor.Index]).TotalSeconds; moel@321: if (deltaTime < 0.01) moel@321: continue; moel@321: moel@321: sensor.Value = energyUnitMultiplier * unchecked( moel@321: energyConsumed - lastEnergyConsumed[sensor.Index]) / deltaTime; moel@321: lastEnergyTime[sensor.Index] = time; moel@321: lastEnergyConsumed[sensor.Index] = energyConsumed; moel@317: } moel@317: } moel@46: } moel@191: } moel@1: }