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;
|
ok = false;
|
||||||
sample_time = 0;
|
sample_time = 0;
|
||||||
percent = 0;
|
percent = 0;
|
||||||
|
total_delta = 0;
|
||||||
memset(label, 0, sizeof(label));
|
memset(label, 0, sizeof(label));
|
||||||
user = 0;
|
user = 0;
|
||||||
nice = 0;
|
nice = 0;
|
||||||
|
@ -205,6 +206,11 @@ SrsProcSystemStat::SrsProcSystemStat()
|
||||||
guest = 0;
|
guest = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int64_t SrsProcSystemStat::total()
|
||||||
|
{
|
||||||
|
return user + nice + sys + idle + iowait + irq + softirq + steal + guest;
|
||||||
|
}
|
||||||
|
|
||||||
SrsProcSelfStat* srs_get_self_proc_stat()
|
SrsProcSelfStat* srs_get_self_proc_stat()
|
||||||
{
|
{
|
||||||
return &_srs_system_cpu_self_stat;
|
return &_srs_system_cpu_self_stat;
|
||||||
|
@ -307,11 +313,13 @@ void srs_update_proc_stat()
|
||||||
SrsProcSystemStat& o = _srs_system_cpu_system_stat;
|
SrsProcSystemStat& o = _srs_system_cpu_system_stat;
|
||||||
|
|
||||||
// @see: http://blog.csdn.net/nineday/article/details/1928847
|
// @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)
|
// @see: http://stackoverflow.com/questions/16011677/calculating-cpu-usage-using-proc-files
|
||||||
- (o.user + o.nice + o.sys + o.idle + o.iowait + o.irq + o.softirq + o.steal + o.guest);
|
if (o.total() > 0) {
|
||||||
int64_t idle = r.idle - o.idle;
|
r.total_delta = r.total() - o.total();
|
||||||
if (total > 0) {
|
}
|
||||||
r.percent = (float)(1 - idle / (double)total);
|
if (r.total_delta > 0) {
|
||||||
|
int64_t idle = r.idle - o.idle;
|
||||||
|
r.percent = (float)(1 - idle / (double)r.total_delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
// upate cache.
|
// upate cache.
|
||||||
|
|
|
@ -238,6 +238,20 @@ public:
|
||||||
* = 523331687 * 1/100 (seconds)
|
* = 523331687 * 1/100 (seconds)
|
||||||
* = 5233316.87 seconds
|
* = 5233316.87 seconds
|
||||||
* the cpu total seconds almost the uptime, the delta is more precise.
|
* 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
|
class SrsProcSystemStat
|
||||||
{
|
{
|
||||||
|
@ -248,10 +262,16 @@ public:
|
||||||
int64_t sample_time;
|
int64_t sample_time;
|
||||||
// the percent of usage. 0.153 is 15.3%.
|
// the percent of usage. 0.153 is 15.3%.
|
||||||
float percent;
|
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
|
// always be cpu
|
||||||
char label[32];
|
char label[32];
|
||||||
|
|
||||||
|
public:
|
||||||
// The amount of time, measured in units of USER_HZ
|
// The amount of time, measured in units of USER_HZ
|
||||||
// (1/100ths of a second on most architectures, use
|
// (1/100ths of a second on most architectures, use
|
||||||
// sysconf(_SC_CLK_TCK) to obtain the right value)
|
// sysconf(_SC_CLK_TCK) to obtain the right value)
|
||||||
|
@ -285,6 +305,9 @@ public:
|
||||||
unsigned long guest;
|
unsigned long guest;
|
||||||
|
|
||||||
SrsProcSystemStat();
|
SrsProcSystemStat();
|
||||||
|
|
||||||
|
// get total cpu units.
|
||||||
|
int64_t total();
|
||||||
};
|
};
|
||||||
|
|
||||||
// get system cpu stat, use cache to avoid performance problem.
|
// get system cpu stat, use cache to avoid performance problem.
|
||||||
|
|
Loading…
Reference in a new issue