core: convert memory/cpu stats to libvirt API

This commit is contained in:
Marek Marczykowski-Górecki 2015-02-09 03:28:01 +01:00
parent f9b2636c73
commit 869675c15c
2 changed files with 37 additions and 25 deletions

View File

@ -712,6 +712,18 @@ class QubesVm(object):
return 0
return self.libvirt_domain.info()[1]
def get_cputime(self):
if dry_run:
return 666
if self.libvirt_domain is None:
return 0
if not self.libvirt_domain.isActive():
return 0
return self.libvirt_domain.info()[4]
def get_mem_static_max(self):
if dry_run:
return 666
@ -723,7 +735,8 @@ class QubesVm(object):
def get_prefmem(self):
# TODO: qmemman is still xen specific
untrusted_meminfo_key = xs.read('', '/local/domain/%s/memory/meminfo'
untrusted_meminfo_key = vmm.xs.read('',
'/local/domain/%s/memory/meminfo'
% self.xid)
if untrusted_meminfo_key is None or untrusted_meminfo_key == '':
return 0

View File

@ -211,42 +211,41 @@ class QubesHost(object):
return self._no_cpus
# TODO
def get_free_xen_memory(self):
ret = self.physinfo['free_memory']
return long(ret)
# TODO
def measure_cpu_usage(self, previous=None, previous_time = None,
def measure_cpu_usage(self, qvmc, previous=None, previous_time = None,
wait_time=1):
"""measure cpu usage for all domains at once"""
if previous is None:
previous_time = time.time()
previous = {}
info = vmm.xc.domain_getinfo(0, qubes_max_qid)
for vm in info:
previous[vm['domid']] = {}
previous[vm['domid']]['cpu_time'] = (
vm['cpu_time'] / vm['online_vcpus'])
previous[vm['domid']]['cpu_usage'] = 0
for vm in qvmc.values():
if not vm.is_running():
continue
cputime = vm.get_cputime()
previous[vm.xid] = {}
previous[vm.xid]['cpu_time'] = (
cputime / vm.vcpus)
previous[vm.xid]['cpu_usage'] = 0
time.sleep(wait_time)
current_time = time.time()
current = {}
info = vmm.xc.domain_getinfo(0, qubes_max_qid)
for vm in info:
current[vm['domid']] = {}
current[vm['domid']]['cpu_time'] = (
vm['cpu_time'] / max(vm['online_vcpus'], 1))
if vm['domid'] in previous.keys():
current[vm['domid']]['cpu_usage'] = (
float(current[vm['domid']]['cpu_time'] -
previous[vm['domid']]['cpu_time']) /
for vm in qvmc.values():
if not vm.is_running():
continue
cputime = vm.get_cputime()
current[vm.xid] = {}
current[vm.xid]['cpu_time'] = (
cputime / max(vm.vcpus, 1))
if vm.xid in previous.keys():
current[vm.xid]['cpu_usage'] = (
float(current[vm.xid]['cpu_time'] -
previous[vm.xid]['cpu_time']) /
long(1000**3) / (current_time-previous_time) * 100)
if current[vm['domid']]['cpu_usage'] < 0:
if current[vm.xid]['cpu_usage'] < 0:
# VM has been rebooted
current[vm['domid']]['cpu_usage'] = 0
current[vm.xid]['cpu_usage'] = 0
else:
current[vm['domid']]['cpu_usage'] = 0
current[vm.xid]['cpu_usage'] = 0
return (current_time, current)