1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 2 /* 3 * Copyright (c) 2022 Meta Platforms, Inc. and 3 * Copyright (c) 2022 Meta Platforms, Inc. and affiliates. 4 * Copyright (c) 2022 Tejun Heo <tj@kernel.org 4 * Copyright (c) 2022 Tejun Heo <tj@kernel.org> 5 * Copyright (c) 2022 David Vernet <dvernet@me 5 * Copyright (c) 2022 David Vernet <dvernet@meta.com> 6 */ 6 */ 7 #include <stdio.h> 7 #include <stdio.h> 8 #include <stdlib.h> 8 #include <stdlib.h> 9 #include <unistd.h> 9 #include <unistd.h> 10 #include <inttypes.h> 10 #include <inttypes.h> 11 #include <signal.h> 11 #include <signal.h> 12 #include <libgen.h> 12 #include <libgen.h> 13 #include <bpf/bpf.h> 13 #include <bpf/bpf.h> 14 #include <scx/common.h> 14 #include <scx/common.h> 15 #include "scx_qmap.bpf.skel.h" 15 #include "scx_qmap.bpf.skel.h" 16 16 17 const char help_fmt[] = 17 const char help_fmt[] = 18 "A simple five-level FIFO queue sched_ext sche 18 "A simple five-level FIFO queue sched_ext scheduler.\n" 19 "\n" 19 "\n" 20 "See the top-level comment in .bpf.c for more 20 "See the top-level comment in .bpf.c for more details.\n" 21 "\n" 21 "\n" 22 "Usage: %s [-s SLICE_US] [-e COUNT] [-t COUNT] 22 "Usage: %s [-s SLICE_US] [-e COUNT] [-t COUNT] [-T COUNT] [-l COUNT] [-b COUNT]\n" 23 " [-P] [-d PID] [-D LEN] [-p] [-v]\n" 23 " [-P] [-d PID] [-D LEN] [-p] [-v]\n" 24 "\n" 24 "\n" 25 " -s SLICE_US Override slice duration\n" 25 " -s SLICE_US Override slice duration\n" 26 " -e COUNT Trigger scx_bpf_error() after 26 " -e COUNT Trigger scx_bpf_error() after COUNT enqueues\n" 27 " -t COUNT Stall every COUNT'th user thr 27 " -t COUNT Stall every COUNT'th user thread\n" 28 " -T COUNT Stall every COUNT'th kernel t 28 " -T COUNT Stall every COUNT'th kernel thread\n" 29 " -l COUNT Trigger dispatch infinite loo 29 " -l COUNT Trigger dispatch infinite looping after COUNT dispatches\n" 30 " -b COUNT Dispatch upto COUNT tasks tog 30 " -b COUNT Dispatch upto COUNT tasks together\n" 31 " -P Print out DSQ content to trac 31 " -P Print out DSQ content to trace_pipe every second, use with -b\n" 32 " -H Boost nice -20 tasks in SHARE 32 " -H Boost nice -20 tasks in SHARED_DSQ, use with -b\n" 33 " -d PID Disallow a process from switc 33 " -d PID Disallow a process from switching into SCHED_EXT (-1 for self)\n" 34 " -D LEN Set scx_exit_info.dump buffer 34 " -D LEN Set scx_exit_info.dump buffer length\n" 35 " -S Suppress qmap-specific debug 35 " -S Suppress qmap-specific debug dump\n" 36 " -p Switch only tasks on SCHED_EX 36 " -p Switch only tasks on SCHED_EXT policy instead of all\n" 37 " -v Print libbpf debug messages\n 37 " -v Print libbpf debug messages\n" 38 " -h Display this help and exit\n" 38 " -h Display this help and exit\n"; 39 39 40 static bool verbose; 40 static bool verbose; 41 static volatile int exit_req; 41 static volatile int exit_req; 42 42 43 static int libbpf_print_fn(enum libbpf_print_l 43 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) 44 { 44 { 45 if (level == LIBBPF_DEBUG && !verbose) 45 if (level == LIBBPF_DEBUG && !verbose) 46 return 0; 46 return 0; 47 return vfprintf(stderr, format, args); 47 return vfprintf(stderr, format, args); 48 } 48 } 49 49 50 static void sigint_handler(int dummy) 50 static void sigint_handler(int dummy) 51 { 51 { 52 exit_req = 1; 52 exit_req = 1; 53 } 53 } 54 54 55 int main(int argc, char **argv) 55 int main(int argc, char **argv) 56 { 56 { 57 struct scx_qmap *skel; 57 struct scx_qmap *skel; 58 struct bpf_link *link; 58 struct bpf_link *link; 59 int opt; 59 int opt; 60 60 61 libbpf_set_print(libbpf_print_fn); 61 libbpf_set_print(libbpf_print_fn); 62 signal(SIGINT, sigint_handler); 62 signal(SIGINT, sigint_handler); 63 signal(SIGTERM, sigint_handler); 63 signal(SIGTERM, sigint_handler); 64 64 65 skel = SCX_OPS_OPEN(qmap_ops, scx_qmap 65 skel = SCX_OPS_OPEN(qmap_ops, scx_qmap); 66 66 67 while ((opt = getopt(argc, argv, "s:e: 67 while ((opt = getopt(argc, argv, "s:e:t:T:l:b:PHd:D:Spvh")) != -1) { 68 switch (opt) { 68 switch (opt) { 69 case 's': 69 case 's': 70 skel->rodata->slice_ns 70 skel->rodata->slice_ns = strtoull(optarg, NULL, 0) * 1000; 71 break; 71 break; 72 case 'e': 72 case 'e': 73 skel->bss->test_error_ 73 skel->bss->test_error_cnt = strtoul(optarg, NULL, 0); 74 break; 74 break; 75 case 't': 75 case 't': 76 skel->rodata->stall_us 76 skel->rodata->stall_user_nth = strtoul(optarg, NULL, 0); 77 break; 77 break; 78 case 'T': 78 case 'T': 79 skel->rodata->stall_ke 79 skel->rodata->stall_kernel_nth = strtoul(optarg, NULL, 0); 80 break; 80 break; 81 case 'l': 81 case 'l': 82 skel->rodata->dsp_inf_ 82 skel->rodata->dsp_inf_loop_after = strtoul(optarg, NULL, 0); 83 break; 83 break; 84 case 'b': 84 case 'b': 85 skel->rodata->dsp_batc 85 skel->rodata->dsp_batch = strtoul(optarg, NULL, 0); 86 break; 86 break; 87 case 'P': 87 case 'P': 88 skel->rodata->print_sh 88 skel->rodata->print_shared_dsq = true; 89 break; 89 break; 90 case 'H': 90 case 'H': 91 skel->rodata->highpri_ 91 skel->rodata->highpri_boosting = true; 92 break; 92 break; 93 case 'd': 93 case 'd': 94 skel->rodata->disallow 94 skel->rodata->disallow_tgid = strtol(optarg, NULL, 0); 95 if (skel->rodata->disa 95 if (skel->rodata->disallow_tgid < 0) 96 skel->rodata-> 96 skel->rodata->disallow_tgid = getpid(); 97 break; 97 break; 98 case 'D': 98 case 'D': 99 skel->struct_ops.qmap_ 99 skel->struct_ops.qmap_ops->exit_dump_len = strtoul(optarg, NULL, 0); 100 break; 100 break; 101 case 'S': 101 case 'S': 102 skel->rodata->suppress 102 skel->rodata->suppress_dump = true; 103 break; 103 break; 104 case 'p': 104 case 'p': 105 skel->struct_ops.qmap_ 105 skel->struct_ops.qmap_ops->flags |= SCX_OPS_SWITCH_PARTIAL; 106 break; 106 break; 107 case 'v': 107 case 'v': 108 verbose = true; 108 verbose = true; 109 break; 109 break; 110 default: 110 default: 111 fprintf(stderr, help_f 111 fprintf(stderr, help_fmt, basename(argv[0])); 112 return opt != 'h'; 112 return opt != 'h'; 113 } 113 } 114 } 114 } 115 115 116 SCX_OPS_LOAD(skel, qmap_ops, scx_qmap, 116 SCX_OPS_LOAD(skel, qmap_ops, scx_qmap, uei); 117 link = SCX_OPS_ATTACH(skel, qmap_ops, 117 link = SCX_OPS_ATTACH(skel, qmap_ops, scx_qmap); 118 118 119 while (!exit_req && !UEI_EXITED(skel, 119 while (!exit_req && !UEI_EXITED(skel, uei)) { 120 long nr_enqueued = skel->bss-> 120 long nr_enqueued = skel->bss->nr_enqueued; 121 long nr_dispatched = skel->bss 121 long nr_dispatched = skel->bss->nr_dispatched; 122 122 123 printf("stats : enq=%lu dsp=% 123 printf("stats : enq=%lu dsp=%lu delta=%ld reenq=%"PRIu64" deq=%"PRIu64" core=%"PRIu64" enq_ddsp=%"PRIu64"\n", 124 nr_enqueued, nr_dispatc 124 nr_enqueued, nr_dispatched, nr_enqueued - nr_dispatched, 125 skel->bss->nr_reenqueue 125 skel->bss->nr_reenqueued, skel->bss->nr_dequeued, 126 skel->bss->nr_core_sche 126 skel->bss->nr_core_sched_execed, 127 skel->bss->nr_ddsp_from 127 skel->bss->nr_ddsp_from_enq); 128 printf(" exp_local=%"P 128 printf(" exp_local=%"PRIu64" exp_remote=%"PRIu64" exp_timer=%"PRIu64" exp_lost=%"PRIu64"\n", 129 skel->bss->nr_expedited 129 skel->bss->nr_expedited_local, 130 skel->bss->nr_expedited 130 skel->bss->nr_expedited_remote, 131 skel->bss->nr_expedited 131 skel->bss->nr_expedited_from_timer, 132 skel->bss->nr_expedited 132 skel->bss->nr_expedited_lost); 133 if (__COMPAT_has_ksym("scx_bpf 133 if (__COMPAT_has_ksym("scx_bpf_cpuperf_cur")) 134 printf("cpuperf: cur m 134 printf("cpuperf: cur min/avg/max=%u/%u/%u target min/avg/max=%u/%u/%u\n", 135 skel->bss->cpup 135 skel->bss->cpuperf_min, 136 skel->bss->cpup 136 skel->bss->cpuperf_avg, 137 skel->bss->cpup 137 skel->bss->cpuperf_max, 138 skel->bss->cpup 138 skel->bss->cpuperf_target_min, 139 skel->bss->cpup 139 skel->bss->cpuperf_target_avg, 140 skel->bss->cpup 140 skel->bss->cpuperf_target_max); 141 fflush(stdout); 141 fflush(stdout); 142 sleep(1); 142 sleep(1); 143 } 143 } 144 144 145 bpf_link__destroy(link); 145 bpf_link__destroy(link); 146 UEI_REPORT(skel, uei); 146 UEI_REPORT(skel, uei); 147 scx_qmap__destroy(skel); 147 scx_qmap__destroy(skel); 148 /* 148 /* 149 * scx_qmap implements ops.cpu_on/offl 149 * scx_qmap implements ops.cpu_on/offline() and doesn't need to restart 150 * on CPU hotplug events. 150 * on CPU hotplug events. 151 */ 151 */ 152 return 0; 152 return 0; 153 } 153 } 154 154
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.