thermal-engine.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_trip(struct thermal_trip *tt, __maybe_unused void *arg)
  36. {
  37. INFO("trip id=%d, type=%d, temp=%d, hyst=%d\n",
  38. tt->id, tt->type, tt->temp, tt->hyst);
  39. return 0;
  40. }
  41. static int show_temp(struct thermal_zone *tz, __maybe_unused void *arg)
  42. {
  43. thermal_cmd_get_temp(arg, tz);
  44. INFO("temperature: %d\n", tz->temp);
  45. return 0;
  46. }
  47. static int show_governor(struct thermal_zone *tz, __maybe_unused void *arg)
  48. {
  49. thermal_cmd_get_governor(arg, tz);
  50. INFO("governor: '%s'\n", tz->governor);
  51. return 0;
  52. }
  53. static int show_tz(struct thermal_zone *tz, __maybe_unused void *arg)
  54. {
  55. INFO("thermal zone '%s', id=%d\n", tz->name, tz->id);
  56. for_each_thermal_trip(tz->trip, show_trip, NULL);
  57. show_temp(tz, arg);
  58. show_governor(tz, arg);
  59. return 0;
  60. }
  61. static int tz_create(const char *name, int tz_id, __maybe_unused void *arg)
  62. {
  63. INFO("Thermal zone '%s'/%d created\n", name, tz_id);
  64. return 0;
  65. }
  66. static int tz_delete(int tz_id, __maybe_unused void *arg)
  67. {
  68. INFO("Thermal zone %d deleted\n", tz_id);
  69. return 0;
  70. }
  71. static int tz_disable(int tz_id, void *arg)
  72. {
  73. struct thermal_data *td = arg;
  74. struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
  75. INFO("Thermal zone %d ('%s') disabled\n", tz_id, tz->name);
  76. return 0;
  77. }
  78. static int tz_enable(int tz_id, void *arg)
  79. {
  80. struct thermal_data *td = arg;
  81. struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
  82. INFO("Thermal zone %d ('%s') enabled\n", tz_id, tz->name);
  83. return 0;
  84. }
  85. static int trip_high(int tz_id, int trip_id, int temp, void *arg)
  86. {
  87. struct thermal_data *td = arg;
  88. struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
  89. INFO("Thermal zone %d ('%s'): trip point %d crossed way up with %d °C\n",
  90. tz_id, tz->name, trip_id, temp);
  91. return 0;
  92. }
  93. static int trip_low(int tz_id, int trip_id, int temp, void *arg)
  94. {
  95. struct thermal_data *td = arg;
  96. struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
  97. INFO("Thermal zone %d ('%s'): trip point %d crossed way down with %d °C\n",
  98. tz_id, tz->name, trip_id, temp);
  99. return 0;
  100. }
  101. static int trip_add(int tz_id, int trip_id, int type, int temp, int hyst, __maybe_unused void *arg)
  102. {
  103. INFO("Trip point added %d: id=%d, type=%d, temp=%d, hyst=%d\n",
  104. tz_id, trip_id, type, temp, hyst);
  105. return 0;
  106. }
  107. static int trip_delete(int tz_id, int trip_id, __maybe_unused void *arg)
  108. {
  109. INFO("Trip point deleted %d: id=%d\n", tz_id, trip_id);
  110. return 0;
  111. }
  112. static int trip_change(int tz_id, int trip_id, int type, int temp,
  113. int hyst, __maybe_unused void *arg)
  114. {
  115. struct thermal_data *td = arg;
  116. struct thermal_zone *tz = thermal_zone_find_by_id(td->tz, tz_id);
  117. INFO("Trip point changed %d: id=%d, type=%d, temp=%d, hyst=%d\n",
  118. tz_id, trip_id, type, temp, hyst);
  119. tz->trip[trip_id].type = type;
  120. tz->trip[trip_id].temp = temp;
  121. tz->trip[trip_id].hyst = hyst;
  122. return 0;
  123. }
  124. static int cdev_add(const char *name, int cdev_id, int max_state, __maybe_unused void *arg)
  125. {
  126. INFO("Cooling device '%s'/%d (max state=%d) added\n", name, cdev_id, max_state);
  127. return 0;
  128. }
  129. static int cdev_delete(int cdev_id, __maybe_unused void *arg)
  130. {
  131. INFO("Cooling device %d deleted", cdev_id);
  132. return 0;
  133. }
  134. static int cdev_update(int cdev_id, int cur_state, __maybe_unused void *arg)
  135. {
  136. INFO("cdev:%d state:%d\n", cdev_id, cur_state);
  137. return 0;
  138. }
  139. static int gov_change(int tz_id, const char *name, __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("%s: governor changed %s -> %s\n", tz->name, tz->governor, name);
  144. strcpy(tz->governor, name);
  145. return 0;
  146. }
  147. static struct thermal_ops ops = {
  148. .events.tz_create = tz_create,
  149. .events.tz_delete = tz_delete,
  150. .events.tz_disable = tz_disable,
  151. .events.tz_enable = tz_enable,
  152. .events.trip_high = trip_high,
  153. .events.trip_low = trip_low,
  154. .events.trip_add = trip_add,
  155. .events.trip_delete = trip_delete,
  156. .events.trip_change = trip_change,
  157. .events.cdev_add = cdev_add,
  158. .events.cdev_delete = cdev_delete,
  159. .events.cdev_update = cdev_update,
  160. .events.gov_change = gov_change
  161. };
  162. static int thermal_event(__maybe_unused int fd, __maybe_unused void *arg)
  163. {
  164. struct thermal_data *td = arg;
  165. return thermal_events_handle(td->th, td);
  166. }
  167. static void usage(const char *cmd)
  168. {
  169. printf("%s : A thermal monitoring engine based on notifications\n", cmd);
  170. printf("Usage: %s [options]\n", cmd);
  171. printf("\t-h, --help\t\tthis help\n");
  172. printf("\t-d, --daemonize\n");
  173. printf("\t-l <level>, --loglevel <level>\tlog level: ");
  174. printf("DEBUG, INFO, NOTICE, WARN, ERROR\n");
  175. printf("\t-s, --syslog\t\toutput to syslog\n");
  176. printf("\n");
  177. exit(0);
  178. }
  179. static int options_init(int argc, char *argv[], struct options *options)
  180. {
  181. int opt;
  182. struct option long_options[] = {
  183. { "help", no_argument, NULL, 'h' },
  184. { "daemonize", no_argument, NULL, 'd' },
  185. { "syslog", no_argument, NULL, 's' },
  186. { "loglevel", required_argument, NULL, 'l' },
  187. { 0, 0, 0, 0 }
  188. };
  189. while (1) {
  190. int optindex = 0;
  191. opt = getopt_long(argc, argv, "l:dhs", long_options, &optindex);
  192. if (opt == -1)
  193. break;
  194. switch (opt) {
  195. case 'l':
  196. options->loglevel = log_str2level(optarg);
  197. break;
  198. case 'd':
  199. options->daemonize = 1;
  200. break;
  201. case 's':
  202. options->logopt = TO_SYSLOG;
  203. break;
  204. case 'h':
  205. usage(basename(argv[0]));
  206. break;
  207. default: /* '?' */
  208. return -1;
  209. }
  210. }
  211. return 0;
  212. }
  213. enum {
  214. THERMAL_ENGINE_SUCCESS = 0,
  215. THERMAL_ENGINE_OPTION_ERROR,
  216. THERMAL_ENGINE_DAEMON_ERROR,
  217. THERMAL_ENGINE_LOG_ERROR,
  218. THERMAL_ENGINE_THERMAL_ERROR,
  219. THERMAL_ENGINE_MAINLOOP_ERROR,
  220. };
  221. int main(int argc, char *argv[])
  222. {
  223. struct thermal_data td;
  224. struct options options = {
  225. .loglevel = LOG_INFO,
  226. .logopt = TO_STDOUT,
  227. };
  228. if (options_init(argc, argv, &options)) {
  229. ERROR("Usage: %s --help\n", argv[0]);
  230. return THERMAL_ENGINE_OPTION_ERROR;
  231. }
  232. if (options.daemonize && daemon(0, 0)) {
  233. ERROR("Failed to daemonize: %p\n");
  234. return THERMAL_ENGINE_DAEMON_ERROR;
  235. }
  236. if (log_init(options.loglevel, basename(argv[0]), options.logopt)) {
  237. ERROR("Failed to initialize logging facility\n");
  238. return THERMAL_ENGINE_LOG_ERROR;
  239. }
  240. td.th = thermal_init(&ops);
  241. if (!td.th) {
  242. ERROR("Failed to initialize the thermal library\n");
  243. return THERMAL_ENGINE_THERMAL_ERROR;
  244. }
  245. td.tz = thermal_zone_discover(td.th);
  246. if (!td.tz) {
  247. ERROR("No thermal zone available\n");
  248. return THERMAL_ENGINE_THERMAL_ERROR;
  249. }
  250. for_each_thermal_zone(td.tz, show_tz, td.th);
  251. if (mainloop_init()) {
  252. ERROR("Failed to initialize the mainloop\n");
  253. return THERMAL_ENGINE_MAINLOOP_ERROR;
  254. }
  255. if (mainloop_add(thermal_events_fd(td.th), thermal_event, &td)) {
  256. ERROR("Failed to setup the mainloop\n");
  257. return THERMAL_ENGINE_MAINLOOP_ERROR;
  258. }
  259. INFO("Waiting for thermal events ...\n");
  260. if (mainloop(-1)) {
  261. ERROR("Mainloop failed\n");
  262. return THERMAL_ENGINE_MAINLOOP_ERROR;
  263. }
  264. return THERMAL_ENGINE_SUCCESS;
  265. }