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'.
This commit is contained in:
committed by
Martin Baulig
parent
c84923132a
commit
9c9ad03d60
@@ -20,6 +20,7 @@
|
||||
Boston, MA 02111-1307, USA. */
|
||||
|
||||
#include <glibtop.h>
|
||||
#include <glibtop/error.h>
|
||||
#include <glibtop/procsignal.h>
|
||||
|
||||
static const unsigned long _glibtop_sysdeps_proc_signal =
|
||||
@@ -31,9 +32,8 @@ static const unsigned long _glibtop_sysdeps_proc_signal =
|
||||
void
|
||||
glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, 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);
|
||||
|
||||
@@ -45,24 +45,41 @@ glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid)
|
||||
return;
|
||||
}
|
||||
|
||||
sprintf (input, "/proc/%d/stat", pid);
|
||||
|
||||
f = fopen (input, "r");
|
||||
if (!f) return;
|
||||
|
||||
nread = fread (input, 1, BUFSIZ, f);
|
||||
|
||||
if (nread < 0) {
|
||||
fclose (f);
|
||||
return;
|
||||
if (pid != server->machine.last_pid) {
|
||||
server->machine.last_pid = pid;
|
||||
server->machine.no_update = 0;
|
||||
}
|
||||
|
||||
input [nread] = 0;
|
||||
|
||||
|
||||
if (!server->machine.no_update) {
|
||||
server->machine.proc_status [0] = 0;
|
||||
server->machine.proc_statm [0] = 0;
|
||||
server->machine.proc_stat [0] = 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 */
|
||||
@@ -72,7 +89,5 @@ glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid)
|
||||
&buf->signal, &buf->blocked, &buf->sigignore,
|
||||
&buf->sigcatch);
|
||||
|
||||
fclose (f);
|
||||
|
||||
buf->flags = _glibtop_sysdeps_proc_signal;
|
||||
}
|
||||
|
Reference in New Issue
Block a user