diff --git a/examples/ChangeLog b/examples/ChangeLog index c3e3e458..f5f424f4 100644 --- a/examples/ChangeLog +++ b/examples/ChangeLog @@ -1,3 +1,7 @@ +1998-09-09 Martin Baulig + + * smp.c: New file. Nice tool to show CPU statistics on SMP systems. + 1998-08-25 Martin Baulig * first.c, second.c, procmap.c: Fixed some compiler warnings. diff --git a/examples/Makefile.am b/examples/Makefile.am index 2e09169d..2d641909 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -12,9 +12,9 @@ DEFS = @DEFS@ libexec_PROGRAMS = first first_static second second_static \ mountlist mountlist_static \ procmap procmap_static \ - @guile_examples@ + @guile_examples@ @smp_examples@ -EXTRA_PROGRAMS = third third_static +EXTRA_PROGRAMS = third third_static smp smp_static first_SOURCES = first.c first_LDADD = $(top_builddir)/lib/libgtop.la \ @@ -74,3 +74,14 @@ mountlist_static_SOURCES= $(mountlist_SOURCES) mountlist_static_LDADD = $(mountlist_LDADD) mountlist_static_LDFLAGS= -static + +smp_SOURCES = smp.c +smp_LDADD = $(top_builddir)/lib/libgtop.la \ + $(top_builddir)/sysdeps/common/libgtop_common.la \ + $(top_builddir)/sysdeps/@sysdeps_dir@/libgtop_sysdeps.la \ + @INTLLIBS@ @LIBSUPPORT@ @LIBGTOP_EXTRA_LIBS@ + +smp_static_SOURCES = $(smp_SOURCES) +smp_static_LDADD = $(smp_LDADD) +smp_static_LDFLAGS = -static + diff --git a/examples/smp.c b/examples/smp.c new file mode 100644 index 00000000..48cf97ba --- /dev/null +++ b/examples/smp.c @@ -0,0 +1,124 @@ +/* $Id$ */ + +/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + This file is part of the Gnome Top Library. + Contributed by Martin Baulig , September 1998. + + The Gnome Top Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Top 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include + +#include +#include +#include + +#include + +int +main (int argc, char *argv []) +{ + glibtop_cpu cpu; + unsigned long frequency; + double total, user, nice, sys, idle; + double b_total, b_user, b_nice, b_sys, b_idle; + double s_total, s_user, s_nice, s_sys, s_idle; + char separator [BUFSIZ], buffer [BUFSIZ]; + int ncpu, i; + + setlocale (LC_ALL, ""); + bindtextdomain (PACKAGE, GTOPLOCALEDIR); + textdomain (PACKAGE); + + glibtop_get_cpu (&cpu); + + ncpu = glibtop_global_server->ncpu ? glibtop_global_server->ncpu : 1; + + frequency = (unsigned long) cpu.frequency; + + total = ((unsigned long) cpu.total) ? ((double) cpu.total) : 1.0; + user = ((unsigned long) cpu.user) ? ((double) cpu.user) : 1.0; + nice = ((unsigned long) cpu.nice) ? ((double) cpu.nice) : 1.0; + sys = ((unsigned long) cpu.sys) ? ((double) cpu.sys) : 1.0; + idle = ((unsigned long) cpu.idle) ? ((double) cpu.idle) : 1.0; + + s_total = s_user = s_nice = s_sys = s_idle = 0.0; + + b_total = total / ncpu; + b_user = user / ncpu; + b_nice = nice / ncpu; + b_sys = sys / ncpu; + b_idle = idle / ncpu; + + memset (separator, '-', 91); + separator [92] = '\0'; + + sprintf (buffer, _("Ticks (%d per second):"), frequency); + + printf ("\n\n%-26s %12s %12s %12s %12s %12s\n%s\n", buffer, + _("Total"), _("User"), _("Nice"), _("Sys"), _("Idle"), separator); + + printf (_("CPU (0x%08lx): %12.0f %12.0f %12.0f %12.0f %12.0f\n\n"), + (unsigned long) cpu.flags, total, user, nice, sys, idle); + + for (i = 0; i < glibtop_global_server->ncpu; i++) { + printf (_("CPU %3d (0x%08lx): %12lu %12lu %12lu %12lu %12lu\n"), i, + (unsigned long) cpu.flags, + (unsigned long) cpu.xcpu_total [i], + (unsigned long) cpu.xcpu_user [i], + (unsigned long) cpu.xcpu_nice [i], + (unsigned long) cpu.xcpu_sys [i], + (unsigned long) cpu.xcpu_idle [i]); + + s_total += fabs (((double) cpu.xcpu_total [i]) - b_total); + s_user += fabs (((double) cpu.xcpu_user [i]) - b_user); + s_nice += fabs (((double) cpu.xcpu_nice [i]) - b_nice); + s_sys += fabs (((double) cpu.xcpu_sys [i]) - b_sys); + s_idle += fabs (((double) cpu.xcpu_idle [i]) - b_idle); + } + + printf ("%s\n\n\n", separator); + + printf ("%-26s %12s %12s %12s %12s %12s\n%s\n", _("Percent:"), + _("Total (%)"), _("User (%)"), _("Nice (%)"), _("Sys (%)"), + _("Idle (%)"), separator); + + printf (_("CPU (0x%08lx): %12.3f %12.3f %12.3f %12.3f %12.3f\n\n"), + (unsigned long) cpu.flags, (double) total * 100.0 / total, + (double) user * 100.0 / total, + (double) nice * 100.0 / total, + (double) sys * 100.0 / total, + (double) idle * 100.0 / total); + + for (i = 0; i < glibtop_global_server->ncpu; i++) { + double p_total, p_user, p_nice, p_sys, p_idle; + + p_total = ((double) cpu.xcpu_total [i]) * 100.0 / total; + p_user = ((double) cpu.xcpu_user [i]) * 100.0 / user; + p_nice = ((double) cpu.xcpu_nice [i]) * 100.0 / nice; + p_sys = ((double) cpu.xcpu_sys [i]) * 100.0 / sys; + p_idle = ((double) cpu.xcpu_idle [i]) * 100.0 / idle; + + printf (_("CPU %3d (0x%08lx): %12.3f %12.3f %12.3f %12.3f %12.3f\n"), + i, (unsigned long) cpu.flags, p_total, p_user, p_nice, + p_sys, p_idle); + } + + printf ("%s\n%-26s %12.3f %12.3f %12.3f %12.3f %12.3f\n\n", separator, + _("Spin:"), s_total * 100.0 / total, s_user * 100.0 / user, + s_nice * 100.0 / nice, s_sys * 100.0 / sys, s_idle * 100.0 / idle); + + exit (0); +}