Added to repository.

* Makefile.am:
	* glibtop_server.c: Added to repository.

	* glibtop_server.h: (get_scaled): Uninlined and moved it to glibtop_server.c.
	(skip_token) : Fixed indentation.

	* procmap.c: (glibtop_get_proc_map_s): Big cleanup. Better allocation
	algorithm.

	* procmem.c:
	* procsegment.c: Added missing initializations.

	* sem_limits.c:
	* shm_limits.c: (glibtop_get_shm_limits_s):
	* swap.c:
	* uptime.c: Added missing const qualifiers.

	* sysinfo.c: (init_sysinfo): Added missing 0 initialization.
	Saved 1 gboolean :D.
This commit is contained in:
Benoît Dejean
2004-06-12 12:05:49 +00:00
parent 70b0925a82
commit 10d56bd5fc
12 changed files with 117 additions and 83 deletions

View File

@@ -1,3 +1,25 @@
2004-06-12 Benoît Dejean <tazforever@dlfp.org>
* Makefile.am:
* glibtop_server.c: Added to repository.
* glibtop_server.h: (get_scaled): Uninlined and moved it to glibtop_server.c.
(skip_token) : Fixed indentation.
* procmap.c: (glibtop_get_proc_map_s): Big cleanup. Better allocation
algorithm.
* procmem.c:
* procsegment.c: Added missing initializations.
* sem_limits.c:
* shm_limits.c: (glibtop_get_shm_limits_s):
* swap.c:
* uptime.c: Added missing const qualifiers.
* sysinfo.c: (init_sysinfo): Added missing 0 initialization.
Saved 1 gboolean :D.
2004-06-08 Benoît Dejean <tazforever@dlfp.org> 2004-06-08 Benoît Dejean <tazforever@dlfp.org>
* procuid.c: (glibtop_get_proc_uid_s): Whitespace clean up. * procuid.c: (glibtop_get_proc_uid_s): Whitespace clean up.

View File

@@ -8,7 +8,7 @@ libgtop_sysdeps_2_0_la_SOURCES = open.c close.c cpu.c mem.c swap.c \
sem_limits.c proclist.c procstate.c procuid.c \ sem_limits.c proclist.c procstate.c procuid.c \
proctime.c procmem.c procsignal.c prockernel.c \ proctime.c procmem.c procsignal.c prockernel.c \
procsegment.c procargs.c procmap.c siglist.c \ procsegment.c procargs.c procmap.c siglist.c \
sysinfo.c netload.c ppp.c sysinfo.c netload.c ppp.c glibtop_server.c
libgtop_sysdeps_2_0_la_LIBADD = @GLIB_LIBS@ libgtop_sysdeps_2_0_la_LIBADD = @GLIB_LIBS@
libgtop_sysdeps_2_0_la_LDFLAGS = $(LT_VERSION_INFO) libgtop_sysdeps_2_0_la_LDFLAGS = $(LT_VERSION_INFO)

View File

@@ -0,0 +1,23 @@
#include <string.h>
#include <stdlib.h>
unsigned long long
get_scaled(const char *buffer, const char *key)
{
const char *ptr;
char *next;
const size_t len = strlen(key);
unsigned long long value = 0;
if ((ptr = strstr(buffer, key)))
{
ptr += len;
value = strtoull(ptr, &next, 0);
if (strchr(next, 'k'))
value *= 1024;
else if (strchr(next, 'M'))
value *= 1024 * 1024;
}
return value;
}

View File

@@ -24,6 +24,7 @@
#ifndef __GLIBTOP_SERVER_H__ #ifndef __GLIBTOP_SERVER_H__
#define __GLIBTOP_SERVER_H__ #define __GLIBTOP_SERVER_H__
#include <fcntl.h> #include <fcntl.h>
#include <ctype.h> #include <ctype.h>
@@ -36,8 +37,8 @@ G_BEGIN_DECLS
static inline char * static inline char *
skip_token (const char *p) skip_token (const char *p)
{ {
while (isspace(*p)) p++; while (isspace(*p)) p++;
while (*p && !isspace(*p)) p++; while (*p && !isspace(*p)) p++;
return (char *)p; return (char *)p;
} }
@@ -57,25 +58,9 @@ skip_line (const char *p)
return (char *) (*p ? p+1 : p); return (char *) (*p ? p+1 : p);
} }
static inline unsigned long long unsigned long long
get_scaled(const char *buffer, const char *key) get_scaled(const char *buffer, const char *key);
{
const char *ptr;
char *next;
const size_t len = strlen(key);
unsigned long long value = 0;
if ((ptr = strstr(buffer, key)))
{
ptr += len;
value = strtoull(ptr, &next, 0);
if (strchr(next, 'k'))
value *= 1024;
else if (strchr(next, 'M'))
value *= 1024 * 1024;
}
return value;
}
static inline int static inline int
proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid) proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid)

View File

@@ -25,6 +25,12 @@
#include <glibtop/error.h> #include <glibtop/error.h>
#include <glibtop/procmap.h> #include <glibtop/procmap.h>
#define PROC_MAPS_FORMAT ((sizeof(void*) == 8) \
? "%16lx-%16lx %4c %16lx %02hx:%02hx %lu%*[ ]%[^\n]\n" \
: "%08lx-%08lx %4c %08lx %02hx:%02hx %lu%*[ ]%[^\n]\n")
static const unsigned long _glibtop_sysdeps_proc_map = static const unsigned long _glibtop_sysdeps_proc_map =
(1L << GLIBTOP_PROC_MAP_NUMBER) + (1L << GLIBTOP_PROC_MAP_TOTAL) + (1L << GLIBTOP_PROC_MAP_NUMBER) + (1L << GLIBTOP_PROC_MAP_TOTAL) +
(1L << GLIBTOP_PROC_MAP_SIZE); (1L << GLIBTOP_PROC_MAP_SIZE);
@@ -48,89 +54,90 @@ glibtop_init_proc_map_s (glibtop *server)
glibtop_map_entry * glibtop_map_entry *
glibtop_get_proc_map_s (glibtop *server, glibtop_proc_map *buf, pid_t pid) glibtop_get_proc_map_s (glibtop *server, glibtop_proc_map *buf, pid_t pid)
{ {
char fn [BUFSIZ]; char filename [GLIBTOP_MAP_FILENAME_LEN+1];
glibtop_map_entry *entry_list = NULL; glibtop_map_entry *entry_list;
int rv, n = 0; gsize allocated;
FILE *maps; FILE *maps;
/* fscanf args */
unsigned short dev_major, dev_minor;
unsigned long start, end, offset, inode;
char flags[4];
/* filename is the 8th argument */
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_MAP, 0); glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_MAP, 0);
memset (buf, 0, sizeof (glibtop_proc_map)); memset (buf, 0, sizeof (glibtop_proc_map));
sprintf (fn, "/proc/%d/maps", pid); sprintf (filename, "/proc/%d/maps", pid);
maps = fopen (fn, "r"); maps = fopen (filename, "r");
if (!maps) return NULL; if (!maps) return NULL;
do { allocated = 32; /* magic */
short dev_major, dev_minor; entry_list = g_new(glibtop_map_entry, allocated);
unsigned long start, end, offset, inode, perm;
char flags [5], *format;
size_t size;
if (sizeof (void*) == 8) for(buf->number = 0; TRUE; buf->number++)
format = "%16lx-%16lx %4c\n %16lx %02hx:%02hx %ld"; {
else unsigned long perm = 0;
format = "%08lx-%08lx %4c\n %08lx %02hx:%02hx %ld";
rv = fscanf (maps, format, int rv;
/* 8 arguments */
rv = fscanf (maps, PROC_MAPS_FORMAT,
&start, &end, flags, &offset, &start, &end, flags, &offset,
&dev_major, &dev_minor, &inode); &dev_major, &dev_minor, &inode, filename);
flags [4] = 0; if(rv == EOF || rv < 7)
break;
if(rv == 7) /* no filename */
filename[0] = '\0';
/* Compute access permissions. */ /* Compute access permissions. */
perm = 0;
if (flags [0] == 'r') if (flags [0] == 'r')
perm |= GLIBTOP_MAP_PERM_READ; perm |= GLIBTOP_MAP_PERM_READ;
if (flags [1] == 'w') if (flags [1] == 'w')
perm |= GLIBTOP_MAP_PERM_WRITE; perm |= GLIBTOP_MAP_PERM_WRITE;
if (flags [2] == 'x') if (flags [2] == 'x')
perm |= GLIBTOP_MAP_PERM_EXECUTE; perm |= GLIBTOP_MAP_PERM_EXECUTE;
if (flags [3] == 's') if (flags [3] == 's')
perm |= GLIBTOP_MAP_PERM_SHARED; perm |= GLIBTOP_MAP_PERM_SHARED;
if (flags [3] == 'p') else if (flags [3] == 'p')
perm |= GLIBTOP_MAP_PERM_PRIVATE; perm |= GLIBTOP_MAP_PERM_PRIVATE;
/* Read filename. */
fn [0] = fgetc (maps); if(buf->number == allocated) {
/* grow by 2 and blank the newly allocated entries */
entry_list = g_renew (glibtop_map_entry, entry_list, allocated * 2);
allocated *= 2;
}
if (fn [0] != '\n' && fn [0] != EOF) { entry_list [buf->number].flags = _glibtop_sysdeps_map_entry;
fscanf (maps, "%*[ ]%[^\n]\n", fn); entry_list [buf->number].start = (guint64) start;
entry_list [buf->number].end = (guint64) end;
} else fn [0] = 0; entry_list [buf->number].offset = (guint64) offset;
entry_list [buf->number].perm = (guint64) perm;
size = (n+1) * sizeof (glibtop_map_entry); entry_list [buf->number].device = (guint64) (dev_major << 8) +
entry_list = g_realloc (entry_list, size);
memset (&(entry_list [n]), 0, sizeof (glibtop_map_entry));
entry_list [n].flags = _glibtop_sysdeps_map_entry;
entry_list [n].start = (guint64) start;
entry_list [n].end = (guint64) end;
entry_list [n].offset = (guint64) offset;
entry_list [n].perm = (guint64) perm;
entry_list [n].device = (guint64) (dev_major << 8) +
(guint64) dev_minor; (guint64) dev_minor;
entry_list [n].inode = (guint64) inode; entry_list [buf->number].inode = (guint64) inode;
g_strlcpy (entry_list [n].filename, fn, sizeof entry_list [n].filename); g_strlcpy (entry_list [buf->number].filename, filename,
sizeof entry_list [buf->number].filename);
n++; }
} while (rv != EOF && rv && fn [0] != EOF);
fclose (maps); fclose (maps);
buf->number = n; buf->flags = _glibtop_sysdeps_proc_map;
buf->size = sizeof (glibtop_map_entry); buf->size = sizeof (glibtop_map_entry);
buf->total = n * sizeof (glibtop_map_entry); buf->total = buf->number * buf->size;
g_renew(glibtop_map_entry, entry_list, buf->number);
return entry_list; return entry_list;
} }

View File

@@ -38,7 +38,7 @@ static const unsigned long _glibtop_sysdeps_proc_mem_statm =
#endif #endif
/* these are for getting the memory statistics */ /* these are for getting the memory statistics */
static int pageshift; /* log base 2 of the pagesize */ static int pageshift = 0; /* log base 2 of the pagesize */
/* define pagetok in terms of pageshift */ /* define pagetok in terms of pageshift */
#define pagetok(size) ((size) << pageshift) #define pagetok(size) ((size) << pageshift)

View File

@@ -42,7 +42,7 @@ static const unsigned long _glibtop_sysdeps_proc_segment_statm =
#endif #endif
/* these are for getting the memory statistics */ /* these are for getting the memory statistics */
static int pageshift; /* log base 2 of the pagesize */ static int pageshift = 0; /* log base 2 of the pagesize */
/* define pagetok in terms of pageshift */ /* define pagetok in terms of pageshift */
#define pagetok(size) ((size) << pageshift) #define pagetok(size) ((size) << pageshift)

View File

@@ -41,7 +41,7 @@ union semun
}; };
#endif #endif
static unsigned long _glibtop_sysdeps_sem_limits = static const unsigned long _glibtop_sysdeps_sem_limits =
(1L << GLIBTOP_IPC_SEMMAP) + (1L << GLIBTOP_IPC_SEMMNI) + (1L << GLIBTOP_IPC_SEMMAP) + (1L << GLIBTOP_IPC_SEMMNI) +
(1L << GLIBTOP_IPC_SEMMNS) + (1L << GLIBTOP_IPC_SEMMNU) + (1L << GLIBTOP_IPC_SEMMNS) + (1L << GLIBTOP_IPC_SEMMNU) +
(1L << GLIBTOP_IPC_SEMMSL) + (1L << GLIBTOP_IPC_SEMOPM) + (1L << GLIBTOP_IPC_SEMMSL) + (1L << GLIBTOP_IPC_SEMOPM) +

View File

@@ -26,7 +26,7 @@
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/shm.h> #include <sys/shm.h>
static unsigned long _glibtop_sysdeps_shm_limits = static const unsigned long _glibtop_sysdeps_shm_limits =
(1L << GLIBTOP_IPC_SHMMAX) + (1L << GLIBTOP_IPC_SHMMIN) + (1L << GLIBTOP_IPC_SHMMAX) + (1L << GLIBTOP_IPC_SHMMIN) +
(1L << GLIBTOP_IPC_SHMMNI) + (1L << GLIBTOP_IPC_SHMSEG) + (1L << GLIBTOP_IPC_SHMMNI) + (1L << GLIBTOP_IPC_SHMSEG) +
(1L << GLIBTOP_IPC_SHMALL); (1L << GLIBTOP_IPC_SHMALL);
@@ -44,7 +44,7 @@ glibtop_init_shm_limits_s (glibtop *server)
void void
glibtop_get_shm_limits_s (glibtop *server, glibtop_shm_limits *buf) glibtop_get_shm_limits_s (glibtop *server, glibtop_shm_limits *buf)
{ {
struct shminfo shminfo; struct shminfo shminfo;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_SHM_LIMITS, 0); glibtop_init_s (&server, GLIBTOP_SYSDEPS_SHM_LIMITS, 0);

View File

@@ -27,11 +27,11 @@
#include <fcntl.h> #include <fcntl.h>
static unsigned long _glibtop_sysdeps_swap = static const unsigned long _glibtop_sysdeps_swap =
(1L << GLIBTOP_SWAP_TOTAL) + (1L << GLIBTOP_SWAP_USED) + (1L << GLIBTOP_SWAP_TOTAL) + (1L << GLIBTOP_SWAP_USED) +
(1L << GLIBTOP_SWAP_FREE); (1L << GLIBTOP_SWAP_FREE);
static unsigned long _glibtop_sysdeps_swap_paging = static const unsigned long _glibtop_sysdeps_swap_paging =
(1L << GLIBTOP_SWAP_PAGEIN) + (1L << GLIBTOP_SWAP_PAGEOUT); (1L << GLIBTOP_SWAP_PAGEIN) + (1L << GLIBTOP_SWAP_PAGEOUT);
/* Init function. */ /* Init function. */

View File

@@ -28,26 +28,23 @@
static const unsigned long _glibtop_sysdeps_sysinfo = static const unsigned long _glibtop_sysdeps_sysinfo =
(1L << GLIBTOP_SYSINFO_CPUINFO); (1L << GLIBTOP_SYSINFO_CPUINFO);
static glibtop_sysinfo sysinfo; static glibtop_sysinfo sysinfo = { .flags = 0 };
static void static void
init_sysinfo (glibtop *server) init_sysinfo (glibtop *server)
{ {
static gboolean is_init = FALSE;
char buffer [BUFSIZ]; char buffer [BUFSIZ];
glibtop_entry *cpuinfo = NULL; glibtop_entry *cpuinfo = NULL;
FILE *f; FILE *f;
if (is_init) return; if(sysinfo.flags) return;
is_init = TRUE;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0); glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0);
memset (&sysinfo, 0, sizeof (glibtop_sysinfo)); memset (&sysinfo, 0, sizeof (glibtop_sysinfo));
g_return_if_fail (f = fopen ("/proc/cpuinfo", "r")); g_return_if_fail ((f = fopen ("/proc/cpuinfo", "r")));
while (fgets (buffer, BUFSIZ, f)) { while (fgets (buffer, BUFSIZ, f)) {
char *p, *start, *key, *value; char *p, *start, *key, *value;

View File

@@ -26,7 +26,7 @@
#include <glibtop/uptime.h> #include <glibtop/uptime.h>
#include <time.h> #include <time.h>
static unsigned long _glibtop_sysdeps_uptime = static const unsigned long _glibtop_sysdeps_uptime =
(1L << GLIBTOP_UPTIME_UPTIME) + (1L << GLIBTOP_UPTIME_IDLETIME); (1L << GLIBTOP_UPTIME_UPTIME) + (1L << GLIBTOP_UPTIME_IDLETIME);
/* Init function. */ /* Init function. */