| 1 |
/* |
| 2 |
* chrt.c - manipulate a task's real-time attributes |
| 3 |
* |
| 4 |
* 27-Apr-2002: initial version - Robert Love <[email protected]> |
| 5 |
* 04-May-2011: make it thread-aware - Davidlohr Bueso <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License, version 2, as |
| 9 |
* published by the Free Software Foundation |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program. If not, see <https://gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Copyright (C) 2004 Robert Love |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <sched.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <getopt.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <stdbool.h> |
| 29 |
#include <sys/time.h> |
| 30 |
#include <sys/resource.h> |
| 31 |
|
| 32 |
#include "c.h" |
| 33 |
#include "nls.h" |
| 34 |
#include "closestream.h" |
| 35 |
#include "strutils.h" |
| 36 |
#include "procfs.h" |
| 37 |
#include "sched_attr.h" |
| 38 |
|
| 39 |
|
| 40 |
/* control struct */ |
| 41 |
struct chrt_ctl { |
| 42 |
pid_t pid; |
| 43 |
int policy; /* SCHED_* */ |
| 44 |
int priority; |
| 45 |
|
| 46 |
uint64_t runtime; /* --sched-* options */ |
| 47 |
uint64_t deadline; |
| 48 |
uint64_t period; |
| 49 |
|
| 50 |
unsigned int all_tasks : 1, /* all threads of the PID */ |
| 51 |
reset_on_fork : 1, /* SCHED_RESET_ON_FORK or SCHED_FLAG_RESET_ON_FORK */ |
| 52 |
altered : 1, /* sched_set**() used */ |
| 53 |
verbose : 1; /* verbose output */ |
| 54 |
}; |
| 55 |
|
| 56 |
static void __attribute__((__noreturn__)) usage(void) |
| 57 |
{ |
| 58 |
FILE *out = stdout; |
| 59 |
|
| 60 |
fputs(_("Show or change the real-time scheduling attributes of a process.\n"), out); |
| 61 |
fputs(USAGE_SEPARATOR, out); |
| 62 |
fputs(_("Set policy:\n" |
| 63 |
" chrt [options] [<priority>] <command> [<argument>...]\n" |
| 64 |
" chrt --pid <policy-option> [options] [<priority>] <PID>\n"), out); |
| 65 |
fputs(USAGE_SEPARATOR, out); |
| 66 |
fputs(_("Get policy:\n" |
| 67 |
" chrt --pid <PID>\n"), out); |
| 68 |
|
| 69 |
fputs(USAGE_SEPARATOR, out); |
| 70 |
fputs(_("Policy options:\n"), out); |
| 71 |
fputs(_(" -b, --batch set policy to SCHED_BATCH\n"), out); |
| 72 |
fputs(_(" -d, --deadline set policy to SCHED_DEADLINE\n"), out); |
| 73 |
fputs(_(" -e, --ext set policy to SCHED_EXT\n"), out); |
| 74 |
fputs(_(" -f, --fifo set policy to SCHED_FIFO\n"), out); |
| 75 |
fputs(_(" -i, --idle set policy to SCHED_IDLE\n"), out); |
| 76 |
fputs(_(" -o, --other set policy to SCHED_OTHER\n"), out); |
| 77 |
fputs(_(" -r, --rr set policy to SCHED_RR (default)\n"), out); |
| 78 |
|
| 79 |
fputs(USAGE_SEPARATOR, out); |
| 80 |
fputs(_("Scheduling options:\n"), out); |
| 81 |
fputs(_(" -R, --reset-on-fork set reset-on-fork flag\n"), out); |
| 82 |
fputs(_(" -T, --sched-runtime <ns> runtime parameter for DEADLINE\n"), out); |
| 83 |
fputs(_(" -P, --sched-period <ns> period parameter for DEADLINE\n"), out); |
| 84 |
fputs(_(" -D, --sched-deadline <ns> deadline parameter for DEADLINE\n"), out); |
| 85 |
|
| 86 |
fputs(USAGE_SEPARATOR, out); |
| 87 |
fputs(_("Other options:\n"), out); |
| 88 |
fputs(_(" -a, --all-tasks operate on all the tasks (threads) for a given pid\n"), out); |
| 89 |
fputs(_(" -m, --max show min and max valid priorities\n"), out); |
| 90 |
fputs(_(" -p, --pid operate on existing given pid\n"), out); |
| 91 |
fputs(_(" -v, --verbose display status information\n"), out); |
| 92 |
|
| 93 |
fputs(USAGE_SEPARATOR, out); |
| 94 |
fprintf(out, USAGE_HELP_OPTIONS(22)); |
| 95 |
|
| 96 |
fprintf(out, USAGE_MAN_TAIL("chrt(1)")); |
| 97 |
exit(EXIT_SUCCESS); |
| 98 |
} |
| 99 |
|
| 100 |
static const char *get_policy_name(int policy) |
| 101 |
{ |
| 102 |
#ifdef SCHED_RESET_ON_FORK |
| 103 |
policy &= ~SCHED_RESET_ON_FORK; |
| 104 |
#endif |
| 105 |
switch (policy) { |
| 106 |
case SCHED_OTHER: |
| 107 |
return "SCHED_OTHER"; |
| 108 |
case SCHED_FIFO: |
| 109 |
return "SCHED_FIFO"; |
| 110 |
#ifdef SCHED_IDLE |
| 111 |
case SCHED_IDLE: |
| 112 |
return "SCHED_IDLE"; |
| 113 |
#endif |
| 114 |
case SCHED_RR: |
| 115 |
return "SCHED_RR"; |
| 116 |
#ifdef SCHED_BATCH |
| 117 |
case SCHED_BATCH: |
| 118 |
return "SCHED_BATCH"; |
| 119 |
#endif |
| 120 |
#ifdef SCHED_DEADLINE |
| 121 |
case SCHED_DEADLINE: |
| 122 |
return "SCHED_DEADLINE"; |
| 123 |
#endif |
| 124 |
#ifdef SCHED_EXT |
| 125 |
case SCHED_EXT: |
| 126 |
return "SCHED_EXT"; |
| 127 |
#endif |
| 128 |
default: |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
return _("unknown"); |
| 133 |
} |
| 134 |
|
| 135 |
static bool supports_custom_slice(int policy) |
| 136 |
{ |
| 137 |
#ifdef SCHED_BATCH |
| 138 |
if (policy == SCHED_BATCH) |
| 139 |
return true; |
| 140 |
#endif |
| 141 |
return policy == SCHED_OTHER; |
| 142 |
} |
| 143 |
|
| 144 |
static bool supports_runtime_param(int policy) |
| 145 |
{ |
| 146 |
#ifdef SCHED_DEADLINE |
| 147 |
if (policy == SCHED_DEADLINE) |
| 148 |
return true; |
| 149 |
#endif |
| 150 |
return supports_custom_slice(policy); |
| 151 |
} |
| 152 |
|
| 153 |
static const char *get_supported_runtime_param_policies(void) |
| 154 |
{ |
| 155 |
#if defined(SCHED_BATCH) |
| 156 |
#if defined(SCHED_DEADLINE) |
| 157 |
return _("SCHED_OTHER, SCHED_BATCH and SCHED_DEADLINE"); |
| 158 |
#else |
| 159 |
return _("SCHED_OTHER and SCHED_BATCH"); |
| 160 |
#endif /* SCHED_DEADLINE */ |
| 161 |
#elif defined(SCHED_DEADLINE) |
| 162 |
return _("SCHED_OTHER and SCHED_DEADLINE"); |
| 163 |
#else |
| 164 |
return _("SCHED_OTHER"); |
| 165 |
#endif |
| 166 |
} |
| 167 |
|
| 168 |
static void show_sched_pid_info(struct chrt_ctl *ctl, pid_t pid) |
| 169 |
{ |
| 170 |
int policy = -1, reset_on_fork = 0, prio = 0; |
| 171 |
uint64_t runtime = 0; |
| 172 |
#ifdef SCHED_DEADLINE |
| 173 |
uint64_t deadline = 0, period = 0; |
| 174 |
#endif |
| 175 |
|
| 176 |
/* don't display "pid 0" as that is confusing */ |
| 177 |
if (!pid) |
| 178 |
pid = getpid(); |
| 179 |
|
| 180 |
errno = 0; |
| 181 |
|
| 182 |
/* |
| 183 |
* New way |
| 184 |
*/ |
| 185 |
#ifdef HAVE_SCHED_SETATTR |
| 186 |
{ |
| 187 |
struct sched_attr sa; |
| 188 |
|
| 189 |
if (sched_getattr(pid, &sa, sizeof(sa), 0) != 0) { |
| 190 |
if (errno == ENOSYS) |
| 191 |
goto fallback; |
| 192 |
err(EXIT_FAILURE, _("failed to get pid %d's policy"), pid); |
| 193 |
} |
| 194 |
|
| 195 |
policy = sa.sched_policy; |
| 196 |
prio = sa.sched_priority; |
| 197 |
reset_on_fork = sa.sched_flags & SCHED_FLAG_RESET_ON_FORK; |
| 198 |
runtime = sa.sched_runtime; |
| 199 |
#ifdef SCHED_DEADLINE |
| 200 |
deadline = sa.sched_deadline; |
| 201 |
period = sa.sched_period; |
| 202 |
#endif |
| 203 |
} |
| 204 |
|
| 205 |
/* |
| 206 |
* Old way |
| 207 |
*/ |
| 208 |
fallback: |
| 209 |
if (errno == ENOSYS) |
| 210 |
#endif |
| 211 |
{ |
| 212 |
struct sched_param sp; |
| 213 |
|
| 214 |
policy = sched_getscheduler(pid); |
| 215 |
if (policy == -1) |
| 216 |
err(EXIT_FAILURE, _("failed to get pid %d's policy"), pid); |
| 217 |
|
| 218 |
if (sched_getparam(pid, &sp) != 0) |
| 219 |
err(EXIT_FAILURE, _("failed to get pid %d's attributes"), pid); |
| 220 |
else |
| 221 |
prio = sp.sched_priority; |
| 222 |
# ifdef SCHED_RESET_ON_FORK |
| 223 |
if (policy & SCHED_RESET_ON_FORK) |
| 224 |
reset_on_fork = 1; |
| 225 |
# endif |
| 226 |
} |
| 227 |
|
| 228 |
if (ctl->altered) |
| 229 |
printf(_("pid %d's new scheduling policy: %s"), pid, get_policy_name(policy)); |
| 230 |
else |
| 231 |
printf(_("pid %d's current scheduling policy: %s"), pid, get_policy_name(policy)); |
| 232 |
|
| 233 |
if (reset_on_fork) |
| 234 |
printf("|SCHED_RESET_ON_FORK"); |
| 235 |
putchar('\n'); |
| 236 |
|
| 237 |
if (ctl->altered) |
| 238 |
printf(_("pid %d's new scheduling priority: %d\n"), pid, prio); |
| 239 |
else |
| 240 |
printf(_("pid %d's current scheduling priority: %d\n"), pid, prio); |
| 241 |
|
| 242 |
if (runtime && supports_custom_slice(policy)) { |
| 243 |
if (ctl->altered) |
| 244 |
printf(_("pid %d's new runtime parameter: %ju\n"), pid, (uintmax_t)runtime); |
| 245 |
else |
| 246 |
printf(_("pid %d's current runtime parameter: %ju\n"), pid, (uintmax_t)runtime); |
| 247 |
} |
| 248 |
|
| 249 |
#ifdef SCHED_DEADLINE |
| 250 |
if (policy == SCHED_DEADLINE) { |
| 251 |
if (ctl->altered) |
| 252 |
printf(_("pid %d's new runtime/deadline/period parameters: %ju/%ju/%ju\n"), |
| 253 |
pid, (uintmax_t)runtime, (uintmax_t)deadline, (uintmax_t)period); |
| 254 |
else |
| 255 |
printf(_("pid %d's current runtime/deadline/period parameters: %ju/%ju/%ju\n"), |
| 256 |
pid, (uintmax_t)runtime, (uintmax_t)deadline, (uintmax_t)period); |
| 257 |
} |
| 258 |
#endif |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
static void show_sched_info(struct chrt_ctl *ctl) |
| 263 |
{ |
| 264 |
if (ctl->all_tasks) { |
| 265 |
#ifdef __linux__ |
| 266 |
DIR *sub = NULL; |
| 267 |
pid_t tid; |
| 268 |
struct path_cxt *pc = ul_new_procfs_path(ctl->pid, NULL); |
| 269 |
|
| 270 |
while (pc && procfs_process_next_tid(pc, &sub, &tid) == 0) |
| 271 |
show_sched_pid_info(ctl, tid); |
| 272 |
|
| 273 |
ul_unref_path(pc); |
| 274 |
#else |
| 275 |
err(EXIT_FAILURE, _("cannot obtain the list of tasks")); |
| 276 |
#endif |
| 277 |
} else |
| 278 |
show_sched_pid_info(ctl, ctl->pid); |
| 279 |
} |
| 280 |
|
| 281 |
static void show_min_max(void) |
| 282 |
{ |
| 283 |
unsigned long i; |
| 284 |
int policies[] = { |
| 285 |
SCHED_OTHER, |
| 286 |
SCHED_FIFO, |
| 287 |
SCHED_RR, |
| 288 |
#ifdef SCHED_BATCH |
| 289 |
SCHED_BATCH, |
| 290 |
#endif |
| 291 |
#ifdef SCHED_IDLE |
| 292 |
SCHED_IDLE, |
| 293 |
#endif |
| 294 |
#ifdef SCHED_DEADLINE |
| 295 |
SCHED_DEADLINE, |
| 296 |
#endif |
| 297 |
#ifdef SCHED_EXT |
| 298 |
SCHED_EXT, |
| 299 |
#endif |
| 300 |
}; |
| 301 |
|
| 302 |
for (i = 0; i < ARRAY_SIZE(policies); i++) { |
| 303 |
int plc = policies[i]; |
| 304 |
int max = sched_get_priority_max(plc); |
| 305 |
int min = sched_get_priority_min(plc); |
| 306 |
|
| 307 |
if (max >= 0 && min >= 0) |
| 308 |
printf(_("%s min/max priority\t: %d/%d\n"), |
| 309 |
get_policy_name(plc), min, max); |
| 310 |
else |
| 311 |
printf(_("%s not supported?\n"), get_policy_name(plc)); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
static int set_sched_one_by_setscheduler(struct chrt_ctl *ctl, pid_t pid) |
| 316 |
{ |
| 317 |
struct sched_param sp = { .sched_priority = ctl->priority }; |
| 318 |
int policy = ctl->policy; |
| 319 |
|
| 320 |
errno = 0; |
| 321 |
# ifdef SCHED_RESET_ON_FORK |
| 322 |
if (ctl->reset_on_fork) |
| 323 |
policy |= SCHED_RESET_ON_FORK; |
| 324 |
# endif |
| 325 |
|
| 326 |
#if defined (__linux__) && defined(SYS_sched_setscheduler) |
| 327 |
/* musl libc returns ENOSYS for its sched_setscheduler library |
| 328 |
* function, because the sched_setscheduler Linux kernel system call |
| 329 |
* does not conform to Posix; so we use the system call directly |
| 330 |
*/ |
| 331 |
return syscall(SYS_sched_setscheduler, pid, policy, &sp); |
| 332 |
#else |
| 333 |
return sched_setscheduler(pid, policy, &sp); |
| 334 |
#endif |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
#ifndef HAVE_SCHED_SETATTR |
| 339 |
static int set_sched_one(struct chrt_ctl *ctl, pid_t pid) |
| 340 |
{ |
| 341 |
return set_sched_one_by_setscheduler(ctl, pid); |
| 342 |
} |
| 343 |
|
| 344 |
#else /* HAVE_SCHED_SETATTR */ |
| 345 |
static int set_sched_one(struct chrt_ctl *ctl, pid_t pid) |
| 346 |
{ |
| 347 |
struct sched_attr sa = { .size = sizeof(struct sched_attr) }; |
| 348 |
|
| 349 |
/* old API is good enough for non-deadline */ |
| 350 |
if (!supports_runtime_param(ctl->policy)) |
| 351 |
return set_sched_one_by_setscheduler(ctl, pid); |
| 352 |
|
| 353 |
/* not changed by chrt, follow the current setting */ |
| 354 |
sa.sched_nice = getpriority(PRIO_PROCESS, pid); |
| 355 |
|
| 356 |
/* use main() to check if the setting makes sense */ |
| 357 |
sa.sched_policy = ctl->policy; |
| 358 |
sa.sched_priority = ctl->priority; |
| 359 |
sa.sched_runtime = ctl->runtime; |
| 360 |
sa.sched_period = ctl->period; |
| 361 |
sa.sched_deadline = ctl->deadline; |
| 362 |
|
| 363 |
# ifdef SCHED_FLAG_RESET_ON_FORK |
| 364 |
/* Don't use SCHED_RESET_ON_FORK for sched_setattr()! */ |
| 365 |
if (ctl->reset_on_fork) |
| 366 |
sa.sched_flags |= SCHED_FLAG_RESET_ON_FORK; |
| 367 |
# endif |
| 368 |
errno = 0; |
| 369 |
return sched_setattr(pid, &sa, 0); |
| 370 |
} |
| 371 |
#endif /* HAVE_SCHED_SETATTR */ |
| 372 |
|
| 373 |
static void set_sched(struct chrt_ctl *ctl) |
| 374 |
{ |
| 375 |
if (ctl->all_tasks) { |
| 376 |
#ifdef __linux__ |
| 377 |
DIR *sub = NULL; |
| 378 |
pid_t tid; |
| 379 |
struct path_cxt *pc = ul_new_procfs_path(ctl->pid, NULL); |
| 380 |
|
| 381 |
if (!pc) |
| 382 |
err(EXIT_FAILURE, _("cannot obtain the list of tasks")); |
| 383 |
|
| 384 |
while (procfs_process_next_tid(pc, &sub, &tid) == 0) { |
| 385 |
if (set_sched_one(ctl, tid) == -1) |
| 386 |
err(EXIT_FAILURE, _("failed to set tid %d's policy"), tid); |
| 387 |
} |
| 388 |
ul_unref_path(pc); |
| 389 |
#else |
| 390 |
err(EXIT_FAILURE, _("cannot obtain the list of tasks")); |
| 391 |
#endif |
| 392 |
} else if (set_sched_one(ctl, ctl->pid) == -1) |
| 393 |
err(EXIT_FAILURE, _("failed to set pid %d's policy"), ctl->pid); |
| 394 |
|
| 395 |
ctl->altered = 1; |
| 396 |
} |
| 397 |
|
| 398 |
int main(int argc, char **argv) |
| 399 |
{ |
| 400 |
struct chrt_ctl _ctl = { .pid = -1, .policy = SCHED_RR }, *ctl = &_ctl; |
| 401 |
int c; |
| 402 |
bool policy_given = false, need_prio = false; |
| 403 |
|
| 404 |
static const struct option longopts[] = { |
| 405 |
{ "all-tasks", no_argument, NULL, 'a' }, |
| 406 |
{ "batch", no_argument, NULL, 'b' }, |
| 407 |
{ "deadline", no_argument, NULL, 'd' }, |
| 408 |
{ "ext", no_argument, NULL, 'e' }, |
| 409 |
{ "fifo", no_argument, NULL, 'f' }, |
| 410 |
{ "idle", no_argument, NULL, 'i' }, |
| 411 |
{ "pid", no_argument, NULL, 'p' }, |
| 412 |
{ "help", no_argument, NULL, 'h' }, |
| 413 |
{ "max", no_argument, NULL, 'm' }, |
| 414 |
{ "other", no_argument, NULL, 'o' }, |
| 415 |
{ "rr", no_argument, NULL, 'r' }, |
| 416 |
{ "sched-runtime", required_argument, NULL, 'T' }, |
| 417 |
{ "sched-period", required_argument, NULL, 'P' }, |
| 418 |
{ "sched-deadline", required_argument, NULL, 'D' }, |
| 419 |
{ "reset-on-fork", no_argument, NULL, 'R' }, |
| 420 |
{ "verbose", no_argument, NULL, 'v' }, |
| 421 |
{ "version", no_argument, NULL, 'V' }, |
| 422 |
{ NULL, no_argument, NULL, 0 } |
| 423 |
}; |
| 424 |
|
| 425 |
setlocale(LC_ALL, ""); |
| 426 |
bindtextdomain(PACKAGE, LOCALEDIR); |
| 427 |
textdomain(PACKAGE); |
| 428 |
close_stdout_atexit(); |
| 429 |
|
| 430 |
while((c = getopt_long(argc, argv, "+abdD:efiphmoP:T:rRvV", longopts, NULL)) != -1) |
| 431 |
{ |
| 432 |
switch (c) { |
| 433 |
case 'a': |
| 434 |
ctl->all_tasks = 1; |
| 435 |
break; |
| 436 |
case 'b': |
| 437 |
#ifdef SCHED_BATCH |
| 438 |
ctl->policy = SCHED_BATCH; |
| 439 |
policy_given = true; |
| 440 |
#endif |
| 441 |
break; |
| 442 |
|
| 443 |
case 'd': |
| 444 |
#ifdef SCHED_DEADLINE |
| 445 |
ctl->policy = SCHED_DEADLINE; |
| 446 |
policy_given = true; |
| 447 |
#endif |
| 448 |
break; |
| 449 |
case 'e': |
| 450 |
#ifdef SCHED_EXT |
| 451 |
ctl->policy = SCHED_EXT; |
| 452 |
policy_given = true; |
| 453 |
#endif |
| 454 |
break; |
| 455 |
case 'f': |
| 456 |
ctl->policy = SCHED_FIFO; |
| 457 |
policy_given = true; |
| 458 |
need_prio = true; |
| 459 |
break; |
| 460 |
case 'R': |
| 461 |
ctl->reset_on_fork = 1; |
| 462 |
break; |
| 463 |
case 'i': |
| 464 |
#ifdef SCHED_IDLE |
| 465 |
ctl->policy = SCHED_IDLE; |
| 466 |
policy_given = true; |
| 467 |
#endif |
| 468 |
break; |
| 469 |
case 'm': |
| 470 |
show_min_max(); |
| 471 |
return EXIT_SUCCESS; |
| 472 |
case 'o': |
| 473 |
ctl->policy = SCHED_OTHER; |
| 474 |
policy_given = true; |
| 475 |
break; |
| 476 |
case 'p': |
| 477 |
ctl->pid = 0; /* indicate that a PID is expected */ |
| 478 |
break; |
| 479 |
case 'r': |
| 480 |
ctl->policy = SCHED_RR; |
| 481 |
policy_given = true; |
| 482 |
need_prio = true; |
| 483 |
break; |
| 484 |
case 'v': |
| 485 |
ctl->verbose = 1; |
| 486 |
break; |
| 487 |
case 'T': |
| 488 |
ctl->runtime = strtou64_or_err(optarg, _("invalid runtime argument")); |
| 489 |
break; |
| 490 |
case 'P': |
| 491 |
ctl->period = strtou64_or_err(optarg, _("invalid period argument")); |
| 492 |
break; |
| 493 |
case 'D': |
| 494 |
ctl->deadline = strtou64_or_err(optarg, _("invalid deadline argument")); |
| 495 |
break; |
| 496 |
|
| 497 |
case 'V': |
| 498 |
print_version(EXIT_SUCCESS); |
| 499 |
case 'h': |
| 500 |
usage(); |
| 501 |
default: |
| 502 |
errtryhelp(EXIT_FAILURE); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
if (argc - optind < 1) { |
| 507 |
warnx(_("too few arguments")); |
| 508 |
errtryhelp(EXIT_FAILURE); |
| 509 |
} |
| 510 |
|
| 511 |
/* If option --pid was given, parse the very last argument as a PID. */ |
| 512 |
if (ctl->pid == 0) { |
| 513 |
errno = 0; |
| 514 |
/* strtopid_or_err() is not suitable here, as 0 can be passed. */ |
| 515 |
ctl->pid = strtos32_or_err(argv[argc - 1], _("invalid PID argument")); |
| 516 |
|
| 517 |
/* If no policy nor priority was given, show current settings. */ |
| 518 |
if (!policy_given && argc - optind == 1) { |
| 519 |
show_sched_info(ctl); |
| 520 |
return EXIT_SUCCESS; |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
if (ctl->policy == SCHED_RR) |
| 525 |
need_prio = true; |
| 526 |
|
| 527 |
if (ctl->pid > -1 && ctl->verbose) |
| 528 |
show_sched_info(ctl); |
| 529 |
|
| 530 |
if (argc - optind > 1 && (ctl->pid > -1 || isdigit_string(argv[optind]))) { |
| 531 |
errno = 0; |
| 532 |
ctl->priority = strtos32_or_err(argv[optind], _("invalid priority argument")); |
| 533 |
|
| 534 |
optind++; |
| 535 |
} else if (need_prio && ctl->pid == -1 && isdigit_string(argv[optind])) |
| 536 |
errx(EXIT_FAILURE, _("no command or priority specified")); |
| 537 |
else if (need_prio) |
| 538 |
errx(EXIT_FAILURE, _("policy %s requires a priority argument"), |
| 539 |
get_policy_name(ctl->policy)); |
| 540 |
else |
| 541 |
ctl->priority = 0; |
| 542 |
|
| 543 |
if (ctl->runtime && !supports_runtime_param(ctl->policy)) |
| 544 |
errx(EXIT_FAILURE, _("--sched-runtime option is supported for %s"), |
| 545 |
get_supported_runtime_param_policies()); |
| 546 |
#ifdef SCHED_DEADLINE |
| 547 |
if ((ctl->deadline || ctl->period) && ctl->policy != SCHED_DEADLINE) |
| 548 |
errx(EXIT_FAILURE, _("--sched-{deadline,period} options are " |
| 549 |
"supported for SCHED_DEADLINE only")); |
| 550 |
if (ctl->policy == SCHED_DEADLINE) { |
| 551 |
/* The basic rule is runtime <= deadline <= period, so we can |
| 552 |
* make deadline and runtime optional on command line. Note we |
| 553 |
* don't check any values or set any defaults; it's kernel's |
| 554 |
* responsibility. |
| 555 |
*/ |
| 556 |
if (ctl->deadline == 0) |
| 557 |
ctl->deadline = ctl->period; |
| 558 |
if (ctl->runtime == 0) |
| 559 |
ctl->runtime = ctl->deadline; |
| 560 |
} |
| 561 |
#else |
| 562 |
if (ctl->deadline || ctl->period) |
| 563 |
errx(EXIT_FAILURE, _("SCHED_DEADLINE is unsupported")); |
| 564 |
#endif |
| 565 |
if (ctl->pid == -1) |
| 566 |
ctl->pid = 0; |
| 567 |
if (ctl->priority < sched_get_priority_min(ctl->policy) || |
| 568 |
sched_get_priority_max(ctl->policy) < ctl->priority) |
| 569 |
errx(EXIT_FAILURE, |
| 570 |
_("unsupported priority value for the policy: %d: see --max for valid range"), |
| 571 |
ctl->priority); |
| 572 |
set_sched(ctl); |
| 573 |
|
| 574 |
if (ctl->verbose) |
| 575 |
show_sched_info(ctl); |
| 576 |
|
| 577 |
if (!ctl->pid) { |
| 578 |
argv += optind; |
| 579 |
|
| 580 |
if (argv[0] && strcmp(argv[0], "--") == 0) |
| 581 |
argv++; |
| 582 |
|
| 583 |
if (!argv[0]) |
| 584 |
errx(EXIT_FAILURE, _("no command specified")); |
| 585 |
|
| 586 |
execvp(argv[0], argv); |
| 587 |
errexec(argv[0]); |
| 588 |
} |
| 589 |
|
| 590 |
return EXIT_SUCCESS; |
| 591 |
} |