1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
13 #include <sys/signalfd.h>
14 #include <sys/timerfd.h>
15 #include <sys/types.h>
19 #include <linux/thermal.h>
21 #include <libconfig.h>
22 #include "thermal-tools.h"
24 #define CLASS_THERMAL "/sys/class/thermal"
27 THERMOMETER_SUCCESS = 0,
28 THERMOMETER_OPTION_ERROR,
29 THERMOMETER_LOG_ERROR,
30 THERMOMETER_CONFIG_ERROR,
31 THERMOMETER_TIME_ERROR,
32 THERMOMETER_INIT_ERROR,
33 THERMOMETER_RUNTIME_ERROR
42 char postfix[PATH_MAX];
43 char output[PATH_MAX];
51 struct configuration {
52 struct tz_regex *tz_regex;
70 static struct tz_regex *configuration_tz_match(const char *expr,
71 struct configuration *config)
75 for (i = 0; i < config->nr_tz_regex; i++) {
77 if (!regexec(&config->tz_regex[i].regex, expr, 0, NULL, 0))
78 return &config->tz_regex[i];
84 static int configuration_default_init(struct configuration *config)
86 config->tz_regex = realloc(config->tz_regex, sizeof(*config->tz_regex) *
87 (config->nr_tz_regex + 1));
89 if (regcomp(&config->tz_regex[config->nr_tz_regex].regex, ".*",
90 REG_NOSUB | REG_EXTENDED)) {
91 ERROR("Invalid regular expression\n");
95 config->tz_regex[config->nr_tz_regex].polling = 250;
96 config->nr_tz_regex = 1;
101 static int configuration_init(const char *path, struct configuration *config)
105 config_setting_t *tz;
108 if (path && access(path, F_OK)) {
109 ERROR("'%s' is not accessible\n", path);
113 if (!path && !config->nr_tz_regex) {
114 INFO("No thermal zones configured, using wildcard for all of them\n");
115 return configuration_default_init(config);
120 if (!config_read_file(&cfg, path)) {
121 ERROR("Failed to parse %s:%d - %s\n", config_error_file(&cfg),
122 config_error_line(&cfg), config_error_text(&cfg));
127 tz = config_lookup(&cfg, "thermal-zones");
129 ERROR("No thermal zone configured to be monitored\n");
133 length = config_setting_length(tz);
135 INFO("Found %d thermal zone(s) regular expression\n", length);
137 for (i = 0; i < length; i++) {
139 config_setting_t *node;
143 node = config_setting_get_elem(tz, i);
145 ERROR("Missing node name '%d'\n", i);
149 if (!config_setting_lookup_string(node, "name", &name)) {
150 ERROR("Thermal zone name not found\n");
154 if (!config_setting_lookup_int(node, "polling", &polling)) {
155 ERROR("Polling value not found");
159 config->tz_regex = realloc(config->tz_regex, sizeof(*config->tz_regex) *
160 (config->nr_tz_regex + 1));
162 if (regcomp(&config->tz_regex[config->nr_tz_regex].regex, name,
163 REG_NOSUB | REG_EXTENDED)) {
164 ERROR("Invalid regular expression '%s'\n", name);
168 config->tz_regex[config->nr_tz_regex].polling = polling;
169 config->nr_tz_regex++;
171 INFO("Thermal zone regular expression '%s' with polling %d\n",
178 static void usage(const char *cmd)
180 printf("%s Version: %s\n", cmd, VERSION);
181 printf("Usage: %s [options]\n", cmd);
182 printf("\t-h, --help\t\tthis help\n");
183 printf("\t-o, --output <dir>\toutput directory for temperature capture\n");
184 printf("\t-c, --config <file>\tconfiguration file\n");
185 printf("\t-d, --duration <seconds>\tcapture duration\n");
186 printf("\t-l, --loglevel <level>\tlog level: ");
187 printf("DEBUG, INFO, NOTICE, WARN, ERROR\n");
188 printf("\t-p, --postfix <string>\tpostfix to be happened at the end of the files\n");
189 printf("\t-s, --syslog\t\toutput to syslog\n");
190 printf("\t-w, --overwrite\t\toverwrite the temperature capture files if they exist\n");
195 static int options_init(int argc, char *argv[], struct options *options)
198 time_t now = time(NULL);
200 struct option long_options[] = {
201 { "help", no_argument, NULL, 'h' },
202 { "config", required_argument, NULL, 'c' },
203 { "duration", required_argument, NULL, 'd' },
204 { "loglevel", required_argument, NULL, 'l' },
205 { "postfix", required_argument, NULL, 'p' },
206 { "output", required_argument, NULL, 'o' },
207 { "syslog", required_argument, NULL, 's' },
208 { "overwrite", no_argument, NULL, 'w' },
212 strftime(options->postfix, sizeof(options->postfix),
213 "-%Y-%m-%d_%H:%M:%S", gmtime(&now));
219 opt = getopt_long(argc, argv, "ho:c:d:l:p:sw", long_options, &optindex);
225 options->config = optarg;
228 options->duration = atoi(optarg) * 1000;
231 options->loglvl = log_str2level(optarg);
234 usage(basename(argv[0]));
237 strcpy(options->postfix, optarg);
240 strcpy(options->output, optarg);
243 options->logopt = TO_SYSLOG;
246 options->overwrite = 1;
249 ERROR("Usage: %s --help\n", argv[0]);
257 static int thermometer_add_tz(const char *path, const char *name, int polling,
258 struct thermometer *thermometer)
261 char tz_path[PATH_MAX];
263 sprintf(tz_path, CLASS_THERMAL"/%s/temp", path);
265 fd = open(tz_path, O_RDONLY);
267 ERROR("Failed to open '%s': %m\n", tz_path);
271 thermometer->tz = realloc(thermometer->tz,
272 sizeof(*thermometer->tz) * (thermometer->nr_tz + 1));
273 if (!thermometer->tz) {
274 ERROR("Failed to allocate thermometer->tz\n");
278 thermometer->tz[thermometer->nr_tz].fd_temp = fd;
279 thermometer->tz[thermometer->nr_tz].name = strdup(name);
280 thermometer->tz[thermometer->nr_tz].polling = polling;
281 thermometer->nr_tz++;
283 INFO("Added thermal zone '%s->%s (polling:%d)'\n", path, name, polling);
288 static int thermometer_init(struct configuration *config,
289 struct thermometer *thermometer)
292 struct dirent *dirent;
293 struct tz_regex *tz_regex;
294 const char *tz_dirname = "thermal_zone";
296 if (mainloop_init()) {
297 ERROR("Failed to start mainloop\n");
301 dir = opendir(CLASS_THERMAL);
303 ERROR("failed to open '%s'\n", CLASS_THERMAL);
307 while ((dirent = readdir(dir))) {
308 char tz_type[THERMAL_NAME_LENGTH];
309 char tz_path[PATH_MAX];
312 if (strncmp(dirent->d_name, tz_dirname, strlen(tz_dirname)))
315 sprintf(tz_path, CLASS_THERMAL"/%s/type", dirent->d_name);
317 tz_file = fopen(tz_path, "r");
319 ERROR("Failed to open '%s': %m", tz_path);
323 fscanf(tz_file, "%s", tz_type);
327 tz_regex = configuration_tz_match(tz_type, config);
331 if (thermometer_add_tz(dirent->d_name, tz_type,
332 tz_regex->polling, thermometer))
341 static int timer_temperature_callback(int fd, void *arg)
344 char buf[16] = { 0 };
346 pread(tz->fd_temp, buf, sizeof(buf), 0);
348 fprintf(tz->file_out, "%ld %s", getuptimeofday_ms(), buf);
350 read(fd, buf, sizeof(buf));
355 static int thermometer_start(struct thermometer *thermometer,
356 struct options *options)
358 struct itimerspec timer_it = { 0 };
363 INFO("Capturing %d thermal zone(s) temperature...\n", thermometer->nr_tz);
365 if (access(options->output, F_OK) && mkdir(options->output, 0700)) {
366 ERROR("Failed to create directory '%s'\n", options->output);
370 for (i = 0; i < thermometer->nr_tz; i++) {
372 asprintf(&path, "%s/%s%s", options->output,
373 thermometer->tz[i].name, options->postfix);
375 if (!options->overwrite && !access(path, F_OK)) {
376 ERROR("'%s' already exists\n", path);
380 f = fopen(path, "w");
382 ERROR("Failed to create '%s':%m\n", path);
386 fprintf(f, "timestamp(ms) %s(°mC)\n", thermometer->tz[i].name);
388 thermometer->tz[i].file_out = f;
390 DEBUG("Created '%s' file for thermal zone '%s'\n", path, thermometer->tz[i].name);
393 * Create polling timer
395 thermometer->tz[i].fd_timer = timerfd_create(CLOCK_MONOTONIC, 0);
396 if (thermometer->tz[i].fd_timer < 0) {
397 ERROR("Failed to create timer for '%s': %m\n",
398 thermometer->tz[i].name);
402 DEBUG("Watching '%s' every %d ms\n",
403 thermometer->tz[i].name, thermometer->tz[i].polling);
405 timer_it.it_interval = timer_it.it_value =
406 msec_to_timespec(thermometer->tz[i].polling);
408 if (timerfd_settime(thermometer->tz[i].fd_timer, 0,
409 &timer_it, NULL) < 0)
412 if (mainloop_add(thermometer->tz[i].fd_timer,
413 timer_temperature_callback,
414 &thermometer->tz[i]))
421 static int thermometer_execute(int argc, char *argv[], char *const envp[], pid_t *pid)
428 ERROR("Failed to fork process: %m");
433 execvpe(argv[0], argv, envp);
440 static int kill_process(__maybe_unused int fd, void *arg)
442 pid_t pid = *(pid_t *)arg;
444 if (kill(pid, SIGTERM))
445 ERROR("Failed to send SIGTERM signal to '%d': %p\n", pid);
446 else if (waitpid(pid, NULL, 0))
447 ERROR("Failed to wait pid '%d': %p\n", pid);
454 static int exit_mainloop(__maybe_unused int fd, __maybe_unused void *arg)
460 static int thermometer_wait(struct options *options, pid_t pid)
466 * If there is a duration specified, we will exit the mainloop
467 * and gracefully close all the files which will flush the
470 if (options->duration) {
471 struct itimerspec timer_it = { 0 };
473 timer_it.it_value = msec_to_timespec(options->duration);
475 fd = timerfd_create(CLOCK_MONOTONIC, 0);
477 ERROR("Failed to create duration timer: %m\n");
481 if (timerfd_settime(fd, 0, &timer_it, NULL)) {
482 ERROR("Failed to set timer time: %m\n");
486 if (mainloop_add(fd, pid < 0 ? exit_mainloop : kill_process, &pid)) {
487 ERROR("Failed to set timer exit mainloop callback\n");
493 * We want to catch any keyboard interrupt, as well as child
494 * signals if any in order to exit properly
497 sigaddset(&mask, SIGINT);
498 sigaddset(&mask, SIGQUIT);
499 sigaddset(&mask, SIGCHLD);
501 if (sigprocmask(SIG_BLOCK, &mask, NULL)) {
502 ERROR("Failed to set sigprocmask: %m\n");
506 fd = signalfd(-1, &mask, 0);
508 ERROR("Failed to set the signalfd: %m\n");
512 if (mainloop_add(fd, exit_mainloop, NULL)) {
513 ERROR("Failed to set timer exit mainloop callback\n");
520 static int thermometer_stop(struct thermometer *thermometer)
524 INFO("Closing/flushing output files\n");
526 for (i = 0; i < thermometer->nr_tz; i++)
527 fclose(thermometer->tz[i].file_out);
532 int main(int argc, char *argv[], char *const envp[])
534 struct options options = {
539 struct configuration config = { 0 };
540 struct thermometer thermometer = { 0 };
544 if (options_init(argc, argv, &options))
545 return THERMOMETER_OPTION_ERROR;
547 if (log_init(options.loglvl, argv[0], options.logopt))
548 return THERMOMETER_LOG_ERROR;
550 if (configuration_init(options.config, &config))
551 return THERMOMETER_CONFIG_ERROR;
553 if (uptimeofday_init())
554 return THERMOMETER_TIME_ERROR;
556 if (thermometer_init(&config, &thermometer))
557 return THERMOMETER_INIT_ERROR;
559 if (thermometer_start(&thermometer, &options))
560 return THERMOMETER_RUNTIME_ERROR;
562 if (thermometer_execute(argc - optind, &argv[optind], envp, &pid))
563 return THERMOMETER_RUNTIME_ERROR;
565 if (thermometer_wait(&options, pid))
566 return THERMOMETER_RUNTIME_ERROR;
568 if (thermometer_stop(&thermometer))
569 return THERMOMETER_RUNTIME_ERROR;
571 return THERMOMETER_SUCCESS;