xref: /aosp_15_r20/external/bcc/libbpf-tools/runqlat.c (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 // Copyright (c) 2020 Wenbo Zhang
3 //
4 // Based on runqlat(8) from BCC by Bredan Gregg.
5 // 10-Aug-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 <time.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <bpf/libbpf.h>
15 #include <bpf/bpf.h>
16 #include "runqlat.h"
17 #include "runqlat.skel.h"
18 #include "trace_helpers.h"
19 
20 struct env {
21 	time_t interval;
22 	pid_t pid;
23 	int times;
24 	bool milliseconds;
25 	bool per_process;
26 	bool per_thread;
27 	bool per_pidns;
28 	bool timestamp;
29 	bool verbose;
30 	char *cgroupspath;
31 	bool cg;
32 } env = {
33 	.interval = 99999999,
34 	.times = 99999999,
35 };
36 
37 static volatile bool exiting;
38 
39 const char *argp_program_version = "runqlat 0.1";
40 const char *argp_program_bug_address =
41 	"https://github.com/iovisor/bcc/tree/master/libbpf-tools";
42 const char argp_program_doc[] =
43 "Summarize run queue (scheduler) latency as a histogram.\n"
44 "\n"
45 "USAGE: runqlat [--help] [-T] [-m] [--pidnss] [-L] [-P] [-p PID] [interval] [count] [-c CG]\n"
46 "\n"
47 "EXAMPLES:\n"
48 "    runqlat         # summarize run queue latency as a histogram\n"
49 "    runqlat 1 10    # print 1 second summaries, 10 times\n"
50 "    runqlat -mT 1   # 1s summaries, milliseconds, and timestamps\n"
51 "    runqlat -P      # show each PID separately\n"
52 "    runqlat -p 185  # trace PID 185 only\n"
53 "    runqlat -c CG   # Trace process under cgroupsPath CG\n";
54 
55 #define OPT_PIDNSS	1	/* --pidnss */
56 
57 static const struct argp_option opts[] = {
58 	{ "timestamp", 'T', NULL, 0, "Include timestamp on output" },
59 	{ "milliseconds", 'm', NULL, 0, "Millisecond histogram" },
60 	{ "pidnss", OPT_PIDNSS, NULL, 0, "Print a histogram per PID namespace" },
61 	{ "pids", 'P', NULL, 0, "Print a histogram per process ID" },
62 	{ "tids", 'L', NULL, 0, "Print a histogram per thread ID" },
63 	{ "pid", 'p', "PID", 0, "Trace this PID only" },
64 	{ "verbose", 'v', NULL, 0, "Verbose debug output" },
65 	{ "cgroup", 'c', "/sys/fs/cgroup/unified", 0, "Trace process in cgroup path"},
66 	{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
67 	{},
68 };
69 
parse_arg(int key,char * arg,struct argp_state * state)70 static error_t parse_arg(int key, char *arg, struct argp_state *state)
71 {
72 	static int pos_args;
73 
74 	switch (key) {
75 	case 'h':
76 		argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
77 		break;
78 	case 'v':
79 		env.verbose = true;
80 		break;
81 	case 'm':
82 		env.milliseconds = true;
83 		break;
84 	case 'p':
85 		errno = 0;
86 		env.pid = strtol(arg, NULL, 10);
87 		if (errno) {
88 			fprintf(stderr, "invalid PID: %s\n", arg);
89 			argp_usage(state);
90 		}
91 		break;
92 	case 'L':
93 		env.per_thread = true;
94 		break;
95 	case 'P':
96 		env.per_process = true;
97 		break;
98 	case OPT_PIDNSS:
99 		env.per_pidns = true;
100 		break;
101 	case 'T':
102 		env.timestamp = true;
103 		break;
104 	case 'c':
105 		env.cgroupspath = arg;
106 		env.cg = true;
107 		break;
108 	case ARGP_KEY_ARG:
109 		errno = 0;
110 		if (pos_args == 0) {
111 			env.interval = strtol(arg, NULL, 10);
112 			if (errno) {
113 				fprintf(stderr, "invalid internal\n");
114 				argp_usage(state);
115 			}
116 		} else if (pos_args == 1) {
117 			env.times = strtol(arg, NULL, 10);
118 			if (errno) {
119 				fprintf(stderr, "invalid times\n");
120 				argp_usage(state);
121 			}
122 		} else {
123 			fprintf(stderr,
124 				"unrecognized positional argument: %s\n", arg);
125 			argp_usage(state);
126 		}
127 		pos_args++;
128 		break;
129 	default:
130 		return ARGP_ERR_UNKNOWN;
131 	}
132 	return 0;
133 }
134 
libbpf_print_fn(enum libbpf_print_level level,const char * format,va_list args)135 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
136 {
137 	if (level == LIBBPF_DEBUG && !env.verbose)
138 		return 0;
139 	return vfprintf(stderr, format, args);
140 }
141 
sig_handler(int sig)142 static void sig_handler(int sig)
143 {
144 	exiting = true;
145 }
146 
print_log2_hists(struct bpf_map * hists)147 static int print_log2_hists(struct bpf_map *hists)
148 {
149 	const char *units = env.milliseconds ? "msecs" : "usecs";
150 	int err, fd = bpf_map__fd(hists);
151 	__u32 lookup_key = -2, next_key;
152 	struct hist hist;
153 
154 	while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
155 		err = bpf_map_lookup_elem(fd, &next_key, &hist);
156 		if (err < 0) {
157 			fprintf(stderr, "failed to lookup hist: %d\n", err);
158 			return -1;
159 		}
160 		if (env.per_process)
161 			printf("\npid = %d %s\n", next_key, hist.comm);
162 		else if (env.per_thread)
163 			printf("\ntid = %d %s\n", next_key, hist.comm);
164 		else if (env.per_pidns)
165 			printf("\npidns = %u %s\n", next_key, hist.comm);
166 		print_log2_hist(hist.slots, MAX_SLOTS, units);
167 		lookup_key = next_key;
168 	}
169 
170 	lookup_key = -2;
171 	while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
172 		err = bpf_map_delete_elem(fd, &next_key);
173 		if (err < 0) {
174 			fprintf(stderr, "failed to cleanup hist : %d\n", err);
175 			return -1;
176 		}
177 		lookup_key = next_key;
178 	}
179 	return 0;
180 }
181 
main(int argc,char ** argv)182 int main(int argc, char **argv)
183 {
184 	static const struct argp argp = {
185 		.options = opts,
186 		.parser = parse_arg,
187 		.doc = argp_program_doc,
188 	};
189 	struct runqlat_bpf *obj;
190 	struct tm *tm;
191 	char ts[32];
192 	time_t t;
193 	int err;
194 	int idx, cg_map_fd;
195 	int cgfd = -1;
196 
197 	err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
198 	if (err)
199 		return err;
200 
201 	if ((env.per_thread && (env.per_process || env.per_pidns)) ||
202 		(env.per_process && env.per_pidns)) {
203 		fprintf(stderr, "pidnss, pids, tids cann't be used together.\n");
204 		return 1;
205 	}
206 
207 	libbpf_set_print(libbpf_print_fn);
208 
209 	obj = runqlat_bpf__open();
210 	if (!obj) {
211 		fprintf(stderr, "failed to open BPF object\n");
212 		return 1;
213 	}
214 
215 	/* initialize global data (filtering options) */
216 	obj->rodata->targ_per_process = env.per_process;
217 	obj->rodata->targ_per_thread = env.per_thread;
218 	obj->rodata->targ_per_pidns = env.per_pidns;
219 	obj->rodata->targ_ms = env.milliseconds;
220 	obj->rodata->targ_tgid = env.pid;
221 	obj->rodata->filter_cg = env.cg;
222 
223 	if (probe_tp_btf("sched_wakeup")) {
224 		bpf_program__set_autoload(obj->progs.handle_sched_wakeup, false);
225 		bpf_program__set_autoload(obj->progs.handle_sched_wakeup_new, false);
226 		bpf_program__set_autoload(obj->progs.handle_sched_switch, false);
227 	} else {
228 		bpf_program__set_autoload(obj->progs.sched_wakeup, false);
229 		bpf_program__set_autoload(obj->progs.sched_wakeup_new, false);
230 		bpf_program__set_autoload(obj->progs.sched_switch, false);
231 	}
232 
233 	err = runqlat_bpf__load(obj);
234 	if (err) {
235 		fprintf(stderr, "failed to load BPF object: %d\n", err);
236 		goto cleanup;
237 	}
238 
239 	/* update cgroup path fd to map */
240 	if (env.cg) {
241 		idx = 0;
242 		cg_map_fd = bpf_map__fd(obj->maps.cgroup_map);
243 		cgfd = open(env.cgroupspath, O_RDONLY);
244 		if (cgfd < 0) {
245 			fprintf(stderr, "Failed opening Cgroup path: %s", env.cgroupspath);
246 			goto cleanup;
247 		}
248 		if (bpf_map_update_elem(cg_map_fd, &idx, &cgfd, BPF_ANY)) {
249 			fprintf(stderr, "Failed adding target cgroup to map");
250 			goto cleanup;
251 		}
252 	}
253 
254 	err = runqlat_bpf__attach(obj);
255 	if (err) {
256 		fprintf(stderr, "failed to attach BPF programs\n");
257 		goto cleanup;
258 	}
259 
260 	printf("Tracing run queue latency... Hit Ctrl-C to end.\n");
261 
262 	signal(SIGINT, sig_handler);
263 
264 	/* main: poll */
265 	while (1) {
266 		sleep(env.interval);
267 		printf("\n");
268 
269 		if (env.timestamp) {
270 			time(&t);
271 			tm = localtime(&t);
272 			strftime(ts, sizeof(ts), "%H:%M:%S", tm);
273 			printf("%-8s\n", ts);
274 		}
275 
276 		err = print_log2_hists(obj->maps.hists);
277 		if (err)
278 			break;
279 
280 		if (exiting || --env.times == 0)
281 			break;
282 	}
283 
284 cleanup:
285 	runqlat_bpf__destroy(obj);
286 	if (cgfd > 0)
287 		close(cgfd);
288 
289 	return err != 0;
290 }
291