1 // SPDX-License-Identifier: GPL-2.0-only
3 * Thermal monitoring tool based on the thermal netlink events.
5 * Copyright (C) 2022 Linaro Ltd.
7 * Author: Daniel Lezcano <daniel.lezcano@kernel.org>
22 #include <sys/epoll.h>
24 #include <sys/types.h>
27 #include "thermal-tools.h"
37 struct thermal_zone
*tz
;
38 struct thermal_handler
*th
;
41 static int show_threshold(struct thermal_threshold
*th
, __maybe_unused
void *arg
)
43 INFO("threshold temp=%d, direction=%d\n",
44 th
->temperature
, th
->direction
);
49 static int show_trip(struct thermal_trip
*tt
, __maybe_unused
void *arg
)
51 INFO("trip id=%d, type=%d, temp=%d, hyst=%d\n",
52 tt
->id
, tt
->type
, tt
->temp
, tt
->hyst
);
57 static int show_temp(struct thermal_zone
*tz
, __maybe_unused
void *arg
)
59 thermal_cmd_get_temp(arg
, tz
);
61 INFO("temperature: %d\n", tz
->temp
);
66 static int show_governor(struct thermal_zone
*tz
, __maybe_unused
void *arg
)
68 thermal_cmd_get_governor(arg
, tz
);
70 INFO("governor: '%s'\n", tz
->governor
);
75 static int show_tz(struct thermal_zone
*tz
, __maybe_unused
void *arg
)
77 INFO("thermal zone '%s', id=%d\n", tz
->name
, tz
->id
);
79 for_each_thermal_trip(tz
->trip
, show_trip
, NULL
);
81 for_each_thermal_threshold(tz
->thresholds
, show_threshold
, NULL
);
85 show_governor(tz
, arg
);
90 static int set_threshold(struct thermal_zone
*tz
, __maybe_unused
void *arg
)
92 struct thermal_handler
*th
= arg
;
93 int thresholds
[] = { 43000, 65000, 49000, 55000, 57000 };
96 INFO("Setting threshold for thermal zone '%s', id=%d\n", tz
->name
, tz
->id
);
98 if (thermal_cmd_threshold_flush(th
, tz
)) {
99 ERROR("Failed to flush all previous thresholds\n");
103 for (i
= 0; i
< sizeof(thresholds
) / sizeof(thresholds
[0]); i
++)
104 if (thermal_cmd_threshold_add(th
, tz
, thresholds
[i
],
105 THERMAL_THRESHOLD_WAY_UP
|
106 THERMAL_THRESHOLD_WAY_DOWN
)) {
107 ERROR("Failed to set threshold\n");
114 static int tz_create(const char *name
, int tz_id
, __maybe_unused
void *arg
)
116 INFO("Thermal zone '%s'/%d created\n", name
, tz_id
);
121 static int tz_delete(int tz_id
, __maybe_unused
void *arg
)
123 INFO("Thermal zone %d deleted\n", tz_id
);
128 static int tz_disable(int tz_id
, void *arg
)
130 struct thermal_data
*td
= arg
;
131 struct thermal_zone
*tz
= thermal_zone_find_by_id(td
->tz
, tz_id
);
133 INFO("Thermal zone %d ('%s') disabled\n", tz_id
, tz
->name
);
138 static int tz_enable(int tz_id
, void *arg
)
140 struct thermal_data
*td
= arg
;
141 struct thermal_zone
*tz
= thermal_zone_find_by_id(td
->tz
, tz_id
);
143 INFO("Thermal zone %d ('%s') enabled\n", tz_id
, tz
->name
);
148 static int trip_high(int tz_id
, int trip_id
, int temp
, void *arg
)
150 struct thermal_data
*td
= arg
;
151 struct thermal_zone
*tz
= thermal_zone_find_by_id(td
->tz
, tz_id
);
153 INFO("Thermal zone %d ('%s'): trip point %d crossed way up with %d °C\n",
154 tz_id
, tz
->name
, trip_id
, temp
);
159 static int trip_low(int tz_id
, int trip_id
, int temp
, void *arg
)
161 struct thermal_data
*td
= arg
;
162 struct thermal_zone
*tz
= thermal_zone_find_by_id(td
->tz
, tz_id
);
164 INFO("Thermal zone %d ('%s'): trip point %d crossed way down with %d °C\n",
165 tz_id
, tz
->name
, trip_id
, temp
);
170 static int trip_add(int tz_id
, int trip_id
, int type
, int temp
, int hyst
, __maybe_unused
void *arg
)
172 INFO("Trip point added %d: id=%d, type=%d, temp=%d, hyst=%d\n",
173 tz_id
, trip_id
, type
, temp
, hyst
);
178 static int trip_delete(int tz_id
, int trip_id
, __maybe_unused
void *arg
)
180 INFO("Trip point deleted %d: id=%d\n", tz_id
, trip_id
);
185 static int trip_change(int tz_id
, int trip_id
, int type
, int temp
,
186 int hyst
, __maybe_unused
void *arg
)
188 struct thermal_data
*td
= arg
;
189 struct thermal_zone
*tz
= thermal_zone_find_by_id(td
->tz
, tz_id
);
191 INFO("Trip point changed %d: id=%d, type=%d, temp=%d, hyst=%d\n",
192 tz_id
, trip_id
, type
, temp
, hyst
);
194 tz
->trip
[trip_id
].type
= type
;
195 tz
->trip
[trip_id
].temp
= temp
;
196 tz
->trip
[trip_id
].hyst
= hyst
;
201 static int cdev_add(const char *name
, int cdev_id
, int max_state
, __maybe_unused
void *arg
)
203 INFO("Cooling device '%s'/%d (max state=%d) added\n", name
, cdev_id
, max_state
);
208 static int cdev_delete(int cdev_id
, __maybe_unused
void *arg
)
210 INFO("Cooling device %d deleted", cdev_id
);
215 static int cdev_update(int cdev_id
, int cur_state
, __maybe_unused
void *arg
)
217 INFO("cdev:%d state:%d\n", cdev_id
, cur_state
);
222 static int gov_change(int tz_id
, const char *name
, __maybe_unused
void *arg
)
224 struct thermal_data
*td
= arg
;
225 struct thermal_zone
*tz
= thermal_zone_find_by_id(td
->tz
, tz_id
);
227 INFO("%s: governor changed %s -> %s\n", tz
->name
, tz
->governor
, name
);
229 strcpy(tz
->governor
, name
);
234 static int threshold_add(int tz_id
, int temp
, int direction
, __maybe_unused
void *arg
)
236 INFO("Threshold added tz_id=%d: temp=%d, direction=%d\n", tz_id
, temp
, direction
);
241 static int threshold_delete(int tz_id
, int temp
, int direction
, __maybe_unused
void *arg
)
243 INFO("Threshold deleted tz_id=%d: temp=%d, direction=%d\n", tz_id
, temp
, direction
);
248 static int threshold_flush(int tz_id
, __maybe_unused
void *arg
)
250 INFO("Thresholds flushed tz_id=%d\n", tz_id
);
255 static int threshold_up(int tz_id
, int temp
, int prev_temp
, __maybe_unused
void *arg
)
257 INFO("Threshold crossed way up tz_id=%d: temp=%d, prev_temp=%d\n",
258 tz_id
, temp
, prev_temp
);
263 static int threshold_down(int tz_id
, int temp
, int prev_temp
, __maybe_unused
void *arg
)
265 INFO("Threshold crossed way down tz_id=%d: temp=%d, prev_temp=%d\n",
266 tz_id
, temp
, prev_temp
);
271 static struct thermal_ops ops
= {
272 .events
.tz_create
= tz_create
,
273 .events
.tz_delete
= tz_delete
,
274 .events
.tz_disable
= tz_disable
,
275 .events
.tz_enable
= tz_enable
,
276 .events
.trip_high
= trip_high
,
277 .events
.trip_low
= trip_low
,
278 .events
.trip_add
= trip_add
,
279 .events
.trip_delete
= trip_delete
,
280 .events
.trip_change
= trip_change
,
281 .events
.cdev_add
= cdev_add
,
282 .events
.cdev_delete
= cdev_delete
,
283 .events
.cdev_update
= cdev_update
,
284 .events
.gov_change
= gov_change
,
285 .events
.threshold_add
= threshold_add
,
286 .events
.threshold_delete
= threshold_delete
,
287 .events
.threshold_flush
= threshold_flush
,
288 .events
.threshold_up
= threshold_up
,
289 .events
.threshold_down
= threshold_down
,
292 static int thermal_event(__maybe_unused
int fd
, __maybe_unused
void *arg
)
294 struct thermal_data
*td
= arg
;
296 return thermal_events_handle(td
->th
, td
);
299 static void usage(const char *cmd
)
301 printf("%s : A thermal monitoring engine based on notifications\n", cmd
);
302 printf("Usage: %s [options]\n", cmd
);
303 printf("\t-h, --help\t\tthis help\n");
304 printf("\t-d, --daemonize\n");
305 printf("\t-l <level>, --loglevel <level>\tlog level: ");
306 printf("DEBUG, INFO, NOTICE, WARN, ERROR\n");
307 printf("\t-s, --syslog\t\toutput to syslog\n");
312 static int options_init(int argc
, char *argv
[], struct options
*options
)
316 struct option long_options
[] = {
317 { "help", no_argument
, NULL
, 'h' },
318 { "daemonize", no_argument
, NULL
, 'd' },
319 { "syslog", no_argument
, NULL
, 's' },
320 { "loglevel", required_argument
, NULL
, 'l' },
328 opt
= getopt_long(argc
, argv
, "l:dhs", long_options
, &optindex
);
334 options
->loglevel
= log_str2level(optarg
);
337 options
->daemonize
= 1;
340 options
->logopt
= TO_SYSLOG
;
343 usage(basename(argv
[0]));
354 THERMAL_ENGINE_SUCCESS
= 0,
355 THERMAL_ENGINE_OPTION_ERROR
,
356 THERMAL_ENGINE_DAEMON_ERROR
,
357 THERMAL_ENGINE_LOG_ERROR
,
358 THERMAL_ENGINE_THERMAL_ERROR
,
359 THERMAL_ENGINE_THRESHOLD_ERROR
,
360 THERMAL_ENGINE_MAINLOOP_ERROR
,
363 int main(int argc
, char *argv
[])
365 struct thermal_data td
;
366 struct options options
= {
367 .loglevel
= LOG_INFO
,
371 if (options_init(argc
, argv
, &options
)) {
372 ERROR("Usage: %s --help\n", argv
[0]);
373 return THERMAL_ENGINE_OPTION_ERROR
;
376 if (options
.daemonize
&& daemon(0, 0)) {
377 ERROR("Failed to daemonize: %p\n");
378 return THERMAL_ENGINE_DAEMON_ERROR
;
381 if (log_init(options
.loglevel
, basename(argv
[0]), options
.logopt
)) {
382 ERROR("Failed to initialize logging facility\n");
383 return THERMAL_ENGINE_LOG_ERROR
;
386 td
.th
= thermal_init(&ops
);
388 ERROR("Failed to initialize the thermal library\n");
389 return THERMAL_ENGINE_THERMAL_ERROR
;
392 td
.tz
= thermal_zone_discover(td
.th
);
394 ERROR("No thermal zone available\n");
395 return THERMAL_ENGINE_THERMAL_ERROR
;
398 for_each_thermal_zone(td
.tz
, set_threshold
, td
.th
);
400 for_each_thermal_zone(td
.tz
, show_tz
, td
.th
);
402 if (mainloop_init()) {
403 ERROR("Failed to initialize the mainloop\n");
404 return THERMAL_ENGINE_MAINLOOP_ERROR
;
407 if (mainloop_add(thermal_events_fd(td
.th
), thermal_event
, &td
)) {
408 ERROR("Failed to setup the mainloop\n");
409 return THERMAL_ENGINE_MAINLOOP_ERROR
;
412 INFO("Waiting for thermal events ...\n");
415 ERROR("Mainloop failed\n");
416 return THERMAL_ENGINE_MAINLOOP_ERROR
;
419 return THERMAL_ENGINE_SUCCESS
;