xref: /linux/tools/sched_ext/scx_qmap.c (revision 8195136669661fdfe54e9a8923c33b31c92fc1da)
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] [-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 to trace_pipe every second, use with -b\n"
32 "  -d PID        Disallow a process from switching into SCHED_EXT (-1 for self)\n"
33 "  -D LEN        Set scx_exit_info.dump buffer length\n"
34 "  -S            Suppress qmap-specific debug dump\n"
35 "  -p            Switch only tasks on SCHED_EXT policy instead of all\n"
36 "  -v            Print libbpf debug messages\n"
37 "  -h            Display this help and exit\n";
38 
39 static bool verbose;
40 static volatile int exit_req;
41 
42 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
43 {
44 	if (level == LIBBPF_DEBUG && !verbose)
45 		return 0;
46 	return vfprintf(stderr, format, args);
47 }
48 
49 static void sigint_handler(int dummy)
50 {
51 	exit_req = 1;
52 }
53 
54 int main(int argc, char **argv)
55 {
56 	struct scx_qmap *skel;
57 	struct bpf_link *link;
58 	int opt;
59 
60 	libbpf_set_print(libbpf_print_fn);
61 	signal(SIGINT, sigint_handler);
62 	signal(SIGTERM, sigint_handler);
63 
64 	skel = SCX_OPS_OPEN(qmap_ops, scx_qmap);
65 
66 	while ((opt = getopt(argc, argv, "s:e:t:T:l:b:Pd:D:Spvh")) != -1) {
67 		switch (opt) {
68 		case 's':
69 			skel->rodata->slice_ns = strtoull(optarg, NULL, 0) * 1000;
70 			break;
71 		case 'e':
72 			skel->bss->test_error_cnt = strtoul(optarg, NULL, 0);
73 			break;
74 		case 't':
75 			skel->rodata->stall_user_nth = strtoul(optarg, NULL, 0);
76 			break;
77 		case 'T':
78 			skel->rodata->stall_kernel_nth = strtoul(optarg, NULL, 0);
79 			break;
80 		case 'l':
81 			skel->rodata->dsp_inf_loop_after = strtoul(optarg, NULL, 0);
82 			break;
83 		case 'b':
84 			skel->rodata->dsp_batch = strtoul(optarg, NULL, 0);
85 			break;
86 		case 'P':
87 			skel->rodata->print_shared_dsq = true;
88 			break;
89 		case 'd':
90 			skel->rodata->disallow_tgid = strtol(optarg, NULL, 0);
91 			if (skel->rodata->disallow_tgid < 0)
92 				skel->rodata->disallow_tgid = getpid();
93 			break;
94 		case 'D':
95 			skel->struct_ops.qmap_ops->exit_dump_len = strtoul(optarg, NULL, 0);
96 			break;
97 		case 'S':
98 			skel->rodata->suppress_dump = true;
99 			break;
100 		case 'p':
101 			skel->struct_ops.qmap_ops->flags |= SCX_OPS_SWITCH_PARTIAL;
102 			break;
103 		case 'v':
104 			verbose = true;
105 			break;
106 		default:
107 			fprintf(stderr, help_fmt, basename(argv[0]));
108 			return opt != 'h';
109 		}
110 	}
111 
112 	SCX_OPS_LOAD(skel, qmap_ops, scx_qmap, uei);
113 	link = SCX_OPS_ATTACH(skel, qmap_ops, scx_qmap);
114 
115 	while (!exit_req && !UEI_EXITED(skel, uei)) {
116 		long nr_enqueued = skel->bss->nr_enqueued;
117 		long nr_dispatched = skel->bss->nr_dispatched;
118 
119 		printf("stats  : enq=%lu dsp=%lu delta=%ld reenq=%"PRIu64" deq=%"PRIu64" core=%"PRIu64" enq_ddsp=%"PRIu64"\n",
120 		       nr_enqueued, nr_dispatched, nr_enqueued - nr_dispatched,
121 		       skel->bss->nr_reenqueued, skel->bss->nr_dequeued,
122 		       skel->bss->nr_core_sched_execed,
123 		       skel->bss->nr_ddsp_from_enq);
124 		if (__COMPAT_has_ksym("scx_bpf_cpuperf_cur"))
125 			printf("cpuperf: cur min/avg/max=%u/%u/%u target min/avg/max=%u/%u/%u\n",
126 			       skel->bss->cpuperf_min,
127 			       skel->bss->cpuperf_avg,
128 			       skel->bss->cpuperf_max,
129 			       skel->bss->cpuperf_target_min,
130 			       skel->bss->cpuperf_target_avg,
131 			       skel->bss->cpuperf_target_max);
132 		fflush(stdout);
133 		sleep(1);
134 	}
135 
136 	bpf_link__destroy(link);
137 	UEI_REPORT(skel, uei);
138 	scx_qmap__destroy(skel);
139 	/*
140 	 * scx_qmap implements ops.cpu_on/offline() and doesn't need to restart
141 	 * on CPU hotplug events.
142 	 */
143 	return 0;
144 }
145