Fixed server->ncpu usage (SMP handling). Need more testing.

* cpu.c: (glibtop_get_cpu_s):
	* glibtop_private.c: (get_scaled), (check_cpu_line):
	* glibtop_private.h:
	* open.c: (glibtop_open_s):
	* proctime.c: (glibtop_get_proc_time_s):

	Fixed server->ncpu usage (SMP handling). Need more testing.
This commit is contained in:
Benoît Dejean
2005-12-13 09:15:49 +00:00
parent ba7355df9e
commit 806a816026
6 changed files with 64 additions and 12 deletions

View File

@@ -1,3 +1,13 @@
2005-12-13 Benoît Dejean <benoit@placenet.org>
* cpu.c: (glibtop_get_cpu_s):
* glibtop_private.c: (get_scaled), (check_cpu_line):
* glibtop_private.h:
* open.c: (glibtop_open_s):
* proctime.c: (glibtop_get_proc_time_s):
Fixed server->ncpu usage (SMP handling). Need more testing.
2005-12-12 Benoît Dejean <benoit@placenet.org> 2005-12-12 Benoît Dejean <benoit@placenet.org>
* procmap.c: (add_smaps), (glibtop_get_proc_map_s): * procmap.c: (add_smaps), (glibtop_get_proc_map_s):

View File

@@ -110,11 +110,11 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
* PER CPU * PER CPU
*/ */
for (i = 0; i < GLIBTOP_NCPU && i < server->ncpu; i++) { for (i = 0; i <= server->ncpu; i++) {
p = skip_line(p); /* move to ^ */ p = skip_line(p); /* move to ^ */
if (strncmp (p, "cpu", 3) || !isdigit (p [3])) if (!check_cpu_line_warn(server, p, i))
break; break;
p = skip_token(p); /* "cpuN" */ p = skip_token(p); /* "cpuN" */
@@ -141,14 +141,14 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
} }
} }
if(i >= 2) /* ok, that's a real SMP */ if(server->ncpu) /* ok, that's a real SMP */
buf->flags |= _glibtop_sysdeps_cpu_smp; buf->flags |= _glibtop_sysdeps_cpu_smp;
if(server->os_version_code >= LINUX_VERSION_CODE(2, 6, 0)) if(server->os_version_code >= LINUX_VERSION_CODE(2, 6, 0))
{ {
buf->flags |= _glibtop_sysdeps_cpu_2_6; buf->flags |= _glibtop_sysdeps_cpu_2_6;
if(i >= 2) /* ok, that's a real SMP */ if(server->ncpu) /* ok, that's a real SMP */
buf->flags |= _glibtop_sysdeps_cpu_smp_2_6; buf->flags |= _glibtop_sysdeps_cpu_smp_2_6;
} }
} }

View File

@@ -21,7 +21,7 @@ get_scaled(const char *buffer, const char *key)
char *next; char *next;
unsigned long long value = 0; unsigned long long value = 0;
if ((ptr = strstr(buffer, key))) if (G_LIKELY((ptr = strstr(buffer, key))))
{ {
ptr += strlen(key); ptr += strlen(key);
value = strtoull(ptr, &next, 0); value = strtoull(ptr, &next, 0);
@@ -29,7 +29,10 @@ get_scaled(const char *buffer, const char *key)
value *= 1024; value *= 1024;
else if (strchr(next, 'M')) else if (strchr(next, 'M'))
value *= 1024 * 1024; value *= 1024 * 1024;
} } else
g_warning("Could not read key '%s' in buffer '%s'",
key, buffer);
return value; return value;
} }
@@ -147,3 +150,16 @@ get_page_size(void)
return pagesize; return pagesize;
} }
gboolean
check_cpu_line(glibtop *server, const char *line, unsigned i)
{
char start[10];
g_snprintf(start, sizeof start, "cpu%u", i);
return g_str_has_prefix(line, start);
}

View File

@@ -21,6 +21,7 @@
#define __LINUX__GLIBTOP_PRIVATE_H__ #define __LINUX__GLIBTOP_PRIVATE_H__
#include <glibtop.h> #include <glibtop.h>
#include <glibtop/error.h>
#include <glib.h> #include <glib.h>
@@ -121,6 +122,26 @@ size_t
get_page_size(void) G_GNUC_INTERNAL; get_page_size(void) G_GNUC_INTERNAL;
gboolean
check_cpu_line(glibtop *server, const char *line, unsigned n) G_GNUC_INTERNAL;
static inline gboolean
check_cpu_line_warn(glibtop *server, const char *line, unsigned i)
{
gboolean ret;
ret = check_cpu_line(server, line, i);
if (G_UNLIKELY(!ret))
glibtop_warn_io_r(server,
"'%s' does not start with 'cpu%u'",
line, i);
return ret;
}
G_END_DECLS G_END_DECLS
#endif /* __LINUX__GLIBTOP_PRIVATE_H__ */ #endif /* __LINUX__GLIBTOP_PRIVATE_H__ */

View File

@@ -77,14 +77,18 @@ glibtop_open_s (glibtop *server, const char *program_name,
file_to_buffer(server, buffer, FILENAME); file_to_buffer(server, buffer, FILENAME);
p = skip_line(p); /* cpu */
for (server->ncpu = 0; server->ncpu < GLIBTOP_NCPU; server->ncpu++) { for (server->ncpu = 0; server->ncpu < GLIBTOP_NCPU; server->ncpu++) {
p = skip_line(p); if (!check_cpu_line(server, p, server->ncpu)) {
server->ncpu--;
if (strncmp (p, "cpu", 3) || !isdigit (p [3]))
break; break;
} }
p = skip_line(p);
}
#ifdef DEBUG #ifdef DEBUG
printf ("\nThis machine has %d CPUs.\n\n", server->ncpu); printf ("\nThis machine has %d CPUs.\n\n", server->ncpu);
#endif #endif

View File

@@ -122,11 +122,12 @@ glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid)
p = skip_multiple_token (p, 3); p = skip_multiple_token (p, 3);
for (i = 0; i < GLIBTOP_NCPU; i++) { for (i = 0; i <= server->ncpu; i++) {
if (strncmp (p+1, "cpu", 3) || !isdigit (p [4]))
if (!check_cpu_line_warn(server, p + 1, i))
break; break;
p += 6; p = skip_token(p);
buf->xcpu_utime [i] = strtoull (p, &p, 0); buf->xcpu_utime [i] = strtoull (p, &p, 0);
buf->xcpu_stime [i] = strtoull (p, &p, 0); buf->xcpu_stime [i] = strtoull (p, &p, 0);
} }