From 0de7c96d2abe488914243f1fd35b6eae1ad5d1ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dejean?= Date: Tue, 25 May 2004 14:33:46 +0000 Subject: [PATCH] Added test for end of string. (get_scaled): Cleaned. * glibtop_server.h (skip_line): Added test for end of string. (get_scaled): Cleaned. * cpu.c (glibtop_get_cpu_s): * open.c (glibtop_open_s): Fixed SMP detection. Closes #142597. * uptime.c (glibtop_get_uptime_s): Added boot_time. --- sysdeps/linux/ChangeLog | 10 +++++++++ sysdeps/linux/cpu.c | 41 ++++++++++++++++++++-------------- sysdeps/linux/glibtop_server.h | 7 +++--- sysdeps/linux/open.c | 26 +++++---------------- sysdeps/linux/uptime.c | 2 ++ 5 files changed, 46 insertions(+), 40 deletions(-) diff --git a/sysdeps/linux/ChangeLog b/sysdeps/linux/ChangeLog index e71ba1d4..743dd5ce 100644 --- a/sysdeps/linux/ChangeLog +++ b/sysdeps/linux/ChangeLog @@ -1,3 +1,13 @@ +2004-05-19 Benoit Dejean + + * glibtop_server.h (skip_line): Added test for end of string. + (get_scaled): Cleaned. + + * cpu.c (glibtop_get_cpu_s): + * open.c (glibtop_open_s): Fixed SMP detection. Closes #142597. + + * uptime.c (glibtop_get_uptime_s): Added boot_time. + 2004-03-09 Bastien Nocera * cpu.c: (glibtop_get_cpu_s): fixup CPU usage calculation on 2.6 diff --git a/sysdeps/linux/cpu.c b/sysdeps/linux/cpu.c index 36d1b507..0a94f832 100644 --- a/sysdeps/linux/cpu.c +++ b/sysdeps/linux/cpu.c @@ -54,8 +54,8 @@ void glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) { char buffer [BUFSIZ], *p; - int fd, len, i; - guint64 total; + int fd, len; + guint i; glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0); @@ -73,6 +73,10 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) buffer [len] = '\0'; + /* + * GLOBAL + */ + p = skip_token (buffer); /* "cpu" */ buf->user = strtoull (p, &p, 0); @@ -85,20 +89,24 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) buf->sys += strtoull(p, &p, 0); /* "irq" */ buf->sys += strtoull(p, &p, 0); /* "softirq" */ - total = buf->user; - total += buf->nice; - total += buf->sys; - total += buf->idle; - buf->total = total; + buf->total = buf->user + buf->nice + buf->sys + buf->idle; buf->frequency = 100; buf->flags = _glibtop_sysdeps_cpu; - for (i = 0; i < server->ncpu; i++) { - if (strncmp (p+1, "cpu", 3) || !isdigit (p [4])) + /* + * PER CPU + */ + + for (i = 0; i < GLIBTOP_NCPU && i < server->ncpu; i++) { + + p = skip_line(p); /* move to ^ */ + + if (strncmp (p, "cpu", 3) || !isdigit (p [3])) break; - p += 6; + p = skip_token(p); /* "cpuN" */ + buf->xcpu_user [i] = strtoull (p, &p, 0); buf->xcpu_nice [i] = strtoull (p, &p, 0); buf->xcpu_sys [i] = strtoull (p, &p, 0); @@ -109,13 +117,12 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) buf->xcpu_sys [i] += strtoull(p, &p, 0); /* "irq" */ buf->xcpu_sys [i] += strtoull(p, &p, 0); /* "softirq" */ - total = buf->xcpu_user [i]; - total += buf->xcpu_nice [i]; - total += buf->xcpu_sys [i]; - total += buf->xcpu_idle [i]; - - buf->xcpu_total [i] = total; + 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; + if(i >= 2) /* ok, that's a real SMP */ + buf->flags |= _glibtop_sysdeps_cpu_smp; } diff --git a/sysdeps/linux/glibtop_server.h b/sysdeps/linux/glibtop_server.h index 22060a3f..d96bbb33 100644 --- a/sysdeps/linux/glibtop_server.h +++ b/sysdeps/linux/glibtop_server.h @@ -53,8 +53,8 @@ skip_multiple_token (const char *p, int count) static inline char * skip_line (const char *p) { - while (*p != '\n') p++; - return (char *) ++p; + while (*p && *p != '\n') p++; + return (char *) (*p ? p+1 : p); } static inline unsigned long long @@ -62,11 +62,12 @@ get_scaled(const char *buffer, const char *key) { const char *ptr; char *next; + const size_t len = strlen(key); unsigned long long value = 0; if ((ptr = strstr(buffer, key))) { - ptr += strlen(key); + ptr += len; value = strtoull(ptr, &next, 0); if (strchr(next, 'k')) value *= 1024; diff --git a/sysdeps/linux/open.c b/sysdeps/linux/open.c index 08bfa379..6943764c 100644 --- a/sysdeps/linux/open.c +++ b/sysdeps/linux/open.c @@ -61,24 +61,14 @@ glibtop_open_s (glibtop *server, const char *program_name, const unsigned long features, const unsigned flags) { - char buffer [BUFSIZ], *p; - struct stat statb; - int fd, len, i; + char buffer [BUFSIZ], *p = buffer; + int fd, len; server->name = program_name; set_linux_version (); server->os_version_code = (unsigned long) linux_version_code; - server->ncpu = 0; - - /* On Linux 2.4.x, /proc/stat has "cpu" and "cpu0" entries even - * for non-SMP systems. Checking whether /proc//cpu exists - * is a much better way to detect SMP. */ - - if (stat ("/proc/1/cpu", &statb)) - return; - fd = open (FILENAME, O_RDONLY); if (fd < 0) glibtop_error_io_r (server, "open (%s)", FILENAME); @@ -90,17 +80,13 @@ glibtop_open_s (glibtop *server, const char *program_name, close (fd); buffer [len] = '\0'; - - p = skip_multiple_token (buffer, 5) + 1; - - for (i = 0; i < GLIBTOP_NCPU; i++) { + + for (server->ncpu = 0; server->ncpu < GLIBTOP_NCPU; server->ncpu++) { + + p = skip_line(p); if (strncmp (p, "cpu", 3) || !isdigit (p [3])) break; - - server->ncpu++; - - p = skip_multiple_token (p, 5) + 1; } #if DEBUG diff --git a/sysdeps/linux/uptime.c b/sysdeps/linux/uptime.c index f54dd1c8..7700b6b4 100644 --- a/sysdeps/linux/uptime.c +++ b/sysdeps/linux/uptime.c @@ -24,6 +24,7 @@ #include #include #include +#include static unsigned long _glibtop_sysdeps_uptime = (1L << GLIBTOP_UPTIME_UPTIME) + (1L << GLIBTOP_UPTIME_IDLETIME); @@ -64,6 +65,7 @@ glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf) buf->uptime = strtod (buffer, &p); buf->idletime = strtod (p, &p); + buf->boot_time = (guint64) time(NULL) - (guint64) buf->uptime; buf->flags = _glibtop_sysdeps_uptime; }