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

TOMOYO Linux Cross Reference
Linux/tools/sched_ext/scx_flatcg.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) 2023 Meta Platforms, Inc. and affiliates.
  4  * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
  5  * Copyright (c) 2023 David Vernet <dvernet@meta.com>
  6  */
  7 #include <stdio.h>
  8 #include <signal.h>
  9 #include <unistd.h>
 10 #include <libgen.h>
 11 #include <limits.h>
 12 #include <inttypes.h>
 13 #include <fcntl.h>
 14 #include <time.h>
 15 #include <bpf/bpf.h>
 16 #include <scx/common.h>
 17 #include "scx_flatcg.h"
 18 #include "scx_flatcg.bpf.skel.h"
 19 
 20 #ifndef FILEID_KERNFS
 21 #define FILEID_KERNFS           0xfe
 22 #endif
 23 
 24 const char help_fmt[] =
 25 "A flattened cgroup hierarchy sched_ext scheduler.\n"
 26 "\n"
 27 "See the top-level comment in .bpf.c for more details.\n"
 28 "\n"
 29 "Usage: %s [-s SLICE_US] [-i INTERVAL] [-f] [-v]\n"
 30 "\n"
 31 "  -s SLICE_US   Override slice duration\n"
 32 "  -i INTERVAL   Report interval\n"
 33 "  -f            Use FIFO scheduling instead of weighted vtime scheduling\n"
 34 "  -v            Print libbpf debug messages\n"
 35 "  -h            Display this help and exit\n";
 36 
 37 static bool verbose;
 38 static volatile int exit_req;
 39 
 40 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
 41 {
 42         if (level == LIBBPF_DEBUG && !verbose)
 43                 return 0;
 44         return vfprintf(stderr, format, args);
 45 }
 46 
 47 static void sigint_handler(int dummy)
 48 {
 49         exit_req = 1;
 50 }
 51 
 52 static float read_cpu_util(__u64 *last_sum, __u64 *last_idle)
 53 {
 54         FILE *fp;
 55         char buf[4096];
 56         char *line, *cur = NULL, *tok;
 57         __u64 sum = 0, idle = 0;
 58         __u64 delta_sum, delta_idle;
 59         int idx;
 60 
 61         fp = fopen("/proc/stat", "r");
 62         if (!fp) {
 63                 perror("fopen(\"/proc/stat\")");
 64                 return 0.0;
 65         }
 66 
 67         if (!fgets(buf, sizeof(buf), fp)) {
 68                 perror("fgets(\"/proc/stat\")");
 69                 fclose(fp);
 70                 return 0.0;
 71         }
 72         fclose(fp);
 73 
 74         line = buf;
 75         for (idx = 0; (tok = strtok_r(line, " \n", &cur)); idx++) {
 76                 char *endp = NULL;
 77                 __u64 v;
 78 
 79                 if (idx == 0) {
 80                         line = NULL;
 81                         continue;
 82                 }
 83                 v = strtoull(tok, &endp, 0);
 84                 if (!endp || *endp != '\0') {
 85                         fprintf(stderr, "failed to parse %dth field of /proc/stat (\"%s\")\n",
 86                                 idx, tok);
 87                         continue;
 88                 }
 89                 sum += v;
 90                 if (idx == 4)
 91                         idle = v;
 92         }
 93 
 94         delta_sum = sum - *last_sum;
 95         delta_idle = idle - *last_idle;
 96         *last_sum = sum;
 97         *last_idle = idle;
 98 
 99         return delta_sum ? (float)(delta_sum - delta_idle) / delta_sum : 0.0;
100 }
101 
102 static void fcg_read_stats(struct scx_flatcg *skel, __u64 *stats)
103 {
104         __u64 cnts[FCG_NR_STATS][skel->rodata->nr_cpus];
105         __u32 idx;
106 
107         memset(stats, 0, sizeof(stats[0]) * FCG_NR_STATS);
108 
109         for (idx = 0; idx < FCG_NR_STATS; idx++) {
110                 int ret, cpu;
111 
112                 ret = bpf_map_lookup_elem(bpf_map__fd(skel->maps.stats),
113                                           &idx, cnts[idx]);
114                 if (ret < 0)
115                         continue;
116                 for (cpu = 0; cpu < skel->rodata->nr_cpus; cpu++)
117                         stats[idx] += cnts[idx][cpu];
118         }
119 }
120 
121 int main(int argc, char **argv)
122 {
123         struct scx_flatcg *skel;
124         struct bpf_link *link;
125         struct timespec intv_ts = { .tv_sec = 2, .tv_nsec = 0 };
126         bool dump_cgrps = false;
127         __u64 last_cpu_sum = 0, last_cpu_idle = 0;
128         __u64 last_stats[FCG_NR_STATS] = {};
129         unsigned long seq = 0;
130         __s32 opt;
131         __u64 ecode;
132 
133         libbpf_set_print(libbpf_print_fn);
134         signal(SIGINT, sigint_handler);
135         signal(SIGTERM, sigint_handler);
136 restart:
137         skel = SCX_OPS_OPEN(flatcg_ops, scx_flatcg);
138 
139         skel->rodata->nr_cpus = libbpf_num_possible_cpus();
140 
141         while ((opt = getopt(argc, argv, "s:i:dfvh")) != -1) {
142                 double v;
143 
144                 switch (opt) {
145                 case 's':
146                         v = strtod(optarg, NULL);
147                         skel->rodata->cgrp_slice_ns = v * 1000;
148                         break;
149                 case 'i':
150                         v = strtod(optarg, NULL);
151                         intv_ts.tv_sec = v;
152                         intv_ts.tv_nsec = (v - (float)intv_ts.tv_sec) * 1000000000;
153                         break;
154                 case 'd':
155                         dump_cgrps = true;
156                         break;
157                 case 'f':
158                         skel->rodata->fifo_sched = true;
159                         break;
160                 case 'v':
161                         verbose = true;
162                         break;
163                 case 'h':
164                 default:
165                         fprintf(stderr, help_fmt, basename(argv[0]));
166                         return opt != 'h';
167                 }
168         }
169 
170         printf("slice=%.1lfms intv=%.1lfs dump_cgrps=%d",
171                (double)skel->rodata->cgrp_slice_ns / 1000000.0,
172                (double)intv_ts.tv_sec + (double)intv_ts.tv_nsec / 1000000000.0,
173                dump_cgrps);
174 
175         SCX_OPS_LOAD(skel, flatcg_ops, scx_flatcg, uei);
176         link = SCX_OPS_ATTACH(skel, flatcg_ops, scx_flatcg);
177 
178         while (!exit_req && !UEI_EXITED(skel, uei)) {
179                 __u64 acc_stats[FCG_NR_STATS];
180                 __u64 stats[FCG_NR_STATS];
181                 float cpu_util;
182                 int i;
183 
184                 cpu_util = read_cpu_util(&last_cpu_sum, &last_cpu_idle);
185 
186                 fcg_read_stats(skel, acc_stats);
187                 for (i = 0; i < FCG_NR_STATS; i++)
188                         stats[i] = acc_stats[i] - last_stats[i];
189 
190                 memcpy(last_stats, acc_stats, sizeof(acc_stats));
191 
192                 printf("\n[SEQ %6lu cpu=%5.1lf hweight_gen=%" PRIu64 "]\n",
193                        seq++, cpu_util * 100.0, skel->data->hweight_gen);
194                 printf("       act:%6llu  deact:%6llu global:%6llu local:%6llu\n",
195                        stats[FCG_STAT_ACT],
196                        stats[FCG_STAT_DEACT],
197                        stats[FCG_STAT_GLOBAL],
198                        stats[FCG_STAT_LOCAL]);
199                 printf("HWT  cache:%6llu update:%6llu   skip:%6llu  race:%6llu\n",
200                        stats[FCG_STAT_HWT_CACHE],
201                        stats[FCG_STAT_HWT_UPDATES],
202                        stats[FCG_STAT_HWT_SKIP],
203                        stats[FCG_STAT_HWT_RACE]);
204                 printf("ENQ   skip:%6llu   race:%6llu\n",
205                        stats[FCG_STAT_ENQ_SKIP],
206                        stats[FCG_STAT_ENQ_RACE]);
207                 printf("CNS   keep:%6llu expire:%6llu  empty:%6llu  gone:%6llu\n",
208                        stats[FCG_STAT_CNS_KEEP],
209                        stats[FCG_STAT_CNS_EXPIRE],
210                        stats[FCG_STAT_CNS_EMPTY],
211                        stats[FCG_STAT_CNS_GONE]);
212                 printf("PNC   next:%6llu  empty:%6llu nocgrp:%6llu  gone:%6llu race:%6llu fail:%6llu\n",
213                        stats[FCG_STAT_PNC_NEXT],
214                        stats[FCG_STAT_PNC_EMPTY],
215                        stats[FCG_STAT_PNC_NO_CGRP],
216                        stats[FCG_STAT_PNC_GONE],
217                        stats[FCG_STAT_PNC_RACE],
218                        stats[FCG_STAT_PNC_FAIL]);
219                 printf("BAD remove:%6llu\n",
220                        acc_stats[FCG_STAT_BAD_REMOVAL]);
221                 fflush(stdout);
222 
223                 nanosleep(&intv_ts, NULL);
224         }
225 
226         bpf_link__destroy(link);
227         ecode = UEI_REPORT(skel, uei);
228         scx_flatcg__destroy(skel);
229 
230         if (UEI_ECODE_RESTART(ecode))
231                 goto restart;
232         return 0;
233 }
234 

~ [ 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