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

This commit is contained in:
2017-06-21 18:23:53 +00:00
parent ed1a89508a
commit 96d67076dc
6 changed files with 62 additions and 47 deletions

View File

@@ -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()

View File

@@ -103,7 +103,7 @@ bool PISystemMonitor::startOnSelf(int interval_ms) {
PIVector<PISystemMonitor::ThreadStats> PISystemMonitor::threadsStatistic() const {
mutex_.lock();
PIVector<PISystemMonitor::ThreadStats> ret = cur_tm.values();
PIVector<PISystemMonitor::ThreadStats> 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<PIThread * > 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<const void*, ThreadStats>::iterator i = cur_tm.begin(); i != cur_tm.end(); ++i) {
for (PIMap<llong, 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);
}
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<PIThread*> 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, "<non-PIThread>");
#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);
}

View File

@@ -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<ThreadStats> cur_ts;
PIMap<const void*, ThreadStats> last_tm, cur_tm;
PIMap<llong, ThreadStats> last_tm, cur_tm;
PIMap<llong, PIString> tbid;
bool self_;
int pID_, page_size, cpu_count, cycle;
PRIVATE_DECLARATION

View File

@@ -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;

View File

@@ -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) {

View File

@@ -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);