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