Rewritten the T-Balancer code to use the FTDI D2XX drivers directly instead of the System.IO.Ports.SerialPort class. The SerialPort class has some ugly problems like http://connect.microsoft.com/VisualStudio/feedback/details/140018/serialport-crashes-after-disconnect-of-usb-com-port or http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/8a1825d2-c84b-4620-91e7-3934a4d47330 for which no real solution seems to exist. And Microsoft doesn't feel like it needs to be fixed for years now.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
41 using System.Diagnostics;
42 using System.Globalization;
43 using System.Reflection;
44 using System.Runtime.InteropServices;
45 using System.Threading;
48 namespace OpenHardwareMonitor.Hardware.CPU {
49 public class IntelCPU : Hardware, IHardware {
56 private uint stepping;
58 private Sensor[] coreTemperatures;
60 private Sensor totalLoad;
61 private Sensor[] coreLoads;
62 private Sensor[] coreClocks;
63 private Sensor busClock;
64 private uint logicalProcessors;
65 private uint logicalProcessorsPerCore;
66 private uint coreCount;
68 private bool invariantTSC;
69 private double estimatedMaxClock;
71 private ulong affinityMask;
72 private CPULoad cpuLoad;
74 private ulong lastTimeStampCount;
75 private long lastTime;
76 private uint maxNehalemMultiplier = 0;
78 private const uint IA32_THERM_STATUS_MSR = 0x019C;
79 private const uint IA32_TEMPERATURE_TARGET = 0x01A2;
80 private const uint IA32_PERF_STATUS = 0x0198;
81 private const uint MSR_PLATFORM_INFO = 0xCE;
83 private string CoreString(int i) {
87 return "CPU Core #" + (i + 1);
90 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
91 private static extern bool GetProcessAffinityMask(IntPtr handle,
92 out IntPtr processMask, out IntPtr systemMask);
94 private float[] Floats(float f) {
95 float[] result = new float[coreCount];
96 for (int i = 0; i < coreCount; i++)
101 public IntelCPU(string name, uint family, uint model, uint stepping,
102 uint[,] cpuidData, uint[,] cpuidExtData) {
105 this.icon = Utilities.EmbeddedResources.GetImage("cpu.png");
107 this.family = family;
109 this.stepping = stepping;
111 logicalProcessors = 0;
112 if (cpuidData.GetLength(0) > 0x0B) {
113 uint eax, ebx, ecx, edx;
114 WinRing0.CpuidEx(0x0B, 0, out eax, out ebx, out ecx, out edx);
115 logicalProcessorsPerCore = ebx & 0xFF;
116 if (logicalProcessorsPerCore > 0) {
117 WinRing0.CpuidEx(0x0B, 1, out eax, out ebx, out ecx, out edx);
118 logicalProcessors = ebx & 0xFF;
121 if (logicalProcessors <= 0 && cpuidData.GetLength(0) > 0x04) {
122 uint coresPerPackage = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
123 uint logicalPerPackage = (cpuidData[1, 1] >> 16) & 0xFF;
124 logicalProcessorsPerCore = logicalPerPackage / coresPerPackage;
125 logicalProcessors = logicalPerPackage;
127 if (logicalProcessors <= 0 && cpuidData.GetLength(0) > 0x01) {
128 uint logicalPerPackage = (cpuidData[1, 1] >> 16) & 0xFF;
129 logicalProcessorsPerCore = logicalPerPackage;
130 logicalProcessors = logicalPerPackage;
132 if (logicalProcessors <= 0) {
133 logicalProcessors = 1;
134 logicalProcessorsPerCore = 1;
137 IntPtr processMask, systemMask;
138 GetProcessAffinityMask(Process.GetCurrentProcess().Handle,
139 out processMask, out systemMask);
140 affinityMask = (ulong)systemMask;
142 // correct values in case HypeThreading is disabled
143 if (logicalProcessorsPerCore > 1) {
144 ulong affinity = affinityMask;
145 int availableLogicalProcessors = 0;
146 while (affinity != 0) {
147 if ((affinity & 0x1) > 0)
148 availableLogicalProcessors++;
151 while (logicalProcessorsPerCore > 1 &&
152 availableLogicalProcessors < logicalProcessors) {
153 logicalProcessors >>= 1;
154 logicalProcessorsPerCore >>= 1;
158 coreCount = logicalProcessors / logicalProcessorsPerCore;
164 case 0x0F: // Intel Core (65nm)
169 tjMax = Floats(80 + 10); break;
171 tjMax = Floats(90 + 10); break;
173 tjMax = Floats(85 + 10); break;
175 tjMax = Floats(80 + 10); break;
177 tjMax = Floats(90 + 10); break;
179 tjMax = Floats(85 + 10); break;
181 tjMax = Floats(85 + 10); break;
183 case 0x17: // Intel Core (45nm)
184 tjMax = Floats(100); break;
185 case 0x1C: // Intel Atom
186 tjMax = Floats(90); break;
187 case 0x1A: // Intel Core i7 LGA1366 (45nm)
188 case 0x1E: // Intel Core i5, i7 LGA1156 (45nm)
189 case 0x25: // Intel Core i3, i5, i7 LGA1156 (32nm)
191 tjMax = new float[coreCount];
192 for (int i = 0; i < coreCount; i++) {
193 if (WinRing0.RdmsrTx(IA32_TEMPERATURE_TARGET, out eax,
195 1 << (int)(logicalProcessorsPerCore * i))))
197 tjMax[i] = (eax >> 16) & 0xFF;
202 if (WinRing0.Rdmsr(MSR_PLATFORM_INFO, out eax, out edx)) {
203 maxNehalemMultiplier = (eax >> 8) & 0xff;
207 tjMax = Floats(100); break;
210 default: tjMax = Floats(100); break;
213 // check if processor supports a digital thermal sensor
214 if (cpuidData.GetLength(0) > 6 && (cpuidData[6, 0] & 1) != 0) {
215 coreTemperatures = new Sensor[coreCount];
216 for (int i = 0; i < coreTemperatures.Length; i++) {
217 coreTemperatures[i] = new Sensor(CoreString(i), i, tjMax[i],
218 SensorType.Temperature, this, new ParameterDescription[] {
219 new ParameterDescription(
220 "TjMax", "TjMax temperature of the core.\n" +
221 "Temperature = TjMax - TSlope * Value.", tjMax[i]),
222 new ParameterDescription(
223 "TSlope", "Temperature slope of the digital thermal sensor.\n" +
224 "Temperature = TjMax - TSlope * Value.", 1)});
227 coreTemperatures = new Sensor[0];
231 totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);
234 coreLoads = new Sensor[coreCount];
235 for (int i = 0; i < coreLoads.Length; i++)
236 coreLoads[i] = new Sensor(CoreString(i), i + 1,
237 SensorType.Load, this);
238 cpuLoad = new CPULoad(coreCount, logicalProcessorsPerCore);
239 if (cpuLoad.IsAvailable) {
240 foreach (Sensor sensor in coreLoads)
241 ActivateSensor(sensor);
242 if (totalLoad != null)
243 ActivateSensor(totalLoad);
246 // check if processor has TSC
247 if (cpuidData.GetLength(0) > 1 && (cpuidData[1, 3] & 0x10) != 0)
252 // check if processor supports invariant TSC
253 if (cpuidExtData.GetLength(0) > 7 && (cpuidExtData[7, 3] & 0x100) != 0)
256 invariantTSC = false;
258 // preload the function
262 // estimate the max clock in MHz
263 estimatedMaxClock = 1e-6 * EstimateMaxClock(0.01);
265 lastTimeStampCount = 0;
267 busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this);
268 coreClocks = new Sensor[coreCount];
269 for (int i = 0; i < coreClocks.Length; i++) {
271 new Sensor(CoreString(i), i + 1, SensorType.Clock, this);
273 ActivateSensor(coreClocks[i]);
283 public string Identifier {
284 get { return "/intelcpu/0"; }
291 private void AppendMSRData(StringBuilder r, uint msr, int core) {
293 if (WinRing0.RdmsrTx(msr, out eax, out edx,
294 (UIntPtr)(1 << (int)(logicalProcessorsPerCore * core)))) {
296 r.Append((msr).ToString("X8"));
298 r.Append((edx).ToString("X8"));
300 r.Append((eax).ToString("X8"));
305 public string GetReport() {
306 StringBuilder r = new StringBuilder();
308 r.AppendLine("Intel CPU");
310 r.AppendFormat("Name: {0}{1}", name, Environment.NewLine);
311 r.AppendFormat("Number of Cores: {0}{1}", coreCount,
312 Environment.NewLine);
313 r.AppendFormat("Threads per Core: {0}{1}", logicalProcessorsPerCore,
314 Environment.NewLine);
315 r.AppendFormat("Affinity Mask: 0x{0}{1}", affinityMask.ToString("X"),
316 Environment.NewLine);
317 r.AppendLine("TSC: " +
318 (hasTSC ? (invariantTSC ? "Invariant" : "Not Invariant") : "None"));
319 r.AppendLine(string.Format(CultureInfo.InvariantCulture,
320 "Timer Frequency: {0} MHz", Stopwatch.Frequency * 1e-6));
321 r.AppendLine(string.Format(CultureInfo.InvariantCulture,
322 "Max Clock: {0} MHz", Math.Round(estimatedMaxClock * 100) * 0.01));
325 for (int i = 0; i < coreCount; i++) {
326 r.AppendLine("MSR Core #" + (i + 1));
328 r.AppendLine(" MSR EDX EAX");
329 AppendMSRData(r, MSR_PLATFORM_INFO, i);
330 AppendMSRData(r, IA32_PERF_STATUS, i);
331 AppendMSRData(r, IA32_THERM_STATUS_MSR, i);
332 AppendMSRData(r, IA32_TEMPERATURE_TARGET, i);
339 private double EstimateMaxClock(double timeWindow) {
340 long ticks = (long)(timeWindow * Stopwatch.Frequency);
341 uint lsbBegin, msbBegin, lsbEnd, msbEnd;
343 Thread.BeginThreadAffinity();
344 long timeBegin = Stopwatch.GetTimestamp() + 2;
345 long timeEnd = timeBegin + ticks;
346 while (Stopwatch.GetTimestamp() < timeBegin) { }
347 WinRing0.Rdtsc(out lsbBegin, out msbBegin);
348 while (Stopwatch.GetTimestamp() < timeEnd) { }
349 WinRing0.Rdtsc(out lsbEnd, out msbEnd);
350 Thread.EndThreadAffinity();
352 ulong countBegin = ((ulong)msbBegin << 32) | lsbBegin;
353 ulong countEnd = ((ulong)msbEnd << 32) | lsbEnd;
355 return (((double)(countEnd - countBegin)) * Stopwatch.Frequency) /
356 (timeEnd - timeBegin);
359 public void Update() {
360 for (int i = 0; i < coreTemperatures.Length; i++) {
362 if (WinRing0.RdmsrTx(
363 IA32_THERM_STATUS_MSR, out eax, out edx,
364 (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) {
365 // if reading is valid
366 if ((eax & 0x80000000) != 0) {
367 // get the dist from tjMax from bits 22:16
368 float deltaT = ((eax & 0x007F0000) >> 16);
369 float tjMax = coreTemperatures[i].Parameters[0].Value;
370 float tSlope = coreTemperatures[i].Parameters[1].Value;
371 coreTemperatures[i].Value = tjMax - tSlope * deltaT;
372 ActivateSensor(coreTemperatures[i]);
374 DeactivateSensor(coreTemperatures[i]);
379 if (cpuLoad.IsAvailable) {
381 for (int i = 0; i < coreLoads.Length; i++)
382 coreLoads[i].Value = cpuLoad.GetCoreLoad(i);
383 if (totalLoad != null)
384 totalLoad.Value = cpuLoad.GetTotalLoad();
389 WinRing0.RdtscTx(out lsb, out msb, (UIntPtr)1);
390 long time = Stopwatch.GetTimestamp();
391 ulong timeStampCount = ((ulong)msb << 32) | lsb;
392 double delta = ((double)(time - lastTime)) / Stopwatch.Frequency;
396 maxClock = (timeStampCount - lastTimeStampCount) / (1e6 * delta);
398 maxClock = estimatedMaxClock;
402 for (int i = 0; i < coreClocks.Length; i++) {
403 System.Threading.Thread.Sleep(1);
404 if (WinRing0.RdmsrTx(IA32_PERF_STATUS, out eax, out edx,
405 (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) {
406 if (maxNehalemMultiplier > 0) { // Core i3, i5, i7
407 uint nehalemMultiplier = eax & 0xff;
408 coreClocks[i].Value =
409 (float)(nehalemMultiplier * maxClock / maxNehalemMultiplier);
410 busClock = (float)(maxClock / maxNehalemMultiplier);
412 uint multiplier = (eax >> 8) & 0x1f;
413 uint maxMultiplier = (edx >> 8) & 0x1f;
414 // factor = multiplier * 2 to handle non integer multipliers
415 uint factor = (multiplier << 1) | ((eax >> 14) & 1);
416 uint maxFactor = (maxMultiplier << 1) | ((edx >> 14) & 1);
418 coreClocks[i].Value = (float)(factor * maxClock / maxFactor);
419 busClock = (float)(2 * maxClock / maxFactor);
422 } else { // Intel Pentium 4
423 // if IA32_PERF_STATUS is not available, assume maxClock
424 coreClocks[i].Value = (float)maxClock;
428 this.busClock.Value = (float)busClock;
429 ActivateSensor(this.busClock);
432 lastTimeStampCount = timeStampCount;