AuroraRuntime/Source/HWInfo/CpuInfo.Linux.cpp
J Reece Wilson fd0c5b51b2 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 05:52:57 +01:00

139 lines
3.8 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: CpuId.Linux.cpp
Date: 2022-1-25
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "HWInfo.hpp"
#include "CpuInfo.hpp"
#include "CpuInfo.Linux.hpp"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
namespace Aurora::HWInfo
{
static AuUInt32 ReadUInt(const AuString &path)
{
AuString contents;
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);
}
gCpuInfo.uThreads++;
gCpuInfo.maskAllCores.Add(cpuId);
}
}
void SetCpuTopologyLinux()
{
SetCaches();
SetCpuA();
gCpuInfo.uSocket = AuMax(gCpuInfo.uSocket, AuUInt8(1));
gCpuInfo.uCores = AuMax(gCpuInfo.uCores, AuUInt8(1));
gCpuInfo.uThreads = gCpuInfo.uThreads ?
gCpuInfo.uThreads :
get_nprocs();
}
}