Hardware/CPU/IntelCPU.cs
author moel.mich
Tue, 02 Feb 2010 21:58:54 +0000
changeset 22 e6832d4b89d2
parent 13 d32fc5f2e822
child 23 1662dea7a261
permissions -rw-r--r--
Release version 0.1.10. Changed core count for Intel Core i5/i7 CPUs. Added CpuidEx function.
     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       logicalProcessorsPerCore = 1;
    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         WinRing0.CpuidEx(0x0B, 1, out eax, out ebx, out ecx, out edx);
    72         logicalProcessors = ebx & 0xFF;            
    73       } else if (cpuidData.GetLength(0) > 0x04) {
    74         logicalProcessors = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
    75         logicalProcessorsPerCore = 1;
    76       } else {
    77         logicalProcessors = 1;
    78         logicalProcessorsPerCore = 1;
    79       }
    80 
    81       coreCount = logicalProcessors / logicalProcessorsPerCore;
    82 
    83       switch (family) {
    84         case 0x06: {
    85           switch (model) {
    86             case 0x0F: // Intel Core 65nm
    87               switch (stepping) {
    88                 case 0x06: // B2
    89                   switch (coreCount) {
    90                     case 2:
    91                       tjMax = 80; break;
    92                     case 4:
    93                       tjMax = 90; break;
    94                     default:
    95                       tjMax = 85; break;
    96                   }
    97                   tjMax = 80; break;
    98                 case 0x0B: // G0
    99                   tjMax = 90; break;
   100                 case 0x0D: // M0
   101                   tjMax = 85; break;
   102                 default:
   103                   tjMax = 85; break;
   104               } break;            
   105             case 0x17: // Intel Core 45nm
   106               tjMax = 100; break;
   107             case 0x1C: // Intel Atom 
   108               tjMax = 90; break;
   109             case 0x1A:
   110               uint eax = 0, edx = 0;
   111               if (WinRing0.RdmsrPx(
   112                   IA32_TEMPERATURE_TARGET, ref eax, ref edx, (UIntPtr)1)) {
   113                 tjMax = (eax >> 16) & 0xFF;
   114               } else
   115                 tjMax = 100;
   116               break;
   117             default:
   118               tjMax = 100; break;
   119           }
   120         } break;
   121         default: tjMax = 100; break;
   122       }
   123 
   124       coreTemperatures = new Sensor[coreCount];
   125       for (int i = 0; i < coreTemperatures.Length; i++)
   126         coreTemperatures[i] =
   127           new Sensor("Core #" + (i + 1), i, tjMax, SensorType.Temperature, 
   128             this);
   129 
   130       Update();                   
   131     }
   132 
   133     public string Name {
   134       get { return name; }
   135     }
   136 
   137     public string Identifier {
   138       get { return "/intelcpu/0"; }
   139     }
   140 
   141     public Image Icon {
   142       get { return icon; }
   143     }
   144 
   145     public ISensor[] Sensors {
   146       get {
   147         return coreTemperatures;
   148       }
   149     }
   150 
   151     public string GetReport() {
   152       StringBuilder r = new StringBuilder();
   153 
   154       r.AppendLine("Intel CPU");
   155       r.AppendLine();
   156       r.AppendFormat("Name: {0}{1}", name, Environment.NewLine);
   157       r.AppendFormat("Number of cores: {0}{1}", coreCount, 
   158         Environment.NewLine);
   159       r.AppendFormat("Threads per core: {0}{1}", logicalProcessorsPerCore,
   160         Environment.NewLine);
   161       r.AppendFormat("TjMax: {0}{1}", tjMax, Environment.NewLine);
   162       r.AppendLine();
   163 
   164       return r.ToString();
   165     }
   166 
   167     public void Update() {
   168 
   169       uint eax = 0, edx = 0;      
   170       for (int i = 0; i < coreTemperatures.Length; i++) {
   171         if (WinRing0.RdmsrPx(
   172           IA32_THERM_STATUS_MSR, ref eax, ref edx, 
   173             (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) 
   174         {
   175           // if reading is valid
   176           if ((eax & 0x80000000) != 0) {
   177             // get the dist from tjMax from bits 22:16
   178             coreTemperatures[i].Value = tjMax - ((eax & 0x007F0000) >> 16);
   179           }
   180         }        
   181       }  
   182     }
   183 
   184     #pragma warning disable 67
   185     public event SensorEventHandler SensorAdded;
   186     public event SensorEventHandler SensorRemoved;
   187     #pragma warning restore 67
   188   }
   189 }