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