Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
memory.cc
Go to the documentation of this file.
1 
18 #include "ion/port/memory.h"
19 
20 #if defined(ION_PLATFORM_LINUX) || defined(ION_PLATFORM_ANDROID) || \
21  defined(ION_PLATFORM_GENERIC_ARM)
22 #include <fstream> // NOLINT(readability/streams)
23 #include <sstream>
24 #endif
25 
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>
31 #endif
32 
33 #if defined(ION_PLATFORM_WINDOWS)
34 #include <windows.h>
36 #include <psapi.h> // NOLINT(build/include_alpha)
37 
38 #pragma comment(lib, "psapi")
39 #endif
40 
41 #include <assert.h> // For checking return values since port has no logging.
42 
43 namespace {
44 
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());
53  std::string line;
54  uint64 value = 0U;
55  while (std::getline(procfs_file, line)) {
56  std::stringstream line_stream(line);
57  std::string line_key, line_units;
58  uint64 line_value;
59  if (line_stream >> line_key && line_key == colon_key &&
60  line_stream >> line_value &&
61  line_stream >> line_units && line_units == "kB") {
62  value = line_value;
63  break;
64  }
65  }
66  assert(value);
67  static const int kKilobyte = 1024;
68  value *= kKilobyte;
69  return value;
70 }
71 #endif
72 
73 } // namespace
74 
75 namespace ion {
76 namespace port {
77 
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;
93 #else
94  return 0U;
95 #endif
96 }
97 
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)
103  int mib[2];
104  mib[0] = CTL_HW;
105  mib[1] = HW_MEMSIZE;
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;
116 #else
117  return 0U;
118 #endif
119 }
120 
121 } // namespace port
122 } // namespace ion
uint64 GetProcessResidentMemorySize()
Return size of the current process in bytes.
Definition: memory.cc:78
double value
uint64 GetSystemMemorySize()
Return the hardware RAM size in bytes.
Definition: memory.cc:98