[libvirt-users] problem when get the vm cpu stat
Michal Privoznik
mprivozn at redhat.com
Fri Apr 19 14:52:59 UTC 2013
On 19.04.2013 09:40, Wangkai (Kevin,C) wrote:
> Hi all,
>
>
>
> I try to get vm cpu stat by function “virDomainGetCPUStats”, when the vm
>
>
>
> Cpu usage is 100%, I can see from the vm shell by “top”, when the info
>
>
>
> Get by the “virDomainGetCPUStats” show only the “cpu_time” increased,
>
>
>
> “user_time” and “system_time” was not changed at all.
>
>
>
> Is that correct ? How can I get the vm cpu usage?
>
>
>
> nparams = virDomainGetCPUStats(domain, NULL, 0, -1, 1, 0);
>
> virDomainGetCPUStats(domain, params, nparams, -1, 1, 0);
You can gather statistics in similar way that 'top' does. Query current
usage in loops and divide increment by time. Something like:
nparams = virDomainGetCPUStats(domain, NULL, 0, -1, 1, 0);
while (1) {
struct timeval now, then;
int i, j;
gettimeofday(&now, NULL);
virDomainGetCPUStats(domain, params_now, nparams, -1, 1, 0);
/* some sleep, e.g. 500ms */
usleep(5000);
virDomainGetCPUStats(domain, params_then, nparams, -1, 1, 0);
gettimeofday(&then, NULL);
/* find appropriate i and j */
/* virDomainGetCPUStats is returning time in microseconds IIRC */
float usage_in_percent = (params_then[i] - params_now[j]) /
(then.tv_usec - now.tv_usec) / 1000;
printf("%f\n", usage_in_percent);
}
More information about the libvirt-users
mailing list