[Libvir] Question on acquiring cpuTime in struct _virDomainInfo

Daniel P. Berrange berrange at redhat.com
Thu May 10 16:32:53 UTC 2007

On Thu, May 10, 2007 at 05:41:33PM +0200, Jan Michael wrote:
> Hi everyone,
> using libvirt I'm trying to calculate cpu utilization of a node in  
> percent. But sometimes values beyond 100.0% are being calculated.  
> This is because a domain spend more time on a cpu than time is  
> elapsed in the meantime.
> A short explanation of the way how cpu utilization is computed in my  
> case:
> 	1. - open two connections with
> 		conn_cur/conn_old = virConnectOpenReadOnly(NULL);
> 	2. - get current time
> 		gettimeofday(&time_old, NULL);
> 	   - get domain by id with
> 		dom_old = virDomainLookupByID(conn_old, id)
> 	   - get domain information
> 		virDomainGetInfo(dom_old, &info_old);
> 	3. - sleep a second
> 	4. - doing same stuff like in 2. but with _cur
> 	5. - compute cpu utilization by dividing used cputime by elapsed time
> 		and multiply with 100
> Am I right if I suppose that cpuTime for _virDomainInfo structure  
> will be directly acquired from the hypervisor in virDomainGetInfo 
> (dom_old, &info_old) or is it already present with getting the domain  
> itself? Is there any better solution of doing this, which is more  
> precise?

This is the best approach - the algorithm you summarized is basically
the same as I use in virt-manager. The reason it sometimes goes above
100% is just due to timing / schedular variations 

   1. get timeofday
   2. get cputime for domA
   3. sleep a while
   4. get timeofday
   5. get cputime for domA

We're basically looking at the ratio of 4-1, against 5-2. It would
be 100% accurate if you could guarentee no time elapased between 
steps 1 & 2, or between steps 4 & 5, but there's always some latency
in there, so occassionally you might end up calculating a value that
is a tiny bit over 100%.  In virt-manager I deal with this by simply
rounding down to 100 if this occurs.

Based on the hypercalls which are available to us, I don't see any
way to avoid this scenario. Then again it is not like we really need
millisecond precision in caculating CPU usage so I don't think its
a problem worrying about too much.

> And another general question:
> The monitoring utility of xen, called xentop, provides also  
> statistics about networking and vbds. Are there any plans to provide  
> this values by libvirt in the future?

I'd like to see the ability to track  network & disk I/O stats.
No one has so far stepped forward to suggest an API or implmentation,
but I'd welcome anyone interested in taking a look at this area.

|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 

More information about the libvir-list mailing list