Read the cputime(usage) of the LXC instance from the cpuacct.usage cgroup parameter and include it as the "time" field of the return value of GetInstanceInfo. This commit also discards the TODO comment about something which is already not accurate.
Signed-off-by: Yuto KAWAMURA(kawamuray) <[email protected]> --- lib/hypervisor/hv_lxc.py | 19 ++++++++++++++++--- test/py/ganeti.hypervisor.hv_lxc_unittest.py | 3 ++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/hypervisor/hv_lxc.py b/lib/hypervisor/hv_lxc.py index 58acfac..332c3dd 100644 --- a/lib/hypervisor/hv_lxc.py +++ b/lib/hypervisor/hv_lxc.py @@ -89,6 +89,7 @@ class LXCHypervisor(hv_base.BaseHypervisor): "cpuset", "memory", "devices", + "cpuacct", ] def __init__(self): @@ -364,6 +365,19 @@ class LXCHypervisor(hv_base.BaseHypervisor): return utils.ParseCpuMask(cpumask) @classmethod + def _GetCgroupCpuUsage(cls, instance_name): + """Return the CPU usage of an instance. + + """ + try: + cputime_ns = cls._GetCgroupInstanceValue(instance_name, "cpuacct.usage") + except EnvironmentError, err: + raise HypervisorError("Failed to get the cpu usage of %s: %s" % + (instance_name, err)) + + return float(cputime_ns) / 10 ** 9 # nano secs to float secs + + @classmethod def _GetCgroupMemoryLimit(cls, instance_name): """Return the memory limit for an instance @@ -407,15 +421,14 @@ class LXCHypervisor(hv_base.BaseHypervisor): @return: (name, id, memory, vcpus, stat, times) """ - # TODO: read container info from the cgroup mountpoint - if not self._IsInstanceAlive(instance_name): return None cpu_list = self._GetCgroupCpuList(instance_name) memory = self._GetCgroupMemoryLimit(instance_name) / (1024 ** 2) + cputime = self._GetCgroupCpuUsage(instance_name) return (instance_name, 0, memory, len(cpu_list), - hv_base.HvInstanceState.RUNNING, 0) + hv_base.HvInstanceState.RUNNING, cputime) def GetAllInstancesInfo(self, hvparams=None): """Get properties of all instances. diff --git a/test/py/ganeti.hypervisor.hv_lxc_unittest.py b/test/py/ganeti.hypervisor.hv_lxc_unittest.py index df7309d..2559d58 100755 --- a/test/py/ganeti.hypervisor.hv_lxc_unittest.py +++ b/test/py/ganeti.hypervisor.hv_lxc_unittest.py @@ -103,12 +103,13 @@ class TestLXCHypervisorGetInstanceInfo(LXCHypervisorTestCase): super(TestLXCHypervisorGetInstanceInfo, self).setUp() self.hv._GetCgroupCpuList = mock.Mock(return_value=[1, 3]) self.hv._GetCgroupMemoryLimit = mock.Mock(return_value=128*(1024**2)) + self.hv._GetCgroupCpuUsage = mock.Mock(return_value=5.01) @patch_object(LXCHypervisor, "_IsInstanceAlive") def testRunningInstance(self, isalive_mock): isalive_mock.return_value = True self.assertEqual(self.hv.GetInstanceInfo("inst1"), - ("inst1", 0, 128, 2, hv_base.HvInstanceState.RUNNING, 0)) + ("inst1", 0, 128, 2, hv_base.HvInstanceState.RUNNING, 5.01)) @patch_object(LXCHypervisor, "_IsInstanceAlive") def testInactiveOrNonexistentInstance(self, isalive_mock): -- 2.0.4
