diff options
author | Benoît Dejean <bdejean@src.gnome.org> | 2004-09-20 20:14:46 +0000 |
---|---|---|
committer | Benoît Dejean <bdejean@src.gnome.org> | 2004-09-20 20:14:46 +0000 |
commit | c7d323749c97da6220ee7cfae3b9fd893129a299 (patch) | |
tree | bd97949ec20022549ba6969b5ca906c4676c11f2 /examples | |
parent | 3d1997ca94bb6d62155c084dce964de3bf38446d (diff) | |
download | libgtop-c7d323749c97da6220ee7cfae3b9fd893129a299.tar.gz |
Added new example : pretty prints the result of glibtop_*. Work in
* .cvsignore:
* Makefile.am:
* pprint.c: (pprint_get_cpu), (pprint_get_fsusage),
(pprint_get_loadavg), (pprint_get_mem), (pprint_get_mountlist),
(pprint_get_msg_limits), (main): Added new example : pretty prints
the result of glibtop_*. Work in progress.
Now builds procargs.
* ../configure.in: Added to static list.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/.cvsignore | 4 | ||||
-rw-r--r-- | examples/ChangeLog | 13 | ||||
-rw-r--r-- | examples/Makefile.am | 20 | ||||
-rw-r--r-- | examples/pprint.c | 192 |
4 files changed, 227 insertions, 2 deletions
diff --git a/examples/.cvsignore b/examples/.cvsignore index 31e727eb..8dcb874c 100644 --- a/examples/.cvsignore +++ b/examples/.cvsignore @@ -14,3 +14,7 @@ second_linux smp_static sysdeps_static netload_static procmap_static mountlist_static smp sysdeps netload procmap mountlist timings timings_static +pprint +pprint_static +procargs +procargs_static diff --git a/examples/ChangeLog b/examples/ChangeLog index 39420f16..d608db97 100644 --- a/examples/ChangeLog +++ b/examples/ChangeLog @@ -1,3 +1,16 @@ +2004-09-20 Benoît Dejean <tazforever@dlfp.org> + + * .cvsignore: + * Makefile.am: + * pprint.c: (pprint_get_cpu), (pprint_get_fsusage), + (pprint_get_loadavg), (pprint_get_mem), (pprint_get_mountlist), + (pprint_get_msg_limits), (main): Added new example : pretty prints + the result of glibtop_*. Work in progress. + + Now builds procargs. + + * ../configure.in: Added to static list. + 2004-07-07 Benoît Dejean <tazforever@dlfp.org> * procargs.c: Added glibtop_get_proc_argv demo. diff --git a/examples/Makefile.am b/examples/Makefile.am index 1f774792..49133396 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -6,7 +6,7 @@ INCLUDES = @INCLUDES@ DEFS = @DEFS@ -noinst_PROGRAMS = first second\ +noinst_PROGRAMS = first second pprint procargs \ mountlist procmap netload sysdeps timings \ @static_targets@ @smp_examples@ @@ -14,7 +14,7 @@ EXTRA_PROGRAMS = first_static second_static \ mountlist_static procmap_static \ third third_static smp smp_static \ netload_static sysdeps_static \ - timings_static + timings_static pprint_static procargs_static first_SOURCES = first.c first_LDADD = $(top_builddir)/lib/libgtop-2.0.la @@ -84,3 +84,19 @@ timings_static_SOURCES = $(timings_SOURCES) timings_static_LDADD = $(timings_LDADD) timings_static_LDFLAGS = -static + +pprint_SOURCES = pprint.c +pprint_LDADD = $(top_builddir)/lib/libgtop-2.0.la + +pprint_static_SOURCES = $(pprint_SOURCES) +pprint_static_LDADD = $(pprint_LDADD) +pprint_static_LDFLAGS = -static + + +procargs_SOURCES = procargs.c +procargs_LDADD = $(top_builddir)/lib/libgtop-2.0.la + +procargs_static_SOURCES = $(procargs_SOURCES) +procargs_static_LDADD = $(procargs_LDADD) +procargs_static_LDFLAGS = -static + diff --git a/examples/pprint.c b/examples/pprint.c new file mode 100644 index 00000000..32b3b227 --- /dev/null +++ b/examples/pprint.c @@ -0,0 +1,192 @@ +#include <glibtop.h> + +#include <glibtop/cpu.h> +#include <glibtop/fsusage.h> +#include <glibtop/loadavg.h> +#include <glibtop/mem.h> +#include <glibtop/mountlist.h> +#include <glibtop/msg_limits.h> + + +#include <stdio.h> +#include <stddef.h> + + +#define buf_offsetof(MEMBER) ((ptrdiff_t)&(buf.MEMBER) - (ptrdiff_t)&buf) + +#define HEADER_PPRINT(FUNC) printf(#FUNC "\tsizeof = %lu B\n", \ +(unsigned long) sizeof buf) + +#define PPRINT(DATA, FORMAT) printf("\t%4lu B %3lu " #DATA " = " FORMAT "\n", \ +(unsigned long) sizeof buf.DATA, (unsigned long) buf_offsetof(DATA), buf.DATA) + +#define PPRINT_ARRAY(ARRAY, SIZE, FORMAT) do { \ +size_t i; \ +printf("\t%4lu B %3lu " #ARRAY "[%lu] = { ", \ +(unsigned long) sizeof buf.ARRAY, (unsigned long) buf_offsetof(ARRAY),\ +(unsigned long) G_N_ELEMENTS(buf.ARRAY)); \ +for(i = 0; i < (SIZE - 1); ++i) printf(".%u = " FORMAT ", ", i, buf.ARRAY[i]); \ +printf(".%u = " FORMAT " }\n", SIZE - 1 , buf.ARRAY[SIZE - 1]); \ +} while(0) + +#define FOOTER_PPRINT() putchar('\n') + + + +static void pprint_get_cpu() +{ + glibtop_cpu buf; + + glibtop_get_cpu(&buf); + + HEADER_PPRINT(glibtop_get_cpu); + PPRINT(flags, "%#llx"); + PPRINT(total, "%llu"); + PPRINT(user, "%llu"); + PPRINT(nice, "%llu"); + PPRINT(sys, "%llu"); + PPRINT(idle, "%llu"); + PPRINT(frequency, "%llu"); + PPRINT_ARRAY(xcpu_total, GLIBTOP_NCPU, "%llu"); + PPRINT_ARRAY(xcpu_user, GLIBTOP_NCPU, "%llu"); + PPRINT_ARRAY(xcpu_nice, GLIBTOP_NCPU, "%llu"); + PPRINT_ARRAY(xcpu_sys, GLIBTOP_NCPU, "%llu"); + PPRINT_ARRAY(xcpu_idle, GLIBTOP_NCPU, "%llu"); + PPRINT(xcpu_flags, "%llx"); + FOOTER_PPRINT(); +} + + + +static void pprint_get_fsusage(const char *mountpoint) +{ + glibtop_fsusage buf; + + glibtop_get_fsusage(&buf, mountpoint); + + HEADER_PPRINT(glibtop_get_fsusage); + PPRINT(flags, "%#llx"); + PPRINT(blocks, "%llu"); + PPRINT(bfree, "%llu"); + PPRINT(bavail, "%llu"); + PPRINT(files, "%llu"); + PPRINT(ffree, "%llu"); + PPRINT(block_size, "%u"); + PPRINT(read, "%llu"); + PPRINT(write, "%llu"); + FOOTER_PPRINT(); +} + + + +static void pprint_get_loadavg() +{ + glibtop_loadavg buf; + + glibtop_get_loadavg(&buf); + + HEADER_PPRINT(glibtop_get_loadavg); + PPRINT(flags, "%#llx"); + PPRINT_ARRAY(loadavg, 3, "%f"); + PPRINT(nr_running, "%llu"); + PPRINT(nr_tasks, "%llu"); + PPRINT(last_pid, "%llu"); + FOOTER_PPRINT(); +} + + + +static void pprint_get_mem() +{ + glibtop_mem buf; + + glibtop_get_mem(&buf); + + HEADER_PPRINT(glibtop_get_mem); + PPRINT(flags, "%#llx"); + PPRINT(total, "%llu"); + PPRINT(used, "%llu"); + PPRINT(free, "%llu"); + PPRINT(shared, "%llu"); + PPRINT(buffer, "%llu"); + PPRINT(cached, "%llu"); + PPRINT(user, "%llu"); + PPRINT(locked, "%llu"); + FOOTER_PPRINT(); +} + + + +static void pprint_get_mountlist(gboolean allfs) +{ + glibtop_mountlist buf; + glibtop_mountentry *entries; + size_t i; + + entries = glibtop_get_mountlist(&buf, allfs); + + HEADER_PPRINT(glibtop_get_mem); + PPRINT(flags, "%#llx"); + PPRINT(number, "%llu"); + PPRINT(total, "%llu"); + PPRINT(size, "%llu"); + + printf("\t%4llu B entries[%llu] = \n\t\t{\n", buf.total, buf.number); + + for(i = 0; i < buf.number; ++i) + { + printf("\t\t .%lu = { .dev = %#llx," + " .devname = \"%s\"," + " .mountir = \"%s\"," + " .type - \"%s\" }\n", + (unsigned long) i, entries[i].dev, + entries[i].devname, + entries[i].mountdir, + entries[i].type); + } + + printf("\t\t}\n"); + FOOTER_PPRINT(); + + g_free(entries); +} + + + +static void pprint_get_msg_limits() +{ + glibtop_msg_limits buf; + + glibtop_get_msg_limits(&buf); + + HEADER_PPRINT(glibtop_get_msg_limits); + PPRINT(flags, "%#llx"); + PPRINT(msgpool, "%llu"); + PPRINT(msgmap, "%llu"); + PPRINT(msgmax, "%llu"); + PPRINT(msgmnb, "%llu"); + PPRINT(msgmni, "%llu"); + PPRINT(msgssz, "%llu"); + PPRINT(msgtql, "%llu"); + FOOTER_PPRINT(); +} + + + +int main() +{ + pprint_get_cpu(); + + pprint_get_fsusage("/"); + + pprint_get_loadavg(); + + pprint_get_mem(); + + pprint_get_mountlist(TRUE); + pprint_get_mountlist(FALSE); + + pprint_get_msg_limits(); + + return 0; +} |