1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
def
get_vm_cpu_rate(
self
, uuid):
"""
get cpu rate
100 * diff_vm_cpu_time / (diff_sys_cpu_time * 1 * 1e9)
return cpurate%
"""
result
=
0
vm_info
=
self
.vms_info.get(uuid,
None
)
vm_info_before
=
self
.vms_info_before.get(uuid,
None
)
if
not
vm_info
or
not
vm_info_before:
return
result
info
=
vm_info.get(
"cpu_mem_state_info"
,
None
)
info_before
=
vm_info_before.get(
"cpu_mem_state_info"
,
None
)
cpu_time
=
info[
-
1
]
cpu_time_before
=
info_before[
-
1
]
last
=
self
.vms_info_timestamp
before
=
self
.vms_info_before_timestamp
if
cpu_time
and
cpu_time_before:
result
=
100
*
abs
(cpu_time_before
-
cpu_time)
/
\
(
abs
(last
-
before)
*
1
*
1e9
)
return
round
(result,
2
)
|
本文转自 swq499809608 51CTO博客,原文链接:http://blog.51cto.com/swq499809608/1404564