Hardware/CPU/AMD10CPU.cs
author moel.mich
Sun, 08 Aug 2010 13:57:26 +0000
changeset 165 813d8bc3192f
parent 134 8b3b9b2e28e5
child 166 fa9dfbfc4145
permissions -rw-r--r--
Refactored the hardware monitoring code into a library (Issue 101).
     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.Diagnostics;
    41 using System.Text;
    42 
    43 namespace OpenHardwareMonitor.Hardware.CPU {
    44 
    45   internal class AMD10CPU : Hardware, IHardware {
    46     private string name;
    47 
    48     private int processorIndex;
    49     private uint pciAddress;
    50 
    51     private Sensor coreTemperature;
    52     private Sensor totalLoad;
    53     private Sensor[] coreLoads;
    54 
    55     private CPULoad cpuLoad;
    56 
    57     private const ushort PCI_AMD_VENDOR_ID = 0x1022;
    58     private const ushort PCI_AMD_10H_MISCELLANEOUS_DEVICE_ID = 0x1203;
    59     private const ushort PCI_AMD_11H_MISCELLANEOUS_DEVICE_ID = 0x1303;
    60     private const uint REPORTED_TEMPERATURE_CONTROL_REGISTER = 0xA4;
    61 
    62     public AMD10CPU(int processorIndex, CPUID[][] cpuid, ISettings settings) {
    63 
    64       this.processorIndex = processorIndex;
    65       this.name = cpuid[0][0].Name;
    66 
    67       int coreCount = cpuid.Length;
    68 
    69       totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this, settings);
    70 
    71       coreLoads = new Sensor[coreCount];
    72       for (int i = 0; i < coreCount; i++) 
    73         coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1,
    74           SensorType.Load, this, settings);
    75 
    76       cpuLoad = new CPULoad(cpuid);
    77       if (cpuLoad.IsAvailable) {
    78         foreach (Sensor sensor in coreLoads)
    79           ActivateSensor(sensor);
    80         ActivateSensor(totalLoad);
    81       }    
    82       
    83       // AMD family 10h processors support only one temperature sensor
    84       coreTemperature = new Sensor(
    85         "Core" + (coreCount > 1 ? " #1 - #" + coreCount : ""), 0,
    86         SensorType.Temperature, this, new ParameterDescription[] {
    87             new ParameterDescription("Offset [°C]", "Temperature offset.", 0)
    88           }, settings);
    89 
    90       pciAddress = WinRing0.FindPciDeviceById(PCI_AMD_VENDOR_ID,
    91         PCI_AMD_10H_MISCELLANEOUS_DEVICE_ID, (byte)processorIndex);
    92       if (pciAddress == 0xFFFFFFFF) 
    93         pciAddress = WinRing0.FindPciDeviceById(PCI_AMD_VENDOR_ID,
    94           PCI_AMD_11H_MISCELLANEOUS_DEVICE_ID, (byte)processorIndex);
    95 
    96       Update();                   
    97     }
    98 
    99     public override string Name {
   100       get { return name; }
   101     }
   102 
   103     public override Identifier Identifier {
   104       get { return new Identifier("amdcpu", processorIndex.ToString()); }
   105     }
   106 
   107     public override HardwareType HardwareType {
   108       get { return HardwareType.CPU; }
   109     }
   110 
   111     public override void Update() {
   112       if (pciAddress != 0xFFFFFFFF) {
   113         uint value;
   114         if (WinRing0.ReadPciConfigDwordEx(pciAddress,
   115           REPORTED_TEMPERATURE_CONTROL_REGISTER, out value)) {
   116           coreTemperature.Value = ((value >> 21) & 0x7FF) / 8.0f +
   117             coreTemperature.Parameters[0].Value;
   118           ActivateSensor(coreTemperature);
   119         } else {
   120           DeactivateSensor(coreTemperature);
   121         }
   122       }
   123 
   124       if (cpuLoad.IsAvailable) {
   125         cpuLoad.Update();
   126         for (int i = 0; i < coreLoads.Length; i++)
   127           coreLoads[i].Value = cpuLoad.GetCoreLoad(i);
   128         totalLoad.Value = cpuLoad.GetTotalLoad();
   129       }
   130     }
   131   }
   132 }