From 9d564134de6d54eba31b8f6bb593b8f4a99f2b66 Mon Sep 17 00:00:00 2001 From: Martin Baulig Date: Wed, 9 Sep 1998 15:17:12 +0000 Subject: [PATCH] Initialize `ncpu' on SMP systems. 1998-09-09 Martin Baulig * open.c (glibtop_init_s): Initialize `ncpu' on SMP systems. * cpu.c (glibtop_get_cpu_s): Added SMP support. * ChangeLog: New file. --- sysdeps/linux/ChangeLog | 8 ++++++++ sysdeps/linux/cpu.c | 39 +++++++++++++++++++++++++++++++++++--- sysdeps/linux/open.c | 42 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 sysdeps/linux/ChangeLog diff --git a/sysdeps/linux/ChangeLog b/sysdeps/linux/ChangeLog new file mode 100644 index 00000000..3cd28852 --- /dev/null +++ b/sysdeps/linux/ChangeLog @@ -0,0 +1,8 @@ +1998-09-09 Martin Baulig + + * open.c (glibtop_init_s): Initialize `ncpu' on SMP systems. + + * cpu.c (glibtop_get_cpu_s): Added SMP support. + + * ChangeLog: New file. + diff --git a/sysdeps/linux/cpu.c b/sysdeps/linux/cpu.c index 8991fa85..d5cfd9d6 100644 --- a/sysdeps/linux/cpu.c +++ b/sysdeps/linux/cpu.c @@ -28,12 +28,21 @@ static const unsigned long _glibtop_sysdeps_cpu = (1 << GLIBTOP_CPU_NICE) + (1 << GLIBTOP_CPU_SYS) + (1 << GLIBTOP_CPU_IDLE) + (1 << GLIBTOP_CPU_FREQUENCY); +static const unsigned long _glibtop_sysdeps_cpu_smp = +(1 << GLIBTOP_XCPU_TOTAL) + (1 << GLIBTOP_XCPU_USER) + +(1 << GLIBTOP_XCPU_NICE) + (1 << GLIBTOP_XCPU_SYS) + +(1 << GLIBTOP_XCPU_IDLE); + /* Init function. */ void glibtop_init_cpu_s (glibtop *server) { +#if HAVE_LIBGTOP_SMP + server->sysdeps.cpu = _glibtop_sysdeps_cpu | _glibtop_sysdeps_cpu_smp; +#else server->sysdeps.cpu = _glibtop_sysdeps_cpu; +#endif } /* Provides information about cpu usage. */ @@ -44,14 +53,12 @@ void glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) { char buffer [BUFSIZ], *p; - int fd, len; + int fd, len, i; glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0); memset (buf, 0, sizeof (glibtop_cpu)); - buf->flags = _glibtop_sysdeps_cpu; - fd = open (FILENAME, O_RDONLY); if (fd < 0) glibtop_error_io_r (server, "open (%s)", FILENAME); @@ -74,4 +81,30 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) buf->total = buf->user + buf->nice + buf->sys + buf->idle; buf->frequency = 100; + buf->flags = _glibtop_sysdeps_cpu; + +#if HAVE_LIBGTOP_SMP + for (i = 0; i < GLIBTOP_NCPU; i++) { + u_int64_t user, nice, sys, idle; + + if (strncmp (p+1, "cpu", 3) || !isdigit (p [4])) + break; + + p += 6; + user = strtoul (p, &p, 0); + nice = strtoul (p, &p, 0); + sys = strtoul (p, &p, 0); + idle = strtoul (p, &p, 0); + + buf->xcpu_user [i] = strtoul (p, &p, 0); + buf->xcpu_nice [i] = strtoul (p, &p, 0); + buf->xcpu_sys [i] = strtoul (p, &p, 0); + buf->xcpu_idle [i] = strtoul (p, &p, 0); + + buf->xcpu_total [i] = buf->xcpu_user [i] + buf->xcpu_nice [i] + + buf->xcpu_sys [i] + buf->xcpu_idle [i]; + } + + buf->flags |= _glibtop_sysdeps_cpu_smp; +#endif } diff --git a/sysdeps/linux/open.c b/sysdeps/linux/open.c index 8f59b3fe..9c6e5e66 100644 --- a/sysdeps/linux/open.c +++ b/sysdeps/linux/open.c @@ -19,6 +19,8 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include +#include #include /* ===================================================== @@ -50,12 +52,52 @@ static void set_linux_version(void) { /* Opens pipe to gtop server. Returns 0 on success and -1 on error. */ +#define FILENAME "/proc/stat" + void glibtop_open_s (glibtop *server, const char *program_name, const unsigned long features, const unsigned flags) { +#ifdef HAVE_LIBGTOP_SMP + char buffer [BUFSIZ], *p; + int fd, len, i; +#endif + server->name = program_name; set_linux_version (); server->os_version_code = (unsigned long) linux_version_code; + + server->ncpu = 0; + +#ifdef HAVE_LIBGTOP_SMP + fd = open (FILENAME, O_RDONLY); + if (fd < 0) + glibtop_error_io_r (server, "open (%s)", FILENAME); + + len = read (fd, buffer, BUFSIZ-1); + if (len < 0) + glibtop_error_io_r (server, "read (%s)", FILENAME); + + close (fd); + + buffer [len] = '\0'; + + p = skip_multiple_token (buffer, 5) + 1; + + for (i = 0; i < GLIBTOP_NCPU; i++) { + + if (strncmp (p, "cpu", 3) || !isdigit (p [3])) + break; + + server->ncpu++; + + p = skip_multiple_token (p, 5) + 1; + } + +#if DEBUG + printf ("\nThis machine has %d CPUs.\n\n", server->ncpu); +#endif + +#endif }