xref: /linux/tools/sched_ext/scx_qmap.c (revision 98d709cba3193f0bec54da4cd76ef499ea2f1ef7)
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 
18 const 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] [-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 "  -v            Print libbpf debug messages\n"
40 "  -h            Display this help and exit\n";
41 
42 static bool verbose;
43 static volatile int exit_req;
44 
45 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
46 {
47 	if (level == LIBBPF_DEBUG && !verbose)
48 		return 0;
49 	return vfprintf(stderr, format, args);
50 }
51 
52 static void sigint_handler(int dummy)
53 {
54 	exit_req = 1;
55 }
56 
57 int main(int argc, char **argv)
58 {
59 	struct scx_qmap *skel;
60 	struct bpf_link *link;
61 	int opt;
62 
63 	libbpf_set_print(libbpf_print_fn);
64 	signal(SIGINT, sigint_handler);
65 	signal(SIGTERM, sigint_handler);
66 
67 	skel = SCX_OPS_OPEN(qmap_ops, scx_qmap);
68 
69 	skel->rodata->slice_ns = __COMPAT_ENUM_OR_ZERO("scx_public_consts", "SCX_SLICE_DFL");
70 
71 	while ((opt = getopt(argc, argv, "s:e:t:T:l:b:PMHc:d:D:Spvh")) != -1) {
72 		switch (opt) {
73 		case 's':
74 			skel->rodata->slice_ns = strtoull(optarg, NULL, 0) * 1000;
75 			break;
76 		case 'e':
77 			skel->bss->test_error_cnt = strtoul(optarg, NULL, 0);
78 			break;
79 		case 't':
80 			skel->rodata->stall_user_nth = strtoul(optarg, NULL, 0);
81 			break;
82 		case 'T':
83 			skel->rodata->stall_kernel_nth = strtoul(optarg, NULL, 0);
84 			break;
85 		case 'l':
86 			skel->rodata->dsp_inf_loop_after = strtoul(optarg, NULL, 0);
87 			break;
88 		case 'b':
89 			skel->rodata->dsp_batch = strtoul(optarg, NULL, 0);
90 			break;
91 		case 'P':
92 			skel->rodata->print_dsqs_and_events = true;
93 			break;
94 		case 'M':
95 			skel->rodata->print_msgs = true;
96 			break;
97 		case 'H':
98 			skel->rodata->highpri_boosting = true;
99 			break;
100 		case 'c': {
101 			struct stat st;
102 			if (stat(optarg, &st) < 0) {
103 				perror("stat");
104 				return 1;
105 			}
106 			skel->struct_ops.qmap_ops->sub_cgroup_id = st.st_ino;
107 			skel->rodata->sub_cgroup_id = st.st_ino;
108 			break;
109 		}
110 		case 'd':
111 			skel->rodata->disallow_tgid = strtol(optarg, NULL, 0);
112 			if (skel->rodata->disallow_tgid < 0)
113 				skel->rodata->disallow_tgid = getpid();
114 			break;
115 		case 'D':
116 			skel->struct_ops.qmap_ops->exit_dump_len = strtoul(optarg, NULL, 0);
117 			break;
118 		case 'S':
119 			skel->rodata->suppress_dump = true;
120 			break;
121 		case 'p':
122 			skel->struct_ops.qmap_ops->flags |= SCX_OPS_SWITCH_PARTIAL;
123 			break;
124 		case 'v':
125 			verbose = true;
126 			break;
127 		default:
128 			fprintf(stderr, help_fmt, basename(argv[0]));
129 			return opt != 'h';
130 		}
131 	}
132 
133 	SCX_OPS_LOAD(skel, qmap_ops, scx_qmap, uei);
134 	link = SCX_OPS_ATTACH(skel, qmap_ops, scx_qmap);
135 
136 	while (!exit_req && !UEI_EXITED(skel, uei)) {
137 		long nr_enqueued = skel->bss->nr_enqueued;
138 		long nr_dispatched = skel->bss->nr_dispatched;
139 
140 		printf("stats  : enq=%lu dsp=%lu delta=%ld reenq/cpu0=%"PRIu64"/%"PRIu64" deq=%"PRIu64" core=%"PRIu64" enq_ddsp=%"PRIu64"\n",
141 		       nr_enqueued, nr_dispatched, nr_enqueued - nr_dispatched,
142 		       skel->bss->nr_reenqueued, skel->bss->nr_reenqueued_cpu0,
143 		       skel->bss->nr_dequeued,
144 		       skel->bss->nr_core_sched_execed,
145 		       skel->bss->nr_ddsp_from_enq);
146 		printf("         exp_local=%"PRIu64" exp_remote=%"PRIu64" exp_timer=%"PRIu64" exp_lost=%"PRIu64"\n",
147 		       skel->bss->nr_expedited_local,
148 		       skel->bss->nr_expedited_remote,
149 		       skel->bss->nr_expedited_from_timer,
150 		       skel->bss->nr_expedited_lost);
151 		if (__COMPAT_has_ksym("scx_bpf_cpuperf_cur"))
152 			printf("cpuperf: cur min/avg/max=%u/%u/%u target min/avg/max=%u/%u/%u\n",
153 			       skel->bss->cpuperf_min,
154 			       skel->bss->cpuperf_avg,
155 			       skel->bss->cpuperf_max,
156 			       skel->bss->cpuperf_target_min,
157 			       skel->bss->cpuperf_target_avg,
158 			       skel->bss->cpuperf_target_max);
159 		fflush(stdout);
160 		sleep(1);
161 	}
162 
163 	bpf_link__destroy(link);
164 	UEI_REPORT(skel, uei);
165 	scx_qmap__destroy(skel);
166 	/*
167 	 * scx_qmap implements ops.cpu_on/offline() and doesn't need to restart
168 	 * on CPU hotplug events.
169 	 */
170 	return 0;
171 }
172