mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
get self proc and system proc stat
This commit is contained in:
parent
117fd67950
commit
951a93ab86
5 changed files with 156 additions and 20 deletions
|
@ -118,6 +118,8 @@ SrsApiV1::SrsApiV1()
|
||||||
handlers.push_back(new SrsApiVersion());
|
handlers.push_back(new SrsApiVersion());
|
||||||
handlers.push_back(new SrsApiSummaries());
|
handlers.push_back(new SrsApiSummaries());
|
||||||
handlers.push_back(new SrsApiRusages());
|
handlers.push_back(new SrsApiRusages());
|
||||||
|
handlers.push_back(new SrsApiSelfProcStats());
|
||||||
|
handlers.push_back(new SrsApiSystemProcStats());
|
||||||
handlers.push_back(new SrsApiAuthors());
|
handlers.push_back(new SrsApiAuthors());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,7 +142,8 @@ int SrsApiV1::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
|
||||||
<< JFIELD_STR("versions", "the version of SRS") << JFIELD_CONT
|
<< JFIELD_STR("versions", "the version of SRS") << JFIELD_CONT
|
||||||
<< JFIELD_STR("summaries", "the summary(pid, argv, pwd, cpu, mem) of SRS") << JFIELD_CONT
|
<< JFIELD_STR("summaries", "the summary(pid, argv, pwd, cpu, mem) of SRS") << JFIELD_CONT
|
||||||
<< JFIELD_STR("rusages", "the rusage of SRS") << JFIELD_CONT
|
<< JFIELD_STR("rusages", "the rusage of SRS") << JFIELD_CONT
|
||||||
<< JFIELD_STR("proc_stats", "the /proc/self/stat of SRS") << JFIELD_CONT
|
<< JFIELD_STR("self_proc_stats", "the self process stats") << JFIELD_CONT
|
||||||
|
<< JFIELD_STR("system_proc_stats", "the system process stats") << JFIELD_CONT
|
||||||
<< JFIELD_STR("authors", "the primary authors and contributors")
|
<< JFIELD_STR("authors", "the primary authors and contributors")
|
||||||
<< JOBJECT_END
|
<< JOBJECT_END
|
||||||
<< JOBJECT_END;
|
<< JOBJECT_END;
|
||||||
|
@ -196,8 +199,8 @@ int SrsApiSummaries::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
SrsRusage* r = srs_get_system_rusage();
|
SrsRusage* r = srs_get_system_rusage();
|
||||||
SrsCpuSelfStat* u = srs_get_self_cpu_stat();
|
SrsProcSelfStat* u = srs_get_self_proc_stat();
|
||||||
SrsCpuSystemStat* s = srs_get_system_cpu_stat();
|
SrsProcSystemStat* s = srs_get_system_proc_stat();
|
||||||
|
|
||||||
ss << JOBJECT_START
|
ss << JOBJECT_START
|
||||||
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
|
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
|
||||||
|
@ -261,6 +264,117 @@ int SrsApiRusages::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
|
||||||
return res_json(skt, req, ss.str());
|
return res_json(skt, req, ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SrsApiSelfProcStats::SrsApiSelfProcStats()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsApiSelfProcStats::~SrsApiSelfProcStats()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SrsApiSelfProcStats::can_handle(const char* path, int length, const char** /*pchild*/)
|
||||||
|
{
|
||||||
|
return srs_path_equals("/self_proc_stats", path, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsApiSelfProcStats::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
|
||||||
|
SrsProcSelfStat* u = srs_get_self_proc_stat();
|
||||||
|
|
||||||
|
ss << JOBJECT_START
|
||||||
|
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("data", JOBJECT_START)
|
||||||
|
<< JFIELD_ORG("self_cpu_stat_ok", (u->ok? "true":"false")) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("pid", u->pid) << JFIELD_CONT
|
||||||
|
<< JFIELD_STR("comm", u->comm) << JFIELD_CONT
|
||||||
|
<< JFIELD_STR("state", u->state) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("ppid", u->ppid) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("pgrp", u->pgrp) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("session", u->session) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("tty_nr", u->tty_nr) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("tpgid", u->tpgid) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("flags", u->flags) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("minflt", u->minflt) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("cminflt", u->cminflt) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("majflt", u->majflt) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("cmajflt", u->cmajflt) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("utime", u->utime) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("stime", u->stime) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("cutime", u->cutime) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("cstime", u->cstime) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("priority", u->priority) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("nice", u->nice) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("num_threads", u->num_threads) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("itrealvalue", u->itrealvalue) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("starttime", u->starttime) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("vsize", u->vsize) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("rss", u->rss) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("rsslim", u->rsslim) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("startcode", u->startcode) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("endcode", u->endcode) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("startstack", u->startstack) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("kstkesp", u->kstkesp) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("kstkeip", u->kstkeip) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("signal", u->signal) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("blocked", u->blocked) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("sigignore", u->sigignore) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("sigcatch", u->sigcatch) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("wchan", u->wchan) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("nswap", u->nswap) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("cnswap", u->cnswap) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("exit_signal", u->exit_signal) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("processor", u->processor) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("rt_priority", u->rt_priority) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("policy", u->policy) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("delayacct_blkio_ticks", u->delayacct_blkio_ticks) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("guest_time", u->guest_time) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("cguest_time", u->cguest_time)
|
||||||
|
<< JOBJECT_END
|
||||||
|
<< JOBJECT_END;
|
||||||
|
|
||||||
|
return res_json(skt, req, ss.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsApiSystemProcStats::SrsApiSystemProcStats()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsApiSystemProcStats::~SrsApiSystemProcStats()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SrsApiSystemProcStats::can_handle(const char* path, int length, const char** /*pchild*/)
|
||||||
|
{
|
||||||
|
return srs_path_equals("/system_proc_stats", path, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsApiSystemProcStats::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
|
||||||
|
SrsProcSystemStat* s = srs_get_system_proc_stat();
|
||||||
|
|
||||||
|
ss << JOBJECT_START
|
||||||
|
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("data", JOBJECT_START)
|
||||||
|
<< JFIELD_ORG("system_cpu_stat_ok", (s->ok? "true":"false")) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("user", s->user) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("nice", s->nice) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("sys", s->sys) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("idle", s->idle) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("iowait", s->iowait) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("irq", s->irq) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("softirq", s->softirq) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("steal", s->steal) << JFIELD_CONT
|
||||||
|
<< JFIELD_ORG("guest", s->guest)
|
||||||
|
<< JOBJECT_END
|
||||||
|
<< JOBJECT_END;
|
||||||
|
|
||||||
|
return res_json(skt, req, ss.str());
|
||||||
|
}
|
||||||
|
|
||||||
SrsApiAuthors::SrsApiAuthors()
|
SrsApiAuthors::SrsApiAuthors()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,6 +109,28 @@ protected:
|
||||||
virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req);
|
virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SrsApiSelfProcStats : public SrsHttpHandler
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SrsApiSelfProcStats();
|
||||||
|
virtual ~SrsApiSelfProcStats();
|
||||||
|
public:
|
||||||
|
virtual bool can_handle(const char* path, int length, const char** pchild);
|
||||||
|
protected:
|
||||||
|
virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req);
|
||||||
|
};
|
||||||
|
|
||||||
|
class SrsApiSystemProcStats : public SrsHttpHandler
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SrsApiSystemProcStats();
|
||||||
|
virtual ~SrsApiSystemProcStats();
|
||||||
|
public:
|
||||||
|
virtual bool can_handle(const char* path, int length, const char** pchild);
|
||||||
|
protected:
|
||||||
|
virtual int do_process_request(SrsSocket* skt, SrsHttpMessage* req);
|
||||||
|
};
|
||||||
|
|
||||||
class SrsApiAuthors : public SrsHttpHandler
|
class SrsApiAuthors : public SrsHttpHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -447,7 +447,7 @@ int SrsServer::cycle()
|
||||||
srs_update_system_rusage();
|
srs_update_system_rusage();
|
||||||
}
|
}
|
||||||
if (i == SRS_SYS_CPU_STAT_RESOLUTION_TIMES) {
|
if (i == SRS_SYS_CPU_STAT_RESOLUTION_TIMES) {
|
||||||
srs_update_system_cpu_stat();
|
srs_update_proc_stat();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,30 +81,30 @@ void srs_update_system_rusage()
|
||||||
_srs_system_rusage.ok = true;
|
_srs_system_rusage.ok = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SrsCpuSelfStat _srs_system_cpu_self_stat;
|
static SrsProcSelfStat _srs_system_cpu_self_stat;
|
||||||
static SrsCpuSystemStat _srs_system_cpu_system_stat;
|
static SrsProcSystemStat _srs_system_cpu_system_stat;
|
||||||
|
|
||||||
SrsCpuSelfStat::SrsCpuSelfStat()
|
SrsProcSelfStat::SrsProcSelfStat()
|
||||||
{
|
{
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsCpuSystemStat::SrsCpuSystemStat()
|
SrsProcSystemStat::SrsProcSystemStat()
|
||||||
{
|
{
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsCpuSelfStat* srs_get_self_cpu_stat()
|
SrsProcSelfStat* srs_get_self_proc_stat()
|
||||||
{
|
{
|
||||||
return &_srs_system_cpu_self_stat;
|
return &_srs_system_cpu_self_stat;
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsCpuSystemStat* srs_get_system_cpu_stat()
|
SrsProcSystemStat* srs_get_system_proc_stat()
|
||||||
{
|
{
|
||||||
return &_srs_system_cpu_system_stat;
|
return &_srs_system_cpu_system_stat;
|
||||||
}
|
}
|
||||||
|
|
||||||
void srs_update_system_cpu_stat()
|
void srs_update_proc_stat()
|
||||||
{
|
{
|
||||||
// system cpu stat
|
// system cpu stat
|
||||||
if (true) {
|
if (true) {
|
||||||
|
@ -114,7 +114,7 @@ void srs_update_system_cpu_stat()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsCpuSystemStat& r = _srs_system_cpu_system_stat;
|
SrsProcSystemStat& r = _srs_system_cpu_system_stat;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int ret = fscanf(f, "%4s %lu %lu %lu %lu %lu "
|
int ret = fscanf(f, "%4s %lu %lu %lu %lu %lu "
|
||||||
"%lu %lu %lu %lu\n",
|
"%lu %lu %lu %lu\n",
|
||||||
|
@ -143,7 +143,7 @@ void srs_update_system_cpu_stat()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsCpuSelfStat& r = _srs_system_cpu_self_stat;
|
SrsProcSelfStat& r = _srs_system_cpu_self_stat;
|
||||||
int ret = fscanf(f, "%d %32s %c %d %d %d %d "
|
int ret = fscanf(f, "%d %32s %c %d %d %d %d "
|
||||||
"%d %u %lu %lu %lu %lu "
|
"%d %u %lu %lu %lu %lu "
|
||||||
"%lu %lu %ld %ld %ld %ld "
|
"%lu %lu %ld %ld %ld %ld "
|
||||||
|
|
|
@ -52,7 +52,7 @@ extern SrsRusage* srs_get_system_rusage();
|
||||||
extern void srs_update_system_rusage();
|
extern void srs_update_system_rusage();
|
||||||
|
|
||||||
// @see: man 5 proc, /proc/[pid]/stat
|
// @see: man 5 proc, /proc/[pid]/stat
|
||||||
struct SrsCpuSelfStat
|
struct SrsProcSelfStat
|
||||||
{
|
{
|
||||||
// whether the data is ok.
|
// whether the data is ok.
|
||||||
bool ok;
|
bool ok;
|
||||||
|
@ -195,11 +195,11 @@ struct SrsCpuSelfStat
|
||||||
// Guest time of the process’s children, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).
|
// Guest time of the process’s children, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).
|
||||||
long cguest_time;
|
long cguest_time;
|
||||||
|
|
||||||
SrsCpuSelfStat();
|
SrsProcSelfStat();
|
||||||
};
|
};
|
||||||
|
|
||||||
// @see: man 5 proc, /proc/stat
|
// @see: man 5 proc, /proc/stat
|
||||||
struct SrsCpuSystemStat
|
struct SrsProcSystemStat
|
||||||
{
|
{
|
||||||
// whether the data is ok.
|
// whether the data is ok.
|
||||||
bool ok;
|
bool ok;
|
||||||
|
@ -238,14 +238,14 @@ struct SrsCpuSystemStat
|
||||||
// operating systems under the control of the Linux kernel.
|
// operating systems under the control of the Linux kernel.
|
||||||
unsigned long guest;
|
unsigned long guest;
|
||||||
|
|
||||||
SrsCpuSystemStat();
|
SrsProcSystemStat();
|
||||||
};
|
};
|
||||||
|
|
||||||
// get system cpu stat, use cache to avoid performance problem.
|
// get system cpu stat, use cache to avoid performance problem.
|
||||||
extern SrsCpuSelfStat* srs_get_self_cpu_stat();
|
extern SrsProcSelfStat* srs_get_self_proc_stat();
|
||||||
// get system cpu stat, use cache to avoid performance problem.
|
// get system cpu stat, use cache to avoid performance problem.
|
||||||
extern SrsCpuSystemStat* srs_get_system_cpu_stat();
|
extern SrsProcSystemStat* srs_get_system_proc_stat();
|
||||||
// the deamon st-thread will update it.
|
// the deamon st-thread will update it.
|
||||||
extern void srs_update_system_cpu_stat();
|
extern void srs_update_proc_stat();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue