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