Skip to content

Commit 7592388

Browse files
authoredJan 10, 2024
Merge pull request torvalds#118 from sched-ext/refactor_tests
scx: Convert remaining testcases to use new framework
2 parents 228db9d + 8d7a79e commit 7592388

12 files changed

+235
-132
lines changed
 

‎tools/testing/selftests/scx/Makefile

+8-14
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,6 @@ $(INCLUDE_DIR)/%.bpf.skel.h: $(SCXOBJ_DIR)/%.bpf.o $(INCLUDE_DIR)/vmlinux.h $(BP
147147
################
148148
# C schedulers #
149149
################
150-
c-sched-targets := \
151-
select_cpu_dfl \
152-
select_cpu_dfl_nodispatch \
153-
select_cpu_dispatch \
154-
select_cpu_dispatch_bad_dsq \
155-
select_cpu_dispatch_dbl_dsp \
156-
select_cpu_vtime
157-
158-
$(c-sched-targets): %: $(filter-out %.bpf.c,%.c) $(INCLUDE_DIR)/%.bpf.skel.h
159-
$(eval sched=$(notdir $@))
160-
$(CC) $(CFLAGS) -c $(sched).c -o $(SCXOBJ_DIR)/$(sched).o
161-
$(CC) -o $@ $(SCXOBJ_DIR)/$(sched).o $(LIBBPF_OUTPUT) $(LDFLAGS)
162-
163-
TEST_GEN_PROGS := $(c-sched-targets)
164150

165151
override define CLEAN
166152
rm -rf $(OUTPUT_DIR)
@@ -176,6 +162,12 @@ auto-test-targets := \
176162
ddsp_vtimelocal_fail \
177163
init_enable_count \
178164
minimal \
165+
select_cpu_dfl \
166+
select_cpu_dfl_nodispatch \
167+
select_cpu_dispatch \
168+
select_cpu_dispatch_bad_dsq \
169+
select_cpu_dispatch_dbl_dsp \
170+
select_cpu_vtime \
179171
test_example
180172

181173
testcase-targets := $(addsuffix .o,$(addprefix $(SCXOBJ_DIR)/,$(auto-test-targets)))
@@ -198,6 +190,8 @@ runner: $(SCXOBJ_DIR)/runner.o $(BPFOBJ) $(testcase-targets)
198190
@echo "$(testcase-targets)"
199191
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
200192

193+
TEST_GEN_PROGS := runner
194+
201195
all: runner
202196

203197
.PHONY: all clean help

‎tools/testing/selftests/scx/ddsp_bogus_dsq_fail.bpf.c

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11+
struct user_exit_info uei;
12+
1113
s32 BPF_STRUCT_OPS(ddsp_bogus_dsq_fail_select_cpu, struct task_struct *p,
1214
s32 prev_cpu, u64 wake_flags)
1315
{
@@ -26,6 +28,11 @@ s32 BPF_STRUCT_OPS(ddsp_bogus_dsq_fail_select_cpu, struct task_struct *p,
2628
return prev_cpu;
2729
}
2830

31+
void BPF_STRUCT_OPS(ddsp_bogus_dsq_fail_exit, struct scx_exit_info *ei)
32+
{
33+
uei_record(&uei, ei);
34+
}
35+
2936
s32 BPF_STRUCT_OPS(ddsp_bogus_dsq_fail_init)
3037
{
3138
scx_bpf_switch_all();
@@ -36,6 +43,7 @@ s32 BPF_STRUCT_OPS(ddsp_bogus_dsq_fail_init)
3643
SEC(".struct_ops.link")
3744
struct sched_ext_ops ddsp_bogus_dsq_fail_ops = {
3845
.select_cpu = ddsp_bogus_dsq_fail_select_cpu,
46+
.exit = ddsp_bogus_dsq_fail_exit,
3947
.init = ddsp_bogus_dsq_fail_init,
4048
.name = "ddsp_bogus_dsq_fail",
4149
.timeout_ms = 1000U,

‎tools/testing/selftests/scx/ddsp_bogus_dsq_fail.c

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static enum scx_test_status run(void *ctx)
3131
SCX_FAIL_IF(!link, "Failed to attach struct_ops");
3232

3333
sleep(1);
34+
35+
SCX_EQ(skel->bss->uei.kind, SCX_EXIT_ERROR);
3436
bpf_link__destroy(link);
3537

3638
return SCX_TEST_PASS;

‎tools/testing/selftests/scx/ddsp_vtimelocal_fail.bpf.c

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
char _license[] SEC("license") = "GPL";
1010

11+
struct user_exit_info uei;
12+
1113
s32 BPF_STRUCT_OPS(ddsp_vtimelocal_fail_select_cpu, struct task_struct *p,
1214
s32 prev_cpu, u64 wake_flags)
1315
{
@@ -23,6 +25,11 @@ s32 BPF_STRUCT_OPS(ddsp_vtimelocal_fail_select_cpu, struct task_struct *p,
2325
return prev_cpu;
2426
}
2527

28+
void BPF_STRUCT_OPS(ddsp_vtimelocal_fail_exit, struct scx_exit_info *ei)
29+
{
30+
uei_record(&uei, ei);
31+
}
32+
2633
s32 BPF_STRUCT_OPS(ddsp_vtimelocal_fail_init)
2734
{
2835
scx_bpf_switch_all();
@@ -34,6 +41,7 @@ SEC(".struct_ops.link")
3441
struct sched_ext_ops ddsp_vtimelocal_fail_ops = {
3542
.select_cpu = ddsp_vtimelocal_fail_select_cpu,
3643
.init = ddsp_vtimelocal_fail_init,
44+
.exit = ddsp_vtimelocal_fail_exit,
3745
.name = "ddsp_vtimelocal_fail",
3846
.timeout_ms = 1000U,
3947
};

‎tools/testing/selftests/scx/ddsp_vtimelocal_fail.c

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ static enum scx_test_status run(void *ctx)
3030
SCX_FAIL_IF(!link, "Failed to attach struct_ops");
3131

3232
sleep(1);
33+
34+
SCX_EQ(skel->bss->uei.kind, SCX_EXIT_ERROR);
3335
bpf_link__destroy(link);
3436

3537
return SCX_TEST_PASS;

‎tools/testing/selftests/scx/scx_test.h

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ enum scx_test_status {
1717
SCX_TEST_FAIL,
1818
};
1919

20+
/* Copied from include/linux/sched/ext.h */
21+
enum scx_test_exit_kind {
22+
SCX_EXIT_NONE,
23+
SCX_EXIT_DONE,
24+
25+
SCX_EXIT_UNREG = 64, /* BPF unregistration */
26+
SCX_EXIT_SYSRQ, /* requested by 'S' sysrq */
27+
28+
SCX_EXIT_ERROR = 1024, /* runtime error, error msg contains details */
29+
SCX_EXIT_ERROR_BPF, /* ERROR but triggered through scx_bpf_error() */
30+
SCX_EXIT_ERROR_STALL, /* watchdog detected stalled runnable tasks */
31+
};
32+
2033
struct scx_test {
2134
/**
2235
* name - The name of the testcase.

‎tools/testing/selftests/scx/select_cpu_dfl.c

+33-13
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,35 @@
44
* Copyright (c) 2023 David Vernet <dvernet@meta.com>
55
* Copyright (c) 2023 Tejun Heo <tj@kernel.org>
66
*/
7-
#include <stdio.h>
8-
#include <unistd.h>
9-
#include <signal.h>
10-
#include <libgen.h>
117
#include <bpf/bpf.h>
128
#include <scx/common.h>
139
#include <sys/wait.h>
10+
#include <unistd.h>
1411
#include "select_cpu_dfl.bpf.skel.h"
1512
#include "scx_test.h"
1613

1714
#define NUM_CHILDREN 1028
1815

19-
int main(int argc, char **argv)
16+
static enum scx_test_status setup(void **ctx)
2017
{
2118
struct select_cpu_dfl *skel;
19+
20+
skel = select_cpu_dfl__open_and_load();
21+
SCX_FAIL_IF(!skel, "Failed to open and load skel");
22+
*ctx = skel;
23+
24+
return SCX_TEST_PASS;
25+
}
26+
27+
static enum scx_test_status run(void *ctx)
28+
{
29+
struct select_cpu_dfl *skel = ctx;
2230
struct bpf_link *link;
2331
pid_t pids[NUM_CHILDREN];
2432
int i, status;
2533

26-
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
27-
28-
skel = select_cpu_dfl__open_and_load();
29-
SCX_BUG_ON(!skel, "Failed to open and load skel");
30-
3134
link = bpf_map__attach_struct_ops(skel->maps.select_cpu_dfl_ops);
32-
SCX_BUG_ON(!link, "Failed to attach struct_ops");
35+
SCX_FAIL_IF(!link, "Failed to attach scheduler");
3336

3437
for (i = 0; i < NUM_CHILDREN; i++) {
3538
pids[i] = fork();
@@ -45,8 +48,25 @@ int main(int argc, char **argv)
4548
}
4649

4750
SCX_ASSERT(!skel->bss->saw_local);
51+
4852
bpf_link__destroy(link);
49-
select_cpu_dfl__destroy(skel);
5053

51-
return 0;
54+
return SCX_TEST_PASS;
5255
}
56+
57+
static void cleanup(void *ctx)
58+
{
59+
struct select_cpu_dfl *skel = ctx;
60+
61+
select_cpu_dfl__destroy(skel);
62+
}
63+
64+
struct scx_test select_cpu_dfl = {
65+
.name = "select_cpu_dfl",
66+
.description = "Verify the default ops.select_cpu() dispatches tasks "
67+
"when idles cores are found, and skips ops.enqueue()",
68+
.setup = setup,
69+
.run = run,
70+
.cleanup = cleanup,
71+
};
72+
REGISTER_SCX_TEST(&select_cpu_dfl)
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
/*
33
* Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4-
* Copyright (c) 2023 Tejun Heo <tj@kernel.org>
54
* Copyright (c) 2023 David Vernet <dvernet@meta.com>
5+
* Copyright (c) 2023 Tejun Heo <tj@kernel.org>
66
*/
7-
#include <stdio.h>
8-
#include <unistd.h>
9-
#include <signal.h>
10-
#include <libgen.h>
117
#include <bpf/bpf.h>
128
#include <scx/common.h>
139
#include <sys/wait.h>
10+
#include <unistd.h>
1411
#include "select_cpu_dfl_nodispatch.bpf.skel.h"
1512
#include "scx_test.h"
1613

1714
#define NUM_CHILDREN 1028
1815

19-
int main(int argc, char **argv)
16+
static enum scx_test_status setup(void **ctx)
2017
{
2118
struct select_cpu_dfl_nodispatch *skel;
19+
20+
skel = select_cpu_dfl_nodispatch__open_and_load();
21+
SCX_FAIL_IF(!skel, "Failed to open and load skel");
22+
*ctx = skel;
23+
24+
return SCX_TEST_PASS;
25+
}
26+
27+
static enum scx_test_status run(void *ctx)
28+
{
29+
struct select_cpu_dfl_nodispatch *skel = ctx;
2230
struct bpf_link *link;
2331
pid_t pids[NUM_CHILDREN];
2432
int i, status;
2533

26-
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
27-
28-
skel = select_cpu_dfl_nodispatch__open_and_load();
29-
SCX_BUG_ON(!skel, "Failed to open and load skel");
30-
3134
link = bpf_map__attach_struct_ops(skel->maps.select_cpu_dfl_nodispatch_ops);
32-
SCX_BUG_ON(!link, "Failed to attach struct_ops");
35+
SCX_FAIL_IF(!link, "Failed to attach scheduler");
3336

3437
for (i = 0; i < NUM_CHILDREN; i++) {
3538
pids[i] = fork();
@@ -45,8 +48,25 @@ int main(int argc, char **argv)
4548
}
4649

4750
SCX_ASSERT(skel->bss->saw_local);
51+
4852
bpf_link__destroy(link);
49-
select_cpu_dfl_nodispatch__destroy(skel);
5053

51-
return 0;
54+
return SCX_TEST_PASS;
55+
}
56+
57+
static void cleanup(void *ctx)
58+
{
59+
struct select_cpu_dfl_nodispatch *skel = ctx;
60+
61+
select_cpu_dfl_nodispatch__destroy(skel);
5262
}
63+
64+
struct scx_test select_cpu_dfl_nodispatch = {
65+
.name = "select_cpu_dfl_nodispatch",
66+
.description = "Verify behavior of scx_bpf_select_cpu_dfl() in "
67+
"ops.select_cpu()",
68+
.setup = setup,
69+
.run = run,
70+
.cleanup = cleanup,
71+
};
72+
REGISTER_SCX_TEST(&select_cpu_dfl_nodispatch)

‎tools/testing/selftests/scx/select_cpu_dispatch.c

+32-14
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,35 @@
44
* Copyright (c) 2023 David Vernet <dvernet@meta.com>
55
* Copyright (c) 2023 Tejun Heo <tj@kernel.org>
66
*/
7-
#include <stdio.h>
8-
#include <unistd.h>
9-
#include <signal.h>
10-
#include <libgen.h>
117
#include <bpf/bpf.h>
128
#include <scx/common.h>
139
#include <sys/wait.h>
10+
#include <unistd.h>
1411
#include "select_cpu_dispatch.bpf.skel.h"
1512
#include "scx_test.h"
1613

1714
#define NUM_CHILDREN 1028
1815

19-
int main(int argc, char **argv)
16+
static enum scx_test_status setup(void **ctx)
2017
{
2118
struct select_cpu_dispatch *skel;
19+
20+
skel = select_cpu_dispatch__open_and_load();
21+
SCX_FAIL_IF(!skel, "Failed to open and load skel");
22+
*ctx = skel;
23+
24+
return SCX_TEST_PASS;
25+
}
26+
27+
static enum scx_test_status run(void *ctx)
28+
{
29+
struct select_cpu_dispatch *skel = ctx;
2230
struct bpf_link *link;
2331
pid_t pids[NUM_CHILDREN];
2432
int i, status;
2533

26-
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
27-
28-
skel = select_cpu_dispatch__open_and_load();
29-
SCX_BUG_ON(!skel, "Failed to open and load skel");
30-
3134
link = bpf_map__attach_struct_ops(skel->maps.select_cpu_dispatch_ops);
32-
SCX_BUG_ON(!link, "Failed to attach struct_ops");
35+
SCX_FAIL_IF(!link, "Failed to attach scheduler");
3336

3437
for (i = 0; i < NUM_CHILDREN; i++) {
3538
pids[i] = fork();
@@ -44,9 +47,24 @@ int main(int argc, char **argv)
4447
SCX_EQ(status, 0);
4548
}
4649

47-
4850
bpf_link__destroy(link);
49-
select_cpu_dispatch__destroy(skel);
5051

51-
return 0;
52+
return SCX_TEST_PASS;
5253
}
54+
55+
static void cleanup(void *ctx)
56+
{
57+
struct select_cpu_dispatch *skel = ctx;
58+
59+
select_cpu_dispatch__destroy(skel);
60+
}
61+
62+
struct scx_test select_cpu_dispatch = {
63+
.name = "select_cpu_dispatch",
64+
.description = "Test direct dispatching to built-in DSQs from "
65+
"ops.select_cpu()",
66+
.setup = setup,
67+
.run = run,
68+
.cleanup = cleanup,
69+
};
70+
REGISTER_SCX_TEST(&select_cpu_dispatch)

0 commit comments

Comments
 (0)