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

This commit is contained in:
2017-06-21 21:57:25 +00:00
parent b8e3094099
commit d2bded692e
4 changed files with 65 additions and 19 deletions

View File

@@ -21,6 +21,7 @@
#include "pisystemmonitor.h" #include "pisystemmonitor.h"
#include "pisysteminfo.h" #include "pisysteminfo.h"
#include "piprocess.h" #include "piprocess.h"
#include "pidir.h"
#include "pitime_win.h" #include "pitime_win.h"
#ifdef WINDOWS #ifdef WINDOWS
# include <psapi.h> # include <psapi.h>
@@ -31,6 +32,8 @@
PRIVATE_DEFINITION_START(PISystemMonitor) PRIVATE_DEFINITION_START(PISystemMonitor)
#ifndef WINDOWS #ifndef WINDOWS
llong cpu_u_cur, cpu_u_prev, cpu_s_cur, cpu_s_prev; llong cpu_u_cur, cpu_u_prev, cpu_s_cur, cpu_s_prev;
PIString proc_dir;
PIFile file, filem;
#else #else
HANDLE hProc; HANDLE hProc;
PROCESS_MEMORY_COUNTERS mem_cnt; PROCESS_MEMORY_COUNTERS mem_cnt;
@@ -75,9 +78,10 @@ bool PISystemMonitor::startOnProcess(int pID, int interval_ms) {
self_ = false; self_ = false;
pID_ = pID; pID_ = pID;
#ifndef WINDOWS #ifndef WINDOWS
file.open("/proc/" + PIString::fromNumber(pID_) + "/stat", PIIODevice::ReadOnly); PRIVATE->proc_dir = PIStringAscii("/proc/") + PIString::fromNumber(pID_) + PIStringAscii("/");
filem.open("/proc/" + PIString::fromNumber(pID_) + "/statm", PIIODevice::ReadOnly); PRIVATE->file. open(PRIVATE->proc_dir + "stat", PIIODevice::ReadOnly);
if (!file.isOpened()) { PRIVATE->filem.open(PRIVATE->proc_dir + "statm", PIIODevice::ReadOnly);
if (!PRIVATE->file.isOpened()) {
piCoutObj << "Can`t find process with ID = " << pID_ << "!"; piCoutObj << "Can`t find process with ID = " << pID_ << "!";
return false; return false;
} }
@@ -131,9 +135,10 @@ void PISystemMonitor::run() {
if (t->isPIObject()) if (t->isPIObject())
tbid[t->tid()] = t->name(); tbid[t->tid()] = t->name();
pitc->unlock(); pitc->unlock();
//piCout << tbid.keys().toType<uint>();
#ifndef WINDOWS #ifndef WINDOWS
file.seekToBegin(); PRIVATE->file.seekToBegin();
PIString str(file.readAll(true)); PIString str(PRIVATE->file.readAll(true));
int si = str.find('(') + 1, fi = 0, cc = 1; int si = str.find('(') + 1, fi = 0, cc = 1;
for (int i = si; i < str.size_s(); ++i) { for (int i = si; i < str.size_s(); ++i) {
if (str[i] == '(') cc++; if (str[i] == '(') cc++;
@@ -162,17 +167,19 @@ void PISystemMonitor::run() {
PRIVATE->cpu_s_prev = PRIVATE->cpu_s_cur; PRIVATE->cpu_s_prev = PRIVATE->cpu_s_cur;
PRIVATE->cpu_u_cur = sl[12].toLLong(); PRIVATE->cpu_u_cur = sl[12].toLLong();
PRIVATE->cpu_s_cur = sl[13].toLLong(); PRIVATE->cpu_s_cur = sl[13].toLLong();
stat.cpu_load_system = PRIVATE->cpu_s_cur - PRIVATE->cpu_s_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; stat.cpu_load_user = (PRIVATE->cpu_u_cur - PRIVATE->cpu_u_prev) / (delay_ / 1000.);
stat.cpu_load_system /= cpu_count; stat.cpu_load_system /= cpu_count;
stat.cpu_load_user /= cpu_count; stat.cpu_load_user /= cpu_count;
cycle = 0; cycle = 0;
//} //}
stat.priority = sl[16].toInt(); stat.priority = sl[16].toInt();
stat.threads = sl[18].toInt(); stat.threads = sl[18].toInt();
//piCout << "\n";
//piCout << sl[0] << sl[12] << sl[13];
filem.seekToBegin(); PRIVATE->filem.seekToBegin();
str = filem.readAll(true); str = PRIVATE->filem.readAll(true);
sl = str.split(" "); sl = str.split(" ");
if (sl.size_s() < 6) return; if (sl.size_s() < 6) return;
stat.virtual_memsize = sl[0].toLong() * page_size; stat.virtual_memsize = sl[0].toLong() * page_size;
@@ -180,6 +187,13 @@ void PISystemMonitor::run() {
stat.share_memsize = sl[2].toLong() * page_size; stat.share_memsize = sl[2].toLong() * page_size;
stat.data_memsize = sl[5].toLong() * page_size; stat.data_memsize = sl[5].toLong() * page_size;
stat.physical_memsize = stat.resident_memsize - stat.share_memsize; 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 #else
stat.ID = pID_; stat.ID = pID_;
// HMODULE hMod; // HMODULE hMod;
@@ -246,6 +260,7 @@ void PISystemMonitor::run() {
ThreadStats & ts_old(last_tm[i.key()]); 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_kernel = calcThreadUsage(ts_new.kernel_time, ts_old.kernel_time);
ts_new.cpu_load_user = calcThreadUsage(ts_new.user_time, ts_old.user_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; last_tm = cur_tm;
lock(); lock();
@@ -269,7 +284,28 @@ void PISystemMonitor::gatherThread(llong id) {
PISystemMonitor::ThreadStats ts; PISystemMonitor::ThreadStats ts;
ts.id = id; ts.id = id;
ts.name = tbid.value(id, "<non-PIThread>"); 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]; FILETIME times[4];
PISystemTime ct = PISystemTime::current(); PISystemTime ct = PISystemTime::current();
HANDLE thdl = OpenThread(THREAD_QUERY_INFORMATION, FALSE, DWORD(id)); 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.work_time = ct - ts.created.toSystemTime();
ts.kernel_time = FILETIME2PISystemTime(times[2]); ts.kernel_time = FILETIME2PISystemTime(times[2]);
ts.user_time = FILETIME2PISystemTime(times[3]); ts.user_time = FILETIME2PISystemTime(times[3]);
#else
#endif #endif
//cur_ts << ts; //cur_ts << ts;
cur_tm[id] = ts; cur_tm[id] = ts;

View File

@@ -85,7 +85,6 @@ private:
void gatherThread(llong id); void gatherThread(llong id);
float calcThreadUsage(PISystemTime & t_new, PISystemTime & t_old); float calcThreadUsage(PISystemTime & t_new, PISystemTime & t_old);
PIFile file, filem;
ProcessStats stat; ProcessStats stat;
PIVector<ThreadStats> cur_ts; PIVector<ThreadStats> cur_ts;
PIMap<llong, ThreadStats> last_tm, cur_tm; PIMap<llong, ThreadStats> last_tm, cur_tm;

View File

@@ -27,6 +27,10 @@
#else #else
# define __THREAD_FUNC_RET__ void* # define __THREAD_FUNC_RET__ void*
#endif #endif
#if defined(LINUX)
# include <sys/syscall.h>
# define gettid() syscall(SYS_gettid)
#endif
#if defined(MAC_OS) || defined(BLACKBERRY) #if defined(MAC_OS) || defined(BLACKBERRY)
# include <pthread.h> # include <pthread.h>
#endif #endif
@@ -213,8 +217,9 @@ bool PIThread::start(int timer_delay) {
if (ret == 0) { if (ret == 0) {
# ifdef MAC_OS # ifdef MAC_OS
pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_); pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_);
# else //# else
tid_ = PRIVATE->thread; // pthread_threadid_np();
// tid_ = PRIVATE->thread;
# endif # endif
#else #else
if (PRIVATE->thread != 0) CloseHandle(PRIVATE->thread); if (PRIVATE->thread != 0) CloseHandle(PRIVATE->thread);
@@ -249,8 +254,8 @@ bool PIThread::startOnce() {
if (ret == 0) { if (ret == 0) {
# ifdef MAC_OS # ifdef MAC_OS
pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_); pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_);
# else //# else
tid_ = PRIVATE->thread; // tid_ = PRIVATE->thread;
# endif # endif
#else #else
if (PRIVATE->thread != 0) CloseHandle(PRIVATE->thread); if (PRIVATE->thread != 0) CloseHandle(PRIVATE->thread);
@@ -386,6 +391,9 @@ void PIThread::__thread_func__(void * t) {
PIThread & ct = *((PIThread * )t); PIThread & ct = *((PIThread * )t);
#ifdef WINDOWS #ifdef WINDOWS
ct.tid_ = GetCurrentThreadId(); ct.tid_ = GetCurrentThreadId();
#endif
#ifdef LINUX
ct.tid_ = gettid();
#endif #endif
PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name()); PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name());
REGISTER_THREAD(&ct); REGISTER_THREAD(&ct);
@@ -457,6 +465,9 @@ void PIThread::__thread_func_once__(void * t) {
PIThread & ct = *((PIThread * )t); PIThread & ct = *((PIThread * )t);
#ifdef WINDOWS #ifdef WINDOWS
ct.tid_ = GetCurrentThreadId(); ct.tid_ = GetCurrentThreadId();
#endif
#ifdef LINUX
ct.tid_ = gettid();
#endif #endif
PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name()); PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name());
REGISTER_THREAD(&ct); REGISTER_THREAD(&ct);

View File

@@ -112,7 +112,7 @@ public:
local_info_base << TileList::Row("Architecture: " + PISystemInfo::instance()->architecture, CellFormat()); local_info_base << TileList::Row("Architecture: " + PISystemInfo::instance()->architecture, CellFormat());
local_info_base << TileList::Row(" CPU count: " + PIString::fromNumber(PISystemInfo::instance()->processorsCount), CellFormat()); local_info_base << TileList::Row(" CPU count: " + PIString::fromNumber(PISystemInfo::instance()->processorsCount), CellFormat());
local_info_base << TileList::Row("", CellFormat()); local_info_base << TileList::Row("", CellFormat());
local_info_base << TileList::Row("PIThreads:", CellFormat()); local_info_base << TileList::Row("Threads:", CellFormat());
return ret; return ret;
} }
PIScreenTile * peerDiagTile() { PIScreenTile * peerDiagTile() {
@@ -222,9 +222,9 @@ public:
piForeachC (PISystemMonitor::ThreadStats & t, ts) { piForeachC (PISystemMonitor::ThreadStats & t, ts) {
PIString line = PIString(++num).expandLeftTo(2, ' ') + ": "; PIString line = PIString(++num).expandLeftTo(2, ' ') + ": ";
line += PIString(t.name).expandRightTo(maxlen, ' ') + ": k "; line += PIString(t.name).expandRightTo(maxlen, ' ') + ": k ";
PIString ns = PIString::fromNumber(t.cpu_load_kernel); ns = ns.left(ns.find('.') + 2); PIString ns = PIString::fromNumber(t.cpu_load_kernel).replaceAll(",", "."); ns = ns.left(ns.find('.') + 2);
line += ns.expandLeftTo(5, ' ') + " %, u "; line += ns.expandLeftTo(5, ' ') + " %, u ";
ns = PIString::fromNumber(t.cpu_load_user); ns = ns.left(ns.find('.') + 2); ns = PIString::fromNumber(t.cpu_load_user).replaceAll(",", "."); ns = ns.left(ns.find('.') + 2);
line += ns.expandLeftTo(5, ' ') + " %"; line += ns.expandLeftTo(5, ' ') + " %";
tile->content << TileList::Row(line, CellFormat()); tile->content << TileList::Row(line, CellFormat());
} }
@@ -393,6 +393,7 @@ int main(int argc, char * argv[]) {
MainMenu * menu = new MainMenu(*daemon); MainMenu * menu = new MainMenu(*daemon);
if (sapp) CONNECTU(sapp, messageReceived, menu, messageFromApp); if (sapp) CONNECTU(sapp, messageReceived, menu, messageFromApp);
if (cli.hasArgument("silent")) { if (cli.hasArgument("silent")) {
PICout::setBufferActive(false);
PIKbdListener ls; PIKbdListener ls;
ls.enableExitCapture(PIKbdListener::F10); ls.enableExitCapture(PIKbdListener::F10);
ls.start(); ls.start();