Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions plugins/cpu/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* and was extended by Eygene Ryabinkin <rea-fbsd@codelabs.ru>
*/

#include <unistd.h>
#include <string.h>
#include "misc.h"
#include "../chart/chart.h"
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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), "<b>Cpu:</b> %d%%", (int)(total[0] * 100));
if (c->core == 0)
{
g_snprintf(buf, sizeof(buf), "<b>Cpu total:</b> %d%%", (int)(total[0] * 100));
}
else
{
g_snprintf(buf, sizeof(buf), "<b>Cpu's core %u:</b> %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);
Expand All @@ -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, "<b>Cpu</b>");
//gtk_widget_set_tooltip_markup(((plugin_instance *)c)->pwid, "<b>Cpu</b>");
cpu_get_load(c);
c->timer = g_timeout_add(1000, (GSourceFunc) cpu_get_load, (gpointer) c);
RET(1);
Expand Down