20 #if defined(ION_PLATFORM_LINUX) || defined(ION_PLATFORM_ANDROID) || \
21 defined(ION_PLATFORM_GENERIC_ARM)
26 #if defined(ION_PLATFORM_MAC) || defined(ION_PLATFORM_IOS)
27 #include <mach/mach.h>
28 #include <sys/errno.h>
29 #include <sys/sysctl.h>
30 #include <sys/types.h>
33 #if defined(ION_PLATFORM_WINDOWS)
38 #pragma comment(lib, "psapi")
45 #if defined(ION_PLATFORM_LINUX) || defined(ION_PLATFORM_ANDROID) || \
46 defined(ION_PLATFORM_GENERIC_ARM)
47 static uint64 GetProcFSValue(
const std::string& filename,
49 const std::string& key) {
50 const std::string colon_key = key + std::string(
":");
51 std::ifstream procfs_file(filename);
52 assert(procfs_file.is_open());
55 while (std::getline(procfs_file, line)) {
56 std::stringstream line_stream(line);
57 std::string line_key, line_units;
59 if (line_stream >> line_key && line_key == colon_key &&
60 line_stream >> line_value &&
61 line_stream >> line_units && line_units ==
"kB") {
67 static const int kKilobyte = 1024;
79 #if defined(ION_PLATFORM_LINUX) || defined(ION_PLATFORM_ANDROID) || \
80 defined(ION_PLATFORM_GENERIC_ARM)
81 return GetProcFSValue(
"/proc/self/status",
"VmRSS");
82 #elif defined(ION_PLATFORM_MAC) || defined(ION_PLATFORM_IOS)
83 struct task_basic_info info;
84 mach_msg_type_number_t info_count = TASK_BASIC_INFO_COUNT;
85 const int error_code = task_info(mach_task_self(), TASK_BASIC_INFO,
86 (task_info_t)&info, &info_count);
87 assert(error_code == KERN_SUCCESS);
88 return info.resident_size;
89 #elif defined(ION_PLATFORM_WINDOWS)
90 PROCESS_MEMORY_COUNTERS pmc;
91 GetProcessMemoryInfo(GetCurrentProcess(), &pmc,
sizeof(pmc));
92 return pmc.WorkingSetSize;
99 #if defined(ION_PLATFORM_LINUX) || defined(ION_PLATFORM_ANDROID) || \
100 defined(ION_PLATFORM_GENERIC_ARM)
101 return GetProcFSValue(
"/proc/meminfo",
"MemTotal");
102 #elif defined(ION_PLATFORM_MAC) || defined(ION_PLATFORM_IOS)
106 int64 system_memory_size;
107 size_t size =
sizeof(system_memory_size);
108 const int error_code = sysctl(mib, 2, &system_memory_size, &size, NULL, 0);
109 assert(error_code != ENOMEM);
110 return system_memory_size;
111 #elif defined(ION_PLATFORM_WINDOWS)
112 MEMORYSTATUSEX memory_info;
113 memory_info.dwLength =
sizeof(MEMORYSTATUSEX);
114 GlobalMemoryStatusEx(&memory_info);
115 return memory_info.ullTotalPhys;
uint64 GetProcessResidentMemorySize()
Return size of the current process in bytes.
uint64 GetSystemMemorySize()
Return the hardware RAM size in bytes.