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