Skip to content

Commit

Permalink
Merge pull request torvalds#118 from sched-ext/refactor_tests
Browse files Browse the repository at this point in the history
scx: Convert remaining testcases to use new framework
  • Loading branch information
htejun authored Jan 10, 2024
2 parents 228db9d + 8d7a79e commit 7592388
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 132 deletions.
22 changes: 8 additions & 14 deletions tools/testing/selftests/scx/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,6 @@ $(INCLUDE_DIR)/%.bpf.skel.h: $(SCXOBJ_DIR)/%.bpf.o $(INCLUDE_DIR)/vmlinux.h $(BP
################
# C schedulers #
################
c-sched-targets := \
select_cpu_dfl \
select_cpu_dfl_nodispatch \
select_cpu_dispatch \
select_cpu_dispatch_bad_dsq \
select_cpu_dispatch_dbl_dsp \
select_cpu_vtime

$(c-sched-targets): %: $(filter-out %.bpf.c,%.c) $(INCLUDE_DIR)/%.bpf.skel.h
$(eval sched=$(notdir $@))
$(CC) $(CFLAGS) -c $(sched).c -o $(SCXOBJ_DIR)/$(sched).o
$(CC) -o $@ $(SCXOBJ_DIR)/$(sched).o $(LIBBPF_OUTPUT) $(LDFLAGS)

TEST_GEN_PROGS := $(c-sched-targets)

override define CLEAN
rm -rf $(OUTPUT_DIR)
Expand All @@ -176,6 +162,12 @@ auto-test-targets := \
ddsp_vtimelocal_fail \
init_enable_count \
minimal \
select_cpu_dfl \
select_cpu_dfl_nodispatch \
select_cpu_dispatch \
select_cpu_dispatch_bad_dsq \
select_cpu_dispatch_dbl_dsp \
select_cpu_vtime \
test_example

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

TEST_GEN_PROGS := runner

all: runner

.PHONY: all clean help
Expand Down
8 changes: 8 additions & 0 deletions tools/testing/selftests/scx/ddsp_bogus_dsq_fail.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

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

struct user_exit_info uei;

s32 BPF_STRUCT_OPS(ddsp_bogus_dsq_fail_select_cpu, struct task_struct *p,
s32 prev_cpu, u64 wake_flags)
{
Expand All @@ -26,6 +28,11 @@ s32 BPF_STRUCT_OPS(ddsp_bogus_dsq_fail_select_cpu, struct task_struct *p,
return prev_cpu;
}

void BPF_STRUCT_OPS(ddsp_bogus_dsq_fail_exit, struct scx_exit_info *ei)
{
uei_record(&uei, ei);
}

s32 BPF_STRUCT_OPS(ddsp_bogus_dsq_fail_init)
{
scx_bpf_switch_all();
Expand All @@ -36,6 +43,7 @@ s32 BPF_STRUCT_OPS(ddsp_bogus_dsq_fail_init)
SEC(".struct_ops.link")
struct sched_ext_ops ddsp_bogus_dsq_fail_ops = {
.select_cpu = ddsp_bogus_dsq_fail_select_cpu,
.exit = ddsp_bogus_dsq_fail_exit,
.init = ddsp_bogus_dsq_fail_init,
.name = "ddsp_bogus_dsq_fail",
.timeout_ms = 1000U,
Expand Down
2 changes: 2 additions & 0 deletions tools/testing/selftests/scx/ddsp_bogus_dsq_fail.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ static enum scx_test_status run(void *ctx)
SCX_FAIL_IF(!link, "Failed to attach struct_ops");

sleep(1);

SCX_EQ(skel->bss->uei.kind, SCX_EXIT_ERROR);
bpf_link__destroy(link);

return SCX_TEST_PASS;
Expand Down
8 changes: 8 additions & 0 deletions tools/testing/selftests/scx/ddsp_vtimelocal_fail.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

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

struct user_exit_info uei;

s32 BPF_STRUCT_OPS(ddsp_vtimelocal_fail_select_cpu, struct task_struct *p,
s32 prev_cpu, u64 wake_flags)
{
Expand All @@ -23,6 +25,11 @@ s32 BPF_STRUCT_OPS(ddsp_vtimelocal_fail_select_cpu, struct task_struct *p,
return prev_cpu;
}

void BPF_STRUCT_OPS(ddsp_vtimelocal_fail_exit, struct scx_exit_info *ei)
{
uei_record(&uei, ei);
}

s32 BPF_STRUCT_OPS(ddsp_vtimelocal_fail_init)
{
scx_bpf_switch_all();
Expand All @@ -34,6 +41,7 @@ SEC(".struct_ops.link")
struct sched_ext_ops ddsp_vtimelocal_fail_ops = {
.select_cpu = ddsp_vtimelocal_fail_select_cpu,
.init = ddsp_vtimelocal_fail_init,
.exit = ddsp_vtimelocal_fail_exit,
.name = "ddsp_vtimelocal_fail",
.timeout_ms = 1000U,
};
2 changes: 2 additions & 0 deletions tools/testing/selftests/scx/ddsp_vtimelocal_fail.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ static enum scx_test_status run(void *ctx)
SCX_FAIL_IF(!link, "Failed to attach struct_ops");

sleep(1);

SCX_EQ(skel->bss->uei.kind, SCX_EXIT_ERROR);
bpf_link__destroy(link);

return SCX_TEST_PASS;
Expand Down
13 changes: 13 additions & 0 deletions tools/testing/selftests/scx/scx_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ enum scx_test_status {
SCX_TEST_FAIL,
};

/* Copied from include/linux/sched/ext.h */
enum scx_test_exit_kind {
SCX_EXIT_NONE,
SCX_EXIT_DONE,

SCX_EXIT_UNREG = 64, /* BPF unregistration */
SCX_EXIT_SYSRQ, /* requested by 'S' sysrq */

SCX_EXIT_ERROR = 1024, /* runtime error, error msg contains details */
SCX_EXIT_ERROR_BPF, /* ERROR but triggered through scx_bpf_error() */
SCX_EXIT_ERROR_STALL, /* watchdog detected stalled runnable tasks */
};

struct scx_test {
/**
* name - The name of the testcase.
Expand Down
46 changes: 33 additions & 13 deletions tools/testing/selftests/scx/select_cpu_dfl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@
* Copyright (c) 2023 David Vernet <dvernet@meta.com>
* Copyright (c) 2023 Tejun Heo <tj@kernel.org>
*/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <libgen.h>
#include <bpf/bpf.h>
#include <scx/common.h>
#include <sys/wait.h>
#include <unistd.h>
#include "select_cpu_dfl.bpf.skel.h"
#include "scx_test.h"

#define NUM_CHILDREN 1028

int main(int argc, char **argv)
static enum scx_test_status setup(void **ctx)
{
struct select_cpu_dfl *skel;

skel = select_cpu_dfl__open_and_load();
SCX_FAIL_IF(!skel, "Failed to open and load skel");
*ctx = skel;

return SCX_TEST_PASS;
}

static enum scx_test_status run(void *ctx)
{
struct select_cpu_dfl *skel = ctx;
struct bpf_link *link;
pid_t pids[NUM_CHILDREN];
int i, status;

libbpf_set_strict_mode(LIBBPF_STRICT_ALL);

skel = select_cpu_dfl__open_and_load();
SCX_BUG_ON(!skel, "Failed to open and load skel");

link = bpf_map__attach_struct_ops(skel->maps.select_cpu_dfl_ops);
SCX_BUG_ON(!link, "Failed to attach struct_ops");
SCX_FAIL_IF(!link, "Failed to attach scheduler");

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

SCX_ASSERT(!skel->bss->saw_local);

bpf_link__destroy(link);
select_cpu_dfl__destroy(skel);

return 0;
return SCX_TEST_PASS;
}

static void cleanup(void *ctx)
{
struct select_cpu_dfl *skel = ctx;

select_cpu_dfl__destroy(skel);
}

struct scx_test select_cpu_dfl = {
.name = "select_cpu_dfl",
.description = "Verify the default ops.select_cpu() dispatches tasks "
"when idles cores are found, and skips ops.enqueue()",
.setup = setup,
.run = run,
.cleanup = cleanup,
};
REGISTER_SCX_TEST(&select_cpu_dfl)
48 changes: 34 additions & 14 deletions tools/testing/selftests/scx/select_cpu_dfl_nodispatch.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
* Copyright (c) 2023 Tejun Heo <tj@kernel.org>
* Copyright (c) 2023 David Vernet <dvernet@meta.com>
* Copyright (c) 2023 Tejun Heo <tj@kernel.org>
*/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <libgen.h>
#include <bpf/bpf.h>
#include <scx/common.h>
#include <sys/wait.h>
#include <unistd.h>
#include "select_cpu_dfl_nodispatch.bpf.skel.h"
#include "scx_test.h"

#define NUM_CHILDREN 1028

int main(int argc, char **argv)
static enum scx_test_status setup(void **ctx)
{
struct select_cpu_dfl_nodispatch *skel;

skel = select_cpu_dfl_nodispatch__open_and_load();
SCX_FAIL_IF(!skel, "Failed to open and load skel");
*ctx = skel;

return SCX_TEST_PASS;
}

static enum scx_test_status run(void *ctx)
{
struct select_cpu_dfl_nodispatch *skel = ctx;
struct bpf_link *link;
pid_t pids[NUM_CHILDREN];
int i, status;

libbpf_set_strict_mode(LIBBPF_STRICT_ALL);

skel = select_cpu_dfl_nodispatch__open_and_load();
SCX_BUG_ON(!skel, "Failed to open and load skel");

link = bpf_map__attach_struct_ops(skel->maps.select_cpu_dfl_nodispatch_ops);
SCX_BUG_ON(!link, "Failed to attach struct_ops");
SCX_FAIL_IF(!link, "Failed to attach scheduler");

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

SCX_ASSERT(skel->bss->saw_local);

bpf_link__destroy(link);
select_cpu_dfl_nodispatch__destroy(skel);

return 0;
return SCX_TEST_PASS;
}

static void cleanup(void *ctx)
{
struct select_cpu_dfl_nodispatch *skel = ctx;

select_cpu_dfl_nodispatch__destroy(skel);
}

struct scx_test select_cpu_dfl_nodispatch = {
.name = "select_cpu_dfl_nodispatch",
.description = "Verify behavior of scx_bpf_select_cpu_dfl() in "
"ops.select_cpu()",
.setup = setup,
.run = run,
.cleanup = cleanup,
};
REGISTER_SCX_TEST(&select_cpu_dfl_nodispatch)
46 changes: 32 additions & 14 deletions tools/testing/selftests/scx/select_cpu_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@
* Copyright (c) 2023 David Vernet <dvernet@meta.com>
* Copyright (c) 2023 Tejun Heo <tj@kernel.org>
*/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <libgen.h>
#include <bpf/bpf.h>
#include <scx/common.h>
#include <sys/wait.h>
#include <unistd.h>
#include "select_cpu_dispatch.bpf.skel.h"
#include "scx_test.h"

#define NUM_CHILDREN 1028

int main(int argc, char **argv)
static enum scx_test_status setup(void **ctx)
{
struct select_cpu_dispatch *skel;

skel = select_cpu_dispatch__open_and_load();
SCX_FAIL_IF(!skel, "Failed to open and load skel");
*ctx = skel;

return SCX_TEST_PASS;
}

static enum scx_test_status run(void *ctx)
{
struct select_cpu_dispatch *skel = ctx;
struct bpf_link *link;
pid_t pids[NUM_CHILDREN];
int i, status;

libbpf_set_strict_mode(LIBBPF_STRICT_ALL);

skel = select_cpu_dispatch__open_and_load();
SCX_BUG_ON(!skel, "Failed to open and load skel");

link = bpf_map__attach_struct_ops(skel->maps.select_cpu_dispatch_ops);
SCX_BUG_ON(!link, "Failed to attach struct_ops");
SCX_FAIL_IF(!link, "Failed to attach scheduler");

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


bpf_link__destroy(link);
select_cpu_dispatch__destroy(skel);

return 0;
return SCX_TEST_PASS;
}

static void cleanup(void *ctx)
{
struct select_cpu_dispatch *skel = ctx;

select_cpu_dispatch__destroy(skel);
}

struct scx_test select_cpu_dispatch = {
.name = "select_cpu_dispatch",
.description = "Test direct dispatching to built-in DSQs from "
"ops.select_cpu()",
.setup = setup,
.run = run,
.cleanup = cleanup,
};
REGISTER_SCX_TEST(&select_cpu_dispatch)
Loading

0 comments on commit 7592388

Please sign in to comment.