1 /**************************************************************************
3 * Copyright (C) 2016 Steven Toth <stoth@kernellabs.com>
4 * Copyright (C) 2016 Zodiac Inflight Innovations
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
30 /* Purpose: Extract lm-sensors data, expose temperature, power, voltage. */
32 #include "hud/hud_private.h"
33 #include "util/list.h"
34 #include "os/os_time.h"
35 #include "util/u_memory.h"
42 #include <sys/types.h>
45 #include <sensors/sensors.h>
47 /* TODO: We don't handle dynamic sensor discovery / arrival or removal.
48 * Static globals specific to this HUD category.
50 static int gsensors_temp_count
= 0;
51 static struct list_head gsensors_temp_list
;
53 struct sensors_temp_info
55 struct list_head list
;
57 /* Combined chip and feature name, human readable. */
60 /* The type of measurement, critical or current. */
66 char featurename
[128];
68 sensors_chip_name
*chip
;
69 const sensors_feature
*feature
;
70 double current
, min
, max
, critical
;
74 get_value(const sensors_chip_name
*name
, const sensors_subfeature
*sub
)
79 err
= sensors_get_value(name
, sub
->number
, &val
);
81 fprintf(stderr
, "ERROR: Can't get value of subfeature %s\n", sub
->name
);
88 get_sensor_values(struct sensors_temp_info
*sti
)
90 const sensors_subfeature
*sf
;
93 case SENSORS_VOLTAGE_CURRENT
:
94 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
95 SENSORS_SUBFEATURE_IN_INPUT
);
97 sti
->current
= get_value(sti
->chip
, sf
);
99 case SENSORS_CURRENT_CURRENT
:
100 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
101 SENSORS_SUBFEATURE_CURR_INPUT
);
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;
108 case SENSORS_TEMP_CURRENT
:
109 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
110 SENSORS_SUBFEATURE_TEMP_INPUT
);
112 sti
->current
= get_value(sti
->chip
, sf
);
114 case SENSORS_TEMP_CRITICAL
:
115 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
116 SENSORS_SUBFEATURE_TEMP_CRIT
);
118 sti
->critical
= get_value(sti
->chip
, sf
);
120 case SENSORS_POWER_CURRENT
:
121 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
122 SENSORS_SUBFEATURE_POWER_INPUT
);
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;
131 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
132 SENSORS_SUBFEATURE_TEMP_MIN
);
134 sti
->min
= get_value(sti
->chip
, sf
);
136 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
137 SENSORS_SUBFEATURE_TEMP_MAX
);
139 sti
->max
= get_value(sti
->chip
, sf
);
142 static struct sensors_temp_info
*
143 find_sti_by_name(const char *n
, unsigned int mode
)
145 list_for_each_entry(struct sensors_temp_info
, sti
, &gsensors_temp_list
, list
) {
146 if (sti
->mode
!= mode
)
148 if (strcasecmp(sti
->name
, n
) == 0)
155 query_sti_load(struct hud_graph
*gr
)
157 struct sensors_temp_info
*sti
= gr
->query_data
;
158 uint64_t now
= os_time_get();
160 if (sti
->last_time
) {
161 if (sti
->last_time
+ gr
->pane
->period
<= now
) {
162 get_sensor_values(sti
);
165 case SENSORS_TEMP_CURRENT
:
166 hud_graph_add_value(gr
, (uint64_t) sti
->current
);
168 case SENSORS_TEMP_CRITICAL
:
169 hud_graph_add_value(gr
, (uint64_t) sti
->critical
);
171 case SENSORS_VOLTAGE_CURRENT
:
172 hud_graph_add_value(gr
, (uint64_t)(sti
->current
* 1000));
174 case SENSORS_CURRENT_CURRENT
:
175 hud_graph_add_value(gr
, (uint64_t) sti
->current
);
177 case SENSORS_POWER_CURRENT
:
178 hud_graph_add_value(gr
, (uint64_t) sti
->current
);
182 sti
->last_time
= now
;
187 get_sensor_values(sti
);
188 sti
->last_time
= now
;
193 free_query_data(void *p
)
195 struct sensors_temp_info
*sti
= (struct sensors_temp_info
*) p
;
196 list_del(&sti
->list
);
198 sensors_free_chip_name(sti
->chip
);
204 * Create and initialize a new object for a specific sensor interface dev.
205 * \param pane parent context.
206 * \param dev_name device name, EG. 'coretemp-isa-0000.Core 1'
207 * \param mode query type (NIC_DIRECTION_RX/WR/RSSI) statistics.
210 hud_sensors_temp_graph_install(struct hud_pane
*pane
, const char *dev_name
,
213 struct hud_graph
*gr
;
214 struct sensors_temp_info
*sti
;
216 int num_devs
= hud_get_num_sensors(0);
220 sti
= find_sti_by_name(dev_name
, mode
);
224 gr
= CALLOC_STRUCT(hud_graph
);
228 snprintf(gr
->name
, sizeof(gr
->name
), "%.6s..%s (%s)",
231 sti
->mode
== SENSORS_VOLTAGE_CURRENT
? "Volts" :
232 sti
->mode
== SENSORS_CURRENT_CURRENT
? "Amps" :
233 sti
->mode
== SENSORS_TEMP_CURRENT
? "Curr" :
234 sti
->mode
== SENSORS_POWER_CURRENT
? "Pow" :
235 sti
->mode
== SENSORS_TEMP_CRITICAL
? "Crit" : "Unkn");
237 gr
->query_data
= sti
;
238 gr
->query_new_value
= query_sti_load
;
240 /* Don't use free() as our callback as that messes up Gallium's
241 * memory debugger. Use simple free_query_data() wrapper.
243 gr
->free_query_data
= free_query_data
;
245 hud_pane_add_graph(pane
, gr
);
247 case SENSORS_TEMP_CURRENT
:
248 case SENSORS_TEMP_CRITICAL
:
249 hud_pane_set_max_value(pane
, 120);
251 case SENSORS_VOLTAGE_CURRENT
:
252 hud_pane_set_max_value(pane
, 12);
254 case SENSORS_CURRENT_CURRENT
:
255 hud_pane_set_max_value(pane
, 5000);
257 case SENSORS_POWER_CURRENT
:
258 hud_pane_set_max_value(pane
, 5000 /* mW */);
264 create_object(const char *chipname
, const char *featurename
,
265 const sensors_chip_name
*chip
, const sensors_feature
*feature
,
268 struct sensors_temp_info
*sti
= CALLOC_STRUCT(sensors_temp_info
);
271 sti
->chip
= (sensors_chip_name
*) chip
;
272 sti
->feature
= feature
;
273 strcpy(sti
->chipname
, chipname
);
274 strcpy(sti
->featurename
, featurename
);
275 snprintf(sti
->name
, sizeof(sti
->name
), "%s.%s", sti
->chipname
,
278 list_addtail(&sti
->list
, &gsensors_temp_list
);
279 gsensors_temp_count
++;
283 build_sensor_list(void)
285 const sensors_chip_name
*chip
;
286 const sensors_chip_name
*match
= 0;
287 const sensors_feature
*feature
;
291 while ((chip
= sensors_get_detected_chips(match
, &chip_nr
))) {
292 sensors_snprintf_chip_name(name
, sizeof(name
), chip
);
294 /* Get all features and filter accordingly. */
296 while ((feature
= sensors_get_features(chip
, &fnr
))) {
297 char *featurename
= sensors_get_label(chip
, feature
);
301 /* Create a 'current' and 'critical' object pair.
302 * Ignore sensor if its not temperature based.
304 switch(feature
->type
) {
305 case SENSORS_FEATURE_TEMP
:
306 create_object(name
, featurename
, chip
, feature
,
307 SENSORS_TEMP_CURRENT
);
308 create_object(name
, featurename
, chip
, feature
,
309 SENSORS_TEMP_CRITICAL
);
311 case SENSORS_FEATURE_IN
:
312 create_object(name
, featurename
, chip
, feature
,
313 SENSORS_VOLTAGE_CURRENT
);
315 case SENSORS_FEATURE_CURR
:
316 create_object(name
, featurename
, chip
, feature
,
317 SENSORS_CURRENT_CURRENT
);
319 case SENSORS_FEATURE_POWER
:
320 create_object(name
, featurename
, chip
, feature
,
321 SENSORS_POWER_CURRENT
);
332 * Initialize internal object arrays and display lmsensors HUD help.
333 * \param displayhelp true if the list of detected devices should be
334 displayed on the console.
335 * \return number of detected lmsensor devices.
338 hud_get_num_sensors(bool displayhelp
)
340 /* Return the number of sensors detected. */
341 if (gsensors_temp_count
)
342 return gsensors_temp_count
;
344 int ret
= sensors_init(NULL
);
348 list_inithead(&gsensors_temp_list
);
350 /* Scan /sys/block, for every object type we support, create and
351 * persist an object to represent its different statistics.
356 list_for_each_entry(struct sensors_temp_info
, sti
, &gsensors_temp_list
, list
) {
359 case SENSORS_TEMP_CURRENT
:
360 snprintf(line
, sizeof(line
), " sensors_temp_cu-%s", sti
->name
);
362 case SENSORS_TEMP_CRITICAL
:
363 snprintf(line
, sizeof(line
), " sensors_temp_cr-%s", sti
->name
);
365 case SENSORS_VOLTAGE_CURRENT
:
366 snprintf(line
, sizeof(line
), " sensors_volt_cu-%s", sti
->name
);
368 case SENSORS_CURRENT_CURRENT
:
369 snprintf(line
, sizeof(line
), " sensors_curr_cu-%s", sti
->name
);
371 case SENSORS_POWER_CURRENT
:
372 snprintf(line
, sizeof(line
), " sensors_pow_cu-%s", sti
->name
);
380 return gsensors_temp_count
;
383 #endif /* HAVE_LIBSENSORS */