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 * 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.
199 hud_sensors_temp_graph_install(struct hud_pane
*pane
, const char *dev_name
,
202 struct hud_graph
*gr
;
203 struct sensors_temp_info
*sti
;
205 int num_devs
= hud_get_num_sensors(0);
209 sti
= find_sti_by_name(dev_name
, mode
);
213 gr
= CALLOC_STRUCT(hud_graph
);
217 snprintf(gr
->name
, sizeof(gr
->name
), "%.6s..%s (%s)",
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");
226 gr
->query_data
= sti
;
227 gr
->query_new_value
= query_sti_load
;
229 hud_pane_add_graph(pane
, gr
);
231 case SENSORS_TEMP_CURRENT
:
232 case SENSORS_TEMP_CRITICAL
:
233 hud_pane_set_max_value(pane
, 120);
235 case SENSORS_VOLTAGE_CURRENT
:
236 hud_pane_set_max_value(pane
, 12);
238 case SENSORS_CURRENT_CURRENT
:
239 hud_pane_set_max_value(pane
, 5000);
241 case SENSORS_POWER_CURRENT
:
242 hud_pane_set_max_value(pane
, 5000 /* mW */);
248 create_object(const char *chipname
, const char *featurename
,
249 const sensors_chip_name
*chip
, const sensors_feature
*feature
,
252 struct sensors_temp_info
*sti
= CALLOC_STRUCT(sensors_temp_info
);
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
,
262 list_addtail(&sti
->list
, &gsensors_temp_list
);
263 gsensors_temp_count
++;
267 build_sensor_list(void)
269 const sensors_chip_name
*chip
;
270 const sensors_chip_name
*match
= 0;
271 const sensors_feature
*feature
;
275 while ((chip
= sensors_get_detected_chips(match
, &chip_nr
))) {
276 sensors_snprintf_chip_name(name
, sizeof(name
), chip
);
278 /* Get all features and filter accordingly. */
280 while ((feature
= sensors_get_features(chip
, &fnr
))) {
281 char *featurename
= sensors_get_label(chip
, feature
);
285 /* Create a 'current' and 'critical' object pair.
286 * Ignore sensor if its not temperature based.
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
);
295 case SENSORS_FEATURE_IN
:
296 create_object(name
, featurename
, chip
, feature
,
297 SENSORS_VOLTAGE_CURRENT
);
299 case SENSORS_FEATURE_CURR
:
300 create_object(name
, featurename
, chip
, feature
,
301 SENSORS_CURRENT_CURRENT
);
303 case SENSORS_FEATURE_POWER
:
304 create_object(name
, featurename
, chip
, feature
,
305 SENSORS_POWER_CURRENT
);
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.
322 hud_get_num_sensors(bool displayhelp
)
324 /* Return the number of sensors detected. */
325 if (gsensors_temp_count
)
326 return gsensors_temp_count
;
328 int ret
= sensors_init(NULL
);
332 list_inithead(&gsensors_temp_list
);
334 /* Scan /sys/block, for every object type we support, create and
335 * persist an object to represent its different statistics.
340 list_for_each_entry(struct sensors_temp_info
, sti
, &gsensors_temp_list
, list
) {
343 case SENSORS_TEMP_CURRENT
:
344 snprintf(line
, sizeof(line
), " sensors_temp_cu-%s", sti
->name
);
346 case SENSORS_TEMP_CRITICAL
:
347 snprintf(line
, sizeof(line
), " sensors_temp_cr-%s", sti
->name
);
349 case SENSORS_VOLTAGE_CURRENT
:
350 snprintf(line
, sizeof(line
), " sensors_volt_cu-%s", sti
->name
);
352 case SENSORS_CURRENT_CURRENT
:
353 snprintf(line
, sizeof(line
), " sensors_curr_cu-%s", sti
->name
);
355 case SENSORS_POWER_CURRENT
:
356 snprintf(line
, sizeof(line
), " sensors_pow_cu-%s", sti
->name
);
364 return gsensors_temp_count
;
367 #endif /* HAVE_LIBSENSORS */