From e30ccbc90f18d060ff0cfe16ddd95179d5f976eb Mon Sep 17 00:00:00 2001 From: Reece Date: Thu, 20 Jan 2022 19:20:23 +0000 Subject: [PATCH] [*] mo better memory reporting info --- Aurora.json | 2 +- Include/Aurora/HWInfo/RamInfo.hpp | 28 ++++++++ Source/HWInfo/RamInfo.cpp | 116 ++++++++++++++++++++++++++++-- 3 files changed, 139 insertions(+), 7 deletions(-) diff --git a/Aurora.json b/Aurora.json index b4d73d3f..214db0cf 100644 --- a/Aurora.json +++ b/Aurora.json @@ -15,7 +15,7 @@ { "filter": {"platforms": "win32"}, "then": { - "links": ["Bcrypt.lib", "UxTheme.lib", "Aux_ulib.lib", "Dbghelp.lib", "ws2_32.lib"] + "links": ["Bcrypt.lib", "UxTheme.lib", "Aux_ulib.lib", "Dbghelp.lib", "ws2_32.lib", "Ntdll.lib"] } } ] diff --git a/Include/Aurora/HWInfo/RamInfo.hpp b/Include/Aurora/HWInfo/RamInfo.hpp index 145fb130..2bd6ce8c 100644 --- a/Include/Aurora/HWInfo/RamInfo.hpp +++ b/Include/Aurora/HWInfo/RamInfo.hpp @@ -11,8 +11,36 @@ namespace Aurora::HWInfo { + /** + * @brief Virtual Address Space / Address Space Available + * @return + */ AUKN_SYM AuOptional GetMemStatProcess(); + + /** + * @brief Private (/unshared) Address Space / Address Space Available + * @return + */ + AUKN_SYM AuOptional GetMemStatProcessBlamed(); + + /** + * @brief Total Unique Address Space / Address Space Available (a/k/a Commit Charge) + * @return + */ AUKN_SYM AuOptional GetMemStatSystem(); + + + /** + * @brief Physical Memory Used / Total Physical Memory + * @return + */ + AUKN_SYM AuOptional GetMemStatPhysical(); + + + /** + * @brief GetMemStatSystem at start-up + * @return + */ AUKN_SYM AuOptional GetMemStatStartup(); AUKN_SYM AuUInt32 GetPageSize(); diff --git a/Source/HWInfo/RamInfo.cpp b/Source/HWInfo/RamInfo.cpp index 92df88fd..a87b114d 100644 --- a/Source/HWInfo/RamInfo.cpp +++ b/Source/HWInfo/RamInfo.cpp @@ -24,6 +24,8 @@ #if defined(AURORA_IS_MODERNNT_DERIVED) #include + #include + #include #endif namespace Aurora::HWInfo @@ -36,15 +38,20 @@ namespace Aurora::HWInfo auto max = GetMemStatSystem().value_or(RamStat {}).available; #if defined(AURORA_IS_MODERNNT_DERIVED) + PROCESS_MEMORY_COUNTERS_EX pmc; + PROCESS_MEMORY_COUNTERS pm; - PROCESS_MEMORY_COUNTERS pmc; - - if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(PROCESS_MEMORY_COUNTERS))) + if (GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast(&pmc), sizeof(PROCESS_MEMORY_COUNTERS_EX))) { - return {}; + return RamStat {pmc.PrivateUsage, max}; } - return RamStat {pmc.WorkingSetSize, max}; + if (GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast(&pm), sizeof(PROCESS_MEMORY_COUNTERS))) + { + return RamStat {pm.WorkingSetSize, max}; + } + + return {}; #elif defined(AURORA_IS_POSIX_DERIVED) @@ -61,6 +68,62 @@ namespace Aurora::HWInfo #endif } + AUKN_SYM AuOptional GetMemStatProcessBlamed() + { + #if defined(AURORA_IS_MODERNNT_DERIVED) + auto max = GetMemStatSystem().value_or(RamStat {}).available; + + if (IsWindows10OrGreater()) + { + struct VM_COUNTERS_EX + { + SIZE_T PeakVirtualSize; + SIZE_T VirtualSize; + ULONG PageFaultCount; + SIZE_T PeakWorkingSetSize; + SIZE_T WorkingSetSize; + SIZE_T QuotaPeakPagedPoolUsage; + SIZE_T QuotaPagedPoolUsage; + SIZE_T QuotaPeakNonPagedPoolUsage; + SIZE_T QuotaNonPagedPoolUsage; + SIZE_T PagefileUsage; + SIZE_T PeakPagefileUsage; + SIZE_T PrivateUsage; + }; + + struct VM_COUNTERS_EX2 + { + VM_COUNTERS_EX CountersEx; + SIZE_T PrivateWorkingSetSize; + ULONGLONG SharedCommitUsage; + } vm; + + static const PROCESSINFOCLASS kProcessVmCounters = static_cast(3); + + if (NtQueryInformationProcess(GetCurrentProcess(), kProcessVmCounters, &vm, sizeof(vm), 0)) + { + // I WILL NOT USE A BLOATED OS THAT LIES TO US + // I WILL NOT USE A BLOATED OS THAT LIES TO US + // I WILL NOT USE A BLOATED OS THAT LIES TO US + // I WILL NOT USE A BLOATED OS THAT LIES TO US + // I WILL NOT USE A BLOATED OS THAT LIES TO US + // I WILL NOT USE A BLOATED OS THAT LIES TO US + return RamStat {vm.PrivateWorkingSetSize, max}; + } + } + + PROCESS_MEMORY_COUNTERS pmc; + if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(PROCESS_MEMORY_COUNTERS))) + { + return {}; + } + + return RamStat {pmc.WorkingSetSize, max}; + #else + return GetMemStatProcess(); + #endif + } + #if defined(AURORA_IS_BSD_DERIVED) struct vmtotal GetVMInfo() { @@ -89,6 +152,47 @@ namespace Aurora::HWInfo return {}; } + return RamStat {statex.ullTotalPageFile - statex.ullAvailPageFile, statex.ullTotalPageFile}; + + #elif defined(AURORA_IS_BSD_DERIVED) + + auto vmInfo = GetVMInfo(); + auto pageSize = QueryBsdHwStat(HW_PAGESIZE).value_or(4096); + auto totalMem = AuUInt64(vmInfo.t_vm) * pageSize; + auto freeMem = AuUInt64(vmInfo.t_free) * pageSize; + + return RamStat {freeMem - totalMem, totalMem}; + + #elif defined(AURORA_IS_LINUX_DERIVED) + + struct sysinfo info; + + if (sysinfo(&info) != 0) + { + return {}; + } + + return RamStat {(info.totalram - info.freeram) + (info.totalswap - info.freeswap), info.totalram + info.totalswap}; + + #else + + return GetMemStatPhysical(); + + #endif + } + + AUKN_SYM AuOptional GetMemStatPhysical() + { + #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) @@ -105,7 +209,7 @@ namespace Aurora::HWInfo 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)}; + return RamStat {vmInfo.t_rm, maxMem.value_or(freeMem)}; #elif defined(AURORA_IS_LINUX_DERIVED)