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 <signal.h> 10 #include <libgen.h> 11 #include <bpf/bpf.h> 12 #include <scx/common.h> 13 #include "scx_simple.bpf.skel.h" 14 15 const char help_fmt[] = 16 "A simple sched_ext scheduler.\n" 17 "\n" 18 "See the top-level comment in .bpf.c for more details.\n" 19 "\n" 20 "Usage: %s [-v]\n" 21 "\n" 22 " -v Print libbpf debug messages\n" 23 " -h Display this help and exit\n"; 24 25 static bool verbose; 26 static volatile int exit_req; 27 28 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) 29 { 30 if (level == LIBBPF_DEBUG && !verbose) 31 return 0; 32 return vfprintf(stderr, format, args); 33 } 34 35 static void sigint_handler(int simple) 36 { 37 exit_req = 1; 38 } 39 40 static void read_stats(struct scx_simple *skel, __u64 *stats) 41 { 42 int nr_cpus = libbpf_num_possible_cpus(); 43 __u64 cnts[2][nr_cpus]; 44 __u32 idx; 45 46 memset(stats, 0, sizeof(stats[0]) * 2); 47 48 for (idx = 0; idx < 2; idx++) { 49 int ret, cpu; 50 51 ret = bpf_map_lookup_elem(bpf_map__fd(skel->maps.stats), 52 &idx, cnts[idx]); 53 if (ret < 0) 54 continue; 55 for (cpu = 0; cpu < nr_cpus; cpu++) 56 stats[idx] += cnts[idx][cpu]; 57 } 58 } 59 60 int main(int argc, char **argv) 61 { 62 struct scx_simple *skel; 63 struct bpf_link *link; 64 __u32 opt; 65 66 libbpf_set_print(libbpf_print_fn); 67 signal(SIGINT, sigint_handler); 68 signal(SIGTERM, sigint_handler); 69 70 skel = SCX_OPS_OPEN(simple_ops, scx_simple); 71 72 while ((opt = getopt(argc, argv, "vh")) != -1) { 73 switch (opt) { 74 case 'v': 75 verbose = true; 76 break; 77 default: 78 fprintf(stderr, help_fmt, basename(argv[0])); 79 return opt != 'h'; 80 } 81 } 82 83 SCX_OPS_LOAD(skel, simple_ops, scx_simple, uei); 84 link = SCX_OPS_ATTACH(skel, simple_ops, scx_simple); 85 86 while (!exit_req && !UEI_EXITED(skel, uei)) { 87 __u64 stats[2]; 88 89 read_stats(skel, stats); 90 printf("local=%llu global=%llu\n", stats[0], stats[1]); 91 fflush(stdout); 92 sleep(1); 93 } 94 95 bpf_link__destroy(link); 96 UEI_REPORT(skel, uei); 97 scx_simple__destroy(skel); 98 return 0; 99 } 100