-
Notifications
You must be signed in to change notification settings - Fork 6
/
ProcReader.cpp
288 lines (236 loc) · 8.95 KB
/
ProcReader.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "ProcReader.h"
#include "helper.h"
#include "definitions.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <cassert>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <dirent.h>
#include <errno.h>
ProcReader::ProcReader(const std::string& processID) :
pid(processID), hasRead(false), status(StatusColumnCount, "0.0"),
cache(), canReadStat(false), canReadStatus(false), canReadIO(false) {
// perform some checks
if (unlikely(!dirExists("/proc/" + pid))) {
return;
}
canReadStat = fileReadable("/proc/" + pid + "/stat");
canReadStatus = fileReadable("/proc/" + pid + "/status");
canReadIO = fileReadable("/proc/" + pid + "/io");
assert(canReadStat); // can this fail? where/when?
assert(canReadStatus); // can this fail? where/when?
}
void ProcReader::readAll() {
readProcessStat();
readProcessStatus();
readProcessIO();
}
void ProcReader::readProcessStat() {
assert(status.size() == StatusColumnCount);
if (!canReadStat)
return;
const std::string fileName = "/proc/" + pid + "/stat";
std::ifstream file(fileName.c_str(), std::ifstream::in);
if (unlikely(!file.good())) {
canReadStat = false; // process may already have been terminated
return;
}
std::string line;
std::getline(file, line);
assert(!line.empty());
std::stringstream sstr(line);
// read first field
sstr >> status[PID];
// second field is the executable name in brackets, may contain spaces and other bad characters
// example name from readproc.c ":-) 1 2 3 4 5 6" -> reverse search for closing bracket ')'
const std::string& tmpStr = sstr.str();
const size_t cmdStart = tmpStr.find("(") + 1;
const size_t cmdEnd = tmpStr.rfind(")");
status[Name] = tmpStr.substr(cmdStart, cmdEnd - cmdStart);
assert(!status[Name].empty());
// skip first two fields
sstr.seekg(cmdEnd + 1);
// remaining fields can be parsed easily as there are no unexpected spaces
std::string skip;
sstr >> status[State] >> status[PPID] >> status[PGRP]
>> skip >> skip >> skip >> skip
>> status[MinFlt] >> skip >> status[MajFlt] >> skip
>> status[UserTimeJiffies] >> status[SystemTimeJiffies] >> skip >> skip
>> status[Priority] >> status[Nice] >> status[Threads]
>> skip >> status[StartTimeJiffies];
hasRead = true;
}
void ProcReader::readProcessStatus() {
assert(status.size() == StatusColumnCount);
if (!canReadStatus)
return;
const std::string fileName = "/proc/" + pid + "/status";
std::ifstream file(fileName.c_str(), std::ifstream::in);
if (unlikely(!file.good())) {
canReadStatus = false; // process may already have been terminated
return;
}
std::string line;
while (std::getline(file, line)) {
assert(!line.empty());
std::stringstream sstr(line);
std::string name, value;
sstr >> name >> value;
if (!sstr) {
continue; // we just read some crap
}
assert(!name.empty());
assert(!value.empty() || name == "Groups:");
if (name == "VmPeak:") {
status[VmPeakkB] = value;
} else if (name == "VmSize:") {
status[VmSizekB] = value;
} else if (name == "VmLck:") {
status[VmLckkB] = value;
} else if (name == "VmHWM:") {
status[VmHWMkB] = value;
} else if (name == "VmRSS:") {
status[VmRSSkB] = value;
} else if (name == "VmSwap:") {
status[VmSwapkB] = value;
}
}
hasRead = true;
}
void ProcReader::readProcessIO() {
assert(status.size() == StatusColumnCount);
if (!canReadIO)
return;
const std::string fileName = "/proc/" + pid + "/io";
std::ifstream file(fileName.c_str(), std::ifstream::in);
if (unlikely(!file.good())) {
canReadIO = false; // process may already have been terminated
return;
}
std::string line;
while (std::getline(file, line)) {
if (unlikely(line.empty()))
continue; // happens sometimes when self-profiling for some mysterious reason
std::stringstream sstr(line);
assert(sstr);
std::string name, value;
sstr >> name >> value;
if (!sstr) {
continue; // we just read some crap
}
assert(!name.empty());
assert(!value.empty());
if (name == "rchar:") {
status[TotReadBytes] = value;
} else if (name == "wchar:") {
status[TotWrittenBytes] = value;
} else if (name == "read_bytes:") {
status[TotReadBytesStorage] = value;
} else if (name == "write_bytes:") {
status[TotWrittenBytesStorage] = value;
} else if (name == "syscr:") {
status[TotReadCalls] = value;
} else if (name == "syscw:") {
status[TotWriteCalls] = value;
}
}
hasRead = true;
}
void ProcReader::updateCache() {
assert(cache.isEmpty);
cache = Cache(status);
}
void ProcReader::calcAll(const Cache& oldCache, const double elapsedSecs) {
if (unlikely(cache.isEmpty)) {
assert(false);
return;
}
if (!hasRead) {
return; // process may already have been terminated
}
calcRuntime();
calcUserSystemTimes();
calcCPUUtilization(oldCache, elapsedSecs);
calcIOUtilization(oldCache, elapsedSecs);
}
void ProcReader::calcRuntime() {
if (unlikely(cache.isEmpty)) {
assert(false);
return;
}
const double systemRuntimeSecs = uptime();
const double processStarttimeSecs = cache.startTimeJiffies / (double)getHertz();
if (unlikely(systemRuntimeSecs - processStarttimeSecs <= 0)) {
cache.runTimeSecs = 0; // may happen with really short intervals shortly after process startup
} else {
cache.runTimeSecs = systemRuntimeSecs - processStarttimeSecs;
}
status[RunTimeSecs] = numberToString(cache.runTimeSecs); // required for output
}
void ProcReader::calcUserSystemTimes() {
if (unlikely(cache.isEmpty)) {
assert(false);
return;
}
const int totProcessCPUTimeJiffies = cache.userTimeJiffies + cache.systemTimeJiffies;
status[UserTimePerc] = numberToString(cache.userTimeJiffies * 100.0 / (double)totProcessCPUTimeJiffies);
status[SystemTimePerc] = numberToString(cache.systemTimeJiffies * 100.0 / (double)totProcessCPUTimeJiffies);
}
void ProcReader::calcCPUUtilization(const Cache& oldCache, const double elapsedSecs) {
if (unlikely(cache.isEmpty)) {
assert(false);
return;
}
if (unlikely(cache.runTimeSecs == 0)) { // really short interval and shortly after process startup
return;
}
const double totProcessCPUTimeSecs = (cache.userTimeJiffies + cache.systemTimeJiffies) / (double)getHertz();
status[AvgCPUPerc] = numberToString((totProcessCPUTimeSecs * 100.0) / cache.runTimeSecs);
if (oldCache.isEmpty) { // first iteration, cannot calculate current CPU
return;
}
const double oldTotProcessCPUTimeSecs = (oldCache.userTimeJiffies + oldCache.systemTimeJiffies) / (double)getHertz();
const double elapsedCPUTimeSecs = totProcessCPUTimeSecs - oldTotProcessCPUTimeSecs;
assert(elapsedCPUTimeSecs >= 0);
status[CurCPUPerc] = numberToString((elapsedCPUTimeSecs * 100.0) / elapsedSecs);
}
void ProcReader::calcIOUtilization(const Cache& oldCache, const double elapsedSecs) {
if (cache.isEmpty) {
assert(false);
return;
}
if (unlikely(cache.runTimeSecs == 0)) { // really short interval and shortly after process startup
return;
}
if (oldCache.isEmpty) // first iteration, cannot calculate current IO
return;
status[CurReadBytes] = numberToString((cache.totReadBytes - oldCache.totReadBytes) / elapsedSecs);
status[CurWrittenBytes] = numberToString((cache.totWrittenBytes - oldCache.totWrittenBytes) / elapsedSecs);
status[CurReadBytesStorage] = numberToString((cache.totReadBytesStorage - oldCache.totReadBytesStorage) / elapsedSecs);
status[CurWrittenBytesStorage] = numberToString((cache.totWrittenBytesStorage - oldCache.totWrittenBytesStorage) / elapsedSecs);
status[CurReadCalls] = numberToString((cache.totReadCalls - oldCache.totReadCalls) / elapsedSecs);
status[CurWriteCalls] = numberToString((cache.totWriteCalls - oldCache.totWriteCalls) / elapsedSecs);
}
PIDSet ProcReader::pids() {
PIDSet pidSet;
DIR* dir = opendir("/proc");
if (unlikely(!dir)) {
std::cerr << "could not open /proc:" << strerror(errno) << std::endl;
assert(false);
return pidSet;
}
struct dirent* entry;
while ((entry = readdir(dir))) {
assert(entry->d_name);
// first character is a number? -> name is a PID
if (std::isdigit(entry->d_name[0])) {
pidSet.insert(entry->d_name);
}
}
closedir(dir);
return pidSet;
}