2024-06-18 20:09:17 +00: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 <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <bpf/bpf.h>
|
|
|
|
#include <scx/common.h>
|
|
|
|
#include "scx_qmap.bpf.skel.h"
|
|
|
|
|
|
|
|
const char help_fmt[] =
|
|
|
|
"A simple five-level FIFO queue sched_ext scheduler.\n"
|
|
|
|
"\n"
|
|
|
|
"See the top-level comment in .bpf.c for more details.\n"
|
|
|
|
"\n"
|
2024-06-18 20:09:18 +00:00
|
|
|
"Usage: %s [-s SLICE_US] [-e COUNT] [-t COUNT] [-T COUNT] [-b COUNT]\n"
|
|
|
|
" [-d PID] [-p] [-v]\n"
|
2024-06-18 20:09:17 +00:00
|
|
|
"\n"
|
|
|
|
" -s SLICE_US Override slice duration\n"
|
|
|
|
" -e COUNT Trigger scx_bpf_error() after COUNT enqueues\n"
|
2024-06-18 20:09:18 +00:00
|
|
|
" -t COUNT Stall every COUNT'th user thread\n"
|
|
|
|
" -T COUNT Stall every COUNT'th kernel thread\n"
|
2024-06-18 20:09:17 +00:00
|
|
|
" -b COUNT Dispatch upto COUNT tasks together\n"
|
2024-06-18 20:09:18 +00:00
|
|
|
" -d PID Disallow a process from switching into SCHED_EXT (-1 for self)\n"
|
2024-06-18 20:09:17 +00:00
|
|
|
" -p Switch only tasks on SCHED_EXT policy intead of all\n"
|
|
|
|
" -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 dummy)
|
|
|
|
{
|
|
|
|
exit_req = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct scx_qmap *skel;
|
|
|
|
struct bpf_link *link;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
libbpf_set_print(libbpf_print_fn);
|
|
|
|
signal(SIGINT, sigint_handler);
|
|
|
|
signal(SIGTERM, sigint_handler);
|
|
|
|
|
|
|
|
skel = SCX_OPS_OPEN(qmap_ops, scx_qmap);
|
|
|
|
|
2024-06-18 20:09:18 +00:00
|
|
|
while ((opt = getopt(argc, argv, "s:e:t:T:b:d:pvh")) != -1) {
|
2024-06-18 20:09:17 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 's':
|
|
|
|
skel->rodata->slice_ns = strtoull(optarg, NULL, 0) * 1000;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
skel->bss->test_error_cnt = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
2024-06-18 20:09:18 +00:00
|
|
|
case 't':
|
|
|
|
skel->rodata->stall_user_nth = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
skel->rodata->stall_kernel_nth = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
2024-06-18 20:09:17 +00:00
|
|
|
case 'b':
|
|
|
|
skel->rodata->dsp_batch = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
2024-06-18 20:09:18 +00:00
|
|
|
case 'd':
|
|
|
|
skel->rodata->disallow_tgid = strtol(optarg, NULL, 0);
|
|
|
|
if (skel->rodata->disallow_tgid < 0)
|
|
|
|
skel->rodata->disallow_tgid = getpid();
|
|
|
|
break;
|
2024-06-18 20:09:17 +00:00
|
|
|
case 'p':
|
|
|
|
skel->struct_ops.qmap_ops->flags |= SCX_OPS_SWITCH_PARTIAL;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, help_fmt, basename(argv[0]));
|
|
|
|
return opt != 'h';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SCX_OPS_LOAD(skel, qmap_ops, scx_qmap);
|
|
|
|
link = SCX_OPS_ATTACH(skel, qmap_ops, scx_qmap);
|
|
|
|
|
|
|
|
while (!exit_req && !UEI_EXITED(skel, uei)) {
|
|
|
|
long nr_enqueued = skel->bss->nr_enqueued;
|
|
|
|
long nr_dispatched = skel->bss->nr_dispatched;
|
|
|
|
|
|
|
|
printf("stats : enq=%lu dsp=%lu delta=%ld deq=%"PRIu64"\n",
|
|
|
|
nr_enqueued, nr_dispatched, nr_enqueued - nr_dispatched,
|
|
|
|
skel->bss->nr_dequeued);
|
|
|
|
fflush(stdout);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bpf_link__destroy(link);
|
|
|
|
UEI_REPORT(skel, uei);
|
|
|
|
scx_qmap__destroy(skel);
|
|
|
|
return 0;
|
|
|
|
}
|