Imported Upstream version 1.1.1
[psensor-pkg-ubuntu.git] / src / lib / cpu.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #include <locale.h>
20 #include <libintl.h>
21 #define _(str) gettext(str)
22
23 #include <string.h>
24
25 #include <glibtop/cpu.h>
26
27 #include "cpu.h"
28
29 static glibtop_cpu *cpu;
30 static float last_used;
31 static float last_total;
32
33 struct psensor *create_cpu_usage_sensor(int measures_len)
34 {
35         char *label;
36         int type;
37         char *id;
38         struct psensor *psensor;
39
40         id = strdup("cpu usage");
41         label = strdup("cpu usage");
42         type = SENSOR_TYPE_CPU_USAGE;
43
44         psensor = psensor_create(id,
45                                  label,
46                                  strdup(_("CPU")),
47                                  type,
48                                  measures_len);
49
50         return psensor;
51 }
52
53 struct psensor * *
54 cpu_psensor_list_add(struct psensor **sensors, int measures_len)
55 {
56         struct psensor *s;
57
58         s = create_cpu_usage_sensor(measures_len);
59
60         return psensor_list_add(sensors, s);
61 }
62
63 static double get_usage()
64 {
65         unsigned long int used = 0;
66         unsigned long int dt;
67         double cpu_rate = UNKNOWN_DBL_VALUE;
68
69         if (!cpu)
70                 cpu = malloc(sizeof(glibtop_cpu));
71
72         glibtop_get_cpu(cpu);
73
74         used = cpu->user + cpu->nice + cpu->sys;
75
76         dt = cpu->total - last_total;
77
78         if (dt)
79                 cpu_rate = 100 * (used - last_used) / dt;
80
81         last_used = used;
82         last_total = cpu->total;
83
84         return cpu_rate;
85 }
86
87 void cpu_usage_sensor_update(struct psensor *s)
88 {
89         psensor_set_current_value(s, get_usage());
90 }
91
92 void cpu_psensor_list_update(struct psensor **sensors)
93 {
94         struct psensor **ss, *s;
95
96         ss = sensors;
97         while (*ss) {
98                 s = *ss;
99
100                 if (!(s->type & SENSOR_TYPE_REMOTE)
101                     && s->type == SENSOR_TYPE_CPU_USAGE)
102                         cpu_usage_sensor_update(s);
103
104                 ss++;
105         }
106 }
107
108 void cpu_cleanup()
109 {
110         if (cpu)
111                 free(cpu);
112 }