xref: /linux/tools/testing/selftests/sched_ext/select_cpu_dfl.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_dfl.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_dfl *skel;
19 
20 	skel = select_cpu_dfl__open();
21 	SCX_FAIL_IF(!skel, "Failed to open");
22 	SCX_ENUM_INIT(skel);
23 	SCX_FAIL_IF(select_cpu_dfl__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_dfl *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_dfl_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 	SCX_ASSERT(!skel->bss->saw_local);
54 
55 	bpf_link__destroy(link);
56 
57 	return SCX_TEST_PASS;
58 }
59 
cleanup(void * ctx)60 static void cleanup(void *ctx)
61 {
62 	struct select_cpu_dfl *skel = ctx;
63 
64 	select_cpu_dfl__destroy(skel);
65 }
66 
67 struct scx_test select_cpu_dfl = {
68 	.name = "select_cpu_dfl",
69 	.description = "Verify the default ops.select_cpu() dispatches tasks "
70 		       "when idles cores are found, and skips ops.enqueue()",
71 	.setup = setup,
72 	.run = run,
73 	.cleanup = cleanup,
74 };
75 REGISTER_SCX_TEST(&select_cpu_dfl)
76