diff options
author | Martin Baulig <martin@home-of-linux.org> | 1998-06-07 20:38:02 +0000 |
---|---|---|
committer | Martin Baulig <martin@src.gnome.org> | 1998-06-07 20:38:02 +0000 |
commit | 9c9ad03d60e25892821169680ff4490a0bf48592 (patch) | |
tree | ab64803e130be2e97f1bfa51d4c1bf3c0fb8bae6 /sysdeps/linux/procsegment.c | |
parent | c84923132a0aaade80a48eb762b27d87640d3921 (diff) | |
download | libgtop-9c9ad03d60e25892821169680ff4490a0bf48592.tar.gz |
New file.
1998-06-07 Martin Baulig <martin@home-of-linux.org>
* sysdeps/linux/glibtop_machine.h: New file.
* sysdeps/linux/*.c: Performance optimizations. We now use
`open' and `read' instead of `fopen' and `fscanf'.
Diffstat (limited to 'sysdeps/linux/procsegment.c')
-rw-r--r-- | sysdeps/linux/procsegment.c | 87 |
1 files changed, 53 insertions, 34 deletions
diff --git a/sysdeps/linux/procsegment.c b/sysdeps/linux/procsegment.c index d5ce5652..1ecefa2a 100644 --- a/sysdeps/linux/procsegment.c +++ b/sysdeps/linux/procsegment.c @@ -20,6 +20,7 @@ Boston, MA 02111-1307, USA. */ #include <config.h> +#include <glibtop/error.h> #include <glibtop/procsegment.h> static const unsigned long _glibtop_sysdeps_proc_segment = @@ -32,11 +33,10 @@ static const unsigned long _glibtop_sysdeps_proc_segment = void glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf, - pid_t pid) + pid_t pid) { - char input [BUFSIZ], *tmp; - int nread; - FILE *f; + char buffer [BUFSIZ], input [BUFSIZ], *tmp; + int fd = 0, nread; glibtop_init_r (&server, 0, 0); @@ -48,24 +48,41 @@ glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf, return; } - sprintf (input, "/proc/%d/stat", pid); + if (pid != server->machine.last_pid) { + server->machine.last_pid = pid; + server->machine.no_update = 0; + } - f = fopen (input, "r"); - if (!f) return; - - nread = fread (input, 1, BUFSIZ, f); - - if (nread < 0) { - fclose (f); - return; + if (!server->machine.no_update) { + server->machine.proc_status [0] = 0; + server->machine.proc_statm [0] = 0; + server->machine.proc_stat [0] = 0; } - - input [nread] = 0; - + + if (server->machine.proc_stat [0]) { + strcpy (buffer, server->machine.proc_stat); + } else { + sprintf (input, "/proc/%d/stat", pid); + + fd = open (input, O_RDONLY); + if (fd == -1) + glibtop_error_r (server, "open (%s): %s", + input, strerror (errno)); + + nread = read (fd, buffer, BUFSIZ); + if (nread == -1) + glibtop_error_r (server, "read (%s): %s", + input, strerror (errno)); + + buffer [nread] = 0; + strcpy (server->machine.proc_stat, buffer); + close (fd); + } + /* This is from guile-utils/gtop/proc/readproc.c */ /* split into "PID (cmd" and "<rest>" */ - tmp = strrchr (input, ')'); + tmp = strrchr (buffer, ')'); *tmp = '\0'; /* replace trailing ')' with NUL */ /* parse these two strings separately, skipping the leading "(". */ sscanf(tmp + 2, /* skip space after ')' too */ @@ -74,26 +91,28 @@ glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf, "%*u %*u %lu %lu %lu", &buf->start_code, &buf->end_code, &buf->start_stack); - fclose (f); - - sprintf (input, "/proc/%d/statm", pid); - - f = fopen (input, "r"); - if (!f) return; - - nread = fread (input, 1, BUFSIZ, f); - - if (nread < 0) { - fclose (f); - return; + if (server->machine.proc_statm [0]) { + strcpy (buffer, server->machine.proc_statm); + } else { + sprintf (input, "/proc/%d/statm", pid); + + fd = open (input, O_RDONLY); + if (fd == -1) + glibtop_error_r (server, "open (%s): %s", + input, strerror (errno)); + + nread = read (fd, buffer, BUFSIZ); + if (nread == -1) + glibtop_error_r (server, "read (%s): %s", + input, strerror (errno)); + + buffer [nread] = 0; + strcpy (server->machine.proc_statm, buffer); + close (fd); } - input [nread] = 0; - - sscanf (input, "%*d %*d %*d %ld %ld %ld %ld", + sscanf (buffer, "%*d %*d %*d %ld %ld %ld %ld", &buf->trs, &buf->lrs, &buf->drs, &buf->dt); - fclose (f); - buf->flags = _glibtop_sysdeps_proc_segment; } |