1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 // Copyright (c) 2019 Facebook
8 #include <bpf/libbpf.h>
10 #include "runqslower.h"
11 #include "runqslower.skel.h"
21 const char *argp_program_version = "runqslower 0.1";
22 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
23 const char argp_program_doc[] =
24 "runqslower Trace long process scheduling delays.\n"
25 " For Linux, uses eBPF, BPF CO-RE, libbpf, BTF.\n"
27 "This script traces high scheduling delays between tasks being\n"
28 "ready to run and them running on CPU after that.\n"
30 "USAGE: runqslower [-p PID] [min_us]\n"
33 " runqslower # trace run queue latency higher than 10000 us (default)\n"
34 " runqslower 1000 # trace run queue latency higher than 1000 us\n"
35 " runqslower -p 123 # trace pid 123 only\n";
37 static const struct argp_option opts[] = {
38 { "pid", 'p', "PID", 0, "Process PID to trace"},
39 { "verbose", 'v', NULL, 0, "Verbose debug output" },
43 static error_t parse_arg(int key, char *arg, struct argp_state *state)
55 pid = strtol(arg, NULL, 10);
56 if (errno || pid <= 0) {
57 fprintf(stderr, "Invalid PID: %s\n", arg);
65 "Unrecognized positional argument: %s\n", arg);
69 min_us = strtoll(arg, NULL, 10);
70 if (errno || min_us <= 0) {
71 fprintf(stderr, "Invalid delay (in us): %s\n", arg);
77 return ARGP_ERR_UNKNOWN;
82 int libbpf_print_fn(enum libbpf_print_level level,
83 const char *format, va_list args)
85 if (level == LIBBPF_DEBUG && !env.verbose)
87 return vfprintf(stderr, format, args);
90 void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
92 const struct runq_event *e = data;
99 strftime(ts, sizeof(ts), "%H:%M:%S", tm);
100 printf("%-8s %-16s %-6d %14llu\n", ts, e->task, e->pid, e->delta_us);
103 void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
105 printf("Lost %llu events on CPU #%d!\n", lost_cnt, cpu);
108 int main(int argc, char **argv)
110 static const struct argp argp = {
113 .doc = argp_program_doc,
115 struct perf_buffer *pb = NULL;
116 struct runqslower_bpf *obj;
119 err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
123 libbpf_set_print(libbpf_print_fn);
125 /* Use libbpf 1.0 API mode */
126 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
128 obj = runqslower_bpf__open();
130 fprintf(stderr, "failed to open and/or load BPF object\n");
134 /* initialize global data (filtering options) */
135 obj->rodata->targ_pid = env.pid;
136 obj->rodata->min_us = env.min_us;
138 err = runqslower_bpf__load(obj);
140 fprintf(stderr, "failed to load BPF object: %d\n", err);
144 err = runqslower_bpf__attach(obj);
146 fprintf(stderr, "failed to attach BPF programs\n");
150 printf("Tracing run queue latency higher than %llu us\n", env.min_us);
151 printf("%-8s %-16s %-6s %14s\n", "TIME", "COMM", "PID", "LAT(us)");
153 pb = perf_buffer__new(bpf_map__fd(obj->maps.events), 64,
154 handle_event, handle_lost_events, NULL, NULL);
155 err = libbpf_get_error(pb);
158 fprintf(stderr, "failed to open perf buffer: %d\n", err);
162 while ((err = perf_buffer__poll(pb, 100)) >= 0)
164 printf("Error polling perf buffer: %d\n", err);
167 perf_buffer__free(pb);
168 runqslower_bpf__destroy(obj);