use domainListGetStats to get cputime

info() from domain returns cputime value rounded to tenth of a
second. we should just use domainListGetStats to get full nanosecond
detail.

Related-bug: #1677159
Related-bug: #1421584
Change-Id: I8d3f5fa8c683a0cbd9cca7072fa4862b8c00c224
(cherry picked from commit fb6cd01753)
This commit is contained in:
gord chung 2016-11-23 16:47:30 +00:00 committed by Mehdi Abaakouk
parent cf895bac40
commit cfd853405a
2 changed files with 11 additions and 3 deletions

View File

@ -109,8 +109,12 @@ class LibvirtInspector(virt_inspector.Inspector):
def inspect_cpus(self, instance):
domain = self._get_domain_not_shut_off_or_raise(instance)
dom_info = domain.info()
return virt_inspector.CPUStats(number=dom_info[3], time=dom_info[4])
# TODO(gordc): this can probably be cached since it can be used to get
# all data related
stats = self.connection.domainListGetStats([domain])
dom_stat = stats[0][1]
return virt_inspector.CPUStats(number=dom_stat['vcpu.current'],
time=dom_stat['cpu.time'])
def inspect_cpu_l3_cache(self, instance):
domain = self._lookup_by_uuid(instance)

View File

@ -48,13 +48,17 @@ class TestLibvirtInspection(base.BaseTestCase):
self.addCleanup(mock.patch.stopall)
def test_inspect_cpus(self):
fake_stats = [({}, {'cpu.time': 999999, 'vcpu.current': 2})]
with contextlib.ExitStack() as stack:
stack.enter_context(mock.patch.object(self.inspector.connection,
'lookupByUUIDString',
return_value=self.domain))
stack.enter_context(mock.patch.object(self.domain, 'info',
return_value=(0, 0, 0,
2, 999999)))
None, None)))
stack.enter_context(mock.patch.object(self.inspector.connection,
'domainListGetStats',
return_value=fake_stats))
cpu_info = self.inspector.inspect_cpus(self.instance)
self.assertEqual(2, cpu_info.number)
self.assertEqual(999999, cpu_info.time)