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 assert(skel->rodata->nr_cpu_ids > 0); 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 bpf_map__set_max_entries(skel->maps.pair_ctx, skel->rodata->nr_cpu_ids / 2); 80 81 /* Resize arrays so their element count is equal to cpu count. */ 82 RESIZE_ARRAY(skel, rodata, pair_cpu, skel->rodata->nr_cpu_ids); 83 RESIZE_ARRAY(skel, rodata, pair_id, skel->rodata->nr_cpu_ids); 84 RESIZE_ARRAY(skel, rodata, in_pair_idx, skel->rodata->nr_cpu_ids); 85 86 for (i = 0; i < skel->rodata->nr_cpu_ids; i++) 87 skel->rodata_pair_cpu->pair_cpu[i] = -1; 88 89 printf("Pairs: "); 90 for (i = 0; i < skel->rodata->nr_cpu_ids; i++) { 91 int j = (i + stride) % skel->rodata->nr_cpu_ids; 92 93 if (skel->rodata_pair_cpu->pair_cpu[i] >= 0) 94 continue; 95 96 SCX_BUG_ON(i == j, 97 "Invalid stride %d - CPU%d wants to be its own pair", 98 stride, i); 99 100 SCX_BUG_ON(skel->rodata_pair_cpu->pair_cpu[j] >= 0, 101 "Invalid stride %d - three CPUs (%d, %d, %d) want to be a pair", 102 stride, i, j, skel->rodata_pair_cpu->pair_cpu[j]); 103 104 skel->rodata_pair_cpu->pair_cpu[i] = j; 105 skel->rodata_pair_cpu->pair_cpu[j] = i; 106 skel->rodata_pair_id->pair_id[i] = i; 107 skel->rodata_pair_id->pair_id[j] = i; 108 skel->rodata_in_pair_idx->in_pair_idx[i] = 0; 109 skel->rodata_in_pair_idx->in_pair_idx[j] = 1; 110 111 printf("[%d, %d] ", i, j); 112 } 113 printf("\n"); 114 115 SCX_OPS_LOAD(skel, pair_ops, scx_pair, uei); 116 117 /* 118 * Populate the cgrp_q_arr map which is an array containing per-cgroup 119 * queues. It'd probably be better to do this from BPF but there are too 120 * many to initialize statically and there's no way to dynamically 121 * populate from BPF. 122 */ 123 outer_fd = bpf_map__fd(skel->maps.cgrp_q_arr); 124 SCX_BUG_ON(outer_fd < 0, "Failed to get outer_fd: %d", outer_fd); 125 126 printf("Initializing"); 127 for (i = 0; i < MAX_CGRPS; i++) { 128 __s32 inner_fd; 129 130 if (exit_req) 131 break; 132 133 inner_fd = bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 0, 134 sizeof(__u32), MAX_QUEUED, NULL); 135 SCX_BUG_ON(inner_fd < 0, "Failed to get inner_fd: %d", 136 inner_fd); 137 SCX_BUG_ON(bpf_map_update_elem(outer_fd, &i, &inner_fd, BPF_ANY), 138 "Failed to set inner map"); 139 close(inner_fd); 140 141 if (!(i % 10)) 142 printf("."); 143 fflush(stdout); 144 } 145 printf("\n"); 146 147 /* 148 * Fully initialized, attach and run. 149 */ 150 link = SCX_OPS_ATTACH(skel, pair_ops, scx_pair); 151 152 while (!exit_req && !UEI_EXITED(skel, uei)) { 153 printf("[SEQ %llu]\n", seq++); 154 printf(" total:%10" PRIu64 " dispatch:%10" PRIu64 " missing:%10" PRIu64 "\n", 155 skel->bss->nr_total, 156 skel->bss->nr_dispatched, 157 skel->bss->nr_missing); 158 printf(" kicks:%10" PRIu64 " preemptions:%7" PRIu64 "\n", 159 skel->bss->nr_kicks, 160 skel->bss->nr_preemptions); 161 printf(" exp:%10" PRIu64 " exp_wait:%10" PRIu64 " exp_empty:%10" PRIu64 "\n", 162 skel->bss->nr_exps, 163 skel->bss->nr_exp_waits, 164 skel->bss->nr_exp_empty); 165 printf("cgnext:%10" PRIu64 " cgcoll:%10" PRIu64 " cgempty:%10" PRIu64 "\n", 166 skel->bss->nr_cgrp_next, 167 skel->bss->nr_cgrp_coll, 168 skel->bss->nr_cgrp_empty); 169 fflush(stdout); 170 sleep(1); 171 } 172 173 bpf_link__destroy(link); 174 ecode = UEI_REPORT(skel, uei); 175 scx_pair__destroy(skel); 176 177 if (UEI_ECODE_RESTART(ecode)) 178 goto restart; 179 return 0; 180 } 181