gallium/hud: fix a problem where objects are free'd while in use.
[mesa.git] / src / gallium / auxiliary / hud / hud_sensors_temp.c
1 /**************************************************************************
2 *
3 * Copyright (C) 2016 Steven Toth <stoth@kernellabs.com>
4 * Copyright (C) 2016 Zodiac Inflight Innovations
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #if HAVE_LIBSENSORS
30 /* Purpose: Extract lm-sensors data, expose temperature, power, voltage. */
31
32 #include "hud/hud_private.h"
33 #include "util/list.h"
34 #include "os/os_time.h"
35 #include "util/u_memory.h"
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <dirent.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <inttypes.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45 #include <sensors/sensors.h>
46
47 /* TODO: We don't handle dynamic sensor discovery / arrival or removal.
48 * Static globals specific to this HUD category.
49 */
50 static int gsensors_temp_count = 0;
51 static struct list_head gsensors_temp_list;
52
53 struct sensors_temp_info
54 {
55 struct list_head list;
56
57 /* Combined chip and feature name, human readable. */
58 char name[64];
59
60 /* The type of measurement, critical or current. */
61 unsigned int mode;
62
63 uint64_t last_time;
64
65 char chipname[64];
66 char featurename[128];
67
68 sensors_chip_name *chip;
69 const sensors_feature *feature;
70 double current, min, max, critical;
71 };
72
73 static double
74 get_value(const sensors_chip_name *name, const sensors_subfeature *sub)
75 {
76 double val;
77 int err;
78
79 err = sensors_get_value(name, sub->number, &val);
80 if (err) {
81 fprintf(stderr, "ERROR: Can't get value of subfeature %s\n", sub->name);
82 val = 0;
83 }
84 return val;
85 }
86
87 static void
88 get_sensor_values(struct sensors_temp_info *sti)
89 {
90 const sensors_subfeature *sf;
91
92 switch(sti->mode) {
93 case SENSORS_VOLTAGE_CURRENT:
94 sf = sensors_get_subfeature(sti->chip, sti->feature,
95 SENSORS_SUBFEATURE_IN_INPUT);
96 if (sf)
97 sti->current = get_value(sti->chip, sf);
98 break;
99 case SENSORS_CURRENT_CURRENT:
100 sf = sensors_get_subfeature(sti->chip, sti->feature,
101 SENSORS_SUBFEATURE_CURR_INPUT);
102 if (sf) {
103 /* Sensors API returns in AMPs, even though driver is reporting mA,
104 * convert back to mA */
105 sti->current = get_value(sti->chip, sf) * 1000;
106 }
107 break;
108 case SENSORS_TEMP_CURRENT:
109 sf = sensors_get_subfeature(sti->chip, sti->feature,
110 SENSORS_SUBFEATURE_TEMP_INPUT);
111 if (sf)
112 sti->current = get_value(sti->chip, sf);
113 break;
114 case SENSORS_TEMP_CRITICAL:
115 sf = sensors_get_subfeature(sti->chip, sti->feature,
116 SENSORS_SUBFEATURE_TEMP_CRIT);
117 if (sf)
118 sti->critical = get_value(sti->chip, sf);
119 break;
120 case SENSORS_POWER_CURRENT:
121 sf = sensors_get_subfeature(sti->chip, sti->feature,
122 SENSORS_SUBFEATURE_POWER_INPUT);
123 if (sf) {
124 /* Sensors API returns in WATTs, even though driver is reporting mW,
125 * convert back to mW */
126 sti->current = get_value(sti->chip, sf) * 1000;
127 }
128 break;
129 }
130
131 sf = sensors_get_subfeature(sti->chip, sti->feature,
132 SENSORS_SUBFEATURE_TEMP_MIN);
133 if (sf)
134 sti->min = get_value(sti->chip, sf);
135
136 sf = sensors_get_subfeature(sti->chip, sti->feature,
137 SENSORS_SUBFEATURE_TEMP_MAX);
138 if (sf)
139 sti->max = get_value(sti->chip, sf);
140 }
141
142 static struct sensors_temp_info *
143 find_sti_by_name(const char *n, unsigned int mode)
144 {
145 list_for_each_entry(struct sensors_temp_info, sti, &gsensors_temp_list, list) {
146 if (sti->mode != mode)
147 continue;
148 if (strcasecmp(sti->name, n) == 0)
149 return sti;
150 }
151 return 0;
152 }
153
154 static void
155 query_sti_load(struct hud_graph *gr)
156 {
157 struct sensors_temp_info *sti = gr->query_data;
158 uint64_t now = os_time_get();
159
160 if (sti->last_time) {
161 if (sti->last_time + gr->pane->period <= now) {
162 get_sensor_values(sti);
163
164 switch (sti->mode) {
165 case SENSORS_TEMP_CURRENT:
166 hud_graph_add_value(gr, (uint64_t) sti->current);
167 break;
168 case SENSORS_TEMP_CRITICAL:
169 hud_graph_add_value(gr, (uint64_t) sti->critical);
170 break;
171 case SENSORS_VOLTAGE_CURRENT:
172 hud_graph_add_value(gr, (uint64_t)(sti->current * 1000));
173 break;
174 case SENSORS_CURRENT_CURRENT:
175 hud_graph_add_value(gr, (uint64_t) sti->current);
176 break;
177 case SENSORS_POWER_CURRENT:
178 hud_graph_add_value(gr, (uint64_t) sti->current);
179 break;
180 }
181
182 sti->last_time = now;
183 }
184 }
185 else {
186 /* initialize */
187 get_sensor_values(sti);
188 sti->last_time = now;
189 }
190 }
191
192 /**
193 * Create and initialize a new object for a specific sensor interface dev.
194 * \param pane parent context.
195 * \param dev_name device name, EG. 'coretemp-isa-0000.Core 1'
196 * \param mode query type (NIC_DIRECTION_RX/WR/RSSI) statistics.
197 */
198 void
199 hud_sensors_temp_graph_install(struct hud_pane *pane, const char *dev_name,
200 unsigned int mode)
201 {
202 struct hud_graph *gr;
203 struct sensors_temp_info *sti;
204
205 int num_devs = hud_get_num_sensors(0);
206 if (num_devs <= 0)
207 return;
208
209 sti = find_sti_by_name(dev_name, mode);
210 if (!sti)
211 return;
212
213 gr = CALLOC_STRUCT(hud_graph);
214 if (!gr)
215 return;
216
217 snprintf(gr->name, sizeof(gr->name), "%.6s..%s (%s)",
218 sti->chipname,
219 sti->featurename,
220 sti->mode == SENSORS_VOLTAGE_CURRENT ? "Volts" :
221 sti->mode == SENSORS_CURRENT_CURRENT ? "Amps" :
222 sti->mode == SENSORS_TEMP_CURRENT ? "Curr" :
223 sti->mode == SENSORS_POWER_CURRENT ? "Pow" :
224 sti->mode == SENSORS_TEMP_CRITICAL ? "Crit" : "Unkn");
225
226 gr->query_data = sti;
227 gr->query_new_value = query_sti_load;
228
229 hud_pane_add_graph(pane, gr);
230 switch (sti->mode) {
231 case SENSORS_TEMP_CURRENT:
232 case SENSORS_TEMP_CRITICAL:
233 hud_pane_set_max_value(pane, 120);
234 break;
235 case SENSORS_VOLTAGE_CURRENT:
236 hud_pane_set_max_value(pane, 12);
237 break;
238 case SENSORS_CURRENT_CURRENT:
239 hud_pane_set_max_value(pane, 5000);
240 break;
241 case SENSORS_POWER_CURRENT:
242 hud_pane_set_max_value(pane, 5000 /* mW */);
243 break;
244 }
245 }
246
247 static void
248 create_object(const char *chipname, const char *featurename,
249 const sensors_chip_name *chip, const sensors_feature *feature,
250 int mode)
251 {
252 struct sensors_temp_info *sti = CALLOC_STRUCT(sensors_temp_info);
253
254 sti->mode = mode;
255 sti->chip = (sensors_chip_name *) chip;
256 sti->feature = feature;
257 strcpy(sti->chipname, chipname);
258 strcpy(sti->featurename, featurename);
259 snprintf(sti->name, sizeof(sti->name), "%s.%s", sti->chipname,
260 sti->featurename);
261
262 list_addtail(&sti->list, &gsensors_temp_list);
263 gsensors_temp_count++;
264 }
265
266 static void
267 build_sensor_list(void)
268 {
269 const sensors_chip_name *chip;
270 const sensors_chip_name *match = 0;
271 const sensors_feature *feature;
272 int chip_nr = 0;
273
274 char name[256];
275 while ((chip = sensors_get_detected_chips(match, &chip_nr))) {
276 sensors_snprintf_chip_name(name, sizeof(name), chip);
277
278 /* Get all features and filter accordingly. */
279 int fnr = 0;
280 while ((feature = sensors_get_features(chip, &fnr))) {
281 char *featurename = sensors_get_label(chip, feature);
282 if (!featurename)
283 continue;
284
285 /* Create a 'current' and 'critical' object pair.
286 * Ignore sensor if its not temperature based.
287 */
288 switch(feature->type) {
289 case SENSORS_FEATURE_TEMP:
290 create_object(name, featurename, chip, feature,
291 SENSORS_TEMP_CURRENT);
292 create_object(name, featurename, chip, feature,
293 SENSORS_TEMP_CRITICAL);
294 break;
295 case SENSORS_FEATURE_IN:
296 create_object(name, featurename, chip, feature,
297 SENSORS_VOLTAGE_CURRENT);
298 break;
299 case SENSORS_FEATURE_CURR:
300 create_object(name, featurename, chip, feature,
301 SENSORS_CURRENT_CURRENT);
302 break;
303 case SENSORS_FEATURE_POWER:
304 create_object(name, featurename, chip, feature,
305 SENSORS_POWER_CURRENT);
306 break;
307 default:
308 break;
309 }
310 free(featurename);
311 }
312 }
313 }
314
315 /**
316 * Initialize internal object arrays and display lmsensors HUD help.
317 * \param displayhelp true if the list of detected devices should be
318 displayed on the console.
319 * \return number of detected lmsensor devices.
320 */
321 int
322 hud_get_num_sensors(bool displayhelp)
323 {
324 /* Return the number of sensors detected. */
325 if (gsensors_temp_count)
326 return gsensors_temp_count;
327
328 int ret = sensors_init(NULL);
329 if (ret)
330 return 0;
331
332 list_inithead(&gsensors_temp_list);
333
334 /* Scan /sys/block, for every object type we support, create and
335 * persist an object to represent its different statistics.
336 */
337 build_sensor_list();
338
339 if (displayhelp) {
340 list_for_each_entry(struct sensors_temp_info, sti, &gsensors_temp_list, list) {
341 char line[64];
342 switch (sti->mode) {
343 case SENSORS_TEMP_CURRENT:
344 snprintf(line, sizeof(line), " sensors_temp_cu-%s", sti->name);
345 break;
346 case SENSORS_TEMP_CRITICAL:
347 snprintf(line, sizeof(line), " sensors_temp_cr-%s", sti->name);
348 break;
349 case SENSORS_VOLTAGE_CURRENT:
350 snprintf(line, sizeof(line), " sensors_volt_cu-%s", sti->name);
351 break;
352 case SENSORS_CURRENT_CURRENT:
353 snprintf(line, sizeof(line), " sensors_curr_cu-%s", sti->name);
354 break;
355 case SENSORS_POWER_CURRENT:
356 snprintf(line, sizeof(line), " sensors_pow_cu-%s", sti->name);
357 break;
358 }
359
360 puts(line);
361 }
362 }
363
364 return gsensors_temp_count;
365 }
366
367 #endif /* HAVE_LIBSENSORS */