Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/sched_ext/scx_pair.c
121797 views
1
/* SPDX-License-Identifier: GPL-2.0 */
2
/*
3
* Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
4
* Copyright (c) 2022 Tejun Heo <[email protected]>
5
* Copyright (c) 2022 David Vernet <[email protected]>
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
optind = 1;
57
skel = SCX_OPS_OPEN(pair_ops, scx_pair);
58
59
skel->rodata->nr_cpu_ids = libbpf_num_possible_cpus();
60
skel->rodata->pair_batch_dur_ns = __COMPAT_ENUM_OR_ZERO("scx_public_consts", "SCX_SLICE_DFL");
61
62
/* pair up the earlier half to the latter by default, override with -s */
63
stride = skel->rodata->nr_cpu_ids / 2;
64
65
while ((opt = getopt(argc, argv, "S:vh")) != -1) {
66
switch (opt) {
67
case 'S':
68
stride = strtoul(optarg, NULL, 0);
69
break;
70
case 'v':
71
verbose = true;
72
break;
73
default:
74
fprintf(stderr, help_fmt, basename(argv[0]));
75
return opt != 'h';
76
}
77
}
78
79
/* Stride must be positive to pair distinct CPUs. */
80
if (stride <= 0) {
81
fprintf(stderr, "Invalid stride %d, must be positive\n", stride);
82
scx_pair__destroy(skel);
83
return -1;
84
}
85
bpf_map__set_max_entries(skel->maps.pair_ctx, skel->rodata->nr_cpu_ids / 2);
86
87
/* Resize arrays so their element count is equal to cpu count. */
88
RESIZE_ARRAY(skel, rodata, pair_cpu, skel->rodata->nr_cpu_ids);
89
RESIZE_ARRAY(skel, rodata, pair_id, skel->rodata->nr_cpu_ids);
90
RESIZE_ARRAY(skel, rodata, in_pair_idx, skel->rodata->nr_cpu_ids);
91
92
for (i = 0; i < skel->rodata->nr_cpu_ids; i++)
93
skel->rodata_pair_cpu->pair_cpu[i] = -1;
94
95
printf("Pairs: ");
96
for (i = 0; i < skel->rodata->nr_cpu_ids; i++) {
97
int j = (i + stride) % skel->rodata->nr_cpu_ids;
98
99
if (skel->rodata_pair_cpu->pair_cpu[i] >= 0)
100
continue;
101
102
SCX_BUG_ON(i == j,
103
"Invalid stride %d - CPU%d wants to be its own pair",
104
stride, i);
105
106
SCX_BUG_ON(skel->rodata_pair_cpu->pair_cpu[j] >= 0,
107
"Invalid stride %d - three CPUs (%d, %d, %d) want to be a pair",
108
stride, i, j, skel->rodata_pair_cpu->pair_cpu[j]);
109
110
skel->rodata_pair_cpu->pair_cpu[i] = j;
111
skel->rodata_pair_cpu->pair_cpu[j] = i;
112
skel->rodata_pair_id->pair_id[i] = i;
113
skel->rodata_pair_id->pair_id[j] = i;
114
skel->rodata_in_pair_idx->in_pair_idx[i] = 0;
115
skel->rodata_in_pair_idx->in_pair_idx[j] = 1;
116
117
printf("[%d, %d] ", i, j);
118
}
119
printf("\n");
120
121
SCX_OPS_LOAD(skel, pair_ops, scx_pair, uei);
122
123
/*
124
* Populate the cgrp_q_arr map which is an array containing per-cgroup
125
* queues. It'd probably be better to do this from BPF but there are too
126
* many to initialize statically and there's no way to dynamically
127
* populate from BPF.
128
*/
129
outer_fd = bpf_map__fd(skel->maps.cgrp_q_arr);
130
SCX_BUG_ON(outer_fd < 0, "Failed to get outer_fd: %d", outer_fd);
131
132
printf("Initializing");
133
for (i = 0; i < MAX_CGRPS; i++) {
134
__s32 inner_fd;
135
136
if (exit_req)
137
break;
138
139
inner_fd = bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 0,
140
sizeof(__u32), MAX_QUEUED, NULL);
141
SCX_BUG_ON(inner_fd < 0, "Failed to get inner_fd: %d",
142
inner_fd);
143
SCX_BUG_ON(bpf_map_update_elem(outer_fd, &i, &inner_fd, BPF_ANY),
144
"Failed to set inner map");
145
close(inner_fd);
146
147
if (!(i % 10))
148
printf(".");
149
fflush(stdout);
150
}
151
printf("\n");
152
153
/*
154
* Fully initialized, attach and run.
155
*/
156
link = SCX_OPS_ATTACH(skel, pair_ops, scx_pair);
157
158
while (!exit_req && !UEI_EXITED(skel, uei)) {
159
printf("[SEQ %llu]\n", seq++);
160
printf(" total:%10" PRIu64 " dispatch:%10" PRIu64 " missing:%10" PRIu64 "\n",
161
skel->bss->nr_total,
162
skel->bss->nr_dispatched,
163
skel->bss->nr_missing);
164
printf(" kicks:%10" PRIu64 " preemptions:%7" PRIu64 "\n",
165
skel->bss->nr_kicks,
166
skel->bss->nr_preemptions);
167
printf(" exp:%10" PRIu64 " exp_wait:%10" PRIu64 " exp_empty:%10" PRIu64 "\n",
168
skel->bss->nr_exps,
169
skel->bss->nr_exp_waits,
170
skel->bss->nr_exp_empty);
171
printf("cgnext:%10" PRIu64 " cgcoll:%10" PRIu64 " cgempty:%10" PRIu64 "\n",
172
skel->bss->nr_cgrp_next,
173
skel->bss->nr_cgrp_coll,
174
skel->bss->nr_cgrp_empty);
175
fflush(stdout);
176
sleep(1);
177
}
178
179
bpf_link__destroy(link);
180
ecode = UEI_REPORT(skel, uei);
181
scx_pair__destroy(skel);
182
183
if (UEI_ECODE_RESTART(ecode))
184
goto restart;
185
return 0;
186
}
187
188