Loading...
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 <daniel.lezcano@kernel.org>
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
29struct options {
30 int loglevel;
31 int logopt;
32 int interactive;
33 int daemonize;
34};
35
36struct thermal_data {
37 struct thermal_zone *tz;
38 struct thermal_handler *th;
39};
40
41static int show_trip(struct thermal_trip *tt, __maybe_unused void *arg)
42{
43 INFO("trip id=%d, type=%d, temp=%d, hyst=%d\n",
44 tt->id, tt->type, tt->temp, tt->hyst);
45
46 return 0;
47}
48
49static int show_temp(struct thermal_zone *tz, __maybe_unused void *arg)
50{
51 thermal_cmd_get_temp(arg, tz);
52
53 INFO("temperature: %d\n", tz->temp);
54
55 return 0;
56}
57
58static int show_governor(struct thermal_zone *tz, __maybe_unused void *arg)
59{
60 thermal_cmd_get_governor(arg, tz);
61
62 INFO("governor: '%s'\n", tz->governor);
63
64 return 0;
65}
66
67static int show_tz(struct thermal_zone *tz, __maybe_unused void *arg)
68{
69 INFO("thermal zone '%s', id=%d\n", tz->name, tz->id);
70
71 for_each_thermal_trip(tz->trip, show_trip, NULL);
72
73 show_temp(tz, arg);
74
75 show_governor(tz, arg);
76
77 return 0;
78}
79
80static int tz_create(const char *name, int tz_id, __maybe_unused void *arg)
81{
82 INFO("Thermal zone '%s'/%d created\n", name, tz_id);
83
84 return 0;
85}
86
87static int tz_delete(int tz_id, __maybe_unused void *arg)
88{
89 INFO("Thermal zone %d deleted\n", tz_id);
90
91 return 0;
92}
93
94static int tz_disable(int tz_id, void *arg)
95{
96 struct thermal_data *td = arg;
97 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
98
99 INFO("Thermal zone %d ('%s') disabled\n", tz_id, tz->name);
100
101 return 0;
102}
103
104static int tz_enable(int tz_id, void *arg)
105{
106 struct thermal_data *td = arg;
107 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
108
109 INFO("Thermal zone %d ('%s') enabled\n", tz_id, tz->name);
110
111 return 0;
112}
113
114static int trip_high(int tz_id, int trip_id, int temp, void *arg)
115{
116 struct thermal_data *td = arg;
117 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
118
119 INFO("Thermal zone %d ('%s'): trip point %d crossed way up with %d °C\n",
120 tz_id, tz->name, trip_id, temp);
121
122 return 0;
123}
124
125static int trip_low(int tz_id, int trip_id, int temp, void *arg)
126{
127 struct thermal_data *td = arg;
128 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
129
130 INFO("Thermal zone %d ('%s'): trip point %d crossed way down with %d °C\n",
131 tz_id, tz->name, trip_id, temp);
132
133 return 0;
134}
135
136static int trip_add(int tz_id, int trip_id, int type, int temp, int hyst, __maybe_unused void *arg)
137{
138 INFO("Trip point added %d: id=%d, type=%d, temp=%d, hyst=%d\n",
139 tz_id, trip_id, type, temp, hyst);
140
141 return 0;
142}
143
144static int trip_delete(int tz_id, int trip_id, __maybe_unused void *arg)
145{
146 INFO("Trip point deleted %d: id=%d\n", tz_id, trip_id);
147
148 return 0;
149}
150
151static int trip_change(int tz_id, int trip_id, int type, int temp,
152 int hyst, __maybe_unused void *arg)
153{
154 struct thermal_data *td = arg;
155 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
156
157 INFO("Trip point changed %d: id=%d, type=%d, temp=%d, hyst=%d\n",
158 tz_id, trip_id, type, temp, hyst);
159
160 tz->trip[trip_id].type = type;
161 tz->trip[trip_id].temp = temp;
162 tz->trip[trip_id].hyst = hyst;
163
164 return 0;
165}
166
167static int cdev_add(const char *name, int cdev_id, int max_state, __maybe_unused void *arg)
168{
169 INFO("Cooling device '%s'/%d (max state=%d) added\n", name, cdev_id, max_state);
170
171 return 0;
172}
173
174static int cdev_delete(int cdev_id, __maybe_unused void *arg)
175{
176 INFO("Cooling device %d deleted", cdev_id);
177
178 return 0;
179}
180
181static int cdev_update(int cdev_id, int cur_state, __maybe_unused void *arg)
182{
183 INFO("cdev:%d state:%d\n", cdev_id, cur_state);
184
185 return 0;
186}
187
188static int gov_change(int tz_id, const char *name, __maybe_unused void *arg)
189{
190 struct thermal_data *td = arg;
191 struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
192
193 INFO("%s: governor changed %s -> %s\n", tz->name, tz->governor, name);
194
195 strcpy(tz->governor, name);
196
197 return 0;
198}
199
200static 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
214};
215
216static int thermal_event(__maybe_unused int fd, __maybe_unused void *arg)
217{
218 struct thermal_data *td = arg;
219
220 return thermal_events_handle(td->th, td);
221}
222
223static void usage(const char *cmd)
224{
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");
232 printf("\n");
233 exit(0);
234}
235
236static int options_init(int argc, char *argv[], struct options *options)
237{
238 int opt;
239
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' },
245 { 0, 0, 0, 0 }
246 };
247
248 while (1) {
249
250 int optindex = 0;
251
252 opt = getopt_long(argc, argv, "l:dhs", long_options, &optindex);
253 if (opt == -1)
254 break;
255
256 switch (opt) {
257 case 'l':
258 options->loglevel = log_str2level(optarg);
259 break;
260 case 'd':
261 options->daemonize = 1;
262 break;
263 case 's':
264 options->logopt = TO_SYSLOG;
265 break;
266 case 'h':
267 usage(basename(argv[0]));
268 break;
269 default: /* '?' */
270 return -1;
271 }
272 }
273
274 return 0;
275}
276
277enum {
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,
284};
285
286int main(int argc, char *argv[])
287{
288 struct thermal_data td;
289 struct options options = {
290 .loglevel = LOG_INFO,
291 .logopt = TO_STDOUT,
292 };
293
294 if (options_init(argc, argv, &options)) {
295 ERROR("Usage: %s --help\n", argv[0]);
296 return THERMAL_ENGINE_OPTION_ERROR;
297 }
298
299 if (options.daemonize && daemon(0, 0)) {
300 ERROR("Failed to daemonize: %p\n");
301 return THERMAL_ENGINE_DAEMON_ERROR;
302 }
303
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;
307 }
308
309 td.th = thermal_init(&ops);
310 if (!td.th) {
311 ERROR("Failed to initialize the thermal library\n");
312 return THERMAL_ENGINE_THERMAL_ERROR;
313 }
314
315 td.tz = thermal_zone_discover(td.th);
316 if (!td.tz) {
317 ERROR("No thermal zone available\n");
318 return THERMAL_ENGINE_THERMAL_ERROR;
319 }
320
321 for_each_thermal_zone(td.tz, show_tz, td.th);
322
323 if (mainloop_init()) {
324 ERROR("Failed to initialize the mainloop\n");
325 return THERMAL_ENGINE_MAINLOOP_ERROR;
326 }
327
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;
331 }
332
333 INFO("Waiting for thermal events ...\n");
334
335 if (mainloop(-1)) {
336 ERROR("Mainloop failed\n");
337 return THERMAL_ENGINE_MAINLOOP_ERROR;
338 }
339
340 return THERMAL_ENGINE_SUCCESS;
341}
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 <daniel.lezcano@kernel.org>
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
29struct options {
30 int loglevel;
31 int logopt;
32 int interactive;
33 int daemonize;
34};
35
36struct thermal_data {
37 struct thermal_zone *tz;
38 struct thermal_handler *th;
39};
40
41static 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
49static 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
57static 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
66static 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
75static 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
90static 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
114static 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
121static 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
128static 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
138static 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
148static 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
159static 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
170static 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
178static 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
185static 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
201static 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
208static 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
215static 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
222static 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
234static 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
241static 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
248static 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
255static 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
263static 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
271static 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
292static 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
299static 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
312static 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
353enum {
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
363int 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}