mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
refine the cpu usage calc, add total_delta.
This commit is contained in:
parent
d0bc0884d1
commit
d2f125b6d6
2 changed files with 36 additions and 5 deletions
|
|
@ -193,6 +193,7 @@ SrsProcSystemStat::SrsProcSystemStat()
|
|||
ok = false;
|
||||
sample_time = 0;
|
||||
percent = 0;
|
||||
total_delta = 0;
|
||||
memset(label, 0, sizeof(label));
|
||||
user = 0;
|
||||
nice = 0;
|
||||
|
|
@ -205,6 +206,11 @@ SrsProcSystemStat::SrsProcSystemStat()
|
|||
guest = 0;
|
||||
}
|
||||
|
||||
int64_t SrsProcSystemStat::total()
|
||||
{
|
||||
return user + nice + sys + idle + iowait + irq + softirq + steal + guest;
|
||||
}
|
||||
|
||||
SrsProcSelfStat* srs_get_self_proc_stat()
|
||||
{
|
||||
return &_srs_system_cpu_self_stat;
|
||||
|
|
@ -307,11 +313,13 @@ void srs_update_proc_stat()
|
|||
SrsProcSystemStat& o = _srs_system_cpu_system_stat;
|
||||
|
||||
// @see: http://blog.csdn.net/nineday/article/details/1928847
|
||||
int64_t total = (r.user + r.nice + r.sys + r.idle + r.iowait + r.irq + r.softirq + r.steal + r.guest)
|
||||
- (o.user + o.nice + o.sys + o.idle + o.iowait + o.irq + o.softirq + o.steal + o.guest);
|
||||
int64_t idle = r.idle - o.idle;
|
||||
if (total > 0) {
|
||||
r.percent = (float)(1 - idle / (double)total);
|
||||
// @see: http://stackoverflow.com/questions/16011677/calculating-cpu-usage-using-proc-files
|
||||
if (o.total() > 0) {
|
||||
r.total_delta = r.total() - o.total();
|
||||
}
|
||||
if (r.total_delta > 0) {
|
||||
int64_t idle = r.idle - o.idle;
|
||||
r.percent = (float)(1 - idle / (double)r.total_delta);
|
||||
}
|
||||
|
||||
// upate cache.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue