Added next_token(). Changed skip_token(), i hope it won't break any bad

* glibtop_server.h: Added next_token(). Changed skip_token(),
        i hope it won't break any bad code.
        * procstate.c: (glibtop_get_proc_state_s): Used next_token.
        * proctime.c: (glibtop_get_proc_time_s): Implemented .start_time
        the way it ought to be. Clean ups.
This commit is contained in:
Benoît Dejean
2004-08-03 11:40:37 +00:00
parent 5b39a4831d
commit 808a2d48ac
4 changed files with 52 additions and 7 deletions

View File

@@ -1,3 +1,11 @@
2004-08-03 Benoît Dejean <tazforever@dlfp.org>
* glibtop_server.h: Added next_token(). Changed skip_token(),
i hope it won't break any bad code.
* procstate.c: (glibtop_get_proc_state_s): Used next_token.
* proctime.c: (glibtop_get_proc_time_s): Implemented .start_time
the way it ought to be. Clean ups
2004-07-22 Benoît Dejean <tazforever@dlfp.org>
* netload.c: (glibtop_get_netload_s): Fixed leak.

View File

@@ -41,12 +41,19 @@ G_BEGIN_DECLS
unsigned get_pageshift();
static inline char*
next_token(const char *p)
{
while (isspace(*p)) p++;
return (char*) p;
}
static inline char *
skip_token (const char *p)
{
while (isspace(*p)) p++;
p = next_token(p);
while (*p && !isspace(*p)) p++;
p = next_token(p);
return (char *)p;
}

View File

@@ -72,16 +72,15 @@ glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid)
buf->flags = _glibtop_sysdeps_proc_state_uid;
sprintf (buffer, "/proc/%d", pid);
/* Now we read the remaining fields. */
if (proc_stat_to_buffer (buffer, pid))
return;
p = strrchr (buffer, ')'); *p = '\0';
p = proc_stat_after_cmd(buffer);
p = next_token(p);
switch(p[2])
switch(*p)
{
case 'R':
buf->state = GLIBTOP_PROCESS_RUNNING;
@@ -112,7 +111,7 @@ glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid)
break;
}
p = skip_token (buffer); p++; /* pid */
p = skip_token (buffer); /* pid */
if (G_UNLIKELY(*p++ != '('))
glibtop_error_r (server, "Bad data in /proc/%d/stat", pid);

View File

@@ -24,6 +24,7 @@
#include <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/proctime.h>
#include <glibtop/uptime.h>
static const unsigned long _glibtop_sysdeps_proc_time =
(1L << GLIBTOP_PROC_TIME_UTIME) + (1L << GLIBTOP_PROC_TIME_CUTIME) +
@@ -65,6 +66,7 @@ glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid)
p = skip_multiple_token (p, 11);
/* clock_t (1/100 s) */
buf->utime = strtoull (p, &p, 0);
buf->stime = strtoull (p, &p, 0);
buf->cutime = strtoull (p, &p, 0);
@@ -76,7 +78,36 @@ glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid)
// lets skip it (using previous skip_multiple_token)
// buf->timeout = strtoull (p, &p, 0);
buf->it_real_value = strtoull (p, &p, 0);
buf->start_time = strtoull (p, &p, 0);
/* seconds since epoch */
{
/* Linux provides start_time as clock_t representing
the start of <pid> after boot_time.
Let's use glibtop_get_uptime to get boot_time.
But i'm not sure if this is safe
See libgtop documentation.
#ifdef __KERNEL__
...
*
* Have the 32 bit jiffies value wrap 5 minutes after boot
* so jiffies wrap bugs show up earlier.
*
#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
...
#endif
start_time may be incremented by INITIAL_JIFFIES, so start_time
may be not be exact. You may also get wrong start_time if your
system clock is not synchronised with you hardware clock.
'man hwclock'
*/
glibtop_uptime up;
glibtop_get_uptime_s(server, &up);
buf->start_time = up.boot_time + strtoull (p, &p, 0) / 100;
}
buf->frequency = 100;