forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignite-cpu.c
159 lines (140 loc) · 4.03 KB
/
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
/*
* Copyright (C) 2016-2017 Canonical, Ltd.
*
* 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.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
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 */
} settings_t;
static pid_t pid; /* PID of ignite process */
static bool enabled; /* true if ignite process running */
#define SETTING(path, default_setting) \
{ path, default_setting, 0, NULL, 0, false }
static settings_t settings[] = {
#if defined(__linux__) && defined(STRESS_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)
};
/*
* ignite_cpu_start()
* crank up the CPUs, start a child process to continually
* set the most demanding CPU settings
*/
void ignite_cpu_start(void)
{
size_t i, n = 0;
if (enabled)
return;
pid = -1;
for (i = 0; settings[i].path; i++) {
char buf[4096];
int ret;
size_t len;
settings[i].ignore = true;
ret = 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 = 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=%d (%s)\n",
settings[i].path, settings[i].default_setting,
-ret, strerror(-ret));
continue;
}
settings[i].setting = calloc(1, len + 1);
if (!settings[i].setting)
continue;
(void)strncpy(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("failed to start ignite cpu daemon, "
"errno=%d (%s)\n", errno, strerror(errno));
return;
} else if (pid == 0) {
/* Child */
(void)setpgid(0, g_pgrp);
stress_parent_died_alarm();
for (;;) {
for (i = 0; settings[i].path; i++) {
if (settings[i].ignore)
continue;
(void)system_write(settings[i].path,
settings[i].default_setting,
settings[i].default_setting_len);
}
sleep(1);
}
} else {
/* Parent */
(void)setpgid(pid, g_pgrp);
}
}
/*
* ignite_cpu_stop()
* stop updating settings and restore to original settings
*/
void ignite_cpu_stop(void)
{
size_t i;
int status;
if (pid > -1) {
(void)kill(pid, SIGTERM);
(void)kill(pid, SIGKILL);
(void)waitpid(pid, &status, 0);
}
for (i = 0; settings[i].path; i++) {
if (settings[i].ignore)
continue;
(void)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;
}