forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore-sched.c
371 lines (338 loc) · 9.28 KB
/
core-sched.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
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022-2023 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"
#ifndef SCHED_FLAG_RESET_ON_FORK
#define SCHED_FLAG_RESET_ON_FORK (0x01)
#endif
#ifndef SCHED_FLAG_RECLAIM
#define SCHED_FLAG_RECLAIM (0x02)
#endif
#ifndef SCHED_FLAG_DL_OVERRUN
#define SCHED_FLAG_DL_OVERRUN (0x04)
#endif
typedef struct {
const int sched;
const char *const sched_name;
} stress_sched_types_t;
static const stress_sched_types_t sched_types[] = {
#if defined(SCHED_BATCH)
{ SCHED_BATCH, "batch" },
#endif
#if defined(SCHED_DEADLINE)
{ SCHED_DEADLINE, "deadline" },
#endif
#if defined(SCHED_FIFO)
{ SCHED_FIFO, "fifo" },
#endif
#if defined(SCHED_IDLE)
{ SCHED_IDLE, "idle" },
#endif
#if defined(SCHED_OTHER)
{ SCHED_OTHER, "other" },
#endif
#if defined(SCHED_RR)
{ SCHED_RR, "rr" },
#endif
};
/*
* get_sched_name()
* convert sched class to human readable string
*/
const char *stress_get_sched_name(const int sched)
{
size_t i;
for (i = 0; i < SIZEOF_ARRAY(sched_types); i++) {
if (sched_types[i].sched == sched)
return sched_types[i].sched_name;
}
return "unknown";
}
#if (defined(_POSIX_PRIORITY_SCHEDULING) || defined(__linux__)) && \
!defined(__OpenBSD__) && \
!defined(__minix__) && \
!defined(__APPLE__) && \
!defined(__serenity__)
static const char prefix[] = "sched";
#if defined(SCHED_DEADLINE) && \
defined(__linux__)
#define HAVE_STRESS_SET_DEADLINE_SCHED (1)
int stress_set_deadline_sched(
const pid_t pid,
const uint64_t period,
const uint64_t runtime,
const uint64_t deadline,
const bool quiet)
{
int rc;
struct shim_sched_attr attr;
(void)memset(&attr, 0, sizeof(attr));
attr.size = sizeof(attr);
attr.sched_policy = SCHED_DEADLINE;
/* make sched_deadline task be able to fork*/
attr.sched_flags = SCHED_FLAG_RESET_ON_FORK;
if (g_opt_flags & OPT_FLAGS_DEADLINE_GRUB)
attr.sched_flags |= SCHED_FLAG_RECLAIM;
attr.sched_nice = 0;
attr.sched_priority = 0;
if (!quiet)
pr_dbg("%s: setting scheduler class '%s' (period=%" PRIu64
", runtime=%" PRIu64 ", deadline=%" PRIu64 ")\n",
prefix, "deadline", period, runtime, deadline);
attr.sched_runtime = runtime;
attr.sched_deadline = deadline;
attr.sched_period = period;
rc = shim_sched_setattr(pid, &attr, 0);
if (rc < 0) {
rc = -errno;
if (!quiet)
pr_inf("%s: cannot set scheduler: errno=%d (%s)\n",
prefix, errno, strerror(errno));
return rc;
}
return 0;
}
#endif
#define HAVE_STRESS_SET_SCHED (1)
/*
* set_sched()
* are sched settings valid, if so, set them
*/
int stress_set_sched(
const pid_t pid,
const int sched,
const int sched_priority,
const bool quiet)
{
#if defined(SCHED_FIFO) || defined(SCHED_RR)
int min, max;
#endif
#if defined(SCHED_DEADLINE) && \
defined(__linux__)
struct shim_sched_attr attr;
#endif
int rc;
struct sched_param param;
const char *sched_name = stress_get_sched_name(sched);
#if defined(SCHED_DEADLINE) && \
defined(__linux__)
uint64_t sched_period = 0;
uint64_t sched_runtime = 10000;
uint64_t sched_deadline = 0;
#endif
(void)memset(¶m, 0, sizeof(param));
switch (sched) {
case UNDEFINED: /* No preference, don't set */
return 0;
#if defined(SCHED_FIFO) || defined(SCHED_RR)
#if defined(SCHED_FIFO)
case SCHED_FIFO:
#endif
#if defined(SCHED_RR)
case SCHED_RR:
#endif
min = sched_get_priority_min(sched);
max = sched_get_priority_max(sched);
param.sched_priority = sched_priority;
if (sched_priority == UNDEFINED) {
if (g_opt_flags & OPT_FLAGS_AGGRESSIVE)
param.sched_priority = max;
else
param.sched_priority = (max - min) / 2;
if (!quiet)
pr_inf("%s: priority not given (or set to -1), defaulting to %d\n",
prefix, param.sched_priority);
}
if ((param.sched_priority < min) ||
(param.sched_priority > max)) {
if (!quiet)
pr_inf("%s: scheduler priority level must be "
"set between %d and %d\n",
prefix, min, max);
return -EINVAL;
}
if (!quiet)
pr_dbg("%s: setting scheduler class '%s', priority %d\n",
prefix, sched_name, param.sched_priority);
break;
#endif
#if defined(SCHED_DEADLINE) && \
defined(__linux__)
case SCHED_DEADLINE:
min = sched_get_priority_min(sched);
max = sched_get_priority_max(sched);
(void)memset(&attr, 0, sizeof(attr));
attr.size = sizeof(attr);
attr.sched_policy = SCHED_DEADLINE;
attr.sched_flags = SCHED_FLAG_RESET_ON_FORK;
attr.sched_nice = SCHED_OTHER;
attr.sched_priority = (unsigned int)sched_priority;
if (sched_priority == UNDEFINED) {
if (g_opt_flags & OPT_FLAGS_AGGRESSIVE)
attr.sched_priority = (uint32_t)max;
else
attr.sched_priority = (uint32_t)((max - min) / 2);
if (!quiet)
pr_inf("%s: priority not given, defaulting to %d\n",
prefix, attr.sched_priority);
}
if ((attr.sched_priority < (uint32_t)min) ||
(attr.sched_priority > (uint32_t)max)) {
if (!quiet)
pr_inf("%s: scheduler priority level must be "
"set between %d and %d\n",
prefix, min, max);
return -EINVAL;
}
if (!quiet)
pr_dbg("%s: setting scheduler class '%s'\n", prefix, sched_name);
(void)stress_get_setting("sched-period", &sched_period);
(void)stress_get_setting("sched-runtime", &sched_runtime);
(void)stress_get_setting("sched-deadline", &sched_deadline);
if (sched_deadline == 0) {
attr.sched_runtime = 90000;
attr.sched_deadline = 100000;
attr.sched_period = 0;
} else {
attr.sched_runtime = sched_runtime;
attr.sched_deadline = sched_deadline;
attr.sched_period = sched_period;
}
pr_dbg("%s: setting scheduler class '%s' (period=%" PRIu64
", runtime=%" PRIu64 ", deadline=%" PRIu64 ")\n",
"deadline", prefix, attr.sched_period,
attr.sched_runtime, attr.sched_deadline);
rc = shim_sched_setattr(pid, &attr, 0);
if (rc < 0) {
/*
* Kernel supports older (smaller) attr
* but userspace supports newer (larger) attr,
* so report this and pass it back up to re-do
* the scheduling with a non SCHED_DEADLINE
* scheduler that requires the larger attr
*/
if (errno == E2BIG)
return -E2BIG;
rc = -errno;
if (!quiet)
pr_inf("%s: cannot set scheduler '%s': errno=%d (%s)\n",
prefix, stress_get_sched_name(sched),
errno, strerror(errno));
return rc;
}
return 0;
break; /* Keep static analysers happy */
#endif
default:
param.sched_priority = 0;
if (sched_priority != UNDEFINED)
if (!quiet)
pr_inf("%s: ignoring priority level for "
"scheduler class '%s'\n", prefix, sched_name);
if (!quiet)
pr_dbg("%s: setting scheduler class '%s'\n", prefix, sched_name);
break;
}
rc = sched_setscheduler(pid, sched, ¶m);
if (rc < 0) {
rc = -errno;
if (!quiet)
pr_inf("%s: cannot set scheduler '%s': errno=%d (%s)\n",
prefix,
stress_get_sched_name(sched),
errno, strerror(errno));
return rc;
}
return 0;
}
#endif
#if !defined(HAVE_STRESS_SET_SCHED)
#define HAVE_STRESS_SET_SCHED (1)
/* No-op shim */
int stress_set_sched(
const pid_t pid,
const int sched,
const int32_t sched_priority,
const bool quiet)
{
(void)pid;
(void)sched;
(void)sched_priority;
(void)quiet;
return 0;
}
#endif
#if !defined(HAVE_STRESS_SET_DEADLINE_SCHED)
#define HAVE_STRESS_SET_DEADLINE_SCHED (1)
/* No-op shim */
int stress_set_deadline_sched(
const pid_t pid,
const uint64_t period,
const uint64_t runtime,
const uint64_t deadline,
const bool quiet)
{
(void)pid;
(void)period;
(void)runtime;
(void)deadline;
(void)quiet;
return 0;
}
#endif
/*
* get_opt_sched()
* get scheduler policy
*/
int32_t stress_get_opt_sched(const char *const str)
{
size_t i;
for (i = 0; i < SIZEOF_ARRAY(sched_types); i++) {
if (!strcmp(sched_types[i].sched_name, str))
return sched_types[i].sched;
}
if (strcmp("which", str))
(void)fprintf(stderr, "Invalid sched option: %s\n", str);
if (SIZEOF_ARRAY(sched_types) == (0)) {
(void)fprintf(stderr, "No scheduler options are available\n");
} else {
(void)fprintf(stderr, "Available scheduler options are:");
for (i = 0; i < SIZEOF_ARRAY(sched_types); i++) {
(void)fprintf(stderr, " %s", sched_types[i].sched_name);
}
(void)fprintf(stderr, "\n");
}
_exit(EXIT_FAILURE);
return 0;
}
/*
* sched_settings_apply()
* fetch scheduler settings and apply them, useful for fork'd
* child stressor processes to set the scheduling settings
* rather than assuming that they are inherited from the
* parent
*/
int sched_settings_apply(const bool quiet)
{
int32_t sched = UNDEFINED;
int32_t sched_prio = UNDEFINED;
(void)stress_get_setting("sched", &sched);
(void)stress_get_setting("sched-prio", &sched_prio);
return stress_set_sched(getpid(), (int)sched, sched_prio, quiet);
}