X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fcpu.c;h=d3fa7eed9776d9d0e34889605375214f2ddc70fd;hb=7f22439c2674661c7295630b3cdb5c8cd3588e61;hp=66c6df92380c57ae5d98f3633d1a5ea838c441d7;hpb=2b51051578ef29b031d0927388c4d62baa3c525e;p=psensor.git diff --git a/src/lib/cpu.c b/src/lib/cpu.c index 66c6df9..d3fa7ee 100644 --- a/src/lib/cpu.c +++ b/src/lib/cpu.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2012 jeanfi@gmail.com + * Copyright (C) 2010-2014 jeanfi@gmail.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -26,22 +26,24 @@ #include "cpu.h" -static glibtop_cpu *cpu; static float last_used; static float last_total; struct psensor *create_cpu_usage_sensor(int measures_len) { - char *label; + char *label, *id; int type; - char *id; struct psensor *psensor; id = strdup("cpu usage"); label = strdup("cpu usage"); - type = SENSOR_TYPE_CPU_USAGE; + type = SENSOR_TYPE_GTOP | SENSOR_TYPE_CPU_USAGE; - psensor = psensor_create(id, label, type, measures_len); + psensor = psensor_create(id, + label, + strdup(_("CPU")), + type, + measures_len); return psensor; } @@ -58,24 +60,24 @@ cpu_psensor_list_add(struct psensor **sensors, int measures_len) static double get_usage() { - unsigned long int used = 0; + glibtop_cpu cpu; + unsigned long int used; unsigned long int dt; - double cpu_rate = UNKNOWN_DBL_VALUE; + double cpu_rate; - if (!cpu) - cpu = malloc(sizeof(glibtop_cpu)); + glibtop_get_cpu(&cpu); - glibtop_get_cpu(cpu); + used = cpu.user + cpu.nice + cpu.sys; - used = cpu->user + cpu->nice + cpu->sys; - - dt = cpu->total - last_total; + dt = cpu.total - last_total; if (dt) - cpu_rate = 100 * (used - last_used) / dt; + cpu_rate = 100.0 * (used - last_used) / dt; + else + cpu_rate = UNKNOWN_DBL_VALUE; last_used = used; - last_total = cpu->total; + last_total = cpu.total; return cpu_rate; } @@ -87,21 +89,15 @@ void cpu_usage_sensor_update(struct psensor *s) void cpu_psensor_list_update(struct psensor **sensors) { - struct psensor **ss, *s; + struct psensor *s; - ss = sensors; - while (*ss) { - s = *ss; + while (*sensors) { + s = *sensors; - if (s->type == SENSOR_TYPE_CPU_USAGE) + if (s->type & SENSOR_TYPE_GTOP + && s->type & SENSOR_TYPE_CPU_USAGE) cpu_usage_sensor_update(s); - ss++; + sensors++; } } - -void cpu_cleanup() -{ - if (cpu) - free(cpu); -}