AuroraRuntime/Source/HWInfo/AuCpuInfo.Linux.cpp

212 lines
5.7 KiB
C++
Raw Normal View History

/***
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
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"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
namespace Aurora::HWInfo
{
AuUInt32 GetCPUIDAPICDCores();
AuUInt32 GetCPUIDAPICDThreads();
static AuUInt32 ReadUInt(const AuString &path)
{
AuString contents;
Further Linux support [+] Begin work on IO futexes for io release on process/thread exit [+] Linux ::readdir iteration [+] AuConsole buffering API [*] Fix sleep as to not get interrupted by signals [*] Switch the type of FS lock used under Linux [*] Linux: Use new IPCHandle encoding scheme [*] Fix undefined behaviour: unintialized timeout values (AuLoop/Linux) [*] Fix undefined behaviour: ConsoleTTY clear line was called of a color of a random value on stack [-] Remainings of std dir iterator [*] Fix pthread_kill (aka send signal to pthread handle) always kills process. This is what you expect bc signal handler inheritance. [*] Reformat the build Aurora.json file [+] Added clang warning ignores to the build file [*] Fix: UNIX need to use STDOUT_FILENO. Was using CRT handle in place of fd by mistake. [+] Linux implementation for IO yield (AuIO::IOYield() - UNIX::LinuxOverlappedYield()) [*] Fix: Linux async end of stream processing. res 0 = zero bytes consumed. <= was detecting this as an error of code 0. Should succeed with zero bytes. [+] Linux LoopQueue missing epilogue hook for the IO processor [*] Various refactors and minor bug fixes [*] Linux fix: Handle pipe EOS as zero [*] Linux fix: thread termination via a user signal of 77. Need a force terminate. [*] IPC handle: fix improper int to bool cast in the header setup within ToString [*] Linux fix: HWInfo CPU topology regression [-] Linux fix: remove SIGABRT handler [*] Missing override in compression, exit, and consoletty headers. [+] Unix Syslog logger backend
2022-08-02 04:52:17 +00:00
if (!AuIOFS::ReadString(path, contents))
{
return 0;
}
char *endPtr;
auto word = strtoll(contents.c_str(), &endPtr, 10);
if (errno == ERANGE)
{
return 0;
}
return word;
}
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");
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");
}
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.uCores++;
gCpuInfo.coreTopology.push_back(children);
gCpuInfo.threadTopology.push_back(children.lower);
}
2022-04-06 04:55:12 +00:00
gCpuInfo.uThreads++;
gCpuInfo.maskAllCores.Add(cpuId);
}
}
static void SetBasicAPICInformation()
{
if (gCpuInfo.uCores == 1 ||
gCpuInfo.uCores == 0)
{
gCpuInfo.uCores = GetCPUIDAPICDCores();
}
if (!gCpuInfo.uThreads)
{
gCpuInfo.uThreads = GetCPUIDAPICDThreads();
}
else
{
return;
}
if (gCpuInfo.uThreads & 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);
gCpuInfo.pCoreTopology.push_back(coreId);
gCpuInfo.maskAllCores.Add(coreId);
}
if (gCpuInfo.uCores == 1 ||
gCpuInfo.uCores == 0)
{
gCpuInfo.uCores = gCpuInfo.uThreads;
}
}
else
{
auto uHalf = gCpuInfo.uThreads / 2;
for (AU_ITERATE_N(i, uHalf))
{
gCpuInfo.maskPCores.SetBit(i);
gCpuInfo.maskPCores.SetBit(i + uHalf);
auto maskA = 1u << i;
auto maskB = 1u << (i + uHalf);
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.pCoreTopology.push_back(coreId);
gCpuInfo.maskAllCores.Add(coreId);
}
gCpuInfo.uCores = gCpuInfo.uThreads / 2;
gCpuInfo.bMaskMTHalf = true;
}
}
void SetCpuTopologyLinux()
{
SetCaches();
SetCpuA();
gCpuInfo.uSocket = AuMax(gCpuInfo.uSocket, AuUInt8(1));
if (gCpuInfo.uCores == 1 ||
gCpuInfo.uCores == 0)
{
SetBasicAPICInformation();
}
}
}