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