Skip to content

Commit

Permalink
vm stats: nearest int instead of truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
fepitre committed Nov 11, 2024
1 parent 8ad9351 commit 798f3be
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
14 changes: 8 additions & 6 deletions qubes/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def get_vm_stats(self, previous_time=None, previous=None, only_vm=None):
"""Measure cpu usage for all domains at once.
If previous measurements are given, CPU usage will be given in
percents of time. Otherwise only absolute value (seconds).
percents of time. Otherwise, only absolute value (seconds).
Return a tuple of (measurements_time, measurements),
where measurements is a dictionary with key: domid, value: dict:
Expand Down Expand Up @@ -404,19 +404,21 @@ def get_vm_stats(self, previous_time=None, previous=None, only_vm=None):
domid = vm['domid']
current[domid] = {}
current[domid]['memory_kb'] = vm['mem_kb']
current[domid]['cpu_time'] = int(vm['cpu_time'])
current[domid]['cpu_time'] = round(vm['cpu_time'])
vcpus = max(vm['online_vcpus'], 1)
if domid in previous:
current[domid]['cpu_usage_raw'] = int(
current[domid]['cpu_usage_raw'] = round(
(current[domid]['cpu_time'] - previous[domid]['cpu_time'])
/ 1000 ** 3 * 100 / (current_time - previous_time))
/ 1000 ** 3 * 100 / (current_time - previous_time)
)
if current[domid]['cpu_usage_raw'] < 0:
# VM has been rebooted
current[domid]['cpu_usage_raw'] = 0
else:
current[domid]['cpu_usage_raw'] = 0
current[domid]['cpu_usage'] = \
int(current[domid]['cpu_usage_raw'] / vcpus)
current[domid]['cpu_usage'] = round(
current[domid]['cpu_usage_raw'] / vcpus
)

return current_time, current

Expand Down
10 changes: 5 additions & 5 deletions qubes/tests/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,20 @@ def test_001_get_vm_stats_twice(self):
expected_info = {
0: {
'cpu_time': 243951379111104,
'cpu_usage': 9,
'cpu_usage_raw': 79,
'cpu_usage': 10,
'cpu_usage_raw': 80,
'memory_kb': 3733212,
},
1: {
'cpu_time': 2849496569205,
'cpu_usage': 99,
'cpu_usage_raw': 99,
'cpu_usage': 100,
'cpu_usage_raw': 100,
'memory_kb': 303916,
},
11: {
'cpu_time': 249658663079978,
'cpu_usage': 12,
'cpu_usage_raw': 99,
'cpu_usage_raw': 100,
'memory_kb': 3782668,
},
}
Expand Down

0 comments on commit 798f3be

Please sign in to comment.