Re: [libvirt] [PATCH] examples: Introduce domtop

2014-07-17 Thread Eric Blake
On 07/16/2014 08:27 AM, Eric Blake wrote:
> On 07/16/2014 07:53 AM, 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 
>> ---
>>  .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
>>
> 
> [first round review - I still plan to compile and examine what happens
> when actually running the program, which may result in more comments...]

I was a bit confused that the usage depends on whether I supply a domain
name or not (it looks like without a name, you just list available names
and quit immediately; with a name, you show stats on just that named
domain).  Not sure if the help text could be enhanced to explain that.

I noticed that even though my guest only has one vcpu assigned, the
stats shown listed four cpus (for my test machine), so this is reporting
the cumulative usage of each host cpu, and not of the guest vcpu.
Definitely worth mentioning what perspective the stats are taken from.

The output is hard to visually break apart - either put a blank line
between output spurts (with the current one line per cpu during the
spurt), or put all cpu stats on a single line per spurt. (Oh, I see Jan
also suggested single line per spurt, so it becomes easier to track
columns for usage patterns).

Looking forward to v2.

-- 
Eric Blake   eblake redhat com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH] examples: Introduce domtop

2014-07-17 Thread Ján Tomko
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 
> ---
>  .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



signature.asc
Description: OpenPGP digital signature
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH] examples: Introduce domtop

2014-07-16 Thread Eric Blake
On 07/16/2014 07:53 AM, 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 
> ---
>  .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
> 

[first round review - I still plan to compile and examine what happens
when actually running the program, which may result in more comments...]

> +++ b/cfg.mk
> @@ -1078,7 +1078,7 @@ exclude_file_name_regexp--sc_prohibit_sprintf = \
>  exclude_file_name_regexp--sc_prohibit_strncpy = ^src/util/virstring\.c$$
>  
>  exclude_file_name_regexp--sc_prohibit_strtol = \
> -  
> ^(src/(util/virsexpr|(vbox|xen|xenxs)/.*)\.c)|(examples/domsuspend/suspend.c)$$
> +  
> ^(src/(util/virsexpr|(vbox|xen|xenxs)/.*)\.c)|(examples/domsuspend/suspend.c)|(examples/domtop/domtop.c)$$

Long line.  I'd be happy with the shorter equivalent:

  ^(src/(util/virsexpr|(vbox|xen|xenxs)/.*)|examples/dom.*/.*)\.c$$

[side question - why are we allowing strtol in vbox, xen, and xenxs?
Probably worth an independent cleanup there]


> +++ b/examples/domtop/domtop.c
> @@ -0,0 +1,388 @@

> +
> +static int debug;
> +static int run_top = 1;

Worth including  and making these bool?

> +
> +printf("\n%s [options] [domain name]\n\n"
> +   "  options:\n"
> +   "-d | --debugenable debug printings\n"

s/printings/messages/

> +   "-h | --help print this help\n"
> +   "-c | --connect=URI  hypervisor connection URI\n"
> +   "-D | --delay=X  delay between updates in miliseconds\n",

s/miliseconds/milliseconds/

> +   unified_progname);
> +}
> +
> +static int
> +parse_argv(int argc, char *argv[],
> +   const char **uri,
> +   const char **dom_name,
> +   unsigned int *mili_seconds)

s/mili_seconds/milliseconds/


> +
> +while ((arg = getopt_long(argc, argv, "+:dhc:D:", opt, NULL)) != -1) {
> +switch (arg) {
> +case 'd':
> +debug = 1;

again, bool might be nicer here.

> +
> +printf("Running domains:\n");
> +printf("\n");

Personal preference - I like puts() rather than printf() when there is
no % in the format string.


> +
> +static int
> +do_top(virConnectPtr conn,
> +   const char *dom_name,
> +   unsigned int mili_seconds)

s/mili_seconds/milliseconds/

Overall looks fairly useful.

-- 
Eric Blake   eblake redhat com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH] examples: Introduce domtop

2014-07-16 Thread Michal Privoznik
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 
---
 .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

diff --git a/.gitignore b/.gitignore
index 2d4d401..90fee91 100644
--- a/.gitignore
+++ b/.gitignore
@@ -75,6 +75,7 @@
 /examples/dominfo/info1
 /examples/domsuspend/suspend
 /examples/dommigrate/dommigrate
+/examples/domtop/domtop
 /examples/hellolibvirt/hellolibvirt
 /examples/openauth/openauth
 /gnulib/lib/*
diff --git a/Makefile.am b/Makefile.am
index a374e1a..4aafe94 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -24,7 +24,7 @@ SUBDIRS = . gnulib/lib include src daemon tools docs 
gnulib/tests \
   examples/dominfo examples/domsuspend examples/apparmor \
   examples/xml/nwfilter examples/openauth examples/systemtap \
   tools/wireshark examples/dommigrate \
-  examples/lxcconvert
+  examples/lxcconvert examples/domtop
 
 ACLOCAL_AMFLAGS = -I m4
 
diff --git a/cfg.mk b/cfg.mk
index baaab71..9880704 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -1078,7 +1078,7 @@ exclude_file_name_regexp--sc_prohibit_sprintf = \
 exclude_file_name_regexp--sc_prohibit_strncpy = ^src/util/virstring\.c$$
 
 exclude_file_name_regexp--sc_prohibit_strtol = \
-  
^(src/(util/virsexpr|(vbox|xen|xenxs)/.*)\.c)|(examples/domsuspend/suspend.c)$$
+  
^(src/(util/virsexpr|(vbox|xen|xenxs)/.*)\.c)|(examples/domsuspend/suspend.c)|(examples/domtop/domtop.c)$$
 
 exclude_file_name_regexp--sc_prohibit_xmlGetProp = ^src/util/virxml\.c$$
 
diff --git a/configure.ac b/configure.ac
index 8001e24..f37c716 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2755,6 +2755,7 @@ AC_CONFIG_FILES([\
 examples/domsuspend/Makefile \
 examples/dominfo/Makefile \
 examples/dommigrate/Makefile \
+examples/domtop/Makefile \
 examples/openauth/Makefile \
 examples/hellolibvirt/Makefile \
 examples/systemtap/Makefile \
diff --git a/examples/domtop/Makefile.am b/examples/domtop/Makefile.am
new file mode 100644
index 000..c5cb6c7
--- /dev/null
+++ b/examples/domtop/Makefile.am
@@ -0,0 +1,27 @@
+## Process this file with automake to produce Makefile.in
+
+## Copyright (C) 2014 Red Hat, Inc.
+##
+## This library is free software; you can redistribute it and/or
+## modify it under the terms of the GNU Lesser General Public
+## License as published by the Free Software Foundation; either
+## version 2.1 of the License, or (at your option) any later version.
+##
+## This library is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this library.  If not, see
+## .
+
+INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include
+LDADDS = $(STATIC_BINARIES) $(WARN_CFLAGS) $(top_builddir)/src/libvirt.la \
+   $(COVERAGE_LDFLAGS)
+
+noinst_PROGRAMS=domtop
+
+domtop_SOURCES=domtop.c
+domtop_LDFLAGS=
+domtop_LDADD= $(LDADDS)
diff --git a/examples/domtop/domtop.c b/examples/domtop/domtop.c
new file mode 100644
index 000..fcdcc66
--- /dev/null
+++ b/examples/domtop/domtop.c
@@ -0,0 +1,388 @@
+/*
+ * domtop.c: Demo program showing how to calculate CPU usage
+ *
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * .
+ *
+ * Author: Michal Privoznik 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include