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