| 1 |
/** |
| 2 |
* SPDX-License-Identifier: EUPL-1.2 |
| 3 |
* |
| 4 |
* coresched.c - manage core scheduling cookies for tasks |
| 5 |
* |
| 6 |
* Copyright (C) 2024 Thijs Raymakers, Phil Auld |
| 7 |
* Licensed under the EUPL v1.2 |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <getopt.h> |
| 11 |
#include <stdbool.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <sys/prctl.h> |
| 14 |
#include <unistd.h> |
| 15 |
|
| 16 |
#include "c.h" |
| 17 |
#include "closestream.h" |
| 18 |
#include "nls.h" |
| 19 |
#include "strutils.h" |
| 20 |
|
| 21 |
// These definitions might not be defined in the header files, even if the |
| 22 |
// prctl interface in the kernel accepts them as valid. |
| 23 |
#ifndef PR_SCHED_CORE |
| 24 |
#define PR_SCHED_CORE 62 |
| 25 |
#endif |
| 26 |
#ifndef PR_SCHED_CORE_GET |
| 27 |
#define PR_SCHED_CORE_GET 0 |
| 28 |
#endif |
| 29 |
#ifndef PR_SCHED_CORE_CREATE |
| 30 |
#define PR_SCHED_CORE_CREATE 1 |
| 31 |
#endif |
| 32 |
#ifndef PR_SCHED_CORE_SHARE_TO |
| 33 |
#define PR_SCHED_CORE_SHARE_TO 2 |
| 34 |
#endif |
| 35 |
#ifndef PR_SCHED_CORE_SHARE_FROM |
| 36 |
#define PR_SCHED_CORE_SHARE_FROM 3 |
| 37 |
#endif |
| 38 |
#ifndef PR_SCHED_CORE_SCOPE_THREAD |
| 39 |
#define PR_SCHED_CORE_SCOPE_THREAD 0 |
| 40 |
#endif |
| 41 |
#ifndef PR_SCHED_CORE_SCOPE_THREAD_GROUP |
| 42 |
#define PR_SCHED_CORE_SCOPE_THREAD_GROUP 1 |
| 43 |
#endif |
| 44 |
#ifndef PR_SCHED_CORE_SCOPE_PROCESS_GROUP |
| 45 |
#define PR_SCHED_CORE_SCOPE_PROCESS_GROUP 2 |
| 46 |
#endif |
| 47 |
|
| 48 |
typedef int sched_core_scope; |
| 49 |
typedef unsigned long long sched_core_cookie; |
| 50 |
typedef enum { |
| 51 |
SCHED_CORE_CMD_GET, |
| 52 |
SCHED_CORE_CMD_NEW, |
| 53 |
SCHED_CORE_CMD_COPY, |
| 54 |
} sched_core_cmd; |
| 55 |
|
| 56 |
struct args { |
| 57 |
pid_t src; |
| 58 |
pid_t dest; |
| 59 |
sched_core_scope type; |
| 60 |
sched_core_cmd cmd; |
| 61 |
int exec_argv_offset; |
| 62 |
}; |
| 63 |
|
| 64 |
static bool sched_core_verbose = false; |
| 65 |
|
| 66 |
static void __attribute__((__noreturn__)) usage(void) |
| 67 |
{ |
| 68 |
fputs(USAGE_HEADER, stdout); |
| 69 |
fprintf(stdout, _(" %s [get] [--source <PID>]\n"), |
| 70 |
program_invocation_short_name); |
| 71 |
fprintf(stdout, _(" %s new [-t <TYPE>] --dest <PID>\n"), |
| 72 |
program_invocation_short_name); |
| 73 |
fprintf(stdout, _(" %s new [-t <TYPE>] -- PROGRAM [ARGS...]\n"), |
| 74 |
program_invocation_short_name); |
| 75 |
fprintf(stdout, |
| 76 |
_(" %s copy [--source <PID>] [-t <TYPE>] --dest <PID>\n"), |
| 77 |
program_invocation_short_name); |
| 78 |
fprintf(stdout, |
| 79 |
_(" %s copy [--source <PID>] [-t <TYPE>] -- PROGRAM [ARGS...]\n"), |
| 80 |
program_invocation_short_name); |
| 81 |
|
| 82 |
fputs(USAGE_SEPARATOR, stdout); |
| 83 |
fputsln(_("Manage core-scheduling cookies for tasks."), stdout); |
| 84 |
|
| 85 |
fputs(USAGE_FUNCTIONS, stdout); |
| 86 |
fputsln(_(" get retrieve the core-scheduling cookie of a PID"), stdout); |
| 87 |
fputsln(_(" new assign a new cookie to an existing PID,\n" |
| 88 |
" or execute a program with a new cookie"), stdout); |
| 89 |
fputsln(_(" copy copy the cookie from an existing PID to another PID,\n" |
| 90 |
" or execute a program with that copied cookie"), stdout); |
| 91 |
|
| 92 |
fputs(USAGE_OPTIONS, stdout); |
| 93 |
fprintf(stdout, |
| 94 |
_(" -s, --source <PID> which PID to get the cookie from; if this option\n" |
| 95 |
" is omitted, the PID of %s itself is used\n"), |
| 96 |
program_invocation_short_name); |
| 97 |
fputsln(_(" -d, --dest <PID> which PID to modify the cookie of"), stdout); |
| 98 |
fputsln(_(" -t, --dest-type <TYPE> type of the new or destination PID; can be one\n" |
| 99 |
" of: pid, tgid, pgid; default: tgid"), stdout); |
| 100 |
|
| 101 |
fputs(USAGE_SEPARATOR, stdout); |
| 102 |
fputsln(_(" -v, --verbose verbose"), stdout); |
| 103 |
fprintf(stdout, USAGE_HELP_OPTIONS(20)); |
| 104 |
fprintf(stdout, USAGE_MAN_TAIL("coresched(1)")); |
| 105 |
exit(EXIT_SUCCESS); |
| 106 |
} |
| 107 |
|
| 108 |
#define bad_usage(FMT...) \ |
| 109 |
do { \ |
| 110 |
warnx(FMT); \ |
| 111 |
errtryhelp(EXIT_FAILURE); \ |
| 112 |
} while (0) |
| 113 |
|
| 114 |
static sched_core_cookie core_sched_get_cookie(pid_t pid) |
| 115 |
{ |
| 116 |
sched_core_cookie cookie = 0; |
| 117 |
if (prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, pid, |
| 118 |
PR_SCHED_CORE_SCOPE_THREAD, &cookie)) |
| 119 |
err(EXIT_FAILURE, _("Failed to get cookie from PID %d"), pid); |
| 120 |
return cookie; |
| 121 |
} |
| 122 |
|
| 123 |
static void core_sched_create_cookie(pid_t pid, sched_core_scope type) |
| 124 |
{ |
| 125 |
if (prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, pid, type, 0)) |
| 126 |
err(EXIT_FAILURE, _("Failed to create cookie for PID %d"), pid); |
| 127 |
} |
| 128 |
|
| 129 |
static void core_sched_pull_cookie(pid_t from) |
| 130 |
{ |
| 131 |
if (prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_FROM, from, |
| 132 |
PR_SCHED_CORE_SCOPE_THREAD, 0)) |
| 133 |
err(EXIT_FAILURE, _("Failed to pull cookie from PID %d"), from); |
| 134 |
} |
| 135 |
|
| 136 |
static void core_sched_push_cookie(pid_t to, sched_core_scope type) |
| 137 |
{ |
| 138 |
if (prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, to, type, 0)) |
| 139 |
err(EXIT_FAILURE, _("Failed to push cookie to PID %d"), to); |
| 140 |
} |
| 141 |
|
| 142 |
static void core_sched_copy_cookie(pid_t from, pid_t to, |
| 143 |
sched_core_scope to_type) |
| 144 |
{ |
| 145 |
core_sched_pull_cookie(from); |
| 146 |
core_sched_push_cookie(to, to_type); |
| 147 |
|
| 148 |
if (sched_core_verbose) { |
| 149 |
sched_core_cookie before = core_sched_get_cookie(from); |
| 150 |
warnx(_("copied cookie 0x%llx from PID %d to PID %d"), before, |
| 151 |
from, to); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
static void core_sched_get_and_print_cookie(pid_t pid) |
| 156 |
{ |
| 157 |
if (sched_core_verbose) { |
| 158 |
sched_core_cookie after = core_sched_get_cookie(pid); |
| 159 |
warnx(_("set cookie of PID %d to 0x%llx"), pid, after); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
static void core_sched_exec_with_cookie(struct args *args, char **argv) |
| 164 |
{ |
| 165 |
// Move the argument list to the first argument of the program |
| 166 |
argv = &argv[args->exec_argv_offset]; |
| 167 |
|
| 168 |
// If a source PID is provided, try to copy the cookie from |
| 169 |
// that PID. Otherwise, create a brand new cookie with the |
| 170 |
// provided type. |
| 171 |
if (args->src) { |
| 172 |
core_sched_pull_cookie(args->src); |
| 173 |
core_sched_get_and_print_cookie(args->src); |
| 174 |
} else { |
| 175 |
pid_t pid = getpid(); |
| 176 |
core_sched_create_cookie(pid, args->type); |
| 177 |
core_sched_get_and_print_cookie(pid); |
| 178 |
} |
| 179 |
|
| 180 |
if (execvp(argv[0], argv)) |
| 181 |
errexec(argv[0]); |
| 182 |
} |
| 183 |
|
| 184 |
// There are two failure conditions for the core scheduling prctl calls |
| 185 |
// that rely on the environment in which coresched is running. |
| 186 |
// 1. If PR_SCHED_CORE is not recognized, or not supported on this system, |
| 187 |
// then prctl will set errno to EINVAL. Assuming all other operands of |
| 188 |
// prctl are valid, we can use errno==EINVAL as a check to see whether |
| 189 |
// core scheduling is available on this system. |
| 190 |
// 2. prctl sets errno to ENODEV if SMT is not available on this system, |
| 191 |
// either because SMT support has been disabled in the kernel, or because |
| 192 |
// the hardware doesn't support it. |
| 193 |
static bool is_core_sched_supported(void) |
| 194 |
{ |
| 195 |
sched_core_cookie cookie = 0; |
| 196 |
if (prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, getpid(), |
| 197 |
PR_SCHED_CORE_SCOPE_THREAD, &cookie)) |
| 198 |
if (errno == EINVAL || errno == ENODEV) |
| 199 |
return false; |
| 200 |
|
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
static sched_core_scope parse_core_sched_type(char *str) |
| 205 |
{ |
| 206 |
if (!strcmp(str, "pid")) |
| 207 |
return PR_SCHED_CORE_SCOPE_THREAD; |
| 208 |
else if (!strcmp(str, "tgid")) |
| 209 |
return PR_SCHED_CORE_SCOPE_THREAD_GROUP; |
| 210 |
else if (!strcmp(str, "pgid")) |
| 211 |
return PR_SCHED_CORE_SCOPE_PROCESS_GROUP; |
| 212 |
|
| 213 |
bad_usage(_("'%s' is an invalid option. Must be one of pid/tgid/pgid"), |
| 214 |
str); |
| 215 |
} |
| 216 |
|
| 217 |
static void parse_and_verify_arguments(int argc, char **argv, struct args *args) |
| 218 |
{ |
| 219 |
int c; |
| 220 |
|
| 221 |
static const struct option longopts[] = { |
| 222 |
{ "source", required_argument, NULL, 's' }, |
| 223 |
{ "dest", required_argument, NULL, 'd' }, |
| 224 |
{ "dest-type", required_argument, NULL, 't' }, |
| 225 |
{ "verbose", no_argument, NULL, 'v' }, |
| 226 |
{ "version", no_argument, NULL, 'V' }, |
| 227 |
{ "help", no_argument, NULL, 'h' }, |
| 228 |
{ NULL, 0, NULL, 0 } |
| 229 |
}; |
| 230 |
|
| 231 |
while ((c = getopt_long(argc, argv, "s:d:t:vVh", longopts, NULL)) != -1) |
| 232 |
switch (c) { |
| 233 |
case 's': |
| 234 |
args->src = strtopid_or_err( |
| 235 |
optarg, _("invalid PID for -s/--source")); |
| 236 |
break; |
| 237 |
case 'd': |
| 238 |
args->dest = strtopid_or_err( |
| 239 |
optarg, _("invalid PID for -d/--dest")); |
| 240 |
break; |
| 241 |
case 't': |
| 242 |
args->type = parse_core_sched_type(optarg); |
| 243 |
break; |
| 244 |
case 'v': |
| 245 |
sched_core_verbose = true; |
| 246 |
break; |
| 247 |
case 'V': |
| 248 |
print_version(EXIT_SUCCESS); |
| 249 |
case 'h': |
| 250 |
usage(); |
| 251 |
default: |
| 252 |
errtryhelp(EXIT_FAILURE); |
| 253 |
} |
| 254 |
|
| 255 |
if (argc <= optind) { |
| 256 |
args->cmd = SCHED_CORE_CMD_GET; |
| 257 |
} else { |
| 258 |
if (!strcmp(argv[optind], "get")) |
| 259 |
args->cmd = SCHED_CORE_CMD_GET; |
| 260 |
else if (!strcmp(argv[optind], "new")) |
| 261 |
args->cmd = SCHED_CORE_CMD_NEW; |
| 262 |
else if (!strcmp(argv[optind], "copy")) |
| 263 |
args->cmd = SCHED_CORE_CMD_COPY; |
| 264 |
else |
| 265 |
bad_usage(_("Unknown function")); |
| 266 |
|
| 267 |
// Since we parsed an extra "option" outside of getopt_long, we have to |
| 268 |
// increment optind manually. |
| 269 |
++optind; |
| 270 |
} |
| 271 |
|
| 272 |
if (args->cmd == SCHED_CORE_CMD_GET && args->dest) |
| 273 |
bad_usage(_("get does not accept the --dest option")); |
| 274 |
|
| 275 |
if (args->cmd == SCHED_CORE_CMD_NEW && args->src) |
| 276 |
bad_usage(_("new does not accept the --source option")); |
| 277 |
|
| 278 |
// If the -s/--source option is not specified, it defaults to the PID |
| 279 |
// of the current coresched process |
| 280 |
if (args->cmd != SCHED_CORE_CMD_NEW && !args->src) |
| 281 |
args->src = getpid(); |
| 282 |
|
| 283 |
// More arguments have been passed, which means that the user wants to run |
| 284 |
// another program with a core scheduling cookie. |
| 285 |
if (argc > optind) { |
| 286 |
switch (args->cmd) { |
| 287 |
case SCHED_CORE_CMD_GET: |
| 288 |
bad_usage(_("bad usage of the get function")); |
| 289 |
break; |
| 290 |
case SCHED_CORE_CMD_NEW: |
| 291 |
if (args->dest) |
| 292 |
bad_usage(_( |
| 293 |
"new requires either a -d/--dest or a command")); |
| 294 |
else |
| 295 |
args->exec_argv_offset = optind; |
| 296 |
break; |
| 297 |
case SCHED_CORE_CMD_COPY: |
| 298 |
if (args->dest) |
| 299 |
bad_usage(_( |
| 300 |
"copy requires either a -d/--dest or a command")); |
| 301 |
else |
| 302 |
args->exec_argv_offset = optind; |
| 303 |
break; |
| 304 |
} |
| 305 |
} else { |
| 306 |
if (args->cmd == SCHED_CORE_CMD_NEW && !args->dest) |
| 307 |
bad_usage(_( |
| 308 |
"new requires either a -d/--dest or a command")); |
| 309 |
if (args->cmd == SCHED_CORE_CMD_COPY && !args->dest) |
| 310 |
bad_usage(_( |
| 311 |
"copy requires either a -d/--dest or a command")); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
int main(int argc, char **argv) |
| 316 |
{ |
| 317 |
struct args args = { .type = PR_SCHED_CORE_SCOPE_THREAD_GROUP }; |
| 318 |
|
| 319 |
setlocale(LC_ALL, ""); |
| 320 |
bindtextdomain(PACKAGE, LOCALEDIR); |
| 321 |
textdomain(PACKAGE); |
| 322 |
close_stdout_atexit(); |
| 323 |
|
| 324 |
parse_and_verify_arguments(argc, argv, &args); |
| 325 |
|
| 326 |
if (!is_core_sched_supported()) |
| 327 |
errx(EXIT_FAILURE, |
| 328 |
_("Core scheduling is not supported on this system. Either SMT " |
| 329 |
"is unavailable or your kernel does not support CONFIG_SCHED_CORE.")); |
| 330 |
|
| 331 |
sched_core_cookie cookie; |
| 332 |
|
| 333 |
switch (args.cmd) { |
| 334 |
case SCHED_CORE_CMD_GET: |
| 335 |
cookie = core_sched_get_cookie(args.src); |
| 336 |
printf(_("cookie of PID %d is 0x%llx\n"), args.src, cookie); |
| 337 |
break; |
| 338 |
case SCHED_CORE_CMD_NEW: |
| 339 |
if (args.exec_argv_offset) { |
| 340 |
core_sched_exec_with_cookie(&args, argv); |
| 341 |
} else { |
| 342 |
core_sched_create_cookie(args.dest, args.type); |
| 343 |
core_sched_get_and_print_cookie(args.dest); |
| 344 |
} |
| 345 |
break; |
| 346 |
case SCHED_CORE_CMD_COPY: |
| 347 |
if (args.exec_argv_offset) |
| 348 |
core_sched_exec_with_cookie(&args, argv); |
| 349 |
else |
| 350 |
core_sched_copy_cookie(args.src, args.dest, args.type); |
| 351 |
break; |
| 352 |
default: |
| 353 |
usage(); |
| 354 |
} |
| 355 |
} |