Hardware/CPU/CPUGroup.cs
author moel.mich
Tue, 27 Apr 2010 07:31:46 +0000
changeset 102 9620449d2620
parent 100 25d18bbaa9cf
child 125 b1278888d9a9
permissions -rw-r--r--
Another small fix of the coreId calculation for multi-CPU systems.
     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   public class CPUGroup : IGroup { 
    46     private List<IHardware> hardware = new List<IHardware>();
    47 
    48     private CPUID[][][] threads;
    49 
    50     private 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 (ArgumentException) { }
    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 CPUID[][] GroupThreadsByCore(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() {
   104       if (!WinRing0.IsCpuid())
   105         return;
   106 
   107       CPUID[][] processorThreads = GetProcessorThreads();
   108       this.threads = new CPUID[processorThreads.Length][][];
   109 
   110       int index = 0;
   111       foreach (CPUID[] threads in processorThreads) {
   112         if (threads.Length == 0)
   113           continue;
   114             
   115         CPUID[][] coreThreads = GroupThreadsByCore(threads);
   116 
   117         this.threads[index] = coreThreads;        
   118 
   119         switch (threads[0].Vendor) {
   120           case Vendor.Intel:
   121             hardware.Add(new IntelCPU(index, coreThreads));
   122             break;
   123           case Vendor.AMD:
   124             switch (threads[0].Family) {
   125               case 0x0F:
   126                 hardware.Add(new AMD0FCPU(index, coreThreads));
   127                 break;
   128               case 0x10:
   129                 hardware.Add(new AMD10CPU(index, coreThreads));
   130                 break;
   131               default:
   132                 break;
   133             } break;
   134           default:
   135             break;
   136         }
   137 
   138         index++;
   139       }
   140     }
   141     
   142     public IHardware[] Hardware {
   143       get {
   144         return hardware.ToArray();
   145       }
   146     }
   147 
   148     private void AppendCpuidData(StringBuilder r, uint[,] data, uint offset) {
   149       for (int i = 0; i < data.GetLength(0); i++) {
   150         r.Append(" ");
   151         r.Append((i + offset).ToString("X8"));
   152         for (int j = 0; j < 4; j++) {
   153           r.Append("  ");
   154           r.Append(data[i, j].ToString("X8"));
   155         }
   156         r.AppendLine();
   157       }
   158     }
   159 
   160     public string GetReport() {
   161 
   162       StringBuilder r = new StringBuilder();
   163       
   164       r.AppendLine("CPUID");
   165       r.AppendLine();
   166 
   167       for (int i = 0; i < threads.Length; i++) {
   168 
   169         r.AppendLine("Processor " + i);
   170         r.AppendLine();
   171         r.AppendFormat("Processor Vendor: {0}{1}", threads[i][0][0].Vendor,
   172           Environment.NewLine);
   173         r.AppendFormat("Processor Brand: {0}{1}", threads[i][0][0].BrandString,
   174           Environment.NewLine);
   175         r.AppendFormat("Family: 0x{0}{1}", 
   176           threads[i][0][0].Family.ToString("X"), Environment.NewLine);
   177         r.AppendFormat("Model: 0x{0}{1}", 
   178           threads[i][0][0].Model.ToString("X"), Environment.NewLine);
   179         r.AppendFormat("Stepping: 0x{0}{1}", 
   180           threads[i][0][0].Stepping.ToString("X"), Environment.NewLine);
   181         r.AppendLine();
   182 
   183         r.AppendLine("CPUID Return Values");
   184         r.AppendLine();
   185         for (int j = 0; j < threads[i].Length; j++)
   186           for (int k = 0; k < threads[i][j].Length; k++) {
   187             r.AppendLine(" CPU Thread: " + threads[i][j][k].Thread);
   188             r.AppendLine(" APIC ID: " + threads[i][j][k].ApicId);
   189             r.AppendLine(" Processor ID: " + threads[i][j][k].ProcessorId);
   190             r.AppendLine(" Core ID: " + threads[i][j][k].CoreId);
   191             r.AppendLine(" Thread ID: " + threads[i][j][k].ThreadId);
   192             r.AppendLine();
   193             r.AppendLine(" Function  EAX       EBX       ECX       EDX");
   194             AppendCpuidData(r, threads[i][j][k].Data, CPUID.CPUID_0);
   195             AppendCpuidData(r, threads[i][j][k].ExtData, CPUID.CPUID_EXT);
   196             r.AppendLine();
   197           }
   198       }
   199       return r.ToString(); 
   200     }
   201 
   202     public void Close() { }
   203   }
   204 }