|
| 1 | +/***************************************************************************** |
| 2 | + * |
| 3 | + * PROJECT: Multi Theft Auto |
| 4 | + * LICENSE: See LICENSE in the top level directory |
| 5 | + * FILE: SharedUtil.Memory.hpp |
| 6 | + * |
| 7 | + * Multi Theft Auto is available from https://multitheftauto.com/ |
| 8 | + * |
| 9 | + *****************************************************************************/ |
| 10 | + |
| 11 | +#include "SharedUtil.Memory.h" |
| 12 | + |
| 13 | +#ifdef _WIN32 |
| 14 | + #include <Windows.h> |
| 15 | + #include <Psapi.h> |
| 16 | + #include <vector> |
| 17 | +#elif defined(__APPLE__) |
| 18 | + #include <mach/mach.h> |
| 19 | + #include <unistd.h> |
| 20 | +#else |
| 21 | + #include <fstream> |
| 22 | + #include <unistd.h> |
| 23 | + #include <sys/sysinfo.h> |
| 24 | +#endif |
| 25 | + |
| 26 | +namespace SharedUtil |
| 27 | +{ |
| 28 | +#ifdef _WIN32 |
| 29 | + static HANDLE g_process = GetCurrentProcess(); |
| 30 | +#endif |
| 31 | + |
| 32 | + bool TryGetProcessMemoryStats(ProcessMemoryStats& out) |
| 33 | + { |
| 34 | + out = {}; |
| 35 | + |
| 36 | +#ifdef _WIN32 |
| 37 | + MEMORYSTATUSEX memoryStatus{}; |
| 38 | + memoryStatus.dwLength = sizeof(memoryStatus); |
| 39 | + |
| 40 | + if (!GlobalMemoryStatusEx(&memoryStatus)) |
| 41 | + return false; |
| 42 | + |
| 43 | + out.virtualMemorySize = static_cast<size_t>(memoryStatus.ullTotalVirtual - memoryStatus.ullAvailVirtual); |
| 44 | + |
| 45 | + static std::vector<char> workingSetBuffer(2048 * sizeof(PSAPI_WORKING_SET_BLOCK) + sizeof(PSAPI_WORKING_SET_INFORMATION)); |
| 46 | + auto workingSetInfo = reinterpret_cast<PSAPI_WORKING_SET_INFORMATION*>(workingSetBuffer.data()); |
| 47 | + BOOL success = QueryWorkingSet(g_process, workingSetBuffer.data(), workingSetBuffer.size()); |
| 48 | + |
| 49 | + if (!success && GetLastError() == ERROR_BAD_LENGTH) |
| 50 | + { |
| 51 | + workingSetInfo->NumberOfEntries += 64; // Insurance in case the number of entries changes. |
| 52 | + workingSetBuffer.resize(workingSetInfo->NumberOfEntries * sizeof(PSAPI_WORKING_SET_BLOCK) + sizeof(PSAPI_WORKING_SET_INFORMATION)); |
| 53 | + workingSetInfo = reinterpret_cast<PSAPI_WORKING_SET_INFORMATION*>(workingSetBuffer.data()); |
| 54 | + success = QueryWorkingSet(g_process, workingSetBuffer.data(), workingSetBuffer.size()); |
| 55 | + } |
| 56 | + |
| 57 | + if (success) |
| 58 | + { |
| 59 | + for (ULONG_PTR i = 0; i < workingSetInfo->NumberOfEntries; ++i) |
| 60 | + { |
| 61 | + const PSAPI_WORKING_SET_BLOCK& block = workingSetInfo->WorkingSetInfo[i]; |
| 62 | + |
| 63 | + if (block.Shared) |
| 64 | + { |
| 65 | + out.sharedMemorySize++; |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + out.privateMemorySize++; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + static size_t pageSize = ([]() { |
| 74 | + SYSTEM_INFO sysInfo{}; |
| 75 | + GetSystemInfo(&sysInfo); |
| 76 | + return static_cast<size_t>(sysInfo.dwPageSize); |
| 77 | + })(); |
| 78 | + |
| 79 | + out.sharedMemorySize *= pageSize; |
| 80 | + out.privateMemorySize *= pageSize; |
| 81 | + out.residentMemorySize = out.sharedMemorySize + out.privateMemorySize; |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + PROCESS_MEMORY_COUNTERS_EX memoryInfo{}; |
| 86 | + |
| 87 | + if (!GetProcessMemoryInfo(g_process, reinterpret_cast<PPROCESS_MEMORY_COUNTERS>(&memoryInfo), sizeof(memoryInfo))) |
| 88 | + return false; |
| 89 | + |
| 90 | + out.residentMemorySize = memoryInfo.WorkingSetSize; |
| 91 | + } |
| 92 | + |
| 93 | + return true; |
| 94 | +#elif defined(__APPLE__) |
| 95 | + struct task_basic_info info; |
| 96 | + mach_msg_type_number_t info_count = TASK_BASIC_INFO_COUNT; |
| 97 | + |
| 98 | + if (task_info(mach_task_self(), TASK_BASIC_INFO, reinterpret_cast<task_info_t>(&info), &info_count) != KERN_SUCCESS) |
| 99 | + return false; |
| 100 | + |
| 101 | + static const size_t pageSize = static_cast<size_t>(getpagesize()); |
| 102 | + out.virtualMemorySize = info.virtual_size * pageSize; |
| 103 | + out.residentMemorySize = info.resident_size * pageSize; |
| 104 | + return true; |
| 105 | +#else |
| 106 | + long rawPageSize = sysconf(_SC_PAGE_SIZE); |
| 107 | + |
| 108 | + if (rawPageSize <= 0) |
| 109 | + return false; |
| 110 | + |
| 111 | + auto pageSize = static_cast<size_t>(rawPageSize); |
| 112 | + |
| 113 | + std::ifstream stat("/proc/self/statm"); |
| 114 | + |
| 115 | + if (!stat.is_open()) |
| 116 | + return false; |
| 117 | + |
| 118 | + size_t size = 0, resident = 0, shared = 0; |
| 119 | + stat >> size >> resident >> shared; |
| 120 | + stat.close(); |
| 121 | + |
| 122 | + out.virtualMemorySize = size * pageSize; |
| 123 | + out.residentMemorySize = resident * pageSize; |
| 124 | + out.sharedMemorySize = shared * pageSize; |
| 125 | + |
| 126 | + if (out.residentMemorySize > out.sharedMemorySize) |
| 127 | + out.privateMemorySize = out.residentMemorySize - out.sharedMemorySize; |
| 128 | + return true; |
| 129 | +#endif |
| 130 | + } |
| 131 | +} |
0 commit comments