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 **************************************************************************/
29 #ifdef HAVE_LIBSENSORS
30 /* Purpose: Extract lm-sensors data, expose temperature, power, voltage. */
32 #include "hud/hud_private.h"
33 #include "util/list.h"
34 #include "util/os_time.h"
35 #include "os/os_thread.h"
36 #include "util/u_memory.h"
43 #include <sys/types.h>
46 #include <sensors/sensors.h>
48 /* TODO: We don't handle dynamic sensor discovery / arrival or removal.
49 * Static globals specific to this HUD category.
51 static int gsensors_temp_count
= 0;
52 static struct list_head gsensors_temp_list
;
53 static mtx_t gsensor_temp_mutex
= _MTX_INITIALIZER_NP
;
55 struct sensors_temp_info
57 struct list_head list
;
59 /* Combined chip and feature name, human readable. */
62 /* The type of measurement, critical or current. */
68 char featurename
[128];
70 sensors_chip_name
*chip
;
71 const sensors_feature
*feature
;
72 double current
, min
, max
, critical
;
76 get_value(const sensors_chip_name
*name
, const sensors_subfeature
*sub
)
81 err
= sensors_get_value(name
, sub
->number
, &val
);
83 fprintf(stderr
, "ERROR: Can't get value of subfeature %s\n", sub
->name
);
90 get_sensor_values(struct sensors_temp_info
*sti
)
92 const sensors_subfeature
*sf
;
95 case SENSORS_VOLTAGE_CURRENT
:
96 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
97 SENSORS_SUBFEATURE_IN_INPUT
);
99 sti
->current
= get_value(sti
->chip
, sf
);
101 case SENSORS_CURRENT_CURRENT
:
102 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
103 SENSORS_SUBFEATURE_CURR_INPUT
);
105 /* Sensors API returns in AMPs, even though driver is reporting mA,
106 * convert back to mA */
107 sti
->current
= get_value(sti
->chip
, sf
) * 1000;
110 case SENSORS_TEMP_CURRENT
:
111 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
112 SENSORS_SUBFEATURE_TEMP_INPUT
);
114 sti
->current
= get_value(sti
->chip
, sf
);
116 case SENSORS_TEMP_CRITICAL
:
117 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
118 SENSORS_SUBFEATURE_TEMP_CRIT
);
120 sti
->critical
= get_value(sti
->chip
, sf
);
122 case SENSORS_POWER_CURRENT
:
123 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
124 SENSORS_SUBFEATURE_POWER_INPUT
);
126 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
127 SENSORS_SUBFEATURE_POWER_AVERAGE
);
129 /* Sensors API returns in WATTs, even though driver is reporting mW,
130 * convert back to mW */
131 sti
->current
= get_value(sti
->chip
, sf
) * 1000;
136 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
137 SENSORS_SUBFEATURE_TEMP_MIN
);
139 sti
->min
= get_value(sti
->chip
, sf
);
141 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
142 SENSORS_SUBFEATURE_TEMP_MAX
);
144 sti
->max
= get_value(sti
->chip
, sf
);
147 static struct sensors_temp_info
*
148 find_sti_by_name(const char *n
, unsigned int mode
)
150 list_for_each_entry(struct sensors_temp_info
, sti
, &gsensors_temp_list
, list
) {
151 if (sti
->mode
!= mode
)
153 if (strcasecmp(sti
->name
, n
) == 0)
160 query_sti_load(struct hud_graph
*gr
, struct pipe_context
*pipe
)
162 struct sensors_temp_info
*sti
= gr
->query_data
;
163 uint64_t now
= os_time_get();
165 if (sti
->last_time
) {
166 if (sti
->last_time
+ gr
->pane
->period
<= now
) {
167 get_sensor_values(sti
);
170 case SENSORS_TEMP_CURRENT
:
171 hud_graph_add_value(gr
, sti
->current
);
173 case SENSORS_TEMP_CRITICAL
:
174 hud_graph_add_value(gr
, sti
->critical
);
176 case SENSORS_VOLTAGE_CURRENT
:
177 hud_graph_add_value(gr
, sti
->current
* 1000);
179 case SENSORS_CURRENT_CURRENT
:
180 hud_graph_add_value(gr
, sti
->current
);
182 case SENSORS_POWER_CURRENT
:
183 hud_graph_add_value(gr
, sti
->current
);
187 sti
->last_time
= now
;
192 get_sensor_values(sti
);
193 sti
->last_time
= now
;
198 * Create and initialize a new object for a specific sensor interface dev.
199 * \param pane parent context.
200 * \param dev_name device name, EG. 'coretemp-isa-0000.Core 1'
201 * \param mode query type (NIC_DIRECTION_RX/WR/RSSI) statistics.
204 hud_sensors_temp_graph_install(struct hud_pane
*pane
, const char *dev_name
,
207 struct hud_graph
*gr
;
208 struct sensors_temp_info
*sti
;
210 int num_devs
= hud_get_num_sensors(0);
214 sti
= find_sti_by_name(dev_name
, mode
);
218 gr
= CALLOC_STRUCT(hud_graph
);
222 snprintf(gr
->name
, sizeof(gr
->name
), "%.6s..%s (%s)",
225 sti
->mode
== SENSORS_VOLTAGE_CURRENT
? "Volts" :
226 sti
->mode
== SENSORS_CURRENT_CURRENT
? "Amps" :
227 sti
->mode
== SENSORS_TEMP_CURRENT
? "Curr" :
228 sti
->mode
== SENSORS_POWER_CURRENT
? "Pow" :
229 sti
->mode
== SENSORS_TEMP_CRITICAL
? "Crit" : "Unkn");
231 gr
->query_data
= sti
;
232 gr
->query_new_value
= query_sti_load
;
234 hud_pane_add_graph(pane
, gr
);
236 case SENSORS_TEMP_CURRENT
:
237 case SENSORS_TEMP_CRITICAL
:
238 hud_pane_set_max_value(pane
, 120);
240 case SENSORS_VOLTAGE_CURRENT
:
241 hud_pane_set_max_value(pane
, 12);
243 case SENSORS_CURRENT_CURRENT
:
244 hud_pane_set_max_value(pane
, 5000);
246 case SENSORS_POWER_CURRENT
:
247 hud_pane_set_max_value(pane
, 5000 /* mW */);
253 create_object(const char *chipname
, const char *featurename
,
254 const sensors_chip_name
*chip
, const sensors_feature
*feature
,
257 struct sensors_temp_info
*sti
= CALLOC_STRUCT(sensors_temp_info
);
260 sti
->chip
= (sensors_chip_name
*) chip
;
261 sti
->feature
= feature
;
262 snprintf(sti
->chipname
, sizeof(sti
->chipname
), "%s", chipname
);
263 snprintf(sti
->featurename
, sizeof(sti
->featurename
), "%s", featurename
);
264 snprintf(sti
->name
, sizeof(sti
->name
), "%s.%s", sti
->chipname
,
267 list_addtail(&sti
->list
, &gsensors_temp_list
);
268 gsensors_temp_count
++;
272 build_sensor_list(void)
274 const sensors_chip_name
*chip
;
275 const sensors_chip_name
*match
= 0;
276 const sensors_feature
*feature
;
280 while ((chip
= sensors_get_detected_chips(match
, &chip_nr
))) {
281 sensors_snprintf_chip_name(name
, sizeof(name
), chip
);
283 /* Get all features and filter accordingly. */
285 while ((feature
= sensors_get_features(chip
, &fnr
))) {
286 char *featurename
= sensors_get_label(chip
, feature
);
290 /* Create a 'current' and 'critical' object pair.
291 * Ignore sensor if its not temperature based.
293 switch(feature
->type
) {
294 case SENSORS_FEATURE_TEMP
:
295 create_object(name
, featurename
, chip
, feature
,
296 SENSORS_TEMP_CURRENT
);
297 create_object(name
, featurename
, chip
, feature
,
298 SENSORS_TEMP_CRITICAL
);
300 case SENSORS_FEATURE_IN
:
301 create_object(name
, featurename
, chip
, feature
,
302 SENSORS_VOLTAGE_CURRENT
);
304 case SENSORS_FEATURE_CURR
:
305 create_object(name
, featurename
, chip
, feature
,
306 SENSORS_CURRENT_CURRENT
);
308 case SENSORS_FEATURE_POWER
:
309 create_object(name
, featurename
, chip
, feature
,
310 SENSORS_POWER_CURRENT
);
321 * Initialize internal object arrays and display lmsensors HUD help.
322 * \param displayhelp true if the list of detected devices should be
323 displayed on the console.
324 * \return number of detected lmsensor devices.
327 hud_get_num_sensors(bool displayhelp
)
329 /* Return the number of sensors detected. */
330 mtx_lock(&gsensor_temp_mutex
);
331 if (gsensors_temp_count
) {
332 mtx_unlock(&gsensor_temp_mutex
);
333 return gsensors_temp_count
;
336 int ret
= sensors_init(NULL
);
338 mtx_unlock(&gsensor_temp_mutex
);
342 list_inithead(&gsensors_temp_list
);
344 /* Scan /sys/block, for every object type we support, create and
345 * persist an object to represent its different statistics.
350 list_for_each_entry(struct sensors_temp_info
, sti
, &gsensors_temp_list
, list
) {
353 case SENSORS_TEMP_CURRENT
:
354 snprintf(line
, sizeof(line
), " sensors_temp_cu-%s", sti
->name
);
356 case SENSORS_TEMP_CRITICAL
:
357 snprintf(line
, sizeof(line
), " sensors_temp_cr-%s", sti
->name
);
359 case SENSORS_VOLTAGE_CURRENT
:
360 snprintf(line
, sizeof(line
), " sensors_volt_cu-%s", sti
->name
);
362 case SENSORS_CURRENT_CURRENT
:
363 snprintf(line
, sizeof(line
), " sensors_curr_cu-%s", sti
->name
);
365 case SENSORS_POWER_CURRENT
:
366 snprintf(line
, sizeof(line
), " sensors_pow_cu-%s", sti
->name
);
374 mtx_unlock(&gsensor_temp_mutex
);
375 return gsensors_temp_count
;
378 #endif /* HAVE_LIBSENSORS */