Skip to content

Commit 5ae5161

Browse files
arighihtejun
authored andcommitted
selftests/sched_ext: Add NUMA-aware scheduler test
Add a selftest to validate the behavior of the NUMA-aware scheduler functionalities, including idle CPU selection within nodes, per-node DSQs and CPU to node mapping. Signed-off-by: Andrea Righi <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 5e3b642 commit 5ae5161

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

tools/testing/selftests/sched_ext/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ auto-test-targets := \
172172
maximal \
173173
maybe_null \
174174
minimal \
175+
numa \
175176
prog_run \
176177
reload_loop \
177178
select_cpu_dfl \
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* A scheduler that validates the behavior of the NUMA-aware
4+
* functionalities.
5+
*
6+
* The scheduler creates a separate DSQ for each NUMA node, ensuring tasks
7+
* are exclusively processed by CPUs within their respective nodes. Idle
8+
* CPUs are selected only within the same node, so task migration can only
9+
* occurs between CPUs belonging to the same node.
10+
*
11+
* Copyright (c) 2025 Andrea Righi <[email protected]>
12+
*/
13+
14+
#include <scx/common.bpf.h>
15+
16+
char _license[] SEC("license") = "GPL";
17+
18+
UEI_DEFINE(uei);
19+
20+
const volatile unsigned int __COMPAT_SCX_PICK_IDLE_IN_NODE;
21+
22+
static bool is_cpu_idle(s32 cpu, int node)
23+
{
24+
const struct cpumask *idle_cpumask;
25+
bool idle;
26+
27+
idle_cpumask = __COMPAT_scx_bpf_get_idle_cpumask_node(node);
28+
idle = bpf_cpumask_test_cpu(cpu, idle_cpumask);
29+
scx_bpf_put_cpumask(idle_cpumask);
30+
31+
return idle;
32+
}
33+
34+
s32 BPF_STRUCT_OPS(numa_select_cpu,
35+
struct task_struct *p, s32 prev_cpu, u64 wake_flags)
36+
{
37+
int node = __COMPAT_scx_bpf_cpu_node(scx_bpf_task_cpu(p));
38+
s32 cpu;
39+
40+
/*
41+
* We could just use __COMPAT_scx_bpf_pick_any_cpu_node() here,
42+
* since it already tries to pick an idle CPU within the node
43+
* first, but let's use both functions for better testing coverage.
44+
*/
45+
cpu = __COMPAT_scx_bpf_pick_idle_cpu_node(p->cpus_ptr, node,
46+
__COMPAT_SCX_PICK_IDLE_IN_NODE);
47+
if (cpu < 0)
48+
cpu = __COMPAT_scx_bpf_pick_any_cpu_node(p->cpus_ptr, node,
49+
__COMPAT_SCX_PICK_IDLE_IN_NODE);
50+
51+
if (is_cpu_idle(cpu, node))
52+
scx_bpf_error("CPU %d should be marked as busy", cpu);
53+
54+
if (__COMPAT_scx_bpf_cpu_node(cpu) != node)
55+
scx_bpf_error("CPU %d should be in node %d", cpu, node);
56+
57+
return cpu;
58+
}
59+
60+
void BPF_STRUCT_OPS(numa_enqueue, struct task_struct *p, u64 enq_flags)
61+
{
62+
int node = __COMPAT_scx_bpf_cpu_node(scx_bpf_task_cpu(p));
63+
64+
scx_bpf_dsq_insert(p, node, SCX_SLICE_DFL, enq_flags);
65+
}
66+
67+
void BPF_STRUCT_OPS(numa_dispatch, s32 cpu, struct task_struct *prev)
68+
{
69+
int node = __COMPAT_scx_bpf_cpu_node(cpu);
70+
71+
scx_bpf_dsq_move_to_local(node);
72+
}
73+
74+
s32 BPF_STRUCT_OPS_SLEEPABLE(numa_init)
75+
{
76+
int node, err;
77+
78+
bpf_for(node, 0, __COMPAT_scx_bpf_nr_node_ids()) {
79+
err = scx_bpf_create_dsq(node, node);
80+
if (err)
81+
return err;
82+
}
83+
84+
return 0;
85+
}
86+
87+
void BPF_STRUCT_OPS(numa_exit, struct scx_exit_info *ei)
88+
{
89+
UEI_RECORD(uei, ei);
90+
}
91+
92+
SEC(".struct_ops.link")
93+
struct sched_ext_ops numa_ops = {
94+
.select_cpu = (void *)numa_select_cpu,
95+
.enqueue = (void *)numa_enqueue,
96+
.dispatch = (void *)numa_dispatch,
97+
.init = (void *)numa_init,
98+
.exit = (void *)numa_exit,
99+
.name = "numa",
100+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (c) 2025 Andrea Righi <[email protected]>
4+
*/
5+
#include <bpf/bpf.h>
6+
#include <scx/common.h>
7+
#include <sys/wait.h>
8+
#include <unistd.h>
9+
#include "numa.bpf.skel.h"
10+
#include "scx_test.h"
11+
12+
static enum scx_test_status setup(void **ctx)
13+
{
14+
struct numa *skel;
15+
16+
skel = numa__open();
17+
SCX_FAIL_IF(!skel, "Failed to open");
18+
SCX_ENUM_INIT(skel);
19+
skel->rodata->__COMPAT_SCX_PICK_IDLE_IN_NODE = SCX_PICK_IDLE_IN_NODE;
20+
skel->struct_ops.numa_ops->flags = SCX_OPS_BUILTIN_IDLE_PER_NODE;
21+
SCX_FAIL_IF(numa__load(skel), "Failed to load skel");
22+
23+
*ctx = skel;
24+
25+
return SCX_TEST_PASS;
26+
}
27+
28+
static enum scx_test_status run(void *ctx)
29+
{
30+
struct numa *skel = ctx;
31+
struct bpf_link *link;
32+
33+
link = bpf_map__attach_struct_ops(skel->maps.numa_ops);
34+
SCX_FAIL_IF(!link, "Failed to attach scheduler");
35+
36+
/* Just sleeping is fine, plenty of scheduling events happening */
37+
sleep(1);
38+
39+
SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_NONE));
40+
bpf_link__destroy(link);
41+
42+
return SCX_TEST_PASS;
43+
}
44+
45+
static void cleanup(void *ctx)
46+
{
47+
struct numa *skel = ctx;
48+
49+
numa__destroy(skel);
50+
}
51+
52+
struct scx_test numa = {
53+
.name = "numa",
54+
.description = "Verify NUMA-aware functionalities",
55+
.setup = setup,
56+
.run = run,
57+
.cleanup = cleanup,
58+
};
59+
REGISTER_SCX_TEST(&numa)

0 commit comments

Comments
 (0)