Hardware/CPU/IntelCPU.cs
author moel.mich
Wed, 03 Feb 2010 18:10:11 +0000
changeset 23 1662dea7a261
parent 22 e6832d4b89d2
child 24 09ab31bee6bd
permissions -rw-r--r--
Fixed Intel CPU coreCount in case CPUID 0x0B is supported but returns only 0.
     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.Reflection;
    42 using System.Text;
    43 
    44 namespace OpenHardwareMonitor.Hardware.CPU {
    45   public class IntelCPU : IHardware {
    46 
    47     private string name;
    48     private Image icon;
    49 
    50     private Sensor[] coreTemperatures;
    51 
    52     private float tjMax = 0;
    53     private uint logicalProcessors;
    54     private uint logicalProcessorsPerCore;
    55     private uint coreCount;
    56 
    57     private const uint IA32_THERM_STATUS_MSR = 0x019C;
    58     private const uint IA32_TEMPERATURE_TARGET = 0x01A2;
    59 
    60     public IntelCPU(string name, uint family, uint model, uint stepping, 
    61       uint[,] cpuidData, uint[,] cpuidExtData) {
    62       
    63       this.name = name;
    64       this.icon = Utilities.EmbeddedResources.GetImage("cpu.png");
    65             
    66       logicalProcessors = 0;
    67       if (cpuidData.GetLength(0) > 0x0B) {
    68         uint eax, ebx, ecx, edx;
    69         WinRing0.CpuidEx(0x0B, 0, out eax, out ebx, out ecx, out edx);
    70         logicalProcessorsPerCore = ebx & 0xFF;
    71         if (logicalProcessorsPerCore > 0) {
    72           WinRing0.CpuidEx(0x0B, 1, out eax, out ebx, out ecx, out edx);
    73           logicalProcessors = ebx & 0xFF;
    74         }   
    75       }
    76       if (logicalProcessors <= 0 && cpuidData.GetLength(0) > 0x04) {
    77         logicalProcessors = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
    78         logicalProcessorsPerCore = 1;
    79       }
    80       if (logicalProcessors <= 0) {
    81         logicalProcessors = 1;
    82         logicalProcessorsPerCore = 1;
    83       }
    84 
    85       coreCount = logicalProcessors / logicalProcessorsPerCore;
    86 
    87       switch (family) {
    88         case 0x06: {
    89           switch (model) {
    90             case 0x0F: // Intel Core 65nm
    91               switch (stepping) {
    92                 case 0x06: // B2
    93                   switch (coreCount) {
    94                     case 2:
    95                       tjMax = 80; break;
    96                     case 4:
    97                       tjMax = 90; break;
    98                     default:
    99                       tjMax = 85; break;
   100                   }
   101                   tjMax = 80; break;
   102                 case 0x0B: // G0
   103                   tjMax = 90; break;
   104                 case 0x0D: // M0
   105                   tjMax = 85; break;
   106                 default:
   107                   tjMax = 85; break;
   108               } break;            
   109             case 0x17: // Intel Core 45nm
   110               tjMax = 100; break;
   111             case 0x1C: // Intel Atom 
   112               tjMax = 90; break;
   113             case 0x1A:
   114               uint eax = 0, edx = 0;
   115               if (WinRing0.RdmsrPx(
   116                   IA32_TEMPERATURE_TARGET, ref eax, ref edx, (UIntPtr)1)) {
   117                 tjMax = (eax >> 16) & 0xFF;
   118               } else
   119                 tjMax = 100;
   120               break;
   121             default:
   122               tjMax = 100; break;
   123           }
   124         } break;
   125         default: tjMax = 100; break;
   126       }
   127 
   128       coreTemperatures = new Sensor[coreCount];
   129       for (int i = 0; i < coreTemperatures.Length; i++)
   130         coreTemperatures[i] =
   131           new Sensor("Core #" + (i + 1), i, tjMax, SensorType.Temperature, 
   132             this);
   133 
   134       Update();                   
   135     }
   136 
   137     public string Name {
   138       get { return name; }
   139     }
   140 
   141     public string Identifier {
   142       get { return "/intelcpu/0"; }
   143     }
   144 
   145     public Image Icon {
   146       get { return icon; }
   147     }
   148 
   149     public ISensor[] Sensors {
   150       get {
   151         return coreTemperatures;
   152       }
   153     }
   154 
   155     public string GetReport() {
   156       StringBuilder r = new StringBuilder();
   157 
   158       r.AppendLine("Intel CPU");
   159       r.AppendLine();
   160       r.AppendFormat("Name: {0}{1}", name, Environment.NewLine);
   161       r.AppendFormat("Number of cores: {0}{1}", coreCount, 
   162         Environment.NewLine);
   163       r.AppendFormat("Threads per core: {0}{1}", logicalProcessorsPerCore,
   164         Environment.NewLine);
   165       r.AppendFormat("TjMax: {0}{1}", tjMax, Environment.NewLine);
   166       r.AppendLine();
   167 
   168       return r.ToString();
   169     }
   170 
   171     public void Update() {
   172 
   173       uint eax = 0, edx = 0;      
   174       for (int i = 0; i < coreTemperatures.Length; i++) {
   175         if (WinRing0.RdmsrPx(
   176           IA32_THERM_STATUS_MSR, ref eax, ref edx, 
   177             (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) 
   178         {
   179           // if reading is valid
   180           if ((eax & 0x80000000) != 0) {
   181             // get the dist from tjMax from bits 22:16
   182             coreTemperatures[i].Value = tjMax - ((eax & 0x007F0000) >> 16);
   183           }
   184         }        
   185       }  
   186     }
   187 
   188     #pragma warning disable 67
   189     public event SensorEventHandler SensorAdded;
   190     public event SensorEventHandler SensorRemoved;
   191     #pragma warning restore 67
   192   }
   193 }