| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 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> |
| 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <unistd.h> |
| 10 | #include <inttypes.h> |
| 11 | #include <signal.h> |
| 12 | #include <libgen.h> |
| 13 | #include <bpf/bpf.h> |
| 14 | #include <scx/common.h> |
| 15 | #include "scx_qmap.bpf.skel.h" |
| 16 | |
| 17 | const char help_fmt[] = |
| 18 | "A simple five-level FIFO queue sched_ext scheduler.\n" |
| 19 | "\n" |
| 20 | "See the top-level comment in .bpf.c for more details.\n" |
| 21 | "\n" |
| 22 | "Usage: %s [-s SLICE_US] [-e COUNT] [-t COUNT] [-T COUNT] [-l COUNT] [-b COUNT]\n" |
| 23 | " [-P] [-M] [-d PID] [-D LEN] [-p] [-v]\n" |
| 24 | "\n" |
| 25 | " -s SLICE_US Override slice duration\n" |
| 26 | " -e COUNT Trigger scx_bpf_error() after COUNT enqueues\n" |
| 27 | " -t COUNT Stall every COUNT'th user thread\n" |
| 28 | " -T COUNT Stall every COUNT'th kernel thread\n" |
| 29 | " -l COUNT Trigger dispatch infinite looping after COUNT dispatches\n" |
| 30 | " -b COUNT Dispatch upto COUNT tasks together\n" |
| 31 | " -P Print out DSQ content and event counters to trace_pipe every second\n" |
| 32 | " -M Print out debug messages to trace_pipe\n" |
| 33 | " -H Boost nice -20 tasks in SHARED_DSQ, use with -b\n" |
| 34 | " -d PID Disallow a process from switching into SCHED_EXT (-1 for self)\n" |
| 35 | " -D LEN Set scx_exit_info.dump buffer length\n" |
| 36 | " -S Suppress qmap-specific debug dump\n" |
| 37 | " -p Switch only tasks on SCHED_EXT policy instead of all\n" |
| 38 | " -v Print libbpf debug messages\n" |
| 39 | " -h Display this help and exit\n" ; |
| 40 | |
| 41 | static bool verbose; |
| 42 | static volatile int exit_req; |
| 43 | |
| 44 | static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) |
| 45 | { |
| 46 | if (level == LIBBPF_DEBUG && !verbose) |
| 47 | return 0; |
| 48 | return vfprintf(stderr, format, args); |
| 49 | } |
| 50 | |
| 51 | static void sigint_handler(int dummy) |
| 52 | { |
| 53 | exit_req = 1; |
| 54 | } |
| 55 | |
| 56 | int main(int argc, char **argv) |
| 57 | { |
| 58 | struct scx_qmap *skel; |
| 59 | struct bpf_link *link; |
| 60 | int opt; |
| 61 | |
| 62 | libbpf_set_print(libbpf_print_fn); |
| 63 | signal(SIGINT, sigint_handler); |
| 64 | signal(SIGTERM, sigint_handler); |
| 65 | |
| 66 | skel = SCX_OPS_OPEN(qmap_ops, scx_qmap); |
| 67 | |
| 68 | skel->rodata->slice_ns = __COMPAT_ENUM_OR_ZERO("scx_public_consts" , "SCX_SLICE_DFL" ); |
| 69 | |
| 70 | while ((opt = getopt(argc, argv, "s:e:t:T:l:b:PMHd:D:Spvh" )) != -1) { |
| 71 | switch (opt) { |
| 72 | case 's': |
| 73 | skel->rodata->slice_ns = strtoull(optarg, NULL, 0) * 1000; |
| 74 | break; |
| 75 | case 'e': |
| 76 | skel->bss->test_error_cnt = strtoul(optarg, NULL, 0); |
| 77 | break; |
| 78 | case 't': |
| 79 | skel->rodata->stall_user_nth = strtoul(optarg, NULL, 0); |
| 80 | break; |
| 81 | case 'T': |
| 82 | skel->rodata->stall_kernel_nth = strtoul(optarg, NULL, 0); |
| 83 | break; |
| 84 | case 'l': |
| 85 | skel->rodata->dsp_inf_loop_after = strtoul(optarg, NULL, 0); |
| 86 | break; |
| 87 | case 'b': |
| 88 | skel->rodata->dsp_batch = strtoul(optarg, NULL, 0); |
| 89 | break; |
| 90 | case 'P': |
| 91 | skel->rodata->print_dsqs_and_events = true; |
| 92 | break; |
| 93 | case 'M': |
| 94 | skel->rodata->print_msgs = true; |
| 95 | break; |
| 96 | case 'H': |
| 97 | skel->rodata->highpri_boosting = true; |
| 98 | break; |
| 99 | case 'd': |
| 100 | skel->rodata->disallow_tgid = strtol(optarg, NULL, 0); |
| 101 | if (skel->rodata->disallow_tgid < 0) |
| 102 | skel->rodata->disallow_tgid = getpid(); |
| 103 | break; |
| 104 | case 'D': |
| 105 | skel->struct_ops.qmap_ops->exit_dump_len = strtoul(optarg, NULL, 0); |
| 106 | break; |
| 107 | case 'S': |
| 108 | skel->rodata->suppress_dump = true; |
| 109 | break; |
| 110 | case 'p': |
| 111 | skel->struct_ops.qmap_ops->flags |= SCX_OPS_SWITCH_PARTIAL; |
| 112 | break; |
| 113 | case 'v': |
| 114 | verbose = true; |
| 115 | break; |
| 116 | default: |
| 117 | fprintf(stderr, help_fmt, basename(argv[0])); |
| 118 | return opt != 'h'; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | SCX_OPS_LOAD(skel, qmap_ops, scx_qmap, uei); |
| 123 | link = SCX_OPS_ATTACH(skel, qmap_ops, scx_qmap); |
| 124 | |
| 125 | while (!exit_req && !UEI_EXITED(skel, uei)) { |
| 126 | long nr_enqueued = skel->bss->nr_enqueued; |
| 127 | long nr_dispatched = skel->bss->nr_dispatched; |
| 128 | |
| 129 | printf("stats : enq=%lu dsp=%lu delta=%ld reenq=%" PRIu64" deq=%" PRIu64" core=%" PRIu64" enq_ddsp=%" PRIu64"\n" , |
| 130 | nr_enqueued, nr_dispatched, nr_enqueued - nr_dispatched, |
| 131 | skel->bss->nr_reenqueued, skel->bss->nr_dequeued, |
| 132 | skel->bss->nr_core_sched_execed, |
| 133 | skel->bss->nr_ddsp_from_enq); |
| 134 | printf(" exp_local=%" PRIu64" exp_remote=%" PRIu64" exp_timer=%" PRIu64" exp_lost=%" PRIu64"\n" , |
| 135 | skel->bss->nr_expedited_local, |
| 136 | skel->bss->nr_expedited_remote, |
| 137 | skel->bss->nr_expedited_from_timer, |
| 138 | skel->bss->nr_expedited_lost); |
| 139 | if (__COMPAT_has_ksym("scx_bpf_cpuperf_cur" )) |
| 140 | printf("cpuperf: cur min/avg/max=%u/%u/%u target min/avg/max=%u/%u/%u\n" , |
| 141 | skel->bss->cpuperf_min, |
| 142 | skel->bss->cpuperf_avg, |
| 143 | skel->bss->cpuperf_max, |
| 144 | skel->bss->cpuperf_target_min, |
| 145 | skel->bss->cpuperf_target_avg, |
| 146 | skel->bss->cpuperf_target_max); |
| 147 | fflush(stdout); |
| 148 | sleep(1); |
| 149 | } |
| 150 | |
| 151 | bpf_link__destroy(link); |
| 152 | UEI_REPORT(skel, uei); |
| 153 | scx_qmap__destroy(skel); |
| 154 | /* |
| 155 | * scx_qmap implements ops.cpu_on/offline() and doesn't need to restart |
| 156 | * on CPU hotplug events. |
| 157 | */ |
| 158 | return 0; |
| 159 | } |
| 160 | |