Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
胡斌
/
srs
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
winlin
2014-07-27 12:47:31 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
d2f125b6d6a4943093237e065ffd0f36f663bf16
d2f125b6
1 parent
d0bc0884
refine the cpu usage calc, add total_delta.
隐藏空白字符变更
内嵌
并排对比
正在显示
2 个修改的文件
包含
36 行增加
和
5 行删除
trunk/src/app/srs_app_utility.cpp
trunk/src/app/srs_app_utility.hpp
trunk/src/app/srs_app_utility.cpp
查看文件 @
d2f125b
...
...
@@ -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.
...
...
trunk/src/app/srs_app_utility.hpp
查看文件 @
d2f125b
...
...
@@ -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.
...
...
请
注册
或
登录
后发表评论