|
3 | 3 | #if defined(_WIN32) |
4 | 4 | #include <cstdlib> |
5 | 5 | #include <windows.h> |
| 6 | + |
| 7 | +#include <psapi.h> |
| 8 | +#elif defined(__APPLE__) |
| 9 | +#include <mach/mach.h> |
| 10 | +#include <sys/sysctl.h> |
| 11 | +#include <unistd.h> |
6 | 12 | #else |
| 13 | +#include <fstream> |
| 14 | +#include <sstream> |
7 | 15 | #include <unistd.h> |
8 | 16 | #endif |
9 | 17 |
|
@@ -59,4 +67,155 @@ dataStorage GetAvailableStorageOnDrive(const std::filesystem::path& directory) |
59 | 67 | return storage; |
60 | 68 | } |
61 | 69 | #endif |
| 70 | + |
| 71 | +// ============================================================================= |
| 72 | +// GetSystemMemoryInfo — platform implementations |
| 73 | +// ============================================================================= |
| 74 | + |
| 75 | +#if defined(_WIN32) |
| 76 | + |
| 77 | +SystemMemoryInfo GetSystemMemoryInfo() |
| 78 | +{ |
| 79 | + SystemMemoryInfo info; |
| 80 | + |
| 81 | + MEMORYSTATUSEX memStatus; |
| 82 | + memStatus.dwLength = sizeof(MEMORYSTATUSEX); |
| 83 | + if(GlobalMemoryStatusEx(&memStatus)) |
| 84 | + { |
| 85 | + info.totalGB = static_cast<double>(memStatus.ullTotalPhys) / (1024.0 * 1024.0 * 1024.0); |
| 86 | + const double availableGB = static_cast<double>(memStatus.ullAvailPhys) / (1024.0 * 1024.0 * 1024.0); |
| 87 | + info.usedGB = info.totalGB - availableGB; |
| 88 | + info.loadPercent = static_cast<double>(memStatus.dwMemoryLoad); |
| 89 | + } |
| 90 | + |
| 91 | + PROCESS_MEMORY_COUNTERS_EX pmc; |
| 92 | + const HANDLE hProcess = GetCurrentProcess(); |
| 93 | + if(GetProcessMemoryInfo(hProcess, reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), sizeof(pmc))) |
| 94 | + { |
| 95 | + info.processGB = static_cast<double>(pmc.WorkingSetSize) / (1024.0 * 1024.0 * 1024.0); |
| 96 | + } |
| 97 | + |
| 98 | + return info; |
| 99 | +} |
| 100 | + |
| 101 | +#elif defined(__APPLE__) |
| 102 | + |
| 103 | +namespace |
| 104 | +{ |
| 105 | +// Helper: read a 64-bit integer sysctl value by name. |
| 106 | +int sysctlInt64(const char* name, int64_t* value) |
| 107 | +{ |
| 108 | + size_t len = sizeof(int64_t); |
| 109 | + return sysctlbyname(name, value, &len, nullptr, 0); |
| 110 | +} |
| 111 | +} // namespace |
| 112 | + |
| 113 | +SystemMemoryInfo GetSystemMemoryInfo() |
| 114 | +{ |
| 115 | + SystemMemoryInfo info; |
| 116 | + |
| 117 | + int64_t tempInt64 = 0; |
| 118 | + |
| 119 | + if(sysctlInt64("hw.memsize", &tempInt64) != 0) |
| 120 | + { |
| 121 | + return info; |
| 122 | + } |
| 123 | + info.totalGB = static_cast<double>(tempInt64) / (1024.0 * 1024.0 * 1024.0); |
| 124 | + |
| 125 | + vm_statistics64_data_t vmstat; |
| 126 | + mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; |
| 127 | + if(host_statistics64(mach_host_self(), HOST_VM_INFO64, reinterpret_cast<host_info64_t>(&vmstat), &count) == KERN_SUCCESS) |
| 128 | + { |
| 129 | + if(sysctlInt64("hw.pagesize", &tempInt64) == 0) |
| 130 | + { |
| 131 | + const int64_t availableBytes = (static_cast<int64_t>(vmstat.free_count) + static_cast<int64_t>(vmstat.inactive_count)) * tempInt64; |
| 132 | + const double availableGB = static_cast<double>(availableBytes) / (1024.0 * 1024.0 * 1024.0); |
| 133 | + info.usedGB = info.totalGB - availableGB; |
| 134 | + if(info.totalGB > 0.0) |
| 135 | + { |
| 136 | + info.loadPercent = info.usedGB * 100.0 / info.totalGB; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + task_vm_info_data_t taskInfo; |
| 142 | + mach_msg_type_number_t taskCount = TASK_VM_INFO_COUNT; |
| 143 | + if(task_info(mach_task_self(), TASK_VM_INFO, reinterpret_cast<task_info_t>(&taskInfo), &taskCount) == KERN_SUCCESS) |
| 144 | + { |
| 145 | + info.processGB = static_cast<double>(taskInfo.phys_footprint) / (1024.0 * 1024.0 * 1024.0); |
| 146 | + } |
| 147 | + |
| 148 | + return info; |
| 149 | +} |
| 150 | + |
| 151 | +#else // Linux |
| 152 | + |
| 153 | +SystemMemoryInfo GetSystemMemoryInfo() |
| 154 | +{ |
| 155 | + SystemMemoryInfo info; |
| 156 | + |
| 157 | + // Parse /proc/meminfo for system-wide memory figures |
| 158 | + { |
| 159 | + std::ifstream file("/proc/meminfo"); |
| 160 | + if(!file.is_open()) |
| 161 | + { |
| 162 | + return info; |
| 163 | + } |
| 164 | + |
| 165 | + uint64_t memTotal = 0; |
| 166 | + uint64_t memFree = 0; |
| 167 | + uint64_t buffers = 0; |
| 168 | + uint64_t cached = 0; |
| 169 | + uint64_t sReclaimable = 0; |
| 170 | + |
| 171 | + std::string line; |
| 172 | + while(std::getline(file, line)) |
| 173 | + { |
| 174 | + std::istringstream iss(line); |
| 175 | + std::string key; |
| 176 | + uint64_t value = 0; |
| 177 | + std::string unit; |
| 178 | + iss >> key >> value >> unit; |
| 179 | + |
| 180 | + if(key == "MemTotal:") |
| 181 | + memTotal = value; |
| 182 | + else if(key == "MemFree:") |
| 183 | + memFree = value; |
| 184 | + else if(key == "Buffers:") |
| 185 | + buffers = value; |
| 186 | + else if(key == "Cached:") |
| 187 | + cached = value; |
| 188 | + else if(key == "SReclaimable:") |
| 189 | + sReclaimable = value; |
| 190 | + } |
| 191 | + |
| 192 | + // Match the 'used' calculation from the 'free' command: |
| 193 | + // used = total - (free + buffers + cached + SReclaimable) |
| 194 | + const uint64_t usedKB = memTotal - (memFree + buffers + cached + sReclaimable); |
| 195 | + info.totalGB = static_cast<double>(memTotal) / (1024.0 * 1024.0); |
| 196 | + info.usedGB = static_cast<double>(usedKB) / (1024.0 * 1024.0); |
| 197 | + if(info.totalGB > 0.0) |
| 198 | + { |
| 199 | + info.loadPercent = info.usedGB * 100.0 / info.totalGB; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + // Read current process resident set size from /proc/self/statm |
| 204 | + { |
| 205 | + std::ifstream statm("/proc/self/statm"); |
| 206 | + if(statm.is_open()) |
| 207 | + { |
| 208 | + unsigned long long totalPages = 0; |
| 209 | + unsigned long long residentPages = 0; |
| 210 | + statm >> totalPages >> residentPages; |
| 211 | + const long pageSizeBytes = sysconf(_SC_PAGESIZE); |
| 212 | + info.processGB = static_cast<double>(residentPages) * static_cast<double>(pageSizeBytes) / (1024.0 * 1024.0 * 1024.0); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + return info; |
| 217 | +} |
| 218 | + |
| 219 | +#endif |
| 220 | + |
62 | 221 | } // namespace nx::core::Memory |
0 commit comments