tmon.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. if (!logging)
  131. return;
  132. /* open local data log file */
  133. tmon_log = fopen(TMON_LOG_FILE, "w+");
  134. if (!tmon_log) {
  135. syslog(LOG_ERR, "failed to open log file %s\n", TMON_LOG_FILE);
  136. return;
  137. }
  138. fprintf(tmon_log, "#----------- THERMAL SYSTEM CONFIG -------------\n");
  139. for (i = 0; i < ptdata.nr_tz_sensor; i++) {
  140. char binding_str[33]; /* size of long + 1 */
  141. int j;
  142. memset(binding_str, 0, sizeof(binding_str));
  143. for (j = 0; j < 32; j++)
  144. binding_str[j] = (ptdata.tzi[i].cdev_binding & 1<<j) ?
  145. '1' : '0';
  146. fprintf(tmon_log, "#thermal zone %s%02d cdevs binding: %32s\n",
  147. ptdata.tzi[i].type,
  148. ptdata.tzi[i].instance,
  149. binding_str);
  150. for (j = 0; j < ptdata.tzi[i].nr_trip_pts; j++) {
  151. fprintf(tmon_log, "#\tTP%02d type:%s, temp:%lu\n", j,
  152. trip_type_name[ptdata.tzi[i].tp[j].type],
  153. ptdata.tzi[i].tp[j].temp);
  154. }
  155. }
  156. for (i = 0; i < ptdata.nr_cooling_dev; i++)
  157. fprintf(tmon_log, "#cooling devices%02d: %s\n",
  158. i, ptdata.cdi[i].type);
  159. fprintf(tmon_log, "#---------- THERMAL DATA LOG STARTED -----------\n");
  160. fprintf(tmon_log, "Samples TargetTemp ");
  161. for (i = 0; i < ptdata.nr_tz_sensor; i++) {
  162. fprintf(tmon_log, "%s%d ", ptdata.tzi[i].type,
  163. ptdata.tzi[i].instance);
  164. }
  165. for (i = 0; i < ptdata.nr_cooling_dev; i++)
  166. fprintf(tmon_log, "%s%d ", ptdata.cdi[i].type,
  167. ptdata.cdi[i].instance);
  168. fprintf(tmon_log, "\n");
  169. }
  170. static struct option opts[] = {
  171. { "control", 1, NULL, 'c' },
  172. { "daemon", 0, NULL, 'd' },
  173. { "time-interval", 1, NULL, 't' },
  174. { "log", 0, NULL, 'l' },
  175. { "help", 0, NULL, 'h' },
  176. { "version", 0, NULL, 'v' },
  177. { "debug", 0, NULL, 'g' },
  178. { 0, 0, NULL, 0 }
  179. };
  180. int main(int argc, char **argv)
  181. {
  182. int err = 0;
  183. int id2 = 0, c;
  184. double yk = 0.0; /* controller output */
  185. int target_tz_index;
  186. if (geteuid() != 0) {
  187. printf("TMON needs to be run as root\n");
  188. exit(EXIT_FAILURE);
  189. }
  190. while ((c = getopt_long(argc, argv, "c:dlht:vgz:", opts, &id2)) != -1) {
  191. switch (c) {
  192. case 'c':
  193. no_control = 0;
  194. strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE);
  195. break;
  196. case 'd':
  197. start_daemon_mode();
  198. printf("Run TMON in daemon mode\n");
  199. break;
  200. case 't':
  201. ticktime = strtod(optarg, NULL);
  202. if (ticktime < 1)
  203. ticktime = 1;
  204. break;
  205. case 'l':
  206. printf("Logging data to /var/tmp/tmon.log\n");
  207. logging = 1;
  208. break;
  209. case 'h':
  210. usage();
  211. break;
  212. case 'v':
  213. version();
  214. break;
  215. case 'g':
  216. debug_on = 1;
  217. break;
  218. case 'z':
  219. target_thermal_zone = strtod(optarg, NULL);
  220. break;
  221. default:
  222. break;
  223. }
  224. }
  225. if (pthread_mutex_init(&input_lock, NULL) != 0) {
  226. fprintf(stderr, "\n mutex init failed, exit\n");
  227. return 1;
  228. }
  229. start_syslog();
  230. if (signal(SIGINT, tmon_sig_handler) == SIG_ERR)
  231. syslog(LOG_DEBUG, "Cannot handle SIGINT\n");
  232. if (signal(SIGTERM, tmon_sig_handler) == SIG_ERR)
  233. syslog(LOG_DEBUG, "Cannot handle SIGINT\n");
  234. if (probe_thermal_sysfs()) {
  235. pthread_mutex_destroy(&input_lock);
  236. closelog();
  237. return -1;
  238. }
  239. initialize_curses();
  240. setup_windows();
  241. signal(SIGWINCH, resize_handler);
  242. show_title_bar();
  243. show_sensors_w();
  244. show_cooling_device();
  245. update_thermal_data();
  246. show_data_w();
  247. prepare_logging();
  248. init_thermal_controller();
  249. nodelay(stdscr, TRUE);
  250. err = pthread_create(&event_tid, NULL, &handle_tui_events, NULL);
  251. if (err != 0) {
  252. printf("\ncan't create thread :[%s]", strerror(err));
  253. tmon_cleanup();
  254. exit(EXIT_FAILURE);
  255. }
  256. /* validate range of user selected target zone, default to the first
  257. * instance if out of range
  258. */
  259. target_tz_index = zone_instance_to_index(target_thermal_zone);
  260. if (target_tz_index < 0) {
  261. target_thermal_zone = ptdata.tzi[0].instance;
  262. syslog(LOG_ERR, "target zone is not found, default to %d\n",
  263. target_thermal_zone);
  264. }
  265. while (1) {
  266. sleep(ticktime);
  267. show_title_bar();
  268. show_sensors_w();
  269. update_thermal_data();
  270. if (!dialogue_on) {
  271. show_data_w();
  272. show_cooling_device();
  273. }
  274. cur_thermal_record++;
  275. time_elapsed += ticktime;
  276. controller_handler(trec[0].temp[target_tz_index] / 1000,
  277. &yk);
  278. trec[0].pid_out_pct = yk;
  279. if (!dialogue_on)
  280. show_control_w();
  281. if (tmon_exit)
  282. break;
  283. }
  284. tmon_cleanup();
  285. return 0;
  286. }
  287. static void start_daemon_mode()
  288. {
  289. daemon_mode = 1;
  290. /* fork */
  291. pid_t sid, pid = fork();
  292. if (pid < 0) {
  293. exit(EXIT_FAILURE);
  294. } else if (pid > 0)
  295. /* kill parent */
  296. exit(EXIT_SUCCESS);
  297. /* disable TUI, it may not be necessary, but saves some resource */
  298. disable_tui();
  299. /* change the file mode mask */
  300. umask(0);
  301. /* new SID for the daemon process */
  302. sid = setsid();
  303. if (sid < 0)
  304. exit(EXIT_FAILURE);
  305. /* change working directory */
  306. if ((chdir("/")) < 0)
  307. exit(EXIT_FAILURE);
  308. sleep(10);
  309. close(STDIN_FILENO);
  310. close(STDOUT_FILENO);
  311. close(STDERR_FILENO);
  312. }