tmon.c 8.8 KB

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