1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 // Copyright (c) 2020 Wenbo Zhang
3 //
4 // Based on hardirq(8) from BCC by Brendan Gregg.
5 // 31-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 "hardirqs.h"
16 #include "hardirqs.skel.h"
17 #include "trace_helpers.h"
18
19 struct env {
20 bool count;
21 bool distributed;
22 bool nanoseconds;
23 time_t interval;
24 int times;
25 bool timestamp;
26 bool verbose;
27 } env = {
28 .interval = 99999999,
29 .times = 99999999,
30 };
31
32 static volatile bool exiting;
33
34 const char *argp_program_version = "hardirqs 0.1";
35 const char *argp_program_bug_address =
36 "https://github.com/iovisor/bcc/tree/master/libbpf-tools";
37 const char argp_program_doc[] =
38 "Summarize hard irq event time as histograms.\n"
39 "\n"
40 "USAGE: hardirqs [--help] [-T] [-N] [-d] [interval] [count]\n"
41 "\n"
42 "EXAMPLES:\n"
43 " hardirqs # sum hard irq event time\n"
44 " hardirqs -d # show hard irq event time as histograms\n"
45 " hardirqs 1 10 # print 1 second summaries, 10 times\n"
46 " hardirqs -NT 1 # 1s summaries, nanoseconds, and timestamps\n";
47
48 static const struct argp_option opts[] = {
49 { "count", 'C', NULL, 0, "Show event counts instead of timing" },
50 { "distributed", 'd', NULL, 0, "Show distributions as histograms" },
51 { "timestamp", 'T', NULL, 0, "Include timestamp on output" },
52 { "nanoseconds", 'N', NULL, 0, "Output in nanoseconds" },
53 { "verbose", 'v', NULL, 0, "Verbose debug output" },
54 { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
55 {},
56 };
57
parse_arg(int key,char * arg,struct argp_state * state)58 static error_t parse_arg(int key, char *arg, struct argp_state *state)
59 {
60 static int pos_args;
61
62 switch (key) {
63 case 'h':
64 argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
65 break;
66 case 'v':
67 env.verbose = true;
68 break;
69 case 'd':
70 env.distributed = true;
71 break;
72 case 'C':
73 env.count = true;
74 break;
75 case 'N':
76 env.nanoseconds = true;
77 break;
78 case 'T':
79 env.timestamp = true;
80 break;
81 case ARGP_KEY_ARG:
82 errno = 0;
83 if (pos_args == 0) {
84 env.interval = strtol(arg, NULL, 10);
85 if (errno) {
86 fprintf(stderr, "invalid internal\n");
87 argp_usage(state);
88 }
89 } else if (pos_args == 1) {
90 env.times = strtol(arg, NULL, 10);
91 if (errno) {
92 fprintf(stderr, "invalid times\n");
93 argp_usage(state);
94 }
95 } else {
96 fprintf(stderr,
97 "unrecognized positional argument: %s\n", arg);
98 argp_usage(state);
99 }
100 pos_args++;
101 break;
102 default:
103 return ARGP_ERR_UNKNOWN;
104 }
105 return 0;
106 }
107
libbpf_print_fn(enum libbpf_print_level level,const char * format,va_list args)108 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
109 {
110 if (level == LIBBPF_DEBUG && !env.verbose)
111 return 0;
112 return vfprintf(stderr, format, args);
113 }
114
sig_handler(int sig)115 static void sig_handler(int sig)
116 {
117 exiting = true;
118 }
119
print_map(struct bpf_map * map)120 static int print_map(struct bpf_map *map)
121 {
122 struct irq_key lookup_key = {}, next_key;
123 struct info info;
124 int fd, err;
125
126 if (env.count) {
127 printf("%-26s %11s\n", "HARDIRQ", "TOTAL_count");
128 } else if (!env.distributed) {
129 const char *units = env.nanoseconds ? "nsecs" : "usecs";
130
131 printf("%-26s %6s%5s\n", "HARDIRQ", "TOTAL_", units);
132 }
133
134 fd = bpf_map__fd(map);
135 while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
136 err = bpf_map_lookup_elem(fd, &next_key, &info);
137 if (err < 0) {
138 fprintf(stderr, "failed to lookup infos: %d\n", err);
139 return -1;
140 }
141 if (!env.distributed)
142 printf("%-26s %11llu\n", next_key.name, info.count);
143 else {
144 const char *units = env.nanoseconds ? "nsecs" : "usecs";
145
146 printf("hardirq = %s\n", next_key.name);
147 print_log2_hist(info.slots, MAX_SLOTS, units);
148 }
149 lookup_key = next_key;
150 }
151
152 memset(&lookup_key, 0, sizeof(lookup_key));
153
154 while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
155 err = bpf_map_delete_elem(fd, &next_key);
156 if (err < 0) {
157 fprintf(stderr, "failed to cleanup infos: %d\n", err);
158 return -1;
159 }
160 lookup_key = next_key;
161 }
162
163 return 0;
164 }
165
main(int argc,char ** argv)166 int main(int argc, char **argv)
167 {
168 static const struct argp argp = {
169 .options = opts,
170 .parser = parse_arg,
171 .doc = argp_program_doc,
172 };
173 struct hardirqs_bpf *obj;
174 struct tm *tm;
175 char ts[32];
176 time_t t;
177 int err;
178
179 err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
180 if (err)
181 return err;
182
183 if (env.count && env.distributed) {
184 fprintf(stderr, "count, distributed cann't be used together.\n");
185 return 1;
186 }
187
188 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
189 libbpf_set_print(libbpf_print_fn);
190
191 obj = hardirqs_bpf__open();
192 if (!obj) {
193 fprintf(stderr, "failed to open BPF object\n");
194 return 1;
195 }
196
197 /* initialize global data (filtering options) */
198 if (!env.count) {
199 obj->rodata->targ_dist = env.distributed;
200 obj->rodata->targ_ns = env.nanoseconds;
201 }
202
203 err = hardirqs_bpf__load(obj);
204 if (err) {
205 fprintf(stderr, "failed to load BPF object: %d\n", err);
206 goto cleanup;
207 }
208
209 if (env.count) {
210 obj->links.handle__irq_handler = bpf_program__attach(obj->progs.handle__irq_handler);
211 if (!obj->links.handle__irq_handler) {
212 err = -errno;
213 fprintf(stderr,
214 "failed to attach irq/irq_handler_entry: %s\n",
215 strerror(-err));
216 }
217 } else {
218 obj->links.irq_handler_entry = bpf_program__attach(obj->progs.irq_handler_entry);
219 if (!obj->links.irq_handler_entry) {
220 err = -errno;
221 fprintf(stderr,
222 "failed to attach irq_handler_entry: %s\n",
223 strerror(-err));
224 }
225 obj->links.irq_handler_exit_exit = bpf_program__attach(obj->progs.irq_handler_exit_exit);
226 if (!obj->links.irq_handler_exit_exit) {
227 err = -errno;
228 fprintf(stderr,
229 "failed to attach irq_handler_exit: %s\n",
230 strerror(-err));
231 }
232 }
233
234 signal(SIGINT, sig_handler);
235
236 if (env.count)
237 printf("Tracing hard irq events... Hit Ctrl-C to end.\n");
238 else
239 printf("Tracing hard irq event time... Hit Ctrl-C to end.\n");
240
241 /* main: poll */
242 while (1) {
243 sleep(env.interval);
244 printf("\n");
245
246 if (env.timestamp) {
247 time(&t);
248 tm = localtime(&t);
249 strftime(ts, sizeof(ts), "%H:%M:%S", tm);
250 printf("%-8s\n", ts);
251 }
252
253 err = print_map(obj->maps.infos);
254 if (err)
255 break;
256
257 if (exiting || --env.times == 0)
258 break;
259 }
260
261 cleanup:
262 hardirqs_bpf__destroy(obj);
263
264 return err != 0;
265 }
266