2 * tmon.c Thermal Monitor (TMON) main function and entry point
4 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
15 * Author: Jacob Pan <jacob.jun.pan@linux.intel.com>
24 #include <sys/types.h>
39 unsigned long ticktime
= 1; /* seconds */
40 unsigned long no_control
= 1; /* monitoring only or use cooling device for
41 * temperature control.
43 double time_elapsed
= 0.0;
44 unsigned long target_temp_user
= 65; /* can be select by tui later */
47 static short daemon_mode
;
48 static int logging
; /* for recording thermal data to a file */
51 /*cooling device used for the PID controller */
52 char ctrl_cdev
[CDEV_NAME_SIZE
] = "None";
53 int target_thermal_zone
; /* user selected target zone instance */
54 static void start_daemon_mode(void);
57 pthread_mutex_t input_lock
;
60 printf("Usage: tmon [OPTION...]\n");
61 printf(" -c, --control cooling device in control\n");
62 printf(" -d, --daemon run as daemon, no TUI\n");
63 printf(" -g, --debug debug message in syslog\n");
64 printf(" -h, --help show this help message\n");
65 printf(" -l, --log log data to /var/tmp/tmon.log\n");
66 printf(" -t, --time-interval sampling time interval, > 1 sec.\n");
67 printf(" -T, --target-temp initial target temperature\n");
68 printf(" -v, --version show version\n");
69 printf(" -z, --zone target thermal zone id\n");
76 printf("TMON version %s\n", VERSION
);
80 static void tmon_cleanup(void)
83 syslog(LOG_INFO
, "TMON exit cleanup\n");
89 pthread_mutex_lock(&input_lock
);
90 pthread_cancel(event_tid
);
91 pthread_mutex_unlock(&input_lock
);
92 pthread_mutex_destroy(&input_lock
);
95 /* relax control knobs, undo throttling */
98 keypad(stdscr
, FALSE
);
109 static void tmon_sig_handler(int sig
)
111 syslog(LOG_INFO
, "TMON caught signal %d\n", sig
);
115 printf("sigterm, exit and clean up\n");
119 printf("sigkill, exit and clean up\n");
123 printf("ctrl-c, exit and clean up\n");
133 static void start_syslog(void)
136 setlogmask(LOG_UPTO(LOG_DEBUG
));
138 setlogmask(LOG_UPTO(LOG_ERR
));
139 openlog("tmon.log", LOG_CONS
| LOG_PID
| LOG_NDELAY
, LOG_LOCAL0
);
140 syslog(LOG_NOTICE
, "TMON started by User %d", getuid());
143 static void prepare_logging(void)
150 /* open local data log file */
151 tmon_log
= fopen(TMON_LOG_FILE
, "w+");
153 syslog(LOG_ERR
, "failed to open log file %s\n", TMON_LOG_FILE
);
157 if (lstat(TMON_LOG_FILE
, &logstat
) < 0) {
158 syslog(LOG_ERR
, "Unable to stat log file %s\n", TMON_LOG_FILE
);
164 /* The log file must be a regular file owned by us */
165 if (S_ISLNK(logstat
.st_mode
)) {
166 syslog(LOG_ERR
, "Log file is a symlink. Will not log\n");
172 if (logstat
.st_uid
!= getuid()) {
173 syslog(LOG_ERR
, "We don't own the log file. Not logging\n");
180 fprintf(tmon_log
, "#----------- THERMAL SYSTEM CONFIG -------------\n");
181 for (i
= 0; i
< ptdata
.nr_tz_sensor
; i
++) {
182 char binding_str
[33]; /* size of long + 1 */
185 memset(binding_str
, 0, sizeof(binding_str
));
186 for (j
= 0; j
< 32; j
++)
187 binding_str
[j
] = (ptdata
.tzi
[i
].cdev_binding
& 1<<j
) ?
190 fprintf(tmon_log
, "#thermal zone %s%02d cdevs binding: %32s\n",
192 ptdata
.tzi
[i
].instance
,
194 for (j
= 0; j
< ptdata
.tzi
[i
].nr_trip_pts
; j
++) {
195 fprintf(tmon_log
, "#\tTP%02d type:%s, temp:%lu\n", j
,
196 trip_type_name
[ptdata
.tzi
[i
].tp
[j
].type
],
197 ptdata
.tzi
[i
].tp
[j
].temp
);
202 for (i
= 0; i
< ptdata
.nr_cooling_dev
; i
++)
203 fprintf(tmon_log
, "#cooling devices%02d: %s\n",
204 i
, ptdata
.cdi
[i
].type
);
206 fprintf(tmon_log
, "#---------- THERMAL DATA LOG STARTED -----------\n");
207 fprintf(tmon_log
, "Samples TargetTemp ");
208 for (i
= 0; i
< ptdata
.nr_tz_sensor
; i
++) {
209 fprintf(tmon_log
, "%s%d ", ptdata
.tzi
[i
].type
,
210 ptdata
.tzi
[i
].instance
);
212 for (i
= 0; i
< ptdata
.nr_cooling_dev
; i
++)
213 fprintf(tmon_log
, "%s%d ", ptdata
.cdi
[i
].type
,
214 ptdata
.cdi
[i
].instance
);
216 fprintf(tmon_log
, "\n");
219 static struct option opts
[] = {
220 { "control", 1, NULL
, 'c' },
221 { "daemon", 0, NULL
, 'd' },
222 { "time-interval", 1, NULL
, 't' },
223 { "target-temp", 1, NULL
, 'T' },
224 { "log", 0, NULL
, 'l' },
225 { "help", 0, NULL
, 'h' },
226 { "version", 0, NULL
, 'v' },
227 { "debug", 0, NULL
, 'g' },
232 int main(int argc
, char **argv
)
236 double yk
= 0.0, temp
; /* controller output */
239 if (geteuid() != 0) {
240 printf("TMON needs to be run as root\n");
244 while ((c
= getopt_long(argc
, argv
, "c:dlht:T:vgz:", opts
, &id2
)) != -1) {
248 strncpy(ctrl_cdev
, optarg
, CDEV_NAME_SIZE
);
252 printf("Run TMON in daemon mode\n");
255 ticktime
= strtod(optarg
, NULL
);
260 temp
= strtod(optarg
, NULL
);
262 fprintf(stderr
, "error: temperature must be positive\n");
265 target_temp_user
= temp
;
268 printf("Logging data to /var/tmp/tmon.log\n");
281 target_thermal_zone
= strtod(optarg
, NULL
);
287 if (pthread_mutex_init(&input_lock
, NULL
) != 0) {
288 fprintf(stderr
, "\n mutex init failed, exit\n");
292 if (signal(SIGINT
, tmon_sig_handler
) == SIG_ERR
)
293 syslog(LOG_DEBUG
, "Cannot handle SIGINT\n");
294 if (signal(SIGTERM
, tmon_sig_handler
) == SIG_ERR
)
295 syslog(LOG_DEBUG
, "Cannot handle SIGINT\n");
297 if (probe_thermal_sysfs()) {
298 pthread_mutex_destroy(&input_lock
);
304 signal(SIGWINCH
, resize_handler
);
307 show_cooling_device();
308 update_thermal_data();
311 init_thermal_controller();
313 nodelay(stdscr
, TRUE
);
314 err
= pthread_create(&event_tid
, NULL
, &handle_tui_events
, NULL
);
316 printf("\ncan't create thread :[%s]", strerror(err
));
321 /* validate range of user selected target zone, default to the first
322 * instance if out of range
324 target_tz_index
= zone_instance_to_index(target_thermal_zone
);
325 if (target_tz_index
< 0) {
326 target_thermal_zone
= ptdata
.tzi
[0].instance
;
327 syslog(LOG_ERR
, "target zone is not found, default to %d\n",
328 target_thermal_zone
);
334 update_thermal_data();
337 show_cooling_device();
339 cur_thermal_record
++;
340 time_elapsed
+= ticktime
;
341 controller_handler(trec
[0].temp
[target_tz_index
] / 1000,
343 trec
[0].pid_out_pct
= yk
;
353 static void start_daemon_mode()
357 pid_t sid
, pid
= fork();
364 /* disable TUI, it may not be necessary, but saves some resource */
367 /* change the file mode mask */
368 umask(S_IWGRP
| S_IWOTH
);
370 /* new SID for the daemon process */
375 /* change working directory */
376 if ((chdir("/")) < 0)
383 close(STDOUT_FILENO
);
384 close(STDERR_FILENO
);