-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cc
More file actions
69 lines (62 loc) · 1.4 KB
/
memory.cc
File metadata and controls
69 lines (62 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <fstream>
#include <iostream>
#include "memory.h"
#include <stdlib.h>
using namespace std;
int main()
{
Memory mem = getMemoryInfo();
cout << "memTotal: " << mem.MemTotal << endl;
cout << "memAvailable: " << mem.MemAvailable << endl;
}
Memory getMemoryInfo()
{
string data;
string memTotal;
string memAvailable;
ifstream infile;
infile.open("/proc/meminfo");
// debug
// infile.open("/root/meminfo");
while (infile >> data)
{
if (data== "MemTotal:") {
infile >> memTotal;
}
if (data == "MemAvailable:")
{
infile >> memAvailable;
}
}
infile.close();
Memory mem;
mem.MemAvailable = atoi(memAvailable.c_str());
mem.MemTotal = atoi(memTotal.c_str()) ;
return mem;
}
Memory getMemoryInfoInDocker()
{
string data;
string memTotal;
string memAvailable;
string memUsage;
ifstream infile;
infile.open("cat /sys/fs/cgroup/memory/memory.limit_in_bytes");
while (infile >> data)
{
if (data == "hierarchical_memory_limit")
{
infile >> memTotal;
}
if (data == "rss")
{
infile >> memUsage;
}
}
infile.close();
Memory mem;
mem.MemTotal = atoi(memTotal.c_str());
mem.MemUsage = atoi(memTotal.c_str());
mem.MemAvailable = mem.MemTotal - mem.MemUsage;
return mem;
}