Hardware/CPU/IntelCPU.cs
author moel.mich
Fri, 12 Feb 2010 22:46:31 +0000
changeset 42 47385d4fc990
parent 26 0e01b63e1fdc
child 44 c150de283ca0
permissions -rw-r--r--
Tray sensor display default color is black and color can be changed now. Fixed CPU load reading for AMD CPUs and added additional misc device for AMD core temperature reading.
     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 Sensor[] coreTemperatures;
    52     private Sensor totalLoad;
    53     private Sensor[] coreLoads;
    54 
    55     private float tjMax = 0;
    56     private uint logicalProcessors;
    57     private uint logicalProcessorsPerCore;
    58     private uint coreCount;
    59 
    60     private CPULoad cpuLoad;
    61     
    62     private const uint IA32_THERM_STATUS_MSR = 0x019C;
    63     private const uint IA32_TEMPERATURE_TARGET = 0x01A2;
    64 
    65     public IntelCPU(string name, uint family, uint model, uint stepping, 
    66       uint[,] cpuidData, uint[,] cpuidExtData) {
    67       
    68       this.name = name;
    69       this.icon = Utilities.EmbeddedResources.GetImage("cpu.png");
    70             
    71       logicalProcessors = 0;
    72       if (cpuidData.GetLength(0) > 0x0B) {
    73         uint eax, ebx, ecx, edx;
    74         WinRing0.CpuidEx(0x0B, 0, out eax, out ebx, out ecx, out edx);
    75         logicalProcessorsPerCore = ebx & 0xFF;
    76         if (logicalProcessorsPerCore > 0) {
    77           WinRing0.CpuidEx(0x0B, 1, out eax, out ebx, out ecx, out edx);
    78           logicalProcessors = ebx & 0xFF;
    79         }   
    80       }
    81       if (logicalProcessors <= 0 && cpuidData.GetLength(0) > 0x04) {
    82         logicalProcessors = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
    83         logicalProcessorsPerCore = 1;
    84       }
    85       if (logicalProcessors <= 0) {
    86         logicalProcessors = 1;
    87         logicalProcessorsPerCore = 1;
    88       }
    89 
    90       coreCount = logicalProcessors / logicalProcessorsPerCore;
    91 
    92       switch (family) {
    93         case 0x06: {
    94           switch (model) {
    95             case 0x0F: // Intel Core 65nm
    96               switch (stepping) {
    97                 case 0x06: // B2
    98                   switch (coreCount) {
    99                     case 2:
   100                       tjMax = 80; break;
   101                     case 4:
   102                       tjMax = 90; break;
   103                     default:
   104                       tjMax = 85; break;
   105                   }
   106                   tjMax = 80; break;
   107                 case 0x0B: // G0
   108                   tjMax = 90; break;
   109                 case 0x0D: // M0
   110                   tjMax = 85; break;
   111                 default:
   112                   tjMax = 85; break;
   113               } break;            
   114             case 0x17: // Intel Core 45nm
   115               tjMax = 100; break;
   116             case 0x1C: // Intel Atom 
   117               tjMax = 90; break;
   118             case 0x1A:
   119               uint eax = 0, edx = 0;
   120               if (WinRing0.RdmsrPx(
   121                   IA32_TEMPERATURE_TARGET, ref eax, ref edx, (UIntPtr)1)) {
   122                 tjMax = (eax >> 16) & 0xFF;
   123               } else
   124                 tjMax = 100;
   125               break;
   126             default:
   127               tjMax = 100; break;
   128           }
   129         } break;
   130         default: tjMax = 100; break;
   131       }
   132 
   133       totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this);     
   134 
   135       coreTemperatures = new Sensor[coreCount];
   136       coreLoads = new Sensor[coreCount];
   137       for (int i = 0; i < coreTemperatures.Length; i++) {
   138         coreTemperatures[i] = new Sensor("Core #" + (i + 1), i, tjMax,
   139           SensorType.Temperature, this);
   140         coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1, 
   141           SensorType.Load, this);
   142       }
   143 
   144       cpuLoad = new CPULoad(coreCount, logicalProcessorsPerCore);
   145       if (cpuLoad.IsAvailable) {
   146         foreach (Sensor sensor in coreLoads)
   147           ActivateSensor(sensor);
   148         ActivateSensor(totalLoad);
   149       }
   150 
   151       Update();                   
   152     }
   153 
   154     public string Name {
   155       get { return name; }
   156     }
   157 
   158     public string Identifier {
   159       get { return "/intelcpu/0"; }
   160     }
   161 
   162     public Image Icon {
   163       get { return icon; }
   164     }
   165 
   166     public string GetReport() {
   167       StringBuilder r = new StringBuilder();
   168 
   169       r.AppendLine("Intel CPU");
   170       r.AppendLine();
   171       r.AppendFormat("Name: {0}{1}", name, Environment.NewLine);
   172       r.AppendFormat("Number of cores: {0}{1}", coreCount, 
   173         Environment.NewLine);
   174       r.AppendFormat("Threads per core: {0}{1}", logicalProcessorsPerCore,
   175         Environment.NewLine);
   176       r.AppendFormat("TjMax: {0}{1}", tjMax, Environment.NewLine);
   177       r.AppendLine();
   178 
   179       return r.ToString();
   180     }
   181 
   182     public void Update() {
   183 
   184       uint eax = 0, edx = 0;      
   185       for (int i = 0; i < coreTemperatures.Length; i++) {
   186         if (WinRing0.RdmsrPx(
   187           IA32_THERM_STATUS_MSR, ref eax, ref edx, 
   188             (UIntPtr)(1 << (int)(logicalProcessorsPerCore * i)))) 
   189         {
   190           // if reading is valid
   191           if ((eax & 0x80000000) != 0) {
   192             // get the dist from tjMax from bits 22:16
   193             coreTemperatures[i].Value = tjMax - ((eax & 0x007F0000) >> 16);
   194             ActivateSensor(coreTemperatures[i]);
   195           } else {
   196             DeactivateSensor(coreTemperatures[i]);
   197           }
   198         }        
   199       }
   200 
   201       if (cpuLoad.IsAvailable) {
   202         cpuLoad.Update();
   203         for (int i = 0; i < coreLoads.Length; i++)
   204           coreLoads[i].Value = cpuLoad.GetCoreLoad(i);
   205         totalLoad.Value = cpuLoad.GetTotalLoad();
   206       }
   207     }  
   208   }
   209 }