diff options
author | Martin Baulig <martin@src.gnome.org> | 1998-08-16 13:37:14 +0000 |
---|---|---|
committer | Martin Baulig <martin@src.gnome.org> | 1998-08-16 13:37:14 +0000 |
commit | 36657db9409c20cbfa650f0a356194aea574eeae (patch) | |
tree | bd15559289bf31cad0e1587f85d870432c9bbb65 /sysdeps | |
parent | 486cc220c43de57f3d9b380b62a5bc94950750e1 (diff) | |
download | libgtop-36657db9409c20cbfa650f0a356194aea574eeae.tar.gz |
Major code cleanups, we now use open () and read ().
Diffstat (limited to 'sysdeps')
-rw-r--r-- | sysdeps/linux/cpu.c | 26 | ||||
-rw-r--r-- | sysdeps/linux/glibtop_server.h | 18 | ||||
-rw-r--r-- | sysdeps/linux/loadavg.c | 47 | ||||
-rw-r--r-- | sysdeps/linux/mem.c | 30 | ||||
-rw-r--r-- | sysdeps/linux/swap.c | 65 | ||||
-rw-r--r-- | sysdeps/linux/uptime.c | 21 |
6 files changed, 160 insertions, 47 deletions
diff --git a/sysdeps/linux/cpu.c b/sysdeps/linux/cpu.c index 8a740e92..8991fa85 100644 --- a/sysdeps/linux/cpu.c +++ b/sysdeps/linux/cpu.c @@ -43,7 +43,8 @@ glibtop_init_cpu_s (glibtop *server) void glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) { - FILE *f; + char buffer [BUFSIZ], *p; + int fd, len; glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0); @@ -51,15 +52,26 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) buf->flags = _glibtop_sysdeps_cpu; - f = fopen ("/proc/stat", "r"); - if (!f) return; + fd = open (FILENAME, O_RDONLY); + if (fd < 0) + glibtop_error_io_r (server, "open (%s)", FILENAME); - fscanf (f, "cpu %Lu %Lu %Lu %Lu\n", - &buf->user, &buf->nice, &buf->sys, &buf->idle); + len = read (fd, buffer, BUFSIZ-1); + if (len < 0) + glibtop_error_io_r (server, "read (%s)", FILENAME); + + close (fd); + + buffer [len] = '\0'; + + p = skip_token (buffer); /* "cpu" */ + + buf->user = strtoul (p, &p, 0); + buf->nice = strtoul (p, &p, 0); + buf->sys = strtoul (p, &p, 0); + buf->idle = strtoul (p, &p, 0); buf->total = buf->user + buf->nice + buf->sys + buf->idle; buf->frequency = 100; - - fclose (f); } diff --git a/sysdeps/linux/glibtop_server.h b/sysdeps/linux/glibtop_server.h index 23664c12..2d3abe49 100644 --- a/sysdeps/linux/glibtop_server.h +++ b/sysdeps/linux/glibtop_server.h @@ -22,8 +22,26 @@ #ifndef __GLIBTOP_SERVER_H__ #define __GLIBTOP_SERVER_H__ +#include <fcntl.h> +#include <ctype.h> + __BEGIN_DECLS +static inline char * +skip_token (const char *p) +{ + while (isspace(*p)) p++; + while (*p && !isspace(*p)) p++; + return (char *)p; +} + +static inline char * +skip_line (const char *p) +{ + while (*p != '\n') p++; + return (char *) p++; +} + #define GLIBTOP_SUID_CPU 0 #define GLIBTOP_SUID_MEM 0 #define GLIBTOP_SUID_SWAP 0 diff --git a/sysdeps/linux/loadavg.c b/sysdeps/linux/loadavg.c index 73adf0f7..742d25a9 100644 --- a/sysdeps/linux/loadavg.c +++ b/sysdeps/linux/loadavg.c @@ -26,6 +26,11 @@ static const unsigned long _glibtop_sysdeps_loadavg = (1 << GLIBTOP_LOADAVG_LOADAVG); +static const unsigned long _glibtop_sysdeps_loadavg_tasks = +(1 << GLIBTOP_LOADAVG_NR_RUNNING) + +(1 << GLIBTOP_LOADAVG_NR_TASKS) + +(1 << GLIBTOP_LOADAVG_LAST_PID); + /* Init function. */ void @@ -41,19 +46,47 @@ glibtop_init_loadavg_s (glibtop *server) void glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf) { - FILE *f; + char buffer [BUFSIZ], *p, *old; + int fd, len; glibtop_init_s (&server, GLIBTOP_SYSDEPS_LOADAVG, 0); memset (buf, 0, sizeof (glibtop_loadavg)); + 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'; + + buf->loadavg [0] = (float) strtod (buffer, &p); + buf->loadavg [1] = (float) strtod (p, &p); + buf->loadavg [2] = (float) strtod (p, &p); + buf->flags = _glibtop_sysdeps_loadavg; - f = fopen ("/proc/loadavg", "r"); - if (!f) return; + while (isspace(*p)) p++; + + /* Older Linux versions don't have the nr_running/nr_tasks fields. */ + + old = p; + while (*p) { + if (*p == '/') + break; + if (!isdigit (*p)) + return; + p++; + } + + buf->nr_running = strtoul (old, &p, 0); p++; + buf->nr_tasks = strtoul (p, &p, 0); + buf->last_pid = strtoul (p, &p, 0); - fscanf (f, "%lf %lf %lf\n", - &buf->loadavg [0], &buf->loadavg [1], &buf->loadavg [2]); - - fclose (f); + buf->flags |= _glibtop_sysdeps_loadavg_tasks; } diff --git a/sysdeps/linux/mem.c b/sysdeps/linux/mem.c index b2f8e27c..80db347c 100644 --- a/sysdeps/linux/mem.c +++ b/sysdeps/linux/mem.c @@ -44,22 +44,36 @@ glibtop_init_mem_s (glibtop *server) void glibtop_get_mem_s (glibtop *server, glibtop_mem *buf) { - FILE *f; + char buffer [BUFSIZ], *p; + int fd, len; glibtop_init_s (&server, GLIBTOP_SYSDEPS_MEM, 0); memset (buf, 0, sizeof (glibtop_mem)); - buf->flags = _glibtop_sysdeps_mem; + 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); - f = fopen ("/proc/meminfo", "r"); - if (!f) return; + buffer [len] = '\0'; - fscanf (f, "%*[^\n]\nMem: %Lu %Lu %Lu %Lu %Lu %Lu\n", - &buf->total, &buf->used, &buf->free, &buf->shared, - &buf->buffer, &buf->cached); + p = skip_line (buffer); + p = skip_token (p); /* "Mem:" */ + + buf->total = strtoul (p, &p, 0); + buf->used = strtoul (p, &p, 0); + buf->free = strtoul (p, &p, 0); + buf->shared = strtoul (p, &p, 0); + buf->buffer = strtoul (p, &p, 0); + buf->cached = strtoul (p, &p, 0); buf->user = buf->total - buf->free - buf->shared - buf->buffer; - fclose (f); + buf->flags = _glibtop_sysdeps_mem; } diff --git a/sysdeps/linux/swap.c b/sysdeps/linux/swap.c index ebc310e7..0ee526c6 100644 --- a/sysdeps/linux/swap.c +++ b/sysdeps/linux/swap.c @@ -27,49 +27,76 @@ static unsigned long _glibtop_sysdeps_swap = (1 << GLIBTOP_SWAP_TOTAL) + (1 << GLIBTOP_SWAP_USED) + -(1 << GLIBTOP_SWAP_FREE) + (1 << GLIBTOP_SWAP_PAGEIN) + -(1 << GLIBTOP_SWAP_PAGEOUT); +(1 << GLIBTOP_SWAP_FREE); + +static unsigned long _glibtop_sysdeps_swap_paging = +(1 << GLIBTOP_SWAP_PAGEIN) + (1 << GLIBTOP_SWAP_PAGEOUT); /* Init function. */ void glibtop_init_swap_s (glibtop *server) { - server->sysdeps.swap = _glibtop_sysdeps_swap; + server->sysdeps.swap = _glibtop_sysdeps_swap | + _glibtop_sysdeps_swap_paging; } /* Provides information about swap usage. */ -#define FILENAME "/proc/meminfo" +#define MEMINFO "/proc/meminfo" +#define PROC_STAT "/proc/stat" void glibtop_get_swap_s (glibtop *server, glibtop_swap *buf) { - char buffer [BUFSIZ+1], *ptr; + char buffer [BUFSIZ], *p; int fd, len; - FILE *f; glibtop_init_s (&server, GLIBTOP_SYSDEPS_SWAP, 0); memset (buf, 0, sizeof (glibtop_swap)); + fd = open (MEMINFO, O_RDONLY); + if (fd < 0) + glibtop_error_io_r (server, "open (%s)", MEMINFO); + + len = read (fd, buffer, BUFSIZ-1); + if (len < 0) + glibtop_error_io_r (server, "read (%s)", MEMINFO); + + close (fd); + + buffer [len] = '\0'; + + p = skip_line (buffer); + p = skip_line (buffer); + p = skip_token (p); /* "Swap:" */ + + buf->total = strtoul (p, &p, 0); + buf->used = strtoul (p, &p, 0); + buf->free = strtoul (p, &p, 0); + buf->flags = _glibtop_sysdeps_swap; - f = fopen ("/proc/meminfo", "r"); - if (!f) return; + fd = open (PROC_STAT, O_RDONLY); + if (fd < 0) + glibtop_error_io_r (server, "open (%s)", PROC_STAT); + + len = read (fd, buffer, BUFSIZ-1); + if (len < 0) + glibtop_error_io_r (server, "read (%s)", PROC_STAT); + + close (fd); + + buffer [len] = '\0'; - fscanf (f, "%*[^\n]\n%*[^\n]\nSwap: %Lu %Lu %Lu\n", - &buf->total, &buf->used, &buf->free); + p = strstr (buffer, "\nswap"); + if (p == NULL) return; - fclose (f); + p = skip_token (p); - fd = open ("/proc/stat", O_RDONLY); - len = read (fd, buffer, BUFSIZ); - close (fd); + buf->pagein = strtoul (p, &p, 0); + buf->pageout = strtoul (p, &p, 0); - ptr = strstr (buffer, "\nswap"); - if (ptr == NULL) return; - - sscanf (ptr, "\nSwap: %Lu %Lu\n", - &buf->pagein, &buf->pageout); + buf->flags |= _glibtop_sysdeps_swap_paging; } diff --git a/sysdeps/linux/uptime.c b/sysdeps/linux/uptime.c index 553e5896..6a75f4ec 100644 --- a/sysdeps/linux/uptime.c +++ b/sysdeps/linux/uptime.c @@ -41,18 +41,27 @@ glibtop_init_uptime_s (glibtop *server) void glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf) { - FILE *f; + char buffer [BUFSIZ], *p; + int fd, len; glibtop_init_s (&server, GLIBTOP_SYSDEPS_UPTIME, 0); memset (buf, 0, sizeof (glibtop_uptime)); - buf->flags = _glibtop_sysdeps_uptime; + 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); - f = fopen ("/proc/uptime", "r"); - if (!f) return; + close (fd); - fscanf (f, "%lf %lf\n", &buf->uptime, &buf->idletime); + buffer [len] = '\0'; - fclose (f); + buf->uptime = (float) strtod (buffer, &p); + buf->idletime = (float) strtod (p, &p); + + buf->flags = _glibtop_sysdeps_uptime; } |