diff --git a/src_main/system/pisystemmonitor.cpp b/src_main/system/pisystemmonitor.cpp index f3145c99..38db3cb8 100755 --- a/src_main/system/pisystemmonitor.cpp +++ b/src_main/system/pisystemmonitor.cpp @@ -21,6 +21,7 @@ #include "pisystemmonitor.h" #include "pisysteminfo.h" #include "piprocess.h" +#include "pidir.h" #include "pitime_win.h" #ifdef WINDOWS # include @@ -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(); #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 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, ""); -#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; diff --git a/src_main/system/pisystemmonitor.h b/src_main/system/pisystemmonitor.h index daecc421..ffec210b 100755 --- a/src_main/system/pisystemmonitor.h +++ b/src_main/system/pisystemmonitor.h @@ -85,7 +85,6 @@ private: void gatherThread(llong id); float calcThreadUsage(PISystemTime & t_new, PISystemTime & t_old); - PIFile file, filem; ProcessStats stat; PIVector cur_ts; PIMap last_tm, cur_tm; diff --git a/src_main/thread/pithread.cpp b/src_main/thread/pithread.cpp index beacd5d5..9f3557a9 100755 --- a/src_main/thread/pithread.cpp +++ b/src_main/thread/pithread.cpp @@ -27,6 +27,10 @@ #else # define __THREAD_FUNC_RET__ void* #endif +#if defined(LINUX) +# include +# define gettid() syscall(SYS_gettid) +#endif #if defined(MAC_OS) || defined(BLACKBERRY) # include #endif @@ -213,8 +217,9 @@ bool PIThread::start(int timer_delay) { if (ret == 0) { # ifdef MAC_OS pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_); -# else - tid_ = PRIVATE->thread; +//# else +// pthread_threadid_np(); +// tid_ = PRIVATE->thread; # endif #else if (PRIVATE->thread != 0) CloseHandle(PRIVATE->thread); @@ -249,8 +254,8 @@ bool PIThread::startOnce() { if (ret == 0) { # ifdef MAC_OS pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_); -# else - tid_ = PRIVATE->thread; +//# else +// tid_ = PRIVATE->thread; # endif #else if (PRIVATE->thread != 0) CloseHandle(PRIVATE->thread); @@ -386,6 +391,9 @@ void PIThread::__thread_func__(void * t) { PIThread & ct = *((PIThread * )t); #ifdef WINDOWS ct.tid_ = GetCurrentThreadId(); +#endif +#ifdef LINUX + ct.tid_ = gettid(); #endif PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name()); REGISTER_THREAD(&ct); @@ -457,6 +465,9 @@ void PIThread::__thread_func_once__(void * t) { PIThread & ct = *((PIThread * )t); #ifdef WINDOWS ct.tid_ = GetCurrentThreadId(); +#endif +#ifdef LINUX + ct.tid_ = gettid(); #endif PIINTROSPECTION_REGISTER_THREAD(ct.tid(), ct.priority(), ct.name()); REGISTER_THREAD(&ct); diff --git a/utils/system_daemon/main.cpp b/utils/system_daemon/main.cpp index a6a24994..0476d690 100755 --- a/utils/system_daemon/main.cpp +++ b/utils/system_daemon/main.cpp @@ -112,7 +112,7 @@ public: 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("", CellFormat()); - local_info_base << TileList::Row("PIThreads:", CellFormat()); + local_info_base << TileList::Row("Threads:", CellFormat()); return ret; } PIScreenTile * peerDiagTile() { @@ -222,9 +222,9 @@ public: piForeachC (PISystemMonitor::ThreadStats & t, ts) { PIString line = PIString(++num).expandLeftTo(2, ' ') + ": "; 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 "; - 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, ' ') + " %"; tile->content << TileList::Row(line, CellFormat()); } @@ -393,6 +393,7 @@ int main(int argc, char * argv[]) { MainMenu * menu = new MainMenu(*daemon); if (sapp) CONNECTU(sapp, messageReceived, menu, messageFromApp); if (cli.hasArgument("silent")) { + PICout::setBufferActive(false); PIKbdListener ls; ls.enableExitCapture(PIKbdListener::F10); ls.start();