xref: /aosp_15_r20/external/bcc/libbpf-tools/wakeuptime.c (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 // Copyright (c) 2022 Nicolas Sterchele
3 //
4 // Based on wakeuptime(8) from BCC by Brendan Gregg
5 // XX-Jul-2022 Nicolas Sterchele 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 <bpf/libbpf.h>
13 #include <bpf/bpf.h>
14 #include "wakeuptime.h"
15 #include "wakeuptime.skel.h"
16 #include "trace_helpers.h"
17 #include <unistd.h>
18 
19 struct env {
20 	pid_t pid;
21 	bool user_threads_only;
22 	bool verbose;
23 	int stack_storage_size;
24 	int perf_max_stack_depth;
25 	__u64 min_block_time;
26 	__u64 max_block_time;
27 	int duration;
28 } env = {
29 	.verbose = false,
30 	.stack_storage_size = 1024,
31 	.perf_max_stack_depth = 127,
32 	.min_block_time = 1,
33 	.max_block_time = -1,
34 	.duration = 99999999,
35 };
36 
37 const char *argp_program_version = "wakeuptime 0.1";
38 const char *argp_program_bug_address =
39 	"https://github.com/iovisor/bcc/tree/master/libbpf-tools";
40 const char argp_program_doc[] =
41 "Summarize sleep to wakeup time by waker kernel stack.\n"
42 "\n"
43 "USAGE: wakeuptime [-h] [-p PID | -u] [-v] [-m MIN-BLOCK-TIME] "
44 "[-M MAX-BLOCK-TIME] ]--perf-max-stack-depth] [--stack-storage-size] [duration]\n"
45 "EXAMPLES:\n"
46 "	wakeuptime		# trace blocked time with waker stacks\n"
47 "	wakeuptime 5		# trace for 5 seconds only\n"
48 "	wakeuptime -u		# don't include kernel threads (user only)\n"
49 "	wakeuptime -p 185	# trace for PID 185 only\n";
50 
51 #define OPT_PERF_MAX_STACK_DEPTH	1 /* --pef-max-stack-depth */
52 #define OPT_STACK_STORAGE_SIZE		2 /* --stack-storage-size */
53 
54 static const struct argp_option opts[] = {
55 	{ "pid", 'p', "PID", 0, "trace this PID only"},
56 	{ "verbose", 'v', NULL, 0, "show raw addresses" },
57 	{ "user-threads-only", 'u', NULL, 0, "user threads only (no kernel threads)" },
58 	{ "perf-max-stack-depth", OPT_PERF_MAX_STACK_DEPTH,
59 		"PERF-MAX-STACK-DEPTH", 0, "the limit for both kernel and user stack traces (default 127)" },
60 	{ "stack-storage-size", OPT_STACK_STORAGE_SIZE, "STACK-STORAGE-SIZE", 0,
61 		"the number of unique stack traces that can be stored and displayed (default 1024)" },
62 	{ "min-block-time", 'm', "MIN-BLOCK-TIME", 0,
63 		"the amount of time in microseconds over which we store traces (default 1)" },
64 	{ "max-block-time", 'M', "MAX-BLOCK-TIME", 0,
65 		"the amount of time in microseconds under which we store traces (default U64_MAX)" },
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 	int pid;
74 
75 	switch (key) {
76 	case 'h':
77 		argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
78 		break;
79 	case 'v':
80 		env.verbose = true;
81 		break;
82 	case 'u':
83 		env.user_threads_only = true;
84 		break;
85 	case 'p':
86 		errno = 0;
87 		pid = strtol(arg, NULL, 10);
88 		if (errno || pid <= 0) {
89 			fprintf(stderr, "Invalid PID: %s\n", arg);
90 			argp_usage(state);
91 		}
92 		env.pid = pid;
93 		break;
94 	case OPT_PERF_MAX_STACK_DEPTH:
95 		errno = 0;
96 		env.perf_max_stack_depth = strtol(arg, NULL, 10);
97 		if (errno) {
98 			fprintf(stderr, "invalid perf max stack depth: %s\n", arg);
99 			argp_usage(state);
100 		}
101 		break;
102 	case OPT_STACK_STORAGE_SIZE:
103 		errno = 0;
104 		env.stack_storage_size = strtol(arg, NULL, 10);
105 		if (errno) {
106 			fprintf(stderr, "invalid stack storage size: %s\n", arg);
107 			argp_usage(state);
108 		}
109 		break;
110 	case 'm':
111 		errno = 0;
112 		env.min_block_time = strtoll(arg, NULL, 10);
113 		if (errno) {
114 			fprintf(stderr, "Invalid min block time (in us): %s\n", arg);
115 			argp_usage(state);
116 		}
117 		break;
118 	case 'M':
119 		errno = 0;
120 		env.max_block_time = strtoll(arg, NULL, 10);
121 		if (errno) {
122 			fprintf(stderr, "Invalid min block time (in us): %s\n", arg);
123 			argp_usage(state);
124 		}
125 		break;
126 	case ARGP_KEY_ARG:
127 		errno = 0;
128 		if (pos_args == 0){
129 			env.duration = strtol(arg, NULL, 10);
130 			if (errno || env.duration <= 0) {
131 				fprintf(stderr, "invalid duration (in s)\n");
132 				argp_usage(state);
133 			}
134 		} else {
135 			fprintf(stderr, "Unrecognized positional argument: %s\n", arg);
136 			argp_usage(state);
137 		}
138 		break;
139 	default:
140 		return ARGP_ERR_UNKNOWN;
141 	}
142 	return 0;
143 }
144 
libbpf_print_fn(enum libbpf_print_level level,const char * format,va_list args)145 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
146 {
147 	if (level == LIBBPF_DEBUG && !env.verbose)
148 		return 0;
149 	return vfprintf(stderr, format, args);
150 }
151 
sig_int(int signo)152 static void sig_int(int signo)
153 {
154 }
155 
print_map(struct ksyms * ksyms,struct wakeuptime_bpf * obj)156 static void print_map(struct ksyms *ksyms, struct wakeuptime_bpf *obj)
157 {
158 	struct key_t lookup_key = {}, next_key;
159 	int err, i, counts_fd, stack_traces_fd;
160 	unsigned long *ip;
161 	const struct ksym *ksym;
162 	__u64 val;
163 
164 	ip = calloc(env.perf_max_stack_depth, sizeof(*ip));
165 	if (!ip) {
166 		fprintf(stderr, "failed to alloc ip\n");
167 		return;
168 	}
169 
170 	counts_fd = bpf_map__fd(obj->maps.counts);
171 	stack_traces_fd = bpf_map__fd(obj->maps.stackmap);
172 
173 	while (!bpf_map_get_next_key(counts_fd, &lookup_key, &next_key)){
174 		err = bpf_map_lookup_elem(counts_fd, &next_key, &val);
175 		if (err < 0) {
176 			fprintf(stderr, "failed to lookup info: %d\n", err);
177 			free(ip);
178 			return;
179 		}
180 		printf("\n	%-16s %s\n", "target:", next_key.target);
181 		lookup_key = next_key;
182 
183 		err = bpf_map_lookup_elem(stack_traces_fd, &next_key.w_k_stack_id, ip);
184 		if (err < 0) {
185 			fprintf(stderr, "missed kernel stack: %d\n", err);
186 		}
187 		for (i = 0; i < env.perf_max_stack_depth && ip[i]; i++) {
188 			ksym = ksyms__map_addr(ksyms, ip[i]);
189 			printf("	%-16lx %s\n", ip[i], ksym ? ksym->name: "Unknown");
190 		}
191 		printf("	%16s %s\n","waker:", next_key.waker);
192 		/*to convert val in microseconds*/
193 		val /= 1000;
194 		printf("	%lld\n", val);
195 	}
196 
197 	free(ip);
198 }
199 
main(int argc,char ** argv)200 int main(int argc, char **argv)
201 {
202 	static const struct argp argp = {
203 		.options = opts,
204 		.parser = parse_arg,
205 		.doc = argp_program_doc,
206 	};
207 	struct wakeuptime_bpf *obj;
208 	struct ksyms *ksyms = NULL;
209 	int err;
210 
211 	err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
212 	if (err)
213 		return err;
214 
215 	if (env.min_block_time >= env.max_block_time) {
216 		fprintf(stderr, "min_block_time should be smaller than max_block_time\n");
217 		return 1;
218 	}
219 
220 	if (env.user_threads_only && env.pid > 0) {
221 		fprintf(stderr, "use either -u or -p");
222 	}
223 
224 	libbpf_set_print(libbpf_print_fn);
225 
226 	obj = wakeuptime_bpf__open();
227 	if (!obj) {
228 		fprintf(stderr, "failed to open BPF object\n");
229 		return 1;
230 	}
231 
232 	obj->rodata->targ_pid = env.pid;
233 	obj->rodata->min_block_ns = env.min_block_time * 1000;
234 	obj->rodata->max_block_ns = env.max_block_time * 1000;
235 	obj->rodata->user_threads_only = env.user_threads_only;
236 
237 	bpf_map__set_value_size(obj->maps.stackmap,
238 				env.perf_max_stack_depth * sizeof(unsigned long));
239 	bpf_map__set_max_entries(obj->maps.stackmap, env.stack_storage_size);
240 
241 	err = wakeuptime_bpf__load(obj);
242 	if (err) {
243 		fprintf(stderr, "failed to load BPF object: %d\n", err);
244 		goto cleanup;
245 	}
246 
247 	ksyms = ksyms__load();
248 	if (!ksyms) {
249 		fprintf(stderr, "failed to load kallsyms\n");
250 		goto cleanup;
251 	}
252 
253 	err = wakeuptime_bpf__attach(obj);
254 	if (err) {
255 		fprintf(stderr, "failed to attach BPF programs\n");
256 		goto cleanup;
257 	}
258 
259 	if (signal(SIGINT, sig_int) == SIG_ERR) {
260 		fprintf(stderr, "can't set signal handler: %s\n", strerror(errno));
261 		err = 1;
262 		goto cleanup;
263 	}
264 
265 	printf("Tracing blocked time (us) by kernel stack\n");
266 	sleep(env.duration);
267 	print_map(ksyms, obj);
268 
269 cleanup:
270 	wakeuptime_bpf__destroy(obj);
271 	ksyms__free(ksyms);
272 	return err != 0;
273 }
274