new release 1.2.0
[psensor-pkg-debian.git] / src / lib / pgtop2.c
1 /*
2  * Copyright (C) 2010-2016 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 #include <glibtop/mem.h>
27
28 #include <pgtop2.h>
29
30 static float last_used;
31 static float last_total;
32
33 static const char *PROVIDER_NAME = "gtop2";
34
35 struct psensor *create_cpu_usage_sensor(int measures_len)
36 {
37         char *label, *id;
38         int type;
39         struct psensor *psensor;
40
41         id = g_strdup_printf("%s cpu usage", PROVIDER_NAME);
42         label = strdup(_("CPU usage"));
43         type = SENSOR_TYPE_GTOP | SENSOR_TYPE_CPU_USAGE;
44
45         psensor = psensor_create(id,
46                                  label,
47                                  strdup(_("CPU")),
48                                  type,
49                                  measures_len);
50
51         return psensor;
52 }
53
54 static struct psensor *create_mem_free_sensor(int measures_len)
55 {
56         char *id;
57         int type;
58
59         id = g_strdup_printf("%s mem free", PROVIDER_NAME);
60         type = SENSOR_TYPE_GTOP | SENSOR_TYPE_MEMORY | SENSOR_TYPE_PERCENT;
61
62         return psensor_create(id,
63                               strdup(_("free memory")),
64                               strdup(_("memory")),
65                               type,
66                               measures_len);
67 }
68
69 static double get_usage(void)
70 {
71         glibtop_cpu cpu;
72         unsigned long int used, dt;
73         double cpu_rate;
74
75         glibtop_get_cpu(&cpu);
76
77         used = cpu.user + cpu.nice + cpu.sys;
78
79         dt = cpu.total - last_total;
80
81         if (dt)
82                 cpu_rate = 100.0 * (used - last_used) / dt;
83         else
84                 cpu_rate = UNKNOWN_DBL_VALUE;
85
86         last_used = used;
87         last_total = cpu.total;
88
89         return cpu_rate;
90 }
91
92 static double get_mem_free(void)
93 {
94         glibtop_mem mem;
95         double v;
96
97         glibtop_get_mem(&mem);
98         v = ((double)mem.free) * 100.0 / mem.total;
99
100         return v;
101 }
102
103 void gtop2_psensor_list_append(struct psensor ***sensors, int measures_len)
104 {
105         psensor_list_append(sensors, create_cpu_usage_sensor(measures_len));
106         psensor_list_append(sensors, create_mem_free_sensor(measures_len));
107 }
108
109 void cpu_usage_sensor_update(struct psensor *s)
110 {
111         double v;
112
113         v = get_usage();
114
115         if (v != UNKNOWN_DBL_VALUE)
116                 psensor_set_current_value(s, v);
117 }
118
119 static void mem_free_sensor_update(struct psensor *s)
120 {
121         double v;
122
123         v = get_mem_free();
124
125         if (v != UNKNOWN_DBL_VALUE)
126                 psensor_set_current_value(s, v);
127 }
128
129 void gtop2_psensor_list_update(struct psensor **sensors)
130 {
131         struct psensor *s;
132
133         while (*sensors) {
134                 s = *sensors;
135
136                 if (!(s->type & SENSOR_TYPE_REMOTE)
137                     && s->type & SENSOR_TYPE_GTOP) {
138                         if (s->type & SENSOR_TYPE_CPU)
139                                 cpu_usage_sensor_update(s);
140                         else if (s->type & SENSOR_TYPE_MEMORY)
141                                 mem_free_sensor_update(s);
142                 }
143
144                 sensors++;
145         }
146 }