Skip to content

Commit 798f3be

Browse files
committed
vm stats: nearest int instead of truncation
1 parent 8ad9351 commit 798f3be

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

qubes/app.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def get_vm_stats(self, previous_time=None, previous=None, only_vm=None):
354354
"""Measure cpu usage for all domains at once.
355355
356356
If previous measurements are given, CPU usage will be given in
357-
percents of time. Otherwise only absolute value (seconds).
357+
percents of time. Otherwise, only absolute value (seconds).
358358
359359
Return a tuple of (measurements_time, measurements),
360360
where measurements is a dictionary with key: domid, value: dict:
@@ -404,19 +404,21 @@ def get_vm_stats(self, previous_time=None, previous=None, only_vm=None):
404404
domid = vm['domid']
405405
current[domid] = {}
406406
current[domid]['memory_kb'] = vm['mem_kb']
407-
current[domid]['cpu_time'] = int(vm['cpu_time'])
407+
current[domid]['cpu_time'] = round(vm['cpu_time'])
408408
vcpus = max(vm['online_vcpus'], 1)
409409
if domid in previous:
410-
current[domid]['cpu_usage_raw'] = int(
410+
current[domid]['cpu_usage_raw'] = round(
411411
(current[domid]['cpu_time'] - previous[domid]['cpu_time'])
412-
/ 1000 ** 3 * 100 / (current_time - previous_time))
412+
/ 1000 ** 3 * 100 / (current_time - previous_time)
413+
)
413414
if current[domid]['cpu_usage_raw'] < 0:
414415
# VM has been rebooted
415416
current[domid]['cpu_usage_raw'] = 0
416417
else:
417418
current[domid]['cpu_usage_raw'] = 0
418-
current[domid]['cpu_usage'] = \
419-
int(current[domid]['cpu_usage_raw'] / vcpus)
419+
current[domid]['cpu_usage'] = round(
420+
current[domid]['cpu_usage_raw'] / vcpus
421+
)
420422

421423
return current_time, current
422424

qubes/tests/app.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,20 @@ def test_001_get_vm_stats_twice(self):
119119
expected_info = {
120120
0: {
121121
'cpu_time': 243951379111104,
122-
'cpu_usage': 9,
123-
'cpu_usage_raw': 79,
122+
'cpu_usage': 10,
123+
'cpu_usage_raw': 80,
124124
'memory_kb': 3733212,
125125
},
126126
1: {
127127
'cpu_time': 2849496569205,
128-
'cpu_usage': 99,
129-
'cpu_usage_raw': 99,
128+
'cpu_usage': 100,
129+
'cpu_usage_raw': 100,
130130
'memory_kb': 303916,
131131
},
132132
11: {
133133
'cpu_time': 249658663079978,
134134
'cpu_usage': 12,
135-
'cpu_usage_raw': 99,
135+
'cpu_usage_raw': 100,
136136
'memory_kb': 3782668,
137137
},
138138
}

0 commit comments

Comments
 (0)