1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 // Copyright (c) 2019 Facebook
7 #include <sys/resource.h>
9 #include <bpf/libbpf.h>
11 #include "runqslower.h"
12 #include "runqslower.skel.h"
22 const char *argp_program_version = "runqslower 0.1";
23 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
24 const char argp_program_doc[] =
25 "runqslower Trace long process scheduling delays.\n"
26 " For Linux, uses eBPF, BPF CO-RE, libbpf, BTF.\n"
28 "This script traces high scheduling delays between tasks being\n"
29 "ready to run and them running on CPU after that.\n"
31 "USAGE: runqslower [-p PID] [min_us]\n"
34 " runqslower # trace run queue latency higher than 10000 us (default)\n"
35 " runqslower 1000 # trace run queue latency higher than 1000 us\n"
36 " runqslower -p 123 # trace pid 123 only\n";
38 static const struct argp_option opts[] = {
39 { "pid", 'p', "PID", 0, "Process PID to trace"},
40 { "verbose", 'v', NULL, 0, "Verbose debug output" },
44 static error_t parse_arg(int key, char *arg, struct argp_state *state)
56 pid = strtol(arg, NULL, 10);
57 if (errno || pid <= 0) {
58 fprintf(stderr, "Invalid PID: %s\n", arg);
66 "Unrecognized positional argument: %s\n", arg);
70 min_us = strtoll(arg, NULL, 10);
71 if (errno || min_us <= 0) {
72 fprintf(stderr, "Invalid delay (in us): %s\n", arg);
78 return ARGP_ERR_UNKNOWN;
83 int libbpf_print_fn(enum libbpf_print_level level,
84 const char *format, va_list args)
86 if (level == LIBBPF_DEBUG && !env.verbose)
88 return vfprintf(stderr, format, args);
91 static int bump_memlock_rlimit(void)
93 struct rlimit rlim_new = {
94 .rlim_cur = RLIM_INFINITY,
95 .rlim_max = RLIM_INFINITY,
98 return setrlimit(RLIMIT_MEMLOCK, &rlim_new);
101 void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
103 const struct event *e = data;
110 strftime(ts, sizeof(ts), "%H:%M:%S", tm);
111 printf("%-8s %-16s %-6d %14llu\n", ts, e->task, e->pid, e->delta_us);
114 void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
116 printf("Lost %llu events on CPU #%d!\n", lost_cnt, cpu);
119 int main(int argc, char **argv)
121 static const struct argp argp = {
124 .doc = argp_program_doc,
126 struct perf_buffer *pb = NULL;
127 struct runqslower_bpf *obj;
130 err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
134 libbpf_set_print(libbpf_print_fn);
136 err = bump_memlock_rlimit();
138 fprintf(stderr, "failed to increase rlimit: %d", err);
142 obj = runqslower_bpf__open();
144 fprintf(stderr, "failed to open and/or load BPF object\n");
148 /* initialize global data (filtering options) */
149 obj->rodata->targ_pid = env.pid;
150 obj->rodata->min_us = env.min_us;
152 err = runqslower_bpf__load(obj);
154 fprintf(stderr, "failed to load BPF object: %d\n", err);
158 err = runqslower_bpf__attach(obj);
160 fprintf(stderr, "failed to attach BPF programs\n");
164 printf("Tracing run queue latency higher than %llu us\n", env.min_us);
165 printf("%-8s %-16s %-6s %14s\n", "TIME", "COMM", "PID", "LAT(us)");
167 pb = perf_buffer__new(bpf_map__fd(obj->maps.events), 64,
168 handle_event, handle_lost_events, NULL, NULL);
169 err = libbpf_get_error(pb);
172 fprintf(stderr, "failed to open perf buffer: %d\n", err);
176 while ((err = perf_buffer__poll(pb, 100)) >= 0)
178 printf("Error polling perf buffer: %d\n", err);
181 perf_buffer__free(pb);
182 runqslower_bpf__destroy(obj);