-
Notifications
You must be signed in to change notification settings - Fork 6
/
ProcReader.h
131 lines (108 loc) · 5.76 KB
/
ProcReader.h
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
#ifndef PROC_READER_H
#define PROC_READER_H PROC_READER_H
#include "ProcCache.h"
#include <set>
#include <string>
#include <vector>
typedef enum {
Name, ///< executable file name
State, ///< process state
PID, ///< process ID
PPID, ///< parent process ID
PGRP, ///< process group ID
AvgCPUPerc, ///< average CPU Utilization, in percent
CurCPUPerc, ///< current utilization since last iteration, in percent
MinFlt, ///< minor page faults
MajFlt, ///< major page faults
UserTimeJiffies, ///< total time spent in user mode, in jiffies
SystemTimeJiffies, ///< total time spent in kernel mode, in jiffies
UserTimePerc, ///< total time spent in user mode, in percent
SystemTimePerc, ///< total time spent in kernel mode, in percent
Priority, ///< process priority
Nice, ///< process nice value
Threads, ///< number of threads
StartTimeJiffies, ///< start time, in jiffies
RunTimeSecs, ///< total runtime, in seconds
VmPeakkB, ///< peak virtual memory size, in kB
VmSizekB, ///< virtual (allocated) memory size, in kB
VmLckkB, ///< locked (non-swapable) memory size, in kB
VmHWMkB, ///< peak resident set size ("high water mark"), in kB
VmRSSkB, ///< resident set (touched memory currently in RAM) size, in kB
VmSwapkB, ///< swap usage, in kB
TotReadBytes, ///< total bytes read (incl. cached)
CurReadBytes, ///< current bytes read (incl. cached), per second
TotReadBytesStorage, ///< total bytes read (excl. cached)
CurReadBytesStorage, ///< current bytes read (excl. cached), per second
TotWrittenBytes, ///< total bytes written (incl. cached)
CurWrittenBytes, ///< current bytes written (incl. cached), per second
TotWrittenBytesStorage, ///< total bytes written (excl. cached)
CurWrittenBytesStorage, ///< current bytes written (excl. cached), per second
TotReadCalls, ///< total calls to read()/pread()
CurReadCalls, ///< current calls to read()/pread()
TotWriteCalls, ///< total calls to write()/pwrite()
CurWriteCalls, ///< current calls to write()/pwrite()
StatusColumnCount
} StatusColumns;
const std::string statusColumnHeader[] = {
"Name", "State", "PID", "PPID", "PGRP", "AvgCPUPerc", "CurCPUPerc", "MinFlt", "MajFlt",
"UserTimeJiffies", "SystemTimeJiffies", "UserTimePerc", "SystemTimePerc",
"Priority", "Nice", "Threads", "StartTimeJiffies", "RunTimeSecs",
"VmPeakkB", "VmSizekB", "VmLckkB", "VmHWMkB", "VmRsskB", "VmSwapkB",
"TotReadBytes", "CurReadBytesPerSec", "TotReadBytesStorage", "CurReadBytesStoragePerSec",
"TotWrittenBytes", "CurWrittenBytesPerSec", "TotWrittenBytesStorage", "CurWrittenBytesStoragePerSec",
"TotReadCalls", "CurReadCalls", "TotWriteCalls", "CurWriteCalls"
};
/// stores all relevant data from /proc/pid/
typedef std::vector<std::string> ProcessStatus;
/// stores all current PIDs from /proc/
typedef std::set<std::string> PIDSet;
/// reads and processes various data from /proc/pid/
class ProcReader {
public:
/// constructs a ProcReader object for the given PID
ProcReader(const std::string& processID);
/// reads all interesting information from /proc,
/// combines @ref readProcessStat(), @ref readProcessStatus() and @ref readProcessIO()
void readAll();
/// parses various information from /proc/pid/stat
void readProcessStat();
/// parses memory-related information from /proc/pid/status
/// @note: we parse the memory-related information from /proc/pid/status
/// instead of/proc/pid/stat because status has more information
void readProcessStatus();
/// parses IO information from /proc/pid/io
void readProcessIO();
/// updates data cache, has to be called before any of the calc functions
/// @note don't call multiple times
void updateCache();
/// processes all read information,
/// combines @ref calcRuntime(), @ref calcUserSystemTimes(),
/// @ref calcCPUUtilization() and @ref calcIOUtilization()
void calcAll(const Cache& oldCache, const double elapsedSecs);
/// calculates total process runtime in seconds
/// and fills @ref runTimeSecs in @p cache
void calcRuntime();
/// calculates user and system times in percent
void calcUserSystemTimes();
/// calculates average and current CPU usage
void calcCPUUtilization(const Cache& oldCache, const double elapsedSecs);
/// calculates current IO load
void calcIOUtilization(const Cache& oldCache, const double elapsedSecs);
/// returns whether this process is a kernel thread
bool isKernelThread() const { return status[PGRP] == "0"; }
/// returns data we have read and processed
const ProcessStatus& getProcessStatus() const { return status; }
/// returns internal data cache
const Cache& getCache() const { return cache; }
/// returns a set of all current PIDs
static PIDSet pids();
private:
std::string pid; ///< PID to read, stored as string for performance reasons (requires no conversions)
bool hasRead; ///< stores if we have read any data from /proc at all
ProcessStatus status; ///< data we have read and processed
Cache cache; ///< cache for read data
bool canReadStat; ///< can we read /proc/pid/stat?
bool canReadStatus; ///< can we read /proc/pid/status?
bool canReadIO; ///< can we read /proc/pid/io?
};
#endif // PROC_READER_H