|
|
|
|
@@ -21,10 +21,23 @@
|
|
|
|
|
#include "pisystemmonitor.h"
|
|
|
|
|
#include "pisysteminfo.h"
|
|
|
|
|
#ifdef WINDOWS
|
|
|
|
|
# include <psapi.h>
|
|
|
|
|
# include <tlhelp32.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PRIVATE_DEFINITION_START(PISystemMonitor)
|
|
|
|
|
#ifndef WINDOWS
|
|
|
|
|
llong cpu_u_cur, cpu_u_prev, cpu_s_cur, cpu_s_prev;
|
|
|
|
|
#else
|
|
|
|
|
HANDLE hProc;
|
|
|
|
|
PROCESS_MEMORY_COUNTERS mem_cnt;
|
|
|
|
|
PISystemTime tm_kernel, tm_user;
|
|
|
|
|
PITimeMeasurer tm;
|
|
|
|
|
#endif
|
|
|
|
|
PRIVATE_DEFINITION_END(PISystemMonitor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PISystemMonitor::PISystemMonitor(): PIThread() {
|
|
|
|
|
pID_ = cycle = 0;
|
|
|
|
|
cpu_count = PISystemInfo::instance()->processorsCount;
|
|
|
|
|
@@ -35,8 +48,8 @@ PISystemMonitor::PISystemMonitor(): PIThread() {
|
|
|
|
|
page_size = getpagesize();
|
|
|
|
|
# endif
|
|
|
|
|
#else
|
|
|
|
|
hProc = 0;
|
|
|
|
|
mem_cnt.cb = sizeof(mem_cnt);
|
|
|
|
|
PRIVATE->hProc = 0;
|
|
|
|
|
PRIVATE->mem_cnt.cb = sizeof(PRIVATE->mem_cnt);
|
|
|
|
|
#endif
|
|
|
|
|
setName("system_monitor");
|
|
|
|
|
}
|
|
|
|
|
@@ -66,12 +79,12 @@ bool PISystemMonitor::startOnProcess(int pID) {
|
|
|
|
|
}
|
|
|
|
|
cycle = -1;
|
|
|
|
|
#else
|
|
|
|
|
hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pID_);
|
|
|
|
|
if (hProc == 0) {
|
|
|
|
|
PRIVATE->hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pID_);
|
|
|
|
|
if (PRIVATE->hProc == 0) {
|
|
|
|
|
piCoutObj << "Can`t open process with ID = " << pID_ << "," << errorString();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
tm.reset();
|
|
|
|
|
PRIVATE->tm.reset();
|
|
|
|
|
#endif
|
|
|
|
|
return start(1000);
|
|
|
|
|
}
|
|
|
|
|
@@ -80,9 +93,9 @@ bool PISystemMonitor::startOnProcess(int pID) {
|
|
|
|
|
void PISystemMonitor::stop() {
|
|
|
|
|
PIThread::stop();
|
|
|
|
|
#ifdef WINDOWS
|
|
|
|
|
if (hProc != 0) {
|
|
|
|
|
CloseHandle(hProc);
|
|
|
|
|
hProc = 0;
|
|
|
|
|
if (PRIVATE->hProc != 0) {
|
|
|
|
|
CloseHandle(PRIVATE->hProc);
|
|
|
|
|
PRIVATE->hProc = 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
@@ -112,17 +125,17 @@ void PISystemMonitor::run() {
|
|
|
|
|
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();
|
|
|
|
|
PRIVATE->cpu_u_prev = PRIVATE->cpu_u_cur = sl[12].toLLong();
|
|
|
|
|
PRIVATE->cpu_s_prev = PRIVATE->cpu_s_cur = sl[13].toLLong();
|
|
|
|
|
}
|
|
|
|
|
cycle++;
|
|
|
|
|
//if (cycle >= 4) {
|
|
|
|
|
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;
|
|
|
|
|
PRIVATE->cpu_u_prev = PRIVATE->cpu_u_cur;
|
|
|
|
|
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 /= cpu_count;
|
|
|
|
|
stat.cpu_load_user /= cpu_count;
|
|
|
|
|
cycle = 0;
|
|
|
|
|
@@ -143,10 +156,10 @@ void PISystemMonitor::run() {
|
|
|
|
|
stat.ID = pID_;
|
|
|
|
|
// HMODULE hMod;
|
|
|
|
|
// DWORD cbNeeded;
|
|
|
|
|
if (GetProcessMemoryInfo(hProc, &mem_cnt, sizeof(mem_cnt)) != 0) {
|
|
|
|
|
stat.physical_memsize = mem_cnt.WorkingSetSize;
|
|
|
|
|
if (GetProcessMemoryInfo(PRIVATE->hProc, &PRIVATE->mem_cnt, sizeof(PRIVATE->mem_cnt)) != 0) {
|
|
|
|
|
stat.physical_memsize = PRIVATE->mem_cnt.WorkingSetSize;
|
|
|
|
|
}
|
|
|
|
|
stat.priority = GetPriorityClass(hProc);
|
|
|
|
|
stat.priority = GetPriorityClass(PRIVATE->hProc);
|
|
|
|
|
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, pID_);
|
|
|
|
|
int thcnt = 0;
|
|
|
|
|
if (snap != 0) {
|
|
|
|
|
@@ -164,12 +177,12 @@ void PISystemMonitor::run() {
|
|
|
|
|
CloseHandle(snap);
|
|
|
|
|
}
|
|
|
|
|
FILETIME ft0, ft1, ft_kernel, ft_user;
|
|
|
|
|
double el_s = tm.elapsed_s() * cpu_count / 100.;
|
|
|
|
|
if (GetProcessTimes(hProc, &ft0, &ft1, &ft_kernel, &ft_user) != 0) {
|
|
|
|
|
double el_s = PRIVATE->tm.elapsed_s() * cpu_count / 100.;
|
|
|
|
|
if (GetProcessTimes(PRIVATE->hProc, &ft0, &ft1, &ft_kernel, &ft_user) != 0) {
|
|
|
|
|
PISystemTime tm_kernel_c(ft_kernel), tm_user_c(ft_user);
|
|
|
|
|
if (cycle < 0) {
|
|
|
|
|
tm_kernel = tm_kernel_c;
|
|
|
|
|
tm_user = tm_user_c;
|
|
|
|
|
PRIVATE->tm_kernel = tm_kernel_c;
|
|
|
|
|
PRIVATE->tm_user = tm_user_c;
|
|
|
|
|
}
|
|
|
|
|
//cycle++;
|
|
|
|
|
cycle = 0;
|
|
|
|
|
@@ -177,16 +190,16 @@ void PISystemMonitor::run() {
|
|
|
|
|
stat.cpu_load_system = 0.f;
|
|
|
|
|
stat.cpu_load_user = 0.f;
|
|
|
|
|
} else {
|
|
|
|
|
stat.cpu_load_system = (tm_kernel_c - tm_kernel).toSeconds() / el_s;
|
|
|
|
|
stat.cpu_load_user = (tm_user_c - tm_user).toSeconds() / el_s;
|
|
|
|
|
stat.cpu_load_system = (tm_kernel_c - PRIVATE->tm_kernel).toSeconds() / el_s;
|
|
|
|
|
stat.cpu_load_user = (tm_user_c - PRIVATE->tm_user).toSeconds() / el_s;
|
|
|
|
|
}
|
|
|
|
|
tm_kernel = tm_kernel_c;
|
|
|
|
|
tm_user = tm_user_c;
|
|
|
|
|
PRIVATE->tm_kernel = tm_kernel_c;
|
|
|
|
|
PRIVATE->tm_user = tm_user_c;
|
|
|
|
|
} else {
|
|
|
|
|
stat.cpu_load_system = 0.f;
|
|
|
|
|
stat.cpu_load_user = 0.f;
|
|
|
|
|
}
|
|
|
|
|
tm.reset();
|
|
|
|
|
PRIVATE->tm.reset();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
stat.cpu_load_system = piClampf(stat.cpu_load_system, 0.f, 100.f);
|
|
|
|
|
|