Skip to content

Commit 856e0a1

Browse files
committed
Don't require Xen to get VM stats
It's not completely Xen agnostic because then it would not be possible to query stubdomains, as libvirt is not aware of them. On the other hand, non-Xen has one more API method working. When not using Xen, construct the dictionary as "xc.domain_getinfo()" would, so the info loop can consume from both inputs.
1 parent bb50910 commit 856e0a1

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

qubes/app.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,44 @@ def get_vm_stats(self, previous_time=None, previous=None, only_vm=None):
417417

418418
current_time = time.time()
419419
current = {}
420-
if not self.app.vmm.is_xen:
421-
raise NotImplementedError("This function requires Xen hypervisor")
422420
if only_vm:
423421
xid = only_vm.xid
424422
if xid < 0:
425423
raise qubes.exc.QubesVMNotRunningError(only_vm)
426-
info = self.app.vmm.xc.domain_getinfo(xid, 1)
427-
if info[0]["domid"] != xid:
428-
raise qubes.exc.QubesVMNotRunningError(only_vm)
424+
if self.app.vmm.is_xen:
425+
info = self.app.vmm.xc.domain_getinfo(xid, 1)
426+
if info[0]["domid"] != xid:
427+
raise qubes.exc.QubesVMNotRunningError(only_vm)
428+
else:
429+
if not only_vm.libvirt_domain:
430+
raise qubes.exc.QubesVMNotRunningError(only_vm)
431+
dom_info = only_vm.libvirt_domain.info()
432+
info = [
433+
{
434+
"domid": only_vm.xid,
435+
"mem_kb": dom_info[2],
436+
"online_vcpus": dom_info[3],
437+
"cpu_time": dom_info[4],
438+
}
439+
]
429440
else:
430-
info = self.app.vmm.xc.domain_getinfo(0, 1024)
441+
if self.app.vmm.is_xen:
442+
info = self.app.vmm.xc.domain_getinfo(0, 1024)
443+
else:
444+
running = self.app.vmm.libvirt_conn.listAllDomains(
445+
libvirt.VIR_CONNECT_GET_ALL_DOMAINS_STATS_ACTIVE
446+
)
447+
info = []
448+
for dom in running:
449+
dom_info = dom.info()
450+
dom_info_dict = {
451+
"domid": dom.ID(),
452+
"mem_kb": dom_info[2],
453+
"online_vcpus": dom_info[3],
454+
"cpu_time": dom_info[4],
455+
}
456+
info.append(dom_info_dict)
457+
431458
# TODO: add stubdomain stats to actual VMs
432459
for vm in info:
433460
domid = vm["domid"]

0 commit comments

Comments
 (0)