Hi, everyone I'm writing application using libvirt and I need call virDomainGetCPUStats to get domain cpu stats . I learn from API reference that this function has to be called when domain is running. So my question is if there is an atomic way to test the domain's state and get its stats? Or, it's ok to call this function when domain is not active, but caller just gets error returned. In my code, I just call it directly and return error. I wonder if this programming practice has subtle problem like race condition.
On Thu, Apr 05, 2012 at 06:06:43PM +0800, Zhihua Che wrote:> Hi, everyone > > I'm writing application using libvirt and I need call > virDomainGetCPUStats to get domain cpu stats . I learn from API > reference that this function has to be called when domain is running. > So my question is if there is an atomic way to test the domain's state > and get its stats? > > Or, it's ok to call this function when domain is not active, but > caller just gets error returned. > > In my code, I just call it directly and return error. I wonder if > this programming practice has subtle problem like race condition.AFAIK there is no way to do this without a race, not least because a VM can power itself off / crash at literally any time. In virt-top, we just catch the error and ignore it. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming blog: http://rwmj.wordpress.com Fedora now supports 80 OCaml packages (the OPEN alternative to F#) http://cocan.org/getting_started_with_ocaml_on_red_hat_and_fedora
On Thu, Apr 05, 2012 at 06:06:43PM +0800, Zhihua Che wrote:> Hi, everyone > > I'm writing application using libvirt and I need call > virDomainGetCPUStats to get domain cpu stats . I learn from API > reference that this function has to be called when domain is running. > So my question is if there is an atomic way to test the domain's state > and get its stats? > > Or, it's ok to call this function when domain is not active, but > caller just gets error returned. > > In my code, I just call it directly and return error. I wonder if > this programming practice has subtle problem like race condition.For a function which is only callable when the domain is running, libvirt will return a special error code VIR_ERR_OPERATION_INVALID if you invoke it when the domain is shutoff. Thus the race-free way to deal with this scenario is to just call the API, and check for VIR_ERR_OPERATION_INVALID. Any other error code is an indication of a "real" error situaton. Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Possibly Parallel Threads
- What features of kernel are required to support virDomainGetCPUStats?
- What does cpu_time returned by virDomainGetCPUStats mean?
- What's the precision of the domain cpu stats?
- Re: VIR_ERR_OPERATION_INVALID from virDomainDestroyFlags call
- [API reference] Are there XML-handling API to retrieve info directly from xml configure file