What does "cpu_time" mean exactly in libvirt?

I can infer the following CPU values ​​from libvirt:

virsh domstats vm1 --cpu-total Domain: 'vm1' cpu.time=6173016809079111 cpu.user=26714880000000 cpu.system=248540680000000 virsh cpu-stats vm1 --total Total: cpu_time 6173017.263233824 seconds user_time 26714.890000000 seconds system_time 248540.700000000 seconds 

What does cpu_time represent here?

I want to calculate the percentage of CPU utilization using this data.

thanks

+6
source share
1 answer

It was an amazingly difficult question to answer! After I spilled the kernel code, I realized what was going on here, and it's pretty nice to know what is going on.

Typically for a process on Linux, total CPU utilization is simply the sum of the time spent in user space and the time spent in kernel space. Therefore, it would be naive to expect user_time + system_time equal to cpu_time . I found that Linux tracks the time spent on vCPU threads executing guest code separately from user space or kernel time.

So cpu_time == user_time + system_time + guest_time

So you can think of system_time + user_time as the QEMU / KVM overhead on the host side. And cpu_time - (user_time + guest_time) as the actual amount of time during which the guest OS started its processors.

To calculate CPU usage, you probably just want to write cpu_time every N seconds and calculate the delta between the two samples. e.g. usage % = 100 * (cpu_time 2 - cpu_time 1) / N

+9
source

Source: https://habr.com/ru/post/1012109/


All Articles