util/memory.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #include "chrono_batch_render/util/memory.h" | ||
| 3 | |||
| 4 | #if defined(_WIN32) | ||
| 5 | #include <windows.h> | ||
| 6 | |||
| 7 | #include <psapi.h> | ||
| 8 | #elif defined(__APPLE__) | ||
| 9 | #include <mach/mach.h> | ||
| 10 | #elif defined(__linux__) | ||
| 11 | #include <fstream> | ||
| 12 | |||
| 13 | #include <unistd.h> | ||
| 14 | #endif | ||
| 15 | |||
| 16 | namespace chrono::batch_render { | ||
| 17 | |||
| 18 | 18 | std::optional<uint64_t> ResidentBytes() { | |
| 19 | #if defined(_WIN32) | ||
| 20 | PROCESS_MEMORY_COUNTERS counters{}; | ||
| 21 | if (GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters))) { | ||
| 22 | return uint64_t(counters.WorkingSetSize); | ||
| 23 | } | ||
| 24 | #elif defined(__APPLE__) | ||
| 25 | mach_task_basic_info_data_t info{}; | ||
| 26 | mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT; | ||
| 27 | if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, reinterpret_cast<task_info_t>(&info), | ||
| 28 | &count) == KERN_SUCCESS) { | ||
| 29 | return uint64_t(info.resident_size); | ||
| 30 | } | ||
| 31 | #elif defined(__linux__) | ||
| 32 | 18 | std::ifstream statm("/proc/self/statm"); | |
| 33 | 18 | uint64_t size = 0; | |
| 34 | 18 | uint64_t resident = 0; | |
| 35 | 18 | if (statm >> size >> resident) { | |
| 36 | 18 | return resident * uint64_t(sysconf(_SC_PAGESIZE)); | |
| 37 | } | ||
| 38 | #endif | ||
| 39 | ✗ | return std::nullopt; | |
| 40 | 18 | } | |
| 41 | |||
| 42 | } | ||
| 43 |