Re: Virt Manager CPU usage calculation algorithm

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 04/09/2012 03:02 PM, Maxim Sditanov wrote:
> Good day
> 
> I implementing in virsh more detailed output for domains
> (https://bugzilla.redhat.com/show_bug.cgi?id=595428).
> And i stuck with CPU usage percentage calculation. How virt manager
> calculate cpu percentage?
> I tried to read virt-manager sources, but it is hard for me to
> understand virt-manager (i am not familiar with python).
> I found function "_sample_cpu_stats" in file src/virtManager/domain.py
> and i think this function make calculations. But i may be wrong
> 
> Please, describe logic, how virt-manager calculate cpu usage
> 

>     def _sample_cpu_stats(self, info, now):

info is the output of dominfo()
now is timestamp of when the info was fetched

>         prevCpuTime = 0
>         prevTimestamp = 0
>         cpuTime = 0
>         cpuTimeAbs = 0

cpuTime is the amount of time the guest has spent scheduled on the host cpus.

>         pcentHostCpu = 0
>         pcentGuestCpu = 0
> 
>         if len(self.record) > 0:
>             prevTimestamp = self.record[0]["timestamp"]
>             prevCpuTime = self.record[0]["cpuTimeAbs"]
> 

We store a bunch of stats history in the list self.record. Get the previous
timestamp and previous cputime

>         if not (info[0] in [libvirt.VIR_DOMAIN_SHUTOFF,
>                             libvirt.VIR_DOMAIN_CRASHED]):
>             guestcpus = info[3]
>             cpuTime = info[4] - prevCpuTime

cpuTime is now the amount of time the guest was scheduled since the last time
we ran dominfo()

>             cpuTimeAbs = info[4]
>             hostcpus = self.conn.host_active_processor_count()
> 
>             pcentbase = (((cpuTime) * 100.0) /
>                          ((now - prevTimestamp) * 1000.0 * 1000.0 * 1000.0))

This is getting the base fraction of how much of the last time period the
guest was running on the CPU. All the multiplication is just getting the
values to be using consistent factors, for example cpuTime is in 10^-9 seconds
it seems.

>             pcentHostCpu = pcentbase / hostcpus
>             pcentGuestCpu = pcentbase / guestcpus
> 

These factor in host and guest cpu counts.

- Cole

>         pcentHostCpu = max(0.0, min(100.0, pcentHostCpu))
>         pcentGuestCpu = max(0.0, min(100.0, pcentGuestCpu))
> 
>         return cpuTime, cpuTimeAbs, pcentHostCpu, pcentGuestCpu




[Index of Archives]     [Linux Virtualization]     [KVM Development]     [CentOS Virtualization]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite Forum]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]     [Video 4 Linux]

  Powered by Linux