2024-06-18 13:09:17 -07:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
|
|
|
|
* Copyright (c) 2022 Tejun Heo <tj@kernel.org>
|
|
|
|
* Copyright (c) 2022 David Vernet <dvernet@meta.com>
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <bpf/bpf.h>
|
|
|
|
#include <scx/common.h>
|
|
|
|
#include "scx_simple.bpf.skel.h"
|
|
|
|
|
|
|
|
const char help_fmt[] =
|
|
|
|
"A simple sched_ext scheduler.\n"
|
|
|
|
"\n"
|
|
|
|
"See the top-level comment in .bpf.c for more details.\n"
|
|
|
|
"\n"
|
2024-06-18 13:09:21 -07:00
|
|
|
"Usage: %s [-f] [-v]\n"
|
2024-06-18 13:09:17 -07:00
|
|
|
"\n"
|
2024-06-18 13:09:21 -07:00
|
|
|
" -f Use FIFO scheduling instead of weighted vtime scheduling\n"
|
2024-06-18 13:09:17 -07:00
|
|
|
" -v Print libbpf debug messages\n"
|
|
|
|
" -h Display this help and exit\n";
|
|
|
|
|
|
|
|
static bool verbose;
|
|
|
|
static volatile int exit_req;
|
|
|
|
|
|
|
|
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
|
|
|
|
{
|
|
|
|
if (level == LIBBPF_DEBUG && !verbose)
|
|
|
|
return 0;
|
|
|
|
return vfprintf(stderr, format, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sigint_handler(int simple)
|
|
|
|
{
|
|
|
|
exit_req = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_stats(struct scx_simple *skel, __u64 *stats)
|
|
|
|
{
|
|
|
|
int nr_cpus = libbpf_num_possible_cpus();
|
|
|
|
__u64 cnts[2][nr_cpus];
|
|
|
|
__u32 idx;
|
|
|
|
|
|
|
|
memset(stats, 0, sizeof(stats[0]) * 2);
|
|
|
|
|
|
|
|
for (idx = 0; idx < 2; idx++) {
|
|
|
|
int ret, cpu;
|
|
|
|
|
|
|
|
ret = bpf_map_lookup_elem(bpf_map__fd(skel->maps.stats),
|
|
|
|
&idx, cnts[idx]);
|
|
|
|
if (ret < 0)
|
|
|
|
continue;
|
|
|
|
for (cpu = 0; cpu < nr_cpus; cpu++)
|
|
|
|
stats[idx] += cnts[idx][cpu];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct scx_simple *skel;
|
|
|
|
struct bpf_link *link;
|
|
|
|
__u32 opt;
|
2024-06-18 13:09:20 -07:00
|
|
|
__u64 ecode;
|
2024-06-18 13:09:17 -07:00
|
|
|
|
|
|
|
libbpf_set_print(libbpf_print_fn);
|
|
|
|
signal(SIGINT, sigint_handler);
|
|
|
|
signal(SIGTERM, sigint_handler);
|
2024-06-18 13:09:20 -07:00
|
|
|
restart:
|
2024-06-18 13:09:17 -07:00
|
|
|
skel = SCX_OPS_OPEN(simple_ops, scx_simple);
|
|
|
|
|
2024-06-18 13:09:21 -07:00
|
|
|
while ((opt = getopt(argc, argv, "fvh")) != -1) {
|
2024-06-18 13:09:17 -07:00
|
|
|
switch (opt) {
|
2024-06-18 13:09:21 -07:00
|
|
|
case 'f':
|
|
|
|
skel->rodata->fifo_sched = true;
|
|
|
|
break;
|
2024-06-18 13:09:17 -07:00
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, help_fmt, basename(argv[0]));
|
|
|
|
return opt != 'h';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 13:09:18 -07:00
|
|
|
SCX_OPS_LOAD(skel, simple_ops, scx_simple, uei);
|
2024-06-18 13:09:17 -07:00
|
|
|
link = SCX_OPS_ATTACH(skel, simple_ops, scx_simple);
|
|
|
|
|
|
|
|
while (!exit_req && !UEI_EXITED(skel, uei)) {
|
|
|
|
__u64 stats[2];
|
|
|
|
|
|
|
|
read_stats(skel, stats);
|
|
|
|
printf("local=%llu global=%llu\n", stats[0], stats[1]);
|
|
|
|
fflush(stdout);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bpf_link__destroy(link);
|
2024-06-18 13:09:20 -07:00
|
|
|
ecode = UEI_REPORT(skel, uei);
|
2024-06-18 13:09:17 -07:00
|
|
|
scx_simple__destroy(skel);
|
2024-06-18 13:09:20 -07:00
|
|
|
|
|
|
|
if (UEI_ECODE_RESTART(ecode))
|
|
|
|
goto restart;
|
2024-06-18 13:09:17 -07:00
|
|
|
return 0;
|
|
|
|
}
|