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_trip(struct thermal_trip *tt, __maybe_unused void *arg)
43 INFO("trip id=%d, type=%d, temp=%d, hyst=%d\n",
44 tt->id, tt->type, tt->temp, tt->hyst);
49 static int show_temp(struct thermal_zone *tz, __maybe_unused void *arg)
51 thermal_cmd_get_temp(arg, tz);
53 INFO("temperature: %d\n", tz->temp);
58 static int show_governor(struct thermal_zone *tz, __maybe_unused void *arg)
60 thermal_cmd_get_governor(arg, tz);
62 INFO("governor: '%s'\n", tz->governor);
67 static int show_tz(struct thermal_zone *tz, __maybe_unused void *arg)
69 INFO("thermal zone '%s', id=%d\n", tz->name, tz->id);
71 for_each_thermal_trip(tz->trip, show_trip, NULL);
75 show_governor(tz, arg);
80 static int tz_create(const char *name, int tz_id, __maybe_unused void *arg)
82 INFO("Thermal zone '%s'/%d created\n", name, tz_id);
87 static int tz_delete(int tz_id, __maybe_unused void *arg)
89 INFO("Thermal zone %d deleted\n", tz_id);
94 static int tz_disable(int tz_id, void *arg)
96 struct thermal_data *td = arg;
97 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
99 INFO("Thermal zone %d ('%s') disabled\n", tz_id, tz->name);
104 static int tz_enable(int tz_id, void *arg)
106 struct thermal_data *td = arg;
107 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
109 INFO("Thermal zone %d ('%s') enabled\n", tz_id, tz->name);
114 static int trip_high(int tz_id, int trip_id, int temp, void *arg)
116 struct thermal_data *td = arg;
117 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
119 INFO("Thermal zone %d ('%s'): trip point %d crossed way up with %d °C\n",
120 tz_id, tz->name, trip_id, temp);
125 static int trip_low(int tz_id, int trip_id, int temp, void *arg)
127 struct thermal_data *td = arg;
128 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
130 INFO("Thermal zone %d ('%s'): trip point %d crossed way down with %d °C\n",
131 tz_id, tz->name, trip_id, temp);
136 static int trip_add(int tz_id, int trip_id, int type, int temp, int hyst, __maybe_unused void *arg)
138 INFO("Trip point added %d: id=%d, type=%d, temp=%d, hyst=%d\n",
139 tz_id, trip_id, type, temp, hyst);
144 static int trip_delete(int tz_id, int trip_id, __maybe_unused void *arg)
146 INFO("Trip point deleted %d: id=%d\n", tz_id, trip_id);
151 static int trip_change(int tz_id, int trip_id, int type, int temp,
152 int hyst, __maybe_unused void *arg)
154 struct thermal_data *td = arg;
155 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
157 INFO("Trip point changed %d: id=%d, type=%d, temp=%d, hyst=%d\n",
158 tz_id, trip_id, type, temp, hyst);
160 tz->trip[trip_id].type = type;
161 tz->trip[trip_id].temp = temp;
162 tz->trip[trip_id].hyst = hyst;
167 static int cdev_add(const char *name, int cdev_id, int max_state, __maybe_unused void *arg)
169 INFO("Cooling device '%s'/%d (max state=%d) added\n", name, cdev_id, max_state);
174 static int cdev_delete(int cdev_id, __maybe_unused void *arg)
176 INFO("Cooling device %d deleted", cdev_id);
181 static int cdev_update(int cdev_id, int cur_state, __maybe_unused void *arg)
183 INFO("cdev:%d state:%d\n", cdev_id, cur_state);
188 static int gov_change(int tz_id, const char *name, __maybe_unused void *arg)
190 struct thermal_data *td = arg;
191 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
193 INFO("%s: governor changed %s -> %s\n", tz->name, tz->governor, name);
195 strcpy(tz->governor, name);
200 static struct thermal_ops ops = {
201 .events.tz_create = tz_create,
202 .events.tz_delete = tz_delete,
203 .events.tz_disable = tz_disable,
204 .events.tz_enable = tz_enable,
205 .events.trip_high = trip_high,
206 .events.trip_low = trip_low,
207 .events.trip_add = trip_add,
208 .events.trip_delete = trip_delete,
209 .events.trip_change = trip_change,
210 .events.cdev_add = cdev_add,
211 .events.cdev_delete = cdev_delete,
212 .events.cdev_update = cdev_update,
213 .events.gov_change = gov_change
216 static int thermal_event(__maybe_unused int fd, __maybe_unused void *arg)
218 struct thermal_data *td = arg;
220 return thermal_events_handle(td->th, td);
223 static void usage(const char *cmd)
225 printf("%s : A thermal monitoring engine based on notifications\n", cmd);
226 printf("Usage: %s [options]\n", cmd);
227 printf("\t-h, --help\t\tthis help\n");
228 printf("\t-d, --daemonize\n");
229 printf("\t-l <level>, --loglevel <level>\tlog level: ");
230 printf("DEBUG, INFO, NOTICE, WARN, ERROR\n");
231 printf("\t-s, --syslog\t\toutput to syslog\n");
236 static int options_init(int argc, char *argv[], struct options *options)
240 struct option long_options[] = {
241 { "help", no_argument, NULL, 'h' },
242 { "daemonize", no_argument, NULL, 'd' },
243 { "syslog", no_argument, NULL, 's' },
244 { "loglevel", required_argument, NULL, 'l' },
252 opt = getopt_long(argc, argv, "l:dhs", long_options, &optindex);
258 options->loglevel = log_str2level(optarg);
261 options->daemonize = 1;
264 options->logopt = TO_SYSLOG;
267 usage(basename(argv[0]));
278 THERMAL_ENGINE_SUCCESS = 0,
279 THERMAL_ENGINE_OPTION_ERROR,
280 THERMAL_ENGINE_DAEMON_ERROR,
281 THERMAL_ENGINE_LOG_ERROR,
282 THERMAL_ENGINE_THERMAL_ERROR,
283 THERMAL_ENGINE_MAINLOOP_ERROR,
286 int main(int argc, char *argv[])
288 struct thermal_data td;
289 struct options options = {
290 .loglevel = LOG_INFO,
294 if (options_init(argc, argv, &options)) {
295 ERROR("Usage: %s --help\n", argv[0]);
296 return THERMAL_ENGINE_OPTION_ERROR;
299 if (options.daemonize && daemon(0, 0)) {
300 ERROR("Failed to daemonize: %p\n");
301 return THERMAL_ENGINE_DAEMON_ERROR;
304 if (log_init(options.loglevel, basename(argv[0]), options.logopt)) {
305 ERROR("Failed to initialize logging facility\n");
306 return THERMAL_ENGINE_LOG_ERROR;
309 td.th = thermal_init(&ops);
311 ERROR("Failed to initialize the thermal library\n");
312 return THERMAL_ENGINE_THERMAL_ERROR;
315 td.tz = thermal_zone_discover(td.th);
317 ERROR("No thermal zone available\n");
318 return THERMAL_ENGINE_THERMAL_ERROR;
321 for_each_thermal_zone(td.tz, show_tz, td.th);
323 if (mainloop_init()) {
324 ERROR("Failed to initialize the mainloop\n");
325 return THERMAL_ENGINE_MAINLOOP_ERROR;
328 if (mainloop_add(thermal_events_fd(td.th), thermal_event, &td)) {
329 ERROR("Failed to setup the mainloop\n");
330 return THERMAL_ENGINE_MAINLOOP_ERROR;
333 INFO("Waiting for thermal events ...\n");
336 ERROR("Mainloop failed\n");
337 return THERMAL_ENGINE_MAINLOOP_ERROR;
340 return THERMAL_ENGINE_SUCCESS;