-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
core-ignite-cpu.c
376 lines (331 loc) · 10.9 KB
/
core-ignite-cpu.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright (C) 2016-2021 Canonical, Ltd.
* Copyright (C) 2022-2024 Colin Ian King.
*
* 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 2
* of the License, or (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "stress-ng.h"
#include "core-arch.h"
#include "core-builtin.h"
#include "core-ignite-cpu.h"
#include "core-killpid.h"
#define SETTING_SCALING_FREQ (0x01)
#define SETTING_CPUINFO_FREQ (0x02)
#define SETTING_FREQ (SETTING_SCALING_FREQ | SETTING_CPUINFO_FREQ)
#define SETTING_ENERGY_PERF_BIAS (0x04)
#define SETTING_GOVERNOR (0x08)
typedef struct {
const char *path; /* Path of /sys control */
const char *default_setting; /* Default maximizing setting to use */
size_t default_setting_len; /* Length of default setting */
char *setting; /* Original setting to restore it */
size_t setting_len; /* Length of setting */
bool ignore; /* true to ignore using this */
} stress_settings_t;
typedef struct {
uint64_t scaling_max_freq; /* Max scaling frequency */
uint64_t scaling_min_freq; /* Min scaling frequency */
uint64_t cpuinfo_max_freq; /* Max cpu frequency */
uint64_t cpuinfo_min_freq; /* Min cpu frequency */
char cur_governor[128]; /* Original governor setting */
uint8_t setting_flag; /* 0 if setting can't be read or set */
int8_t energy_perf_bias; /* Energy perf bias */
} stress_cpu_setting_t;
static stress_cpu_setting_t *cpu_settings; /* Array of cpu settings */
static pid_t pid; /* PID of ignite process */
static bool enabled; /* true if ignite process running */
static int32_t max_cpus; /* max cpus configured */
static int latency_fd; /* /dev/cpu_dma_latency fd */
#define SETTING(path, default_setting) \
{ path, default_setting, 0, NULL, 0, false }
static stress_settings_t settings[] = {
#if defined(__linux__) && \
defined(STRESS_ARCH_X86)
/* x86 Intel P-State maximizing settings */
SETTING("/sys/devices/system/cpu/intel_pstate/max_perf_pct", "100"),
SETTING("/sys/devices/system/cpu/intel_pstate/no_turbo", "0"),
#endif
SETTING(NULL, NULL)
};
/*
* stress_ignite_cpu_set()
* attempt to apply settings, disable settings in settings_flag
* if we cannot apply them.
*/
static void stress_ignite_cpu_set(
bool maximize_freq,
const int32_t cpu,
const uint64_t max_freq,
const uint64_t min_freq,
const int8_t energy_perf_bias,
const char *governor,
uint8_t *setting_flag)
{
char path[PATH_MAX];
char buffer[128];
if (((*setting_flag & SETTING_FREQ) == SETTING_FREQ) && (max_freq > 0) && (min_freq <= max_freq)) {
const uint64_t freq_delta = (max_freq - min_freq) / 10;
uint64_t freq;
(void)snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%" PRId32
"/cpufreq/scaling_max_freq", cpu);
(void)snprintf(buffer, sizeof(buffer), "%" PRIu64 "\n", max_freq);
if (stress_system_write(path, buffer, strlen(buffer)) < 0)
*setting_flag &= ~SETTING_SCALING_FREQ;
/* Try to set min to be 100% of max down to lowest, which ever works first*/
(void)snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%" PRId32
"/cpufreq/scaling_min_freq", cpu);
freq = (maximize_freq) ? max_freq : min_freq;
while ((freq_delta > 0) && (freq >= min_freq)) {
(void)snprintf(buffer, sizeof(buffer), "%" PRIu64 "\n", freq);
if (stress_system_write(path, buffer, strlen(buffer)) >= 0)
break;
freq -= freq_delta;
}
}
if ((*setting_flag & SETTING_ENERGY_PERF_BIAS) && (energy_perf_bias >= 0)) {
(void)snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%" PRId32
"/power/energy_perf_bias", cpu);
(void)snprintf(buffer, sizeof(buffer), "%" PRId8 "\n", energy_perf_bias);
if (stress_system_write(path, buffer, strlen(buffer)) < 0)
*setting_flag &= ~SETTING_ENERGY_PERF_BIAS;
}
if ((*setting_flag & SETTING_GOVERNOR) && (*governor != '\0')) {
(void)snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%" PRId32
"/cpufreq/scaling_governor", cpu);
if (stress_system_write(path, governor, strlen(governor)) < 0)
*setting_flag &= ~SETTING_GOVERNOR;
}
}
/*
* stress_ignite_cpu_start()
* crank up the CPUs, start a child process to continually
* set the most demanding CPU settings
*/
void stress_ignite_cpu_start(void)
{
size_t i, n = 0;
if (enabled)
return;
latency_fd = open("/dev/cpu_dma_latency", O_WRONLY);
if (latency_fd != -1) {
int32_t lat = 0;
VOID_RET(ssize_t, write(latency_fd, &lat, sizeof(lat)));
}
max_cpus = stress_get_processors_configured();
if (max_cpus < 1)
max_cpus = 1; /* Has to have at least 1 cpu! */
cpu_settings = (stress_cpu_setting_t *)calloc((size_t)max_cpus, sizeof(*cpu_settings));
if (!cpu_settings) {
pr_dbg("ignite-cpu: no cpu settings allocated\n");
} else {
int32_t cpu;
char buffer[128];
char path[PATH_MAX];
/*
* Gather per-cpu max scaling frequencies and governors
*/
for (cpu = 0; cpu < max_cpus; cpu++) {
ssize_t ret;
cpu_settings[cpu].scaling_max_freq = 0;
cpu_settings[cpu].scaling_min_freq = 0;
cpu_settings[cpu].cpuinfo_max_freq = 0;
cpu_settings[cpu].cpuinfo_min_freq = 0;
cpu_settings[cpu].setting_flag = 0;
cpu_settings[cpu].energy_perf_bias = -1;
(void)shim_memset(buffer, 0, sizeof(buffer));
(void)snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%" PRId32
"/cpufreq/scaling_max_freq", cpu);
ret = stress_system_read(path, buffer, sizeof(buffer));
if (ret > 0) {
ret = sscanf(buffer, "%" SCNu64,
&cpu_settings[cpu].scaling_max_freq);
if (ret == 1)
cpu_settings[cpu].setting_flag |= SETTING_SCALING_FREQ;
}
(void)shim_memset(buffer, 0, sizeof(buffer));
(void)snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%" PRId32
"/cpufreq/scaling_min_freq", cpu);
ret = stress_system_read(path, buffer, sizeof(buffer));
if (ret > 0) {
ret = sscanf(buffer, "%" SCNu64,
&cpu_settings[cpu].scaling_min_freq);
if (ret != 1)
cpu_settings[cpu].setting_flag &= ~SETTING_SCALING_FREQ;
}
(void)shim_memset(buffer, 0, sizeof(buffer));
(void)snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%" PRId32
"/cpufreq/cpuinfo_max_freq", cpu);
ret = stress_system_read(path, buffer, sizeof(buffer));
if (ret > 0) {
ret = sscanf(buffer, "%" SCNu64,
&cpu_settings[cpu].cpuinfo_max_freq);
if (ret == 1)
cpu_settings[cpu].setting_flag |= SETTING_CPUINFO_FREQ;
}
(void)shim_memset(buffer, 0, sizeof(buffer));
(void)snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%" PRId32
"/cpufreq/cpuinfo_min_freq", cpu);
ret = stress_system_read(path, buffer, sizeof(buffer));
if (ret > 0) {
ret = sscanf(buffer, "%" SCNu64,
&cpu_settings[cpu].cpuinfo_min_freq);
if (ret != 1)
cpu_settings[cpu].setting_flag &= ~SETTING_CPUINFO_FREQ;
}
(void)shim_memset(cpu_settings[cpu].cur_governor, 0,
sizeof(cpu_settings[cpu].cur_governor));
(void)snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%" PRId32
"/cpufreq/scaling_governor", cpu);
ret = stress_system_read(path, cpu_settings[cpu].cur_governor,
sizeof(cpu_settings[cpu].cur_governor));
if (ret > 0)
cpu_settings[cpu].setting_flag |= SETTING_GOVERNOR;
(void)shim_memset(buffer, 0, sizeof(buffer));
(void)snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%" PRId32
"/power/energy_perf_bias", cpu);
ret = stress_system_read(path, buffer, sizeof(buffer));
if (ret > 0) {
int8_t bias;
ret = sscanf(buffer, "%" SCNd8, &bias);
if (ret == 1) {
cpu_settings[cpu].energy_perf_bias = bias;
cpu_settings[cpu].setting_flag |= SETTING_ENERGY_PERF_BIAS;
}
}
}
}
pid = -1;
for (i = 0; settings[i].path; i++) {
char buf[4096];
ssize_t ret;
size_t len;
settings[i].ignore = true;
ret = stress_system_read(settings[i].path, buf, sizeof(buf) - 1);
if (ret < 0)
continue;
buf[ret] = '\0';
len = strlen(buf);
if (len == 0)
continue;
settings[i].default_setting_len =
strlen(settings[i].default_setting);
/* If we can't update the setting, skip it */
ret = stress_system_write(settings[i].path,
settings[i].default_setting,
settings[i].default_setting_len);
if (ret < 0) {
pr_dbg("ignite-cpu: cannot set %s to %s, "
"errno=%zd (%s)\n",
settings[i].path, settings[i].default_setting,
-ret, strerror((int)-ret));
continue;
}
settings[i].setting = (char *)calloc(1, len + 1);
if (!settings[i].setting)
continue;
(void)shim_strscpy(settings[i].setting, buf, len);
settings[i].setting_len = len;
settings[i].ignore = false;
n++;
}
if (n == 0)
return;
enabled = true;
pid = fork();
if (pid < 0) {
pr_dbg("ignite-cpu: failed to start ignite cpu daemon, "
"errno=%d (%s)\n", errno, strerror(errno));
return;
} else if (pid == 0) {
/* Child */
stress_parent_died_alarm();
stress_set_proc_state_str("ignite","periodic");
while (stress_continue_flag()) {
for (i = 0; settings[i].path; i++) {
if (settings[i].ignore)
continue;
(void)stress_system_write(settings[i].path,
settings[i].default_setting,
settings[i].default_setting_len);
}
if (cpu_settings) {
int32_t cpu;
const char *governor = (stress_mwc8() < 240) ? "performance" : "powersave";
/*
* Attempt to crank CPUs up to max freq
*/
for (cpu = 0; cpu < max_cpus; cpu++) {
if (cpu_settings[cpu].setting_flag == 0)
continue;
stress_ignite_cpu_set(true, cpu,
cpu_settings[cpu].cpuinfo_max_freq,
cpu_settings[cpu].cpuinfo_min_freq,
0,
governor,
&cpu_settings[cpu].setting_flag);
}
}
(void)sleep(1);
}
_exit(0);
}
}
/*
* stress_ignite_cpu_stop()
* stop updating settings and restore to original settings
*/
void stress_ignite_cpu_stop(void)
{
size_t i;
if (latency_fd != -1) {
(void)close(latency_fd);
latency_fd = -1;
}
if (pid > -1)
(void)stress_kill_pid_wait(pid, NULL);
if (cpu_settings) {
int32_t cpu;
for (cpu = 0; cpu < max_cpus; cpu++) {
stress_ignite_cpu_set(false, cpu,
cpu_settings[cpu].cpuinfo_max_freq,
cpu_settings[cpu].cpuinfo_min_freq,
cpu_settings[cpu].energy_perf_bias,
cpu_settings[cpu].cur_governor,
&cpu_settings[cpu].setting_flag);
}
free(cpu_settings);
}
for (i = 0; settings[i].path; i++) {
if (settings[i].ignore)
continue;
(void)stress_system_write(settings[i].path, settings[i].setting,
settings[i].setting_len);
free(settings[i].setting);
settings[i].setting = NULL;
settings[i].ignore = true;
}
enabled = false;
}