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.
This commit is contained in:
@@ -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>
|
2004-06-12 Benoît Dejean <tazforever@dlfp.org>
|
||||||
|
|
||||||
* Makefile.am:
|
* Makefile.am:
|
||||||
|
@@ -1,17 +1,23 @@
|
|||||||
|
#include <glibtop.h>
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
|
||||||
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;
|
const char *ptr;
|
||||||
char *next;
|
char *next;
|
||||||
const size_t len = strlen(key);
|
|
||||||
unsigned long long value = 0;
|
unsigned long long value = 0;
|
||||||
|
|
||||||
if ((ptr = strstr(buffer, key)))
|
if ((ptr = strstr(buffer, key)))
|
||||||
{
|
{
|
||||||
ptr += len;
|
ptr += strlen(key);
|
||||||
value = strtoull(ptr, &next, 0);
|
value = strtoull(ptr, &next, 0);
|
||||||
if (strchr(next, 'k'))
|
if (strchr(next, 'k'))
|
||||||
value *= 1024;
|
value *= 1024;
|
||||||
@@ -21,3 +27,26 @@ get_scaled(const char *buffer, const char *key)
|
|||||||
return value;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -62,27 +62,8 @@ unsigned long long
|
|||||||
get_scaled(const char *buffer, const char *key);
|
get_scaled(const char *buffer, const char *key);
|
||||||
|
|
||||||
|
|
||||||
static inline int
|
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);
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int
|
static inline int
|
||||||
proc_stat_to_buffer (char *buffer, pid_t pid)
|
proc_stat_to_buffer (char *buffer, pid_t pid)
|
||||||
|
@@ -22,9 +22,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
#include <glibtop/error.h>
|
||||||
#include <glibtop/cpu.h>
|
#include <glibtop/cpu.h>
|
||||||
#include <glibtop/sysinfo.h>
|
#include <glibtop/sysinfo.h>
|
||||||
|
|
||||||
|
#define FILENAME "/proc/cpuinfo"
|
||||||
|
|
||||||
static const unsigned long _glibtop_sysdeps_sysinfo =
|
static const unsigned long _glibtop_sysdeps_sysinfo =
|
||||||
(1L << GLIBTOP_SYSINFO_CPUINFO);
|
(1L << GLIBTOP_SYSINFO_CPUINFO);
|
||||||
|
|
||||||
@@ -33,10 +36,9 @@ static glibtop_sysinfo sysinfo = { .flags = 0 };
|
|||||||
static void
|
static void
|
||||||
init_sysinfo (glibtop *server)
|
init_sysinfo (glibtop *server)
|
||||||
{
|
{
|
||||||
|
int fd;
|
||||||
|
ssize_t len;
|
||||||
char buffer [BUFSIZ];
|
char buffer [BUFSIZ];
|
||||||
glibtop_entry *cpuinfo = NULL;
|
|
||||||
FILE *f;
|
|
||||||
|
|
||||||
if(sysinfo.flags) return;
|
if(sysinfo.flags) return;
|
||||||
|
|
||||||
@@ -44,47 +46,72 @@ init_sysinfo (glibtop *server)
|
|||||||
|
|
||||||
memset (&sysinfo, 0, sizeof (glibtop_sysinfo));
|
memset (&sysinfo, 0, sizeof (glibtop_sysinfo));
|
||||||
|
|
||||||
g_return_if_fail ((f = fopen ("/proc/cpuinfo", "r")));
|
|
||||||
|
|
||||||
while (fgets (buffer, BUFSIZ, f)) {
|
/* load the file */
|
||||||
char *p, *start, *key, *value;
|
|
||||||
|
|
||||||
if (cpuinfo == NULL) {
|
fd = open (FILENAME, O_RDONLY);
|
||||||
cpuinfo = &sysinfo.cpuinfo [sysinfo.ncpu++];
|
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)
|
buffer [len] = '\0';
|
||||||
sysinfo.ncpu = GLIBTOP_NCPU;
|
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
p = strchr (buffer, ':');
|
|
||||||
if (!p) continue;
|
|
||||||
|
|
||||||
/* Remove leading spaces from `p'. */
|
/* the last key has no value and has not been added */
|
||||||
*p = '\0'; start = p; p++;
|
if(*p)
|
||||||
while (isspace (*p)) p++;
|
g_free(*p);
|
||||||
|
|
||||||
/* Remove trailing spaces from `buffer'. */
|
/* just g_free instead of g_strvfree because we stole
|
||||||
while ((start > buffer) && (*start) && isspace (*start))
|
the memory*/
|
||||||
*start-- = '\0';
|
|
||||||
|
|
||||||
key = g_strdup (buffer);
|
g_free(processors);
|
||||||
value = g_strdup (p);
|
|
||||||
|
|
||||||
g_ptr_array_add (cpuinfo->labels, key);
|
|
||||||
|
|
||||||
g_hash_table_insert (cpuinfo->values, key, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose (f);
|
g_strfreev(processors);
|
||||||
|
|
||||||
sysinfo.flags = _glibtop_sysdeps_sysinfo;
|
sysinfo.flags = _glibtop_sysdeps_sysinfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
glibtop_sysinfo *
|
const glibtop_sysinfo *
|
||||||
glibtop_get_sysinfo_s (glibtop *server)
|
glibtop_get_sysinfo_s (glibtop *server)
|
||||||
{
|
{
|
||||||
init_sysinfo (server);
|
init_sysinfo (server);
|
||||||
|
Reference in New Issue
Block a user