feat(util): add support for memory usage on Windows

This commit is contained in:
Marcus Holland-Moritz 2025-05-25 12:58:33 +02:00
parent 038541f94e
commit e9cff93636
2 changed files with 18 additions and 2 deletions

View File

@ -850,7 +850,7 @@ foreach(tgt ${LIBDWARFS_TARGETS} ${LIBDWARFS_OBJECT_TARGETS} dwarfs_test_helpers
endif() endif()
if(WIN32) if(WIN32)
target_link_libraries(${tgt} PRIVATE ntdll.lib dbghelp.lib) target_link_libraries(${tgt} PRIVATE ntdll.lib dbghelp.lib psapi.lib)
endif() endif()
endforeach() endforeach()

View File

@ -38,6 +38,10 @@
#include <optional> #include <optional>
#include <type_traits> #include <type_traits>
#ifdef _WIN32
#define PSAPI_VERSION 1
#endif
#if __has_include(<utf8cpp/utf8.h>) #if __has_include(<utf8cpp/utf8.h>)
#include <utf8cpp/utf8.h> #include <utf8cpp/utf8.h>
#else #else
@ -55,6 +59,10 @@
#include <dwarfs/config.h> #include <dwarfs/config.h>
#ifdef _WIN32
#include <psapi.h>
#endif
#ifdef DWARFS_STACKTRACE_ENABLED #ifdef DWARFS_STACKTRACE_ENABLED
#include <cpptrace/cpptrace.hpp> #include <cpptrace/cpptrace.hpp>
#include <csignal> #include <csignal>
@ -591,7 +599,15 @@ std::tm safe_localtime(std::time_t t) {
std::optional<size_t> get_self_memory_usage() { std::optional<size_t> get_self_memory_usage() {
#if defined(_WIN32) #if defined(_WIN32)
// TODO PROCESS_MEMORY_COUNTERS_EX pmc{};
if (::GetProcessMemoryInfo(::GetCurrentProcess(),
reinterpret_cast<PPROCESS_MEMORY_COUNTERS>(&pmc),
sizeof(pmc))) {
if (pmc.PrivateUsage > 0) {
return static_cast<size_t>(pmc.PrivateUsage);
}
}
#elif defined(__APPLE__) #elif defined(__APPLE__)
task_vm_info info{}; task_vm_info info{};
mach_msg_type_number_t count = TASK_VM_INFO_COUNT; mach_msg_type_number_t count = TASK_VM_INFO_COUNT;