/lib/LinuxProcessMonitor.cpp

https://bitbucket.org/arild/thesis · C++ · 425 lines · 363 code · 60 blank · 2 comment · 12 complexity · 51bcbdaa42f73559ab341ce63227e0da MD5 · raw file

  1. #include "LinuxProcessMonitor.h"
  2. LinuxProcessMonitor::LinuxProcessMonitor()
  3. {
  4. _jiffy = sysconf(_SC_CLK_TCK);
  5. _buffer = new char[300]; // size is 200 but we use 300 for future versions (ubuntu version for example has many more fields...)
  6. memset(_buffer, 0, 300);
  7. _procstat = NULL;
  8. _procstatm = NULL;
  9. _procio = NULL;
  10. _utime = _stime = _rchar = _wchar = 0;
  11. _updateTime = 0.;
  12. }
  13. LinuxProcessMonitor::~LinuxProcessMonitor()
  14. {
  15. if (_procstat)
  16. fclose(_procstat);
  17. if (_procstatm)
  18. fclose(_procstatm);
  19. if (_procio)
  20. fclose(_procio);
  21. }
  22. bool LinuxProcessMonitor::open(int pid)
  23. {
  24. stringstream sstat, sstatm;
  25. sstat << "/proc/" << pid << "/stat";
  26. sstatm << "/proc/" << pid << "/statm";
  27. _procstat = fopen(sstat.str().c_str(), "r");
  28. _procstatm = fopen(sstatm.str().c_str(), "r");
  29. if (!_procstat || !_procstatm) {
  30. //cout << "Error opening files..." << endl;
  31. return false;
  32. }
  33. return true;
  34. }
  35. bool LinuxProcessMonitor::OpenIo(int pid)
  36. {
  37. stringstream sio;
  38. sio << "/proc/" << pid << "/io";
  39. _procio = fopen(sio.str().c_str(), "r");
  40. if (!_procio) {
  41. //cout << "Error opening io files..." << endl;
  42. return false;
  43. }
  44. return true;
  45. }
  46. double GetTimeInSec()
  47. {
  48. struct timeval t;
  49. gettimeofday(&t, NULL);
  50. return (double) t.tv_sec + 0.000001 * (double) t.tv_usec;
  51. }
  52. void LinuxProcessMonitor::update()
  53. {
  54. _prevUserTime = _utime;
  55. _prevSystemTime = _stime;
  56. _prevUpdateTime = _updateTime;
  57. _prevTotalNetworkRead = _rchar;
  58. _prevTotalNetworkWrite = _wchar;
  59. _updateTime = GetTimeInSec();
  60. if (_procstat != NULL) {
  61. rewind(_procstat);
  62. fread(_buffer, 1, 300, _procstat);
  63. sscanf(
  64. _buffer,
  65. "%d %s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d",
  66. &_pid, _comm, &_state, &_ppid, &_pgrp, &_session, &_tty_nr, &_tpgid, &_flags, &_minflt,
  67. &_cminflt, &_majflt, &_cmajflt, &_utime, &_stime, &_cutime, &_cstime, &_priority,
  68. &_nice, &_num_threads, &_itrealvalue, &_starttime, &_vsize, &_rss, &_rsslim,
  69. &_startcode, &_endcode, &_startstack, &_kstkesp, &_kstkeip, &_signal, &_blocked,
  70. &_sigignore, &_sigcatch, &_wchan, &_nswap, &_cnswap, &_exit_signal, &_processor);
  71. }
  72. if (_procstatm != NULL) {
  73. rewind(_procstatm);
  74. fread(_buffer, 1, 300, _procstatm);
  75. sscanf(_buffer, "%lu %lu %lu %lu %lu %lu %lu", &_size, &_resident, &_share, &_text, &_lib,
  76. &_data, &_dt);
  77. }
  78. if (_procio != NULL) {
  79. rewind(_procio);
  80. fread(_buffer, 1, 300, _procio);
  81. sscanf(_buffer, "%*s %lu %*s %lu %*s %lu %*s %lu %*s %lu %*s %lu %*s %lu", &_rchar, &_wchar,
  82. &_syscr, &_syscw, &_read_bytes, &_write_bytes, &_cancelled_write_bytes);
  83. }
  84. }
  85. void LinuxProcessMonitor::printAll()
  86. {
  87. double time = GetTimeInSec();
  88. cout << fixed << setprecision(8) << time << ": ";
  89. printf(
  90. "%d %s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d ",
  91. _pid, _comm, _state, _ppid, _pgrp, _session, _tty_nr, _tpgid, _flags, _minflt,
  92. _cminflt, _majflt, _cmajflt, _utime, _stime, _cutime, _cstime, _priority, _nice,
  93. _num_threads, _itrealvalue, _starttime, _vsize, _rss, _rsslim, _startcode, _endcode,
  94. _startstack, _kstkesp, _kstkeip, _signal, _blocked, _sigignore, _sigcatch, _wchan,
  95. _nswap, _cnswap, _exit_signal, _processor);
  96. printf("%lu %lu %lu %lu %lu %lu %lu ", _size, _resident, _share, _text, _lib, _data, _dt);
  97. cout << fixed << setprecision(5) << getUserTime() << " " << getSystemTime() << " "
  98. << getDataSize() << endl;
  99. }
  100. double LinuxProcessMonitor::getUserTime()
  101. {
  102. double seconds = (double) _utime / (double) _jiffy;
  103. return seconds;
  104. }
  105. double LinuxProcessMonitor::getSystemTime()
  106. {
  107. double seconds = (double) _stime / (double) _jiffy;
  108. return seconds;
  109. }
  110. double LinuxProcessMonitor::getUserCPULoad()
  111. {
  112. double load = (((double) _utime - (double) _prevUserTime) / (double) _jiffy) / (_updateTime
  113. - _prevUpdateTime);
  114. return load * 100.0;
  115. }
  116. double LinuxProcessMonitor::getSystemCPULoad()
  117. {
  118. double load = (((double) _stime - (double) _prevSystemTime) / (double) _jiffy) / (_updateTime
  119. - _prevUpdateTime);
  120. return load * 100.0;
  121. }
  122. double LinuxProcessMonitor::getTotalProgramSize()
  123. {
  124. return (double) (_size * 4096.0) / (1024. * 1024.0);
  125. }
  126. double LinuxProcessMonitor::getCodeSize()
  127. {
  128. return (double) (_text * 4096.0) / (1024. * 1024.0);
  129. }
  130. double LinuxProcessMonitor::getSharedPages()
  131. {
  132. return (double) (_share * 4096.0) / (1024. * 1024.0);
  133. }
  134. double LinuxProcessMonitor::getDataSize()
  135. {
  136. return (double) (_data * 4096.0) / (1024. * 1024.0);
  137. }
  138. double LinuxProcessMonitor::getResidentSetSize()
  139. {
  140. return (double) (_resident * 4096.0) / (1024. * 1024.0);
  141. }
  142. int LinuxProcessMonitor::pid()
  143. {
  144. return _pid;
  145. }
  146. char* LinuxProcessMonitor::comm()
  147. {
  148. return _comm;
  149. }
  150. char LinuxProcessMonitor::state()
  151. {
  152. return _state;
  153. }
  154. int LinuxProcessMonitor::ppid()
  155. {
  156. return _ppid;
  157. }
  158. int LinuxProcessMonitor::pgrp()
  159. {
  160. return _pgrp;
  161. }
  162. int LinuxProcessMonitor::session()
  163. {
  164. return _session;
  165. }
  166. int LinuxProcessMonitor::tty_nr()
  167. {
  168. return _tty_nr;
  169. }
  170. int LinuxProcessMonitor::tpgid()
  171. {
  172. return _tpgid;
  173. }
  174. unsigned long LinuxProcessMonitor::flags()
  175. {
  176. return _flags;
  177. }
  178. unsigned long LinuxProcessMonitor::minflt()
  179. {
  180. return _minflt;
  181. }
  182. unsigned long LinuxProcessMonitor::cminflt()
  183. {
  184. return _cminflt;
  185. }
  186. unsigned long LinuxProcessMonitor::majflt()
  187. {
  188. return _majflt;
  189. }
  190. unsigned long LinuxProcessMonitor::cmajflt()
  191. {
  192. return _cmajflt;
  193. }
  194. unsigned long LinuxProcessMonitor::utime()
  195. {
  196. return _utime;
  197. }
  198. unsigned long LinuxProcessMonitor::stime()
  199. {
  200. return _stime;
  201. }
  202. long int LinuxProcessMonitor::cutime()
  203. {
  204. return _cutime;
  205. }
  206. long int LinuxProcessMonitor::cstime()
  207. {
  208. return _cstime;
  209. }
  210. long int LinuxProcessMonitor::priority()
  211. {
  212. return _priority;
  213. }
  214. long int LinuxProcessMonitor::nice()
  215. {
  216. return _nice;
  217. }
  218. long int LinuxProcessMonitor::num_threads()
  219. {
  220. return _num_threads;
  221. }
  222. long int LinuxProcessMonitor::itrealvalue()
  223. {
  224. return _itrealvalue;
  225. }
  226. unsigned long LinuxProcessMonitor::starttime()
  227. {
  228. return _starttime;
  229. }
  230. unsigned long LinuxProcessMonitor::vsize()
  231. {
  232. return _vsize;
  233. }
  234. long int LinuxProcessMonitor::rss()
  235. {
  236. return _rss;
  237. }
  238. unsigned long LinuxProcessMonitor::rsslim()
  239. {
  240. return _rsslim;
  241. }
  242. unsigned long LinuxProcessMonitor::startcode()
  243. {
  244. return _startcode;
  245. }
  246. unsigned long LinuxProcessMonitor::endcode()
  247. {
  248. return _endcode;
  249. }
  250. unsigned long LinuxProcessMonitor::startstack()
  251. {
  252. return _startstack;
  253. }
  254. unsigned long LinuxProcessMonitor::kstkesp()
  255. {
  256. return _kstkesp;
  257. }
  258. unsigned long LinuxProcessMonitor::kstkeip()
  259. {
  260. return _kstkeip;
  261. }
  262. unsigned long LinuxProcessMonitor::signal()
  263. {
  264. return _signal;
  265. }
  266. unsigned long LinuxProcessMonitor::blocked()
  267. {
  268. return _blocked;
  269. }
  270. unsigned long LinuxProcessMonitor::sigignore()
  271. {
  272. return _sigignore;
  273. }
  274. unsigned long LinuxProcessMonitor::sigcatch()
  275. {
  276. return _sigcatch;
  277. }
  278. unsigned long LinuxProcessMonitor::wchan()
  279. {
  280. return _wchan;
  281. }
  282. unsigned long LinuxProcessMonitor::nswap()
  283. {
  284. return _nswap;
  285. }
  286. unsigned long LinuxProcessMonitor::cnswap()
  287. {
  288. return _cnswap;
  289. }
  290. int LinuxProcessMonitor::exit_signal()
  291. {
  292. return _exit_signal;
  293. }
  294. int LinuxProcessMonitor::processor()
  295. {
  296. return _processor;
  297. }
  298. long LinuxProcessMonitor::jiffy()
  299. {
  300. return _jiffy;
  301. }
  302. unsigned long LinuxProcessMonitor::size()
  303. {
  304. return _size;
  305. }
  306. unsigned long LinuxProcessMonitor::resident()
  307. {
  308. return _resident;
  309. }
  310. unsigned long LinuxProcessMonitor::share()
  311. {
  312. return _share;
  313. }
  314. unsigned long LinuxProcessMonitor::text()
  315. {
  316. return _text;
  317. }
  318. unsigned long LinuxProcessMonitor::lib()
  319. {
  320. return _lib;
  321. }
  322. unsigned long LinuxProcessMonitor::data()
  323. {
  324. return _data;
  325. }
  326. unsigned long LinuxProcessMonitor::dt()
  327. {
  328. return _dt;
  329. }
  330. unsigned long LinuxProcessMonitor::rchar()
  331. {
  332. return _rchar;
  333. }
  334. unsigned long LinuxProcessMonitor::wchar()
  335. {
  336. return _wchar;
  337. }
  338. unsigned long LinuxProcessMonitor::syscr()
  339. {
  340. return _syscr;
  341. }
  342. unsigned long LinuxProcessMonitor::syscw()
  343. {
  344. return _syscw;
  345. }
  346. unsigned long LinuxProcessMonitor::read_bytes()
  347. {
  348. return _read_bytes;
  349. }
  350. unsigned long LinuxProcessMonitor::write_bytes()
  351. {
  352. return _write_bytes;
  353. }
  354. unsigned long LinuxProcessMonitor::cancelled_write_bytes()
  355. {
  356. return _cancelled_write_bytes;
  357. }
  358. unsigned long LinuxProcessMonitor::GetTotalIoInBytes()
  359. {
  360. return _rchar - _prevTotalNetworkRead;
  361. }
  362. unsigned long LinuxProcessMonitor::GetTotalIoOutBytes()
  363. {
  364. return _wchar - _prevTotalNetworkWrite;
  365. }