X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fcpu.c;h=8f377232b07f2f7420ae11076d6c383ae64082d9;hb=6e24120564e3ea12e9d9a5d19e96947186491727;hp=18bf6c4358ccc72fcad4aa4a2520c1264122e28c;hpb=45c8086f97097df36ba8cd0552f152be80f79b12;p=psensor.git diff --git a/src/lib/cpu.c b/src/lib/cpu.c index 18bf6c4..8f37723 100644 --- a/src/lib/cpu.c +++ b/src/lib/cpu.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2011 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,7 +26,6 @@ #include "cpu.h" -static glibtop_cpu *cpu; static float last_used; static float last_total; @@ -39,9 +38,13 @@ struct psensor *create_cpu_usage_sensor(int measures_len) 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 +61,22 @@ cpu_psensor_list_add(struct psensor **sensors, int measures_len) static double get_usage() { + glibtop_cpu cpu; unsigned long int used = 0; unsigned long int dt; double cpu_rate = UNKNOWN_DBL_VALUE; - 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; last_used = used; - last_total = cpu->total; + last_total = cpu.total; return cpu_rate; } @@ -93,15 +94,10 @@ void cpu_psensor_list_update(struct psensor **sensors) while (*ss) { s = *ss; - 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++; } } - -void cpu_cleanup() -{ - if (cpu) - free(cpu); -}