Hardware/CPU/IntelCPU.cs
author moel.mich
Tue, 16 Feb 2010 21:44:25 +0000
changeset 47 1d02fde1bb23
parent 44 c150de283ca0
child 49 b418098ceca3
permissions -rw-r--r--
Restored CTSHolding check and added more report output for T-Balancer enumeration.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     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
     8  
     9   http://www.mozilla.org/MPL/
    10 
    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.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    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.
    21 
    22   Contributor(s):
    23 
    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.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Drawing;
    41 using System.Diagnostics;
    42 using System.Reflection;
    43 using System.Text;
    44 
    45 namespace OpenHardwareMonitor.Hardware.CPU {
    46   public class IntelCPU : Hardware, IHardware {
    47 
    48     private string name;
    49     private Image icon;
    50 
    51     private uint family;
    52     private uint model;
    53     private uint stepping;
    54 
    55     private Sensor[] coreTemperatures;
    56     private Sensor totalLoad;
    57     private Sensor[] coreLoads;
    58     private Sensor[] coreClocks;
    59     private Sensor busClock;
    60 
    61     private float tjMax = 0;
    62     private uint logicalProcessors;
    63     private uint logicalProcessorsPerCore;
    64     private uint coreCount;
    65 
    66     private CPULoad cpuLoad;
    67 
    68     private ulong lastCount;    
    69     private long lastTime;
    70     private uint maxNehalemMultiplier = 0;
    71     
    72     private const uint IA32_THERM_STATUS_MSR = 0x019C;
    73     private const uint IA32_TEMPERATURE_TARGET = 0x01A2;
    74     private const uint IA32_PERF_STATUS = 0x0198;
    75     private const uint MSR_PLATFORM_INFO = 0xCE;
    76 
    77     public IntelCPU(string name, uint family, uint model, uint stepping, 
    78       uint[,] cpuidData, uint[,] cpuidExtData) {
    79       
    80       this.name = name;
    81       this.icon = Utilities.EmbeddedResources.GetImage("cpu.png");
    82 
    83       this.family = family;
    84       this.model = model;
    85       this.stepping = stepping;
    86 
    87       logicalProcessors = 0;
    88       if (cpuidData.GetLength(0) > 0x0B) {
    89         uint eax, ebx, ecx, edx;
    90         WinRing0.CpuidEx(0x0B, 0, out eax, out ebx, out ecx, out edx);
    91         logicalProcessorsPerCore = ebx & 0xFF;
    92         if (logicalProcessorsPerCore > 0) {
    93           WinRing0.CpuidEx(0x0B, 1, out eax, out ebx, out ecx, out edx);
    94           logicalProcessors = ebx & 0xFF;
    95         }   
    96       }
    97       if (logicalProcessors <= 0 && cpuidData.GetLength(0) > 0x04) {
    98         logicalProcessors = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
    99         logicalProcessorsPerCore = 1;
   100       }
   101       if (logicalProcessors <= 0) {
   102         logicalProcessors = 1;
   103         logicalProcessorsPerCore = 1;
   104       }
   105 
   106       coreCount = logicalProcessors / logicalProcessorsPerCore;
   107 
   108       // check if processor supports a digital thermal sensor
   109       if (cpuidData.GetLength(0) > 6 && (cpuidData[6, 0] & 1) != 0) {
   110 
   111         switch (family) {
   112           case 0x06: {
   113               switch (model) {
   114                 case 0x0F: // Intel Core 65nm
   115                   switch (stepping) {
   116                     case 0x06: // B2
   117                       switch (coreCount) {
   118                         case 2:
   119                           tjMax = 80; break;
   120                         case 4:
   121                           tjMax = 90; break;
   122                         default:
   123                           tjMax = 85; break;
   124                       }
   125                       tjMax = 80; break;
   126                     case 0x0B: // G0
   127                       tjMax = 90; break;
   128                     case 0x0D: // M0
   129                       tjMax = 85; break;
   130                     default:
   131                       tjMax = 85; break;
   132                   } break;
   133                 case 0x17: // Intel Core 45nm
   134                   tjMax = 100; break;
   135                 case 0x1C: // Intel Atom 
   136                   tjMax = 90; break;
   137                 case 0x1A: // Intel Core i7
   138                 case 0x1E: // Intel Core i5
   139                   uint eax, edx;
   140                   if (WinRing0.Rdmsr(IA32_TEMPERATURE_TARGET, out eax, out edx)) 
   141                   {
   142                     tjMax = (eax >> 16) & 0xFF;
   143                   } else
   144                     tjMax = 100;
   145                   break;
   146                 default:
   147                   tjMax = 100; break;
   148               }
   149             } break;
   150           default: tjMax = 100; break;
   151         }
   152 
   153         if (family == 0x06 && model >= 0x1A) { // Core i5, i7
   154           uint eax, edx;
   155           if (WinRing0.Rdmsr(MSR_PLATFORM_INFO, out eax, out edx)) {
   156             maxNehalemMultiplier = (eax >> 8) & 0xff;
   157           }
   158         }
   159 
   160         coreTemperatures = new Sensor[coreCount];
   161         for (int i = 0; i < coreTemperatures.Length; i++) {
   162           coreTemperatures[i] = new Sensor("Core #" + (i + 1), i, tjMax,
   163             SensorType.Temperature, this);
   164         }
   165       } else {
   166         coreTemperatures = new Sensor[0];
   167       }
   168               
   169       totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);   
   170       coreLoads = new Sensor[coreCount];
   171       for (int i = 0; i < coreLoads.Length; i++) 
   172         coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1,
   173           SensorType.Load, this);     
   174       cpuLoad = new CPULoad(coreCount, logicalProcessorsPerCore);
   175       if (cpuLoad.IsAvailable) {
   176         foreach (Sensor sensor in coreLoads)
   177           ActivateSensor(sensor);
   178         ActivateSensor(totalLoad);
   179       }
   180 
   181       lastCount = 0;
   182       lastTime = 0;
   183       busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this);      
   184       coreClocks = new Sensor[coreCount];
   185       for (int i = 0; i < coreClocks.Length; i++) {
   186         coreClocks[i] = 
   187           new Sensor("Core #" + (i + 1), i + 1, SensorType.Clock, this);
   188         ActivateSensor(coreClocks[i]);
   189       }
   190       
   191       Update();                   
   192     }
   193 
   194     public string Name {
   195       get { return name; }
   196     }
   197 
   198     public string Identifier {
   199       get { return "/intelcpu/0"; }
   200     }
   201 
   202     public Image Icon {
   203       get { return icon; }
   204     }
   205 
   206     public string GetReport() {
   207       StringBuilder r = new StringBuilder();
   208 
   209       r.AppendLine("Intel CPU");
   210       r.AppendLine();
   211       r.AppendFormat("Name: {0}{1}", name, Environment.NewLine);
   212       r.AppendFormat("Number of cores: {0}{1}", coreCount, 
   213         Environment.NewLine);
   214       r.AppendFormat("Threads per core: {0}{1}", logicalProcessorsPerCore,
   215         Environment.NewLine);
   216       r.AppendFormat("TjMax: {0}{1}", tjMax, Environment.NewLine);
   217       r.AppendLine();
   218 
   219       return r.ToString();
   220     }
   221 
   222     public void Update() {
   223             
   224       for (int i = 0; i < coreTemperatures.Length; i++) {
   225         uint eax, edx;
   226         if (WinRing0.RdmsrTx(
   227           IA32_THERM_STATUS_MSR, out eax, out edx, 
   228             (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) 
   229         {
   230           // if reading is valid
   231           if ((eax & 0x80000000) != 0) {
   232             // get the dist from tjMax from bits 22:16
   233             coreTemperatures[i].Value = tjMax - ((eax & 0x007F0000) >> 16);
   234             ActivateSensor(coreTemperatures[i]);
   235           } else {
   236             DeactivateSensor(coreTemperatures[i]);
   237           }
   238         }        
   239       }
   240 
   241       if (cpuLoad.IsAvailable) {
   242         cpuLoad.Update();
   243         for (int i = 0; i < coreLoads.Length; i++)
   244           coreLoads[i].Value = cpuLoad.GetCoreLoad(i);
   245         totalLoad.Value = cpuLoad.GetTotalLoad();
   246       }
   247      
   248       uint lsb, msb;
   249       bool valid = WinRing0.RdtscTx(out lsb, out msb, (UIntPtr)1);
   250       long time = Stopwatch.GetTimestamp();
   251       ulong count = ((ulong)msb << 32) | lsb;
   252       double delta = ((double)(time - lastTime)) / Stopwatch.Frequency;
   253       if (valid && delta > 0.5) {
   254         double maxClock = (count - lastCount) / (1e6 * delta);
   255         double busClock = 0;
   256         uint eax, edx;
   257         for (int i = 0; i < coreClocks.Length; i++) {
   258           System.Threading.Thread.Sleep(1);
   259           if (WinRing0.RdmsrTx(IA32_PERF_STATUS, out eax, out edx,
   260             (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) {
   261             if (model < 0x1A) { // Core 2
   262               uint multiplier = (eax >> 8) & 0x1f;
   263               uint maxMultiplier = (edx >> 8) & 0x1f;
   264               // factor = multiplier * 2 to handle non integer multipliers 
   265               uint factor = (multiplier << 1) | ((eax >> 14) & 1);
   266               uint maxFactor = (maxMultiplier << 1) | ((edx >> 14) & 1);
   267               if (maxFactor > 0) {
   268                 coreClocks[i].Value = (float)(factor * maxClock / maxFactor);
   269                 busClock = (float)(2 * maxClock / maxFactor);
   270               }
   271             } else { // Core i5, i7
   272               uint nehalemMultiplier = eax & 0xff;
   273               if (maxNehalemMultiplier > 0) {
   274                 coreClocks[i].Value =
   275                   (float)(nehalemMultiplier * maxClock / maxNehalemMultiplier);
   276                 busClock = (float)(maxClock / maxNehalemMultiplier);
   277               }
   278             }
   279           } else { // Intel Pentium 4
   280             // if IA32_PERF_STATUS is not available, assume maxClock
   281             coreClocks[i].Value = (float)maxClock;
   282           }
   283         }
   284         if (busClock > 0) {
   285           this.busClock.Value = (float)busClock;
   286           ActivateSensor(this.busClock);
   287         }
   288       }
   289       lastCount = count;
   290       lastTime = time;
   291     }
   292   }  
   293 }