summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoît Dejean <bdejean@src.gnome.org>2004-06-12 22:37:35 +0000
committerBenoît Dejean <bdejean@src.gnome.org>2004-06-12 22:37:35 +0000
commit09febbf67d3e7395f160b1d682647e2a3007bedc (patch)
tree3c545a5a647c87ca22372b118e83f1dc2e4cc56c
parentd5b8a71e5b791fc4e6bf5ee2263ae397202e4f32 (diff)
downloadlibgtop-09febbf67d3e7395f160b1d682647e2a3007bedc.tar.gz
Cleaned. (proc_file_to_buffer): Uninlined.
* glibtop_server.h: * glibtop_server.c: (get_scaled): Cleaned. (proc_file_to_buffer): Uninlined. * sysinfo.c: (init_sysinfo): Re-implemented.
-rw-r--r--sysdeps/linux/ChangeLog8
-rw-r--r--sysdeps/linux/glibtop_server.c33
-rw-r--r--sysdeps/linux/glibtop_server.h23
-rw-r--r--sysdeps/linux/sysinfo.c81
4 files changed, 95 insertions, 50 deletions
diff --git a/sysdeps/linux/ChangeLog b/sysdeps/linux/ChangeLog
index 79ac6c0c..c3a39a76 100644
--- a/sysdeps/linux/ChangeLog
+++ b/sysdeps/linux/ChangeLog
@@ -1,3 +1,11 @@
+2004-06-13 Benoît Dejean <tazforever@dlfp.org>
+
+ * glibtop_server.h:
+ * glibtop_server.c: (get_scaled): Cleaned.
+ (proc_file_to_buffer): Uninlined.
+
+ * sysinfo.c: (init_sysinfo): Re-implemented.
+
2004-06-12 Benoît Dejean <tazforever@dlfp.org>
* Makefile.am:
diff --git a/sysdeps/linux/glibtop_server.c b/sysdeps/linux/glibtop_server.c
index 73075036..a33d39db 100644
--- a/sysdeps/linux/glibtop_server.c
+++ b/sysdeps/linux/glibtop_server.c
@@ -1,17 +1,23 @@
+#include <glibtop.h>
+
+#include <glib.h>
+
#include <string.h>
#include <stdlib.h>
+#include <fcntl.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;
+ ptr += strlen(key);
value = strtoull(ptr, &next, 0);
if (strchr(next, 'k'))
value *= 1024;
@@ -21,3 +27,26 @@ get_scaled(const char *buffer, const char *key)
return value;
}
+
+int
+proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid)
+{
+ char filename [256];
+ int fd;
+ ssize_t len;
+
+ g_snprintf (filename, sizeof filename, fmt, pid);
+
+ fd = open (filename, O_RDONLY);
+ if (fd < 0) return -1;
+
+ len = read (fd, buffer, BUFSIZ-1);
+ close (fd);
+
+ if (len < 0) return -1;
+
+ buffer [len] = '\0';
+
+ return 0;
+}
+
diff --git a/sysdeps/linux/glibtop_server.h b/sysdeps/linux/glibtop_server.h
index b1f3d7d6..0946cf1e 100644
--- a/sysdeps/linux/glibtop_server.h
+++ b/sysdeps/linux/glibtop_server.h
@@ -62,27 +62,8 @@ 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)
-{
- char filename [BUFSIZ];
- int fd, len;
-
- sprintf (filename, fmt, pid);
-
- fd = open (filename, O_RDONLY);
- if (fd < 0) return -1;
-
- len = read (fd, buffer, BUFSIZ-1);
- close (fd);
-
- if (len < 0)
- return -1;
-
- buffer [len] = '\0';
-
- return 0;
-}
+int
+proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid);
static inline int
proc_stat_to_buffer (char *buffer, pid_t pid)
diff --git a/sysdeps/linux/sysinfo.c b/sysdeps/linux/sysinfo.c
index 40844f37..384c62d8 100644
--- a/sysdeps/linux/sysinfo.c
+++ b/sysdeps/linux/sysinfo.c
@@ -22,9 +22,12 @@
*/
#include <config.h>
+#include <glibtop/error.h>
#include <glibtop/cpu.h>
#include <glibtop/sysinfo.h>
+#define FILENAME "/proc/cpuinfo"
+
static const unsigned long _glibtop_sysdeps_sysinfo =
(1L << GLIBTOP_SYSINFO_CPUINFO);
@@ -33,10 +36,9 @@ static glibtop_sysinfo sysinfo = { .flags = 0 };
static void
init_sysinfo (glibtop *server)
{
-
+ int fd;
+ ssize_t len;
char buffer [BUFSIZ];
- glibtop_entry *cpuinfo = NULL;
- FILE *f;
if(sysinfo.flags) return;
@@ -44,47 +46,72 @@ init_sysinfo (glibtop *server)
memset (&sysinfo, 0, sizeof (glibtop_sysinfo));
- g_return_if_fail ((f = fopen ("/proc/cpuinfo", "r")));
- while (fgets (buffer, BUFSIZ, f)) {
- char *p, *start, *key, *value;
+ /* load the file */
- if (cpuinfo == NULL) {
- cpuinfo = &sysinfo.cpuinfo [sysinfo.ncpu++];
+ fd = open (FILENAME, O_RDONLY);
+ if (fd < 0)
+ glibtop_error_io_r (server, "open (%s)", FILENAME);
- cpuinfo->labels = g_ptr_array_new ();
+ len = read (fd, buffer, BUFSIZ-1);
+ if (len < 0)
+ glibtop_error_io_r (server, "read (%s)", FILENAME);
- cpuinfo->values = g_hash_table_new (NULL, NULL);
+ close (fd);
- if (sysinfo.ncpu > GLIBTOP_NCPU)
- sysinfo.ncpu = GLIBTOP_NCPU;
- }
+ buffer [len] = '\0';
+
+
+ /* cpuinfo records are seperated by a blank line */
+ gchar ** const processors = g_strsplit(buffer, "\n\n", 0);
+
+
+ for(sysinfo.ncpu = 0;
+ sysinfo.ncpu < GLIBTOP_NCPU && *processors[sysinfo.ncpu];
+ sysinfo.ncpu++) {
+
+ gchar **parts, **p;
- p = strchr (buffer, ':');
- if (!p) continue;
+ glibtop_entry * const cpuinfo = &sysinfo.cpuinfo[sysinfo.ncpu];
+
+ cpuinfo->labels = g_ptr_array_new ();
+
+ cpuinfo->values = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, g_free);
+
+ cpuinfo->descriptions = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, g_free);
+
+ /* "<key> : <value>" */
+ parts = g_strsplit_set(processors[sysinfo.ncpu], ":\n", 0);
+
+ for(p = parts; *p && *(p+1); p += 2) {
+
+ /* stole the allocated memory */
+ gchar * const key = g_strstrip( *p );
+ gchar * const value = g_strstrip( *(p+1) );
+
+ g_hash_table_insert(cpuinfo->values, key, value);
+ }
- /* Remove leading spaces from `p'. */
- *p = '\0'; start = p; p++;
- while (isspace (*p)) p++;
- /* Remove trailing spaces from `buffer'. */
- while ((start > buffer) && (*start) && isspace (*start))
- *start-- = '\0';
+ /* the last key has no value and has not been added */
+ if(*p)
+ g_free(*p);
- key = g_strdup (buffer);
- value = g_strdup (p);
+ /* just g_free instead of g_strvfree because we stole
+ the memory*/
- g_ptr_array_add (cpuinfo->labels, key);
+ g_free(processors);
- g_hash_table_insert (cpuinfo->values, key, value);
}
- fclose (f);
+ g_strfreev(processors);
sysinfo.flags = _glibtop_sysdeps_sysinfo;
}
-glibtop_sysinfo *
+const glibtop_sysinfo *
glibtop_get_sysinfo_s (glibtop *server)
{
init_sysinfo (server);