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:
Martin Baulig
1998-06-07 20:38:02 +00:00
committed by Martin Baulig
parent c84923132a
commit 9c9ad03d60
13 changed files with 456 additions and 199 deletions

View File

@@ -20,6 +20,7 @@
Boston, MA 02111-1307, USA. */
#include <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/procstate.h>
#include <sys/stat.h>
@@ -33,10 +34,9 @@ static const unsigned long _glibtop_sysdeps_proc_state =
void
glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid)
{
char input [BUFSIZ], *tmp;
char buffer [BUFSIZ], input [BUFSIZ], *tmp;
struct stat statb;
int nread;
FILE *f;
int fd, nread;
glibtop_init_r (&server, 0, 0);
@@ -48,6 +48,12 @@ glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid)
return;
}
server->machine.last_pid = pid;
server->machine.no_update = 0;
server->machine.proc_status [0] = 0;
server->machine.proc_statm [0] = 0;
server->machine.proc_stat [0] = 0;
sprintf (input, "/proc/%d/stat", pid);
/* IMPORTANT NOTICE: For security reasons it is extremely important
@@ -61,28 +67,34 @@ glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid)
buf->uid = statb.st_uid;
buf->gid = statb.st_gid;
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));
f = fopen (input, "r");
if (!f) return;
nread = fread (input, 1, BUFSIZ, f);
if (nread < 0) {
fclose (f);
return;
}
input [nread] = 0;
buffer [nread] = 0;
server->machine.no_update = 1;
server->machine.last_pid = pid;
strcpy (server->machine.proc_stat, buffer);
/* 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 "(". */
memset (buf->cmd, 0, sizeof (buf->cmd));
sscanf (input, "%d (%39c", &pid, buf->cmd);
sscanf (buffer, "%d (%39c", &pid, buf->cmd);
sscanf(tmp + 2, "%c", &buf->state); /* skip space after ')' too */
close (fd);
buf->flags = _glibtop_sysdeps_proc_state;
}