[libvirt] [PATCH] examples: Introduce domtop
Ján Tomko
jtomko at redhat.com
Thu Jul 17 12:19:10 UTC 2014
On 07/16/2014 03:53 PM, Michal Privoznik wrote:
> There's this question on the list that is asked over and over again.
> How do I get {cpu, memory, ...} usage in percentage? Or its modified
> version: How do I plot nice graphs like virt-manager does?
>
> It would be nice if we have an example to inspire people. And that's
> what domtop should do. Yes, it could be written in different ways, but
> I've chosen this one as I think it show explicitly what users need to
> implement in order to imitate virt-manager's graphing.
>
> Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
> ---
> .gitignore | 1 +
> Makefile.am | 2 +-
> cfg.mk | 2 +-
> configure.ac | 1 +
> examples/domtop/Makefile.am | 27 +++
> examples/domtop/domtop.c | 388 ++++++++++++++++++++++++++++++++++++++++++++
> libvirt.spec.in | 2 +-
> 7 files changed, 420 insertions(+), 3 deletions(-)
> create mode 100644 examples/domtop/Makefile.am
> create mode 100644 examples/domtop/domtop.c
> +
> +static void
> +print_cpu_usage(const char *dom_name,
> + size_t cpu,
> + size_t ncpus,
> + unsigned long long then,
> + virTypedParameterPtr then_params,
> + size_t then_nparams,
> + unsigned long long now,
> + virTypedParameterPtr now_params,
> + size_t now_nparams)
> +{
> + size_t i, j, k;
> + size_t nparams = now_nparams;
> +
> + if (then_nparams != now_nparams) {
> + /* this should not happen (TM) */
> + ERROR("parameters counts don't match");
> + return;
> + }
> +
> + for (i = 0; i < ncpus; i++) {
> + size_t pos;
> + double usage;
> +
> + /* check if the vCPU is in the maps */
> + if (now_params[i * nparams].type == 0 ||
> + then_params[i * then_nparams].type == 0)
> + continue;
> +
> + for (j = 0; j < nparams; j++) {
> + pos = i * nparams + j;
> + if (STREQ(then_params[pos].field, VIR_DOMAIN_CPU_STATS_CPUTIME) ||
> + STREQ(then_params[pos].field, VIR_DOMAIN_CPU_STATS_VCPUTIME))
> + break;
> + }
> +
> + if (j == nparams) {
> + ERROR("unable to find %s", VIR_DOMAIN_CPU_STATS_CPUTIME);
> + return;
> + }
> +
> + DEBUG("now_params=%llu then_params=%llu now=%llu then=%llu",
> + now_params[pos].value.ul, then_params[pos].value.ul, now, then);
> +
> + /* @now_params and @then_params are in nanoseconds, @now and @then are
> + * in microseconds. In ideal world, we would translate them both into
> + * the same scale, divide one by another and multiply by factor of 100
> + * to get percentage. However, the count of floating point operations
> + * performed has a bad affect on the precision, so instead of dividing
s/affect/effect/
> + * @now_params and @then_params by 1000 and then multiplying again by
> + * 100, we divide only once by 10 and get the same result. */
> + usage = (now_params[pos].value.ul - then_params[pos].value.ul) /
> + (now - then) / 10;
> +
> + printf("CPU%zu: %.2lf\n", cpu + i, usage);
I think printing all the CPUs on one line would look nicer, and it would be
easier to see which numbers are changing.
ACK
Jan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20140717/b9ff3fb8/attachment-0001.sig>
More information about the libvir-list
mailing list