diff --git a/CMakeLists.txt b/CMakeLists.txt index 1834903d..e720420e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -141,7 +141,7 @@ if(DEBUG) message(STATUS "Building debug version") else() set(CMAKE_BUILD_TYPE "Release") - message(STATUS "Building relaese version") + message(STATUS "Building release version") endif() diff --git a/src_main/system/pisystemmonitor.cpp b/src_main/system/pisystemmonitor.cpp index c171a7bd..f3145c99 100755 --- a/src_main/system/pisystemmonitor.cpp +++ b/src_main/system/pisystemmonitor.cpp @@ -103,7 +103,7 @@ bool PISystemMonitor::startOnSelf(int interval_ms) { PIVector PISystemMonitor::threadsStatistic() const { mutex_.lock(); - PIVector ret = cur_tm.values(); + PIVector 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 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::iterator i = cur_tm.begin(); i != cur_tm.end(); ++i) { + for (PIMap::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 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, ""); #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); +} diff --git a/src_main/system/pisystemmonitor.h b/src_main/system/pisystemmonitor.h index 5ccbfc31..daecc421 100755 --- a/src_main/system/pisystemmonitor.h +++ b/src_main/system/pisystemmonitor.h @@ -54,7 +54,8 @@ public: float cpu_load_user; }; struct ThreadStats { - ThreadStats() {cpu_load_kernel = cpu_load_user = -1.f;} + ThreadStats() {id = 0; cpu_load_kernel = cpu_load_user = -1.f;} + llong id; PIString name; PIDateTime created; PISystemTime work_time; @@ -81,13 +82,14 @@ public: private: void run(); void makeStrings(); - void gatherThreadsStats(); + 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; + PIMap last_tm, cur_tm; + PIMap tbid; bool self_; int pID_, page_size, cpu_count, cycle; PRIVATE_DECLARATION diff --git a/src_main/thread/pithread.cpp b/src_main/thread/pithread.cpp index 502418f3..3e4ce98e 100755 --- a/src_main/thread/pithread.cpp +++ b/src_main/thread/pithread.cpp @@ -271,7 +271,7 @@ bool PIThread::startOnce() { void PIThread::terminate() { if (PRIVATE->thread == 0) return; - REGISTER_THREAD(this); + UNREGISTER_THREAD(this); PIINTROSPECTION_UNREGISTER_THREAD(tid()); terminating = running_ = false; tid_ = -1; diff --git a/utils/system_daemon/daemon.cpp b/utils/system_daemon/daemon.cpp index c09ed828..9aa99b9b 100644 --- a/utils/system_daemon/daemon.cpp +++ b/utils/system_daemon/daemon.cpp @@ -468,13 +468,6 @@ void Daemon::tileEvent(PIScreenTile * t, TileEvent e) { void Daemon::keyEvent(PIKbdListener::KeyEvent key) { - /*if (key.key == PIKbdListener::F2) { - PICout::setBufferActive(true, true); - piCout << sys_mon.threadsStatistic(); - PIFile f("_.txt", PIIODevice::ReadWrite); - f.clear(); - f << PICout::buffer(true); - }*/ if (!tile_root->visible) return; if (screen->dialogTile()) return; switch (key.key) { diff --git a/utils/system_daemon/main.cpp b/utils/system_daemon/main.cpp index 87dd8088..a6a24994 100755 --- a/utils/system_daemon/main.cpp +++ b/utils/system_daemon/main.cpp @@ -78,9 +78,10 @@ public: mt->hide(); mt->name() = "peer diag"; center->addTile(mt); mtiles << mt; - TilePICout * outt = new TilePICout(); - outt->size_policy = PIScreenTypes::Expanding; - screen->rootTile()->addTile(outt); + tpicout = new TilePICout(); + tpicout->hide(); + tpicout->size_policy = PIScreenTypes::Expanding; + screen->rootTile()->addTile(tpicout); CONNECTU(screen, tileEvent, this, tileEvent) CONNECTU(screen, keyPressed, this, keyEvent) @@ -219,7 +220,7 @@ public: piForeachC (PISystemMonitor::ThreadStats & t, ts) maxlen = piMaxi(maxlen, t.name.length()); piForeachC (PISystemMonitor::ThreadStats & t, ts) { - PIString line = PIString(num++).expandLeftTo(2, ' ') + ": "; + 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); line += ns.expandLeftTo(5, ' ') + " %, u "; @@ -280,6 +281,10 @@ public: } } EVENT_HANDLER1(void, keyEvent, PIKbdListener::KeyEvent, e) { + if (e.key == PIKbdListener::F9) { + tpicout->visible = !tpicout->visible; + return; + } if (e.key == PIKbdListener::Esc && e.modifiers[PIKbdListener::Shift]) { PIKbdListener::exiting = true; return; @@ -296,6 +301,7 @@ public: Daemon & daemon_; PIScreenTile * tmenu, * tinfo, * tfm, * tdaemon, * tpeer, * tpeerdiag; TileList * peers_tl, * addrs_tl, * peermap_tl; + TilePICout * tpicout; TileSimple * title; TileSimple * peerinfo_tl, * peerinfo_header; TileSimple * peerdiagdata_tl, * peerdiagservice_tl; @@ -380,6 +386,7 @@ int main(int argc, char * argv[]) { Daemon * daemon = new Daemon(); if (!sip.isEmpty()) daemon->setTcpServerIP(sip); sys_mon.startOnSelf(); + //sys_mon.startOnProcess(12404); screen->enableExitCapture(PIKbdListener::F10); if (!name.isEmpty()) daemon->changeName(pisd_prefix + name);