[libvirt-users] problem when get the vm cpu stat

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); Thanks, Kevin.

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); }

-----Original Message----- From: Michal Privoznik [mailto:mprivozn@redhat.com] Sent: Friday, April 19, 2013 10:53 PM To: Wangkai (Kevin,C) Cc: libvirt-users@redhat.com Subject: Re: [libvirt-users] problem when get the vm cpu stat
Hi all,
I try to get vm cpu stat by function "virDomainGetCPUStats", when
On 19.04.2013 09:40, Wangkai (Kevin,C) wrote: 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); }
[Wangkai (Kevin,C)] Thank you, I will do this way.
participants (2)
-
Michal Privoznik
-
Wangkai (Kevin,C)