xref: /aosp_15_r20/external/bcc/libbpf-tools/hardirqs.c (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
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 <fcntl.h>
14 #include <bpf/libbpf.h>
15 #include <bpf/bpf.h>
16 #include "hardirqs.h"
17 #include "hardirqs.skel.h"
18 #include "trace_helpers.h"
19 
20 struct env {
21 	bool count;
22 	bool distributed;
23 	bool nanoseconds;
24 	time_t interval;
25 	int times;
26 	bool timestamp;
27 	bool verbose;
28 	char *cgroupspath;
29 	bool cg;
30 } env = {
31 	.interval = 99999999,
32 	.times = 99999999,
33 };
34 
35 static volatile bool exiting;
36 
37 const char *argp_program_version = "hardirqs 0.1";
38 const char *argp_program_bug_address =
39 	"https://github.com/iovisor/bcc/tree/master/libbpf-tools";
40 const char argp_program_doc[] =
41 "Summarize hard irq event time as histograms.\n"
42 "\n"
43 "USAGE: hardirqs [--help] [-T] [-N] [-d] [interval] [count] [-c CG]\n"
44 "\n"
45 "EXAMPLES:\n"
46 "    hardirqs            # sum hard irq event time\n"
47 "    hardirqs -d         # show hard irq event time as histograms\n"
48 "    hardirqs 1 10       # print 1 second summaries, 10 times\n"
49 "    hardirqs -c CG      # Trace process under cgroupsPath CG\n"
50 "    hardirqs -NT 1      # 1s summaries, nanoseconds, and timestamps\n";
51 
52 static const struct argp_option opts[] = {
53 	{ "count", 'C', NULL, 0, "Show event counts instead of timing" },
54 	{ "distributed", 'd', NULL, 0, "Show distributions as histograms" },
55 	{ "cgroup", 'c', "/sys/fs/cgroup/unified", 0, "Trace process in cgroup path" },
56 	{ "timestamp", 'T', NULL, 0, "Include timestamp on output" },
57 	{ "nanoseconds", 'N', NULL, 0, "Output in nanoseconds" },
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 'd':
75 		env.distributed = true;
76 		break;
77 	case 'C':
78 		env.count = true;
79 		break;
80 	case 'c':
81 		env.cgroupspath = arg;
82 		env.cg = true;
83 		break;
84 	case 'N':
85 		env.nanoseconds = true;
86 		break;
87 	case 'T':
88 		env.timestamp = true;
89 		break;
90 	case ARGP_KEY_ARG:
91 		errno = 0;
92 		if (pos_args == 0) {
93 			env.interval = strtol(arg, NULL, 10);
94 			if (errno) {
95 				fprintf(stderr, "invalid internal\n");
96 				argp_usage(state);
97 			}
98 		} else if (pos_args == 1) {
99 			env.times = strtol(arg, NULL, 10);
100 			if (errno) {
101 				fprintf(stderr, "invalid times\n");
102 				argp_usage(state);
103 			}
104 		} else {
105 			fprintf(stderr,
106 				"unrecognized positional argument: %s\n", arg);
107 			argp_usage(state);
108 		}
109 		pos_args++;
110 		break;
111 	default:
112 		return ARGP_ERR_UNKNOWN;
113 	}
114 	return 0;
115 }
116 
libbpf_print_fn(enum libbpf_print_level level,const char * format,va_list args)117 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
118 {
119 	if (level == LIBBPF_DEBUG && !env.verbose)
120 		return 0;
121 	return vfprintf(stderr, format, args);
122 }
123 
sig_handler(int sig)124 static void sig_handler(int sig)
125 {
126 	exiting = true;
127 }
128 
print_map(struct bpf_map * map)129 static int print_map(struct bpf_map *map)
130 {
131 	struct irq_key lookup_key = {}, next_key;
132 	struct info info;
133 	int fd, err;
134 
135 	if (env.count) {
136 		printf("%-26s %11s\n", "HARDIRQ", "TOTAL_count");
137 	} else if (!env.distributed) {
138 		const char *units = env.nanoseconds ? "nsecs" : "usecs";
139 
140 		printf("%-26s %6s%5s\n", "HARDIRQ", "TOTAL_", units);
141 	}
142 
143 	fd = bpf_map__fd(map);
144 	while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
145 		err = bpf_map_lookup_elem(fd, &next_key, &info);
146 		if (err < 0) {
147 			fprintf(stderr, "failed to lookup infos: %d\n", err);
148 			return -1;
149 		}
150 		if (!env.distributed)
151 			printf("%-26s %11llu\n", next_key.name, info.count);
152 		else {
153 			const char *units = env.nanoseconds ? "nsecs" : "usecs";
154 
155 			printf("hardirq = %s\n", next_key.name);
156 			print_log2_hist(info.slots, MAX_SLOTS, units);
157 		}
158 		lookup_key = next_key;
159 	}
160 
161 	memset(&lookup_key, 0, sizeof(lookup_key));
162 
163 	while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
164 		err = bpf_map_delete_elem(fd, &next_key);
165 		if (err < 0) {
166 			fprintf(stderr, "failed to cleanup infos: %d\n", err);
167 			return -1;
168 		}
169 		lookup_key = next_key;
170 	}
171 
172 	return 0;
173 }
174 
main(int argc,char ** argv)175 int main(int argc, char **argv)
176 {
177 	static const struct argp argp = {
178 		.options = opts,
179 		.parser = parse_arg,
180 		.doc = argp_program_doc,
181 	};
182 	struct hardirqs_bpf *obj;
183 	struct tm *tm;
184 	char ts[32];
185 	time_t t;
186 	int err;
187 	int idx, cg_map_fd;
188 	int cgfd = -1;
189 
190 	err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
191 	if (err)
192 		return err;
193 
194 	if (env.count && env.distributed) {
195 		fprintf(stderr, "count, distributed cann't be used together.\n");
196 		return 1;
197 	}
198 
199 	libbpf_set_print(libbpf_print_fn);
200 
201 	obj = hardirqs_bpf__open();
202 	if (!obj) {
203 		fprintf(stderr, "failed to open BPF object\n");
204 		return 1;
205 	}
206 
207 	if (probe_tp_btf("irq_handler_entry")) {
208 		bpf_program__set_autoload(obj->progs.irq_handler_entry, false);
209 		bpf_program__set_autoload(obj->progs.irq_handler_exit, false);
210 		if (env.count)
211 			bpf_program__set_autoload(obj->progs.irq_handler_exit_btf, false);
212 	} else {
213 		bpf_program__set_autoload(obj->progs.irq_handler_entry_btf, false);
214 		bpf_program__set_autoload(obj->progs.irq_handler_exit_btf, false);
215 		if (env.count)
216 			bpf_program__set_autoload(obj->progs.irq_handler_exit, false);
217 	}
218 
219 	obj->rodata->filter_cg = env.cg;
220 	obj->rodata->do_count = env.count;
221 
222 	/* initialize global data (filtering options) */
223 	if (!env.count) {
224 		obj->rodata->targ_dist = env.distributed;
225 		obj->rodata->targ_ns = env.nanoseconds;
226 	}
227 
228 	err = hardirqs_bpf__load(obj);
229 	if (err) {
230 		fprintf(stderr, "failed to load BPF object: %d\n", err);
231 		goto cleanup;
232 	}
233 
234 	/* update cgroup path fd to map */
235 	if (env.cg) {
236 		idx = 0;
237 		cg_map_fd = bpf_map__fd(obj->maps.cgroup_map);
238 		cgfd = open(env.cgroupspath, O_RDONLY);
239 		if (cgfd < 0) {
240 			fprintf(stderr, "Failed opening Cgroup path: %s", env.cgroupspath);
241 			goto cleanup;
242 		}
243 		if (bpf_map_update_elem(cg_map_fd, &idx, &cgfd, BPF_ANY)) {
244 			fprintf(stderr, "Failed adding target cgroup to map");
245 			goto cleanup;
246 		}
247 	}
248 
249 	err = hardirqs_bpf__attach(obj);
250 	if (err) {
251 		fprintf(stderr, "failed to attach BPF object: %d\n", err);
252 		goto cleanup;
253 	}
254 
255 	signal(SIGINT, sig_handler);
256 
257 	if (env.count)
258 		printf("Tracing hard irq events... Hit Ctrl-C to end.\n");
259 	else
260 		printf("Tracing hard irq event time... Hit Ctrl-C to end.\n");
261 
262 	/* main: poll */
263 	while (1) {
264 		sleep(env.interval);
265 		printf("\n");
266 
267 		if (env.timestamp) {
268 			time(&t);
269 			tm = localtime(&t);
270 			strftime(ts, sizeof(ts), "%H:%M:%S", tm);
271 			printf("%-8s\n", ts);
272 		}
273 
274 		err = print_map(obj->maps.infos);
275 		if (err)
276 			break;
277 
278 		if (exiting || --env.times == 0)
279 			break;
280 	}
281 
282 cleanup:
283 	hardirqs_bpf__destroy(obj);
284 	if (cgfd > 0)
285 		close(cgfd);
286 
287 	return err != 0;
288 }
289