Commit ef83bc0c authored by Anael Beutot's avatar Anael Beutot
Browse files

Implemented cpuuse tag for VMs.

parent 71ab2daf
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -56,6 +56,9 @@ class VirtualMachine(object):

        logger.debug('Virtual Machine tags: %s', self.tags)

        #: keep record of CPU stats (libev timestamp, cpu time)
        self.cpu_stats = (hypervisor.handler.main.evloop.now(), dom.info()[4])

    @property
    def state(self):
        return self._state
+11 −2
Original line number Diff line number Diff line
@@ -76,8 +76,17 @@ def cpu(dom):

@ttl(10)
@refresh(5)
def cpuuse():
    pass
@_vir_tag
def cpuuse(dom):
    """Represent CPU use in percent average on 5 seconds."""
    state, _, _, vcpu, cpu_time = dom.lv_dom.info()
    if state != 1:  # if VM is not running
        return None
    old_cpu_stats = dom.cpu_stats
    dom.cpu_stats = (dom.hypervisor.handler.main.evloop.now(), cpu_time)
    # calculate CPU percentage
    return '%.2f' % max(0., min(100., ((cpu_time - old_cpu_stats[1]) * 100.) / (
        (dom.cpu_stats[0] - old_cpu_stats[0]) * 1000. * 1000. * 1000.)))


@_vir_tag