05.11.2011 - stable version, 0.1.0, self-test program, work at GCC 2.95 - 4.5, VC 2010, MinGW, Linux, Windows, QNX
This commit is contained in:
105
pisystemmonitor.cpp
Normal file
105
pisystemmonitor.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
#include "pisystemmonitor.h"
|
||||
|
||||
|
||||
PISystemMonitor::PISystemMonitor(): PIThread() {
|
||||
pID_ = cycle = 0;
|
||||
cpu_count = 1;
|
||||
#ifndef WINDOWS
|
||||
# ifdef QNX
|
||||
page_size = 4096;
|
||||
# else
|
||||
page_size = getpagesize();
|
||||
# endif
|
||||
cpu_count = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
if (cpu_count < 1) cpu_count = 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PISystemMonitor::startOnProcess(int pID) {
|
||||
stop();
|
||||
pID_ = pID;
|
||||
#ifndef WINDOWS
|
||||
file.open("/proc/" + PIString::fromNumber(pID_) + "/stat", PIFile::Read);
|
||||
filem.open("/proc/" + PIString::fromNumber(pID_) + "/statm", PIFile::Read);
|
||||
if (!file.isOpened()) {
|
||||
cout << "[PISystemMonitor] Can`t find process with ID = " << pID_ << "!" << endl;
|
||||
return false;
|
||||
}
|
||||
cycle = -1;
|
||||
#endif
|
||||
return start(25);
|
||||
}
|
||||
|
||||
|
||||
void PISystemMonitor::run() {
|
||||
#ifndef WINDOWS
|
||||
PIString str(file.readAll(true));
|
||||
int si = str.find('(') + 1, fi = 0, cc = 1;
|
||||
for (int i = si; i < str.size_s(); ++i) {
|
||||
if (str[i] == '(') cc++;
|
||||
if (str[i] == ')') cc--;
|
||||
if (cc <= 0) {
|
||||
fi = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
stat.exec_name = str.mid(si, fi - si);
|
||||
str.cutMid(si - 1, fi - si + 3);
|
||||
PIStringList sl = str.split(" ");
|
||||
if (sl.size_s() < 18) return;
|
||||
stat.ID = sl[0].toInt();
|
||||
stat.state = sl[1];
|
||||
stat.parent_ID = sl[2].toInt();
|
||||
stat.group_ID = sl[3].toInt();
|
||||
stat.session_ID = sl[4].toInt();
|
||||
if (cycle < 0) {
|
||||
cpu_u_prev = cpu_u_cur = sl[12].toLLong();
|
||||
cpu_s_prev = cpu_s_cur = sl[13].toLLong();
|
||||
}
|
||||
cycle++;
|
||||
if (cycle >= 40) {
|
||||
cpu_u_prev = cpu_u_cur;
|
||||
cpu_s_prev = cpu_s_cur;
|
||||
cpu_u_cur = sl[12].toLLong();
|
||||
cpu_s_cur = sl[13].toLLong();
|
||||
stat.cpu_load_system = cpu_s_cur - cpu_s_prev;
|
||||
stat.cpu_load_user = cpu_u_cur - cpu_u_prev;
|
||||
if (stat.cpu_load_system > 100) stat.cpu_load_system = 100;
|
||||
if (stat.cpu_load_user > 100) stat.cpu_load_user = 100;
|
||||
stat.cpu_load_system /= cpu_count;
|
||||
stat.cpu_load_user /= cpu_count;
|
||||
cycle = 0;
|
||||
}
|
||||
stat.priority = sl[16].toInt();
|
||||
stat.threads = sl[18].toInt();
|
||||
|
||||
str = filem.readAll(true);
|
||||
sl = str.split(" ");
|
||||
if (sl.size_s() < 5) return;
|
||||
stat.virtual_memsize = sl[0].toLong() * page_size;
|
||||
stat.resident_memsize = sl[1].toLong() * page_size;
|
||||
stat.share_memsize = sl[2].toLong() * page_size;
|
||||
stat.data_memsize = sl[5].toLong() * page_size;
|
||||
stat.physical_memsize = stat.resident_memsize - stat.share_memsize;
|
||||
|
||||
stat.physical_memsize_readable = readableSize(stat.physical_memsize);
|
||||
stat.resident_memsize_readable = readableSize(stat.resident_memsize);
|
||||
stat.share_memsize_readable = readableSize(stat.share_memsize);
|
||||
stat.virtual_memsize_readable = readableSize(stat.virtual_memsize);
|
||||
stat.data_memsize_readable = readableSize(stat.data_memsize);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
PIString PISystemMonitor::readableSize(long bytes) {
|
||||
if (bytes < 999) return PIString::fromNumber(bytes) + " B";
|
||||
long res = bytes / 1024;
|
||||
if (res < 999) return PIString::fromNumber(res) + " kB";
|
||||
res = res / 1024;
|
||||
if (res < 999) return PIString::fromNumber(res) + " MB";
|
||||
res = res / 1024;
|
||||
if (res < 999) return PIString::fromNumber(res) + " GB";
|
||||
res = res / 1024;
|
||||
return PIString::fromNumber(res) + " PB";
|
||||
}
|
||||
Reference in New Issue
Block a user