2 Copyright (C) 2010-2011 jeanfi@gmail.com
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #define _(str) gettext(str)
26 #include <glibtop/cpu.h>
30 static glibtop_cpu *cpu;
31 static float last_used;
32 static float last_total;
34 struct psensor *create_cpu_usage_sensor(int measures_len)
39 struct psensor *psensor;
41 id = strdup("cpu usage");
42 label = strdup("cpu usage");
43 type = SENSOR_TYPE_CPU_USAGE;
45 psensor = psensor_create(id, label, type, measures_len);
51 cpu_psensor_list_add(struct psensor **sensors, int measures_len)
55 s = create_cpu_usage_sensor(measures_len);
57 return psensor_list_add(sensors, s);
60 static double get_usage()
62 unsigned long int used = 0;
64 double cpu_rate = UNKNOWN_DBL_VALUE;
67 cpu = malloc(sizeof(glibtop_cpu));
71 used = cpu->user + cpu->nice + cpu->sys;
73 dt = cpu->total - last_total;
76 cpu_rate = 100 * (used - last_used) / dt;
79 last_total = cpu->total;
84 void cpu_usage_sensor_update(struct psensor *s)
86 psensor_set_current_value(s, get_usage());
89 void cpu_psensor_list_update(struct psensor **sensors)
91 struct psensor **ss, *s;
97 if (s->type == SENSOR_TYPE_CPU_USAGE)
98 cpu_usage_sensor_update(s);