thermal-engine.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #include <syslog.h>
  20. #include <sys/epoll.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include <thermal.h>
  24. #include "thermal-tools.h"
  25. struct options {
  26. int loglevel;
  27. int logopt;
  28. int interactive;
  29. int daemonize;
  30. };
  31. struct thermal_data {
  32. struct thermal_zone *tz;
  33. struct thermal_handler *th;
  34. };
  35. static int show_threshold(struct thermal_threshold *th, __maybe_unused void *arg)
  36. {
  37. INFO("threshold temp=%d, direction=%d\n",
  38. th->temperature, th->direction);
  39. return 0;
  40. }
  41. static 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. return 0;
  46. }
  47. static int show_temp(struct thermal_zone *tz, __maybe_unused void *arg)
  48. {
  49. thermal_cmd_get_temp(arg, tz);
  50. INFO("temperature: %d\n", tz->temp);
  51. return 0;
  52. }
  53. static int show_governor(struct thermal_zone *tz, __maybe_unused void *arg)
  54. {
  55. thermal_cmd_get_governor(arg, tz);
  56. INFO("governor: '%s'\n", tz->governor);
  57. return 0;
  58. }
  59. static int show_tz(struct thermal_zone *tz, __maybe_unused void *arg)
  60. {
  61. INFO("thermal zone '%s', id=%d\n", tz->name, tz->id);
  62. for_each_thermal_trip(tz->trip, show_trip, NULL);
  63. for_each_thermal_threshold(tz->thresholds, show_threshold, NULL);
  64. show_temp(tz, arg);
  65. show_governor(tz, arg);
  66. return 0;
  67. }
  68. static int set_threshold(struct thermal_zone *tz, __maybe_unused void *arg)
  69. {
  70. struct thermal_handler *th = arg;
  71. int thresholds[] = { 43000, 65000, 49000, 55000, 57000 };
  72. size_t i;
  73. INFO("Setting threshold for thermal zone '%s', id=%d\n", tz->name, tz->id);
  74. if (thermal_cmd_threshold_flush(th, tz)) {
  75. ERROR("Failed to flush all previous thresholds\n");
  76. return -1;
  77. }
  78. for (i = 0; i < sizeof(thresholds) / sizeof(thresholds[0]); i++)
  79. if (thermal_cmd_threshold_add(th, tz, thresholds[i],
  80. THERMAL_THRESHOLD_WAY_UP |
  81. THERMAL_THRESHOLD_WAY_DOWN)) {
  82. ERROR("Failed to set threshold\n");
  83. return -1;
  84. }
  85. return 0;
  86. }
  87. static int tz_create(const char *name, int tz_id, __maybe_unused void *arg)
  88. {
  89. INFO("Thermal zone '%s'/%d created\n", name, tz_id);
  90. return 0;
  91. }
  92. static int tz_delete(int tz_id, __maybe_unused void *arg)
  93. {
  94. INFO("Thermal zone %d deleted\n", tz_id);
  95. return 0;
  96. }
  97. static int tz_disable(int tz_id, void *arg)
  98. {
  99. struct thermal_data *td = arg;
  100. struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
  101. INFO("Thermal zone %d ('%s') disabled\n", tz_id, tz->name);
  102. return 0;
  103. }
  104. static 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. INFO("Thermal zone %d ('%s') enabled\n", tz_id, tz->name);
  109. return 0;
  110. }
  111. static int trip_high(int tz_id, int trip_id, int temp, void *arg)
  112. {
  113. struct thermal_data *td = arg;
  114. struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
  115. INFO("Thermal zone %d ('%s'): trip point %d crossed way up with %d °C\n",
  116. tz_id, tz->name, trip_id, temp);
  117. return 0;
  118. }
  119. static int trip_low(int tz_id, int trip_id, int temp, void *arg)
  120. {
  121. struct thermal_data *td = arg;
  122. struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
  123. INFO("Thermal zone %d ('%s'): trip point %d crossed way down with %d °C\n",
  124. tz_id, tz->name, trip_id, temp);
  125. return 0;
  126. }
  127. static int trip_add(int tz_id, int trip_id, int type, int temp, int hyst, __maybe_unused void *arg)
  128. {
  129. INFO("Trip point added %d: id=%d, type=%d, temp=%d, hyst=%d\n",
  130. tz_id, trip_id, type, temp, hyst);
  131. return 0;
  132. }
  133. static int trip_delete(int tz_id, int trip_id, __maybe_unused void *arg)
  134. {
  135. INFO("Trip point deleted %d: id=%d\n", tz_id, trip_id);
  136. return 0;
  137. }
  138. static int trip_change(int tz_id, int trip_id, int type, int temp,
  139. int hyst, __maybe_unused void *arg)
  140. {
  141. struct thermal_data *td = arg;
  142. struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
  143. INFO("Trip point changed %d: id=%d, type=%d, temp=%d, hyst=%d\n",
  144. tz_id, trip_id, type, temp, hyst);
  145. tz->trip[trip_id].type = type;
  146. tz->trip[trip_id].temp = temp;
  147. tz->trip[trip_id].hyst = hyst;
  148. return 0;
  149. }
  150. static int cdev_add(const char *name, int cdev_id, int max_state, __maybe_unused void *arg)
  151. {
  152. INFO("Cooling device '%s'/%d (max state=%d) added\n", name, cdev_id, max_state);
  153. return 0;
  154. }
  155. static int cdev_delete(int cdev_id, __maybe_unused void *arg)
  156. {
  157. INFO("Cooling device %d deleted", cdev_id);
  158. return 0;
  159. }
  160. static int cdev_update(int cdev_id, int cur_state, __maybe_unused void *arg)
  161. {
  162. INFO("cdev:%d state:%d\n", cdev_id, cur_state);
  163. return 0;
  164. }
  165. static int gov_change(int tz_id, const char *name, __maybe_unused void *arg)
  166. {
  167. struct thermal_data *td = arg;
  168. struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
  169. INFO("%s: governor changed %s -> %s\n", tz->name, tz->governor, name);
  170. strcpy(tz->governor, name);
  171. return 0;
  172. }
  173. static int threshold_add(int tz_id, int temp, int direction, __maybe_unused void *arg)
  174. {
  175. INFO("Threshold added tz_id=%d: temp=%d, direction=%d\n", tz_id, temp, direction);
  176. return 0;
  177. }
  178. static int threshold_delete(int tz_id, int temp, int direction, __maybe_unused void *arg)
  179. {
  180. INFO("Threshold deleted tz_id=%d: temp=%d, direction=%d\n", tz_id, temp, direction);
  181. return 0;
  182. }
  183. static int threshold_flush(int tz_id, __maybe_unused void *arg)
  184. {
  185. INFO("Thresholds flushed tz_id=%d\n", tz_id);
  186. return 0;
  187. }
  188. static int threshold_up(int tz_id, int temp, int prev_temp, __maybe_unused void *arg)
  189. {
  190. INFO("Threshold crossed way up tz_id=%d: temp=%d, prev_temp=%d\n",
  191. tz_id, temp, prev_temp);
  192. return 0;
  193. }
  194. static int threshold_down(int tz_id, int temp, int prev_temp, __maybe_unused void *arg)
  195. {
  196. INFO("Threshold crossed way down tz_id=%d: temp=%d, prev_temp=%d\n",
  197. tz_id, temp, prev_temp);
  198. return 0;
  199. }
  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,
  214. .events.threshold_add = threshold_add,
  215. .events.threshold_delete = threshold_delete,
  216. .events.threshold_flush = threshold_flush,
  217. .events.threshold_up = threshold_up,
  218. .events.threshold_down = threshold_down,
  219. };
  220. static int thermal_event(__maybe_unused int fd, __maybe_unused void *arg)
  221. {
  222. struct thermal_data *td = arg;
  223. return thermal_events_handle(td->th, td);
  224. }
  225. static void usage(const char *cmd)
  226. {
  227. printf("%s : A thermal monitoring engine based on notifications\n", cmd);
  228. printf("Usage: %s [options]\n", cmd);
  229. printf("\t-h, --help\t\tthis help\n");
  230. printf("\t-d, --daemonize\n");
  231. printf("\t-l <level>, --loglevel <level>\tlog level: ");
  232. printf("DEBUG, INFO, NOTICE, WARN, ERROR\n");
  233. printf("\t-s, --syslog\t\toutput to syslog\n");
  234. printf("\n");
  235. exit(0);
  236. }
  237. static int options_init(int argc, char *argv[], struct options *options)
  238. {
  239. int opt;
  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. while (1) {
  248. int optindex = 0;
  249. opt = getopt_long(argc, argv, "l:dhs", long_options, &optindex);
  250. if (opt == -1)
  251. break;
  252. switch (opt) {
  253. case 'l':
  254. options->loglevel = log_str2level(optarg);
  255. break;
  256. case 'd':
  257. options->daemonize = 1;
  258. break;
  259. case 's':
  260. options->logopt = TO_SYSLOG;
  261. break;
  262. case 'h':
  263. usage(basename(argv[0]));
  264. break;
  265. default: /* '?' */
  266. return -1;
  267. }
  268. }
  269. return 0;
  270. }
  271. enum {
  272. THERMAL_ENGINE_SUCCESS = 0,
  273. THERMAL_ENGINE_OPTION_ERROR,
  274. THERMAL_ENGINE_DAEMON_ERROR,
  275. THERMAL_ENGINE_LOG_ERROR,
  276. THERMAL_ENGINE_THERMAL_ERROR,
  277. THERMAL_ENGINE_THRESHOLD_ERROR,
  278. THERMAL_ENGINE_MAINLOOP_ERROR,
  279. };
  280. int main(int argc, char *argv[])
  281. {
  282. struct thermal_data td;
  283. struct options options = {
  284. .loglevel = LOG_INFO,
  285. .logopt = TO_STDOUT,
  286. };
  287. if (options_init(argc, argv, &options)) {
  288. ERROR("Usage: %s --help\n", argv[0]);
  289. return THERMAL_ENGINE_OPTION_ERROR;
  290. }
  291. if (options.daemonize && daemon(0, 0)) {
  292. ERROR("Failed to daemonize: %m\n");
  293. return THERMAL_ENGINE_DAEMON_ERROR;
  294. }
  295. if (log_init(options.loglevel, basename(argv[0]), options.logopt)) {
  296. ERROR("Failed to initialize logging facility\n");
  297. return THERMAL_ENGINE_LOG_ERROR;
  298. }
  299. td.th = thermal_init(&ops);
  300. if (!td.th) {
  301. ERROR("Failed to initialize the thermal library\n");
  302. return THERMAL_ENGINE_THERMAL_ERROR;
  303. }
  304. td.tz = thermal_zone_discover(td.th);
  305. if (!td.tz) {
  306. ERROR("No thermal zone available\n");
  307. return THERMAL_ENGINE_THERMAL_ERROR;
  308. }
  309. for_each_thermal_zone(td.tz, set_threshold, td.th);
  310. for_each_thermal_zone(td.tz, show_tz, td.th);
  311. if (mainloop_init()) {
  312. ERROR("Failed to initialize the mainloop\n");
  313. return THERMAL_ENGINE_MAINLOOP_ERROR;
  314. }
  315. if (mainloop_add(thermal_events_fd(td.th), thermal_event, &td)) {
  316. ERROR("Failed to setup the mainloop\n");
  317. return THERMAL_ENGINE_MAINLOOP_ERROR;
  318. }
  319. INFO("Waiting for thermal events ...\n");
  320. if (mainloop(-1)) {
  321. ERROR("Mainloop failed\n");
  322. return THERMAL_ENGINE_MAINLOOP_ERROR;
  323. }
  324. return THERMAL_ENGINE_SUCCESS;
  325. }