Major code cleanups, we now use open () and read ().
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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++;
|
||||
|
||||
fscanf (f, "%lf %lf %lf\n",
|
||||
&buf->loadavg [0], &buf->loadavg [1], &buf->loadavg [2]);
|
||||
/* Older Linux versions don't have the nr_running/nr_tasks fields. */
|
||||
|
||||
fclose (f);
|
||||
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);
|
||||
|
||||
buf->flags |= _glibtop_sysdeps_loadavg_tasks;
|
||||
}
|
||||
|
@@ -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);
|
||||
|
||||
f = fopen ("/proc/meminfo", "r");
|
||||
if (!f) return;
|
||||
len = read (fd, buffer, BUFSIZ-1);
|
||||
if (len < 0)
|
||||
glibtop_error_io_r (server, "read (%s)", FILENAME);
|
||||
|
||||
fscanf (f, "%*[^\n]\nMem: %Lu %Lu %Lu %Lu %Lu %Lu\n",
|
||||
&buf->total, &buf->used, &buf->free, &buf->shared,
|
||||
&buf->buffer, &buf->cached);
|
||||
close (fd);
|
||||
|
||||
buffer [len] = '\0';
|
||||
|
||||
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;
|
||||
}
|
||||
|
@@ -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);
|
||||
|
||||
fscanf (f, "%*[^\n]\n%*[^\n]\nSwap: %Lu %Lu %Lu\n",
|
||||
&buf->total, &buf->used, &buf->free);
|
||||
len = read (fd, buffer, BUFSIZ-1);
|
||||
if (len < 0)
|
||||
glibtop_error_io_r (server, "read (%s)", PROC_STAT);
|
||||
|
||||
fclose (f);
|
||||
close (fd);
|
||||
|
||||
fd = open ("/proc/stat", O_RDONLY);
|
||||
len = read (fd, buffer, BUFSIZ);
|
||||
close (fd);
|
||||
buffer [len] = '\0';
|
||||
|
||||
ptr = strstr (buffer, "\nswap");
|
||||
if (ptr == NULL) return;
|
||||
p = strstr (buffer, "\nswap");
|
||||
if (p == NULL) return;
|
||||
|
||||
sscanf (ptr, "\nSwap: %Lu %Lu\n",
|
||||
&buf->pagein, &buf->pageout);
|
||||
p = skip_token (p);
|
||||
|
||||
buf->pagein = strtoul (p, &p, 0);
|
||||
buf->pageout = strtoul (p, &p, 0);
|
||||
|
||||
buf->flags |= _glibtop_sysdeps_swap_paging;
|
||||
}
|
||||
|
@@ -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));
|
||||
|
||||
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->uptime = (float) strtod (buffer, &p);
|
||||
buf->idletime = (float) strtod (p, &p);
|
||||
|
||||
buf->flags = _glibtop_sysdeps_uptime;
|
||||
|
||||
f = fopen ("/proc/uptime", "r");
|
||||
if (!f) return;
|
||||
|
||||
fscanf (f, "%lf %lf\n", &buf->uptime, &buf->idletime);
|
||||
|
||||
fclose (f);
|
||||
}
|
||||
|
Reference in New Issue
Block a user