AuroraRuntime/Source/HWInfo/RamInfo.cpp
Reece a626fbea24 [+] HWInfo::GetPageSize()
[+] Parse::SplitNewlines(..., ..., true) where the return value is the remaining unbuffered line
[*] Gross hack we should remove to drop std string parse exception abuse logs
[*] Update AuroraForEach and AuroraInterfaces
[*] Experiment with using alternative os address space reserve + commit mechanics for Memory::Heaps
[*] global fast rand device should be seeded with at least 64 bits of secure rng data. ideally, we should max out entropy with secure bits, but we dont
2021-10-14 12:35:05 +01:00

155 lines
3.1 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: RamInfo.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "HWInfo.hpp"
#include "RamInfo.hpp"
#if defined(AURORA_IS_BSD_DERIVED)
#include <sys/sysctl.h>
#include <sys/vmmeter.h>
#include <sys/limits.h>
#include <vm/vm_param.h>
#endif
#if defined(AURORA_IS_LINUX_DERIVED)
#include <sys/sysinfo.h>
#include <sys/resource.h>
#endif
#if defined(AURORA_IS_MODERNNT_DERIVED)
#include <psapi.h>
#endif
namespace Aurora::HWInfo
{
static AuOptional<RamStat> gMemStartup;
static AuUInt32 gPageSize;
AUKN_SYM AuOptional<RamStat> GetMemStatProcess()
{
auto max = GetMemStatSystem().value_or(RamStat {}).available;
#if defined(AURORA_IS_MODERNNT_DERIVED)
PROCESS_MEMORY_COUNTERS pmc;
if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(PROCESS_MEMORY_COUNTERS)))
{
return {};
}
return RamStat {pmc.WorkingSetSize, max};
#elif defined(AURORA_IS_POSIX_DERIVED)
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
auto used = AuUInt64(usage.ru_maxrss) * 1024;
return RamStat {used, max};
#else
return {};
#endif
}
#if defined(AURORA_IS_BSD_DERIVED)
struct vmtotal GetVMInfo()
{
struct vmtotal info;
int mib[2];
mib[0] = CTL_VM;
mib[1] = VM_TOTAL;
size_t len = sizeof(info);
sysctl(mib, 2, &info, &len, NULL, 0);
return info;
}
#endif
AUKN_SYM AuOptional<RamStat> GetMemStatSystem()
{
#if defined(AURORA_IS_MODERNNT_DERIVED)
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
if (!GlobalMemoryStatusEx(&statex))
{
return {};
}
return RamStat {statex.ullTotalPhys - statex.ullAvailPhys, statex.ullTotalPhys};
#elif defined(AURORA_IS_BSD_DERIVED)
auto maxMem = QueryBsdHwStat(HW_PHYSMEM);
auto vmInfo = GetVMInfo();
auto freeMem = AuUInt64(vmInfo.t_free) * QueryBsdHwStat(HW_PAGESIZE).value_or(4096);
return RamStat {maxMem.value_or(freeMem * 2) - freeMem, maxMem.value_or(0)};
#elif defined(AURORA_IS_LINUX_DERIVED)
struct sysinfo info;
if (sysinfo(&info) != 0)
{
return {};
}
return RamStat {info.totalram - info.freeram, info.totalram};
#else
return {};
#endif
}
AUKN_SYM AuOptional<RamStat> GetMemStatStartup()
{
return gMemStartup;
}
AUKN_SYM AuUInt32 GetPageSize()
{
return gPageSize;
}
static void SetPageSize()
{
#if defined(AURORA_IS_MODERNNT_DERIVED)
SYSTEM_INFO info;
GetSystemInfo(&info);
gPageSize = info.dwPageSize;
#elif defined(AURORA_IS_POSIX_DERIVED)
gPageSize = getpagesize();
#else
gPageSize = 4096;
#endif
}
void InitRamInfo()
{
gMemStartup = GetMemStatSystem();
SetPageSize();
}
}