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