winlin

refine the cpu usage calc, add total_delta.

@@ -193,6 +193,7 @@ SrsProcSystemStat::SrsProcSystemStat() @@ -193,6 +193,7 @@ SrsProcSystemStat::SrsProcSystemStat()
193 ok = false; 193 ok = false;
194 sample_time = 0; 194 sample_time = 0;
195 percent = 0; 195 percent = 0;
  196 + total_delta = 0;
196 memset(label, 0, sizeof(label)); 197 memset(label, 0, sizeof(label));
197 user = 0; 198 user = 0;
198 nice = 0; 199 nice = 0;
@@ -205,6 +206,11 @@ SrsProcSystemStat::SrsProcSystemStat() @@ -205,6 +206,11 @@ SrsProcSystemStat::SrsProcSystemStat()
205 guest = 0; 206 guest = 0;
206 } 207 }
207 208
  209 +int64_t SrsProcSystemStat::total()
  210 +{
  211 + return user + nice + sys + idle + iowait + irq + softirq + steal + guest;
  212 +}
  213 +
208 SrsProcSelfStat* srs_get_self_proc_stat() 214 SrsProcSelfStat* srs_get_self_proc_stat()
209 { 215 {
210 return &_srs_system_cpu_self_stat; 216 return &_srs_system_cpu_self_stat;
@@ -307,11 +313,13 @@ void srs_update_proc_stat() @@ -307,11 +313,13 @@ void srs_update_proc_stat()
307 SrsProcSystemStat& o = _srs_system_cpu_system_stat; 313 SrsProcSystemStat& o = _srs_system_cpu_system_stat;
308 314
309 // @see: http://blog.csdn.net/nineday/article/details/1928847 315 // @see: http://blog.csdn.net/nineday/article/details/1928847
310 - int64_t total = (r.user + r.nice + r.sys + r.idle + r.iowait + r.irq + r.softirq + r.steal + r.guest)  
311 - - (o.user + o.nice + o.sys + o.idle + o.iowait + o.irq + o.softirq + o.steal + o.guest);  
312 - int64_t idle = r.idle - o.idle;  
313 - if (total > 0) {  
314 - r.percent = (float)(1 - idle / (double)total); 316 + // @see: http://stackoverflow.com/questions/16011677/calculating-cpu-usage-using-proc-files
  317 + if (o.total() > 0) {
  318 + r.total_delta = r.total() - o.total();
  319 + }
  320 + if (r.total_delta > 0) {
  321 + int64_t idle = r.idle - o.idle;
  322 + r.percent = (float)(1 - idle / (double)r.total_delta);
315 } 323 }
316 324
317 // upate cache. 325 // upate cache.
@@ -238,6 +238,20 @@ public: @@ -238,6 +238,20 @@ public:
238 * = 523331687 * 1/100 (seconds) 238 * = 523331687 * 1/100 (seconds)
239 * = 5233316.87 seconds 239 * = 5233316.87 seconds
240 * the cpu total seconds almost the uptime, the delta is more precise. 240 * the cpu total seconds almost the uptime, the delta is more precise.
  241 +*
  242 +* we run the command about 26minutes:
  243 +* [winlin@SRS ~]$ cat /proc/uptime && cat /proc/stat
  244 +* 5276739.83 4701090.76
  245 +* cpu 43514105 973 8548948 466278556 4150480 190899 804937 0 0
  246 +* where the uptime is 5276739.83s
  247 +* cpu total = 43514105+973+8548948+466278556+4150480+190899+804937+0+0 (USER_HZ)
  248 +* = 523488898 (USER_HZ)
  249 +* = 523488898 * 1/100 (seconds)
  250 +* = 5234888.98 seconds
  251 +* where:
  252 +* uptime delta = 1586.82s
  253 +* cpu total delta = 1572.11s
  254 +* the deviation is more smaller.
241 */ 255 */
242 class SrsProcSystemStat 256 class SrsProcSystemStat
243 { 257 {
@@ -248,10 +262,16 @@ public: @@ -248,10 +262,16 @@ public:
248 int64_t sample_time; 262 int64_t sample_time;
249 // the percent of usage. 0.153 is 15.3%. 263 // the percent of usage. 0.153 is 15.3%.
250 float percent; 264 float percent;
  265 + // the total cpu time units
  266 + // @remark, zero for the previous total() is zero.
  267 + // the usaged_cpu_delta = total_delta * percent
  268 + // previous cpu total = this->total() - total_delta
  269 + int64_t total_delta;
251 270
252 // always be cpu 271 // always be cpu
253 char label[32]; 272 char label[32];
254 273
  274 +public:
255 // The amount of time, measured in units of USER_HZ 275 // The amount of time, measured in units of USER_HZ
256 // (1/100ths of a second on most architectures, use 276 // (1/100ths of a second on most architectures, use
257 // sysconf(_SC_CLK_TCK) to obtain the right value) 277 // sysconf(_SC_CLK_TCK) to obtain the right value)
@@ -285,6 +305,9 @@ public: @@ -285,6 +305,9 @@ public:
285 unsigned long guest; 305 unsigned long guest;
286 306
287 SrsProcSystemStat(); 307 SrsProcSystemStat();
  308 +
  309 + // get total cpu units.
  310 + int64_t total();
288 }; 311 };
289 312
290 // get system cpu stat, use cache to avoid performance problem. 313 // get system cpu stat, use cache to avoid performance problem.