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

This commit is contained in:
2019-06-26 18:52:05 +00:00
parent 52907c85b2
commit 0c432eae34
16 changed files with 301 additions and 142 deletions

View File

@@ -259,11 +259,11 @@ if(INTROSPECTION)
else()
message(STATUS "Building PIP without introspection")
endif()
if ((NOT _PIP_SAVED_DEFS) OR (NOT "x${_PIP_SAVED_DEFS}" STREQUAL "x${_PIP_DEFS}"))
if ((NOT DEFINED _PIP_SAVED_DEFS) OR (NOT "x${_PIP_SAVED_DEFS}" STREQUAL "x${_PIP_DEFS}"))
set(_PIP_SAVED_DEFS "${_PIP_DEFS}" CACHE STRING "pip_defs" FORCE)
file(WRITE "${PIP_SRC_MAIN}/pip_defs.h" "// This file was generated by PIP CMake, don`t edit it!\n")
if (NOT "x${_PIP_DEFS}" STREQUAL "x")
file(APPEND "${PIP_SRC_MAIN}/pip_defs.h" "#define ${_PIP_DEFS}\n")
file(APPEND "${PIP_SRC_MAIN}/pip_defs.h" "#ifndef ${_PIP_DEFS}\n# define ${_PIP_DEFS}\n#endif\n")
endif()
endif()
list(APPEND HDRS "${PIP_SRC_MAIN}/pip_defs.h")

View File

@@ -845,14 +845,14 @@ void PIConsole::addVariable(const PIString & name, const PIDiagnostics * ptr, in
}
void PIConsole::addVariable(const PIString & name, const PISystemMonitor * ptr, int col, FormatFlags format) {
addString("monitor " + name, col, format | PIConsole::Bold);
addVariable("PID", &(ptr->statistic().ID), col, format);
//addVariable("PID", &(ptr->statistic().ID), col, format);
//addVariable("state", &(ptr->statistic().state), col, format);
addVariable("threads", &(ptr->statistic().threads), col, format);
addVariable("priority", &(ptr->statistic().priority), col, format);
addVariable("memory physical", &(ptr->statistic().physical_memsize_readable), col, format);
addVariable("memory shared", &(ptr->statistic().share_memsize_readable), col, format);
addVariable("cpu load kernel", &(ptr->statistic().cpu_load_system), col, format);
addVariable("cpu load user", &(ptr->statistic().cpu_load_user), col, format);
//addVariable("threads", &(ptr->statistic().threads), col, format);
//addVariable("priority", &(ptr->statistic().priority), col, format);
//addVariable("memory physical", &(ptr->statistic().physical_memsize_readable), col, format);
//addVariable("memory shared", &(ptr->statistic().share_memsize_readable), col, format);
//addVariable("cpu load kernel", &(ptr->statistic().cpu_load_system), col, format);
//addVariable("cpu load user", &(ptr->statistic().cpu_load_user), col, format);
}
void PIConsole::addBitVariable(const PIString & name, const void * ptr, int fromBit, int bitCount, int col, FormatFlags format) {
vid++; tv.id = vid; tv.size = sizeof(ullong); tv.name = name; tv.bitFrom = fromBit; tv.bitCount = bitCount; tv.type = 14; tv.ptr = ptr; tv.format = format;

View File

@@ -28,6 +28,7 @@ class PIThread;
class PITimer;
class PIPeer;
class PIIntrospection;
class PIIntrospectionServer;
#ifdef PIP_INTROSPECTION
#define __PIINTROSPECTION_SINGLETON_H__(T) \

View File

@@ -23,13 +23,6 @@
#include "piintrospection_base.h"
//#if defined(__PIIS__)
//# undef __PIIS__
//#endif
//# if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
//#endif
#if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
class PIIntrospectionContainers;

View File

@@ -17,8 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef PIP_INTROSPECTION
#include "piintrospection_containers_p.h"
#include <stdio.h>
@@ -95,9 +93,9 @@ uint PIIntrospectionContainers::typeID(const char * tn) {
PIByteArray & operator <<(PIByteArray & s, const std::unordered_map<uint, std::string> & v) {
PIByteArray & operator <<(PIByteArray & s, const std::map<uint, std::string> & v) {
PIMap<uint, PIString> m;
for (typename std::unordered_map<uint, std::string>::const_iterator i = v.cbegin(); i != v.cend(); ++i) {
for (typename std::map<uint, std::string>::const_iterator i = v.cbegin(); i != v.cend(); ++i) {
m[i->first] = PIStringAscii(i->second.c_str());
}
s << m;
@@ -113,5 +111,3 @@ PIByteArray & operator >>(PIByteArray & s, PIIntrospectionContainers::Type & v)
s >> v.count >> v.items >> v.bytes_allocated >> v.bytes_used;
return s;
}
#endif //PIP_INTROSPECTION

View File

@@ -20,10 +20,8 @@
#ifndef PIINTROSPECTION_CONTAINERS_P_H
#define PIINTROSPECTION_CONTAINERS_P_H
#ifdef PIP_INTROSPECTION
#include "pimutex.h"
#include <unordered_map>
#include <map>
#include <string>
#include "picrc.h"
@@ -49,16 +47,15 @@ public:
ullong bytes_used;
};
std::unordered_map<uint, Type> data;
std::unordered_map<uint, std::string> typenames;
std::map<uint, Type> data;
std::map<uint, std::string> typenames;
PIMutex mutex;
CRC_32 crc;
};
PIByteArray & operator <<(PIByteArray & s, const std::unordered_map<uint, std::string> & v);
PIByteArray & operator <<(PIByteArray & s, const std::map<uint, std::string> & v);
PIByteArray & operator <<(PIByteArray & s, const PIIntrospectionContainers::Type & v);
PIByteArray & operator >>(PIByteArray & s, PIIntrospectionContainers::Type & v);
#endif // PIP_INTROSPECTION
#endif // PIINTROSPECTION_CONTAINERS_P_H

View File

@@ -21,6 +21,7 @@
#include "piintrospection_server.h"
#include "piintrospection_server_p.h"
#include "piprocess.h"
#include "pichunkstream.h"
@@ -31,15 +32,32 @@ PRIVATE_DEFINITION_END(PIIntrospectionServer)
PIIntrospectionServer::PIIntrospectionServer(): PIPeer(genName()) {
PRIVATE->process_info = PIIntrospection::getInfo();
sysmon = 0;
}
PIIntrospectionServer::~PIIntrospectionServer() {
PIPeer::stop();
if (sysmon)
if (sysmon->property("__iserver__").toBool())
delete sysmon;
sysmon = 0;
}
void PIIntrospectionServer::start() {
if (!sysmon) {
sysmon = PISystemMonitor::Pool::instance()->getByPID(PIProcess::currentPID());
if (sysmon) {
piCoutObj << "using existing sysmon";
CONNECTU(sysmon, deleted, this, sysmonDeleted);
} else {
piCoutObj << "create own sysmon";
sysmon = new PISystemMonitor();
sysmon->setProperty("__iserver__", true);
sysmon->startOnSelf();
}
}
PIPeer::start();
}
@@ -60,6 +78,11 @@ void PIIntrospectionServer::dataReceived(const PIString & from, const PIByteArra
PIChunkStream cs;
if (ri.types[PIIntrospection::itInfo])
cs.add(PIIntrospection::itInfo, PIIntrospection::packInfo());
if (ri.types[PIIntrospection::itProcStat]) {
sysmon_mutex.lock();
cs.add(PIIntrospection::itProcStat, PIIntrospection::packProcStat(sysmon));
sysmon_mutex.unlock();
}
if (ri.types[PIIntrospection::itContainers])
cs.add(PIIntrospection::itContainers, PIIntrospection::packContainers());
if (ri.types[PIIntrospection::itObjects])
@@ -72,5 +95,10 @@ void PIIntrospectionServer::dataReceived(const PIString & from, const PIByteArra
send(from, ba);
}
#endif // PIP_INTROSPECTION
void PIIntrospectionServer::sysmonDeleted() {
PIMutexLocker _ml(sysmon_mutex);
sysmon = 0;
}
#endif // PIP_INTROSPECTION

View File

@@ -26,6 +26,7 @@
#if defined(PIP_INTROSPECTION) && !defined(PIP_FORCE_NO_PIINTROSPECTION)
class PIIntrospectionServer;
class PISystemMonitor;
# define PIINTROSPECTION_SERVER (PIIntrospectionServer::instance())
# define PIINTROSPECTION_START PIINTROSPECTION_SERVER->start();
@@ -44,9 +45,13 @@ private:
PIString genName();
virtual void dataReceived(const PIString & from, const PIByteArray & data);
EVENT_HANDLER(void, sysmonDeleted);
PRIVATE_DECLARATION
PITimer itimer;
PISystemMonitor * sysmon;
PIMutex sysmon_mutex;
};
#else

View File

@@ -17,8 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef PIP_INTROSPECTION
#include "piintrospection_server_p.h"
#include "pichunkstream.h"
#include "piinit.h"
@@ -88,9 +86,9 @@ PIVector<PIIntrospection::ObjectInfo> PIIntrospection::getObjects() {
}
PIByteArray & operator <<(PIByteArray & s, const std::unordered_map<uint, PIIntrospectionContainers::Type> & v) {
PIByteArray & operator <<(PIByteArray & s, const std::map<uint, PIIntrospectionContainers::Type> & v) {
PIMap<uint, PIIntrospectionContainers::Type> m;
for (typename std::unordered_map<uint, PIIntrospectionContainers::Type>::const_iterator i = v.cbegin(); i != v.cend(); ++i) {
for (typename std::map<uint, PIIntrospectionContainers::Type>::const_iterator i = v.cbegin(); i != v.cend(); ++i) {
m[i->first] = i->second;
}
s << m;
@@ -188,13 +186,37 @@ void PIIntrospection::unpackInfo(PIByteArray & ba, PIIntrospection::ProcessInfo
}
PIByteArray PIIntrospection::packProcStat(PISystemMonitor * sm) {
ProcessStat ps;
if (sm) {
ps.proc = sm->statistic();
ps.threads = sm->threadsStatistic();
}
PIByteArray ret;
ret << ps.proc << ps.threads;
return ret;
}
void PIIntrospection::unpackProcStat(PIByteArray & ba, PIIntrospection::ProcessStat & info) {
ba >> info.proc >> info.threads;
}
PIByteArray PIIntrospection::packContainers() {
PIByteArray ret;
PIIntrospectionContainers * p = PIINTROSPECTION_CONTAINERS->p;
p->mutex.lock();
std::unordered_map<uint, PIIntrospectionContainers::Type> data = p->data;
std::unordered_map<uint, std::string> typenames = p->typenames;
p->mutex.unlock();
std::map<uint, PIIntrospectionContainers::Type> data;
std::map<uint, std::string> typenames;
PIIntrospectionContainers * p = 0;
#ifdef PIP_INTROSPECTION
p = PIINTROSPECTION_CONTAINERS->p;
#endif
if (p) {
p->mutex.lock();
data = p->data;
typenames = p->typenames;
p->mutex.unlock();
}
ret << data << typenames;
return ret;
}
@@ -209,15 +231,22 @@ void PIIntrospection::unpackContainers(PIByteArray & ba, PIMap<uint, PIIntrospec
PIByteArray PIIntrospection::packThreads() {
PIByteArray ret;
PIIntrospectionThreads * p = PIINTROSPECTION_THREADS->p;
p->mutex.lock();
PIMap<PIThread*, PIIntrospectionThreads::ThreadInfo> & tm(p->threads);
for (PIMap<PIThread*, PIIntrospectionThreads::ThreadInfo>::iterator i = tm.begin(); i != tm.end(); ++i) {
i.value().classname = PIStringAscii(i.key()->className());
i.value().name = i.key()->name();
PIIntrospectionThreads * p = 0;
#ifdef PIP_INTROSPECTION
p = PIINTROSPECTION_THREADS->p;
#endif
if (p) {
p->mutex.lock();
PIMap<PIThread*, PIIntrospectionThreads::ThreadInfo> & tm(p->threads);
for (PIMap<PIThread*, PIIntrospectionThreads::ThreadInfo>::iterator i = tm.begin(); i != tm.end(); ++i) {
i.value().classname = PIStringAscii(i.key()->className());
i.value().name = i.key()->name();
}
ret << tm.values();
p->mutex.unlock();
} else {
ret << PIVector<PIIntrospectionThreads::ThreadInfo>();
}
ret << tm.values();
p->mutex.unlock();
return ret;
}
@@ -239,5 +268,3 @@ void PIIntrospection::unpackObjects(PIByteArray & ba, PIVector<PIIntrospection::
objects.clear();
ba >> objects;
}
#endif // PIP_INTROSPECTION

View File

@@ -20,13 +20,11 @@
#ifndef PIINTROSPECTION_SERVER_P_H
#define PIINTROSPECTION_SERVER_P_H
#ifdef PIP_INTROSPECTION
#include "piintrospection_containers.h"
#include "piintrospection_containers_p.h"
#include "piintrospection_threads.h"
#include "piintrospection_threads_p.h"
#include "pitime.h"
#include "pisystemmonitor.h"
class PIIntrospection {
@@ -34,9 +32,10 @@ public:
enum InfoTypes {
itInfo = 0x01,
itContainers = 0x02,
itObjects = 0x04,
itThreads = 0x08,
itProcStat = 0x02,
itContainers = 0x04,
itObjects = 0x08,
itThreads = 0x10,
};
struct RequiredInfo {
@@ -54,6 +53,13 @@ public:
PIStringList build_options;
};
struct ProcessStat {
ProcessStat() {}
PISystemMonitor::ProcessStats proc;
PIVector<PISystemMonitor::ThreadStats> threads;
};
struct ObjectInfo {
ObjectInfo();
@@ -71,6 +77,9 @@ public:
static PIByteArray packInfo();
static void unpackInfo(PIByteArray & ba, ProcessInfo & info);
static PIByteArray packProcStat(PISystemMonitor * sm);
static void unpackProcStat(PIByteArray & ba, ProcessStat & info);
static PIByteArray packContainers();
static void unpackContainers(PIByteArray & ba, PIMap<uint, PIIntrospectionContainers::Type> & data, PIMap<uint, PIString> & typenames);
@@ -82,7 +91,7 @@ public:
};
PIByteArray & operator <<(PIByteArray & s, const std::unordered_map<uint, PIIntrospectionContainers::Type> & v);
PIByteArray & operator <<(PIByteArray & s, const std::map<uint, PIIntrospectionContainers::Type> & v);
PIByteArray & operator <<(PIByteArray & b, const PIIntrospection::RequiredInfo & v);
PIByteArray & operator >>(PIByteArray & b, PIIntrospection::RequiredInfo & v);
@@ -93,5 +102,4 @@ PIByteArray & operator >>(PIByteArray & b, PIIntrospection::ProcessInfo & v);
PIByteArray & operator <<(PIByteArray & b, const PIIntrospection::ObjectInfo & v);
PIByteArray & operator >>(PIByteArray & b, PIIntrospection::ObjectInfo & v);
#endif // PIP_INTROSPECTION
#endif // PIINTROSPECTION_SERVER_P_H

View File

@@ -17,8 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef PIP_INTROSPECTION
#include "piintrospection_threads_p.h"
@@ -100,5 +98,3 @@ PIByteArray & operator >>(PIByteArray & b, PIIntrospectionThreads::ThreadInfo &
v.state = (PIIntrospectionThreads::ThreadState)st;
return b;
}
#endif // PIP_INTROSPECTION

View File

@@ -20,8 +20,6 @@
#ifndef PIINTROSPECTION_THREADS_P_H
#define PIINTROSPECTION_THREADS_P_H
#ifdef PIP_INTROSPECTION
#include "pimap.h"
#include "pithread.h"
@@ -62,5 +60,4 @@ public:
PIByteArray & operator <<(PIByteArray & b, const PIIntrospectionThreads::ThreadInfo & v);
PIByteArray & operator >>(PIByteArray & b, PIIntrospectionThreads::ThreadInfo & v);
#endif // PIP_INTROSPECTION
#endif // PIINTROSPECTION_THREADS_P_H

View File

@@ -37,6 +37,32 @@
#endif
PISystemMonitor::ProcessStatsFixed::ProcessStatsFixed() {
ID = parent_ID = group_ID = session_ID = priority = threads = 0;
physical_memsize = resident_memsize = share_memsize = virtual_memsize = data_memsize = 0;
cpu_load_user = cpu_load_system = 0.f;
}
void PISystemMonitor::ProcessStats::makeStrings() {
physical_memsize_readable.setReadableSize(physical_memsize);
resident_memsize_readable.setReadableSize(resident_memsize);
share_memsize_readable.setReadableSize(share_memsize);
virtual_memsize_readable.setReadableSize(virtual_memsize);
data_memsize_readable.setReadableSize(data_memsize);
}
PISystemMonitor::ThreadStatsFixed::ThreadStatsFixed() {
id = 0;
cpu_load_kernel = cpu_load_user = -1.f;
}
#ifndef FREERTOS
PRIVATE_DEFINITION_START(PISystemMonitor)
#ifndef WINDOWS
@@ -82,17 +108,11 @@ PISystemMonitor::~PISystemMonitor() {
}
PISystemMonitor::ProcessStats::ProcessStats() {
ID = parent_ID = group_ID = session_ID = priority = threads = 0;
physical_memsize = resident_memsize = share_memsize = virtual_memsize = data_memsize = 0;
cpu_load_user = cpu_load_system = 0.f;
}
#ifndef FREERTOS
bool PISystemMonitor::startOnProcess(int pID, int interval_ms) {
stop();
pID_ = pID;
Pool::instance()->add(this);
cycle = -1;
#ifndef WINDOWS
# ifndef MAC_OS
@@ -116,6 +136,7 @@ bool PISystemMonitor::startOnProcess(int pID, int interval_ms) {
}
#endif
bool PISystemMonitor::startOnSelf(int interval_ms) {
#ifndef FREERTOS
bool ret = startOnProcess(PIProcess::currentPID(), interval_ms);
@@ -135,6 +156,13 @@ PIVector<PISystemMonitor::ThreadStats> PISystemMonitor::threadsStatistic() const
}
void PISystemMonitor::setStatistic(const PISystemMonitor::ProcessStats & s) {
PIMutexLocker _ml(stat_mutex);
stat = s;
stat.makeStrings();
}
void PISystemMonitor::stop() {
PIThread::stop();
#ifdef WINDOWS
@@ -143,7 +171,13 @@ void PISystemMonitor::stop() {
PRIVATE->hProc = 0;
}
#endif
Pool::instance()->remove(this);
}
PISystemMonitor::ProcessStats PISystemMonitor::statistic() const {
PIMutexLocker _ml(stat_mutex);
return stat;
}
@@ -169,7 +203,8 @@ void PISystemMonitor::run() {
gatherThread(t->tid());
#else
//piCout << tbid.keys().toType<uint>();
stat.ID = pID_;
ProcessStats tstat;
tstat.ID = pID_;
#ifndef WINDOWS
tbid[pID_] = "main";
# ifdef MAC_OS
@@ -186,10 +221,10 @@ void PISystemMonitor::run() {
PRIVATE->cpu_s_prev = PRIVATE->cpu_s_cur;
PRIVATE->cpu_u_cur = uint64toST(ru.ri_user_time);
PRIVATE->cpu_s_cur = uint64toST(ru.ri_system_time);
stat.cpu_load_system = 100.f * (PRIVATE->cpu_s_cur - PRIVATE->cpu_s_prev).toMilliseconds() / delay_;
stat.cpu_load_user = 100.f * (PRIVATE->cpu_u_cur - PRIVATE->cpu_u_prev).toMilliseconds() / delay_;
//stat.cpu_load_system /= cpu_count;
//stat.cpu_load_user /= cpu_count;
tstat.cpu_load_system = 100.f * (PRIVATE->cpu_s_cur - PRIVATE->cpu_s_prev).toMilliseconds() / delay_;
tstat.cpu_load_user = 100.f * (PRIVATE->cpu_u_cur - PRIVATE->cpu_u_prev).toMilliseconds() / delay_;
//tstat.cpu_load_system /= cpu_count;
//tstat.cpu_load_user /= cpu_count;
cycle = 0;
//piCout << (PRIVATE->cpu_u_cur - PRIVATE->cpu_u_prev).toMilliseconds() / delay_;
# else
@@ -204,15 +239,15 @@ void PISystemMonitor::run() {
break;
}
}
stat.exec_name = str.mid(si, fi - si);
tstat.exec_name = str.mid(si, fi - si);
str.cutMid(si - 1, fi - si + 3);
PIStringList sl = str.split(" ");
if (sl.size_s() < 19) return;
stat.ID = sl[0].toInt();
stat.state = sl[1];
stat.parent_ID = sl[2].toInt();
stat.group_ID = sl[3].toInt();
stat.session_ID = sl[4].toInt();
tstat.ID = sl[0].toInt();
tstat.state = sl[1];
tstat.parent_ID = sl[2].toInt();
tstat.group_ID = sl[3].toInt();
tstat.session_ID = sl[4].toInt();
if (cycle < 0) {
PRIVATE->cpu_u_prev = PRIVATE->cpu_u_cur = sl[12].toLLong();
PRIVATE->cpu_s_prev = PRIVATE->cpu_s_cur = sl[13].toLLong();
@@ -223,14 +258,14 @@ 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) / (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;
tstat.cpu_load_system = (PRIVATE->cpu_s_cur - PRIVATE->cpu_s_prev) / (delay_ / 1000.);
tstat.cpu_load_user = (PRIVATE->cpu_u_cur - PRIVATE->cpu_u_prev) / (delay_ / 1000.);
tstat.cpu_load_system /= cpu_count;
tstat.cpu_load_user /= cpu_count;
cycle = 0;
//}
stat.priority = sl[16].toInt();
stat.threads = sl[18].toInt();
tstat.priority = sl[16].toInt();
tstat.threads = sl[18].toInt();
//piCout << "\n";
//piCout << sl[0] << sl[12] << sl[13];
@@ -238,11 +273,11 @@ void PISystemMonitor::run() {
str = PRIVATE->filem.readAll(true);
sl = str.split(" ");
if (sl.size_s() < 6) return;
stat.virtual_memsize = sl[0].toLong() * page_size;
stat.resident_memsize = sl[1].toLong() * page_size;
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;
tstat.virtual_memsize = sl[0].toLong() * page_size;
tstat.resident_memsize = sl[1].toLong() * page_size;
tstat.share_memsize = sl[2].toLong() * page_size;
tstat.data_memsize = sl[5].toLong() * page_size;
tstat.physical_memsize = tstat.resident_memsize - tstat.share_memsize;
PIVector<PIFile::FileInfo> tld = PIDir(PRIVATE->proc_dir + "task").entries();
piForeachC (PIFile::FileInfo & i, tld) {
@@ -255,9 +290,9 @@ void PISystemMonitor::run() {
// HMODULE hMod;
// DWORD cbNeeded;
if (GetProcessMemoryInfo(PRIVATE->hProc, &PRIVATE->mem_cnt, sizeof(PRIVATE->mem_cnt)) != 0) {
stat.physical_memsize = PRIVATE->mem_cnt.WorkingSetSize;
tstat.physical_memsize = PRIVATE->mem_cnt.WorkingSetSize;
}
stat.priority = GetPriorityClass(PRIVATE->hProc);
tstat.priority = GetPriorityClass(PRIVATE->hProc);
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, pID_);
int thcnt = 0;
if (snap != 0) {
@@ -276,7 +311,7 @@ void PISystemMonitor::run() {
//piCout << thread.th32ThreadID;
}
}
stat.threads = thcnt;
tstat.threads = thcnt;
CloseHandle(snap);
}
FILETIME ft0, ft1, ft_kernel, ft_user;
@@ -291,24 +326,24 @@ void PISystemMonitor::run() {
//cycle++;
cycle = 0;
if (el_s <= 0.) {
stat.cpu_load_system = 0.f;
stat.cpu_load_user = 0.f;
tstat.cpu_load_system = 0.f;
tstat.cpu_load_user = 0.f;
} else {
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;
tstat.cpu_load_system = (tm_kernel_c - PRIVATE->tm_kernel).toSeconds() / el_s;
tstat.cpu_load_user = (tm_user_c - PRIVATE->tm_user).toSeconds() / el_s;
}
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;
tstat.cpu_load_system = 0.f;
tstat.cpu_load_user = 0.f;
}
PRIVATE->tm.reset();
#endif
#endif
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);
tstat.cpu_load_system = piClampf(tstat.cpu_load_system, 0.f, 100.f);
tstat.cpu_load_user = piClampf(tstat.cpu_load_user , 0.f, 100.f);
//PISystemTime dt = PISystemTime::fromMilliseconds(delay_);
for (PIMap<llong, ThreadStats>::iterator i = cur_tm.begin(); i != cur_tm.end(); ++i) {
@@ -323,19 +358,13 @@ void PISystemMonitor::run() {
lock();
cur_ts = cur_tm.values();
unlock();
stat.ram_total = totalRAM();
stat.ram_used = usedRAM();
stat.ram_free = freeRAM();
makeStrings();
}
void PISystemMonitor::makeStrings() {
stat.physical_memsize_readable.setReadableSize(stat.physical_memsize);
stat.resident_memsize_readable.setReadableSize(stat.resident_memsize);
stat.share_memsize_readable.setReadableSize(stat.share_memsize);
stat.virtual_memsize_readable.setReadableSize(stat.virtual_memsize);
stat.data_memsize_readable.setReadableSize(stat.data_memsize);
tstat.ram_total = totalRAM();
tstat.ram_used = usedRAM();
tstat.ram_free = freeRAM();
stat_mutex.lock();
stat = tstat;
stat.makeStrings();
stat_mutex.unlock();
}
@@ -428,3 +457,60 @@ ullong PISystemMonitor::usedRAM() {
#endif
return 0;
}
PISystemMonitor::Pool * PISystemMonitor::Pool::instance() {
static Pool ret;
return &ret;
}
PISystemMonitor * PISystemMonitor::Pool::getByPID(int pID) {
PIMutexLocker _ml(mutex);
return sysmons.value(pID, 0);
}
void PISystemMonitor::Pool::add(PISystemMonitor * sm) {
PIMutexLocker _ml(mutex);
sysmons[sm->pID()] = sm;
}
void PISystemMonitor::Pool::remove(PISystemMonitor * sm) {
PIMutexLocker _ml(mutex);
sysmons.remove(sm->pID());
}
PIByteArray & operator <<(PIByteArray & s, const PISystemMonitor::ProcessStats & v) {
s << PIByteArray::RawData(&v, sizeof(PISystemMonitor::ProcessStatsFixed))
<< v.exec_name << v.state;
return s;
}
PIByteArray & operator >>(PIByteArray & s, PISystemMonitor::ProcessStats & v) {
s >> PIByteArray::RawData(&v, sizeof(PISystemMonitor::ProcessStatsFixed))
>> v.exec_name >> v.state;
v.makeStrings();
return s;
}
PIByteArray & operator <<(PIByteArray & s, const PISystemMonitor::ThreadStats & v) {
s << PIByteArray::RawData(&v, sizeof(PISystemMonitor::ThreadStatsFixed))
<< v.name << v.created;
return s;
}
PIByteArray & operator >>(PIByteArray & s, PISystemMonitor::ThreadStats & v) {
s >> PIByteArray::RawData(&v, sizeof(PISystemMonitor::ThreadStatsFixed))
>> v.name >> v.created;
return s;
}

View File

@@ -26,14 +26,14 @@
class PIP_EXPORT PISystemMonitor: public PIThread
{
PIOBJECT_SUBCLASS(PISystemMonitor, PIThread)
friend class PIIntrospectionServer;
public:
PISystemMonitor();
~PISystemMonitor();
struct ProcessStats {
ProcessStats();
PIString exec_name;
PIString state;
#pragma pack(push, 1)
struct ProcessStatsFixed {
ProcessStatsFixed();
int ID;
int parent_ID;
int group_ID;
@@ -48,40 +48,47 @@ public:
ullong ram_total;
ullong ram_free;
ullong ram_used;
PIString physical_memsize_readable;
PIString resident_memsize_readable;
PIString share_memsize_readable;
PIString virtual_memsize_readable;
PIString data_memsize_readable;
float cpu_load_system;
float cpu_load_user;
};
struct ThreadStats {
ThreadStats() {id = 0; cpu_load_kernel = cpu_load_user = -1.f;}
struct ThreadStatsFixed {
ThreadStatsFixed();
llong id;
PIString name;
PIDateTime created;
PISystemTime work_time;
PISystemTime kernel_time;
PISystemTime user_time;
float cpu_load_kernel;
float cpu_load_user;
/*PIString device;
PIString filesystem;
PIString label;
ullong space_all;
ullong space_used;
ullong space_free;*/
};
#pragma pack(pop)
struct ProcessStats: ProcessStatsFixed {
void makeStrings();
PIString exec_name;
PIString state;
PIString physical_memsize_readable;
PIString resident_memsize_readable;
PIString share_memsize_readable;
PIString virtual_memsize_readable;
PIString data_memsize_readable;
};
struct ThreadStats: ThreadStatsFixed {
PIString name;
PIDateTime created;
};
#ifndef FREERTOS
bool startOnProcess(int pID, int interval_ms = 1000);
#endif
bool startOnSelf(int interval_ms = 1000);
void stop();
const ProcessStats & statistic() const {return stat;}
int pID() const {return pID_;}
ProcessStats statistic() const;
PIVector<ThreadStats> threadsStatistic() const;
void setStatistic(const ProcessStats & s) {stat = s; makeStrings();}
void setStatistic(const ProcessStats & s);
static ullong totalRAM();
static ullong freeRAM();
@@ -90,19 +97,32 @@ public:
private:
void run();
void makeStrings();
void gatherThread(llong id);
float calcThreadUsage(PISystemTime & t_new, PISystemTime & t_old);
ProcessStats stat;
PIVector<ThreadStats> cur_ts;
PIMap<llong, ThreadStats> last_tm, cur_tm;
PIMap<llong, PIString> tbid;
mutable PIMutex stat_mutex;
int pID_, page_size, cpu_count, cycle;
#ifndef FREERTOS
PRIVATE_DECLARATION
#endif
class Pool {
friend class PISystemMonitor;
public:
static Pool * instance();
PISystemMonitor * getByPID(int pID);
private:
void add(PISystemMonitor * sm);
void remove(PISystemMonitor * sm);
PIMap<int, PISystemMonitor*> sysmons;
PIMutex mutex;
};
};
inline PICout operator <<(PICout s, const PISystemMonitor::ThreadStats & v) {
@@ -118,4 +138,9 @@ inline PICout operator <<(PICout s, const PISystemMonitor::ThreadStats & v) {
return s;
}
PIByteArray & operator <<(PIByteArray & s, const PISystemMonitor::ProcessStats & v);
PIByteArray & operator >>(PIByteArray & s, PISystemMonitor::ProcessStats & v);
PIByteArray & operator <<(PIByteArray & s, const PISystemMonitor::ThreadStats & v);
PIByteArray & operator >>(PIByteArray & s, PISystemMonitor::ThreadStats & v);
#endif // PISYSTEMMONITOR_H

View File

@@ -309,7 +309,7 @@ bool PIThread::_startThread(void * func) {
//PICout(PICoutManipulators::DefaultControls) << "pthread_create" << PRIVATE->thread;
pthread_attr_destroy(&attr);
if (ret == 0) {
pthread_setname_np(PRIVATE->thread, ((PIString&)name().resize(15, '\0')).dataAscii());
pthread_setname_np(PRIVATE->thread, ((PIString&)name().elided(15, 0.4f).resize(15, '\0')).dataAscii());
# ifdef MAC_OS
pthread_threadid_np(PRIVATE->thread, (__uint64_t*)&tid_);
# else

View File

@@ -889,7 +889,7 @@ void Daemon::makeMyHostInfo() {
void Daemon::makeOtherHostInfo() {
PISystemMonitor::ProcessStats & ps(const_cast<PISystemMonitor::ProcessStats & >(sys_mon_other.statistic()));
PISystemMonitor::ProcessStats ps(sys_mon_other.statistic());
ps.ID = info_other.ID;
ps.threads = info_other.threads;
ps.priority = info_other.priority;