1 // SPDX-License-Identifier: GPL-2.0-only
3 * Use the core scheduling prctl() to test core scheduling cookies control.
5 * Copyright (c) 2021 Oracle and/or its affiliates.
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of version 2.1 of the GNU Lesser General Public License as
11 * published by the Free Software Foundation.
13 * This library is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, see <http://www.gnu.org/licenses>.
23 #include <sys/eventfd.h>
25 #include <sys/types.h>
27 #include <sys/prctl.h>
35 #if __GLIBC_PREREQ(2, 30) == 0
36 #include <sys/syscall.h>
37 static pid_t gettid(void)
39 return syscall(SYS_gettid);
44 #define PR_SCHED_CORE 62
45 #define PR_SCHED_CORE_GET 0
46 #define PR_SCHED_CORE_CREATE 1 /* create unique core_sched cookie */
47 #define PR_SCHED_CORE_SHARE_TO 2 /* push core_sched cookie to pid */
48 #define PR_SCHED_CORE_SHARE_FROM 3 /* pull core_sched cookie to pid */
49 #define PR_SCHED_CORE_MAX 4
52 #define MAX_PROCESSES 128
53 #define MAX_THREADS 128
55 static const char USAGE[] = "cs_prctl_test [options]\n"
57 " -P : number of processes to create.\n"
58 " -T : number of threads per process to create.\n"
59 " -d : delay time to keep tasks alive.\n"
60 " -k : keep tasks alive until keypress.\n";
62 enum pid_type {PIDTYPE_PID = 0, PIDTYPE_TGID, PIDTYPE_PGID};
64 const int THREAD_CLONE_FLAGS = CLONE_THREAD | CLONE_SIGHAND | CLONE_FS | CLONE_VM | CLONE_FILES;
70 int thr_tids[MAX_THREADS];
73 static struct child_args procs[MAX_PROCESSES];
74 static int num_processes = 2;
75 static int need_cleanup;
77 static int _prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4,
82 res = prctl(option, arg2, arg3, arg4, arg5);
83 printf("%d = prctl(%d, %ld, %ld, %ld, %lx)\n", res, option, (long)arg2, (long)arg3,
88 #define STACK_SIZE (1024 * 1024)
90 #define handle_error(msg) __handle_error(__FILE__, __LINE__, msg)
91 static void __handle_error(char *fn, int ln, char *msg)
94 printf("(%s:%d) - ", fn, ln);
97 for (pidx = 0; pidx < num_processes; ++pidx)
98 kill(procs[pidx].cpid, 15);
104 static void handle_usage(int rc, char *msg)
112 static unsigned long get_cs_cookie(int pid)
114 unsigned long long cookie;
117 ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, pid, PIDTYPE_PID,
118 (unsigned long)&cookie);
120 printf("Not a core sched system\n");
127 static int child_func_thread(void __attribute__((unused))*arg)
134 static void create_threads(int num_threads, int thr_tids[])
140 for (i = 0; i < num_threads; ++i) {
141 child_stack = malloc(STACK_SIZE);
143 handle_error("child stack allocate");
145 tid = clone(child_func_thread, child_stack + STACK_SIZE, THREAD_CLONE_FLAGS, NULL);
147 handle_error("clone thread");
152 static int child_func_process(void *arg)
154 struct child_args *ca = (struct child_args *)arg;
159 create_threads(ca->num_threads, ca->thr_tids);
161 ret = write(ca->pfd[1], &ca->thr_tids, sizeof(int) * ca->num_threads);
163 printf("write failed on pfd[%d] - error (%s)\n",
164 ca->pfd[1], strerror(errno));
173 static unsigned char child_func_process_stack[STACK_SIZE];
175 void create_processes(int num_processes, int num_threads, struct child_args proc[])
180 for (i = 0; i < num_processes; ++i) {
181 proc[i].num_threads = num_threads;
183 if (pipe(proc[i].pfd) == -1)
184 handle_error("pipe() failed");
186 cpid = clone(child_func_process, child_func_process_stack + STACK_SIZE,
189 close(proc[i].pfd[1]);
192 for (i = 0; i < num_processes; ++i) {
193 ret = read(proc[i].pfd[0], &proc[i].thr_tids, sizeof(int) * proc[i].num_threads);
195 printf("read failed on proc[%d].pfd[0] error (%s)\n",
197 close(proc[i].pfd[0]);
201 void disp_processes(int num_processes, struct child_args proc[])
205 printf("tid=%d, / tgid=%d / pgid=%d: %lx\n", gettid(), getpid(), getpgid(0),
206 get_cs_cookie(getpid()));
208 for (i = 0; i < num_processes; ++i) {
209 printf(" tid=%d, / tgid=%d / pgid=%d: %lx\n", proc[i].cpid, proc[i].cpid,
210 getpgid(proc[i].cpid), get_cs_cookie(proc[i].cpid));
211 for (j = 0; j < proc[i].num_threads; ++j) {
212 printf(" tid=%d, / tgid=%d / pgid=%d: %lx\n", proc[i].thr_tids[j],
213 proc[i].cpid, getpgid(0), get_cs_cookie(proc[i].thr_tids[j]));
221 #define validate(v) _validate(__LINE__, v, #v)
222 void _validate(int line, int val, char *msg)
226 printf("(%d) FAILED: %s\n", line, msg);
228 printf("(%d) PASSED: %s\n", line, msg);
232 int main(int argc, char *argv[])
242 while ((opt = getopt(argc, argv, ":hkT:P:d:")) != -1) {
245 num_processes = (int)strtol(optarg, NULL, 10);
248 num_threads = (int)strtoul(optarg, NULL, 10);
251 delay = (int)strtol(optarg, NULL, 10);
260 handle_usage(20, "unknown option");
264 if (num_processes < 1 || num_processes > MAX_PROCESSES)
265 handle_usage(1, "Bad processes value");
267 if (num_threads < 1 || num_threads > MAX_THREADS)
268 handle_usage(2, "Bad thread value");
275 /* put into separate process group */
276 if (setpgid(0, 0) != 0)
277 handle_error("process group");
279 printf("\n## Create a thread/process/process group hierarchy\n");
280 create_processes(num_processes, num_threads, procs);
282 disp_processes(num_processes, procs);
283 validate(get_cs_cookie(0) == 0);
285 printf("\n## Set a cookie on entire process group\n");
286 if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, PIDTYPE_PGID, 0) < 0)
287 handle_error("core_sched create failed -- PGID");
288 disp_processes(num_processes, procs);
290 validate(get_cs_cookie(0) != 0);
292 /* get a random process pid */
293 pidx = rand() % num_processes;
294 pid = procs[pidx].cpid;
296 validate(get_cs_cookie(0) == get_cs_cookie(pid));
297 validate(get_cs_cookie(0) == get_cs_cookie(procs[pidx].thr_tids[0]));
299 printf("\n## Set a new cookie on entire process/TGID [%d]\n", pid);
300 if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, pid, PIDTYPE_TGID, 0) < 0)
301 handle_error("core_sched create failed -- TGID");
302 disp_processes(num_processes, procs);
304 validate(get_cs_cookie(0) != get_cs_cookie(pid));
305 validate(get_cs_cookie(pid) != 0);
306 validate(get_cs_cookie(pid) == get_cs_cookie(procs[pidx].thr_tids[0]));
308 printf("\n## Copy the cookie of current/PGID[%d], to pid [%d] as PIDTYPE_PID\n",
310 if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, pid, PIDTYPE_PID, 0) < 0)
311 handle_error("core_sched share to itself failed -- PID");
312 disp_processes(num_processes, procs);
314 validate(get_cs_cookie(0) == get_cs_cookie(pid));
315 validate(get_cs_cookie(pid) != 0);
316 validate(get_cs_cookie(pid) != get_cs_cookie(procs[pidx].thr_tids[0]));
318 printf("\n## Copy cookie from a thread [%d] to current/PGID [%d] as PIDTYPE_PID\n",
319 procs[pidx].thr_tids[0], getpid());
320 if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_FROM, procs[pidx].thr_tids[0],
322 handle_error("core_sched share from thread failed -- PID");
323 disp_processes(num_processes, procs);
325 validate(get_cs_cookie(0) == get_cs_cookie(procs[pidx].thr_tids[0]));
326 validate(get_cs_cookie(pid) != get_cs_cookie(procs[pidx].thr_tids[0]));
328 printf("\n## Copy cookie from current [%d] to current as pidtype PGID\n", getpid());
329 if (_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, 0, PIDTYPE_PGID, 0) < 0)
330 handle_error("core_sched share to self failed -- PGID");
331 disp_processes(num_processes, procs);
333 validate(get_cs_cookie(0) == get_cs_cookie(pid));
334 validate(get_cs_cookie(pid) != 0);
335 validate(get_cs_cookie(pid) == get_cs_cookie(procs[pidx].thr_tids[0]));
337 validate(_prctl(PR_SCHED_CORE, PR_SCHED_CORE_MAX, 0, PIDTYPE_PGID, 0) < 0
340 validate(_prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, 0, PIDTYPE_PGID, 1) < 0
344 printf("TESTS FAILED. errors: %d\n", errors);
347 printf("SUCCESS !!!\n");
355 for (pidx = 0; pidx < num_processes; ++pidx)
356 kill(procs[pidx].cpid, 15);