Hardware/CPU/CPUGroup.cs
author moel.mich
Sat, 16 Apr 2011 14:49:47 +0000
changeset 272 037a2d66082f
parent 266 2687ac753d90
child 301 d14ce71cef44
permissions -rw-r--r--
A small correction for AMD K8 (family 0Fh) core temperature reading, and fan reading on older revision of IT8712F chips. This should fix Issue 42 and Issue 194.
     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-2011
    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.Globalization;
    41 using System.Text;
    42 
    43 namespace OpenHardwareMonitor.Hardware.CPU {
    44 
    45   internal class CPUGroup : IGroup {
    46     private readonly List<GenericCPU> hardware = new List<GenericCPU>();
    47 
    48     private readonly CPUID[][][] threads;
    49 
    50     private static CPUID[][] GetProcessorThreads() {
    51 
    52       List<CPUID> threads = new List<CPUID>();
    53       for (int i = 0; i < 32; i++) {
    54         try {
    55           threads.Add(new CPUID(i));
    56         } catch (ArgumentOutOfRangeException) { }
    57       }
    58 
    59       SortedDictionary<uint, List<CPUID>> processors =
    60         new SortedDictionary<uint, List<CPUID>>();
    61       foreach (CPUID thread in threads) {
    62         List<CPUID> list;
    63         processors.TryGetValue(thread.ProcessorId, out list);
    64         if (list == null) {
    65           list = new List<CPUID>();
    66           processors.Add(thread.ProcessorId, list);
    67         }
    68         list.Add(thread);
    69       }
    70 
    71       CPUID[][] processorThreads = new CPUID[processors.Count][];
    72       int index = 0;
    73       foreach (List<CPUID> list in processors.Values) {
    74         processorThreads[index] = list.ToArray();
    75         index++;
    76       }
    77       return processorThreads;
    78     }
    79 
    80     private static CPUID[][] GroupThreadsByCore(IEnumerable<CPUID> threads) {
    81 
    82       SortedDictionary<uint, List<CPUID>> cores = 
    83         new SortedDictionary<uint, List<CPUID>>();
    84       foreach (CPUID thread in threads) {
    85         List<CPUID> coreList;
    86         cores.TryGetValue(thread.CoreId, out coreList);
    87         if (coreList == null) {
    88           coreList = new List<CPUID>();
    89           cores.Add(thread.CoreId, coreList);
    90         }
    91         coreList.Add(thread);
    92       }
    93 
    94       CPUID[][] coreThreads = new CPUID[cores.Count][];
    95       int index = 0;
    96       foreach (List<CPUID> list in cores.Values) {
    97         coreThreads[index] = list.ToArray();
    98         index++;
    99       }
   100       return coreThreads;
   101     }
   102 
   103     public CPUGroup(ISettings settings) {
   104 
   105       CPUID[][] processorThreads = GetProcessorThreads();
   106       this.threads = new CPUID[processorThreads.Length][][];
   107 
   108       int index = 0;
   109       foreach (CPUID[] threads in processorThreads) {
   110         if (threads.Length == 0)
   111           continue;
   112             
   113         CPUID[][] coreThreads = GroupThreadsByCore(threads);
   114 
   115         this.threads[index] = coreThreads;
   116 
   117         switch (threads[0].Vendor) {
   118           case Vendor.Intel:
   119             hardware.Add(new IntelCPU(index, coreThreads, settings));
   120             break;
   121           case Vendor.AMD:
   122             switch (threads[0].Family) {
   123               case 0x0F:
   124                 hardware.Add(new AMD0FCPU(index, coreThreads, settings));
   125                 break;
   126               case 0x10:
   127               case 0x11:
   128               case 0x14:
   129                 hardware.Add(new AMD10CPU(index, coreThreads, settings));
   130                 break;
   131               default:
   132                 hardware.Add(new GenericCPU(index, coreThreads, settings));
   133                 break;
   134             } break;
   135           default:
   136             hardware.Add(new GenericCPU(index, coreThreads, settings));
   137             break;
   138         }
   139 
   140         index++;
   141       }
   142     }
   143     
   144     public IHardware[] Hardware {
   145       get {
   146         return hardware.ToArray();
   147       }
   148     }
   149 
   150     private static void AppendCpuidData(StringBuilder r, uint[,] data, 
   151       uint offset) 
   152     {
   153       for (int i = 0; i < data.GetLength(0); i++) {
   154         r.Append(" ");
   155         r.Append((i + offset).ToString("X8", CultureInfo.InvariantCulture));
   156         for (int j = 0; j < 4; j++) {
   157           r.Append("  ");
   158           r.Append(data[i, j].ToString("X8", CultureInfo.InvariantCulture));
   159         }
   160         r.AppendLine();
   161       }
   162     }
   163 
   164     public string GetReport() {
   165       if (threads == null)
   166         return null;
   167       
   168       StringBuilder r = new StringBuilder();
   169       
   170       r.AppendLine("CPUID");
   171       r.AppendLine();
   172 
   173       for (int i = 0; i < threads.Length; i++) {
   174 
   175         r.AppendLine("Processor " + i);
   176         r.AppendLine();
   177         r.AppendFormat("Processor Vendor: {0}{1}", threads[i][0][0].Vendor,
   178           Environment.NewLine);
   179         r.AppendFormat("Processor Brand: {0}{1}", threads[i][0][0].BrandString,
   180           Environment.NewLine);
   181         r.AppendFormat("Family: 0x{0}{1}", 
   182           threads[i][0][0].Family.ToString("X", CultureInfo.InvariantCulture), 
   183           Environment.NewLine);
   184         r.AppendFormat("Model: 0x{0}{1}",
   185           threads[i][0][0].Model.ToString("X", CultureInfo.InvariantCulture), 
   186           Environment.NewLine);
   187         r.AppendFormat("Stepping: 0x{0}{1}",
   188           threads[i][0][0].Stepping.ToString("X", CultureInfo.InvariantCulture), 
   189           Environment.NewLine);
   190         r.AppendLine();
   191 
   192         r.AppendLine("CPUID Return Values");
   193         r.AppendLine();
   194         for (int j = 0; j < threads[i].Length; j++)
   195           for (int k = 0; k < threads[i][j].Length; k++) {
   196             r.AppendLine(" CPU Thread: " + threads[i][j][k].Thread);
   197             r.AppendLine(" APIC ID: " + threads[i][j][k].ApicId);
   198             r.AppendLine(" Processor ID: " + threads[i][j][k].ProcessorId);
   199             r.AppendLine(" Core ID: " + threads[i][j][k].CoreId);
   200             r.AppendLine(" Thread ID: " + threads[i][j][k].ThreadId);
   201             r.AppendLine();
   202             r.AppendLine(" Function  EAX       EBX       ECX       EDX");
   203             AppendCpuidData(r, threads[i][j][k].Data, CPUID.CPUID_0);
   204             AppendCpuidData(r, threads[i][j][k].ExtData, CPUID.CPUID_EXT);
   205             r.AppendLine();
   206           }
   207       }
   208       return r.ToString(); 
   209     }
   210 
   211     public void Close() {
   212       foreach (GenericCPU cpu in hardware) {
   213         cpu.Close();
   214       }
   215     }
   216   }
   217 }