git-svn-id: svn://db.shs.com.ru/pip@513 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-06-21 14:00:45 +00:00
parent 6ac45691cc
commit ed1a89508a
7 changed files with 253 additions and 18 deletions

View File

@@ -20,6 +20,7 @@
#include "piincludes_p.h"
#include "pisystemmonitor.h"
#include "pisysteminfo.h"
#include "piprocess.h"
#include "pitime_win.h"
#ifdef WINDOWS
# include <psapi.h>
@@ -40,6 +41,7 @@ PRIVATE_DEFINITION_END(PISystemMonitor)
PISystemMonitor::PISystemMonitor(): PIThread() {
self_ = false;
pID_ = cycle = 0;
cpu_count = PISystemInfo::instance()->processorsCount;
#ifndef WINDOWS
@@ -68,8 +70,9 @@ PISystemMonitor::ProcessStats::ProcessStats() {
}
bool PISystemMonitor::startOnProcess(int pID) {
bool PISystemMonitor::startOnProcess(int pID, int interval_ms) {
stop();
self_ = false;
pID_ = pID;
#ifndef WINDOWS
file.open("/proc/" + PIString::fromNumber(pID_) + "/stat", PIIODevice::ReadOnly);
@@ -87,7 +90,22 @@ bool PISystemMonitor::startOnProcess(int pID) {
}
PRIVATE->tm.reset();
#endif
return start(1000);
return start(interval_ms);
}
bool PISystemMonitor::startOnSelf(int interval_ms) {
bool ret = startOnProcess(PIProcess::currentPID(), interval_ms);
self_ = true;
return ret;
}
PIVector<PISystemMonitor::ThreadStats> PISystemMonitor::threadsStatistic() const {
mutex_.lock();
PIVector<PISystemMonitor::ThreadStats> ret = cur_tm.values();
mutex_.unlock();
return ret;
}
@@ -206,6 +224,21 @@ 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) {
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);
}
lock();
cur_ts = cur_tm.values();
unlock();
makeStrings();
}
@@ -217,3 +250,37 @@ void PISystemMonitor::makeStrings() {
stat.virtual_memsize_readable.setReadableSize(stat.virtual_memsize);
stat.data_memsize_readable.setReadableSize(stat.data_memsize);
}
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();
#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;
}
pitc->unlock();
}
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);}