tmon.c 8.6 KB

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