2 * Copyright (C) 2010-2014 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * 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
21 #define _(str) gettext(str)
25 #include <glibtop/cpu.h>
29 static float last_used;
30 static float last_total;
32 struct psensor *create_cpu_usage_sensor(int measures_len)
36 struct psensor *psensor;
38 id = strdup("cpu usage");
39 label = strdup("cpu usage");
40 type = SENSOR_TYPE_GTOP | SENSOR_TYPE_CPU_USAGE;
42 psensor = psensor_create(id,
52 cpu_psensor_list_add(struct psensor **sensors, int measures_len)
56 s = create_cpu_usage_sensor(measures_len);
58 return psensor_list_add(sensors, s);
61 static double get_usage()
64 unsigned long int used;
68 glibtop_get_cpu(&cpu);
70 used = cpu.user + cpu.nice + cpu.sys;
72 dt = cpu.total - last_total;
75 cpu_rate = 100.0 * (used - last_used) / dt;
77 cpu_rate = UNKNOWN_DBL_VALUE;
80 last_total = cpu.total;
85 void cpu_usage_sensor_update(struct psensor *s)
87 psensor_set_current_value(s, get_usage());
90 void cpu_psensor_list_update(struct psensor **sensors)
97 if (s->type & SENSOR_TYPE_GTOP
98 && s->type & SENSOR_TYPE_CPU_USAGE)
99 cpu_usage_sensor_update(s);