2022-01-26 04:22:12 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
File: AuCpuId.Linux.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.Linux.hpp"
|
2022-01-26 04:22:12 +00:00
|
|
|
|
2022-04-06 05:06:48 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysinfo.h>
|
2022-01-26 04:22:12 +00:00
|
|
|
|
|
|
|
namespace Aurora::HWInfo
|
|
|
|
{
|
2022-03-30 21:45:10 +00:00
|
|
|
static AuUInt32 ReadUInt(const AuString &path)
|
|
|
|
{
|
|
|
|
AuString contents;
|
2022-08-02 04:52:17 +00:00
|
|
|
if (!AuIOFS::ReadString(path, contents))
|
2022-03-30 21:45:10 +00:00
|
|
|
{
|
2022-06-11 23:01:27 +00:00
|
|
|
return 0;
|
2022-03-30 21:45:10 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
char *endPtr;
|
|
|
|
auto word = strtoll(contents.c_str(), &endPtr, 10);
|
|
|
|
if (errno == ERANGE)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return word;
|
2022-03-30 21:45:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 04:50:12 +00:00
|
|
|
static AuString ReadString(const AuString &path)
|
|
|
|
{
|
|
|
|
AuString contents;
|
|
|
|
AuIOFS::ReadString(path, contents);
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2022-03-30 21:45:10 +00:00
|
|
|
static void SetCaches()
|
|
|
|
{
|
|
|
|
gCpuInfo.dwCacheLine = ReadUInt("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size");
|
|
|
|
gCpuInfo.dwCacheL1 = ReadUInt("/sys/devices/system/cpu/cpu0/cache/index1/size");
|
|
|
|
gCpuInfo.dwCacheL2 = ReadUInt("/sys/devices/system/cpu/cpu0/cache/index2/size");
|
|
|
|
gCpuInfo.dwCacheL3 = ReadUInt("/sys/devices/system/cpu/cpu0/cache/index3/size");
|
|
|
|
}
|
|
|
|
|
2022-04-06 04:50:12 +00:00
|
|
|
static void SetCpuA()
|
|
|
|
{
|
|
|
|
static const AuString kBasePath = "/sys/devices/system/cpu/";
|
|
|
|
|
|
|
|
AuList<AuString> files;
|
|
|
|
if (!AuIOFS::DirsInDirectory(kBasePath, files))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuList<AuTuple<AuUInt8, AuString>> cpuThreads;
|
|
|
|
cpuThreads.reserve(files.size());
|
|
|
|
|
|
|
|
std::sort(files.begin(), files.end());
|
|
|
|
|
|
|
|
for (auto & file : files)
|
|
|
|
{
|
|
|
|
if (AuStartsWith(file, "cpu"))
|
|
|
|
{
|
|
|
|
char *endPtr;
|
|
|
|
auto word = strtoll(file.c_str() + 3, &endPtr, 10);
|
|
|
|
if (errno == ERANGE) continue;
|
|
|
|
if (*endPtr != '\x00') continue;
|
|
|
|
|
|
|
|
cpuThreads.push_back(AuMakeTuple(word, file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bIsHyperThreaded {};
|
|
|
|
for (auto &[threadId, coreStr] : cpuThreads)
|
|
|
|
{
|
2022-04-10 15:40:49 +00:00
|
|
|
auto cpuId = CpuBitId(threadId);
|
2022-04-06 04:50:12 +00:00
|
|
|
|
2022-04-10 15:40:49 +00:00
|
|
|
auto coreID = ReadUInt(kBasePath + coreStr + "/topology/core_id");
|
2022-06-11 23:01:27 +00:00
|
|
|
auto cpuList = ReadString(kBasePath + coreStr + "/topology/core_cpus_list");
|
2022-04-06 04:50:12 +00:00
|
|
|
|
|
|
|
auto isHVCore = AuStringContains(cpuList, ",");
|
|
|
|
|
|
|
|
if (!bIsHyperThreaded && isHVCore)
|
|
|
|
{
|
|
|
|
bIsHyperThreaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bIsHyperThreaded && !isHVCore)
|
|
|
|
{
|
|
|
|
gCpuInfo.maskECores.Add(cpuId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gCpuInfo.maskPCores.Add(cpuId);
|
|
|
|
gCpuInfo.pCoreTopology.push_back(cpuId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (coreID == threadId)
|
|
|
|
{
|
|
|
|
auto children = CpuBitId();
|
|
|
|
|
|
|
|
auto cores = AuSplitString(cpuList, ",");
|
|
|
|
for (const auto core : cores)
|
|
|
|
{
|
|
|
|
char *endPtr;
|
|
|
|
auto word = strtoll(core.data(), &endPtr, 10);
|
|
|
|
if (errno == ERANGE) continue;
|
|
|
|
children.Add(CpuBitId(word));
|
|
|
|
}
|
2022-04-06 05:06:48 +00:00
|
|
|
|
2022-04-10 15:40:49 +00:00
|
|
|
gCpuInfo.uCores++;
|
2022-04-06 04:50:12 +00:00
|
|
|
gCpuInfo.coreTopology.push_back(children);
|
2022-04-06 05:06:48 +00:00
|
|
|
gCpuInfo.threadTopology.push_back(children.lower);
|
2022-04-06 04:50:12 +00:00
|
|
|
}
|
2022-04-06 04:55:12 +00:00
|
|
|
|
2022-04-10 15:40:49 +00:00
|
|
|
gCpuInfo.uThreads++;
|
2022-04-06 04:50:12 +00:00
|
|
|
gCpuInfo.maskAllCores.Add(cpuId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 04:22:12 +00:00
|
|
|
void SetCpuTopologyLinux()
|
|
|
|
{
|
2022-03-30 21:45:10 +00:00
|
|
|
SetCaches();
|
2022-04-06 04:50:12 +00:00
|
|
|
SetCpuA();
|
|
|
|
|
2022-04-10 15:40:49 +00:00
|
|
|
gCpuInfo.uSocket = AuMax(gCpuInfo.uSocket, AuUInt8(1));
|
|
|
|
gCpuInfo.uCores = AuMax(gCpuInfo.uCores, AuUInt8(1));
|
|
|
|
gCpuInfo.uThreads = gCpuInfo.uThreads ?
|
|
|
|
gCpuInfo.uThreads :
|
|
|
|
get_nprocs();
|
2022-01-26 04:22:12 +00:00
|
|
|
}
|
|
|
|
}
|