Merge remote-tracking branch 'origin/pr/307'

* origin/pr/307:
  Implement missing get_cputime() in AdminVM
  Add admin.vm.CurrentState method
This commit is contained in:
Marek Marczykowski-Górecki 2020-01-16 04:12:42 +01:00
commit 309dd11b1d
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
4 changed files with 42 additions and 0 deletions

View File

@ -109,6 +109,7 @@ ADMIN_API_METHODS_SIMPLE = \
admin.vm.volume.Set.revisions_to_keep \
admin.vm.volume.Set.rw \
admin.vm.Stats \
admin.vm.CurrentState \
$(null)
ifeq ($(OS),Linux)

View File

@ -1677,3 +1677,18 @@ class QubesAdminAPI(qubes.api.AbstractQubesAPI):
except asyncio.CancelledError:
# valid method to terminate this loop
pass
@qubes.api.method('admin.vm.CurrentState', no_payload=True,
scope='local', read=True)
@asyncio.coroutine
def vm_current_state(self):
self.enforce(not self.arg)
self.fire_event_for_permission()
state = {
'mem': self.dest.get_mem(),
'mem_static_max': self.dest.get_mem_static_max(),
'cputime': self.dest.get_cputime(),
'power_state': self.dest.get_power_state(),
}
return ' '.join('{}={}'.format(k, v) for k, v in state.items())

View File

@ -2614,6 +2614,20 @@ netvm default=True type=vm
value = self.call_mgmt_func(b'admin.pool.volume.List', b'dom0', b'pool1')
self.assertEqual(value, 'vol1\nvol2\n')
def test_800_current_state_default(self):
value = self.call_mgmt_func(b'admin.vm.CurrentState', b'test-vm1')
self.assertEqual(
value, 'mem=0 mem_static_max=0 cputime=0 power_state=Halted')
def test_801_current_state_changed(self):
self.vm.get_mem = lambda: 512
self.vm.get_mem_static_max = lambda: 1024
self.vm.get_cputime = lambda: 100
self.vm.get_power_state = lambda: 'Running'
value = self.call_mgmt_func(b'admin.vm.CurrentState', b'test-vm1')
self.assertEqual(
value, 'mem=512 mem_static_max=1024 cputime=100 power_state=Running')
def test_990_vm_unexpected_payload(self):
methods_with_no_payload = [
b'admin.vm.List',

View File

@ -160,6 +160,18 @@ class AdminVM(qubes.vm.BaseVM):
self.log.warning('Failed to get memory limit for dom0: %s', e)
return 4096
def get_cputime(self):
'''Get total CPU time burned by Dom0 since start.
.. seealso:
:py:meth:`qubes.vm.qubesvm.QubesVM.get_cputime`
'''
try:
return self.libvirt_domain.info()[4]
except libvirt.libvirtError as e:
self.log.warning('Failed to get CPU time for dom0: %s', e)
return 0
def verify_files(self):
'''Always :py:obj:`True`