1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

add cpu stat

This commit is contained in:
winlin 2014-04-19 21:23:34 +08:00
parent 4c5aae7804
commit 117fd67950
7 changed files with 510 additions and 14 deletions

View file

@ -57,3 +57,116 @@ void srs_update_system_time_ms()
_srs_system_time_us_cache = now_us;
}
static SrsRusage _srs_system_rusage;
SrsRusage::SrsRusage()
{
ok = false;
memset(&r, 0, sizeof(rusage));
}
SrsRusage* srs_get_system_rusage()
{
return &_srs_system_rusage;
}
void srs_update_system_rusage()
{
if (getrusage(RUSAGE_SELF, &_srs_system_rusage.r) < 0) {
srs_warn("getrusage failed, ignore");
return;
}
_srs_system_rusage.ok = true;
}
static SrsCpuSelfStat _srs_system_cpu_self_stat;
static SrsCpuSystemStat _srs_system_cpu_system_stat;
SrsCpuSelfStat::SrsCpuSelfStat()
{
ok = false;
}
SrsCpuSystemStat::SrsCpuSystemStat()
{
ok = false;
}
SrsCpuSelfStat* srs_get_self_cpu_stat()
{
return &_srs_system_cpu_self_stat;
}
SrsCpuSystemStat* srs_get_system_cpu_stat()
{
return &_srs_system_cpu_system_stat;
}
void srs_update_system_cpu_stat()
{
// system cpu stat
if (true) {
FILE* f = fopen("/proc/stat", "r");
if (f == NULL) {
srs_warn("open system cpu stat failed, ignore");
return;
}
SrsCpuSystemStat& r = _srs_system_cpu_system_stat;
for (;;) {
int ret = fscanf(f, "%4s %lu %lu %lu %lu %lu "
"%lu %lu %lu %lu\n",
r.label, &r.user, &r.nice, &r.sys, &r.idle, &r.iowait,
&r.irq, &r.softirq, &r.steal, &r.guest);
r.ok = false;
if (ret == EOF) {
break;
}
if (strcmp("cpu", r.label) == 0) {
r.ok = true;
break;
}
}
fclose(f);
}
// self cpu stat
if (true) {
FILE* f = fopen("/proc/self/stat", "r");
if (f == NULL) {
srs_warn("open self cpu stat failed, ignore");
return;
}
SrsCpuSelfStat& r = _srs_system_cpu_self_stat;
int ret = fscanf(f, "%d %32s %c %d %d %d %d "
"%d %u %lu %lu %lu %lu "
"%lu %lu %ld %ld %ld %ld "
"%ld %ld %llu %lu %ld "
"%lu %lu %lu %lu %lu "
"%lu %lu %lu %lu %lu "
"%lu %lu %lu %d %d "
"%u %u %llu "
"%lu %ld",
&r.pid, r.comm, &r.state, &r.ppid, &r.pgrp, &r.session, &r.tty_nr,
&r.tpgid, &r.flags, &r.minflt, &r.cminflt, &r.majflt, &r.cmajflt,
&r.utime, &r.stime, &r.cutime, &r.cstime, &r.priority, &r.nice,
&r.num_threads, &r.itrealvalue, &r.starttime, &r.vsize, &r.rss,
&r.rsslim, &r.startcode, &r.endcode, &r.startstack, &r.kstkesp,
&r.kstkeip, &r.signal, &r.blocked, &r.sigignore, &r.sigcatch,
&r.wchan, &r.nswap, &r.cnswap, &r.exit_signal, &r.processor,
&r.rt_priority, &r.policy, &r.delayacct_blkio_ticks,
&r.guest_time, &r.cguest_time);
if (ret >= 0) {
r.ok = true;
}
fclose(f);
}
}