2010-04-24 19:59:52 +00:00
|
|
|
|
/*
|
|
|
|
|
|
2012-05-27 14:23:31 +00:00
|
|
|
|
This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2020-02-08 00:36:51 +01:00
|
|
|
|
Copyright (C) 2009-2020 Michael Möller <mmoeller@openhardwaremonitor.org>
|
2012-05-27 14:23:31 +00:00
|
|
|
|
|
2010-04-24 19:59:52 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace OpenHardwareMonitor.Hardware.CPU {
|
2010-08-08 13:57:26 +00:00
|
|
|
|
|
|
|
|
|
internal enum Vendor {
|
2010-04-24 19:59:52 +00:00
|
|
|
|
Unknown,
|
|
|
|
|
Intel,
|
|
|
|
|
AMD,
|
|
|
|
|
}
|
2010-08-08 13:57:26 +00:00
|
|
|
|
|
|
|
|
|
internal class CPUID {
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2020-05-04 23:11:33 +02:00
|
|
|
|
private readonly int group;
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly int thread;
|
2020-05-04 23:11:33 +02:00
|
|
|
|
private readonly GroupAffinity affinity;
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly Vendor vendor = Vendor.Unknown;
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly string cpuBrandString = "";
|
|
|
|
|
private readonly string name = "";
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly uint[,] cpuidData = new uint[0, 0];
|
|
|
|
|
private readonly uint[,] cpuidExtData = new uint[0, 0];
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly uint family;
|
|
|
|
|
private readonly uint model;
|
|
|
|
|
private readonly uint stepping;
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly uint apicId;
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly uint threadMaskWith;
|
|
|
|
|
private readonly uint coreMaskWith;
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private readonly uint processorId;
|
|
|
|
|
private readonly uint coreId;
|
|
|
|
|
private readonly uint threadId;
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
public const uint CPUID_0 = 0;
|
|
|
|
|
public const uint CPUID_EXT = 0x80000000;
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-08-15 14:46:58 +00:00
|
|
|
|
private static void AppendRegister(StringBuilder b, uint value) {
|
2010-04-24 19:59:52 +00:00
|
|
|
|
b.Append((char)((value) & 0xff));
|
|
|
|
|
b.Append((char)((value >> 8) & 0xff));
|
|
|
|
|
b.Append((char)((value >> 16) & 0xff));
|
|
|
|
|
b.Append((char)((value >> 24) & 0xff));
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-15 14:46:58 +00:00
|
|
|
|
private static uint NextLog2(long x) {
|
2010-04-30 20:53:40 +00:00
|
|
|
|
if (x <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
x--;
|
|
|
|
|
uint count = 0;
|
|
|
|
|
while (x > 0) {
|
|
|
|
|
x >>= 1;
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 23:11:33 +02:00
|
|
|
|
public static CPUID Get(int group, int thread) {
|
|
|
|
|
if (thread >= 64)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var affinity = GroupAffinity.Single((ushort)group, thread);
|
|
|
|
|
|
|
|
|
|
var previousAffinity = ThreadAffinity.Set(affinity);
|
|
|
|
|
if (previousAffinity == GroupAffinity.Undefined)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return new CPUID(group, thread, affinity);
|
|
|
|
|
} finally {
|
|
|
|
|
ThreadAffinity.Set(previousAffinity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CPUID(int group, int thread, GroupAffinity affinity) {
|
|
|
|
|
this.group = group;
|
2010-04-24 19:59:52 +00:00
|
|
|
|
this.thread = thread;
|
2020-05-04 23:11:33 +02:00
|
|
|
|
this.affinity = affinity;
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-05-22 12:12:50 +00:00
|
|
|
|
uint maxCpuid = 0;
|
|
|
|
|
uint maxCpuidExt = 0;
|
|
|
|
|
|
2010-04-24 19:59:52 +00:00
|
|
|
|
uint eax, ebx, ecx, edx;
|
|
|
|
|
|
2020-05-04 23:11:33 +02:00
|
|
|
|
Opcode.Cpuid(CPUID_0, 0, out eax, out ebx, out ecx, out edx);
|
|
|
|
|
if (eax > 0)
|
|
|
|
|
maxCpuid = eax;
|
|
|
|
|
else
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
StringBuilder vendorBuilder = new StringBuilder();
|
|
|
|
|
AppendRegister(vendorBuilder, ebx);
|
|
|
|
|
AppendRegister(vendorBuilder, edx);
|
|
|
|
|
AppendRegister(vendorBuilder, ecx);
|
|
|
|
|
string cpuVendor = vendorBuilder.ToString();
|
|
|
|
|
switch (cpuVendor) {
|
|
|
|
|
case "GenuineIntel":
|
|
|
|
|
vendor = Vendor.Intel;
|
|
|
|
|
break;
|
|
|
|
|
case "AuthenticAMD":
|
|
|
|
|
vendor = Vendor.AMD;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
vendor = Vendor.Unknown;
|
|
|
|
|
break;
|
2010-04-24 19:59:52 +00:00
|
|
|
|
}
|
2020-05-04 23:11:33 +02:00
|
|
|
|
eax = ebx = ecx = edx = 0;
|
|
|
|
|
Opcode.Cpuid(CPUID_EXT, 0, out eax, out ebx, out ecx, out edx);
|
|
|
|
|
if (eax > CPUID_EXT)
|
|
|
|
|
maxCpuidExt = eax - CPUID_EXT;
|
|
|
|
|
else
|
|
|
|
|
return;
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-05-15 10:38:47 +00:00
|
|
|
|
maxCpuid = Math.Min(maxCpuid, 1024);
|
2020-05-04 23:11:33 +02:00
|
|
|
|
maxCpuidExt = Math.Min(maxCpuidExt, 1024);
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
|
|
|
|
cpuidData = new uint[maxCpuid + 1, 4];
|
|
|
|
|
for (uint i = 0; i < (maxCpuid + 1); i++)
|
2020-05-04 23:11:33 +02:00
|
|
|
|
Opcode.Cpuid(CPUID_0 + i, 0,
|
2010-04-24 19:59:52 +00:00
|
|
|
|
out cpuidData[i, 0], out cpuidData[i, 1],
|
2020-05-04 23:11:33 +02:00
|
|
|
|
out cpuidData[i, 2], out cpuidData[i, 3]);
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
2010-04-25 12:35:16 +00:00
|
|
|
|
cpuidExtData = new uint[maxCpuidExt + 1, 4];
|
|
|
|
|
for (uint i = 0; i < (maxCpuidExt + 1); i++)
|
2020-05-04 23:11:33 +02:00
|
|
|
|
Opcode.Cpuid(CPUID_EXT + i, 0,
|
|
|
|
|
out cpuidExtData[i, 0], out cpuidExtData[i, 1],
|
|
|
|
|
out cpuidExtData[i, 2], out cpuidExtData[i, 3]);
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
|
|
|
|
StringBuilder nameBuilder = new StringBuilder();
|
|
|
|
|
for (uint i = 2; i <= 4; i++) {
|
2020-05-04 23:11:33 +02:00
|
|
|
|
Opcode.Cpuid(CPUID_EXT + i, 0, out eax, out ebx, out ecx, out edx);
|
|
|
|
|
AppendRegister(nameBuilder, eax);
|
|
|
|
|
AppendRegister(nameBuilder, ebx);
|
|
|
|
|
AppendRegister(nameBuilder, ecx);
|
|
|
|
|
AppendRegister(nameBuilder, edx);
|
2010-04-24 19:59:52 +00:00
|
|
|
|
}
|
|
|
|
|
nameBuilder.Replace('\0', ' ');
|
2020-05-04 23:11:33 +02:00
|
|
|
|
cpuBrandString = nameBuilder.ToString().Trim();
|
2020-02-22 23:57:07 +01:00
|
|
|
|
nameBuilder.Replace("Dual-Core Processor", "");
|
|
|
|
|
nameBuilder.Replace("Triple-Core Processor", "");
|
2012-01-02 20:45:18 +00:00
|
|
|
|
nameBuilder.Replace("Quad-Core Processor", "");
|
|
|
|
|
nameBuilder.Replace("Six-Core Processor", "");
|
|
|
|
|
nameBuilder.Replace("Eight-Core Processor", "");
|
2020-03-08 15:31:19 +01:00
|
|
|
|
nameBuilder.Replace("Dual Core Processor", "");
|
|
|
|
|
nameBuilder.Replace("Quad Core Processor", "");
|
2020-02-22 23:57:07 +01:00
|
|
|
|
nameBuilder.Replace("12-Core Processor", "");
|
|
|
|
|
nameBuilder.Replace("16-Core Processor", "");
|
|
|
|
|
nameBuilder.Replace("24-Core Processor", "");
|
|
|
|
|
nameBuilder.Replace("32-Core Processor", "");
|
|
|
|
|
nameBuilder.Replace("64-Core Processor", "");
|
|
|
|
|
nameBuilder.Replace("6-Core Processor", "");
|
|
|
|
|
nameBuilder.Replace("8-Core Processor", "");
|
2020-03-08 15:31:19 +01:00
|
|
|
|
nameBuilder.Replace("with Radeon Vega Mobile Gfx", "");
|
|
|
|
|
nameBuilder.Replace("w/ Radeon Vega Mobile Gfx", "");
|
|
|
|
|
nameBuilder.Replace("with Radeon Vega Graphics", "");
|
|
|
|
|
nameBuilder.Replace("APU with Radeon(tm) HD Graphics", "");
|
|
|
|
|
nameBuilder.Replace("APU with Radeon(TM) HD Graphics", "");
|
|
|
|
|
nameBuilder.Replace("APU with AMD Radeon R2 Graphics", "");
|
|
|
|
|
nameBuilder.Replace("APU with AMD Radeon R3 Graphics", "");
|
|
|
|
|
nameBuilder.Replace("APU with AMD Radeon R4 Graphics", "");
|
|
|
|
|
nameBuilder.Replace("APU with AMD Radeon R5 Graphics", "");
|
|
|
|
|
nameBuilder.Replace("APU with Radeon(tm) R3", "");
|
|
|
|
|
nameBuilder.Replace("RADEON R2, 4 COMPUTE CORES 2C+2G", "");
|
|
|
|
|
nameBuilder.Replace("RADEON R4, 5 COMPUTE CORES 2C+3G", "");
|
|
|
|
|
nameBuilder.Replace("RADEON R5, 5 COMPUTE CORES 2C+3G", "");
|
|
|
|
|
nameBuilder.Replace("RADEON R5, 10 COMPUTE CORES 4C+6G", "");
|
|
|
|
|
nameBuilder.Replace("RADEON R7, 10 COMPUTE CORES 4C+6G", "");
|
|
|
|
|
nameBuilder.Replace("RADEON R7, 12 COMPUTE CORES 4C+8G", "");
|
2020-05-04 23:11:33 +02:00
|
|
|
|
nameBuilder.Replace("Radeon R5, 6 Compute Cores 2C+4G", "");
|
2020-03-08 15:31:19 +01:00
|
|
|
|
nameBuilder.Replace("Radeon R5, 8 Compute Cores 4C+4G", "");
|
|
|
|
|
nameBuilder.Replace("Radeon R6, 10 Compute Cores 4C+6G", "");
|
|
|
|
|
nameBuilder.Replace("Radeon R7, 10 Compute Cores 4C+6G", "");
|
|
|
|
|
nameBuilder.Replace("Radeon R7, 12 Compute Cores 4C+8G", "");
|
|
|
|
|
nameBuilder.Replace("R5, 10 Compute Cores 4C+6G", "");
|
|
|
|
|
nameBuilder.Replace("R7, 12 COMPUTE CORES 4C+8G", "");
|
|
|
|
|
nameBuilder.Replace("(R)", " ");
|
|
|
|
|
nameBuilder.Replace("(TM)", " ");
|
|
|
|
|
nameBuilder.Replace("(tm)", " ");
|
|
|
|
|
nameBuilder.Replace("CPU", " ");
|
2020-02-22 23:57:07 +01:00
|
|
|
|
|
2010-04-24 19:59:52 +00:00
|
|
|
|
for (int i = 0; i < 10; i++) nameBuilder.Replace(" ", " ");
|
|
|
|
|
name = nameBuilder.ToString();
|
|
|
|
|
if (name.Contains("@"))
|
|
|
|
|
name = name.Remove(name.LastIndexOf('@'));
|
2020-05-04 23:11:33 +02:00
|
|
|
|
name = name.Trim();
|
2010-04-24 19:59:52 +00:00
|
|
|
|
|
|
|
|
|
this.family = ((cpuidData[1, 0] & 0x0FF00000) >> 20) +
|
|
|
|
|
((cpuidData[1, 0] & 0x0F00) >> 8);
|
|
|
|
|
this.model = ((cpuidData[1, 0] & 0x0F0000) >> 12) +
|
|
|
|
|
((cpuidData[1, 0] & 0xF0) >> 4);
|
|
|
|
|
this.stepping = (cpuidData[1, 0] & 0x0F);
|
|
|
|
|
|
|
|
|
|
this.apicId = (cpuidData[1, 1] >> 24) & 0xFF;
|
|
|
|
|
|
|
|
|
|
switch (vendor) {
|
|
|
|
|
case Vendor.Intel:
|
|
|
|
|
uint maxCoreAndThreadIdPerPackage = (cpuidData[1, 1] >> 16) & 0xFF;
|
2010-04-25 12:35:16 +00:00
|
|
|
|
uint maxCoreIdPerPackage;
|
|
|
|
|
if (maxCpuid >= 4)
|
|
|
|
|
maxCoreIdPerPackage = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
|
|
|
|
|
else
|
|
|
|
|
maxCoreIdPerPackage = 1;
|
2020-05-04 23:11:33 +02:00
|
|
|
|
threadMaskWith =
|
2010-04-30 20:53:40 +00:00
|
|
|
|
NextLog2(maxCoreAndThreadIdPerPackage / maxCoreIdPerPackage);
|
|
|
|
|
coreMaskWith = NextLog2(maxCoreIdPerPackage);
|
2010-04-24 19:59:52 +00:00
|
|
|
|
break;
|
2020-05-04 23:11:33 +02:00
|
|
|
|
case Vendor.AMD:
|
2020-02-08 00:36:51 +01:00
|
|
|
|
if (this.family == 0x17) {
|
|
|
|
|
coreMaskWith = (cpuidExtData[8, 2] >> 12) & 0xF;
|
2020-05-04 23:11:33 +02:00
|
|
|
|
threadMaskWith =
|
2020-02-08 00:36:51 +01:00
|
|
|
|
NextLog2(((cpuidExtData[0x1E, 1] >> 8) & 0xFF) + 1);
|
|
|
|
|
} else {
|
|
|
|
|
uint corePerPackage;
|
|
|
|
|
if (maxCpuidExt >= 8)
|
|
|
|
|
corePerPackage = (cpuidExtData[8, 2] & 0xFF) + 1;
|
|
|
|
|
else
|
|
|
|
|
corePerPackage = 1;
|
|
|
|
|
coreMaskWith = NextLog2(corePerPackage);
|
|
|
|
|
threadMaskWith = 0;
|
2020-05-04 23:11:33 +02:00
|
|
|
|
}
|
2010-04-24 19:59:52 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
threadMaskWith = 0;
|
|
|
|
|
coreMaskWith = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
processorId = (apicId >> (int)(coreMaskWith + threadMaskWith));
|
2020-05-04 23:11:33 +02:00
|
|
|
|
coreId = ((apicId >> (int)(threadMaskWith))
|
2010-04-27 07:31:46 +00:00
|
|
|
|
- (processorId << (int)(coreMaskWith)));
|
2010-04-25 12:35:16 +00:00
|
|
|
|
threadId = apicId
|
|
|
|
|
- (processorId << (int)(coreMaskWith + threadMaskWith))
|
2020-05-04 23:11:33 +02:00
|
|
|
|
- (coreId << (int)(threadMaskWith));
|
2010-04-24 19:59:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name {
|
|
|
|
|
get { return name; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string BrandString {
|
|
|
|
|
get { return cpuBrandString; }
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 23:11:33 +02:00
|
|
|
|
public int Group {
|
|
|
|
|
get {
|
|
|
|
|
return group;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-24 19:59:52 +00:00
|
|
|
|
public int Thread {
|
|
|
|
|
get { return thread; }
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 23:11:33 +02:00
|
|
|
|
public GroupAffinity Affinity {
|
|
|
|
|
get {
|
|
|
|
|
return affinity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-24 19:59:52 +00:00
|
|
|
|
public Vendor Vendor {
|
|
|
|
|
get { return vendor; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint Family {
|
|
|
|
|
get { return family; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint Model {
|
|
|
|
|
get { return model; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint Stepping {
|
|
|
|
|
get { return stepping; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint ApicId {
|
|
|
|
|
get { return apicId; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint ProcessorId {
|
|
|
|
|
get { return processorId; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint CoreId {
|
|
|
|
|
get { return coreId; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint ThreadId {
|
|
|
|
|
get { return threadId; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint[,] Data {
|
|
|
|
|
get { return cpuidData; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint[,] ExtData {
|
|
|
|
|
get { return cpuidExtData; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|