-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryMonitor.cxx
136 lines (117 loc) · 3.53 KB
/
MemoryMonitor.cxx
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
____ _ __ ____ __ ____
/ __/___(_) / ___ ____/ __ \__ _____ ___ / /_ / _/__ ____
_\ \/ __/ / _ \/ -_) __/ /_/ / // / -_|_-</ __/ _/ // _ \/ __/
/___/\__/_/_.__/\__/_/ \___\_\_,_/\__/___/\__/ /___/_//_/\__(_)
Copyright 2012 SciberQuest Inc.
*/
#include "MemoryMonitor.h"
#include "FsUtils.h"
#include "SQMacros.h"
#include <vector>
using std::vector;
#include <string>
using std::string;
//-----------------------------------------------------------------------------
MemoryMonitor::MemoryMonitor()
:
SystemTotal(0)
{
// get the total system memory
int ok;
vector<string> meminfo;
ok=LoadLines("/proc/meminfo",meminfo);
if (!ok)
{
sqErrorMacro(cerr,"Failed to open /proc/meminfo.");
return;
}
ok=NameValue(meminfo,"MemTotal:",this->SystemTotal);
if (!ok)
{
sqErrorMacro(cerr,"Failed to get the total system memory.");
return;
}
}
//-----------------------------------------------------------------------------
float MemoryMonitor::GetVmRSSPercent()
{
return (float)this->GetVmRSS()/(float)this->GetSystemTotal();
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetVmRSS()
{
return this->GetStatusField("VmRSS:");
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetVmPeak()
{
return this->GetStatusField("VmPeak:");
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetVmSize()
{
return this->GetStatusField("VmSize:");
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetVmLock()
{
return this->GetStatusField("VmLck:");
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetVmHWM()
{
return this->GetStatusField("VmHWM:");
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetVmData()
{
return this->GetStatusField("VmData:");
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetVmStack()
{
return this->GetStatusField("VmStk:");
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetVmExec()
{
return this->GetStatusField("VmExe:");
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetVmLib()
{
return this->GetStatusField("VmLib:");
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetVmPTE()
{
return this->GetStatusField("VmPTE:");
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetVmSwap()
{
return this->GetStatusField("VmSwap:");
}
//-----------------------------------------------------------------------------
unsigned long long MemoryMonitor::GetStatusField(const char *name)
{
// load a fresh copy of /proc/self/status get the value from name
// value pairs there in
int ok;
vector<string> status;
ok=LoadLines("/proc/self/status",status);
if (!ok)
{
sqErrorMacro(cerr,"Failed to open /proc/self/status.");
return -1;
}
unsigned long long value;
ok=NameValue(status,name,value);
if (!ok)
{
sqErrorMacro(cerr,"Failed to find " << name << ".");
return -1;
}
return value;
}