summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoît Dejean <bdejean@src.gnome.org>2004-06-12 12:05:49 +0000
committerBenoît Dejean <bdejean@src.gnome.org>2004-06-12 12:05:49 +0000
commit10d56bd5fcb34689b1c91fe8153356219f8ffa2e (patch)
tree9ceb3b7e98f1b72311a8f77b54222c3cbbe1ad22
parent70b0925a826828e2fb7b683258f9c66eeb10142d (diff)
downloadlibgtop-10d56bd5fcb34689b1c91fe8153356219f8ffa2e.tar.gz
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.
-rw-r--r--sysdeps/linux/ChangeLog22
-rw-r--r--sysdeps/linux/Makefile.am2
-rw-r--r--sysdeps/linux/glibtop_server.c23
-rw-r--r--sysdeps/linux/glibtop_server.h27
-rw-r--r--sysdeps/linux/procmap.c103
-rw-r--r--sysdeps/linux/procmem.c2
-rw-r--r--sysdeps/linux/procsegment.c2
-rw-r--r--sysdeps/linux/sem_limits.c2
-rw-r--r--sysdeps/linux/shm_limits.c4
-rw-r--r--sysdeps/linux/swap.c4
-rw-r--r--sysdeps/linux/sysinfo.c9
-rw-r--r--sysdeps/linux/uptime.c2
12 files changed, 118 insertions, 84 deletions
diff --git a/sysdeps/linux/ChangeLog b/sysdeps/linux/ChangeLog
index f506a9e7..79ac6c0c 100644
--- a/sysdeps/linux/ChangeLog
+++ b/sysdeps/linux/ChangeLog
@@ -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>
* procuid.c: (glibtop_get_proc_uid_s): Whitespace clean up.
diff --git a/sysdeps/linux/Makefile.am b/sysdeps/linux/Makefile.am
index 139b6811..9f26335e 100644
--- a/sysdeps/linux/Makefile.am
+++ b/sysdeps/linux/Makefile.am
@@ -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 \
proctime.c procmem.c procsignal.c prockernel.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_LDFLAGS = $(LT_VERSION_INFO)
diff --git a/sysdeps/linux/glibtop_server.c b/sysdeps/linux/glibtop_server.c
new file mode 100644
index 00000000..73075036
--- /dev/null
+++ b/sysdeps/linux/glibtop_server.c
@@ -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;
+}
+
diff --git a/sysdeps/linux/glibtop_server.h b/sysdeps/linux/glibtop_server.h
index e99af495..b1f3d7d6 100644
--- a/sysdeps/linux/glibtop_server.h
+++ b/sysdeps/linux/glibtop_server.h
@@ -24,6 +24,7 @@
#ifndef __GLIBTOP_SERVER_H__
#define __GLIBTOP_SERVER_H__
+
#include <fcntl.h>
#include <ctype.h>
@@ -36,8 +37,8 @@ G_BEGIN_DECLS
static inline char *
skip_token (const char *p)
{
- while (isspace(*p)) p++;
- while (*p && !isspace(*p)) p++;
+ while (isspace(*p)) p++;
+ while (*p && !isspace(*p)) p++;
return (char *)p;
}
@@ -57,25 +58,9 @@ skip_line (const char *p)
return (char *) (*p ? p+1 : p);
}
-static inline 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;
-}
+unsigned long long
+get_scaled(const char *buffer, const char *key);
+
static inline int
proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid)
diff --git a/sysdeps/linux/procmap.c b/sysdeps/linux/procmap.c
index f217bc75..fbe3d0dc 100644
--- a/sysdeps/linux/procmap.c
+++ b/sysdeps/linux/procmap.c
@@ -25,6 +25,12 @@
#include <glibtop/error.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 =
(1L << GLIBTOP_PROC_MAP_NUMBER) + (1L << GLIBTOP_PROC_MAP_TOTAL) +
(1L << GLIBTOP_PROC_MAP_SIZE);
@@ -48,89 +54,90 @@ glibtop_init_proc_map_s (glibtop *server)
glibtop_map_entry *
glibtop_get_proc_map_s (glibtop *server, glibtop_proc_map *buf, pid_t pid)
{
- char fn [BUFSIZ];
- glibtop_map_entry *entry_list = NULL;
- int rv, n = 0;
+ char filename [GLIBTOP_MAP_FILENAME_LEN+1];
+ glibtop_map_entry *entry_list;
+ gsize allocated;
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);
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;
- do {
- short dev_major, dev_minor;
- unsigned long start, end, offset, inode, perm;
- char flags [5], *format;
- size_t size;
+ allocated = 32; /* magic */
+ entry_list = g_new(glibtop_map_entry, allocated);
+
+ for(buf->number = 0; TRUE; buf->number++)
+ {
+ unsigned long perm = 0;
- if (sizeof (void*) == 8)
- format = "%16lx-%16lx %4c\n %16lx %02hx:%02hx %ld";
- else
- format = "%08lx-%08lx %4c\n %08lx %02hx:%02hx %ld";
+ int rv;
- rv = fscanf (maps, format,
+ /* 8 arguments */
+ rv = fscanf (maps, PROC_MAPS_FORMAT,
&start, &end, flags, &offset,
- &dev_major, &dev_minor, &inode);
+ &dev_major, &dev_minor, &inode, filename);
- flags [4] = 0;
+ if(rv == EOF || rv < 7)
+ break;
- /* Compute access permissions. */
+ if(rv == 7) /* no filename */
+ filename[0] = '\0';
- perm = 0;
+ /* Compute access permissions. */
if (flags [0] == 'r')
perm |= GLIBTOP_MAP_PERM_READ;
+
if (flags [1] == 'w')
perm |= GLIBTOP_MAP_PERM_WRITE;
+
if (flags [2] == 'x')
perm |= GLIBTOP_MAP_PERM_EXECUTE;
+
if (flags [3] == 's')
perm |= GLIBTOP_MAP_PERM_SHARED;
- if (flags [3] == 'p')
+ else if (flags [3] == 'p')
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);
-
- } else fn [0] = 0;
-
- size = (n+1) * sizeof (glibtop_map_entry);
-
- 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) +
+ entry_list [buf->number].start = (guint64) start;
+ entry_list [buf->number].end = (guint64) end;
+ entry_list [buf->number].offset = (guint64) offset;
+ entry_list [buf->number].perm = (guint64) perm;
+ entry_list [buf->number].device = (guint64) (dev_major << 8) +
(guint64) dev_minor;
- entry_list [n].inode = (guint64) inode;
-
- g_strlcpy (entry_list [n].filename, fn, sizeof entry_list [n].filename);
+ entry_list [buf->number].inode = (guint64) inode;
- n++;
-
- } while (rv != EOF && rv && fn [0] != EOF);
+ g_strlcpy (entry_list [buf->number].filename, filename,
+ sizeof entry_list [buf->number].filename);
+ }
fclose (maps);
- buf->number = n;
+ buf->flags = _glibtop_sysdeps_proc_map;
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;
}
diff --git a/sysdeps/linux/procmem.c b/sysdeps/linux/procmem.c
index c0562de3..00f76b48 100644
--- a/sysdeps/linux/procmem.c
+++ b/sysdeps/linux/procmem.c
@@ -38,7 +38,7 @@ static const unsigned long _glibtop_sysdeps_proc_mem_statm =
#endif
/* 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(size) ((size) << pageshift)
diff --git a/sysdeps/linux/procsegment.c b/sysdeps/linux/procsegment.c
index 8fc4b4b9..34d9a7b2 100644
--- a/sysdeps/linux/procsegment.c
+++ b/sysdeps/linux/procsegment.c
@@ -42,7 +42,7 @@ static const unsigned long _glibtop_sysdeps_proc_segment_statm =
#endif
/* 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(size) ((size) << pageshift)
diff --git a/sysdeps/linux/sem_limits.c b/sysdeps/linux/sem_limits.c
index cd6c5bd8..692da62c 100644
--- a/sysdeps/linux/sem_limits.c
+++ b/sysdeps/linux/sem_limits.c
@@ -41,7 +41,7 @@ union semun
};
#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_SEMMNS) + (1L << GLIBTOP_IPC_SEMMNU) +
(1L << GLIBTOP_IPC_SEMMSL) + (1L << GLIBTOP_IPC_SEMOPM) +
diff --git a/sysdeps/linux/shm_limits.c b/sysdeps/linux/shm_limits.c
index a1b6f282..542fd449 100644
--- a/sysdeps/linux/shm_limits.c
+++ b/sysdeps/linux/shm_limits.c
@@ -26,7 +26,7 @@
#include <sys/ipc.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_SHMMNI) + (1L << GLIBTOP_IPC_SHMSEG) +
(1L << GLIBTOP_IPC_SHMALL);
@@ -44,7 +44,7 @@ glibtop_init_shm_limits_s (glibtop *server)
void
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);
diff --git a/sysdeps/linux/swap.c b/sysdeps/linux/swap.c
index b7b9b7be..c9a42e5e 100644
--- a/sysdeps/linux/swap.c
+++ b/sysdeps/linux/swap.c
@@ -27,11 +27,11 @@
#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_FREE);
-static unsigned long _glibtop_sysdeps_swap_paging =
+static const unsigned long _glibtop_sysdeps_swap_paging =
(1L << GLIBTOP_SWAP_PAGEIN) + (1L << GLIBTOP_SWAP_PAGEOUT);
/* Init function. */
diff --git a/sysdeps/linux/sysinfo.c b/sysdeps/linux/sysinfo.c
index 5761b1a8..40844f37 100644
--- a/sysdeps/linux/sysinfo.c
+++ b/sysdeps/linux/sysinfo.c
@@ -28,26 +28,23 @@
static const unsigned long _glibtop_sysdeps_sysinfo =
(1L << GLIBTOP_SYSINFO_CPUINFO);
-static glibtop_sysinfo sysinfo;
+static glibtop_sysinfo sysinfo = { .flags = 0 };
static void
init_sysinfo (glibtop *server)
{
- static gboolean is_init = FALSE;
char buffer [BUFSIZ];
glibtop_entry *cpuinfo = NULL;
FILE *f;
- if (is_init) return;
-
- is_init = TRUE;
+ if(sysinfo.flags) return;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0);
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)) {
char *p, *start, *key, *value;
diff --git a/sysdeps/linux/uptime.c b/sysdeps/linux/uptime.c
index 7700b6b4..74d3d7f3 100644
--- a/sysdeps/linux/uptime.c
+++ b/sysdeps/linux/uptime.c
@@ -26,7 +26,7 @@
#include <glibtop/uptime.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);
/* Init function. */