]> Git Repo - linux.git/blob - tools/thermal/thermal-engine/thermal-engine.c
Linux 6.14-rc3
[linux.git] / tools / thermal / thermal-engine / thermal-engine.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Thermal monitoring tool based on the thermal netlink events.
4  *
5  * Copyright (C) 2022 Linaro Ltd.
6  *
7  * Author: Daniel Lezcano <[email protected]>
8  */
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <getopt.h>
12 #include <libgen.h>
13 #include <limits.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <signal.h>
18 #include <unistd.h>
19
20 #include <syslog.h>
21
22 #include <sys/epoll.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25
26 #include <thermal.h>
27 #include "thermal-tools.h"
28
29 struct options {
30         int loglevel;
31         int logopt;
32         int interactive;
33         int daemonize;
34 };
35
36 struct thermal_data {
37         struct thermal_zone *tz;
38         struct thermal_handler *th;
39 };
40
41 static int show_threshold(struct thermal_threshold *th, __maybe_unused void *arg)
42 {
43         INFO("threshold temp=%d, direction=%d\n",
44              th->temperature, th->direction);
45
46         return 0;
47 }
48
49 static int show_trip(struct thermal_trip *tt, __maybe_unused void *arg)
50 {
51         INFO("trip id=%d, type=%d, temp=%d, hyst=%d\n",
52              tt->id, tt->type, tt->temp, tt->hyst);
53
54         return 0;
55 }
56
57 static int show_temp(struct thermal_zone *tz, __maybe_unused void *arg)
58 {
59         thermal_cmd_get_temp(arg, tz);
60
61         INFO("temperature: %d\n", tz->temp);
62
63         return 0;
64 }
65
66 static int show_governor(struct thermal_zone *tz, __maybe_unused void *arg)
67 {
68         thermal_cmd_get_governor(arg, tz);
69
70         INFO("governor: '%s'\n", tz->governor);
71
72         return 0;
73 }
74
75 static int show_tz(struct thermal_zone *tz, __maybe_unused void *arg)
76 {
77         INFO("thermal zone '%s', id=%d\n", tz->name, tz->id);
78
79         for_each_thermal_trip(tz->trip, show_trip, NULL);
80
81         for_each_thermal_threshold(tz->thresholds, show_threshold, NULL);
82
83         show_temp(tz, arg);
84
85         show_governor(tz, arg);
86
87         return 0;
88 }
89
90 static int set_threshold(struct thermal_zone *tz, __maybe_unused void *arg)
91 {
92         struct thermal_handler *th = arg;
93         int thresholds[] = { 43000, 65000, 49000, 55000, 57000 };
94         size_t i;
95
96         INFO("Setting threshold for thermal zone '%s', id=%d\n", tz->name, tz->id);
97
98         if (thermal_cmd_threshold_flush(th, tz)) {
99                 ERROR("Failed to flush all previous thresholds\n");
100                 return -1;
101         }
102
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");
108                         return -1;
109                 }
110
111         return 0;
112 }
113
114 static int tz_create(const char *name, int tz_id, __maybe_unused void *arg)
115 {
116         INFO("Thermal zone '%s'/%d created\n", name, tz_id);
117
118         return 0;
119 }
120
121 static int tz_delete(int tz_id, __maybe_unused void *arg)
122 {
123         INFO("Thermal zone %d deleted\n", tz_id);
124
125         return 0;
126 }
127
128 static int tz_disable(int tz_id, void *arg)
129 {
130         struct thermal_data *td = arg;
131         struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
132
133         INFO("Thermal zone %d ('%s') disabled\n", tz_id, tz->name);
134
135         return 0;
136 }
137
138 static int tz_enable(int tz_id, void *arg)
139 {
140         struct thermal_data *td = arg;
141         struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
142
143         INFO("Thermal zone %d ('%s') enabled\n", tz_id, tz->name);
144
145         return 0;
146 }
147
148 static int trip_high(int tz_id, int trip_id, int temp, void *arg)
149 {
150         struct thermal_data *td = arg;
151         struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
152
153         INFO("Thermal zone %d ('%s'): trip point %d crossed way up with %d °C\n",
154              tz_id, tz->name, trip_id, temp);
155
156         return 0;
157 }
158
159 static int trip_low(int tz_id, int trip_id, int temp, void *arg)
160 {
161         struct thermal_data *td = arg;
162         struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
163
164         INFO("Thermal zone %d ('%s'): trip point %d crossed way down with %d °C\n",
165              tz_id, tz->name, trip_id, temp);
166
167         return 0;
168 }
169
170 static int trip_add(int tz_id, int trip_id, int type, int temp, int hyst, __maybe_unused void *arg)
171 {
172         INFO("Trip point added %d: id=%d, type=%d, temp=%d, hyst=%d\n",
173              tz_id, trip_id, type, temp, hyst);
174
175         return 0;
176 }
177
178 static int trip_delete(int tz_id, int trip_id, __maybe_unused void *arg)
179 {
180         INFO("Trip point deleted %d: id=%d\n", tz_id, trip_id);
181
182         return 0;
183 }
184
185 static int trip_change(int tz_id, int trip_id, int type, int temp,
186                        int hyst, __maybe_unused void *arg)
187 {
188         struct thermal_data *td = arg;
189         struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
190
191         INFO("Trip point changed %d: id=%d, type=%d, temp=%d, hyst=%d\n",
192              tz_id, trip_id, type, temp, hyst);
193
194         tz->trip[trip_id].type = type;
195         tz->trip[trip_id].temp = temp;
196         tz->trip[trip_id].hyst = hyst;
197
198         return 0;
199 }
200
201 static int cdev_add(const char *name, int cdev_id, int max_state, __maybe_unused void *arg)
202 {
203         INFO("Cooling device '%s'/%d (max state=%d) added\n", name, cdev_id, max_state);
204
205         return 0;
206 }
207
208 static int cdev_delete(int cdev_id, __maybe_unused void *arg)
209 {
210         INFO("Cooling device %d deleted", cdev_id);
211
212         return 0;
213 }
214
215 static int cdev_update(int cdev_id, int cur_state, __maybe_unused void *arg)
216 {
217         INFO("cdev:%d state:%d\n", cdev_id, cur_state);
218
219         return 0;
220 }
221
222 static int gov_change(int tz_id, const char *name, __maybe_unused void *arg)
223 {
224         struct thermal_data *td = arg;
225         struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
226
227         INFO("%s: governor changed %s -> %s\n", tz->name, tz->governor, name);
228
229         strcpy(tz->governor, name);
230
231         return 0;
232 }
233
234 static int threshold_add(int tz_id, int temp, int direction, __maybe_unused void *arg)
235 {
236         INFO("Threshold added tz_id=%d: temp=%d, direction=%d\n", tz_id, temp, direction);
237
238         return 0;
239 }
240
241 static int threshold_delete(int tz_id, int temp, int direction, __maybe_unused void *arg)
242 {
243         INFO("Threshold deleted tz_id=%d: temp=%d, direction=%d\n", tz_id, temp, direction);
244
245         return 0;
246 }
247
248 static int threshold_flush(int tz_id, __maybe_unused void *arg)
249 {
250         INFO("Thresholds flushed tz_id=%d\n", tz_id);
251
252         return 0;
253 }
254
255 static int threshold_up(int tz_id, int temp, int prev_temp, __maybe_unused void *arg)
256 {
257         INFO("Threshold crossed way up tz_id=%d: temp=%d, prev_temp=%d\n",
258              tz_id, temp, prev_temp);
259
260         return 0;
261 }
262
263 static int threshold_down(int tz_id, int temp, int prev_temp, __maybe_unused void *arg)
264 {
265         INFO("Threshold crossed way down tz_id=%d: temp=%d, prev_temp=%d\n",
266              tz_id, temp, prev_temp);
267
268         return 0;
269 }
270
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,
290 };
291
292 static int thermal_event(__maybe_unused int fd, __maybe_unused void *arg)
293 {
294         struct thermal_data *td = arg;
295
296         return thermal_events_handle(td->th, td);
297 }
298
299 static void usage(const char *cmd)
300 {
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");
308         printf("\n");
309         exit(0);
310 }
311
312 static int options_init(int argc, char *argv[], struct options *options)
313 {
314         int opt;
315
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' },
321                 { 0, 0, 0, 0 }
322         };
323
324         while (1) {
325
326                 int optindex = 0;
327
328                 opt = getopt_long(argc, argv, "l:dhs", long_options, &optindex);
329                 if (opt == -1)
330                         break;
331
332                 switch (opt) {
333                 case 'l':
334                         options->loglevel = log_str2level(optarg);
335                         break;
336                 case 'd':
337                         options->daemonize = 1;
338                         break;
339                 case 's':
340                         options->logopt = TO_SYSLOG;
341                         break;
342                 case 'h':
343                         usage(basename(argv[0]));
344                         break;
345                 default: /* '?' */
346                         return -1;
347                 }
348         }
349
350         return 0;
351 }
352
353 enum {
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,
361 };
362
363 int main(int argc, char *argv[])
364 {
365         struct thermal_data td;
366         struct options options = {
367                 .loglevel = LOG_INFO,
368                 .logopt = TO_STDOUT,
369         };
370
371         if (options_init(argc, argv, &options)) {
372                 ERROR("Usage: %s --help\n", argv[0]);
373                 return THERMAL_ENGINE_OPTION_ERROR;
374         }
375
376         if (options.daemonize && daemon(0, 0)) {
377                 ERROR("Failed to daemonize: %p\n");
378                 return THERMAL_ENGINE_DAEMON_ERROR;
379         }
380
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;
384         }
385
386         td.th = thermal_init(&ops);
387         if (!td.th) {
388                 ERROR("Failed to initialize the thermal library\n");
389                 return THERMAL_ENGINE_THERMAL_ERROR;
390         }
391
392         td.tz = thermal_zone_discover(td.th);
393         if (!td.tz) {
394                 ERROR("No thermal zone available\n");
395                 return THERMAL_ENGINE_THERMAL_ERROR;
396         }
397
398         for_each_thermal_zone(td.tz, set_threshold, td.th);
399
400         for_each_thermal_zone(td.tz, show_tz, td.th);
401
402         if (mainloop_init()) {
403                 ERROR("Failed to initialize the mainloop\n");
404                 return THERMAL_ENGINE_MAINLOOP_ERROR;
405         }
406
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;
410         }
411
412         INFO("Waiting for thermal events ...\n");
413
414         if (mainloop(-1)) {
415                 ERROR("Mainloop failed\n");
416                 return THERMAL_ENGINE_MAINLOOP_ERROR;
417         }
418
419         return THERMAL_ENGINE_SUCCESS;
420 }
This page took 0.054635 seconds and 4 git commands to generate.