~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/sched_ext/scx_central.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 #define _GNU_SOURCE
  8 #include <sched.h>
  9 #include <stdio.h>
 10 #include <unistd.h>
 11 #include <inttypes.h>
 12 #include <signal.h>
 13 #include <libgen.h>
 14 #include <bpf/bpf.h>
 15 #include <scx/common.h>
 16 #include "scx_central.bpf.skel.h"
 17 
 18 const char help_fmt[] =
 19 "A central FIFO sched_ext scheduler.\n"
 20 "\n"
 21 "See the top-level comment in .bpf.c for more details.\n"
 22 "\n"
 23 "Usage: %s [-s SLICE_US] [-c CPU]\n"
 24 "\n"
 25 "  -s SLICE_US   Override slice duration\n"
 26 "  -c CPU        Override the central CPU (default: 0)\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_central *skel;
 48         struct bpf_link *link;
 49         __u64 seq = 0, ecode;
 50         __s32 opt;
 51         cpu_set_t *cpuset;
 52 
 53         libbpf_set_print(libbpf_print_fn);
 54         signal(SIGINT, sigint_handler);
 55         signal(SIGTERM, sigint_handler);
 56 restart:
 57         skel = SCX_OPS_OPEN(central_ops, scx_central);
 58 
 59         skel->rodata->central_cpu = 0;
 60         skel->rodata->nr_cpu_ids = libbpf_num_possible_cpus();
 61 
 62         while ((opt = getopt(argc, argv, "s:c:pvh")) != -1) {
 63                 switch (opt) {
 64                 case 's':
 65                         skel->rodata->slice_ns = strtoull(optarg, NULL, 0) * 1000;
 66                         break;
 67                 case 'c':
 68                         skel->rodata->central_cpu = 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         /* Resize arrays so their element count is equal to cpu count. */
 80         RESIZE_ARRAY(skel, data, cpu_gimme_task, skel->rodata->nr_cpu_ids);
 81         RESIZE_ARRAY(skel, data, cpu_started_at, skel->rodata->nr_cpu_ids);
 82 
 83         SCX_OPS_LOAD(skel, central_ops, scx_central, uei);
 84 
 85         /*
 86          * Affinitize the loading thread to the central CPU, as:
 87          * - That's where the BPF timer is first invoked in the BPF program.
 88          * - We probably don't want this user space component to take up a core
 89          *   from a task that would benefit from avoiding preemption on one of
 90          *   the tickless cores.
 91          *
 92          * Until BPF supports pinning the timer, it's not guaranteed that it
 93          * will always be invoked on the central CPU. In practice, this
 94          * suffices the majority of the time.
 95          */
 96         cpuset = CPU_ALLOC(skel->rodata->nr_cpu_ids);
 97         SCX_BUG_ON(!cpuset, "Failed to allocate cpuset");
 98         CPU_ZERO(cpuset);
 99         CPU_SET(skel->rodata->central_cpu, cpuset);
100         SCX_BUG_ON(sched_setaffinity(0, sizeof(cpuset), cpuset),
101                    "Failed to affinitize to central CPU %d (max %d)",
102                    skel->rodata->central_cpu, skel->rodata->nr_cpu_ids - 1);
103         CPU_FREE(cpuset);
104 
105         link = SCX_OPS_ATTACH(skel, central_ops, scx_central);
106 
107         if (!skel->data->timer_pinned)
108                 printf("WARNING : BPF_F_TIMER_CPU_PIN not available, timer not pinned to central\n");
109 
110         while (!exit_req && !UEI_EXITED(skel, uei)) {
111                 printf("[SEQ %llu]\n", seq++);
112                 printf("total   :%10" PRIu64 "    local:%10" PRIu64 "   queued:%10" PRIu64 "  lost:%10" PRIu64 "\n",
113                        skel->bss->nr_total,
114                        skel->bss->nr_locals,
115                        skel->bss->nr_queued,
116                        skel->bss->nr_lost_pids);
117                 printf("timer   :%10" PRIu64 " dispatch:%10" PRIu64 " mismatch:%10" PRIu64 " retry:%10" PRIu64 "\n",
118                        skel->bss->nr_timers,
119                        skel->bss->nr_dispatches,
120                        skel->bss->nr_mismatches,
121                        skel->bss->nr_retries);
122                 printf("overflow:%10" PRIu64 "\n",
123                        skel->bss->nr_overflows);
124                 fflush(stdout);
125                 sleep(1);
126         }
127 
128         bpf_link__destroy(link);
129         ecode = UEI_REPORT(skel, uei);
130         scx_central__destroy(skel);
131 
132         if (UEI_ECODE_RESTART(ecode))
133                 goto restart;
134         return 0;
135 }
136 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php