1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 // Copyright (c) 2020 Wenbo Zhang
3 //
4 // Based on runqlen(8) from BCC by Brendan Gregg.
5 // 11-Sep-2020 Wenbo Zhang Created this.
6 #include <argp.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <time.h>
13 #include <linux/perf_event.h>
14 #include <asm/unistd.h>
15 #include <bpf/libbpf.h>
16 #include <bpf/bpf.h>
17 #include "runqlen.h"
18 #include "runqlen.skel.h"
19 #include "btf_helpers.h"
20 #include "trace_helpers.h"
21
22 #define max(x, y) ({ \
23 typeof(x) _max1 = (x); \
24 typeof(y) _max2 = (y); \
25 (void) (&_max1 == &_max2); \
26 _max1 > _max2 ? _max1 : _max2; })
27
28 struct env {
29 bool per_cpu;
30 bool runqocc;
31 bool timestamp;
32 bool host;
33 time_t interval;
34 int freq;
35 int times;
36 bool verbose;
37 } env = {
38 .interval = 99999999,
39 .times = 99999999,
40 .freq = 99,
41 };
42
43 static volatile bool exiting;
44
45 const char *argp_program_version = "runqlen 0.1";
46 const char *argp_program_bug_address =
47 "https://github.com/iovisor/bcc/tree/master/libbpf-tools";
48 const char argp_program_doc[] =
49 "Summarize scheduler run queue length as a histogram.\n"
50 "\n"
51 "USAGE: runqlen [--help] [-C] [-O] [-T] [-f FREQUENCY] [interval] [count]\n"
52 "\n"
53 "EXAMPLES:\n"
54 " runqlen # summarize run queue length as a histogram\n"
55 " runqlen 1 10 # print 1 second summaries, 10 times\n"
56 " runqlen -T 1 # 1s summaries and timestamps\n"
57 " runqlen -O # report run queue occupancy\n"
58 " runqlen -C # show each CPU separately\n"
59 " runqlen -H # show nr_running from host's rq instead of cfs_rq\n"
60 " runqlen -f 199 # sample at 199HZ\n";
61
62 static const struct argp_option opts[] = {
63 { "cpus", 'C', NULL, 0, "Print output for each CPU separately" },
64 { "frequency", 'f', "FREQUENCY", 0, "Sample with a certain frequency" },
65 { "runqocc", 'O', NULL, 0, "Report run queue occupancy" },
66 { "timestamp", 'T', NULL, 0, "Include timestamp on output" },
67 { "verbose", 'v', NULL, 0, "Verbose debug output" },
68 { "host", 'H', NULL, 0, "Report nr_running from host's rq" },
69 { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
70 {},
71 };
72
parse_arg(int key,char * arg,struct argp_state * state)73 static error_t parse_arg(int key, char *arg, struct argp_state *state)
74 {
75 static int pos_args;
76
77 switch (key) {
78 case 'h':
79 argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
80 break;
81 case 'v':
82 env.verbose = true;
83 break;
84 case 'C':
85 env.per_cpu = true;
86 break;
87 case 'O':
88 env.runqocc = true;
89 break;
90 case 'T':
91 env.timestamp = true;
92 break;
93 case 'H':
94 env.host = true;
95 break;
96 case 'f':
97 errno = 0;
98 env.freq = strtol(arg, NULL, 10);
99 if (errno || env.freq <= 0) {
100 fprintf(stderr, "Invalid freq (in hz): %s\n", arg);
101 argp_usage(state);
102 }
103 break;
104 case ARGP_KEY_ARG:
105 errno = 0;
106 if (pos_args == 0) {
107 env.interval = strtol(arg, NULL, 10);
108 if (errno) {
109 fprintf(stderr, "invalid internal\n");
110 argp_usage(state);
111 }
112 } else if (pos_args == 1) {
113 env.times = strtol(arg, NULL, 10);
114 if (errno) {
115 fprintf(stderr, "invalid times\n");
116 argp_usage(state);
117 }
118 } else {
119 fprintf(stderr,
120 "unrecognized positional argument: %s\n", arg);
121 argp_usage(state);
122 }
123 pos_args++;
124 break;
125 default:
126 return ARGP_ERR_UNKNOWN;
127 }
128 return 0;
129 }
130
131 static int nr_cpus;
132
open_and_attach_perf_event(int freq,struct bpf_program * prog,struct bpf_link * links[])133 static int open_and_attach_perf_event(int freq, struct bpf_program *prog,
134 struct bpf_link *links[])
135 {
136 struct perf_event_attr attr = {
137 .type = PERF_TYPE_SOFTWARE,
138 .freq = 1,
139 .sample_period = freq,
140 .config = PERF_COUNT_SW_CPU_CLOCK,
141 };
142 int i, fd;
143
144 for (i = 0; i < nr_cpus; i++) {
145 fd = syscall(__NR_perf_event_open, &attr, -1, i, -1, 0);
146 if (fd < 0) {
147 /* Ignore CPU that is offline */
148 if (errno == ENODEV)
149 continue;
150 fprintf(stderr, "failed to init perf sampling: %s\n",
151 strerror(errno));
152 return -1;
153 }
154 links[i] = bpf_program__attach_perf_event(prog, fd);
155 if (!links[i]) {
156 fprintf(stderr, "failed to attach perf event on cpu: %d\n", i);
157 close(fd);
158 return -1;
159 }
160 }
161
162 return 0;
163 }
164
libbpf_print_fn(enum libbpf_print_level level,const char * format,va_list args)165 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
166 {
167 if (level == LIBBPF_DEBUG && !env.verbose)
168 return 0;
169 return vfprintf(stderr, format, args);
170 }
171
sig_handler(int sig)172 static void sig_handler(int sig)
173 {
174 exiting = true;
175 }
176
177 static struct hist zero;
178
print_runq_occupancy(struct runqlen_bpf__bss * bss)179 static void print_runq_occupancy(struct runqlen_bpf__bss *bss)
180 {
181 struct hist hist;
182 int slot, i = 0;
183 float runqocc;
184
185 do {
186 __u64 samples, idle = 0, queued = 0;
187
188 hist = bss->hists[i];
189 bss->hists[i] = zero;
190 for (slot = 0; slot < MAX_SLOTS; slot++) {
191 __u64 val = hist.slots[slot];
192
193 if (slot == 0)
194 idle += val;
195 else
196 queued += val;
197 }
198 samples = idle + queued;
199 runqocc = queued * 1.0 / max(1ULL, samples);
200 if (env.per_cpu)
201 printf("runqocc, CPU %-3d %6.2f%%\n", i,
202 100 * runqocc);
203 else
204 printf("runqocc: %0.2f%%\n", 100 * runqocc);
205 } while (env.per_cpu && ++i < nr_cpus);
206 }
207
print_linear_hists(struct runqlen_bpf__bss * bss)208 static void print_linear_hists(struct runqlen_bpf__bss *bss)
209 {
210 struct hist hist;
211 int i = 0;
212
213 do {
214 hist = bss->hists[i];
215 bss->hists[i] = zero;
216 if (env.per_cpu)
217 printf("cpu = %d\n", i);
218 print_linear_hist(hist.slots, MAX_SLOTS, 0, 1, "runqlen");
219 } while (env.per_cpu && ++i < nr_cpus);
220 }
221
main(int argc,char ** argv)222 int main(int argc, char **argv)
223 {
224 LIBBPF_OPTS(bpf_object_open_opts, open_opts);
225 static const struct argp argp = {
226 .options = opts,
227 .parser = parse_arg,
228 .doc = argp_program_doc,
229 };
230 struct bpf_link *links[MAX_CPU_NR] = {};
231 struct runqlen_bpf *obj;
232 struct tm *tm;
233 char ts[32];
234 int err, i;
235 time_t t;
236
237 err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
238 if (err)
239 return err;
240
241 libbpf_set_print(libbpf_print_fn);
242
243 nr_cpus = libbpf_num_possible_cpus();
244 if (nr_cpus < 0) {
245 printf("failed to get # of possible cpus: '%s'!\n",
246 strerror(-nr_cpus));
247 return 1;
248 }
249 if (nr_cpus > MAX_CPU_NR) {
250 fprintf(stderr, "the number of cpu cores is too big, please "
251 "increase MAX_CPU_NR's value and recompile");
252 return 1;
253 }
254
255 err = ensure_core_btf(&open_opts);
256 if (err) {
257 fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
258 return 1;
259 }
260
261 obj = runqlen_bpf__open_opts(&open_opts);
262 if (!obj) {
263 fprintf(stderr, "failed to open BPF object\n");
264 return 1;
265 }
266
267 /* initialize global data (filtering options) */
268 obj->rodata->targ_per_cpu = env.per_cpu;
269 obj->rodata->targ_host = env.host;
270
271 err = runqlen_bpf__load(obj);
272 if (err) {
273 fprintf(stderr, "failed to load BPF object: %d\n", err);
274 goto cleanup;
275 }
276
277 if (!obj->bss) {
278 fprintf(stderr, "Memory-mapping BPF maps is supported starting from Linux 5.7, please upgrade.\n");
279 goto cleanup;
280 }
281
282 err = open_and_attach_perf_event(env.freq, obj->progs.do_sample, links);
283 if (err)
284 goto cleanup;
285
286 printf("Sampling run queue length... Hit Ctrl-C to end.\n");
287
288 signal(SIGINT, sig_handler);
289
290 while (1) {
291 sleep(env.interval);
292 printf("\n");
293
294 if (env.timestamp) {
295 time(&t);
296 tm = localtime(&t);
297 strftime(ts, sizeof(ts), "%H:%M:%S", tm);
298 printf("%-8s\n", ts);
299 }
300
301 if (env.runqocc)
302 print_runq_occupancy(obj->bss);
303 else
304 print_linear_hists(obj->bss);
305
306 if (exiting || --env.times == 0)
307 break;
308 }
309
310 cleanup:
311 for (i = 0; i < nr_cpus; i++)
312 bpf_link__destroy(links[i]);
313 runqlen_bpf__destroy(obj);
314 cleanup_core_btf(&open_opts);
315
316 return err != 0;
317 }
318