xref: /linux/tools/sched_ext/scx_pair.c (revision 625be3456b3ced6e2dca6166962c0cf6cc2e546d)
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 <unistd.h>
9 #include <inttypes.h>
10 #include <signal.h>
11 #include <assert.h>
12 #include <libgen.h>
13 #include <bpf/bpf.h>
14 #include <scx/common.h>
15 #include "scx_pair.h"
16 #include "scx_pair.bpf.skel.h"
17 
18 const char help_fmt[] =
19 "A demo sched_ext core-scheduler which always makes every sibling CPU pair\n"
20 "execute from the same CPU cgroup.\n"
21 "\n"
22 "See the top-level comment in .bpf.c for more details.\n"
23 "\n"
24 "Usage: %s [-S STRIDE]\n"
25 "\n"
26 "  -S STRIDE     Override CPU pair stride (default: nr_cpus_ids / 2)\n"
27 "  -v            Print libbpf debug messages\n"
28 "  -h            Display this help and exit\n";
29 
30 static bool verbose;
31 static volatile int exit_req;
32 
33 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
34 {
35 	if (level == LIBBPF_DEBUG && !verbose)
36 		return 0;
37 	return vfprintf(stderr, format, args);
38 }
39 
40 static void sigint_handler(int dummy)
41 {
42 	exit_req = 1;
43 }
44 
45 int main(int argc, char **argv)
46 {
47 	struct scx_pair *skel;
48 	struct bpf_link *link;
49 	__u64 seq = 0, ecode;
50 	__s32 stride, i, opt, outer_fd;
51 
52 	libbpf_set_print(libbpf_print_fn);
53 	signal(SIGINT, sigint_handler);
54 	signal(SIGTERM, sigint_handler);
55 restart:
56 	skel = SCX_OPS_OPEN(pair_ops, scx_pair);
57 
58 	skel->rodata->nr_cpu_ids = libbpf_num_possible_cpus();
59 	skel->rodata->pair_batch_dur_ns = __COMPAT_ENUM_OR_ZERO("scx_public_consts", "SCX_SLICE_DFL");
60 
61 	/* pair up the earlier half to the latter by default, override with -s */
62 	stride = skel->rodata->nr_cpu_ids / 2;
63 
64 	while ((opt = getopt(argc, argv, "S:vh")) != -1) {
65 		switch (opt) {
66 		case 'S':
67 			stride = strtoul(optarg, NULL, 0);
68 			break;
69 		case 'v':
70 			verbose = true;
71 			break;
72 		default:
73 			fprintf(stderr, help_fmt, basename(argv[0]));
74 			return opt != 'h';
75 		}
76 	}
77 
78 	/* Stride must be positive to pair distinct CPUs. */
79 	if (stride <= 0) {
80 		fprintf(stderr, "Invalid stride %d, must be positive\n", stride);
81 		scx_pair__destroy(skel);
82 		return -1;
83 	}
84 	bpf_map__set_max_entries(skel->maps.pair_ctx, skel->rodata->nr_cpu_ids / 2);
85 
86 	/* Resize arrays so their element count is equal to cpu count. */
87 	RESIZE_ARRAY(skel, rodata, pair_cpu, skel->rodata->nr_cpu_ids);
88 	RESIZE_ARRAY(skel, rodata, pair_id, skel->rodata->nr_cpu_ids);
89 	RESIZE_ARRAY(skel, rodata, in_pair_idx, skel->rodata->nr_cpu_ids);
90 
91 	for (i = 0; i < skel->rodata->nr_cpu_ids; i++)
92 		skel->rodata_pair_cpu->pair_cpu[i] = -1;
93 
94 	printf("Pairs: ");
95 	for (i = 0; i < skel->rodata->nr_cpu_ids; i++) {
96 		int j = (i + stride) % skel->rodata->nr_cpu_ids;
97 
98 		if (skel->rodata_pair_cpu->pair_cpu[i] >= 0)
99 			continue;
100 
101 		SCX_BUG_ON(i == j,
102 			   "Invalid stride %d - CPU%d wants to be its own pair",
103 			   stride, i);
104 
105 		SCX_BUG_ON(skel->rodata_pair_cpu->pair_cpu[j] >= 0,
106 			   "Invalid stride %d - three CPUs (%d, %d, %d) want to be a pair",
107 			   stride, i, j, skel->rodata_pair_cpu->pair_cpu[j]);
108 
109 		skel->rodata_pair_cpu->pair_cpu[i] = j;
110 		skel->rodata_pair_cpu->pair_cpu[j] = i;
111 		skel->rodata_pair_id->pair_id[i] = i;
112 		skel->rodata_pair_id->pair_id[j] = i;
113 		skel->rodata_in_pair_idx->in_pair_idx[i] = 0;
114 		skel->rodata_in_pair_idx->in_pair_idx[j] = 1;
115 
116 		printf("[%d, %d] ", i, j);
117 	}
118 	printf("\n");
119 
120 	SCX_OPS_LOAD(skel, pair_ops, scx_pair, uei);
121 
122 	/*
123 	 * Populate the cgrp_q_arr map which is an array containing per-cgroup
124 	 * queues. It'd probably be better to do this from BPF but there are too
125 	 * many to initialize statically and there's no way to dynamically
126 	 * populate from BPF.
127 	 */
128 	outer_fd = bpf_map__fd(skel->maps.cgrp_q_arr);
129 	SCX_BUG_ON(outer_fd < 0, "Failed to get outer_fd: %d", outer_fd);
130 
131 	printf("Initializing");
132         for (i = 0; i < MAX_CGRPS; i++) {
133 		__s32 inner_fd;
134 
135 		if (exit_req)
136 			break;
137 
138 		inner_fd = bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 0,
139 					  sizeof(__u32), MAX_QUEUED, NULL);
140 		SCX_BUG_ON(inner_fd < 0, "Failed to get inner_fd: %d",
141 			   inner_fd);
142 		SCX_BUG_ON(bpf_map_update_elem(outer_fd, &i, &inner_fd, BPF_ANY),
143 			   "Failed to set inner map");
144 		close(inner_fd);
145 
146 		if (!(i % 10))
147 			printf(".");
148 		fflush(stdout);
149         }
150 	printf("\n");
151 
152 	/*
153 	 * Fully initialized, attach and run.
154 	 */
155 	link = SCX_OPS_ATTACH(skel, pair_ops, scx_pair);
156 
157 	while (!exit_req && !UEI_EXITED(skel, uei)) {
158 		printf("[SEQ %llu]\n", seq++);
159 		printf(" total:%10" PRIu64 " dispatch:%10" PRIu64 "   missing:%10" PRIu64 "\n",
160 		       skel->bss->nr_total,
161 		       skel->bss->nr_dispatched,
162 		       skel->bss->nr_missing);
163 		printf(" kicks:%10" PRIu64 " preemptions:%7" PRIu64 "\n",
164 		       skel->bss->nr_kicks,
165 		       skel->bss->nr_preemptions);
166 		printf("   exp:%10" PRIu64 " exp_wait:%10" PRIu64 " exp_empty:%10" PRIu64 "\n",
167 		       skel->bss->nr_exps,
168 		       skel->bss->nr_exp_waits,
169 		       skel->bss->nr_exp_empty);
170 		printf("cgnext:%10" PRIu64 "   cgcoll:%10" PRIu64 "   cgempty:%10" PRIu64 "\n",
171 		       skel->bss->nr_cgrp_next,
172 		       skel->bss->nr_cgrp_coll,
173 		       skel->bss->nr_cgrp_empty);
174 		fflush(stdout);
175 		sleep(1);
176 	}
177 
178 	bpf_link__destroy(link);
179 	ecode = UEI_REPORT(skel, uei);
180 	scx_pair__destroy(skel);
181 
182 	if (UEI_ECODE_RESTART(ecode))
183 		goto restart;
184 	return 0;
185 }
186