13 #ifndef __STOUT_PROC_HPP__ 14 #define __STOUT_PROC_HPP__ 18 #error "stout/proc.hpp is only available on Linux systems." 24 #include <sys/types.h> 56 const std::string& _comm,
64 unsigned long _minflt,
65 unsigned long _cminflt,
66 unsigned long _majflt,
67 unsigned long _cmajflt,
76 unsigned long long _starttime,
79 unsigned long _rsslim,
80 unsigned long _startcode,
81 unsigned long _endcode,
82 unsigned long _startstack,
83 unsigned long _kstkeip,
84 unsigned long _signal,
85 unsigned long _blocked,
86 unsigned long _sigcatch,
89 unsigned long _cnswap)
180 std::istringstream data(read.
get());
229 data >> _ >> comm >> state >> ppid >> pgrp >> session >> tty_nr
230 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
231 >> utime >> stime >> cutime >> cstime >> priority >> nice
232 >> num_threads >> itrealvalue >> starttime >> vsize >> rss
233 >> rsslim >> startcode >> endcode >> startstack >> kstkeip
234 >> signal >> blocked >> sigcatch >> wchan >> nswap >>
cnswap;
237 if (data.fail() && !data.eof()) {
238 return Error(
"Failed to read/parse '" + path +
"'");
247 return ProcessStatus(pid, comm, state, ppid, pgrp, session, tty_nr,
248 tpgid, flags, minflt, cminflt, majflt, cmajflt,
249 utime, stime, cutime, cstime, priority, nice,
250 num_threads, itrealvalue, starttime, vsize, rss,
251 rsslim, startcode, endcode, startstack, kstkeip,
252 signal, blocked, sigcatch, wchan, nswap, cnswap);
258 const std::string
path =
pid.isSome()
262 std::ifstream
file(path.c_str());
264 if (!
file.is_open()) {
271 return Error(
"Failed to open '" + path +
"'");
274 std::stringbuf buffer;
278 file.get(buffer,
'\0');
282 return Error(
"Failed to read '" + path +
"'");
283 }
else if (!
file.eof()) {
287 }
while (!
file.eof());
296 std::set<pid_t>
pids;
300 return Error(
"Failed to list files in /proc: " + entries.
error());
303 foreach (
const std::string& entry, entries.
get()) {
306 pids.insert(pid.
get());
314 return Error(
"Failed to determine pids from /proc");
327 return Error(
"Failed to list files in " + path +
": " + entries.
error());
330 foreach (
const std::string& entry, entries.
get()) {
333 threads.insert(thread.
get());
337 if (!threads.empty()) {
341 return Error(
"Failed to determine thread ids from /proc");
358 unsigned long long btime = 0;
360 std::ifstream
file(
"/proc/stat");
362 if (!file.is_open()) {
363 return Error(
"Failed to open /proc/stat");
367 while (std::getline(file, line)) {
368 if (line.find(
"btime ") == 0) {
370 numify<unsigned long long>(line.substr(6));
373 return Error(
"Failed to parse /proc/stat: " + number.
error());
376 btime = number.
get();
381 if (file.fail() && !file.eof()) {
382 return Error(
"Failed to read /proc/stat");
393 CPU(
unsigned int _id,
unsigned int _core,
unsigned int _socket)
394 :
id(_id), core(_core),
socket(_socket) {}
423 return lhs.
id < rhs.
id;
429 return stream <<
"CPU (id:" << cpu.
id <<
", " 430 <<
"core:" << cpu.
core <<
", " 431 <<
"socket:" << cpu.
socket <<
")";
438 std::list<CPU> results;
440 std::ifstream
file(
"/proc/cpuinfo");
442 if (!file.is_open()) {
443 return Error(
"Failed to open /proc/cpuinfo");
452 while (std::getline(file, line)) {
453 if (line.find(
"processor") == 0 ||
454 line.find(
"physical id") == 0 ||
455 line.find(
"core id") == 0) {
459 if (tokens.size() < 2) {
460 return Error(
"Unexpected format in /proc/cpuinfo: " +
470 if (line.find(
"processor") == 0) {
473 results.push_back(
CPU(
id.
get(), 0, 0));
476 }
else if (line.find(
"physical id") == 0) {
478 return Error(
"Unexpected format in /proc/cpuinfo");
480 socket = value.
get();
481 }
else if (line.find(
"core id") == 0) {
483 return Error(
"Unexpected format in /proc/cpuinfo");
490 results.push_back(
CPU(
id.
get(), core.
get(), socket.
get()));
501 results.push_back(
CPU(
id.
get(), 0, 0));
504 if (file.fail() && !file.eof()) {
505 return Error(
"Failed to read /proc/cpuinfo");
513 #endif // __STOUT_PROC_HPP__ CPU(unsigned int _id, unsigned int _core, unsigned int _socket)
Definition: proc.hpp:393
bool exists(const std::string &path)
Definition: exists.hpp:26
const pid_t pgrp
Definition: proc.hpp:130
const unsigned long signal
Definition: proc.hpp:155
const unsigned long cmajflt
Definition: proc.hpp:138
Definition: errorbase.hpp:36
const pid_t tpgid
Definition: proc.hpp:133
const unsigned long vsize
Definition: proc.hpp:148
const unsigned long long btime
Definition: proc.hpp:350
const unsigned long minflt
Definition: proc.hpp:135
T & get()&
Definition: try.hpp:80
const unsigned long majflt
Definition: proc.hpp:137
const unsigned long cnswap
Definition: proc.hpp:160
bool operator==(const CPU &lhs, const CPU &rhs)
Definition: proc.hpp:403
unsigned int core
Definition: proc.hpp:398
unsigned int id
Definition: proc.hpp:397
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
const pid_t ppid
Definition: proc.hpp:129
const unsigned long cminflt
Definition: proc.hpp:136
bool operator<(const CPU &lhs, const CPU &rhs)
Definition: proc.hpp:410
const unsigned long nswap
Definition: proc.hpp:159
const long num_threads
Definition: proc.hpp:145
std::string join(const std::string &path1, const std::string &path2, const char _separator=os::PATH_SEPARATOR)
Definition: path.hpp:116
const unsigned long endcode
Definition: proc.hpp:152
const long cutime
Definition: proc.hpp:141
const unsigned long blocked
Definition: proc.hpp:156
bool isSome() const
Definition: option.hpp:116
std::string remove(const std::string &from, const std::string &substring, Mode mode=ANY)
Definition: strings.hpp:41
const long rss
Definition: proc.hpp:149
const unsigned long startcode
Definition: proc.hpp:151
const unsigned long wchan
Definition: proc.hpp:158
DWORD pid_t
Definition: windows.hpp:181
std::vector< std::string > tokenize(const std::string &s, const std::string &delims, const Option< size_t > &maxTokens=None())
Definition: strings.hpp:139
const long priority
Definition: proc.hpp:143
Definition: strings.hpp:36
URI file(const std::string &path)
Creates a file URI with the given path on the local host.
Definition: file.hpp:33
const unsigned long utime
Definition: proc.hpp:139
SystemStatus(unsigned long long _btime)
Definition: proc.hpp:348
const long cstime
Definition: proc.hpp:142
Try< std::set< pid_t > > pids()
Definition: proc.hpp:294
const unsigned long long starttime
Definition: proc.hpp:147
bool isSome() const
Definition: try.hpp:77
const T & get() const &
Definition: option.hpp:119
Try< std::set< pid_t > > threads(pid_t pid)
Definition: proc.hpp:319
const pid_t session
Definition: proc.hpp:131
const unsigned long startstack
Definition: proc.hpp:153
std::ostream & operator<<(std::ostream &stream, const CPU &cpu)
Definition: proc.hpp:427
static Try error(const E &e)
Definition: try.hpp:43
const char state
Definition: proc.hpp:128
const unsigned int flags
Definition: proc.hpp:134
Result< std::string > read(int_fd fd, size_t size)
Definition: read.hpp:55
Result< std::string > cmdline(const Option< pid_t > &pid=None())
Definition: proc.hpp:256
const pid_t pid
Definition: proc.hpp:126
bool isError() const
Definition: try.hpp:78
Result< Credentials > read(const Path &path)
Definition: credentials.hpp:35
const long itrealvalue
Definition: proc.hpp:146
const std::string comm
Definition: proc.hpp:127
const long nice
Definition: proc.hpp:144
const unsigned long rsslim
Definition: proc.hpp:150
unsigned int socket
Definition: proc.hpp:399
const unsigned long stime
Definition: proc.hpp:140
ProcessStatus(pid_t _pid, const std::string &_comm, char _state, pid_t _ppid, pid_t _pgrp, pid_t _session, int _tty_nr, pid_t _tpgid, unsigned int _flags, unsigned long _minflt, unsigned long _cminflt, unsigned long _majflt, unsigned long _cmajflt, unsigned long _utime, unsigned long _stime, long _cutime, long _cstime, long _priority, long _nice, long _num_threads, long _itrealvalue, unsigned long long _starttime, unsigned long _vsize, long _rss, unsigned long _rsslim, unsigned long _startcode, unsigned long _endcode, unsigned long _startstack, unsigned long _kstkeip, unsigned long _signal, unsigned long _blocked, unsigned long _sigcatch, unsigned long _wchan, unsigned long _nswap, unsigned long _cnswap)
Definition: proc.hpp:54
const unsigned long sigcatch
Definition: proc.hpp:157
const int tty_nr
Definition: proc.hpp:132
Try< std::list< CPU > > cpus()
Definition: proc.hpp:436
Try< Netlink< struct nl_sock > > socket(int protocol=NETLINK_ROUTE)
Definition: internal.hpp:91
std::string stringify(int flags)
const unsigned long kstkeip
Definition: proc.hpp:154
Try< std::list< std::string > > ls(const std::string &directory)
Definition: ls.hpp:29
Definition: strings.hpp:35