git-svn-id: svn://db.shs.com.ru/pip@514 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -103,7 +103,7 @@ bool PISystemMonitor::startOnSelf(int interval_ms) {
|
||||
|
||||
PIVector<PISystemMonitor::ThreadStats> PISystemMonitor::threadsStatistic() const {
|
||||
mutex_.lock();
|
||||
PIVector<PISystemMonitor::ThreadStats> ret = cur_tm.values();
|
||||
PIVector<PISystemMonitor::ThreadStats> ret = cur_ts;
|
||||
mutex_.unlock();
|
||||
return ret;
|
||||
}
|
||||
@@ -122,6 +122,15 @@ void PISystemMonitor::stop() {
|
||||
|
||||
|
||||
void PISystemMonitor::run() {
|
||||
cur_tm.clear();
|
||||
tbid.clear();
|
||||
__PIThreadCollection * pitc = __PIThreadCollection::instance();
|
||||
pitc->lock();
|
||||
PIVector<PIThread * > tv = pitc->threads();
|
||||
piForeach (PIThread * t, tv)
|
||||
if (t->isPIObject())
|
||||
tbid[t->tid()] = t->name();
|
||||
pitc->unlock();
|
||||
#ifndef WINDOWS
|
||||
file.seekToBegin();
|
||||
PIString str(file.readAll(true));
|
||||
@@ -185,11 +194,16 @@ void PISystemMonitor::run() {
|
||||
THREADENTRY32 thread;
|
||||
thread.dwSize = sizeof(THREADENTRY32);
|
||||
if (Thread32First(snap, &thread) == TRUE) {
|
||||
if (thread.th32OwnerProcessID == DWORD(pID_))
|
||||
if (thread.th32OwnerProcessID == DWORD(pID_)) {
|
||||
++thcnt;
|
||||
gatherThread(thread.th32ThreadID);
|
||||
}
|
||||
while (Thread32Next(snap, &thread) == TRUE) {
|
||||
if (thread.th32OwnerProcessID == DWORD(pID_))
|
||||
if (thread.th32OwnerProcessID == DWORD(pID_)) {
|
||||
++thcnt;
|
||||
gatherThread(thread.th32ThreadID);
|
||||
}
|
||||
//piCout << thread.th32ThreadID;
|
||||
}
|
||||
}
|
||||
stat.threads = thcnt;
|
||||
@@ -225,16 +239,15 @@ void PISystemMonitor::run() {
|
||||
stat.cpu_load_system = piClampf(stat.cpu_load_system, 0.f, 100.f);
|
||||
stat.cpu_load_user = piClampf(stat.cpu_load_user, 0.f, 100.f);
|
||||
|
||||
last_tm = cur_tm;
|
||||
gatherThreadsStats();
|
||||
//PISystemTime dt = PISystemTime::fromMilliseconds(delay_);
|
||||
for (PIMap<const void*, ThreadStats>::iterator i = cur_tm.begin(); i != cur_tm.end(); ++i) {
|
||||
for (PIMap<llong, ThreadStats>::iterator i = cur_tm.begin(); i != cur_tm.end(); ++i) {
|
||||
if (!last_tm.contains(i.key())) continue;
|
||||
ThreadStats & ts_new(i.value());
|
||||
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);
|
||||
}
|
||||
last_tm = cur_tm;
|
||||
lock();
|
||||
cur_ts = cur_tm.values();
|
||||
unlock();
|
||||
@@ -252,35 +265,35 @@ void PISystemMonitor::makeStrings() {
|
||||
}
|
||||
|
||||
|
||||
void PISystemMonitor::gatherThreadsStats() {
|
||||
cur_ts.clear();
|
||||
cur_tm.clear();
|
||||
if (!self_) return;
|
||||
__PIThreadCollection * pitc = __PIThreadCollection::instance();
|
||||
pitc->lock();
|
||||
PIVector<PIThread*> tv = pitc->threads();
|
||||
piForeachC (PIThread * t, tv) {
|
||||
ThreadStats ts;
|
||||
ts.name = t->name();
|
||||
void PISystemMonitor::gatherThread(llong id) {
|
||||
PISystemMonitor::ThreadStats ts;
|
||||
ts.id = id;
|
||||
ts.name = tbid.value(id, "<non-PIThread>");
|
||||
#ifdef WINDOWS
|
||||
FILETIME times[4];
|
||||
PISystemTime ct = PISystemTime::current();
|
||||
if (GetThreadTimes(t->handle(), &(times[0]), &(times[1]), &(times[2]), &(times[3])) == 0) {
|
||||
piCout << "[PISystemMonitor] threadsInfo():: GetThreadTimes() error:" << errorString();
|
||||
continue;
|
||||
}
|
||||
ts.created = FILETIME2PIDateTime(times[0]);
|
||||
ts.work_time = ct - ts.created.toSystemTime();
|
||||
ts.kernel_time = FILETIME2PISystemTime(times[2]);
|
||||
ts.user_time = FILETIME2PISystemTime(times[3]);
|
||||
#endif
|
||||
cur_ts << ts;
|
||||
cur_tm[t] = ts;
|
||||
FILETIME times[4];
|
||||
PISystemTime ct = PISystemTime::current();
|
||||
HANDLE thdl = OpenThread(THREAD_QUERY_INFORMATION, FALSE, DWORD(id));
|
||||
if (thdl == NULL) {
|
||||
piCout << "[PISystemMonitor] gatherThread(" << id << "):: OpenThread() error:" << errorString();
|
||||
return;
|
||||
}
|
||||
pitc->unlock();
|
||||
if (GetThreadTimes(thdl, &(times[0]), &(times[1]), &(times[2]), &(times[3])) == 0) {
|
||||
piCout << "[PISystemMonitor] gatherThread(" << id << "):: GetThreadTimes() error:" << errorString();
|
||||
return;
|
||||
}
|
||||
CloseHandle(thdl);
|
||||
ts.created = FILETIME2PIDateTime(times[0]);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
float PISystemMonitor::calcThreadUsage(PISystemTime & t_new, PISystemTime & t_old) {
|
||||
if (delay_ <= 0) return -1.;
|
||||
return piClampf(100. * ((t_new - t_old).toMilliseconds() / delay_), 0.f, 100.f);}
|
||||
return piClampf(100. * ((t_new - t_old).toMilliseconds() / delay_), 0.f, 100.f);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user