git-svn-id: svn://db.shs.com.ru/pip@516 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#include "pisystemmonitor.h"
|
||||
#include "pisysteminfo.h"
|
||||
#include "piprocess.h"
|
||||
#include "pidir.h"
|
||||
#include "pitime_win.h"
|
||||
#ifdef WINDOWS
|
||||
# include <psapi.h>
|
||||
@@ -31,6 +32,8 @@
|
||||
PRIVATE_DEFINITION_START(PISystemMonitor)
|
||||
#ifndef WINDOWS
|
||||
llong cpu_u_cur, cpu_u_prev, cpu_s_cur, cpu_s_prev;
|
||||
PIString proc_dir;
|
||||
PIFile file, filem;
|
||||
#else
|
||||
HANDLE hProc;
|
||||
PROCESS_MEMORY_COUNTERS mem_cnt;
|
||||
@@ -75,9 +78,10 @@ bool PISystemMonitor::startOnProcess(int pID, int interval_ms) {
|
||||
self_ = false;
|
||||
pID_ = pID;
|
||||
#ifndef WINDOWS
|
||||
file.open("/proc/" + PIString::fromNumber(pID_) + "/stat", PIIODevice::ReadOnly);
|
||||
filem.open("/proc/" + PIString::fromNumber(pID_) + "/statm", PIIODevice::ReadOnly);
|
||||
if (!file.isOpened()) {
|
||||
PRIVATE->proc_dir = PIStringAscii("/proc/") + PIString::fromNumber(pID_) + PIStringAscii("/");
|
||||
PRIVATE->file. open(PRIVATE->proc_dir + "stat", PIIODevice::ReadOnly);
|
||||
PRIVATE->filem.open(PRIVATE->proc_dir + "statm", PIIODevice::ReadOnly);
|
||||
if (!PRIVATE->file.isOpened()) {
|
||||
piCoutObj << "Can`t find process with ID = " << pID_ << "!";
|
||||
return false;
|
||||
}
|
||||
@@ -131,9 +135,10 @@ void PISystemMonitor::run() {
|
||||
if (t->isPIObject())
|
||||
tbid[t->tid()] = t->name();
|
||||
pitc->unlock();
|
||||
//piCout << tbid.keys().toType<uint>();
|
||||
#ifndef WINDOWS
|
||||
file.seekToBegin();
|
||||
PIString str(file.readAll(true));
|
||||
PRIVATE->file.seekToBegin();
|
||||
PIString str(PRIVATE->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++;
|
||||
@@ -162,17 +167,19 @@ void PISystemMonitor::run() {
|
||||
PRIVATE->cpu_s_prev = PRIVATE->cpu_s_cur;
|
||||
PRIVATE->cpu_u_cur = sl[12].toLLong();
|
||||
PRIVATE->cpu_s_cur = sl[13].toLLong();
|
||||
stat.cpu_load_system = PRIVATE->cpu_s_cur - PRIVATE->cpu_s_prev;
|
||||
stat.cpu_load_user = PRIVATE->cpu_u_cur - PRIVATE->cpu_u_prev;
|
||||
stat.cpu_load_system = (PRIVATE->cpu_s_cur - PRIVATE->cpu_s_prev) / (delay_ / 1000.);
|
||||
stat.cpu_load_user = (PRIVATE->cpu_u_cur - PRIVATE->cpu_u_prev) / (delay_ / 1000.);
|
||||
stat.cpu_load_system /= cpu_count;
|
||||
stat.cpu_load_user /= cpu_count;
|
||||
cycle = 0;
|
||||
//}
|
||||
stat.priority = sl[16].toInt();
|
||||
stat.threads = sl[18].toInt();
|
||||
//piCout << "\n";
|
||||
//piCout << sl[0] << sl[12] << sl[13];
|
||||
|
||||
filem.seekToBegin();
|
||||
str = filem.readAll(true);
|
||||
PRIVATE->filem.seekToBegin();
|
||||
str = PRIVATE->filem.readAll(true);
|
||||
sl = str.split(" ");
|
||||
if (sl.size_s() < 6) return;
|
||||
stat.virtual_memsize = sl[0].toLong() * page_size;
|
||||
@@ -180,6 +187,13 @@ void PISystemMonitor::run() {
|
||||
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;
|
||||
|
||||
PIVector<PIFile::FileInfo> tld = PIDir(PRIVATE->proc_dir + "task").entries();
|
||||
piForeachC (PIFile::FileInfo & i, tld) {
|
||||
if (i.flags[PIFile::FileInfo::Dot] || i.flags[PIFile::FileInfo::DotDot])
|
||||
continue;
|
||||
gatherThread(i.name().toInt());
|
||||
}
|
||||
#else
|
||||
stat.ID = pID_;
|
||||
// HMODULE hMod;
|
||||
@@ -246,6 +260,7 @@ void PISystemMonitor::run() {
|
||||
ThreadStats & ts_old(last_tm[i.key()]);
|
||||
ts_new.cpu_load_kernel = calcThreadUsage(ts_new.kernel_time, ts_old.kernel_time);
|
||||
ts_new.cpu_load_user = calcThreadUsage(ts_new.user_time, ts_old.user_time);
|
||||
//piCout << ts_new.cpu_load_user;
|
||||
}
|
||||
last_tm = cur_tm;
|
||||
lock();
|
||||
@@ -269,7 +284,28 @@ void PISystemMonitor::gatherThread(llong id) {
|
||||
PISystemMonitor::ThreadStats ts;
|
||||
ts.id = id;
|
||||
ts.name = tbid.value(id, "<non-PIThread>");
|
||||
#ifdef WINDOWS
|
||||
#ifndef WINDOWS
|
||||
PIFile f(PRIVATE->proc_dir + "task/" + PIString(id) + "/stat");
|
||||
//piCout << f.path();
|
||||
if (!f.open(PIIODevice::ReadOnly))
|
||||
return;
|
||||
PIString str = f.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;
|
||||
}
|
||||
}
|
||||
str.cutMid(si - 1, fi - si + 3);
|
||||
PIStringList sl = str.split(" ");
|
||||
if (sl.size_s() < 14) return;
|
||||
//piCout << sl[0] << sl[12] << sl[13];
|
||||
ts.user_time = PISystemTime::fromMilliseconds(sl[12].toInt() * 10.);
|
||||
ts.kernel_time = PISystemTime::fromMilliseconds(sl[13].toInt() * 10.);
|
||||
#else
|
||||
FILETIME times[4];
|
||||
PISystemTime ct = PISystemTime::current();
|
||||
HANDLE thdl = OpenThread(THREAD_QUERY_INFORMATION, FALSE, DWORD(id));
|
||||
@@ -286,7 +322,6 @@ void PISystemMonitor::gatherThread(llong id) {
|
||||
ts.work_time = ct - ts.created.toSystemTime();
|
||||
ts.kernel_time = FILETIME2PISystemTime(times[2]);
|
||||
ts.user_time = FILETIME2PISystemTime(times[3]);
|
||||
#else
|
||||
#endif
|
||||
//cur_ts << ts;
|
||||
cur_tm[id] = ts;
|
||||
|
||||
Reference in New Issue
Block a user