Hardware/CPU/CPUGroup.cs
author moel.mich
Tue, 02 Feb 2010 21:58:54 +0000
changeset 22 e6832d4b89d2
parent 14 51c2f209da6d
child 35 2daf59392c88
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.Text;
    41 
    42 namespace OpenHardwareMonitor.Hardware.CPU {
    43 
    44   public class CPUGroup : IGroup { 
    45     private List<IHardware> hardware = new List<IHardware>();
    46 
    47     private string cpuBrandString;
    48     private string cpuVendor;
    49     private uint[,] cpuidData;
    50     private uint[,] cpuidExtData;
    51 
    52     private uint family;
    53     private uint model;
    54     private uint stepping;
    55 
    56     private static uint CPUID = 0;
    57     private static uint CPUID_EXT = 0x80000000;
    58 
    59     public static void AppendRegister(StringBuilder b, uint value) {
    60       b.Append((char)((value) & 0xff));
    61       b.Append((char)((value >> 8) & 0xff));
    62       b.Append((char)((value >> 16) & 0xff));
    63       b.Append((char)((value >> 24) & 0xff));
    64     }
    65 
    66     public CPUGroup() {
    67 
    68       if (!WinRing0.IsAvailable) 
    69         return;
    70 
    71       if (WinRing0.IsCpuid()) {
    72         uint maxCPUID = 0;
    73         uint maxCPUID_EXT = 0;
    74         uint eax, ebx, ecx, edx;
    75 
    76         
    77         if (WinRing0.Cpuid(CPUID, out eax, out ebx, out ecx, out edx)) {
    78           maxCPUID = eax;
    79           StringBuilder vendorBuilder = new StringBuilder();
    80           AppendRegister(vendorBuilder, ebx);
    81           AppendRegister(vendorBuilder, edx);
    82           AppendRegister(vendorBuilder, ecx);
    83           cpuVendor = vendorBuilder.ToString();
    84 
    85           eax = ebx = ecx = edx = 0;
    86           if (WinRing0.Cpuid(CPUID_EXT, out eax, out ebx, out ecx, out edx)) {
    87             maxCPUID_EXT = eax - CPUID_EXT;
    88           }
    89         }
    90         if (maxCPUID == 0 || maxCPUID_EXT == 0)
    91           return;        
    92 
    93         cpuidData = new uint[maxCPUID + 1, 4];
    94         for (uint i = 0; i < (maxCPUID + 1); i++)
    95           WinRing0.Cpuid(CPUID + i, out cpuidData[i, 0], out cpuidData[i, 1],
    96             out cpuidData[i, 2], out cpuidData[i, 3]);
    97 
    98         cpuidExtData = new uint[maxCPUID_EXT + 1, 4];
    99         for (uint i = 0; i < (maxCPUID_EXT + 1); i++)
   100           WinRing0.Cpuid(CPUID_EXT + i, out cpuidExtData[i, 0],
   101             out cpuidExtData[i, 1], out cpuidExtData[i, 2],
   102             out cpuidExtData[i, 3]);
   103 
   104         StringBuilder nameBuilder = new StringBuilder();
   105         for (uint i = 2; i <= 4; i++) {
   106           if (WinRing0.Cpuid(CPUID_EXT + i, out eax, out ebx, out ecx, out edx)) 
   107           {
   108             AppendRegister(nameBuilder, eax);
   109             AppendRegister(nameBuilder, ebx);
   110             AppendRegister(nameBuilder, ecx);
   111             AppendRegister(nameBuilder, edx);
   112           }
   113         }
   114         cpuBrandString = nameBuilder.ToString().Trim('\0');
   115         nameBuilder.Replace("(R)", " ");
   116         nameBuilder.Replace("(TM)", " ");
   117         nameBuilder.Replace("(tm)", " ");
   118         nameBuilder.Replace("CPU", "");
   119         for (int i = 0; i < 10; i++) nameBuilder.Replace("  ", " ");
   120         string name = nameBuilder.ToString();
   121         if (name.Contains("@"))
   122           name = name.Remove(name.LastIndexOf('@')); 
   123         name = name.Trim();
   124 
   125         this.family = ((cpuidData[1, 0] & 0x0FF00000) >> 20) +
   126           ((cpuidData[1, 0] & 0x0F00) >> 8);
   127         this.model = ((cpuidData[1, 0] & 0x0F0000) >> 12) +
   128           ((cpuidData[1, 0] & 0xF0) >> 4);
   129         this.stepping = (cpuidData[1, 0] & 0x0F);
   130 
   131         switch (cpuVendor) {
   132           case "GenuineIntel":
   133             // check if processor supports a digital thermal sensor
   134             if (maxCPUID >= 6 && (cpuidData[6, 0] & 1) != 0) 
   135               hardware.Add(new IntelCPU(name, family, model, stepping, 
   136                 cpuidData, cpuidExtData));
   137             break;
   138           case "AuthenticAMD":                       
   139             // check if processor supports a digital thermal sensor            
   140             if (maxCPUID_EXT >= 7 && (cpuidExtData[7, 3] & 1) != 0) {
   141               switch (family) {
   142                 case 0x0F:
   143                   hardware.Add(new AMD0FCPU(name, family, model, stepping,
   144                     cpuidData, cpuidExtData));
   145                   break;
   146                 case 0x10:
   147                   hardware.Add(new AMD10CPU(name, family, model, stepping,
   148                     cpuidData, cpuidExtData));
   149                   break;
   150                 default:
   151                   break;
   152               }
   153             }
   154             break;
   155           default:
   156             break;
   157         }
   158       }
   159     }
   160 
   161     public IHardware[] Hardware {
   162       get {
   163         return hardware.ToArray();
   164       }
   165     }
   166 
   167     private void AppendCpuidData(StringBuilder r, uint[,] data, uint offset) {
   168       for (int i = 0; i < data.GetLength(0); i++) {
   169         r.Append(" ");
   170         r.Append((i + offset).ToString("X8"));
   171         for (int j = 0; j < 4; j++) {
   172           r.Append("  ");
   173           r.Append(data[i, j].ToString("X8"));
   174         }
   175         r.AppendLine();
   176       }
   177     }
   178 
   179     public string GetReport() {
   180 
   181       StringBuilder r = new StringBuilder();
   182 
   183       r.AppendLine("CPUID");
   184       r.AppendLine();
   185       r.AppendFormat("Processor Vendor: {0}{1}", cpuVendor, 
   186         Environment.NewLine);
   187       r.AppendFormat("Processor Brand: {0}{1}", cpuBrandString, 
   188         Environment.NewLine);
   189       r.AppendFormat("Family: 0x{0}{1}", family.ToString("X"),
   190         Environment.NewLine);
   191       r.AppendFormat("Model: 0x{0}{1}", model.ToString("X"),
   192         Environment.NewLine);
   193       r.AppendFormat("Stepping: 0x{0}{1}", stepping.ToString("X"),
   194         Environment.NewLine);
   195       r.AppendLine();
   196 
   197       r.AppendLine("CPUID Return Values");
   198       r.AppendLine();
   199 
   200       if (cpuidData != null) {
   201         r.AppendLine(" Function  EAX       EBX       ECX       EDX");
   202         AppendCpuidData(r, cpuidData, CPUID);
   203         AppendCpuidData(r, cpuidExtData, CPUID_EXT);
   204         r.AppendLine();
   205       }
   206 
   207       return r.ToString();
   208     }
   209 
   210     public void Close() { }
   211   }
   212 }