[+] Modern Linux cpuinfo

[*] Replace getpid with gettid
This commit is contained in:
Reece Wilson 2022-04-06 05:50:12 +01:00
parent 8fe83de42f
commit b97ed198c3
4 changed files with 85 additions and 7 deletions

View File

@ -37,6 +37,13 @@ namespace Aurora::HWInfo
return 0;
}
static AuString ReadString(const AuString &path)
{
AuString contents;
AuIOFS::ReadString(path, contents);
return contents;
}
static void SetCaches()
{
gCpuInfo.dwCacheLine = ReadUInt("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size");
@ -45,15 +52,90 @@ namespace Aurora::HWInfo
gCpuInfo.dwCacheL3 = ReadUInt("/sys/devices/system/cpu/cpu0/cache/index3/size");
}
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)
{
auto cpuId = CpuBitId(threadId);
auto coreID = ReadUInt(kBasePath + coreStr + "/topology/core_id");
auto cpuList = ReadString(kBasePath + coreStr +"/topology/core_cpus_list");
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));
}
gCpuInfo.coreTopology.push_back(children);
gCpuInfo.threadTopology.push_back(children.lower);
}
gCpuInfo.maskAllCores.Add(cpuId);
}
}
void SetCpuTopologyLinux()
{
gCpuInfo.uSocket = 1;
gCpuInfo.uCores = 1;
gCpuInfo.uThreads = get_nprocs();
// TODO: parse /proc/cpuinfo
gCpuInfo.bMaskMTHalf = true;
SetCaches();
SetCpuA();
gCpuInfo.uCores = gCpuInfo.coreTopology.size();
}
}

View File

@ -143,10 +143,6 @@ namespace Aurora::HWInfo
}
}
}
return true;
}
void SetCpuTopologyNT()

View File

@ -63,7 +63,7 @@ namespace Aurora::IO::FS
{
AuUInt read;
auto file = OpenReadUnique(path);
auto file = OpenReadUnique(path, EFileAdvisoryLockLevel::eNoSafety);
SysCheckReturn(file, false);
auto len = file->GetLength();

View File

@ -247,7 +247,7 @@ namespace Aurora::Processes
{
AU_LOCK_GUARD(gRWLock->AsWritable());
AuTryInsert(gPidLookupMap, getpid(), this);
AuTryInsert(gPidLookupMap, gettid(), this);
}
this->alive_ = true;