Hardware/CPU/CPUGroup.cs
author moel.mich
Thu, 12 Aug 2010 20:53:27 +0000
changeset 166 fa9dfbfc4145
parent 165 813d8bc3192f
child 167 b7cc9d09aefe
permissions -rw-r--r--
Changed the project files to Visual Studio 2010. Fixed some Code Analysis warnings.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
moel@1
    22
  Contributor(s):
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@166
    40
using System.Globalization;
moel@90
    41
using System.Diagnostics;
moel@1
    42
using System.Text;
moel@1
    43
moel@1
    44
namespace OpenHardwareMonitor.Hardware.CPU {
moel@1
    45
moel@165
    46
  internal class CPUGroup : IGroup { 
moel@1
    47
    private List<IHardware> hardware = new List<IHardware>();
moel@1
    48
moel@90
    49
    private CPUID[][][] threads;
moel@1
    50
moel@90
    51
    private CPUID[][] GetProcessorThreads() {
moel@1
    52
moel@90
    53
      List<CPUID> threads = new List<CPUID>();
moel@90
    54
      for (int i = 0; i < 32; i++) {
moel@90
    55
        try {
moel@90
    56
          threads.Add(new CPUID(i));
moel@90
    57
        } catch (ArgumentException) { }
moel@90
    58
      }
moel@1
    59
moel@90
    60
      SortedDictionary<uint, List<CPUID>> processors =
moel@90
    61
        new SortedDictionary<uint, List<CPUID>>();
moel@90
    62
      foreach (CPUID thread in threads) {
moel@90
    63
        List<CPUID> list;
moel@90
    64
        processors.TryGetValue(thread.ProcessorId, out list);
moel@90
    65
        if (list == null) {
moel@90
    66
          list = new List<CPUID>();
moel@90
    67
          processors.Add(thread.ProcessorId, list);
moel@90
    68
        }
moel@90
    69
        list.Add(thread);
moel@90
    70
      }
moel@90
    71
moel@90
    72
      CPUID[][] processorThreads = new CPUID[processors.Count][];
moel@90
    73
      int index = 0;
moel@90
    74
      foreach (List<CPUID> list in processors.Values) {
moel@90
    75
        processorThreads[index] = list.ToArray();
moel@90
    76
        index++;
moel@90
    77
      }
moel@90
    78
      return processorThreads;
moel@90
    79
    }
moel@90
    80
moel@90
    81
    private CPUID[][] GroupThreadsByCore(CPUID[] threads) {
moel@90
    82
moel@90
    83
      SortedDictionary<uint, List<CPUID>> cores = 
moel@90
    84
        new SortedDictionary<uint, List<CPUID>>();
moel@90
    85
      foreach (CPUID thread in threads) {
moel@90
    86
        List<CPUID> coreList;
moel@90
    87
        cores.TryGetValue(thread.CoreId, out coreList);
moel@90
    88
        if (coreList == null) {
moel@90
    89
          coreList = new List<CPUID>();
moel@90
    90
          cores.Add(thread.CoreId, coreList);
moel@90
    91
        }
moel@90
    92
        coreList.Add(thread);
moel@90
    93
      }
moel@90
    94
moel@90
    95
      CPUID[][] coreThreads = new CPUID[cores.Count][];
moel@90
    96
      int index = 0;
moel@90
    97
      foreach (List<CPUID> list in cores.Values) {
moel@90
    98
        coreThreads[index] = list.ToArray();
moel@90
    99
        index++;
moel@90
   100
      }
moel@90
   101
      return coreThreads;
moel@1
   102
    }
moel@1
   103
moel@165
   104
    public CPUGroup(ISettings settings) {
moel@125
   105
      // No implementation for cpuid on Unix systems
moel@125
   106
      int p = (int)System.Environment.OSVersion.Platform;
moel@125
   107
      if ((p == 4) || (p == 128)) 
moel@125
   108
        return;
moel@125
   109
      
moel@90
   110
      if (!WinRing0.IsCpuid())
moel@1
   111
        return;
moel@1
   112
moel@90
   113
      CPUID[][] processorThreads = GetProcessorThreads();
moel@90
   114
      this.threads = new CPUID[processorThreads.Length][][];
moel@1
   115
moel@90
   116
      int index = 0;
moel@90
   117
      foreach (CPUID[] threads in processorThreads) {
moel@90
   118
        if (threads.Length == 0)
moel@90
   119
          continue;
moel@90
   120
            
moel@90
   121
        CPUID[][] coreThreads = GroupThreadsByCore(threads);
moel@1
   122
moel@101
   123
        this.threads[index] = coreThreads;        
moel@1
   124
moel@90
   125
        switch (threads[0].Vendor) {
moel@90
   126
          case Vendor.Intel:
moel@165
   127
            hardware.Add(new IntelCPU(index, coreThreads, settings));
moel@1
   128
            break;
moel@90
   129
          case Vendor.AMD:
moel@90
   130
            switch (threads[0].Family) {
moel@90
   131
              case 0x0F:
moel@165
   132
                hardware.Add(new AMD0FCPU(index, coreThreads, settings));
moel@90
   133
                break;
moel@90
   134
              case 0x10:
moel@165
   135
                hardware.Add(new AMD10CPU(index, coreThreads, settings));
moel@90
   136
                break;
moel@90
   137
              default:
moel@90
   138
                break;
moel@90
   139
            } break;
moel@1
   140
          default:
moel@1
   141
            break;
moel@101
   142
        }
moel@101
   143
moel@101
   144
        index++;
moel@1
   145
      }
moel@1
   146
    }
moel@90
   147
    
moel@1
   148
    public IHardware[] Hardware {
moel@1
   149
      get {
moel@1
   150
        return hardware.ToArray();
moel@1
   151
      }
moel@1
   152
    }
moel@1
   153
moel@1
   154
    private void AppendCpuidData(StringBuilder r, uint[,] data, uint offset) {
moel@1
   155
      for (int i = 0; i < data.GetLength(0); i++) {
moel@1
   156
        r.Append(" ");
moel@166
   157
        r.Append((i + offset).ToString("X8", CultureInfo.InvariantCulture));
moel@1
   158
        for (int j = 0; j < 4; j++) {
moel@1
   159
          r.Append("  ");
moel@166
   160
          r.Append(data[i, j].ToString("X8", CultureInfo.InvariantCulture));
moel@1
   161
        }
moel@1
   162
        r.AppendLine();
moel@1
   163
      }
moel@1
   164
    }
moel@1
   165
moel@1
   166
    public string GetReport() {
moel@125
   167
      if (threads == null)
moel@125
   168
        return null;
moel@125
   169
      
moel@1
   170
      StringBuilder r = new StringBuilder();
moel@90
   171
      
moel@1
   172
      r.AppendLine("CPUID");
moel@1
   173
      r.AppendLine();
moel@1
   174
moel@90
   175
      for (int i = 0; i < threads.Length; i++) {
moel@1
   176
moel@90
   177
        r.AppendLine("Processor " + i);
moel@18
   178
        r.AppendLine();
moel@90
   179
        r.AppendFormat("Processor Vendor: {0}{1}", threads[i][0][0].Vendor,
moel@90
   180
          Environment.NewLine);
moel@90
   181
        r.AppendFormat("Processor Brand: {0}{1}", threads[i][0][0].BrandString,
moel@90
   182
          Environment.NewLine);
moel@90
   183
        r.AppendFormat("Family: 0x{0}{1}", 
moel@166
   184
          threads[i][0][0].Family.ToString("X", CultureInfo.InvariantCulture), 
moel@166
   185
          Environment.NewLine);
moel@166
   186
        r.AppendFormat("Model: 0x{0}{1}",
moel@166
   187
          threads[i][0][0].Model.ToString("X", CultureInfo.InvariantCulture), 
moel@166
   188
          Environment.NewLine);
moel@166
   189
        r.AppendFormat("Stepping: 0x{0}{1}",
moel@166
   190
          threads[i][0][0].Stepping.ToString("X", CultureInfo.InvariantCulture), 
moel@166
   191
          Environment.NewLine);
moel@90
   192
        r.AppendLine();
moel@90
   193
moel@90
   194
        r.AppendLine("CPUID Return Values");
moel@90
   195
        r.AppendLine();
moel@90
   196
        for (int j = 0; j < threads[i].Length; j++)
moel@90
   197
          for (int k = 0; k < threads[i][j].Length; k++) {
moel@90
   198
            r.AppendLine(" CPU Thread: " + threads[i][j][k].Thread);
moel@90
   199
            r.AppendLine(" APIC ID: " + threads[i][j][k].ApicId);
moel@90
   200
            r.AppendLine(" Processor ID: " + threads[i][j][k].ProcessorId);
moel@90
   201
            r.AppendLine(" Core ID: " + threads[i][j][k].CoreId);
moel@90
   202
            r.AppendLine(" Thread ID: " + threads[i][j][k].ThreadId);
moel@90
   203
            r.AppendLine();
moel@90
   204
            r.AppendLine(" Function  EAX       EBX       ECX       EDX");
moel@90
   205
            AppendCpuidData(r, threads[i][j][k].Data, CPUID.CPUID_0);
moel@90
   206
            AppendCpuidData(r, threads[i][j][k].ExtData, CPUID.CPUID_EXT);
moel@90
   207
            r.AppendLine();
moel@90
   208
          }
moel@18
   209
      }
moel@90
   210
      return r.ToString(); 
moel@1
   211
    }
moel@1
   212
moel@1
   213
    public void Close() { }
moel@1
   214
  }
moel@1
   215
}