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