Added test for end of string. (get_scaled): Cleaned.

* glibtop_server.h (skip_line): Added test for end of string.
        (get_scaled): Cleaned.

        * cpu.c (glibtop_get_cpu_s):
        * open.c (glibtop_open_s): Fixed SMP detection. Closes #142597.
        * uptime.c (glibtop_get_uptime_s): Added boot_time.
This commit is contained in:
Benoît Dejean
2004-05-25 14:33:46 +00:00
parent f070fea744
commit 0de7c96d2a
5 changed files with 46 additions and 40 deletions

View File

@@ -1,3 +1,13 @@
2004-05-19 Benoit Dejean <TazForEver@dlfp.org>
* glibtop_server.h (skip_line): Added test for end of string.
(get_scaled): Cleaned.
* cpu.c (glibtop_get_cpu_s):
* open.c (glibtop_open_s): Fixed SMP detection. Closes #142597.
* uptime.c (glibtop_get_uptime_s): Added boot_time.
2004-03-09 Bastien Nocera <hadess@hadess.net>
* cpu.c: (glibtop_get_cpu_s): fixup CPU usage calculation on 2.6

View File

@@ -54,8 +54,8 @@ void
glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
{
char buffer [BUFSIZ], *p;
int fd, len, i;
guint64 total;
int fd, len;
guint i;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0);
@@ -73,6 +73,10 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
buffer [len] = '\0';
/*
* GLOBAL
*/
p = skip_token (buffer); /* "cpu" */
buf->user = strtoull (p, &p, 0);
@@ -85,20 +89,24 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
buf->sys += strtoull(p, &p, 0); /* "irq" */
buf->sys += strtoull(p, &p, 0); /* "softirq" */
total = buf->user;
total += buf->nice;
total += buf->sys;
total += buf->idle;
buf->total = total;
buf->total = buf->user + buf->nice + buf->sys + buf->idle;
buf->frequency = 100;
buf->flags = _glibtop_sysdeps_cpu;
for (i = 0; i < server->ncpu; i++) {
if (strncmp (p+1, "cpu", 3) || !isdigit (p [4]))
/*
* PER CPU
*/
for (i = 0; i < GLIBTOP_NCPU && i < server->ncpu; i++) {
p = skip_line(p); /* move to ^ */
if (strncmp (p, "cpu", 3) || !isdigit (p [3]))
break;
p += 6;
p = skip_token(p); /* "cpuN" */
buf->xcpu_user [i] = strtoull (p, &p, 0);
buf->xcpu_nice [i] = strtoull (p, &p, 0);
buf->xcpu_sys [i] = strtoull (p, &p, 0);
@@ -109,13 +117,12 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
buf->xcpu_sys [i] += strtoull(p, &p, 0); /* "irq" */
buf->xcpu_sys [i] += strtoull(p, &p, 0); /* "softirq" */
total = buf->xcpu_user [i];
total += buf->xcpu_nice [i];
total += buf->xcpu_sys [i];
total += buf->xcpu_idle [i];
buf->xcpu_total [i] = total;
buf->xcpu_total[i] = buf->xcpu_user [i] \
+ buf->xcpu_nice [i] \
+ buf->xcpu_sys [i] \
+ buf->xcpu_idle [i];
}
buf->flags |= _glibtop_sysdeps_cpu_smp;
if(i >= 2) /* ok, that's a real SMP */
buf->flags |= _glibtop_sysdeps_cpu_smp;
}

View File

@@ -53,8 +53,8 @@ skip_multiple_token (const char *p, int count)
static inline char *
skip_line (const char *p)
{
while (*p != '\n') p++;
return (char *) ++p;
while (*p && *p != '\n') p++;
return (char *) (*p ? p+1 : p);
}
static inline unsigned long long
@@ -62,11 +62,12 @@ 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 += strlen(key);
ptr += len;
value = strtoull(ptr, &next, 0);
if (strchr(next, 'k'))
value *= 1024;

View File

@@ -61,24 +61,14 @@ glibtop_open_s (glibtop *server, const char *program_name,
const unsigned long features,
const unsigned flags)
{
char buffer [BUFSIZ], *p;
struct stat statb;
int fd, len, i;
char buffer [BUFSIZ], *p = buffer;
int fd, len;
server->name = program_name;
set_linux_version ();
server->os_version_code = (unsigned long) linux_version_code;
server->ncpu = 0;
/* On Linux 2.4.x, /proc/stat has "cpu" and "cpu0" entries even
* for non-SMP systems. Checking whether /proc/<pid>/cpu exists
* is a much better way to detect SMP. */
if (stat ("/proc/1/cpu", &statb))
return;
fd = open (FILENAME, O_RDONLY);
if (fd < 0)
glibtop_error_io_r (server, "open (%s)", FILENAME);
@@ -90,17 +80,13 @@ glibtop_open_s (glibtop *server, const char *program_name,
close (fd);
buffer [len] = '\0';
p = skip_multiple_token (buffer, 5) + 1;
for (i = 0; i < GLIBTOP_NCPU; i++) {
for (server->ncpu = 0; server->ncpu < GLIBTOP_NCPU; server->ncpu++) {
p = skip_line(p);
if (strncmp (p, "cpu", 3) || !isdigit (p [3]))
break;
server->ncpu++;
p = skip_multiple_token (p, 5) + 1;
}
#if DEBUG

View File

@@ -24,6 +24,7 @@
#include <config.h>
#include <glibtop/error.h>
#include <glibtop/uptime.h>
#include <time.h>
static unsigned long _glibtop_sysdeps_uptime =
(1L << GLIBTOP_UPTIME_UPTIME) + (1L << GLIBTOP_UPTIME_IDLETIME);
@@ -64,6 +65,7 @@ glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf)
buf->uptime = strtod (buffer, &p);
buf->idletime = strtod (p, &p);
buf->boot_time = (guint64) time(NULL) - (guint64) buf->uptime;
buf->flags = _glibtop_sysdeps_uptime;
}