Added. These functions are commonly used. Misc cleanups.

* glibtop_server.h:
	* glibtop_server.c: (try_file_to_buffer), (file_to_buffer): Added. These
	functions are commonly used. Misc cleanups.

	* cpu.c: (glibtop_get_cpu_s):
	* loadavg.c: (glibtop_get_loadavg_s):
	* mem.c: (glibtop_get_mem_s):
	* open.c: (glibtop_open_s):
	* swap.c: (glibtop_get_swap_s):
	* sysinfo.c: (init_sysinfo):
	* uptime.c: (glibtop_get_uptime_s): Replaced open/read/close by file_to_buffer().
This commit is contained in:
Benoît Dejean
2004-07-03 13:28:59 +00:00
parent 8eae848c3f
commit 50f086cb68
10 changed files with 92 additions and 118 deletions

View File

@@ -1,3 +1,17 @@
2004-07-03 Benoît Dejean <tazforever@dlfp.org>
* glibtop_server.h:
* glibtop_server.c: (try_file_to_buffer), (file_to_buffer): Added. These
functions are commonly used. Misc cleanups.
* cpu.c: (glibtop_get_cpu_s):
* loadavg.c: (glibtop_get_loadavg_s):
* mem.c: (glibtop_get_mem_s):
* open.c: (glibtop_open_s):
* swap.c: (glibtop_get_swap_s):
* sysinfo.c: (init_sysinfo):
* uptime.c: (glibtop_get_uptime_s): Replaced open/read/close by file_to_buffer().
2004-06-18 Benoît Dejean <tazforever@dlfp.org> 2004-06-18 Benoît Dejean <tazforever@dlfp.org>
* loadavg.c: (glibtop_get_loadavg_s): Whitespace cleanup. * loadavg.c: (glibtop_get_loadavg_s): Whitespace cleanup.

View File

@@ -54,24 +54,13 @@ void
glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
{ {
char buffer [BUFSIZ], *p; char buffer [BUFSIZ], *p;
int fd, len;
int i; int i;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0); glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0);
memset (buf, 0, sizeof (glibtop_cpu)); memset (buf, 0, sizeof (glibtop_cpu));
fd = open (FILENAME, O_RDONLY); file_to_buffer(server, buffer, FILENAME);
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';
/* /*
* GLOBAL * GLOBAL

View File

@@ -1,9 +1,11 @@
#include <glibtop.h> #include <glibtop.h>
#include <glibtop/error.h>
#include <glib.h> #include <glib.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h> #include <fcntl.h>
@@ -31,28 +33,58 @@ get_scaled(const char *buffer, const char *key)
} }
int /*
proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid) * Read functions
*/
enum TRY_FILE_TO_BUFFER
{ {
char filename [256]; TRY_FILE_TO_BUFFER_OK = 0,
TRY_FILE_TO_BUFFER_OPEN = -1,
TRY_FILE_TO_BUFFER_READ = -2
};
int try_file_to_buffer(char *buffer, const char *format, ...)
{
char path[4096];
int fd; int fd;
ssize_t len; ssize_t len;
va_list pa;
g_snprintf (filename, sizeof filename, fmt, pid); va_start(pa, format);
fd = open (filename, O_RDONLY); /* C99 also provides vsnprintf */
if (fd < 0) return -1; g_vsnprintf(path, sizeof path, format, pa);
va_end(pa);
if((fd = open (path, O_RDONLY)) < 0)
return TRY_FILE_TO_BUFFER_OPEN;
len = read (fd, buffer, BUFSIZ-1); len = read (fd, buffer, BUFSIZ-1);
close (fd); close (fd);
if (len < 0) return -1; if (len < 0)
return TRY_FILE_TO_BUFFER_READ;
buffer [len] = '\0'; buffer [len] = '\0';
return 0; return TRY_FILE_TO_BUFFER_OK;
} }
void
file_to_buffer(glibtop *server, char *buffer, const char *filename)
{
switch(try_file_to_buffer(buffer, filename))
{
case TRY_FILE_TO_BUFFER_OPEN:
glibtop_error_io_r (server, "open (%s)", filename);
case TRY_FILE_TO_BUFFER_READ:
glibtop_error_io_r (server, "read (%s)", filename);
}
}
#warning "Ignore the following warning" #warning "Ignore the following warning"
unsigned get_pageshift() unsigned get_pageshift()
{ {

View File

@@ -21,8 +21,10 @@
Boston, MA 02111-1307, USA. Boston, MA 02111-1307, USA.
*/ */
#ifndef __GLIBTOP_SERVER_H__ #ifndef __LINUX__GLIBTOP_SERVER_H__
#define __GLIBTOP_SERVER_H__ #define __LINUX__GLIBTOP_SERVER_H__
#include <glibtop.h>
#include <glib.h> #include <glib.h>
@@ -36,8 +38,10 @@ G_BEGIN_DECLS
#define LINUX_VERSION(x,y,z) (0x10000*(x) + 0x100*(y) + z) #define LINUX_VERSION(x,y,z) (0x10000*(x) + 0x100*(y) + z)
unsigned get_pageshift(); unsigned get_pageshift();
static inline char * static inline char *
skip_token (const char *p) skip_token (const char *p)
{ {
@@ -55,6 +59,7 @@ skip_multiple_token (const char *p, size_t count)
return (char *)p; return (char *)p;
} }
static inline char * static inline char *
skip_line (const char *p) skip_line (const char *p)
{ {
@@ -62,12 +67,29 @@ skip_line (const char *p)
return (char *) (*p ? p+1 : p); return (char *) (*p ? p+1 : p);
} }
unsigned long long unsigned long long
get_scaled(const char *buffer, const char *key); get_scaled(const char *buffer, const char *key);
/* aborts on error */
void
file_to_buffer(glibtop *server, char *buffer, const char *filename);
/* return < 0 on error, otherwise 0 on success */
int int
proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid); try_file_to_buffer(char *buffer, const char *format, ...);
/* some inline functions that wrap proc path
* as fast as macros :)
*/
static inline int
proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid)
{
return try_file_to_buffer(buffer, fmt, pid);
}
static inline int static inline int
proc_stat_to_buffer (char *buffer, pid_t pid) proc_stat_to_buffer (char *buffer, pid_t pid)
@@ -87,6 +109,7 @@ proc_statm_to_buffer (char *buffer, pid_t pid)
return proc_file_to_buffer (buffer, "/proc/%d/statm", pid); return proc_file_to_buffer (buffer, "/proc/%d/statm", pid);
} }
static inline char * static inline char *
proc_stat_after_cmd (char *p) proc_stat_after_cmd (char *p)
{ {
@@ -121,4 +144,4 @@ proc_stat_after_cmd (char *p)
G_END_DECLS G_END_DECLS
#endif #endif /* __LINUX__GLIBTOP_SERVER_H__ */

View File

@@ -49,23 +49,12 @@ void
glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf) glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf)
{ {
char buffer [BUFSIZ], *p, *old; char buffer [BUFSIZ], *p, *old;
int fd, len;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_LOADAVG, 0); glibtop_init_s (&server, GLIBTOP_SYSDEPS_LOADAVG, 0);
memset (buf, 0, sizeof (glibtop_loadavg)); memset (buf, 0, sizeof (glibtop_loadavg));
fd = open (FILENAME, O_RDONLY); file_to_buffer(server, buffer, FILENAME);
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] = strtod (buffer, &p); buf->loadavg [0] = strtod (buffer, &p);
buf->loadavg [1] = strtod (p, &p); buf->loadavg [1] = strtod (p, &p);

View File

@@ -47,23 +47,10 @@ void
glibtop_get_mem_s (glibtop *server, glibtop_mem *buf) glibtop_get_mem_s (glibtop *server, glibtop_mem *buf)
{ {
char buffer [BUFSIZ]; char buffer [BUFSIZ];
int fd, len;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_MEM, 0); glibtop_init_s (&server, GLIBTOP_SYSDEPS_MEM, 0);
memset (buf, 0, sizeof (glibtop_mem)); file_to_buffer(server, buffer, FILENAME);
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->total = get_scaled(buffer, "MemTotal:"); buf->total = get_scaled(buffer, "MemTotal:");
buf->free = get_scaled(buffer, "MemFree:"); buf->free = get_scaled(buffer, "MemFree:");

View File

@@ -66,23 +66,12 @@ glibtop_open_s (glibtop *server, const char *program_name,
const unsigned flags) const unsigned flags)
{ {
char buffer [BUFSIZ], *p = buffer; char buffer [BUFSIZ], *p = buffer;
int fd, len;
server->name = program_name; server->name = program_name;
server->os_version_code = get_linux_version(); server->os_version_code = get_linux_version();
fd = open (FILENAME, O_RDONLY); file_to_buffer(server, buffer, FILENAME);
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';
for (server->ncpu = 0; server->ncpu < GLIBTOP_NCPU; server->ncpu++) { for (server->ncpu = 0; server->ncpu < GLIBTOP_NCPU; server->ncpu++) {

View File

@@ -52,23 +52,12 @@ void
glibtop_get_swap_s (glibtop *server, glibtop_swap *buf) glibtop_get_swap_s (glibtop *server, glibtop_swap *buf)
{ {
char buffer [BUFSIZ], *p; char buffer [BUFSIZ], *p;
int fd, len;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_SWAP, 0); glibtop_init_s (&server, GLIBTOP_SYSDEPS_SWAP, 0);
memset (buf, 0, sizeof (glibtop_swap)); memset (buf, 0, sizeof (glibtop_swap));
fd = open (MEMINFO, O_RDONLY); file_to_buffer(server, buffer, MEMINFO);
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';
/* Kernel 2.6 with multiple lines */ /* Kernel 2.6 with multiple lines */
@@ -78,17 +67,7 @@ glibtop_get_swap_s (glibtop *server, glibtop_swap *buf)
buf->flags = _glibtop_sysdeps_swap; buf->flags = _glibtop_sysdeps_swap;
fd = open (PROC_STAT, O_RDONLY); file_to_buffer(server, buffer, PROC_STAT);
if (fd < 0)
glibtop_error_io_r (server, "open (%s)", PROC_STAT);
len = read (fd, buffer, BUFSIZ-1);
if (len < 0)
glibtop_error_io_r (server, "read (%s)", PROC_STAT);
close (fd);
buffer [len] = '\0';
p = strstr (buffer, "\nswap"); p = strstr (buffer, "\nswap");
if (p == NULL) return; if (p == NULL) return;

View File

@@ -36,8 +36,6 @@ 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];
if(G_LIKELY(sysinfo.flags)) return; if(G_LIKELY(sysinfo.flags)) return;
@@ -46,21 +44,7 @@ init_sysinfo (glibtop *server)
memset (&sysinfo, 0, sizeof (glibtop_sysinfo)); memset (&sysinfo, 0, sizeof (glibtop_sysinfo));
file_to_buffer(server, buffer, FILENAME);
/* load the file */
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';
/* cpuinfo records are seperated by a blank line */ /* cpuinfo records are seperated by a blank line */
gchar ** const processors = g_strsplit(buffer, "\n\n", 0); gchar ** const processors = g_strsplit(buffer, "\n\n", 0);
@@ -97,8 +81,7 @@ init_sysinfo (glibtop *server)
/* the last key has no value and has not been added */ /* the last key has no value and has not been added */
if(*p) if(*p) g_free(*p);
g_free(*p);
/* just g_free instead of g_strvfree because we stole /* just g_free instead of g_strvfree because we stole
the memory*/ the memory*/

View File

@@ -45,23 +45,12 @@ void
glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf) glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf)
{ {
char buffer [BUFSIZ], *p; char buffer [BUFSIZ], *p;
int fd, len;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_UPTIME, 0); glibtop_init_s (&server, GLIBTOP_SYSDEPS_UPTIME, 0);
memset (buf, 0, sizeof (glibtop_uptime)); memset (buf, 0, sizeof (glibtop_uptime));
fd = open (FILENAME, O_RDONLY); file_to_buffer(server, buffer, FILENAME);
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 = strtod (buffer, &p); buf->uptime = strtod (buffer, &p);
buf->idletime = strtod (p, &p); buf->idletime = strtod (p, &p);