1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * tmon.c Thermal Monitor (TMON) main function and entry point
5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
7 * Author: Jacob Pan <jacob.jun.pan@linux.intel.com>
15 #include <sys/types.h>
30 unsigned long ticktime
= 1; /* seconds */
31 unsigned long no_control
= 1; /* monitoring only or use cooling device for
32 * temperature control.
34 double time_elapsed
= 0.0;
35 unsigned long target_temp_user
= 65; /* can be select by tui later */
38 static short daemon_mode
;
39 static int logging
; /* for recording thermal data to a file */
42 /*cooling device used for the PID controller */
43 char ctrl_cdev
[CDEV_NAME_SIZE
] = "None";
44 int target_thermal_zone
; /* user selected target zone instance */
45 static void start_daemon_mode(void);
48 pthread_mutex_t input_lock
;
51 printf("Usage: tmon [OPTION...]\n");
52 printf(" -c, --control cooling device in control\n");
53 printf(" -d, --daemon run as daemon, no TUI\n");
54 printf(" -g, --debug debug message in syslog\n");
55 printf(" -h, --help show this help message\n");
56 printf(" -l, --log log data to /var/tmp/tmon.log\n");
57 printf(" -t, --time-interval sampling time interval, > 1 sec.\n");
58 printf(" -T, --target-temp initial target temperature\n");
59 printf(" -v, --version show version\n");
60 printf(" -z, --zone target thermal zone id\n");
67 printf("TMON version %s\n", VERSION
);
71 static void tmon_cleanup(void)
74 syslog(LOG_INFO
, "TMON exit cleanup\n");
80 pthread_mutex_lock(&input_lock
);
81 pthread_cancel(event_tid
);
82 pthread_mutex_unlock(&input_lock
);
83 pthread_mutex_destroy(&input_lock
);
86 /* relax control knobs, undo throttling */
89 keypad(stdscr
, FALSE
);
100 static void tmon_sig_handler(int sig
)
102 syslog(LOG_INFO
, "TMON caught signal %d\n", sig
);
106 printf("sigterm, exit and clean up\n");
110 printf("sigkill, exit and clean up\n");
114 printf("ctrl-c, exit and clean up\n");
124 static void start_syslog(void)
127 setlogmask(LOG_UPTO(LOG_DEBUG
));
129 setlogmask(LOG_UPTO(LOG_ERR
));
130 openlog("tmon.log", LOG_CONS
| LOG_PID
| LOG_NDELAY
, LOG_LOCAL0
);
131 syslog(LOG_NOTICE
, "TMON started by User %d", getuid());
134 static void prepare_logging(void)
141 /* open local data log file */
142 tmon_log
= fopen(TMON_LOG_FILE
, "w+");
144 syslog(LOG_ERR
, "failed to open log file %s\n", TMON_LOG_FILE
);
148 if (lstat(TMON_LOG_FILE
, &logstat
) < 0) {
149 syslog(LOG_ERR
, "Unable to stat log file %s\n", TMON_LOG_FILE
);
155 /* The log file must be a regular file owned by us */
156 if (S_ISLNK(logstat
.st_mode
)) {
157 syslog(LOG_ERR
, "Log file is a symlink. Will not log\n");
163 if (logstat
.st_uid
!= getuid()) {
164 syslog(LOG_ERR
, "We don't own the log file. Not logging\n");
171 fprintf(tmon_log
, "#----------- THERMAL SYSTEM CONFIG -------------\n");
172 for (i
= 0; i
< ptdata
.nr_tz_sensor
; i
++) {
173 char binding_str
[33]; /* size of long + 1 */
176 memset(binding_str
, 0, sizeof(binding_str
));
177 for (j
= 0; j
< 32; j
++)
178 binding_str
[j
] = (ptdata
.tzi
[i
].cdev_binding
& 1<<j
) ?
181 fprintf(tmon_log
, "#thermal zone %s%02d cdevs binding: %32s\n",
183 ptdata
.tzi
[i
].instance
,
185 for (j
= 0; j
< ptdata
.tzi
[i
].nr_trip_pts
; j
++) {
186 fprintf(tmon_log
, "#\tTP%02d type:%s, temp:%lu\n", j
,
187 trip_type_name
[ptdata
.tzi
[i
].tp
[j
].type
],
188 ptdata
.tzi
[i
].tp
[j
].temp
);
193 for (i
= 0; i
< ptdata
.nr_cooling_dev
; i
++)
194 fprintf(tmon_log
, "#cooling devices%02d: %s\n",
195 i
, ptdata
.cdi
[i
].type
);
197 fprintf(tmon_log
, "#---------- THERMAL DATA LOG STARTED -----------\n");
198 fprintf(tmon_log
, "Samples TargetTemp ");
199 for (i
= 0; i
< ptdata
.nr_tz_sensor
; i
++) {
200 fprintf(tmon_log
, "%s%d ", ptdata
.tzi
[i
].type
,
201 ptdata
.tzi
[i
].instance
);
203 for (i
= 0; i
< ptdata
.nr_cooling_dev
; i
++)
204 fprintf(tmon_log
, "%s%d ", ptdata
.cdi
[i
].type
,
205 ptdata
.cdi
[i
].instance
);
207 fprintf(tmon_log
, "\n");
210 static struct option opts
[] = {
211 { "control", 1, NULL
, 'c' },
212 { "daemon", 0, NULL
, 'd' },
213 { "time-interval", 1, NULL
, 't' },
214 { "target-temp", 1, NULL
, 'T' },
215 { "log", 0, NULL
, 'l' },
216 { "help", 0, NULL
, 'h' },
217 { "version", 0, NULL
, 'v' },
218 { "debug", 0, NULL
, 'g' },
223 int main(int argc
, char **argv
)
227 double yk
= 0.0, temp
; /* controller output */
230 if (geteuid() != 0) {
231 printf("TMON needs to be run as root\n");
235 while ((c
= getopt_long(argc
, argv
, "c:dlht:T:vgz:", opts
, &id2
)) != -1) {
239 strncpy(ctrl_cdev
, optarg
, CDEV_NAME_SIZE
);
243 printf("Run TMON in daemon mode\n");
246 ticktime
= strtod(optarg
, NULL
);
251 temp
= strtod(optarg
, NULL
);
253 fprintf(stderr
, "error: temperature must be positive\n");
256 target_temp_user
= temp
;
259 printf("Logging data to /var/tmp/tmon.log\n");
272 target_thermal_zone
= strtod(optarg
, NULL
);
278 if (pthread_mutex_init(&input_lock
, NULL
) != 0) {
279 fprintf(stderr
, "\n mutex init failed, exit\n");
283 if (signal(SIGINT
, tmon_sig_handler
) == SIG_ERR
)
284 syslog(LOG_DEBUG
, "Cannot handle SIGINT\n");
285 if (signal(SIGTERM
, tmon_sig_handler
) == SIG_ERR
)
286 syslog(LOG_DEBUG
, "Cannot handle SIGINT\n");
288 if (probe_thermal_sysfs()) {
289 pthread_mutex_destroy(&input_lock
);
295 signal(SIGWINCH
, resize_handler
);
298 show_cooling_device();
299 update_thermal_data();
302 init_thermal_controller();
304 nodelay(stdscr
, TRUE
);
305 err
= pthread_create(&event_tid
, NULL
, &handle_tui_events
, NULL
);
307 printf("\ncan't create thread :[%s]", strerror(err
));
312 /* validate range of user selected target zone, default to the first
313 * instance if out of range
315 target_tz_index
= zone_instance_to_index(target_thermal_zone
);
316 if (target_tz_index
< 0) {
317 target_thermal_zone
= ptdata
.tzi
[0].instance
;
318 syslog(LOG_ERR
, "target zone is not found, default to %d\n",
319 target_thermal_zone
);
325 update_thermal_data();
328 show_cooling_device();
330 time_elapsed
+= ticktime
;
331 controller_handler(trec
[0].temp
[target_tz_index
] / 1000,
333 trec
[0].pid_out_pct
= yk
;
343 static void start_daemon_mode()
347 pid_t sid
, pid
= fork();
354 /* disable TUI, it may not be necessary, but saves some resource */
357 /* change the file mode mask */
358 umask(S_IWGRP
| S_IWOTH
);
360 /* new SID for the daemon process */
365 /* change working directory */
366 if ((chdir("/")) < 0)
373 close(STDOUT_FILENO
);
374 close(STDERR_FILENO
);