2022-01-26 04:22:12 +00:00
|
|
|
/***
|
2023-07-09 09:02:21 +00:00
|
|
|
Copyright (C) 2022-2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
2022-01-26 04:22:12 +00:00
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
File: AuCpuId.NT.cpp
|
2022-01-26 04:22:12 +00:00
|
|
|
Date: 2022-1-25
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2022-11-17 07:46:07 +00:00
|
|
|
#include "AuHWInfo.hpp"
|
|
|
|
#include "AuCpuInfo.hpp"
|
|
|
|
#include "AuCpuInfo.NT.hpp"
|
2022-01-26 04:22:12 +00:00
|
|
|
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
#include <VersionHelpers.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Aurora::HWInfo
|
|
|
|
{
|
2023-07-09 09:02:21 +00:00
|
|
|
static bool SetWindows10CpuSetInfoSlow()
|
2022-01-26 04:22:12 +00:00
|
|
|
{
|
|
|
|
SYSTEM_CPU_SET_INFORMATION cpuSetInfo[128];
|
|
|
|
SYSTEM_LOGICAL_PROCESSOR_INFORMATION sysinfo[128];
|
2023-07-09 09:02:21 +00:00
|
|
|
DWORD dwLength {};
|
2022-12-14 01:35:18 +00:00
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
if (!pGetSystemCpuSetInformation)
|
2022-12-14 01:35:18 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
if (!pGetSystemCpuSetInformation(cpuSetInfo, sizeof(cpuSetInfo), &dwLength, 0, 0))
|
2022-01-26 04:22:12 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CpuInfo
|
|
|
|
{
|
|
|
|
AuList<AuUInt8> low;
|
2023-07-09 09:02:21 +00:00
|
|
|
AuList<AuPair<AuUInt8, CpuBitId>> server;
|
2022-01-26 04:22:12 +00:00
|
|
|
CpuBitId mask;
|
|
|
|
};
|
2023-07-09 09:02:21 +00:00
|
|
|
|
2022-01-26 04:22:12 +00:00
|
|
|
AuBST<AuUInt8, CpuInfo> cpuThreads;
|
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
AuUInt8 uThreadCount = AuUInt8(dwLength / sizeof(decltype(*cpuSetInfo)));
|
|
|
|
|
|
|
|
AuUInt8 uBestClass {};
|
|
|
|
bool bHasBestClass {};
|
|
|
|
|
|
|
|
if (uThreadCount)
|
|
|
|
{
|
|
|
|
AuUInt8 countTable[255] {};
|
|
|
|
for (AU_ITERATE_N(i, uThreadCount))
|
|
|
|
{
|
|
|
|
countTable[cpuSetInfo[i].CpuSet.EfficiencyClass]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt8 uBestClassCounter {};
|
|
|
|
for (AU_ITERATE_N(i, AuArraySize(countTable)))
|
|
|
|
{
|
|
|
|
auto uCurrentCount = countTable[i];
|
|
|
|
|
|
|
|
if (uCurrentCount < uBestClassCounter)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uBestClassCounter = uCurrentCount;
|
|
|
|
bHasBestClass = true;
|
|
|
|
uBestClass = i;
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 04:22:12 +00:00
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
for (AU_ITERATE_N(i, uThreadCount))
|
2022-01-26 04:22:12 +00:00
|
|
|
{
|
2022-03-16 17:12:08 +00:00
|
|
|
auto &idx = cpuThreads[cpuSetInfo[i].CpuSet.CoreIndex];
|
|
|
|
|
|
|
|
// Win7 KAFFINITY = u64 affinity masks
|
|
|
|
// Windows 10 + seems to be ((this->group + 1ul) * 0x100ul) + index
|
|
|
|
// Windows internals says...
|
|
|
|
// ULONG sets[] = { 0x100, 0x101, 0x102, 0x103 };
|
|
|
|
// ::SetProcessDefaultCpuSets/SetThreadSelectedCpuSets(::GetCurrentProcess(), sets, _countof(sets));
|
|
|
|
// (useless)
|
|
|
|
// People generally isolate group and keep logical processors in a different set, kinda worthless for bitwise math
|
|
|
|
|
|
|
|
SysAssert(cpuSetInfo[i].CpuSet.LogicalProcessorIndex < 64);
|
|
|
|
|
|
|
|
#if defined(_AU_MASSIVE_CPUID)
|
|
|
|
SysAssert(cpuSetInfo[i].CpuSet.Group < 4);
|
|
|
|
#else
|
|
|
|
SysAssert(cpuSetInfo[i].CpuSet.Group < 2);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
AuUInt8 id = AuUInt8(cpuSetInfo[i].CpuSet.LogicalProcessorIndex /*no greater than 64*/ + (cpuSetInfo[i].CpuSet.Group * 64));
|
2022-01-26 04:22:12 +00:00
|
|
|
auto cpuId = CpuBitId(id);
|
2022-03-16 17:12:08 +00:00
|
|
|
|
|
|
|
auto sets = cpuId.ToCpuSets();
|
|
|
|
SysAssert(sets.size() == 1);
|
|
|
|
SysAssert(sets[0] == cpuSetInfo[i].CpuSet.Id);
|
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
idx.server.push_back(AuMakePair(cpuSetInfo[i].CpuSet.EfficiencyClass, cpuId));
|
2022-01-26 04:22:12 +00:00
|
|
|
idx.low.push_back(id);
|
|
|
|
idx.mask.Add(cpuId);
|
2022-03-20 12:50:36 +00:00
|
|
|
gCpuInfo.maskAllCores.Add(cpuId);
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &[cpuId, coreIds] : cpuThreads)
|
|
|
|
{
|
|
|
|
AuUInt64 shortMask {};
|
2023-07-09 09:02:21 +00:00
|
|
|
for (const auto &[eClass, id] : coreIds.server)
|
2022-01-26 04:22:12 +00:00
|
|
|
{
|
2023-07-09 09:02:21 +00:00
|
|
|
if (bHasBestClass &&
|
|
|
|
eClass != uBestClass)
|
2022-01-26 04:22:12 +00:00
|
|
|
{
|
|
|
|
gCpuInfo.maskECores.Add(id);
|
|
|
|
}
|
2022-03-20 12:50:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
gCpuInfo.maskPCores.Add(id);
|
|
|
|
gCpuInfo.pCoreTopology.push_back(id);
|
|
|
|
}
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &id : coreIds.low)
|
|
|
|
{
|
|
|
|
shortMask |= AuUInt64(1) << AuUInt64(id);
|
|
|
|
}
|
|
|
|
|
2022-03-20 12:50:36 +00:00
|
|
|
gCpuInfo.coreTopology.push_back(coreIds.mask);
|
2022-01-26 04:22:12 +00:00
|
|
|
gCpuInfo.threadTopology.push_back(shortMask);
|
|
|
|
}
|
|
|
|
|
2022-02-18 10:46:51 +00:00
|
|
|
gCpuInfo.uSocket = 1;
|
2023-07-09 09:02:21 +00:00
|
|
|
gCpuInfo.uThreads = uThreadCount;
|
2022-06-14 15:59:37 +00:00
|
|
|
gCpuInfo.uCores = AuUInt8(cpuThreads.size());
|
2022-01-26 04:22:12 +00:00
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
if (!pGetLogicalProcessorInformation)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pGetLogicalProcessorInformation(sysinfo, &dwLength))
|
2022-01-26 04:22:12 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-18 10:46:51 +00:00
|
|
|
gCpuInfo.uSocket = 0;
|
2023-07-09 09:02:21 +00:00
|
|
|
dwLength /= sizeof(*sysinfo);
|
2022-01-26 04:22:12 +00:00
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
for (auto i = 0u; i < dwLength; i++)
|
2022-01-26 04:22:12 +00:00
|
|
|
{
|
|
|
|
if (sysinfo[i].Relationship == RelationProcessorPackage)
|
|
|
|
{
|
2022-02-18 10:46:51 +00:00
|
|
|
gCpuInfo.uSocket++;
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
2022-03-30 21:45:10 +00:00
|
|
|
|
|
|
|
if (sysinfo[i].Relationship == RelationCache)
|
|
|
|
{
|
|
|
|
switch (sysinfo[i].Cache.Level)
|
|
|
|
{
|
|
|
|
case 1:
|
2022-12-14 05:25:05 +00:00
|
|
|
gCpuInfo.dwCacheLine = AuMax<AuUInt32>(gCpuInfo.dwCacheLine, sysinfo[i].Cache.LineSize);
|
|
|
|
gCpuInfo.dwCacheL1 = AuMax<AuUInt32>(gCpuInfo.dwCacheL1, sysinfo[i].Cache.Size);
|
2022-03-30 21:45:10 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2022-12-14 05:25:05 +00:00
|
|
|
gCpuInfo.dwCacheL2 = AuMax<AuUInt32>(gCpuInfo.dwCacheL2, sysinfo[i].Cache.Size);
|
2022-03-30 21:45:10 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2022-12-14 05:25:05 +00:00
|
|
|
gCpuInfo.dwCacheL3 = AuMax<AuUInt32>(gCpuInfo.dwCacheL3, sysinfo[i].Cache.Size);
|
2022-03-30 21:45:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
2022-04-10 16:18:21 +00:00
|
|
|
|
|
|
|
return true;
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
static bool SetWindowsXPSp3ExtendedInformation()
|
2022-01-26 04:22:12 +00:00
|
|
|
{
|
|
|
|
SYSTEM_LOGICAL_PROCESSOR_INFORMATION sysinfo[128];
|
2023-07-09 09:02:21 +00:00
|
|
|
DWORD dwLength = AuArraySize(sysinfo) * sizeof(*sysinfo);
|
2022-01-26 04:22:12 +00:00
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
if (!pGetLogicalProcessorInformation)
|
2022-01-26 04:22:12 +00:00
|
|
|
{
|
2023-07-09 09:02:21 +00:00
|
|
|
return false;
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
if (!pGetLogicalProcessorInformation(sysinfo, &dwLength))
|
2022-01-26 04:22:12 +00:00
|
|
|
{
|
2023-07-09 09:02:21 +00:00
|
|
|
return false;
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
dwLength /= sizeof(*sysinfo);
|
2022-01-26 04:22:12 +00:00
|
|
|
|
2022-02-18 10:46:51 +00:00
|
|
|
gCpuInfo.uSocket = 0;
|
|
|
|
gCpuInfo.uCores = 0;
|
|
|
|
gCpuInfo.uThreads = 0;
|
2022-01-26 04:22:12 +00:00
|
|
|
|
|
|
|
bool sparse = false;
|
2022-03-20 12:50:36 +00:00
|
|
|
bool hasHTCores = false;
|
2023-07-09 09:02:21 +00:00
|
|
|
for (auto i = 0u; i < dwLength; i++)
|
2022-01-26 04:22:12 +00:00
|
|
|
{
|
|
|
|
if (sysinfo[i].Relationship == RelationProcessorCore)
|
|
|
|
{
|
|
|
|
auto mask = sysinfo[i].ProcessorMask;
|
|
|
|
|
2022-02-18 10:46:51 +00:00
|
|
|
gCpuInfo.uCores++;
|
2022-01-26 04:22:12 +00:00
|
|
|
gCpuInfo.threadTopology.push_back(mask);
|
|
|
|
|
|
|
|
CpuBitId serverId;
|
|
|
|
serverId.lower = mask;
|
2022-12-14 01:35:18 +00:00
|
|
|
gCpuInfo.coreTopology.push_back(serverId);
|
2022-01-26 04:22:12 +00:00
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
auto uThreadsInCore = AuPopCnt(mask);
|
|
|
|
gCpuInfo.uThreads += uThreadsInCore;
|
2022-01-26 04:22:12 +00:00
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
hasHTCores |= (uThreadsInCore == 2);
|
2022-03-20 12:50:36 +00:00
|
|
|
|
2023-07-09 09:02:21 +00:00
|
|
|
if (hasHTCores && (uThreadsInCore == 1))
|
2022-03-20 12:50:36 +00:00
|
|
|
{
|
|
|
|
AuUInt8 idx {};
|
2022-12-14 01:35:18 +00:00
|
|
|
while (serverId.CpuBitScanForward(idx, idx))
|
2022-03-20 12:50:36 +00:00
|
|
|
{
|
|
|
|
gCpuInfo.maskECores.Add(idx);
|
2022-12-14 01:35:18 +00:00
|
|
|
idx++;
|
2022-03-20 12:50:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AuUInt8 idx {};
|
2022-12-14 01:35:18 +00:00
|
|
|
while (serverId.CpuBitScanForward(idx, idx))
|
2022-03-20 12:50:36 +00:00
|
|
|
{
|
|
|
|
gCpuInfo.maskPCores.Add(idx);
|
2022-12-14 01:35:18 +00:00
|
|
|
idx++;
|
2022-03-20 12:50:36 +00:00
|
|
|
}
|
|
|
|
gCpuInfo.pCoreTopology.push_back(mask);
|
|
|
|
}
|
|
|
|
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
|
|
|
else if (sysinfo[i].Relationship == RelationProcessorPackage)
|
|
|
|
{
|
2022-02-18 10:46:51 +00:00
|
|
|
gCpuInfo.uSocket++;
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
2022-12-14 05:25:05 +00:00
|
|
|
else if (sysinfo[i].Relationship == RelationCache)
|
|
|
|
{
|
|
|
|
switch (sysinfo[i].Cache.Level)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
gCpuInfo.dwCacheLine = AuMax<AuUInt32>(gCpuInfo.dwCacheLine, sysinfo[i].Cache.LineSize);
|
|
|
|
gCpuInfo.dwCacheL1 = AuMax<AuUInt32>(gCpuInfo.dwCacheL1, sysinfo[i].Cache.Size);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
gCpuInfo.dwCacheL2 = AuMax<AuUInt32>(gCpuInfo.dwCacheL2, sysinfo[i].Cache.Size);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
gCpuInfo.dwCacheL3 = AuMax<AuUInt32>(gCpuInfo.dwCacheL3, sysinfo[i].Cache.Size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 10:46:51 +00:00
|
|
|
gCpuInfo.bMaskMTContig = !sparse;
|
|
|
|
gCpuInfo.bMaskMTHalf = sparse;
|
2023-07-09 09:02:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetBasicWindowsXPInformation()
|
|
|
|
{
|
|
|
|
SYSTEM_INFO sysinfo;
|
|
|
|
GetSystemInfo(&sysinfo);
|
|
|
|
|
|
|
|
gCpuInfo.uSocket = 1;
|
|
|
|
gCpuInfo.uCores = 1;
|
|
|
|
gCpuInfo.uThreads = sysinfo.dwNumberOfProcessors;
|
|
|
|
|
|
|
|
if (sysinfo.dwNumberOfProcessors & 1)
|
|
|
|
{
|
|
|
|
for (AU_ITERATE_N(i, gCpuInfo.uThreads))
|
|
|
|
{
|
|
|
|
auto mask = 1 << i;
|
|
|
|
|
|
|
|
gCpuInfo.maskPCores.SetBit(i);
|
|
|
|
|
|
|
|
gCpuInfo.threadTopology.push_back(mask);
|
|
|
|
|
|
|
|
CpuBitId coreId;
|
|
|
|
coreId.lower = mask;
|
|
|
|
gCpuInfo.coreTopology.push_back(coreId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (AU_ITERATE_N(i, gCpuInfo.uThreads))
|
|
|
|
{
|
|
|
|
if (i & 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
gCpuInfo.maskPCores.SetBit(i);
|
|
|
|
gCpuInfo.maskPCores.SetBit(i + 1);
|
|
|
|
|
|
|
|
auto maskA = 1u << i;
|
|
|
|
auto maskB = 1u << (i + 1);
|
|
|
|
auto maskC = maskA | maskB;
|
|
|
|
|
|
|
|
gCpuInfo.threadTopology.push_back(maskA);
|
|
|
|
gCpuInfo.threadTopology.push_back(maskB);
|
|
|
|
|
|
|
|
CpuBitId coreId;
|
|
|
|
coreId.lower = maskC;
|
|
|
|
gCpuInfo.coreTopology.push_back(coreId);
|
|
|
|
}
|
|
|
|
|
|
|
|
gCpuInfo.uCores = gCpuInfo.uThreads / 2;
|
|
|
|
gCpuInfo.uThreads = 2;
|
|
|
|
|
|
|
|
gCpuInfo.bMaskMTHalf = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetCpuTopologyNT()
|
|
|
|
{
|
|
|
|
if (SetWindows10CpuSetInfoSlow())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SetWindowsXPSp3ExtendedInformation())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetBasicWindowsXPInformation();
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
|
|
|
}
|