From e9cff93636c440543cb81c1ec8245eabb8ea98f2 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sun, 25 May 2025 12:58:33 +0200 Subject: [PATCH] feat(util): add support for memory usage on Windows --- CMakeLists.txt | 2 +- src/util.cpp | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51044803..d4dbc9c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -850,7 +850,7 @@ foreach(tgt ${LIBDWARFS_TARGETS} ${LIBDWARFS_OBJECT_TARGETS} dwarfs_test_helpers endif() if(WIN32) - target_link_libraries(${tgt} PRIVATE ntdll.lib dbghelp.lib) + target_link_libraries(${tgt} PRIVATE ntdll.lib dbghelp.lib psapi.lib) endif() endforeach() diff --git a/src/util.cpp b/src/util.cpp index 3b414087..d9b8ca71 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -38,6 +38,10 @@ #include #include +#ifdef _WIN32 +#define PSAPI_VERSION 1 +#endif + #if __has_include() #include #else @@ -55,6 +59,10 @@ #include +#ifdef _WIN32 +#include +#endif + #ifdef DWARFS_STACKTRACE_ENABLED #include #include @@ -591,7 +599,15 @@ std::tm safe_localtime(std::time_t t) { std::optional get_self_memory_usage() { #if defined(_WIN32) - // TODO + PROCESS_MEMORY_COUNTERS_EX pmc{}; + + if (::GetProcessMemoryInfo(::GetCurrentProcess(), + reinterpret_cast(&pmc), + sizeof(pmc))) { + if (pmc.PrivateUsage > 0) { + return static_cast(pmc.PrivateUsage); + } + } #elif defined(__APPLE__) task_vm_info info{}; mach_msg_type_number_t count = TASK_VM_INFO_COUNT;