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 /* Sensors API returns in WATTs, even though driver is reporting mW,
127 * convert back to mW */
128 sti
->current
= get_value(sti
->chip
, sf
) * 1000;
133 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
134 SENSORS_SUBFEATURE_TEMP_MIN
);
136 sti
->min
= get_value(sti
->chip
, sf
);
138 sf
= sensors_get_subfeature(sti
->chip
, sti
->feature
,
139 SENSORS_SUBFEATURE_TEMP_MAX
);
141 sti
->max
= get_value(sti
->chip
, sf
);
144 static struct sensors_temp_info
*
145 find_sti_by_name(const char *n
, unsigned int mode
)
147 list_for_each_entry(struct sensors_temp_info
, sti
, &gsensors_temp_list
, list
) {
148 if (sti
->mode
!= mode
)
150 if (strcasecmp(sti
->name
, n
) == 0)
157 query_sti_load(struct hud_graph
*gr
, struct pipe_context
*pipe
)
159 struct sensors_temp_info
*sti
= gr
->query_data
;
160 uint64_t now
= os_time_get();
162 if (sti
->last_time
) {
163 if (sti
->last_time
+ gr
->pane
->period
<= now
) {
164 get_sensor_values(sti
);
167 case SENSORS_TEMP_CURRENT
:
168 hud_graph_add_value(gr
, sti
->current
);
170 case SENSORS_TEMP_CRITICAL
:
171 hud_graph_add_value(gr
, sti
->critical
);
173 case SENSORS_VOLTAGE_CURRENT
:
174 hud_graph_add_value(gr
, sti
->current
* 1000);
176 case SENSORS_CURRENT_CURRENT
:
177 hud_graph_add_value(gr
, sti
->current
);
179 case SENSORS_POWER_CURRENT
:
180 hud_graph_add_value(gr
, sti
->current
);
184 sti
->last_time
= now
;
189 get_sensor_values(sti
);
190 sti
->last_time
= now
;
195 * Create and initialize a new object for a specific sensor interface dev.
196 * \param pane parent context.
197 * \param dev_name device name, EG. 'coretemp-isa-0000.Core 1'
198 * \param mode query type (NIC_DIRECTION_RX/WR/RSSI) statistics.
201 hud_sensors_temp_graph_install(struct hud_pane
*pane
, const char *dev_name
,
204 struct hud_graph
*gr
;
205 struct sensors_temp_info
*sti
;
207 int num_devs
= hud_get_num_sensors(0);
211 sti
= find_sti_by_name(dev_name
, mode
);
215 gr
= CALLOC_STRUCT(hud_graph
);
219 snprintf(gr
->name
, sizeof(gr
->name
), "%.6s..%s (%s)",
222 sti
->mode
== SENSORS_VOLTAGE_CURRENT
? "Volts" :
223 sti
->mode
== SENSORS_CURRENT_CURRENT
? "Amps" :
224 sti
->mode
== SENSORS_TEMP_CURRENT
? "Curr" :
225 sti
->mode
== SENSORS_POWER_CURRENT
? "Pow" :
226 sti
->mode
== SENSORS_TEMP_CRITICAL
? "Crit" : "Unkn");
228 gr
->query_data
= sti
;
229 gr
->query_new_value
= query_sti_load
;
231 hud_pane_add_graph(pane
, gr
);
233 case SENSORS_TEMP_CURRENT
:
234 case SENSORS_TEMP_CRITICAL
:
235 hud_pane_set_max_value(pane
, 120);
237 case SENSORS_VOLTAGE_CURRENT
:
238 hud_pane_set_max_value(pane
, 12);
240 case SENSORS_CURRENT_CURRENT
:
241 hud_pane_set_max_value(pane
, 5000);
243 case SENSORS_POWER_CURRENT
:
244 hud_pane_set_max_value(pane
, 5000 /* mW */);
250 create_object(const char *chipname
, const char *featurename
,
251 const sensors_chip_name
*chip
, const sensors_feature
*feature
,
254 struct sensors_temp_info
*sti
= CALLOC_STRUCT(sensors_temp_info
);
257 sti
->chip
= (sensors_chip_name
*) chip
;
258 sti
->feature
= feature
;
259 strcpy(sti
->chipname
, chipname
);
260 strcpy(sti
->featurename
, featurename
);
261 snprintf(sti
->name
, sizeof(sti
->name
), "%s.%s", sti
->chipname
,
264 list_addtail(&sti
->list
, &gsensors_temp_list
);
265 gsensors_temp_count
++;
269 build_sensor_list(void)
271 const sensors_chip_name
*chip
;
272 const sensors_chip_name
*match
= 0;
273 const sensors_feature
*feature
;
277 while ((chip
= sensors_get_detected_chips(match
, &chip_nr
))) {
278 sensors_snprintf_chip_name(name
, sizeof(name
), chip
);
280 /* Get all features and filter accordingly. */
282 while ((feature
= sensors_get_features(chip
, &fnr
))) {
283 char *featurename
= sensors_get_label(chip
, feature
);
287 /* Create a 'current' and 'critical' object pair.
288 * Ignore sensor if its not temperature based.
290 switch(feature
->type
) {
291 case SENSORS_FEATURE_TEMP
:
292 create_object(name
, featurename
, chip
, feature
,
293 SENSORS_TEMP_CURRENT
);
294 create_object(name
, featurename
, chip
, feature
,
295 SENSORS_TEMP_CRITICAL
);
297 case SENSORS_FEATURE_IN
:
298 create_object(name
, featurename
, chip
, feature
,
299 SENSORS_VOLTAGE_CURRENT
);
301 case SENSORS_FEATURE_CURR
:
302 create_object(name
, featurename
, chip
, feature
,
303 SENSORS_CURRENT_CURRENT
);
305 case SENSORS_FEATURE_POWER
:
306 create_object(name
, featurename
, chip
, feature
,
307 SENSORS_POWER_CURRENT
);
318 * Initialize internal object arrays and display lmsensors HUD help.
319 * \param displayhelp true if the list of detected devices should be
320 displayed on the console.
321 * \return number of detected lmsensor devices.
324 hud_get_num_sensors(bool displayhelp
)
326 /* Return the number of sensors detected. */
327 mtx_lock(&gsensor_temp_mutex
);
328 if (gsensors_temp_count
) {
329 mtx_unlock(&gsensor_temp_mutex
);
330 return gsensors_temp_count
;
333 int ret
= sensors_init(NULL
);
335 mtx_unlock(&gsensor_temp_mutex
);
339 list_inithead(&gsensors_temp_list
);
341 /* Scan /sys/block, for every object type we support, create and
342 * persist an object to represent its different statistics.
347 list_for_each_entry(struct sensors_temp_info
, sti
, &gsensors_temp_list
, list
) {
350 case SENSORS_TEMP_CURRENT
:
351 snprintf(line
, sizeof(line
), " sensors_temp_cu-%s", sti
->name
);
353 case SENSORS_TEMP_CRITICAL
:
354 snprintf(line
, sizeof(line
), " sensors_temp_cr-%s", sti
->name
);
356 case SENSORS_VOLTAGE_CURRENT
:
357 snprintf(line
, sizeof(line
), " sensors_volt_cu-%s", sti
->name
);
359 case SENSORS_CURRENT_CURRENT
:
360 snprintf(line
, sizeof(line
), " sensors_curr_cu-%s", sti
->name
);
362 case SENSORS_POWER_CURRENT
:
363 snprintf(line
, sizeof(line
), " sensors_pow_cu-%s", sti
->name
);
371 mtx_unlock(&gsensor_temp_mutex
);
372 return gsensors_temp_count
;
375 #endif /* HAVE_LIBSENSORS */