forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-schedpolicy.c
399 lines (352 loc) · 10.5 KB
/
stress-schedpolicy.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 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-capabilities.h"
#if defined(__NR_sched_getattr)
#define HAVE_SCHED_GETATTR
#endif
#if defined(__NR_sched_setattr)
#define HAVE_SCHED_SETATTR
#endif
static const stress_help_t help[] = {
{ NULL, "schedpolicy N", "start N workers that exercise scheduling policy" },
{ NULL, "schedpolicy-ops N", "stop after N scheduling policy bogo operations" },
{ NULL, NULL, NULL }
};
#if (defined(_POSIX_PRIORITY_SCHEDULING) || defined(__linux__)) && \
!defined(__OpenBSD__) && \
!defined(__minix__) && \
!defined(__APPLE__)
static const int policies[] = {
#if defined(SCHED_IDLE)
SCHED_IDLE,
#endif
#if defined(SCHED_FIFO)
SCHED_FIFO,
#endif
#if defined(SCHED_RR)
SCHED_RR,
#endif
#if defined(SCHED_OTHER)
SCHED_OTHER,
#endif
#if defined(SCHED_BATCH)
SCHED_BATCH,
#endif
#if defined(SCHED_DEADLINE)
SCHED_DEADLINE,
#endif
};
static int stress_schedpolicy(const stress_args_t *args)
{
int policy = args->instance % SIZEOF_ARRAY(policies);
#if defined(_POSIX_PRIORITY_SCHEDULING)
const bool root_or_nice_capability = stress_check_capability(SHIM_CAP_SYS_NICE);
#endif
#if defined(HAVE_SCHED_GETATTR) && \
defined(HAVE_SCHED_SETATTR)
uint32_t sched_util_min = ~0U;
uint32_t sched_util_max = 0;
uint32_t sched_util_max_value = 0;
int counter = 0;
#endif
#if defined(_POSIX_PRIORITY_SCHEDULING)
int n = 0;
#endif
if (SIZEOF_ARRAY(policies) == (0)) {
if (args->instance == 0) {
pr_inf_skip("%s: no scheduling policies "
"available, skipping test\n",
args->name);
}
return EXIT_NOT_IMPLEMENTED;
}
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
#if defined(HAVE_SCHED_GETATTR) && \
defined(HAVE_SCHED_SETATTR)
struct shim_sched_attr attr;
#else
UNEXPECTED
#endif
struct sched_param param;
int ret = 0;
int max_prio, min_prio, rng_prio;
int new_policy = policies[policy];
const pid_t pid = stress_mwc1() ? 0 : args->pid;
const char *new_policy_name = stress_get_sched_name(new_policy);
if (!keep_stressing(args))
break;
shim_sched_yield();
errno = 0;
switch (new_policy) {
#if defined(SCHED_DEADLINE) && \
defined(HAVE_SCHED_GETATTR) && \
defined(HAVE_SCHED_SETATTR)
case SCHED_DEADLINE:
/*
* Only have 1 RT deadline instance running
*/
if (args->instance == 0) {
(void)memset(&attr, 0, sizeof(attr));
attr.size = sizeof(attr);
attr.sched_flags = 0;
attr.sched_nice = 0;
attr.sched_priority = 0;
attr.sched_policy = SCHED_DEADLINE;
/* runtime <= deadline <= period */
attr.sched_runtime = 64 * 1000000;
attr.sched_deadline = 128 * 1000000;
attr.sched_period = 256 * 1000000;
ret = shim_sched_setattr(0, &attr, 0);
break;
}
CASE_FALLTHROUGH;
#endif
#if defined(SCHED_IDLE)
case SCHED_IDLE:
CASE_FALLTHROUGH;
#endif
#if defined(SCHED_BATCH)
case SCHED_BATCH:
CASE_FALLTHROUGH;
#endif
#if defined(SCHED_OTHER)
case SCHED_OTHER:
#endif
/* Exercise illegal policy */
(void)memset(¶m, 0, sizeof(param));
VOID_RET(int, sched_setscheduler(pid, -1, ¶m));
/* Exercise invalid PID */
param.sched_priority = 0;
VOID_RET(int, sched_setscheduler(-1, new_policy, ¶m));
/* Exercise invalid priority */
param.sched_priority = ~0;
VOID_RET(int, sched_setscheduler(pid, new_policy, ¶m));
param.sched_priority = 0;
ret = sched_setscheduler(pid, new_policy, ¶m);
break;
#if defined(SCHED_RR)
case SCHED_RR:
#if defined(HAVE_SCHED_RR_GET_INTERVAL)
{
struct timespec t;
VOID_RET(int, sched_rr_get_interval(pid, &t));
}
#endif
CASE_FALLTHROUGH;
#endif
#if defined(SCHED_FIFO)
case SCHED_FIFO:
#endif
min_prio = sched_get_priority_min(new_policy);
max_prio = sched_get_priority_max(new_policy);
/* Check if min/max is supported or not */
if ((min_prio == -1) || (max_prio == -1))
continue;
rng_prio = max_prio - min_prio;
if (rng_prio == 0) {
pr_err("%s: invalid min/max priority "
"range for scheduling policy %s "
"(min=%d, max=%d)\n",
args->name,
new_policy_name,
min_prio, max_prio);
break;
}
param.sched_priority = ((int)stress_mwc32() % rng_prio) +
min_prio;
ret = sched_setscheduler(pid, new_policy, ¶m);
break;
default:
/* Should never get here */
break;
}
if (ret < 0) {
/*
* Some systems return EINVAL for non-POSIX
* scheduling policies, silently ignore these
* failures.
*/
if ((errno != EPERM) &&
(errno != EINVAL) &&
(errno != EINTR) &&
(errno != ENOSYS) &&
(errno != EBUSY)) {
pr_fail("%s: sched_setscheduler "
"failed: errno=%d (%s) "
"for scheduler policy %s\n",
args->name, errno, strerror(errno),
new_policy_name);
}
} else {
ret = sched_getscheduler(pid);
if (ret < 0) {
pr_fail("%s: sched_getscheduler failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
} else if (ret != policies[policy]) {
pr_fail("%s: sched_getscheduler "
"failed: pid %d has policy %d (%s) "
"but function returned %d instead\n",
args->name, (int)pid, new_policy,
new_policy_name, ret);
}
}
#if defined(_POSIX_PRIORITY_SCHEDULING)
if (n++ >= 1024) {
n = 0;
/* Exercise invalid sched_getparam syscall */
(void)memset(¶m, 0, sizeof(param));
VOID_RET(int, sched_getparam(-1, ¶m));
#if defined(__linux__)
/* Linux allows NULL param, will return EFAULT */
(void)memset(¶m, 0, sizeof(param));
VOID_RET(int, sched_getparam(pid, NULL));
#endif
/* Exercise bad pid, ESRCH error */
(void)memset(¶m, 0, sizeof(param));
VOID_RET(int, sched_getparam(stress_get_unused_pid_racy(false), ¶m));
/* Exercise invalid sched_setparam syscall */
(void)memset(¶m, 0, sizeof(param));
VOID_RET(int, sched_setparam(-1, ¶m));
#if defined(__linux__)
/* Linux allows NULL param, will return EFAULT */
VOID_RET(int, sched_setparam(pid, NULL));
#endif
/*
* Exercise bad pid, ESRCH error only if process does not
* root or nice capability (to avoid clobbering processes we
* don't own
*/
if (!root_or_nice_capability) {
VOID_RET(int, sched_setparam(stress_get_unused_pid_racy(false), ¶m));
}
}
/* Exercise with invalid PID */
VOID_RET(int, sched_getscheduler(-1));
/* Exercise with bad pid, ESRCH error */
VOID_RET(int, sched_getscheduler(stress_get_unused_pid_racy(false)));
(void)memset(¶m, 0, sizeof(param));
ret = sched_getparam(pid, ¶m);
if ((ret < 0) && ((errno != EINVAL) && (errno != EPERM)))
pr_fail("%s: sched_getparam failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
ret = sched_setparam(pid, ¶m);
if ((ret < 0) && ((errno != EINVAL) && (errno != EPERM)))
pr_fail("%s: sched_setparam failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
#endif
#if defined(HAVE_SCHED_GETATTR) && \
defined(HAVE_SCHED_SETATTR)
/* Exercise too large attr > page size */
{
char large_attr[args->page_size + 16];
(void)memset(large_attr, 0, sizeof(large_attr));
VOID_RET(int, shim_sched_getattr(pid,
(struct shim_sched_attr *)large_attr,
(unsigned int)sizeof(large_attr), 0));
}
/* Exercise invalid sched_getattr syscalls */
(void)memset(&attr, 0, sizeof(attr));
VOID_RET(int, shim_sched_getattr(pid, &attr, sizeof(attr), ~0U));
/* Exercise -ve pid */
(void)memset(&attr, 0, sizeof(attr));
VOID_RET(int, shim_sched_getattr(-1, &attr, sizeof(attr), 0));
/* Exercise bad pid, ESRCH error */
(void)memset(&attr, 0, sizeof(attr));
VOID_RET(int, shim_sched_getattr(stress_get_unused_pid_racy(false),
&attr, sizeof(attr), 0));
/*
* Nothing too clever here, just get and set for now
*/
(void)memset(&attr, 0, sizeof(attr));
attr.size = sizeof(attr);
ret = shim_sched_getattr(pid, &attr, sizeof(attr), 0);
if (ret < 0) {
if (errno != ENOSYS) {
pr_fail("%s: sched_getattr failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
}
if (attr.sched_util_max != 0) {
/*
* Newer Linux kernels support a min/max setting,
* and if supported then find the min/max settings
*/
if (sched_util_min > attr.sched_util_min)
sched_util_min = attr.sched_util_min;
if (sched_util_max < attr.sched_util_max)
sched_util_max = attr.sched_util_max;
/* Sanity check */
if (sched_util_min > sched_util_max)
sched_util_min = sched_util_max;
/* Zero value means not initialized yet */
if (sched_util_max_value == 0)
sched_util_max_value = sched_util_max;
attr.sched_util_max = sched_util_max_value;
}
/*
* Exercise invalid sched_getattr syscalls, even if the
* syscalls succeed only correct value will be set,
* hence ignoring whether syscall succeeds or fails
*/
VOID_RET(int, shim_sched_setattr(pid, &attr, ~0U));
VOID_RET(int, shim_sched_setattr(-1, &attr, 0));
attr.size = sizeof(attr);
ret = shim_sched_setattr(pid, &attr, 0);
if (ret < 0) {
if (errno != ENOSYS) {
pr_fail("%s: sched_setattr failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
}
/*
* Cycle down max value to min value to
* exercise scheduler
*/
if ((counter++ > 256) &&
(sched_util_max_value > 0) &&
(sched_util_max_value > sched_util_min)) {
sched_util_max_value--;
counter = 0;
}
#else
UNEXPECTED
#endif
policy++;
policy %= SIZEOF_ARRAY(policies);
inc_counter(args);
} while (keep_stressing(args));
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
return EXIT_SUCCESS;
}
stressor_info_t stress_schedpolicy_info = {
.stressor = stress_schedpolicy,
.class = CLASS_INTERRUPT | CLASS_SCHEDULER | CLASS_OS,
.verify = VERIFY_ALWAYS,
.help = help
};
#else
stressor_info_t stress_schedpolicy_info = {
.stressor = stress_not_implemented,
.class = CLASS_INTERRUPT | CLASS_SCHEDULER | CLASS_OS,
.help = help
};
#endif