1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 // Copyright (c) 2021 Wenbo Zhang
3 //
4 // Based on offcputime(8) from BCC by Brendan Gregg.
5 // 19-Mar-2021 Wenbo Zhang Created this.
6 #include <argp.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <time.h>
12 #include <bpf/libbpf.h>
13 #include <bpf/bpf.h>
14 #include "offcputime.h"
15 #include "offcputime.skel.h"
16 #include "trace_helpers.h"
17
18 static struct env {
19 pid_t pid;
20 pid_t tid;
21 bool user_threads_only;
22 bool kernel_threads_only;
23 int stack_storage_size;
24 int perf_max_stack_depth;
25 __u64 min_block_time;
26 __u64 max_block_time;
27 long state;
28 int duration;
29 bool verbose;
30 } env = {
31 .pid = -1,
32 .tid = -1,
33 .stack_storage_size = 1024,
34 .perf_max_stack_depth = 127,
35 .min_block_time = 1,
36 .max_block_time = -1,
37 .state = -1,
38 .duration = 99999999,
39 };
40
41 static volatile bool exiting;
42
43 const char *argp_program_version = "offcputime 0.1";
44 const char *argp_program_bug_address =
45 "https://github.com/iovisor/bcc/tree/master/libbpf-tools";
46 const char argp_program_doc[] =
47 "Summarize off-CPU time by stack trace.\n"
48 "\n"
49 "USAGE: offcputime [--help] [-p PID | -u | -k] [-m MIN-BLOCK-TIME] "
50 "[-M MAX-BLOCK-TIME] [--state] [--perf-max-stack-depth] [--stack-storage-size] "
51 "[duration]\n"
52 "EXAMPLES:\n"
53 " offcputime # trace off-CPU stack time until Ctrl-C\n"
54 " offcputime 5 # trace for 5 seconds only\n"
55 " offcputime -m 1000 # trace only events that last more than 1000 usec\n"
56 " offcputime -M 10000 # trace only events that last less than 10000 usec\n"
57 " offcputime -p 185 # only trace threads for PID 185\n"
58 " offcputime -t 188 # only trace thread 188\n"
59 " offcputime -u # only trace user threads (no kernel)\n"
60 " offcputime -k # only trace kernel threads (no user)\n";
61
62 #define OPT_PERF_MAX_STACK_DEPTH 1 /* --pef-max-stack-depth */
63 #define OPT_STACK_STORAGE_SIZE 2 /* --stack-storage-size */
64 #define OPT_STATE 3 /* --state */
65
66 static const struct argp_option opts[] = {
67 { "pid", 'p', "PID", 0, "Trace this PID only" },
68 { "tid", 't', "TID", 0, "Trace this TID only" },
69 { "user-threads-only", 'u', NULL, 0,
70 "User threads only (no kernel threads)" },
71 { "kernel-threads-only", 'k', NULL, 0,
72 "Kernel threads only (no user threads)" },
73 { "perf-max-stack-depth", OPT_PERF_MAX_STACK_DEPTH,
74 "PERF-MAX-STACK-DEPTH", 0, "the limit for both kernel and user stack traces (default 127)" },
75 { "stack-storage-size", OPT_STACK_STORAGE_SIZE, "STACK-STORAGE-SIZE", 0,
76 "the number of unique stack traces that can be stored and displayed (default 1024)" },
77 { "min-block-time", 'm', "MIN-BLOCK-TIME", 0,
78 "the amount of time in microseconds over which we store traces (default 1)" },
79 { "max-block-time", 'M', "MAX-BLOCK-TIME", 0,
80 "the amount of time in microseconds under which we store traces (default U64_MAX)" },
81 { "state", OPT_STATE, "STATE", 0, "filter on this thread state bitmask (eg, 2 == TASK_UNINTERRUPTIBLE) see include/linux/sched.h" },
82 { "verbose", 'v', NULL, 0, "Verbose debug output" },
83 { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
84 {},
85 };
86
parse_arg(int key,char * arg,struct argp_state * state)87 static error_t parse_arg(int key, char *arg, struct argp_state *state)
88 {
89 static int pos_args;
90
91 switch (key) {
92 case 'h':
93 argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
94 break;
95 case 'v':
96 env.verbose = true;
97 break;
98 case 'p':
99 errno = 0;
100 env.pid = strtol(arg, NULL, 10);
101 if (errno) {
102 fprintf(stderr, "invalid PID: %s\n", arg);
103 argp_usage(state);
104 }
105 break;
106 case 't':
107 errno = 0;
108 env.tid = strtol(arg, NULL, 10);
109 if (errno || env.tid <= 0) {
110 fprintf(stderr, "Invalid TID: %s\n", arg);
111 argp_usage(state);
112 }
113 break;
114 case 'u':
115 env.user_threads_only = true;
116 break;
117 case 'k':
118 env.kernel_threads_only = true;
119 break;
120 case OPT_PERF_MAX_STACK_DEPTH:
121 errno = 0;
122 env.perf_max_stack_depth = strtol(arg, NULL, 10);
123 if (errno) {
124 fprintf(stderr, "invalid perf max stack depth: %s\n", arg);
125 argp_usage(state);
126 }
127 break;
128 case OPT_STACK_STORAGE_SIZE:
129 errno = 0;
130 env.stack_storage_size = strtol(arg, NULL, 10);
131 if (errno) {
132 fprintf(stderr, "invalid stack storage size: %s\n", arg);
133 argp_usage(state);
134 }
135 break;
136 case 'm':
137 errno = 0;
138 env.min_block_time = strtoll(arg, NULL, 10);
139 if (errno) {
140 fprintf(stderr, "Invalid min block time (in us): %s\n", arg);
141 argp_usage(state);
142 }
143 break;
144 case 'M':
145 errno = 0;
146 env.max_block_time = strtoll(arg, NULL, 10);
147 if (errno) {
148 fprintf(stderr, "Invalid min block time (in us): %s\n", arg);
149 argp_usage(state);
150 }
151 break;
152 case OPT_STATE:
153 errno = 0;
154 env.state = strtol(arg, NULL, 10);
155 if (errno || env.state < 0 || env.state > 2) {
156 fprintf(stderr, "Invalid task state: %s\n", arg);
157 argp_usage(state);
158 }
159 break;
160 case ARGP_KEY_ARG:
161 if (pos_args++) {
162 fprintf(stderr,
163 "Unrecognized positional argument: %s\n", arg);
164 argp_usage(state);
165 }
166 errno = 0;
167 env.duration = strtol(arg, NULL, 10);
168 if (errno || env.duration <= 0) {
169 fprintf(stderr, "Invalid duration (in s): %s\n", arg);
170 argp_usage(state);
171 }
172 break;
173 default:
174 return ARGP_ERR_UNKNOWN;
175 }
176 return 0;
177 }
178
libbpf_print_fn(enum libbpf_print_level level,const char * format,va_list args)179 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
180 {
181 if (level == LIBBPF_DEBUG && !env.verbose)
182 return 0;
183 return vfprintf(stderr, format, args);
184 }
185
sig_handler(int sig)186 static void sig_handler(int sig)
187 {
188 }
189
print_map(struct ksyms * ksyms,struct syms_cache * syms_cache,struct offcputime_bpf * obj)190 static void print_map(struct ksyms *ksyms, struct syms_cache *syms_cache,
191 struct offcputime_bpf *obj)
192 {
193 struct key_t lookup_key = {}, next_key;
194 const struct ksym *ksym;
195 const struct syms *syms;
196 const struct sym *sym;
197 int err, i, ifd, sfd;
198 unsigned long *ip;
199 struct val_t val;
200
201 ip = calloc(env.perf_max_stack_depth, sizeof(*ip));
202 if (!ip) {
203 fprintf(stderr, "failed to alloc ip\n");
204 return;
205 }
206
207 ifd = bpf_map__fd(obj->maps.info);
208 sfd = bpf_map__fd(obj->maps.stackmap);
209 while (!bpf_map_get_next_key(ifd, &lookup_key, &next_key)) {
210 err = bpf_map_lookup_elem(ifd, &next_key, &val);
211 if (err < 0) {
212 fprintf(stderr, "failed to lookup info: %d\n", err);
213 goto cleanup;
214 }
215 lookup_key = next_key;
216 if (val.delta == 0)
217 continue;
218 if (bpf_map_lookup_elem(sfd, &next_key.kern_stack_id, ip) != 0) {
219 fprintf(stderr, " [Missed Kernel Stack]\n");
220 goto print_ustack;
221 }
222 for (i = 0; i < env.perf_max_stack_depth && ip[i]; i++) {
223 ksym = ksyms__map_addr(ksyms, ip[i]);
224 printf(" %s\n", ksym ? ksym->name : "Unknown");
225 }
226
227 print_ustack:
228 if (next_key.user_stack_id == -1)
229 goto skip_ustack;
230
231 if (bpf_map_lookup_elem(sfd, &next_key.user_stack_id, ip) != 0) {
232 fprintf(stderr, " [Missed User Stack]\n");
233 continue;
234 }
235
236 syms = syms_cache__get_syms(syms_cache, next_key.tgid);
237 if (!syms) {
238 fprintf(stderr, "failed to get syms\n");
239 goto skip_ustack;
240 }
241 for (i = 0; i < env.perf_max_stack_depth && ip[i]; i++) {
242 sym = syms__map_addr(syms, ip[i]);
243 if (sym)
244 printf(" %s\n", sym->name);
245 else
246 printf(" [unknown]\n");
247 }
248
249 skip_ustack:
250 printf(" %-16s %s (%d)\n", "-", val.comm, next_key.pid);
251 printf(" %lld\n\n", val.delta);
252 }
253
254 cleanup:
255 free(ip);
256 }
257
main(int argc,char ** argv)258 int main(int argc, char **argv)
259 {
260 static const struct argp argp = {
261 .options = opts,
262 .parser = parse_arg,
263 .doc = argp_program_doc,
264 };
265 struct syms_cache *syms_cache = NULL;
266 struct ksyms *ksyms = NULL;
267 struct offcputime_bpf *obj;
268 int err;
269
270 err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
271 if (err)
272 return err;
273 if (env.user_threads_only && env.kernel_threads_only) {
274 fprintf(stderr, "user_threads_only and kernel_threads_only cannot be used together.\n");
275 return 1;
276 }
277 if (env.min_block_time >= env.max_block_time) {
278 fprintf(stderr, "min_block_time should be smaller than max_block_time\n");
279 return 1;
280 }
281
282 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
283 libbpf_set_print(libbpf_print_fn);
284
285 obj = offcputime_bpf__open();
286 if (!obj) {
287 fprintf(stderr, "failed to open BPF object\n");
288 return 1;
289 }
290
291 /* initialize global data (filtering options) */
292 obj->rodata->targ_tgid = env.pid;
293 obj->rodata->targ_pid = env.tid;
294 obj->rodata->user_threads_only = env.user_threads_only;
295 obj->rodata->kernel_threads_only = env.kernel_threads_only;
296 obj->rodata->state = env.state;
297 obj->rodata->min_block_ns = env.min_block_time;
298 obj->rodata->max_block_ns = env.max_block_time;
299
300 bpf_map__set_value_size(obj->maps.stackmap,
301 env.perf_max_stack_depth * sizeof(unsigned long));
302 bpf_map__set_max_entries(obj->maps.stackmap, env.stack_storage_size);
303
304 err = offcputime_bpf__load(obj);
305 if (err) {
306 fprintf(stderr, "failed to load BPF programs\n");
307 goto cleanup;
308 }
309 ksyms = ksyms__load();
310 if (!ksyms) {
311 fprintf(stderr, "failed to load kallsyms\n");
312 goto cleanup;
313 }
314 syms_cache = syms_cache__new(0);
315 if (!syms_cache) {
316 fprintf(stderr, "failed to create syms_cache\n");
317 goto cleanup;
318 }
319 err = offcputime_bpf__attach(obj);
320 if (err) {
321 fprintf(stderr, "failed to attach BPF programs\n");
322 goto cleanup;
323 }
324
325 signal(SIGINT, sig_handler);
326
327 /*
328 * We'll get sleep interrupted when someone presses Ctrl-C (which will
329 * be "handled" with noop by sig_handler).
330 */
331 sleep(env.duration);
332
333 print_map(ksyms, syms_cache, obj);
334
335 cleanup:
336 offcputime_bpf__destroy(obj);
337 syms_cache__free(syms_cache);
338 ksyms__free(ksyms);
339 return err != 0;
340 }
341