May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

How do I calculate %CPU in my own libvirt programs?

How do I calculate %CPU in my own libvirt programs?

 

Virt-top FAQ ( http://people.redhat.com/~rjones/virt-top/faq.html#calccpu )

Simple %CPU usage for a domain is calculated by sampling virDomainGetInfoperiodically and looking at the virDomainInfo cpuTime field. This 64 bit field counts nanoseconds of CPU time used by the domain since the domain booted.
Let t be the number of seconds between samples. (Make sure that t is measured as accurately as possible, using something like gettimeofday(2) to measure the real sampling interval).
Let cpu_time_diff be the change in cpuTime over this time, which is the number of nanoseconds of CPU time used by the domain, ie:

cpu_time_diff = cpuTimenow — cpuTimet seconds ago

Let nr_cores be the number of processors (cores) on the system. Use virNodeGetInfoto get this.
Then, %CPU used by the domain is:

%CPU = 100 × cpu_time_diff / (t × nr_cores × 109)

Because sampling doesn’t happen instantaneously, this can be greater than 100%. This is particularly a problem where you have many domains and you have to make avirDomainGetInfo call for each one.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>