diff --git a/plugins/cpu/cpu.c b/plugins/cpu/cpu.c index fef3ca8..e680d40 100644 --- a/plugins/cpu/cpu.c +++ b/plugins/cpu/cpu.c @@ -7,6 +7,7 @@ * and was extended by Eygene Ryabinkin */ +#include #include #include "misc.h" #include "../chart/chart.h" @@ -29,6 +30,7 @@ typedef struct { struct cpu_stat cpu_prev; int timer; gchar *colors[1]; + unsigned int core; } cpu_priv; static chart_class *k; @@ -38,22 +40,36 @@ static void cpu_destructor(plugin_instance *p); #if defined __linux__ static int -cpu_get_load_real(struct cpu_stat *cpu) +cpu_get_load_real(struct cpu_stat *cpu, unsigned int core) { FILE *stat; - + gchar buf[40]; memset(cpu, 0, sizeof(struct cpu_stat)); - stat = fopen("/proc/stat", "r"); - if(!stat) - return -1; - if (fscanf(stat, "cpu %lu %lu %lu %lu %lu", &cpu->u, &cpu->n, &cpu->s, - &cpu->i, &cpu->w)); - fclose(stat); + if (core == 0) + { + stat = fopen("/proc/stat", "r"); + if(!stat) + return -1; + if (fscanf(stat, "cpu %lu %lu %lu %lu %lu", &cpu->u, &cpu->n, &cpu->s, + &cpu->i, &cpu->w)); + fclose(stat); + } + else + { + DBG("buf=%s\n", buf); + sprintf(buf, "%s%u %s", "/bin/grep cpu", core-1, "/proc/stat"); + stat = popen(buf, "r"); + if (!stat) + return -1; + sprintf(buf, "%s%u %s", "cpu", core-1, "%lu %lu %lu %lu %lu"); + if (fscanf(stat, buf, &cpu->u, &cpu->n, &cpu->s,&cpu->i, &cpu->w)); + pclose(stat); + } return 0; } #elif defined __FreeBSD__ static int -cpu_get_load_real(struct cpu_stat *cpu) +cpu_get_load_real(struct cpu_stat *cpu, const unsigned int core) { static int mib[2] = { -1, -1 }, init = 0; size_t j; @@ -81,13 +97,13 @@ cpu_get_load_real(struct cpu_stat *cpu) } #else static int -cpu_get_load_real(struct cpu_stat *s) +cpu_get_load_real(struct cpu_stat *s, const unsigned int core) { memset(cpu, 0, sizeof(struct cpu_stat)); return 0; } #endif - + static int cpu_get_load(cpu_priv *c) { @@ -100,8 +116,8 @@ cpu_get_load(cpu_priv *c) memset(&cpu, 0, sizeof(cpu)); memset(&cpu_diff, 0, sizeof(cpu_diff)); memset(&total, 0, sizeof(total)); - - if (cpu_get_load_real(&cpu)) + + if (cpu_get_load_real(&cpu, c->core)) goto end; cpu_diff.u = cpu.u - c->cpu_prev.u; @@ -117,7 +133,14 @@ cpu_get_load(cpu_priv *c) end: DBG("total=%f a=%f b=%f\n", total[0], a, b); - g_snprintf(buf, sizeof(buf), "Cpu: %d%%", (int)(total[0] * 100)); + if (c->core == 0) + { + g_snprintf(buf, sizeof(buf), "Cpu total: %d%%", (int)(total[0] * 100)); + } + else + { + g_snprintf(buf, sizeof(buf), "Cpu's core %u: %d%%", c->core,(int)(total[0] * 100)); + } gtk_widget_set_tooltip_markup(((plugin_instance *)c)->pwid, buf); k->add_tick(&c->chart, total); RET(TRUE); @@ -136,9 +159,11 @@ cpu_constructor(plugin_instance *p) c = (cpu_priv *) p; c->colors[0] = "green"; XCG(p->xc, "Color", &c->colors[0], str); - + XCG(p->xc, "Core", (int*) &c->core, int); + if (c->core > sysconf(_SC_NPROCESSORS_CONF)) + c->core = sysconf(_SC_NPROCESSORS_CONF); k->set_rows(&c->chart, 1, c->colors); - gtk_widget_set_tooltip_markup(((plugin_instance *)c)->pwid, "Cpu"); + //gtk_widget_set_tooltip_markup(((plugin_instance *)c)->pwid, "Cpu"); cpu_get_load(c); c->timer = g_timeout_add(1000, (GSourceFunc) cpu_get_load, (gpointer) c); RET(1);