mirror of
https://github.com/ossrs/srs.git
synced 2025-02-13 03:41:55 +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.
|
||||
|
|
|
@ -238,6 +238,20 @@ public:
|
|||
* = 523331687 * 1/100 (seconds)
|
||||
* = 5233316.87 seconds
|
||||
* the cpu total seconds almost the uptime, the delta is more precise.
|
||||
*
|
||||
* we run the command about 26minutes:
|
||||
* [winlin@SRS ~]$ cat /proc/uptime && cat /proc/stat
|
||||
* 5276739.83 4701090.76
|
||||
* cpu 43514105 973 8548948 466278556 4150480 190899 804937 0 0
|
||||
* where the uptime is 5276739.83s
|
||||
* cpu total = 43514105+973+8548948+466278556+4150480+190899+804937+0+0 (USER_HZ)
|
||||
* = 523488898 (USER_HZ)
|
||||
* = 523488898 * 1/100 (seconds)
|
||||
* = 5234888.98 seconds
|
||||
* where:
|
||||
* uptime delta = 1586.82s
|
||||
* cpu total delta = 1572.11s
|
||||
* the deviation is more smaller.
|
||||
*/
|
||||
class SrsProcSystemStat
|
||||
{
|
||||
|
@ -248,10 +262,16 @@ public:
|
|||
int64_t sample_time;
|
||||
// the percent of usage. 0.153 is 15.3%.
|
||||
float percent;
|
||||
// the total cpu time units
|
||||
// @remark, zero for the previous total() is zero.
|
||||
// the usaged_cpu_delta = total_delta * percent
|
||||
// previous cpu total = this->total() - total_delta
|
||||
int64_t total_delta;
|
||||
|
||||
// always be cpu
|
||||
char label[32];
|
||||
|
||||
public:
|
||||
// The amount of time, measured in units of USER_HZ
|
||||
// (1/100ths of a second on most architectures, use
|
||||
// sysconf(_SC_CLK_TCK) to obtain the right value)
|
||||
|
@ -285,6 +305,9 @@ public:
|
|||
unsigned long guest;
|
||||
|
||||
SrsProcSystemStat();
|
||||
|
||||
// get total cpu units.
|
||||
int64_t total();
|
||||
};
|
||||
|
||||
// get system cpu stat, use cache to avoid performance problem.
|
||||
|
|
Loading…
Reference in a new issue