xref: /linux/tools/testing/selftests/sched_ext/select_cpu_dispatch.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1*a5db7817SDavid Vernet /* SPDX-License-Identifier: GPL-2.0 */
2*a5db7817SDavid Vernet /*
3*a5db7817SDavid Vernet  * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4*a5db7817SDavid Vernet  * Copyright (c) 2023 David Vernet <dvernet@meta.com>
5*a5db7817SDavid Vernet  * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
6*a5db7817SDavid Vernet  */
7*a5db7817SDavid Vernet #include <bpf/bpf.h>
8*a5db7817SDavid Vernet #include <scx/common.h>
9*a5db7817SDavid Vernet #include <sys/wait.h>
10*a5db7817SDavid Vernet #include <unistd.h>
11*a5db7817SDavid Vernet #include "select_cpu_dispatch.bpf.skel.h"
12*a5db7817SDavid Vernet #include "scx_test.h"
13*a5db7817SDavid Vernet 
14*a5db7817SDavid Vernet #define NUM_CHILDREN 1028
15*a5db7817SDavid Vernet 
setup(void ** ctx)16*a5db7817SDavid Vernet static enum scx_test_status setup(void **ctx)
17*a5db7817SDavid Vernet {
18*a5db7817SDavid Vernet 	struct select_cpu_dispatch *skel;
19*a5db7817SDavid Vernet 
20*a5db7817SDavid Vernet 	skel = select_cpu_dispatch__open_and_load();
21*a5db7817SDavid Vernet 	SCX_FAIL_IF(!skel, "Failed to open and load skel");
22*a5db7817SDavid Vernet 	*ctx = skel;
23*a5db7817SDavid Vernet 
24*a5db7817SDavid Vernet 	return SCX_TEST_PASS;
25*a5db7817SDavid Vernet }
26*a5db7817SDavid Vernet 
run(void * ctx)27*a5db7817SDavid Vernet static enum scx_test_status run(void *ctx)
28*a5db7817SDavid Vernet {
29*a5db7817SDavid Vernet 	struct select_cpu_dispatch *skel = ctx;
30*a5db7817SDavid Vernet 	struct bpf_link *link;
31*a5db7817SDavid Vernet 	pid_t pids[NUM_CHILDREN];
32*a5db7817SDavid Vernet 	int i, status;
33*a5db7817SDavid Vernet 
34*a5db7817SDavid Vernet 	link = bpf_map__attach_struct_ops(skel->maps.select_cpu_dispatch_ops);
35*a5db7817SDavid Vernet 	SCX_FAIL_IF(!link, "Failed to attach scheduler");
36*a5db7817SDavid Vernet 
37*a5db7817SDavid Vernet 	for (i = 0; i < NUM_CHILDREN; i++) {
38*a5db7817SDavid Vernet 		pids[i] = fork();
39*a5db7817SDavid Vernet 		if (pids[i] == 0) {
40*a5db7817SDavid Vernet 			sleep(1);
41*a5db7817SDavid Vernet 			exit(0);
42*a5db7817SDavid Vernet 		}
43*a5db7817SDavid Vernet 	}
44*a5db7817SDavid Vernet 
45*a5db7817SDavid Vernet 	for (i = 0; i < NUM_CHILDREN; i++) {
46*a5db7817SDavid Vernet 		SCX_EQ(waitpid(pids[i], &status, 0), pids[i]);
47*a5db7817SDavid Vernet 		SCX_EQ(status, 0);
48*a5db7817SDavid Vernet 	}
49*a5db7817SDavid Vernet 
50*a5db7817SDavid Vernet 	bpf_link__destroy(link);
51*a5db7817SDavid Vernet 
52*a5db7817SDavid Vernet 	return SCX_TEST_PASS;
53*a5db7817SDavid Vernet }
54*a5db7817SDavid Vernet 
cleanup(void * ctx)55*a5db7817SDavid Vernet static void cleanup(void *ctx)
56*a5db7817SDavid Vernet {
57*a5db7817SDavid Vernet 	struct select_cpu_dispatch *skel = ctx;
58*a5db7817SDavid Vernet 
59*a5db7817SDavid Vernet 	select_cpu_dispatch__destroy(skel);
60*a5db7817SDavid Vernet }
61*a5db7817SDavid Vernet 
62*a5db7817SDavid Vernet struct scx_test select_cpu_dispatch = {
63*a5db7817SDavid Vernet 	.name = "select_cpu_dispatch",
64*a5db7817SDavid Vernet 	.description = "Test direct dispatching to built-in DSQs from "
65*a5db7817SDavid Vernet 		       "ops.select_cpu()",
66*a5db7817SDavid Vernet 	.setup = setup,
67*a5db7817SDavid Vernet 	.run = run,
68*a5db7817SDavid Vernet 	.cleanup = cleanup,
69*a5db7817SDavid Vernet };
70*a5db7817SDavid Vernet REGISTER_SCX_TEST(&select_cpu_dispatch)
71