1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include "sysfs.h"
#include "cpufreq.h"
#include "debug.h"
#include "cputemp2maxfreq.h"
#include "failsafe.h"
#include "version.h"
#include "argparse.h"
#include "logger.h"
#include "cputemp.h"
// Set default config
struct s_config config={
"", // Name of this program, set by argparse
"conservative", // Governor
70000, // Temperature
"auto", // Temperature input
100000, // Frequency step
2000000, // Fallback frequency
10, // Interval
"stdout", // Logger name
&logger_stdout, // Logger function
0, // Don't log measurement data
"", // CSV logfile
0, // Don't overwrite CSV logfile
NULL, // File handler for CSV file
0, // Don't use unix timestamps in log outputs
-1, // Transition latency, default to autodetect
};
struct s_cpudata cpudata;
void signal_handler(int signum)
{
config.logger("Received signal %d, exiting.",signum);
failsafe(0);
}
char *transition_latency_remarks[] = {
"Autodetected",
"Set via commandline",
"This seems low, consider setting it via -d"
};
char *autodetect_sensors[] = {
"Package id 0",
"Tctl",
"k10temp",
NULL
};
int main(int argc,char **argv)
{
int count;
long int diff;
long int newfreq;
struct s_sensor sensor;
char *transition_latency_remark;
argparse(argc,argv);
config.logger("%s version %s, buildtime %s %s",config.name,version(),__DATE__,__TIME__);
// Log configuration
config.logger("Configuration:");
config.logger("Governor: %s",config.governor);
config.logger("Temperature: %ld",config.max_temp);
config.logger("Temp input: %s",config.temp_input);
config.logger("Frequency step: %ld",config.freq_step);
config.logger("Fallback frquency: %ld",config.fallback_freq);
config.logger("Interval: %d",config.interval);
config.logger("Logger: %s (%p)",config.logger_name,config.logger);
config.logger("Log measurement data: %d",config.log_data);
if (config.csvlog[0]==0)
{
config.logger("CSV Log file: Disabled");
} else {
config.logger("CSV Log file: %s",config.csvlog);
config.logger("Overwrite CSV log: %d",config.csvoverwrite);
}
config.logger("Use unix timestamps: %d",config.use_unixtime);
if (config.transition_latency<0)
{
config.logger("Transition latency: Autodetect");
} else {
config.logger("Transition latency: %d",config.transition_latency);
}
if ((config.max_temp<VALID_TEMP_MIN) || (config.max_temp>VALID_TEMP_MAX))
{
config.logger("Invalid temperature, range is %d-%d",VALID_TEMP_MIN,VALID_TEMP_MAX);
exit(1);
}
if ((config.freq_step<VALID_STEP_MIN) || (config.freq_step>VALID_STEP_MAX))
{
config.logger("Invalid frequency step, range is %d-%d",VALID_STEP_MIN,VALID_STEP_MAX);
exit(1);
}
if ((config.interval<VALID_INTERVAL_MIN) || (config.interval>VALID_INTERVAL_MAX))
{
config.logger("Invalid poll interval, range is %d-%d",VALID_INTERVAL_MIN,VALID_INTERVAL_MAX);
exit(1);
}
// Autodetect temperature input if needed
if (config.temp_input[0]!='/')
{
if (strcmp(config.temp_input,"auto")==0)
{
config.logger("Starting temperature sensor autodetection");
for(count=0;autodetect_sensors[count]!=NULL;count++)
{
DEBUG1_MAIN("Searching for sensor \"%s\"\n",autodetect_sensors[count]);
cputemp_find_sensor(autodetect_sensors[count],&sensor);
if (sensor.valid==1) break;
}
} else {
DEBUG1_MAIN("Starting temperature sensor detection with name \"%s\"\n",config.temp_input);
cputemp_find_sensor(config.temp_input,&sensor);
}
if (sensor.valid==1)
{
DEBUG1_MAIN("Found sensor \"%s\"\n",sensor.name);
DEBUG1_MAIN("Sensor file: %s\n",sensor.filename);
strncpy(config.temp_input,sensor.filename,sizeof(config.temp_input));
config.logger("Detection returned temperature input: %s",config.temp_input);
config.logger("Temperature input name: %s",sensor.name);
} else {
config.logger("Detection failed to return a valid sensor, set it using -i");
exit(1);
}
}
// Get and validate CPU data
cpudata.min_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/cpuinfo_min_freq");
cpudata.max_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq");
cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
if (config.transition_latency<0)
{
transition_latency_remark=transition_latency_remarks[0];
cpudata.transition_latency=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/cpuinfo_transition_latency");
if (cpudata.transition_latency<1000) transition_latency_remark=transition_latency_remarks[2];
} else {
transition_latency_remark=transition_latency_remarks[1];
cpudata.transition_latency=config.transition_latency;
}
config.logger("CPU data:");
config.logger("Minimum frequency: %ld",cpudata.min_freq);
config.logger("Maximum frequency: %ld",cpudata.max_freq);
config.logger("Scaling maximum frequency: %ld",cpudata.scale_max);
config.logger("Transition Latency: %ld (%s)",cpudata.transition_latency,transition_latency_remark);
if ((cpudata.min_freq<VALID_FREQ_MIN) || (cpudata.min_freq>VALID_FREQ_MAX) ||
(cpudata.max_freq<VALID_FREQ_MIN) || (cpudata.max_freq>VALID_FREQ_MAX) ||
(cpudata.scale_max<VALID_FREQ_MIN) || (cpudata.scale_max>VALID_FREQ_MAX) ||
(cpudata.transition_latency<VALID_TRANS_MIN) || (cpudata.transition_latency>VALID_TRANS_MAX))
{
// If we have to fail now, there's not much we can do because we have no data
config.logger("Invalid CPU data, exiting");
exit(1);
}
// Modify fallback frequency if needed
if (config.fallback_freq<cpudata.min_freq)
{
config.logger("Fallback frequency is lower than CPU's minimum frequency, setting to %ld",cpudata.min_freq);
config.fallback_freq=cpudata.min_freq;
}
if (config.fallback_freq>cpudata.max_freq)
{
config.logger("Fallback frequency is higher than CPU's maximum frequency, setting to %ld",cpudata.max_freq);
config.fallback_freq=cpudata.max_freq;
}
// Set the governor
if (strcmp(config.governor,"keep")!=0)
{
if (cpufreq_set_str("scaling_governor",config.governor,0)<0)
{
config.logger("Failed to set governor, error %d (%s)",errno,strerror(errno));
// We failed to set the governor, call the failsafe
failsafe(1);
}
}
// Initialise CSV logging
if (config.csvlog[0]!=0) csvlog_init();
// Set signal handlers
signal(SIGTERM,signal_handler);
signal(SIGINT,signal_handler);
signal(SIGQUIT,signal_handler);
while(1)
{
// Get new measurements
cpudata.cur_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq");
if ((cpudata.cur_freq<VALID_FREQ_MIN) || (cpudata.cur_freq>VALID_FREQ_MAX))
{
config.logger("Invalid current frequency reported by CPU, exiting");
failsafe(1);
}
cpudata.cur_temp=sysfs_read_long_int(config.temp_input);
if ((cpudata.cur_temp<VALID_TEMP_MIN) || (cpudata.cur_temp>VALID_TEMP_MAX))
{
config.logger("Invalid current temperature reported by CPU, exiting");
failsafe(1);
}
DEBUG1_MAIN("Data: %ld %ld %ld %ld %ld\n",cpudata.cur_temp,config.max_temp,cpudata.max_freq,cpudata.scale_max,cpudata.cur_freq);
if (config.log_data>0)
{
config.logger("CPU Temperature: %ld, CPU Frequency: %ld",cpudata.cur_temp/1000,cpudata.cur_freq);
}
if (config.csvfile!=NULL) csvlog_write();
diff=config.max_temp-cpudata.cur_temp;
// Check if we should increase
if ((diff>=1000) && (cpudata.scale_max<cpudata.max_freq))
{
newfreq=cpudata.scale_max+(config.freq_step*(diff/1000));
if (newfreq>cpudata.max_freq) newfreq=cpudata.max_freq;
DEBUG1_MAIN("Increase to %ld\n",newfreq);
config.logger("Increase scaling_max_freq to %ld",newfreq);
// Set new value and validate
if (cpufreq_set_long_int("scaling_max_freq",newfreq,cpudata.transition_latency)<0)
{
config.logger("Failed to set scaling_max_freq, exiting");
failsafe(1);
}
cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
if ((cpudata.scale_max<VALID_FREQ_MIN) || (cpudata.scale_max>VALID_FREQ_MAX))
{
config.logger("Invalid scale_max_freq reported, exiting");
failsafe(1);
}
}
// Check if we should decrease
if ((diff<=-1000) && (cpudata.scale_max>cpudata.min_freq))
{
newfreq=cpudata.scale_max+(config.freq_step*(diff/1000));
if (newfreq<cpudata.min_freq) newfreq=cpudata.min_freq;
DEBUG1_MAIN("Decrease to %ld\n",newfreq);
config.logger("Decrease scaling_max_freq to %ld",newfreq);
// Set new value and validate
if (cpufreq_set_long_int("scaling_max_freq",newfreq,cpudata.transition_latency)<0)
{
config.logger("Failed to set scaling_max_freq, exiting");
failsafe(1);
}
cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
if ((cpudata.scale_max<VALID_FREQ_MIN) || (cpudata.scale_max>VALID_FREQ_MAX))
{
config.logger("Invalid scale_max_freq reported, exiting");
failsafe(1);
}
}
sleep(config.interval);
}
}
|