Skip to content

Commit 4addb9f

Browse files
committed
Implement process memory usage for Linux.
1 parent dffe2cb commit 4addb9f

File tree

1 file changed

+70
-17
lines changed

1 file changed

+70
-17
lines changed

Common/Cpp/MemoryUtilization/MemoryUtilization_Linux.tpp

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,56 @@ namespace PokemonAutomation{
1919

2020

2121

22+
uint64_t parse_integer(const char*& ptr){
23+
uint64_t ret = 0;
24+
25+
// Skip the white space.
26+
while (true){
27+
char ch = *ptr;
28+
switch (ch){
29+
case '\0':
30+
return ret;
31+
case ' ':
32+
case '\t':
33+
ptr++;
34+
continue;
35+
default:;
36+
}
37+
break;
38+
}
39+
40+
// Parse the integer.
41+
while (true){
42+
char ch = *ptr++;
43+
if (ch < '0' || ch > '9'){
44+
break;
45+
}
46+
ret *= 10;
47+
ret += ch - '0';
48+
}
49+
50+
return ret;
51+
}
52+
2253
MemoryUsage process_memory_usage(){
2354
MemoryUsage usage;
2455

56+
uint64_t page_size = sysconf(_SC_PAGE_SIZE);
2557

26-
usage.total_system_memory = (uint64_t)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
58+
usage.total_system_memory = (uint64_t)sysconf(_SC_PHYS_PAGES) * page_size;
2759

2860
{
2961
uint64_t bytes = 0;
3062

63+
// Running this at our poll rate is a terrible idea.
64+
#if 1
3165
const std::string TOKEN = "MemAvailable:";
3266

3367
std::ifstream file("/proc/meminfo");
3468
std::string line;
3569
while (std::getline(file, line)){
70+
// cout << line << endl;
71+
3672
size_t pos = line.find(TOKEN);
3773
if (pos == std::string::npos){
3874
continue;
@@ -41,33 +77,50 @@ MemoryUsage process_memory_usage(){
4177
// Skip the token.
4278
pos += TOKEN.size();
4379

44-
// Skip the white space.
45-
while (line[pos] != '\0' && line[pos] == ' '){
46-
pos++;
47-
}
48-
49-
// Parse the integer.
50-
while (true){
51-
char ch = line[pos++];
52-
if (ch < '0' || ch > '9'){
53-
break;
54-
}
55-
bytes *= 10;
56-
bytes += ch - '0';
57-
}
58-
bytes *= 1024;
80+
const char* ptr = &line[pos];
81+
bytes = parse_integer(ptr) * 1024;
5982

6083
break;
6184
}
85+
#endif
6286

6387
if (bytes == 0){
64-
bytes = (uint64_t)sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
88+
bytes = (uint64_t)sysconf(_SC_AVPHYS_PAGES) * page_size;
6589
}
6690
bytes = std::min(bytes, usage.total_system_memory);
6791

6892
usage.total_used_system_memory = usage.total_system_memory - bytes;
6993
}
7094

95+
uint64_t swapped = 0;
96+
{
97+
const std::string VmRSS = "VmRSS:";
98+
const std::string VmSwap = "VmSwap:";
99+
100+
pid_t current_pid = getpid();
101+
std::ifstream file("/proc/" + std::to_string(current_pid) + "/status");
102+
std::string line;
103+
while (std::getline(file, line)){
104+
// cout << line << endl;
105+
106+
size_t pos = line.find(VmRSS);
107+
if (pos != std::string::npos){
108+
pos += VmRSS.size();
109+
const char* ptr = &line[pos];
110+
usage.process_physical_memory = parse_integer(ptr) * 1024;
111+
continue;
112+
}
113+
pos = line.find(VmSwap);
114+
if (pos != std::string::npos){
115+
pos += VmSwap.size();
116+
const char* ptr = &line[pos];
117+
swapped = parse_integer(ptr) * 1024;
118+
continue;
119+
}
120+
}
121+
usage.process_virtual_memory = usage.process_physical_memory + swapped;
122+
}
123+
71124
return usage;
72125
}
73126

0 commit comments

Comments
 (0)