1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
4 * Copyright (c) 2022 Tejun Heo <tj@kernel.org>
5 * Copyright (c) 2022 David Vernet <dvernet@meta.com>
12 #include <scx/common.h>
13 #include "scx_simple.bpf.skel.h"
15 const char help_fmt
[] =
16 "A simple sched_ext scheduler.\n"
18 "See the top-level comment in .bpf.c for more details.\n"
20 "Usage: %s [-f] [-v]\n"
22 " -f Use FIFO scheduling instead of weighted vtime scheduling\n"
23 " -v Print libbpf debug messages\n"
24 " -h Display this help and exit\n";
27 static volatile int exit_req
;
29 static int libbpf_print_fn(enum libbpf_print_level level
, const char *format
, va_list args
)
31 if (level
== LIBBPF_DEBUG
&& !verbose
)
33 return vfprintf(stderr
, format
, args
);
36 static void sigint_handler(int simple
)
41 static void read_stats(struct scx_simple
*skel
, __u64
*stats
)
43 int nr_cpus
= libbpf_num_possible_cpus();
44 __u64 cnts
[2][nr_cpus
];
47 memset(stats
, 0, sizeof(stats
[0]) * 2);
49 for (idx
= 0; idx
< 2; idx
++) {
52 ret
= bpf_map_lookup_elem(bpf_map__fd(skel
->maps
.stats
),
56 for (cpu
= 0; cpu
< nr_cpus
; cpu
++)
57 stats
[idx
] += cnts
[idx
][cpu
];
61 int main(int argc
, char **argv
)
63 struct scx_simple
*skel
;
64 struct bpf_link
*link
;
68 libbpf_set_print(libbpf_print_fn
);
69 signal(SIGINT
, sigint_handler
);
70 signal(SIGTERM
, sigint_handler
);
72 skel
= SCX_OPS_OPEN(simple_ops
, scx_simple
);
74 while ((opt
= getopt(argc
, argv
, "fvh")) != -1) {
77 skel
->rodata
->fifo_sched
= true;
83 fprintf(stderr
, help_fmt
, basename(argv
[0]));
88 SCX_OPS_LOAD(skel
, simple_ops
, scx_simple
, uei
);
89 link
= SCX_OPS_ATTACH(skel
, simple_ops
, scx_simple
);
91 while (!exit_req
&& !UEI_EXITED(skel
, uei
)) {
94 read_stats(skel
, stats
);
95 printf("local=%llu global=%llu\n", stats
[0], stats
[1]);
100 bpf_link__destroy(link
);
101 ecode
= UEI_REPORT(skel
, uei
);
102 scx_simple__destroy(skel
);
104 if (UEI_ECODE_RESTART(ecode
))