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
|
#include "stress-ng.h"
#include "core-attribute.h"
#include "core-builtin.h"
#include "core-sched.h"
#include <sched.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
const stress_sched_types_t stress_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_EXT)
{ SCHED_EXT, "sched_ext" },
#endif
#if defined(SCHED_RR)
{ SCHED_RR, "rr" },
#endif
};
const size_t stress_sched_types_length = SIZEOF_ARRAY(stress_sched_types);
const char * PURE stress_get_sched_name(const int sched)
{
size_t i;
for (i = 0; i < stress_sched_types_length; i++) {
if (stress_sched_types[i].sched == sched)
return stress_sched_types[i].sched_name;
}
return "unknown";
}
#if (defined(_POSIX_PRIORITY_SCHEDULING) || defined(__linux__)) && \
!defined(__OpenBSD__) && \
!defined(__minix__) && \
!defined(__APPLE__) && \
!defined(__HAIKU__) && \
!defined(__serenity__)
static const char prefix[] = "sched";
#if defined(SCHED_DEADLINE) && \
defined(__linux__)
#define HAVE_STRESS_SET_DEADLINE_SCHED (1)
#endif
#define HAVE_STRESS_SET_SCHED (1)
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)shim_memset(¶m, 0, sizeof(param));
switch (sched) {
case UNDEFINED:
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)shim_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) {
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;
#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)
int PURE 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
int32_t stress_get_opt_sched(const char *const str)
{
size_t i;
for (i = 0; i < stress_sched_types_length; i++) {
if (!strcmp(stress_sched_types[i].sched_name, str))
return stress_sched_types[i].sched;
}
if (strcmp("which", str))
(void)fprintf(stderr, "invalid sched option: %s\n", str);
if (stress_sched_types_length == (0)) {
(void)fprintf(stderr, "no scheduler options are available\n");
} else {
(void)fprintf(stderr, "available scheduler options are:");
for (i = 0; i < stress_sched_types_length; i++) {
(void)fprintf(stderr, " %s", stress_sched_types[i].sched_name);
}
(void)fprintf(stderr, "\n");
}
_exit(EXIT_FAILURE);
return 0;
}
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);
}
|