Commit 549608ea authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux

Pull thermal management updates from Zhang Rui:
 "This time we only have a few changes as there are no soc thermal
  changes from Eduardo.  The only big change is the introduction of
  TMON, a tool to help visualize, tune, and test the thermal subsystem.
  The rest is mostly cleanups and fixes all over.

  Specifics:

   - introduce TMON, a tool base on thermal sysfs I/F.  It can be used
     to visualize, tune and test the thermal subsystem.

   - fix a zone/cooling device binding problem, when both thermal zone
     bind parameters and .bind() callback are available"

* 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux:
  tools/thermal: Introduce tmon, a tool for thermal subsystem
  thermal: Fix binding problem when there is thermal zone params
  thermal: cpu_cooling: fix return value check in cpufreq_cooling_register()
  Thermal: Check for validity before doing kfree
  thermal/intel_powerclamp: Add newer CPU models
  Thermal: Tidy up error handling in powerclamp_init
  thermal: Kconfig: cosmetic fixes
  ACPI/thermal : Remove zone disabled warning
  typo in drivers/thermal/Kconfig: lpatform instead of platform
parents 2f466d33 86e0a0bd
......@@ -514,10 +514,9 @@ static void acpi_thermal_check(void *data)
{
struct acpi_thermal *tz = data;
if (!tz->tz_enabled) {
pr_warn("thermal zone is disabled \n");
if (!tz->tz_enabled)
return;
}
thermal_zone_device_update(tz->thermal_zone);
}
......@@ -569,9 +568,10 @@ static int thermal_set_mode(struct thermal_zone_device *thermal,
*/
if (mode == THERMAL_DEVICE_ENABLED)
enable = 1;
else if (mode == THERMAL_DEVICE_DISABLED)
else if (mode == THERMAL_DEVICE_DISABLED) {
enable = 0;
else
pr_warn("thermal zone will be disabled\n");
} else
return -EINVAL;
if (enable != tz->tz_enabled) {
......
......@@ -56,7 +56,7 @@ config THERMAL_DEFAULT_GOV_USER_SPACE
select THERMAL_GOV_USER_SPACE
help
Select this if you want to let the user space manage the
lpatform thermals.
platform thermals.
endchoice
......@@ -69,6 +69,7 @@ config THERMAL_GOV_STEP_WISE
bool "Step_wise thermal governor"
help
Enable this to manage platform thermals using a simple linear
governor.
config THERMAL_GOV_USER_SPACE
bool "User_space thermal governor"
......@@ -116,14 +117,14 @@ config SPEAR_THERMAL
depends on OF
help
Enable this to plug the SPEAr thermal sensor driver into the Linux
thermal framework
thermal framework.
config RCAR_THERMAL
tristate "Renesas R-Car thermal driver"
depends on ARCH_SHMOBILE
help
Enable this to plug the R-Car thermal sensor driver into the Linux
thermal framework
thermal framework.
config KIRKWOOD_THERMAL
tristate "Temperature sensor on Marvell Kirkwood SoCs"
......
......@@ -469,10 +469,10 @@ cpufreq_cooling_register(const struct cpumask *clip_cpus)
cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
&cpufreq_cooling_ops);
if (!cool_dev) {
if (IS_ERR(cool_dev)) {
release_idr(&cpufreq_idr, cpufreq_dev->id);
kfree(cpufreq_dev);
return ERR_PTR(-EINVAL);
return cool_dev;
}
cpufreq_dev->cool_dev = cool_dev;
cpufreq_dev->cpufreq_state = 0;
......
......@@ -675,6 +675,11 @@ static const struct x86_cpu_id intel_powerclamp_ids[] = {
{ X86_VENDOR_INTEL, 6, 0x2e},
{ X86_VENDOR_INTEL, 6, 0x2f},
{ X86_VENDOR_INTEL, 6, 0x3a},
{ X86_VENDOR_INTEL, 6, 0x3c},
{ X86_VENDOR_INTEL, 6, 0x3e},
{ X86_VENDOR_INTEL, 6, 0x3f},
{ X86_VENDOR_INTEL, 6, 0x45},
{ X86_VENDOR_INTEL, 6, 0x46},
{}
};
MODULE_DEVICE_TABLE(x86cpu, intel_powerclamp_ids);
......@@ -758,21 +763,39 @@ static int powerclamp_init(void)
/* probe cpu features and ids here */
retval = powerclamp_probe();
if (retval)
return retval;
goto exit_free;
/* set default limit, maybe adjusted during runtime based on feedback */
window_size = 2;
register_hotcpu_notifier(&powerclamp_cpu_notifier);
powerclamp_thread = alloc_percpu(struct task_struct *);
if (!powerclamp_thread) {
retval = -ENOMEM;
goto exit_unregister;
}
cooling_dev = thermal_cooling_device_register("intel_powerclamp", NULL,
&powerclamp_cooling_ops);
if (IS_ERR(cooling_dev))
return -ENODEV;
if (IS_ERR(cooling_dev)) {
retval = -ENODEV;
goto exit_free_thread;
}
if (!duration)
duration = jiffies_to_msecs(DEFAULT_DURATION_JIFFIES);
powerclamp_create_debug_files();
return 0;
exit_free_thread:
free_percpu(powerclamp_thread);
exit_unregister:
unregister_hotcpu_notifier(&powerclamp_cpu_notifier);
exit_free:
kfree(cpu_clamping_mask);
return retval;
}
module_init(powerclamp_init);
......
......@@ -247,10 +247,11 @@ static void bind_cdev(struct thermal_cooling_device *cdev)
if (!pos->tzp && !pos->ops->bind)
continue;
if (!pos->tzp && pos->ops->bind) {
if (pos->ops->bind) {
ret = pos->ops->bind(pos, cdev);
if (ret)
print_bind_err_msg(pos, cdev, ret);
continue;
}
tzp = pos->tzp;
......@@ -282,8 +283,8 @@ static void bind_tz(struct thermal_zone_device *tz)
mutex_lock(&thermal_list_lock);
/* If there is no platform data, try to use ops->bind */
if (!tzp && tz->ops->bind) {
/* If there is ops->bind, try to use ops->bind */
if (tz->ops->bind) {
list_for_each_entry(pos, &thermal_cdev_list, node) {
ret = tz->ops->bind(tz, pos);
if (ret)
......@@ -1038,7 +1039,8 @@ static void thermal_release(struct device *dev)
sizeof("thermal_zone") - 1)) {
tz = to_thermal_zone(dev);
kfree(tz);
} else {
} else if(!strncmp(dev_name(dev), "cooling_device",
sizeof("cooling_device") - 1)){
cdev = to_cooling_device(dev);
kfree(cdev);
}
......
......@@ -15,6 +15,7 @@ help:
@echo ' net - misc networking tools'
@echo ' vm - misc vm tools'
@echo ' x86_energy_perf_policy - Intel energy policy tool'
@echo ' tmon - thermal monitoring and tuning tool'
@echo ''
@echo 'You can do:'
@echo ' $$ make -C tools/ <tool>_install'
......@@ -50,6 +51,9 @@ selftests: FORCE
turbostat x86_energy_perf_policy: FORCE
$(call descend,power/x86/$@)
tmon: FORCE
$(call descend,thermal/$@)
cpupower_install:
$(call descend,power/$(@:_install=),install)
......@@ -62,9 +66,13 @@ selftests_install:
turbostat_install x86_energy_perf_policy_install:
$(call descend,power/x86/$(@:_install=),install)
tmon_install:
$(call descend,thermal/$(@:_install=),install)
install: cgroup_install cpupower_install firewire_install lguest_install \
perf_install selftests_install turbostat_install usb_install \
virtio_install vm_install net_install x86_energy_perf_policy_install
virtio_install vm_install net_install x86_energy_perf_policy_install \
tmon
cpupower_clean:
$(call descend,power/cpupower,clean)
......@@ -84,8 +92,11 @@ selftests_clean:
turbostat_clean x86_energy_perf_policy_clean:
$(call descend,power/x86/$(@:_clean=),clean)
tmon_clean:
$(call descend,thermal/tmon,clean)
clean: cgroup_clean cpupower_clean firewire_clean lguest_clean perf_clean \
selftests_clean turbostat_clean usb_clean virtio_clean \
vm_clean net_clean x86_energy_perf_policy_clean
vm_clean net_clean x86_energy_perf_policy_clean tmon_clean
.PHONY: FORCE
VERSION = 1.0
BINDIR=usr/bin
WARNFLAGS=-Wall -Wshadow -W -Wformat -Wimplicit-function-declaration -Wimplicit-int
CFLAGS= -O1 ${WARNFLAGS} -fstack-protector
CC=gcc
CFLAGS+=-D VERSION=\"$(VERSION)\"
LDFLAGS+=
TARGET=tmon
INSTALL_PROGRAM=install -m 755 -p
DEL_FILE=rm -f
INSTALL_CONFIGFILE=install -m 644 -p
CONFIG_FILE=
CONFIG_PATH=
OBJS = tmon.o tui.o sysfs.o pid.o
OBJS +=
tmon: $(OBJS) Makefile tmon.h
$(CC) ${CFLAGS} $(LDFLAGS) $(OBJS) -o $(TARGET) -lm -lpanel -lncursesw -lpthread
valgrind: tmon
sudo valgrind -v --track-origins=yes --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./$(TARGET) 1> /dev/null
install:
- mkdir -p $(INSTALL_ROOT)/$(BINDIR)
- $(INSTALL_PROGRAM) "$(TARGET)" "$(INSTALL_ROOT)/$(BINDIR)/$(TARGET)"
- mkdir -p $(INSTALL_ROOT)/$(CONFIG_PATH)
- $(INSTALL_CONFIGFILE) "$(CONFIG_FILE)" "$(INSTALL_ROOT)/$(CONFIG_PATH)"
uninstall:
$(DEL_FILE) "$(INSTALL_ROOT)/$(BINDIR)/$(TARGET)"
$(CONFIG_FILE) "$(CONFIG_PATH)"
clean:
find . -name "*.o" | xargs $(DEL_FILE)
rm -f $(TARGET)
dist:
git tag v$(VERSION)
git archive --format=tar --prefix="$(TARGET)-$(VERSION)/" v$(VERSION) | \
gzip > $(TARGET)-$(VERSION).tar.gz
TMON - A Monitoring and Testing Tool for Linux kernel thermal subsystem
Why TMON?
==========
Increasingly, Linux is running on thermally constrained devices. The simple
thermal relationship between processor and fan has become past for modern
computers.
As hardware vendors cope with the thermal constraints on their products, more
and more sensors are added, new cooling capabilities are introduced. The
complexity of the thermal relationship can grow exponentially among cooling
devices, zones, sensors, and trip points. They can also change dynamically.
To expose such relationship to the userspace, Linux generic thermal layer
introduced sysfs entry at /sys/class/thermal with a matrix of symbolic
links, trip point bindings, and device instances. To traverse such
matrix by hand is not a trivial task. Testing is also difficult in that
thermal conditions are often exception cases that hard to reach in
normal operations.
TMON is conceived as a tool to help visualize, tune, and test the
complex thermal subsystem.
Files
=====
tmon.c : main function for set up and configurations.
tui.c : handles ncurses based user interface
sysfs.c : access to the generic thermal sysfs
pid.c : a proportional-integral-derivative (PID) controller
that can be used for thermal relationship training.
Requirements
============
Depends on ncurses
Build
=========
$ make
$ sudo ./tmon -h
Usage: tmon [OPTION...]
-c, --control cooling device in control
-d, --daemon run as daemon, no TUI
-l, --log log data to /var/tmp/tmon.log
-h, --help show this help message
-t, --time-interval set time interval for sampling
-v, --version show version
-g, --debug debug message in syslog
1. For monitoring only:
$ sudo ./tmon
/*
* pid.c PID controller for testing cooling devices
*
*
*
* Copyright (C) 2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version
* 2 or later as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Author Name Jacob Pan <jacob.jun.pan@linux.intel.com>
*
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <dirent.h>
#include <libintl.h>
#include <ctype.h>
#include <assert.h>
#include <time.h>
#include <limits.h>
#include <math.h>
#include <sys/stat.h>
#include <syslog.h>
#include "tmon.h"
/**************************************************************************
* PID (Proportional-Integral-Derivative) controller is commonly used in
* linear control system, consider the the process.
* G(s) = U(s)/E(s)
* kp = proportional gain
* ki = integral gain
* kd = derivative gain
* Ts
* We use type C Alan Bradley equation which takes set point off the
* output dependency in P and D term.
*
* y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*Ts*e[k] - Kd*(x[k]
* - 2*x[k-1]+x[k-2])/Ts
*
*
***********************************************************************/
struct pid_params p_param;
/* cached data from previous loop */
static double xk_1, xk_2; /* input temperature x[k-#] */
/*
* TODO: make PID parameters tuned automatically,
* 1. use CPU burn to produce open loop unit step response
* 2. calculate PID based on Ziegler-Nichols rule
*
* add a flag for tuning PID
*/
int init_thermal_controller(void)
{
int ret = 0;
/* init pid params */
p_param.ts = ticktime;
/* TODO: get it from TUI tuning tab */
p_param.kp = .36;
p_param.ki = 5.0;
p_param.kd = 0.19;
p_param.t_target = target_temp_user;
return ret;
}
void controller_reset(void)
{
/* TODO: relax control data when not over thermal limit */
syslog(LOG_DEBUG, "TC inactive, relax p-state\n");
p_param.y_k = 0.0;
xk_1 = 0.0;
xk_2 = 0.0;
set_ctrl_state(0);
}
/* To be called at time interval Ts. Type C PID controller.
* y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*Ts*e[k] - Kd*(x[k]
* - 2*x[k-1]+x[k-2])/Ts
* TODO: add low pass filter for D term
*/
#define GUARD_BAND (2)
void controller_handler(const double xk, double *yk)
{
double ek;
double p_term, i_term, d_term;
ek = p_param.t_target - xk; /* error */
if (ek >= 3.0) {
syslog(LOG_DEBUG, "PID: %3.1f Below set point %3.1f, stop\n",
xk, p_param.t_target);
controller_reset();
*yk = 0.0;
return;
}
/* compute intermediate PID terms */
p_term = -p_param.kp * (xk - xk_1);
i_term = p_param.kp * p_param.ki * p_param.ts * ek;
d_term = -p_param.kp * p_param.kd * (xk - 2 * xk_1 + xk_2) / p_param.ts;
/* compute output */
*yk += p_term + i_term + d_term;
/* update sample data */
xk_1 = xk;
xk_2 = xk_1;
/* clamp output adjustment range */
if (*yk < -LIMIT_HIGH)
*yk = -LIMIT_HIGH;
else if (*yk > -LIMIT_LOW)
*yk = -LIMIT_LOW;
p_param.y_k = *yk;
set_ctrl_state(lround(fabs(p_param.y_k)));
}
This diff is collapsed.
.TH TMON 8
.SH NAME
\fBtmon\fP - A monitoring and testing tool for Linux kernel thermal subsystem
.SH SYNOPSIS
.ft B
.B tmon
.RB [ Options ]
.br
.SH DESCRIPTION
\fBtmon \fP can be used to visualize thermal relationship and
real-time thermal data; tune
and test cooling devices and sensors; collect thermal data for offline
analysis and plot. \fBtmon\fP must be run as root in order to control device
states via sysfs.
.PP
\fBFunctions\fP
.PP
.nf
1. Thermal relationships:
- show thermal zone information
- show cooling device information
- show trip point binding within each thermal zone
- show trip point and cooling device instance bindings
.PP
2. Real time data display
- show temperature of all thermal zones w.r.t. its trip points and types
- show states of all cooling devices
.PP
3. Thermal relationship learning and device tuning
- with a built-in Proportional Integral Derivative (\fBPID\fP)
controller, user can pair a cooling device to a thermal sensor for
testing the effectiveness and learn about the thermal distance between the two
- allow manual control of cooling device states and target temperature
.PP
4. Data logging in /var/tmp/tmon.log
- contains thermal configuration data, i.e. cooling device, thermal
zones, and trip points. Can be used for data collection in remote
debugging.
- log real-time thermal data into space separated format that can be
directly consumed by plotting tools such as Rscript.
.SS Options
.PP
The \fB-c --control\fP option sets a cooling device type to control temperature
of a thermal zone
.PP
The \fB-d --daemon\fP option runs \fBtmon \fP as daemon without user interface
.PP
The \fB-g --debug\fP option allow debug messages to be stored in syslog
.PP
The \fB-h --help\fP option shows help message
.PP
The \fB-l --log\fP option write data to /var/tmp/tmon.log
.PP
The \fB-t --time-interval\fP option sets the polling interval in seconds
.PP
The \fB-v --version\fP option shows the version of \fBtmon \fP
.PP
The \fB-z --zone\fP option sets the target therma zone instance to be controlled
.PP
.SH FIELD DESCRIPTIONS
.nf
.PP
\fBP \fP passive cooling trip point type
\fBA \fP active cooling trip point type (fan)
\fBC \fP critical trip point type
\fBA \fP hot trip point type
\fBkp \fP proportional gain of \fBPID\fP controller
\fBki \fP integral gain of \fBPID\fP controller
\fBkd \fP derivative gain of \fBPID\fP controller
.SH REQUIREMENT
Build depends on ncurses
.PP
Runtime depends on window size large enough to show the number of
devices found on the system.
.PP
.SH INTERACTIVE COMMANDS
.pp
.nf
\fBCtrl-C, q/Q\fP stops \fBtmon\fP
\fBTAB\fP shows tuning pop up panel, choose a letter to modify
.SH EXAMPLES
Without any parameters, tmon is in monitoring only mode and refresh
screen every 1 second.
.PP
1. For monitoring only:
.nf
$ sudo ./tmon
2. Use Processor cooling device to control thermal zone 0 at default 65C.
$ sudo ./tmon -c Processor -z 0
3. Use intel_powerclamp(idle injection) cooling device to control thermal zone 1
$ sudo ./tmon -c intel_powerclamp -z 1
4. Turn on debug and collect data log at /var/tmp/tmon.log
$ sudo ./tmon -g -l
For example, the log below shows PID controller was adjusting current states
for all cooling devices with "Processor" type such that thermal zone 0
can stay below 65 dC.
#---------- THERMAL DATA LOG STARTED -----------
Samples TargetTemp acpitz0 acpitz1 Fan0 Fan1 Fan2 Fan3 Fan4 Fan5
Fan6 Fan7 Fan8 Fan9 Processor10 Processor11 Processor12 Processor13
LCD14 intel_powerclamp15 1 65.0 65 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 2
65.0 66 65 0 0 0 0 0 0 0 0 0 0 4 4 4 4 6 0 3 65.0 60 54 0 0 0 0 0 0 0 0
0 0 4 4 4 4 6 0 4 65.0 53 53 0 0 0 0 0 0 0 0 0 0 4 4 4 4 6 0
5 65.0 52 52 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0
6 65.0 53 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0
7 65.0 68 70 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0
8 65.0 68 68 0 0 0 0 0 0 0 0 0 0 5 5 5 5 6 0
9 65.0 68 68 0 0 0 0 0 0 0 0 0 0 6 6 6 6 6 0
10 65.0 67 67 0 0 0 0 0 0 0 0 0 0 7 7 7 7 6 0
11 65.0 67 67 0 0 0 0 0 0 0 0 0 0 8 8 8 8 6 0
12 65.0 67 67 0 0 0 0 0 0 0 0 0 0 8 8 8 8 6 0
13 65.0 67 67 0 0 0 0 0 0 0 0 0 0 9 9 9 9 6 0
14 65.0 66 66 0 0 0 0 0 0 0 0 0 0 10 10 10 10 6 0
15 65.0 66 67 0 0 0 0 0 0 0 0 0 0 10 10 10 10 6 0
16 65.0 66 66 0 0 0 0 0 0 0 0 0 0 11 11 11 11 6 0
17 65.0 66 66 0 0 0 0 0 0 0 0 0 0 11 11 11 11 6 0
18 65.0 64 61 0 0 0 0 0 0 0 0 0 0 11 11 11 11 6 0
19 65.0 60 59 0 0 0 0 0 0 0 0 0 0 12 12 12 12 6 0
Data can be read directly into an array by an example R-script below:
#!/usr/bin/Rscript
tdata <- read.table("/var/tmp/tmon.log", header=T, comment.char="#")
attach(tdata)
jpeg("tmon.jpg")
X11()
g_range <- range(0, intel_powerclamp15, TargetTemp, acpitz0)
plot( Samples, intel_powerclamp15, col="blue", ylim=g_range, axes=FALSE, ann=FALSE)
par(new=TRUE)
lines(TargetTemp, type="o", pch=22, lty=2, col="red")
dev.off()
/*
* tmon.c Thermal Monitor (TMON) main function and entry point
*
* Copyright (C) 2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version
* 2 or later as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Author: Jacob Pan <jacob.jun.pan@linux.intel.com>
*
*/
#include <getopt.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ncurses.h>
#include <ctype.h>
#include <time.h>
#include <signal.h>
#include <limits.h>
#include <sys/time.h>
#include <pthread.h>
#include <math.h>
#include <stdarg.h>
#include <syslog.h>
#include "tmon.h"
unsigned long ticktime = 1; /* seconds */
unsigned long no_control = 1; /* monitoring only or use cooling device for
* temperature control.
*/
double time_elapsed = 0.0;
unsigned long target_temp_user = 65; /* can be select by tui later */
int dialogue_on;
int tmon_exit;
static short daemon_mode;
static int logging; /* for recording thermal data to a file */
static int debug_on;
FILE *tmon_log;
/*cooling device used for the PID controller */
char ctrl_cdev[CDEV_NAME_SIZE] = "None";
int target_thermal_zone; /* user selected target zone instance */
static void start_daemon_mode(void);
pthread_t event_tid;
pthread_mutex_t input_lock;
void usage()
{
printf("Usage: tmon [OPTION...]\n");
printf(" -c, --control cooling device in control\n");
printf(" -d, --daemon run as daemon, no TUI\n");
printf(" -g, --debug debug message in syslog\n");
printf(" -h, --help show this help message\n");
printf(" -l, --log log data to /var/tmp/tmon.log\n");
printf(" -t, --time-interval sampling time interval, > 1 sec.\n");
printf(" -v, --version show version\n");
printf(" -z, --zone target thermal zone id\n");
exit(0);
}
void version()
{
printf("TMON version %s\n", VERSION);
exit(EXIT_SUCCESS);
}
static void tmon_cleanup(void)
{
syslog(LOG_INFO, "TMON exit cleanup\n");
fflush(stdout);
refresh();
if (tmon_log)
fclose(tmon_log);
if (event_tid) {
pthread_mutex_lock(&input_lock);
pthread_cancel(event_tid);
pthread_mutex_unlock(&input_lock);
pthread_mutex_destroy(&input_lock);
}
closelog();
/* relax control knobs, undo throttling */
set_ctrl_state(0);
keypad(stdscr, FALSE);
echo();
nocbreak();
close_windows();
endwin();
free_thermal_data();
exit(1);
}
static void tmon_sig_handler(int sig)
{
syslog(LOG_INFO, "TMON caught signal %d\n", sig);
refresh();
switch (sig) {
case SIGTERM:
printf("sigterm, exit and clean up\n");
fflush(stdout);
break;
case SIGKILL:
printf("sigkill, exit and clean up\n");
fflush(stdout);
break;
case SIGINT:
printf("ctrl-c, exit and clean up\n");
fflush(stdout);
break;
default:
break;
}
tmon_exit = true;
}
static void start_syslog(void)
{
if (debug_on)
setlogmask(LOG_UPTO(LOG_DEBUG));
else
setlogmask(LOG_UPTO(LOG_ERR));
openlog("tmon.log", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);
syslog(LOG_NOTICE, "TMON started by User %d", getuid());
}
static void prepare_logging(void)
{
int i;
if (!logging)
return;
/* open local data log file */
tmon_log = fopen(TMON_LOG_FILE, "w+");
if (!tmon_log) {
syslog(LOG_ERR, "failed to open log file %s\n", TMON_LOG_FILE);
return;
}
fprintf(tmon_log, "#----------- THERMAL SYSTEM CONFIG -------------\n");
for (i = 0; i < ptdata.nr_tz_sensor; i++) {
char binding_str[33]; /* size of long + 1 */
int j;
memset(binding_str, 0, sizeof(binding_str));
for (j = 0; j < 32; j++)
binding_str[j] = (ptdata.tzi[i].cdev_binding & 1<<j) ?
'1' : '0';
fprintf(tmon_log, "#thermal zone %s%02d cdevs binding: %32s\n",
ptdata.tzi[i].type,
ptdata.tzi[i].instance,
binding_str);
for (j = 0; j < ptdata.tzi[i].nr_trip_pts; j++) {
fprintf(tmon_log, "#\tTP%02d type:%s, temp:%lu\n", j,
trip_type_name[ptdata.tzi[i].tp[j].type],
ptdata.tzi[i].tp[j].temp);
}
}
for (i = 0; i < ptdata.nr_cooling_dev; i++)
fprintf(tmon_log, "#cooling devices%02d: %s\n",
i, ptdata.cdi[i].type);
fprintf(tmon_log, "#---------- THERMAL DATA LOG STARTED -----------\n");
fprintf(tmon_log, "Samples TargetTemp ");
for (i = 0; i < ptdata.nr_tz_sensor; i++) {
fprintf(tmon_log, "%s%d ", ptdata.tzi[i].type,
ptdata.tzi[i].instance);
}
for (i = 0; i < ptdata.nr_cooling_dev; i++)
fprintf(tmon_log, "%s%d ", ptdata.cdi[i].type,
ptdata.cdi[i].instance);
fprintf(tmon_log, "\n");
}
static struct option opts[] = {
{ "control", 1, NULL, 'c' },
{ "daemon", 0, NULL, 'd' },
{ "time-interval", 1, NULL, 't' },
{ "log", 0, NULL, 'l' },
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ "debug", 0, NULL, 'g' },
{ 0, 0, NULL, 0 }
};
int main(int argc, char **argv)
{
int err = 0;
int id2 = 0, c;
double yk = 0.0; /* controller output */
int target_tz_index;
if (geteuid() != 0) {
printf("TMON needs to be run as root\n");
exit(EXIT_FAILURE);
}
while ((c = getopt_long(argc, argv, "c:dlht:vgz:", opts, &id2)) != -1) {
switch (c) {
case 'c':
no_control = 0;
strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE);
break;
case 'd':
start_daemon_mode();
printf("Run TMON in daemon mode\n");
break;
case 't':
ticktime = strtod(optarg, NULL);
if (ticktime < 1)
ticktime = 1;
break;
case 'l':
printf("Logging data to /var/tmp/tmon.log\n");
logging = 1;
break;
case 'h':
usage();
break;
case 'v':
version();
break;
case 'g':
debug_on = 1;
break;
case 'z':
target_thermal_zone = strtod(optarg, NULL);
break;
default:
break;
}
}
if (pthread_mutex_init(&input_lock, NULL) != 0) {
fprintf(stderr, "\n mutex init failed, exit\n");
return 1;
}
start_syslog();
if (signal(SIGINT, tmon_sig_handler) == SIG_ERR)
syslog(LOG_DEBUG, "Cannot handle SIGINT\n");
if (signal(SIGTERM, tmon_sig_handler) == SIG_ERR)
syslog(LOG_DEBUG, "Cannot handle SIGINT\n");
if (probe_thermal_sysfs()) {
pthread_mutex_destroy(&input_lock);
closelog();
return -1;
}
initialize_curses();
setup_windows();
signal(SIGWINCH, resize_handler);
show_title_bar();
show_sensors_w();
show_cooling_device();
update_thermal_data();
show_data_w();
prepare_logging();
init_thermal_controller();
nodelay(stdscr, TRUE);
err = pthread_create(&event_tid, NULL, &handle_tui_events, NULL);
if (err != 0) {
printf("\ncan't create thread :[%s]", strerror(err));
tmon_cleanup();
exit(EXIT_FAILURE);
}
/* validate range of user selected target zone, default to the first
* instance if out of range
*/
target_tz_index = zone_instance_to_index(target_thermal_zone);
if (target_tz_index < 0) {
target_thermal_zone = ptdata.tzi[0].instance;
syslog(LOG_ERR, "target zone is not found, default to %d\n",
target_thermal_zone);
}
while (1) {
sleep(ticktime);
show_title_bar();
show_sensors_w();
update_thermal_data();
if (!dialogue_on) {
show_data_w();
show_cooling_device();
}
cur_thermal_record++;
time_elapsed += ticktime;
controller_handler(trec[0].temp[target_tz_index] / 1000,
&yk);
trec[0].pid_out_pct = yk;
if (!dialogue_on)
show_control_w();
if (tmon_exit)
break;
}
tmon_cleanup();
return 0;
}
static void start_daemon_mode()
{
daemon_mode = 1;
/* fork */
pid_t sid, pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
} else if (pid > 0)
/* kill parent */
exit(EXIT_SUCCESS);
/* disable TUI, it may not be necessary, but saves some resource */
disable_tui();
/* change the file mode mask */
umask(0);
/* new SID for the daemon process */
sid = setsid();
if (sid < 0)
exit(EXIT_FAILURE);
/* change working directory */
if ((chdir("/")) < 0)
exit(EXIT_FAILURE);
sleep(10);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
/*
* tmon.h contains data structures and constants used by TMON
*
* Copyright (C) 2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version
* 2 or later as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Author Name Jacob Pan <jacob.jun.pan@linux.intel.com>
*
*/
#ifndef TMON_H
#define TMON_H
#define MAX_DISP_TEMP 125
#define MAX_CTRL_TEMP 105
#define MIN_CTRL_TEMP 40
#define MAX_NR_TZONE 16
#define MAX_NR_CDEV 32
#define MAX_NR_TRIP 16
#define MAX_NR_CDEV_TRIP 12 /* number of cooling devices that can bind
* to a thermal zone trip.
*/
#define MAX_TEMP_KC 140000
/* starting char position to draw sensor data, such as tz names
* trip point list, etc.
*/
#define DATA_LEFT_ALIGN 10
#define NR_LINES_TZDATA 1
#define TMON_LOG_FILE "/var/tmp/tmon.log"
extern unsigned long ticktime;
extern double time_elapsed;
extern unsigned long target_temp_user;
extern int dialogue_on;
extern char ctrl_cdev[];
extern pthread_mutex_t input_lock;
extern int tmon_exit;
extern int target_thermal_zone;
/* use fixed size record to simplify data processing and transfer
* TBD: more info to be added, e.g. programmable trip point data.
*/
struct thermal_data_record {
struct timeval tv;
unsigned long temp[MAX_NR_TZONE];
double pid_out_pct;
};
struct cdev_info {
char type[64];
int instance;
unsigned long max_state;
unsigned long cur_state;
unsigned long flag;
};
enum trip_type {
THERMAL_TRIP_CRITICAL,
THERMAL_TRIP_HOT,
THERMAL_TRIP_PASSIVE,
THERMAL_TRIP_ACTIVE,
NR_THERMAL_TRIP_TYPE,
};
struct trip_point {
enum trip_type type;
unsigned long temp;
unsigned long hysteresis;
int attribute; /* programmability etc. */
};
/* thermal zone configuration information, binding with cooling devices could
* change at runtime.
*/
struct tz_info {
char type[256]; /* e.g. acpitz */
int instance;
int passive; /* active zone has passive node to force passive mode */
int nr_cdev; /* number of cooling device binded */
int nr_trip_pts;
struct trip_point tp[MAX_NR_TRIP];
unsigned long cdev_binding; /* bitmap for attached cdevs */
/* cdev bind trip points, allow one cdev bind to multiple trips */
unsigned long trip_binding[MAX_NR_CDEV];
};
struct tmon_platform_data {
int nr_tz_sensor;
int nr_cooling_dev;
/* keep track of instance ids since there might be gaps */
int max_tz_instance;
int max_cdev_instance;
struct tz_info *tzi;
struct cdev_info *cdi;
};
struct control_ops {
void (*set_ratio)(unsigned long ratio);
unsigned long (*get_ratio)(unsigned long ratio);
};
enum cdev_types {
CDEV_TYPE_PROC,
CDEV_TYPE_FAN,
CDEV_TYPE_MEM,
CDEV_TYPE_NR,
};
/* REVISIT: the idea is to group sensors if possible, e.g. on intel mid
* we have "skin0", "skin1", "sys", "msicdie"
* on DPTF enabled systems, we might have PCH, TSKN, TAMB, etc.
*/
enum tzone_types {
TZONE_TYPE_ACPI,
TZONE_TYPE_PCH,
TZONE_TYPE_NR,
};
/* limit the output of PID controller adjustment */
#define LIMIT_HIGH (95)
#define LIMIT_LOW (2)
struct pid_params {
double kp; /* Controller gain from Dialog Box */
double ki; /* Time-constant for I action from Dialog Box */
double kd; /* Time-constant for D action from Dialog Box */
double ts;
double k_lpf;
double t_target;
double y_k;
};
extern int init_thermal_controller(void);
extern void controller_handler(const double xk, double *yk);
extern struct tmon_platform_data ptdata;
extern struct pid_params p_param;
extern FILE *tmon_log;
extern int cur_thermal_record; /* index to the trec array */
extern struct thermal_data_record trec[];
extern const char *trip_type_name[];
extern unsigned long no_control;
extern void initialize_curses(void);
extern void show_controller_stats(char *line);
extern void show_title_bar(void);
extern void setup_windows(void);
extern void disable_tui(void);
extern void show_sensors_w(void);
extern void show_data_w(void);
extern void write_status_bar(int x, char *line);
extern void show_control_w();
extern void show_cooling_device(void);
extern void show_dialogue(void);
extern int update_thermal_data(void);
extern int probe_thermal_sysfs(void);
extern void free_thermal_data(void);
extern void resize_handler(int sig);
extern void set_ctrl_state(unsigned long state);
extern void get_ctrl_state(unsigned long *state);
extern void *handle_tui_events(void *arg);
extern int sysfs_set_ulong(char *path, char *filename, unsigned long val);
extern int zone_instance_to_index(int zone_inst);
extern void close_windows(void);
#define PT_COLOR_DEFAULT 1
#define PT_COLOR_HEADER_BAR 2
#define PT_COLOR_ERROR 3
#define PT_COLOR_RED 4
#define PT_COLOR_YELLOW 5
#define PT_COLOR_GREEN 6
#define PT_COLOR_BRIGHT 7
#define PT_COLOR_BLUE 8
/* each thermal zone uses 12 chars, 8 for name, 2 for instance, 2 space
* also used to list trip points in forms of AAAC, which represents
* A: Active
* C: Critical
*/
#define TZONE_RECORD_SIZE 12
#define TZ_LEFT_ALIGN 32
#define CDEV_NAME_SIZE 20
#define CDEV_FLAG_IN_CONTROL (1 << 0)
/* dialogue box starts */
#define DIAG_X 48
#define DIAG_Y 8
#define THERMAL_SYSFS "/sys/class/thermal"
#define CDEV "cooling_device"
#define TZONE "thermal_zone"
#define TDATA_LEFT 16
#endif /* TMON_H */
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment