1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 // Copyright (c) 2019 Facebook
3 #include <argp.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/resource.h>
8 #include <time.h>
9 #include <bpf/libbpf.h>
10 #include <bpf/bpf.h>
11 #include "runqslower.h"
12 #include "runqslower.skel.h"
13 
14 struct env {
15 	pid_t pid;
16 	__u64 min_us;
17 	bool verbose;
18 } env = {
19 	.min_us = 10000,
20 };
21 
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"
27 "\n"
28 "This script traces high scheduling delays between tasks being\n"
29 "ready to run and them running on CPU after that.\n"
30 "\n"
31 "USAGE: runqslower [-p PID] [min_us]\n"
32 "\n"
33 "EXAMPLES:\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";
37 
38 static const struct argp_option opts[] = {
39 	{ "pid", 'p', "PID", 0, "Process PID to trace"},
40 	{ "verbose", 'v', NULL, 0, "Verbose debug output" },
41 	{},
42 };
43 
44 static error_t parse_arg(int key, char *arg, struct argp_state *state)
45 {
46 	static int pos_args;
47 	int pid;
48 	long long min_us;
49 
50 	switch (key) {
51 	case 'v':
52 		env.verbose = true;
53 		break;
54 	case 'p':
55 		errno = 0;
56 		pid = strtol(arg, NULL, 10);
57 		if (errno || pid <= 0) {
58 			fprintf(stderr, "Invalid PID: %s\n", arg);
59 			argp_usage(state);
60 		}
61 		env.pid = pid;
62 		break;
63 	case ARGP_KEY_ARG:
64 		if (pos_args++) {
65 			fprintf(stderr,
66 				"Unrecognized positional argument: %s\n", arg);
67 			argp_usage(state);
68 		}
69 		errno = 0;
70 		min_us = strtoll(arg, NULL, 10);
71 		if (errno || min_us <= 0) {
72 			fprintf(stderr, "Invalid delay (in us): %s\n", arg);
73 			argp_usage(state);
74 		}
75 		env.min_us = min_us;
76 		break;
77 	default:
78 		return ARGP_ERR_UNKNOWN;
79 	}
80 	return 0;
81 }
82 
83 int libbpf_print_fn(enum libbpf_print_level level,
84 		    const char *format, va_list args)
85 {
86 	if (level == LIBBPF_DEBUG && !env.verbose)
87 		return 0;
88 	return vfprintf(stderr, format, args);
89 }
90 
91 static int bump_memlock_rlimit(void)
92 {
93 	struct rlimit rlim_new = {
94 		.rlim_cur	= RLIM_INFINITY,
95 		.rlim_max	= RLIM_INFINITY,
96 	};
97 
98 	return setrlimit(RLIMIT_MEMLOCK, &rlim_new);
99 }
100 
101 void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
102 {
103 	const struct event *e = data;
104 	struct tm *tm;
105 	char ts[32];
106 	time_t t;
107 
108 	time(&t);
109 	tm = localtime(&t);
110 	strftime(ts, sizeof(ts), "%H:%M:%S", tm);
111 	printf("%-8s %-16s %-6d %14llu\n", ts, e->task, e->pid, e->delta_us);
112 }
113 
114 void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
115 {
116 	printf("Lost %llu events on CPU #%d!\n", lost_cnt, cpu);
117 }
118 
119 int main(int argc, char **argv)
120 {
121 	static const struct argp argp = {
122 		.options = opts,
123 		.parser = parse_arg,
124 		.doc = argp_program_doc,
125 	};
126 	struct perf_buffer *pb = NULL;
127 	struct runqslower_bpf *obj;
128 	int err;
129 
130 	err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
131 	if (err)
132 		return err;
133 
134 	libbpf_set_print(libbpf_print_fn);
135 
136 	err = bump_memlock_rlimit();
137 	if (err) {
138 		fprintf(stderr, "failed to increase rlimit: %d", err);
139 		return 1;
140 	}
141 
142 	obj = runqslower_bpf__open();
143 	if (!obj) {
144 		fprintf(stderr, "failed to open and/or load BPF object\n");
145 		return 1;
146 	}
147 
148 	/* initialize global data (filtering options) */
149 	obj->rodata->targ_pid = env.pid;
150 	obj->rodata->min_us = env.min_us;
151 
152 	err = runqslower_bpf__load(obj);
153 	if (err) {
154 		fprintf(stderr, "failed to load BPF object: %d\n", err);
155 		goto cleanup;
156 	}
157 
158 	err = runqslower_bpf__attach(obj);
159 	if (err) {
160 		fprintf(stderr, "failed to attach BPF programs\n");
161 		goto cleanup;
162 	}
163 
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)");
166 
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);
170 	if (err) {
171 		pb = NULL;
172 		fprintf(stderr, "failed to open perf buffer: %d\n", err);
173 		goto cleanup;
174 	}
175 
176 	while ((err = perf_buffer__poll(pb, 100)) >= 0)
177 		;
178 	printf("Error polling perf buffer: %d\n", err);
179 
180 cleanup:
181 	perf_buffer__free(pb);
182 	runqslower_bpf__destroy(obj);
183 
184 	return err != 0;
185 }
186