This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_throttle.c
193 lines (152 loc) · 5.05 KB
/
cpu_throttle.c
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
/**
* cpu_throttle.c
*
* Copyright (C) 2017 Vincent Zvikaramba <vincent.zvikaramba@mail.utoronto.ca>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This source 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.
*
* You should have received a copy of the GNU General Public License
* along with the source code. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "cpu_throttle.h"
int main(int argc, char *argv[])
{
/* initialise the sigaction struct */
struct sigaction sa;
/* loop variables */
int i, rc;
/* Read sysfs interfaces and populate the throttle_settings
* buffer with basic defaults. */
initialise_settings();
/* parse the command line */
parse_commmand_line(argc, argv);
/* initialise the sigaction struct */
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
/*define the pthreads array */
pthread_t pthreads_arr[settings.num_cores+1];
/* set up signal handlers */
if (sigaction(SIGINT, &sa, NULL) == -1) {
perror("sigaction");
}
if (sigaction(SIGTERM, &sa, NULL) == -1) {
perror("sigaction");
}
if (sigaction(SIGHUP, &sa, NULL) == -1) {
perror("sigaction");
}
/* print statements to stderr for now */
log_file = stderr;
if (write_config) {
/* write to configuration file if one was passed */
LOGI("Saving configuration...\n", getpid());
write_configuration_file();
return 0;
}
else {
/* read a configuration file if one was passed */
read_configuration_file();
}
/* validate the settings read */
validate_settings();
/* open the log file */
if (settings.logging_enabled) {
if (!(log_file = fopen(settings.log_path, "a+"))) {
perror("fopen");
fprintf(stderr, "Could not open log file\n");
/* print statements to stderr */
log_file = stderr;
}
}
LOGI("Firing up...\n", getpid());
LOGI("\n",getpid());
LOGI("\tFound cpu core temp hwmon node at %d.\n",
getpid(), sysfs_coretemp_hwmon_node);
/* check if the sysfs fan control node exist */
if (sysfs_fanctrl_hwmon_subnode == -1) {
LOGW("\tCould not find fan control hwmon directory."
" Working without it.\n", getpid());
}
else {
LOGI("\tFound fan-control sysfs interface at node %d.\n",
getpid(), sysfs_fanctrl_hwmon_subnode);
}
LOGI("\tSuccessfully read cpu scaling limits.\n"
"\t\t\tmax_freq:%dMHz min_freq:%dMHz\n", getpid(),
KHZ_TO_MHZ(cpuinfo_max_freq),
KHZ_TO_MHZ(cpuinfo_min_freq));
/* print some information about the values we set */
LOGI("\n",getpid());
LOGI("\tSet polling interval to %dms.\n",
getpid(), US_TO_MS(settings.polling_interval));
LOGI("\tSet maximum scaling freq to %dMHz.\n",
getpid(), KHZ_TO_MHZ(settings.cpu_max_freq));
LOGI("\tSet cpu scaling step to %dMHz.\n",
getpid(), KHZ_TO_MHZ(settings.cpu_scaling_step));
LOGI("\tSet cpu target temperature to %dC.\n",
getpid(), MC_TO_C(settings.cpu_target_temperature));
LOGI("\tSet cpus to throttle to %d.\n", getpid(), settings.num_cores);
LOGI("\n",getpid());
LOGI("\tSet hysteresis to %dC."
" Temp range: %dC <= %dC <= %dC \n",
getpid(), MC_TO_C(settings.hysteresis),
MC_TO_C(hysteresis_lower_limit),
MC_TO_C(settings.cpu_target_temperature),
MC_TO_C(hysteresis_upper_limit));
LOGI("\tSet hysteresis threshold to %d intervals.\n",
getpid(), settings.hysteresis_reset_threshold);
LOGI("\n",getpid());
if (sysfs_fanctrl_hwmon_subnode != -1) {
LOGI("\n",getpid());
LOGI("\tSet fan scaling step to %d.\n",
getpid(), settings.fan_scaling_step);
LOGI("\tSet fan minimum speed to %d.\n",
getpid(), settings.fan_min_speed);
LOGI("\tSuccessfully read fan speed limits.\n"
"\t\tspeed_max: %d\t speed_min: %d\n", getpid(),
fan_hw_max_speed, fan_hw_min_speed);
LOGI("\n",getpid());
}
/* start the scaling/throttling threads */
LOGI("Done reading/setting throttling parameters. "
"Starting throttling threads...\n", getpid());
/* use a fixed memory area for the values
* needed by pthread */
int core[settings.num_cores];
/* start the worker threads */
for (i = 0; i < settings.num_cores; i++) {
LOGI("Starting thread for cpu%d...\n", getpid(), i);
core[i] = i;
rc = pthread_create(&(pthreads_arr[i]),
NULL, cpu_worker, &(core[i]));
if (rc) {
LOGE("Failed to start thread for cpu%d...\n",
getpid(), core[i]);
exit(EXIT_FAILURE);
}
}
/* start the fan worker */
rc = pthread_create(&(pthreads_arr[i]), NULL, fan_worker, NULL);
if (rc) {
LOGE("Failed to start fan worker thread.\n", getpid());
}
/* wait for the worker threads to finish */
for (i = 0; i <= settings.num_cores; i++) {
LOGI("[worker%d] Waiting for thread...\n", getpid(), i);
int rc = pthread_join(pthreads_arr[i], NULL);
if (rc) {
LOGE("[worker%d] Failed to join thread.\n", getpid(), i);
}
}
return 0;
}