-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtm_usage.cpp
More file actions
111 lines (97 loc) · 3.1 KB
/
Copy pathtm_usage.cpp
File metadata and controls
111 lines (97 loc) · 3.1 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
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
// **************************************************************************
// File [ tm_usage.cpp ]
// Author [ littleshamoo ]
// Synopsis [ functions to calculate CPU time and memory usage ]
// Date [ Ver 3.0 started 2010/12/20 ]
// **************************************************************************
#include <sys/resource.h> // for getrusage()
#include <sys/time.h> // for gettimeofday()
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include "tm_usage.h"
using namespace std;
using namespace CommonNs;
TmUsage::TmUsage() {
tStart_.uTime = 0;
tStart_.sTime = 0;
tStart_.rTime = 0;
tStart_.vmSize = 0;
tStart_.vmPeak = 0;
tStart_.vmDiff = 0;
pStart_.uTime = 0;
pStart_.sTime = 0;
pStart_.rTime = 0;
pStart_.vmSize = 0;
pStart_.vmPeak = 0;
pStart_.vmDiff = 0;
}
TmUsage::~TmUsage() {}
bool TmUsage::totalStart() {
return checkUsage(tStart_);
}
bool TmUsage::periodStart() {
return checkUsage(pStart_);
}
bool TmUsage::getTotalUsage(TmStat &st) const {
if (!checkUsage(st))
return false;
st.uTime -= tStart_.uTime;
st.sTime -= tStart_.sTime;
st.rTime -= tStart_.rTime;
st.vmDiff = st.vmSize - tStart_.vmSize;
st.vmPeak = st.vmPeak > tStart_.vmPeak ? st.vmPeak : tStart_.vmPeak;
return true;
}
bool TmUsage::getPeriodUsage(TmStat &st) const {
if (!checkUsage(st))
return false;
st.uTime -= pStart_.uTime;
st.sTime -= pStart_.sTime;
st.rTime -= pStart_.rTime;
st.vmDiff = st.vmSize - pStart_.vmSize;
st.vmPeak = st.vmPeak > pStart_.vmPeak ? st.vmPeak : pStart_.vmPeak;
return true;
}
// **************************************************************************
// Function [ checkUsage(TmStat &) ]
// Author [ littleshamoo ]
// Synopsis [ get user time and system time using getrusage() function and
// get real time using gettimeofday() function and read
// "/proc/self/status" to get memory usage ]
// **************************************************************************
// {{{ bool checkUsage(TmStat &) const
bool TmUsage::checkUsage(TmStat &st) const {
// check user time and system time
rusage tUsg;
timeval tReal;
getrusage(RUSAGE_SELF, &tUsg);
gettimeofday(&tReal, NULL);
st.uTime = tUsg.ru_utime.tv_sec * 1000000 + tUsg.ru_utime.tv_usec;
st.sTime = tUsg.ru_stime.tv_sec * 1000000 + tUsg.ru_stime.tv_usec;
st.rTime = tReal.tv_sec * 1000000 + tReal.tv_usec;
// check current memory and peak memory
FILE *fmem = fopen("/proc/self/status", "r");
if (!fmem) {
fprintf(stderr,
"**ERROR TmUsage::checkUsage(): cannot get memory usage\n");
st.vmSize = 0;
st.vmPeak = 0;
return false;
}
char membuf[128];
while (fgets(membuf, 128, fmem)) {
char *ch;
if ((ch = strstr(membuf, "VmPeak:"))) {
st.vmPeak = atol(ch + 7);
continue;
}
if ((ch = strstr(membuf, "VmSize:"))) {
st.vmSize = atol(ch + 7);
break;
}
}
fclose(fmem);
return true;
}
//}}}