xref: /aosp_15_r20/external/bcc/libbpf-tools/llcstat.c (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 // Copyright (c) 2020 Wenbo Zhang
3 //
4 // Based on llcstat(8) from BCC by Teng Qin.
5 // 29-Sep-2020   Wenbo Zhang   Created this.
6 // 20-Jun-2022   YeZhengMao    Added tid info.
7 #include <argp.h>
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <linux/perf_event.h>
13 #include <asm/unistd.h>
14 #include <bpf/libbpf.h>
15 #include <bpf/bpf.h>
16 #include "llcstat.h"
17 #include "llcstat.skel.h"
18 #include "btf_helpers.h"
19 #include "trace_helpers.h"
20 
21 struct env {
22 	int sample_period;
23 	time_t duration;
24 	bool verbose;
25 	bool per_thread;
26 } env = {
27 	.sample_period = 100,
28 	.duration = 10,
29 };
30 
31 static volatile bool exiting;
32 
33 const char *argp_program_version = "llcstat 0.1";
34 const char *argp_program_bug_address =
35 	"https://github.com/iovisor/bcc/tree/master/libbpf-tools";
36 const char argp_program_doc[] =
37 "Summarize cache references and misses by PID.\n"
38 "\n"
39 "USAGE: llcstat [--help] [-c SAMPLE_PERIOD] [duration]\n";
40 
41 static const struct argp_option opts[] = {
42 	{ "sample_period", 'c', "SAMPLE_PERIOD", 0, "Sample one in this many "
43 	  "number of cache reference / miss events" },
44 	{ "verbose", 'v', NULL, 0, "Verbose debug output" },
45 	{ "tid", 't', NULL, 0,
46 	  "Summarize cache references and misses by PID/TID" },
47 	{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
48 	{},
49 };
50 
parse_arg(int key,char * arg,struct argp_state * state)51 static error_t parse_arg(int key, char *arg, struct argp_state *state)
52 {
53 	static int pos_args;
54 
55 	switch (key) {
56 	case 'h':
57 		argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
58 		break;
59 	case 'v':
60 		env.verbose = true;
61 		break;
62 	case 't':
63 		env.per_thread = true;
64 		break;
65 	case 'c':
66 		errno = 0;
67 		env.sample_period = strtol(arg, NULL, 10);
68 		if (errno) {
69 			fprintf(stderr, "invalid sample period\n");
70 			argp_usage(state);
71 		}
72 		break;
73 	case ARGP_KEY_ARG:
74 		if (pos_args++) {
75 			fprintf(stderr,
76 				"unrecognized positional argument: %s\n", arg);
77 			argp_usage(state);
78 		}
79 		errno = 0;
80 		env.duration = strtol(arg, NULL, 10);
81 		if (errno) {
82 			fprintf(stderr, "invalid duration\n");
83 			argp_usage(state);
84 		}
85 		break;
86 	default:
87 		return ARGP_ERR_UNKNOWN;
88 	}
89 	return 0;
90 }
91 
92 static int nr_cpus;
93 
open_and_attach_perf_event(__u64 config,int period,struct bpf_program * prog,struct bpf_link * links[])94 static int open_and_attach_perf_event(__u64 config, int period,
95 				struct bpf_program *prog,
96 				struct bpf_link *links[])
97 {
98 	struct perf_event_attr attr = {
99 		.type = PERF_TYPE_HARDWARE,
100 		.freq = 0,
101 		.sample_period = period,
102 		.config = config,
103 	};
104 	int i, fd;
105 
106 	for (i = 0; i < nr_cpus; i++) {
107 		fd = syscall(__NR_perf_event_open, &attr, -1, i, -1, 0);
108 		if (fd < 0) {
109 			/* Ignore CPU that is offline */
110 			if (errno == ENODEV)
111 				continue;
112 			fprintf(stderr, "failed to init perf sampling: %s\n",
113 				strerror(errno));
114 			return -1;
115 		}
116 		links[i] = bpf_program__attach_perf_event(prog, fd);
117 		if (!links[i]) {
118 			fprintf(stderr, "failed to attach perf event on cpu: %d\n", i);
119 			close(fd);
120 			return -1;
121 		}
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_map(struct bpf_map * map)138 static void print_map(struct bpf_map *map)
139 {
140 	__u64 total_ref = 0, total_miss = 0, total_hit, hit;
141 	__u32 pid, cpu, tid;
142 	struct key_info lookup_key = { .cpu = -1 }, next_key;
143 	int err, fd = bpf_map__fd(map);
144 	struct value_info info;
145 
146 	while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
147 		err = bpf_map_lookup_elem(fd, &next_key, &info);
148 		if (err < 0) {
149 			fprintf(stderr, "failed to lookup infos: %d\n", err);
150 			return;
151 		}
152 		hit = info.ref > info.miss ? info.ref - info.miss : 0;
153 		cpu = next_key.cpu;
154 		pid = next_key.pid;
155 		tid = next_key.tid;
156 		printf("%-8u ", pid);
157 		if (env.per_thread) {
158 			printf("%-8u ", tid);
159 		}
160 		printf("%-16s %-4u %12llu %12llu %6.2f%%\n",
161 			info.comm, cpu, info.ref, info.miss,
162 			info.ref > 0 ? hit * 1.0 / info.ref * 100 : 0);
163 		total_miss += info.miss;
164 		total_ref += info.ref;
165 		lookup_key = next_key;
166 	}
167 	total_hit = total_ref > total_miss ? total_ref - total_miss : 0;
168 	printf("Total References: %llu Total Misses: %llu Hit Rate: %.2f%%\n",
169 		total_ref, total_miss, total_ref > 0 ?
170 		total_hit * 1.0 / total_ref * 100 : 0);
171 
172 	lookup_key.cpu = -1;
173 	while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
174 		err = bpf_map_delete_elem(fd, &next_key);
175 		if (err < 0) {
176 			fprintf(stderr, "failed to cleanup infos: %d\n", err);
177 			return;
178 		}
179 		lookup_key = next_key;
180 	}
181 }
182 
main(int argc,char ** argv)183 int main(int argc, char **argv)
184 {
185 	struct bpf_link **rlinks = NULL, **mlinks = NULL;
186 	LIBBPF_OPTS(bpf_object_open_opts, open_opts);
187 	static const struct argp argp = {
188 		.options = opts,
189 		.parser = parse_arg,
190 		.doc = argp_program_doc,
191 	};
192 	struct llcstat_bpf *obj;
193 	int err, i;
194 
195 	err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
196 	if (err)
197 		return err;
198 
199 	libbpf_set_print(libbpf_print_fn);
200 
201 	nr_cpus = libbpf_num_possible_cpus();
202 	if (nr_cpus < 0) {
203 		fprintf(stderr, "failed to get # of possible cpus: '%s'!\n",
204 			strerror(-nr_cpus));
205 		return 1;
206 	}
207 	mlinks = calloc(nr_cpus, sizeof(*mlinks));
208 	rlinks = calloc(nr_cpus, sizeof(*rlinks));
209 	if (!mlinks || !rlinks) {
210 		fprintf(stderr, "failed to alloc mlinks or rlinks\n");
211 		return 1;
212 	}
213 
214 	err = ensure_core_btf(&open_opts);
215 	if (err) {
216 		fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
217 		return 1;
218 	}
219 
220 	obj = llcstat_bpf__open_opts(&open_opts);
221 	if (!obj) {
222 		fprintf(stderr, "failed to open and/or load BPF object\n");
223 		goto cleanup;
224 	}
225 
226 	obj->rodata->targ_per_thread = env.per_thread;
227 
228 	err = llcstat_bpf__load(obj);
229 	if (err) {
230 		fprintf(stderr, "failed to load BPF object: %d\n", err);
231 		goto cleanup;
232 	}
233 
234 	if (open_and_attach_perf_event(PERF_COUNT_HW_CACHE_MISSES,
235 					env.sample_period,
236 					obj->progs.on_cache_miss, mlinks))
237 		goto cleanup;
238 	if (open_and_attach_perf_event(PERF_COUNT_HW_CACHE_REFERENCES,
239 					env.sample_period,
240 					obj->progs.on_cache_ref, rlinks))
241 		goto cleanup;
242 
243 	printf("Running for %ld seconds or Hit Ctrl-C to end.\n", env.duration);
244 
245 	signal(SIGINT, sig_handler);
246 
247 	sleep(env.duration);
248 
249 	printf("%-8s ", "PID");
250 	if (env.per_thread) {
251 		printf("%-8s ", "TID");
252 	}
253 	printf("%-16s %-4s %12s %12s %7s\n",
254 		"NAME", "CPU", "REFERENCE", "MISS", "HIT%");
255 
256 	print_map(obj->maps.infos);
257 
258 cleanup:
259 	for (i = 0; i < nr_cpus; i++) {
260 		bpf_link__destroy(mlinks[i]);
261 		bpf_link__destroy(rlinks[i]);
262 	}
263 	free(mlinks);
264 	free(rlinks);
265 	llcstat_bpf__destroy(obj);
266 	cleanup_core_btf(&open_opts);
267 
268 	return err != 0;
269 }
270