tmon.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * tmon.c Thermal Monitor (TMON) main function and entry point
  4. *
  5. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  6. *
  7. * Author: Jacob Pan <jacob.jun.pan@linux.intel.com>
  8. */
  9. #include <getopt.h>
  10. #include <unistd.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <ncurses.h>
  17. #include <ctype.h>
  18. #include <time.h>
  19. #include <signal.h>
  20. #include <limits.h>
  21. #include <sys/time.h>
  22. #include <pthread.h>
  23. #include <math.h>
  24. #include <stdarg.h>
  25. #include <syslog.h>
  26. #include "tmon.h"
  27. unsigned long ticktime = 1; /* seconds */
  28. unsigned long no_control = 1; /* monitoring only or use cooling device for
  29. * temperature control.
  30. */
  31. double time_elapsed = 0.0;
  32. unsigned long target_temp_user = 65; /* can be select by tui later */
  33. int dialogue_on;
  34. int tmon_exit;
  35. static short daemon_mode;
  36. static int logging; /* for recording thermal data to a file */
  37. static int debug_on;
  38. FILE *tmon_log;
  39. /*cooling device used for the PID controller */
  40. char ctrl_cdev[CDEV_NAME_SIZE] = "None";
  41. int target_thermal_zone; /* user selected target zone instance */
  42. static void start_daemon_mode(void);
  43. pthread_t event_tid;
  44. pthread_mutex_t input_lock;
  45. void usage(void)
  46. {
  47. printf("Usage: tmon [OPTION...]\n");
  48. printf(" -c, --control cooling device in control\n");
  49. printf(" -d, --daemon run as daemon, no TUI\n");
  50. printf(" -g, --debug debug message in syslog\n");
  51. printf(" -h, --help show this help message\n");
  52. printf(" -l, --log log data to /var/tmp/tmon.log\n");
  53. printf(" -t, --time-interval sampling time interval, > 1 sec.\n");
  54. printf(" -T, --target-temp initial target temperature\n");
  55. printf(" -v, --version show version\n");
  56. printf(" -z, --zone target thermal zone id\n");
  57. exit(0);
  58. }
  59. void version(void)
  60. {
  61. printf("TMON version %s\n", VERSION);
  62. exit(EXIT_SUCCESS);
  63. }
  64. static void tmon_cleanup(void)
  65. {
  66. syslog(LOG_INFO, "TMON exit cleanup\n");
  67. fflush(stdout);
  68. refresh();
  69. if (tmon_log)
  70. fclose(tmon_log);
  71. if (event_tid) {
  72. pthread_mutex_lock(&input_lock);
  73. pthread_cancel(event_tid);
  74. pthread_mutex_unlock(&input_lock);
  75. pthread_mutex_destroy(&input_lock);
  76. }
  77. closelog();
  78. /* relax control knobs, undo throttling */
  79. set_ctrl_state(0);
  80. keypad(stdscr, FALSE);
  81. echo();
  82. nocbreak();
  83. close_windows();
  84. endwin();
  85. free_thermal_data();
  86. exit(1);
  87. }
  88. static void tmon_sig_handler(int sig)
  89. {
  90. syslog(LOG_INFO, "TMON caught signal %d\n", sig);
  91. refresh();
  92. switch (sig) {
  93. case SIGTERM:
  94. printf("sigterm, exit and clean up\n");
  95. fflush(stdout);
  96. break;
  97. case SIGKILL:
  98. printf("sigkill, exit and clean up\n");
  99. fflush(stdout);
  100. break;
  101. case SIGINT:
  102. printf("ctrl-c, exit and clean up\n");
  103. fflush(stdout);
  104. break;
  105. default:
  106. break;
  107. }
  108. tmon_exit = true;
  109. }
  110. static void start_syslog(void)
  111. {
  112. if (debug_on)
  113. setlogmask(LOG_UPTO(LOG_DEBUG));
  114. else
  115. setlogmask(LOG_UPTO(LOG_ERR));
  116. openlog("tmon.log", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);
  117. syslog(LOG_NOTICE, "TMON started by User %d", getuid());
  118. }
  119. static void prepare_logging(void)
  120. {
  121. int i;
  122. struct stat logstat;
  123. if (!logging)
  124. return;
  125. /* open local data log file */
  126. tmon_log = fopen(TMON_LOG_FILE, "w+");
  127. if (!tmon_log) {
  128. syslog(LOG_ERR, "failed to open log file %s\n", TMON_LOG_FILE);
  129. return;
  130. }
  131. if (lstat(TMON_LOG_FILE, &logstat) < 0) {
  132. syslog(LOG_ERR, "Unable to stat log file %s\n", TMON_LOG_FILE);
  133. fclose(tmon_log);
  134. tmon_log = NULL;
  135. return;
  136. }
  137. /* The log file must be a regular file owned by us */
  138. if (S_ISLNK(logstat.st_mode)) {
  139. syslog(LOG_ERR, "Log file is a symlink. Will not log\n");
  140. fclose(tmon_log);
  141. tmon_log = NULL;
  142. return;
  143. }
  144. if (logstat.st_uid != getuid()) {
  145. syslog(LOG_ERR, "We don't own the log file. Not logging\n");
  146. fclose(tmon_log);
  147. tmon_log = NULL;
  148. return;
  149. }
  150. fprintf(tmon_log, "#----------- THERMAL SYSTEM CONFIG -------------\n");
  151. for (i = 0; i < ptdata.nr_tz_sensor; i++) {
  152. char binding_str[33]; /* size of long + 1 */
  153. int j;
  154. memset(binding_str, 0, sizeof(binding_str));
  155. for (j = 0; j < 32; j++)
  156. binding_str[j] = (ptdata.tzi[i].cdev_binding & (1 << j)) ?
  157. '1' : '0';
  158. fprintf(tmon_log, "#thermal zone %s%02d cdevs binding: %32s\n",
  159. ptdata.tzi[i].type,
  160. ptdata.tzi[i].instance,
  161. binding_str);
  162. for (j = 0; j < ptdata.tzi[i].nr_trip_pts; j++) {
  163. fprintf(tmon_log, "#\tTP%02d type:%s, temp:%lu\n", j,
  164. trip_type_name[ptdata.tzi[i].tp[j].type],
  165. ptdata.tzi[i].tp[j].temp);
  166. }
  167. }
  168. for (i = 0; i < ptdata.nr_cooling_dev; i++)
  169. fprintf(tmon_log, "#cooling devices%02d: %s\n",
  170. i, ptdata.cdi[i].type);
  171. fprintf(tmon_log, "#---------- THERMAL DATA LOG STARTED -----------\n");
  172. fprintf(tmon_log, "Samples TargetTemp ");
  173. for (i = 0; i < ptdata.nr_tz_sensor; i++) {
  174. fprintf(tmon_log, "%s%d ", ptdata.tzi[i].type,
  175. ptdata.tzi[i].instance);
  176. }
  177. for (i = 0; i < ptdata.nr_cooling_dev; i++)
  178. fprintf(tmon_log, "%s%d ", ptdata.cdi[i].type,
  179. ptdata.cdi[i].instance);
  180. fprintf(tmon_log, "\n");
  181. }
  182. static struct option opts[] = {
  183. { "control", 1, NULL, 'c' },
  184. { "daemon", 0, NULL, 'd' },
  185. { "time-interval", 1, NULL, 't' },
  186. { "target-temp", 1, NULL, 'T' },
  187. { "log", 0, NULL, 'l' },
  188. { "help", 0, NULL, 'h' },
  189. { "version", 0, NULL, 'v' },
  190. { "debug", 0, NULL, 'g' },
  191. { 0, 0, NULL, 0 }
  192. };
  193. int main(int argc, char **argv)
  194. {
  195. int err = 0;
  196. int id2 = 0, c;
  197. double yk = 0.0, temp; /* controller output */
  198. int target_tz_index;
  199. if (geteuid() != 0) {
  200. printf("TMON needs to be run as root\n");
  201. exit(EXIT_FAILURE);
  202. }
  203. while ((c = getopt_long(argc, argv, "c:dlht:T:vgz:", opts, &id2)) != -1) {
  204. switch (c) {
  205. case 'c':
  206. no_control = 0;
  207. strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE);
  208. break;
  209. case 'd':
  210. start_daemon_mode();
  211. printf("Run TMON in daemon mode\n");
  212. break;
  213. case 't':
  214. ticktime = strtod(optarg, NULL);
  215. if (ticktime < 1)
  216. ticktime = 1;
  217. break;
  218. case 'T':
  219. temp = strtod(optarg, NULL);
  220. if (temp < 0) {
  221. fprintf(stderr, "error: temperature must be positive\n");
  222. return 1;
  223. }
  224. target_temp_user = temp;
  225. break;
  226. case 'l':
  227. printf("Logging data to /var/tmp/tmon.log\n");
  228. logging = 1;
  229. break;
  230. case 'h':
  231. usage();
  232. break;
  233. case 'v':
  234. version();
  235. break;
  236. case 'g':
  237. debug_on = 1;
  238. break;
  239. case 'z':
  240. target_thermal_zone = strtod(optarg, NULL);
  241. break;
  242. default:
  243. break;
  244. }
  245. }
  246. if (pthread_mutex_init(&input_lock, NULL) != 0) {
  247. fprintf(stderr, "\n mutex init failed, exit\n");
  248. return 1;
  249. }
  250. start_syslog();
  251. if (signal(SIGINT, tmon_sig_handler) == SIG_ERR)
  252. syslog(LOG_DEBUG, "Cannot handle SIGINT\n");
  253. if (signal(SIGTERM, tmon_sig_handler) == SIG_ERR)
  254. syslog(LOG_DEBUG, "Cannot handle SIGTERM\n");
  255. if (probe_thermal_sysfs()) {
  256. pthread_mutex_destroy(&input_lock);
  257. closelog();
  258. return -1;
  259. }
  260. initialize_curses();
  261. setup_windows();
  262. signal(SIGWINCH, resize_handler);
  263. show_title_bar();
  264. show_sensors_w();
  265. show_cooling_device();
  266. update_thermal_data();
  267. show_data_w();
  268. prepare_logging();
  269. init_thermal_controller();
  270. nodelay(stdscr, TRUE);
  271. err = pthread_create(&event_tid, NULL, &handle_tui_events, NULL);
  272. if (err != 0) {
  273. printf("\ncan't create thread :[%s]", strerror(err));
  274. tmon_cleanup();
  275. exit(EXIT_FAILURE);
  276. }
  277. /* validate range of user selected target zone, default to the first
  278. * instance if out of range
  279. */
  280. target_tz_index = zone_instance_to_index(target_thermal_zone);
  281. if (target_tz_index < 0) {
  282. target_thermal_zone = ptdata.tzi[0].instance;
  283. syslog(LOG_ERR, "target zone is not found, default to %d\n",
  284. target_thermal_zone);
  285. }
  286. while (1) {
  287. sleep(ticktime);
  288. show_title_bar();
  289. show_sensors_w();
  290. update_thermal_data();
  291. if (!dialogue_on) {
  292. show_data_w();
  293. show_cooling_device();
  294. }
  295. time_elapsed += ticktime;
  296. controller_handler(trec[0].temp[target_tz_index] / 1000, &yk);
  297. trec[0].pid_out_pct = yk;
  298. if (!dialogue_on)
  299. show_control_w();
  300. if (tmon_exit)
  301. break;
  302. }
  303. tmon_cleanup();
  304. return 0;
  305. }
  306. static void start_daemon_mode(void)
  307. {
  308. daemon_mode = 1;
  309. /* fork */
  310. pid_t sid, pid = fork();
  311. if (pid < 0)
  312. exit(EXIT_FAILURE);
  313. else if (pid > 0)
  314. /* kill parent */
  315. exit(EXIT_SUCCESS);
  316. /* disable TUI, it may not be necessary, but saves some resource */
  317. disable_tui();
  318. /* change the file mode mask */
  319. umask(S_IWGRP | S_IWOTH);
  320. /* new SID for the daemon process */
  321. sid = setsid();
  322. if (sid < 0)
  323. exit(EXIT_FAILURE);
  324. /* change working directory */
  325. if ((chdir("/")) < 0)
  326. exit(EXIT_FAILURE);
  327. sleep(10);
  328. close(STDIN_FILENO);
  329. close(STDOUT_FILENO);
  330. close(STDERR_FILENO);
  331. }