xref: /openbmc/linux/tools/bpf/bpftool/map_perf_ring.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1907b2236SJakub Kicinski // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2f412eed9SJakub Kicinski /* Copyright (C) 2018 Netronome Systems, Inc. */
3f412eed9SJakub Kicinski /* This program is free software; you can redistribute it and/or
4f412eed9SJakub Kicinski  * modify it under the terms of version 2 of the GNU General Public
5f412eed9SJakub Kicinski  * License as published by the Free Software Foundation.
6f412eed9SJakub Kicinski  */
7f412eed9SJakub Kicinski #include <errno.h>
8f412eed9SJakub Kicinski #include <fcntl.h>
9229c3b47SToke Høiland-Jørgensen #include <bpf/libbpf.h>
10f412eed9SJakub Kicinski #include <poll.h>
11f412eed9SJakub Kicinski #include <signal.h>
12f412eed9SJakub Kicinski #include <stdbool.h>
13f412eed9SJakub Kicinski #include <stdio.h>
14f412eed9SJakub Kicinski #include <stdlib.h>
15f412eed9SJakub Kicinski #include <string.h>
16f412eed9SJakub Kicinski #include <time.h>
17f412eed9SJakub Kicinski #include <unistd.h>
18f412eed9SJakub Kicinski #include <linux/bpf.h>
19f412eed9SJakub Kicinski #include <linux/perf_event.h>
20f412eed9SJakub Kicinski #include <sys/ioctl.h>
21f412eed9SJakub Kicinski #include <sys/mman.h>
22f412eed9SJakub Kicinski #include <sys/syscall.h>
23f412eed9SJakub Kicinski 
24229c3b47SToke Høiland-Jørgensen #include <bpf/bpf.h>
25f412eed9SJakub Kicinski 
26f412eed9SJakub Kicinski #include "main.h"
27f412eed9SJakub Kicinski 
28f412eed9SJakub Kicinski #define MMAP_PAGE_CNT	16
29f412eed9SJakub Kicinski 
309b190f18SAndrii Nakryiko static volatile bool stop;
31f412eed9SJakub Kicinski 
32f412eed9SJakub Kicinski struct perf_event_sample {
33f412eed9SJakub Kicinski 	struct perf_event_header header;
346e7e034eSQuentin Monnet 	__u64 time;
35f412eed9SJakub Kicinski 	__u32 size;
36f412eed9SJakub Kicinski 	unsigned char data[];
37f412eed9SJakub Kicinski };
38f412eed9SJakub Kicinski 
399b190f18SAndrii Nakryiko struct perf_event_lost {
409b190f18SAndrii Nakryiko 	struct perf_event_header header;
419b190f18SAndrii Nakryiko 	__u64 id;
429b190f18SAndrii Nakryiko 	__u64 lost;
439b190f18SAndrii Nakryiko };
449b190f18SAndrii Nakryiko 
int_exit(int signo)45f412eed9SJakub Kicinski static void int_exit(int signo)
46f412eed9SJakub Kicinski {
47f412eed9SJakub Kicinski 	fprintf(stderr, "Stopping...\n");
48f412eed9SJakub Kicinski 	stop = true;
49f412eed9SJakub Kicinski }
50f412eed9SJakub Kicinski 
519b190f18SAndrii Nakryiko struct event_pipe_ctx {
529b190f18SAndrii Nakryiko 	bool all_cpus;
539b190f18SAndrii Nakryiko 	int cpu;
549b190f18SAndrii Nakryiko 	int idx;
559b190f18SAndrii Nakryiko };
569b190f18SAndrii Nakryiko 
573dca2115SDaniel Borkmann static enum bpf_perf_event_ret
print_bpf_output(void * private_data,int cpu,struct perf_event_header * event)589b190f18SAndrii Nakryiko print_bpf_output(void *private_data, int cpu, struct perf_event_header *event)
59f412eed9SJakub Kicinski {
609b190f18SAndrii Nakryiko 	struct perf_event_sample *e = container_of(event,
619b190f18SAndrii Nakryiko 						   struct perf_event_sample,
623dca2115SDaniel Borkmann 						   header);
639b190f18SAndrii Nakryiko 	struct perf_event_lost *lost = container_of(event,
649b190f18SAndrii Nakryiko 						    struct perf_event_lost,
659b190f18SAndrii Nakryiko 						    header);
669b190f18SAndrii Nakryiko 	struct event_pipe_ctx *ctx = private_data;
679b190f18SAndrii Nakryiko 	int idx = ctx->all_cpus ? cpu : ctx->idx;
68f412eed9SJakub Kicinski 
69f412eed9SJakub Kicinski 	if (json_output) {
70f412eed9SJakub Kicinski 		jsonw_start_object(json_wtr);
71f412eed9SJakub Kicinski 		jsonw_name(json_wtr, "type");
72f412eed9SJakub Kicinski 		jsonw_uint(json_wtr, e->header.type);
73f412eed9SJakub Kicinski 		jsonw_name(json_wtr, "cpu");
749b190f18SAndrii Nakryiko 		jsonw_uint(json_wtr, cpu);
75f412eed9SJakub Kicinski 		jsonw_name(json_wtr, "index");
769b190f18SAndrii Nakryiko 		jsonw_uint(json_wtr, idx);
77f412eed9SJakub Kicinski 		if (e->header.type == PERF_RECORD_SAMPLE) {
78e3687510SJakub Kicinski 			jsonw_name(json_wtr, "timestamp");
79e3687510SJakub Kicinski 			jsonw_uint(json_wtr, e->time);
80f412eed9SJakub Kicinski 			jsonw_name(json_wtr, "data");
81f412eed9SJakub Kicinski 			print_data_json(e->data, e->size);
82f412eed9SJakub Kicinski 		} else if (e->header.type == PERF_RECORD_LOST) {
83f412eed9SJakub Kicinski 			jsonw_name(json_wtr, "lost");
84f412eed9SJakub Kicinski 			jsonw_start_object(json_wtr);
85f412eed9SJakub Kicinski 			jsonw_name(json_wtr, "id");
86f412eed9SJakub Kicinski 			jsonw_uint(json_wtr, lost->id);
87f412eed9SJakub Kicinski 			jsonw_name(json_wtr, "count");
88f412eed9SJakub Kicinski 			jsonw_uint(json_wtr, lost->lost);
89f412eed9SJakub Kicinski 			jsonw_end_object(json_wtr);
90f412eed9SJakub Kicinski 		}
91f412eed9SJakub Kicinski 		jsonw_end_object(json_wtr);
92f412eed9SJakub Kicinski 	} else {
93f412eed9SJakub Kicinski 		if (e->header.type == PERF_RECORD_SAMPLE) {
94e3687510SJakub Kicinski 			printf("== @%lld.%09lld CPU: %d index: %d =====\n",
95e3687510SJakub Kicinski 			       e->time / 1000000000ULL, e->time % 1000000000ULL,
969b190f18SAndrii Nakryiko 			       cpu, idx);
97f412eed9SJakub Kicinski 			fprint_hex(stdout, e->data, e->size, " ");
98f412eed9SJakub Kicinski 			printf("\n");
99f412eed9SJakub Kicinski 		} else if (e->header.type == PERF_RECORD_LOST) {
100f412eed9SJakub Kicinski 			printf("lost %lld events\n", lost->lost);
101f412eed9SJakub Kicinski 		} else {
102f412eed9SJakub Kicinski 			printf("unknown event type=%d size=%d\n",
103f412eed9SJakub Kicinski 			       e->header.type, e->header.size);
104f412eed9SJakub Kicinski 		}
105f412eed9SJakub Kicinski 	}
106d0cabbb0SJakub Kicinski 
107d0cabbb0SJakub Kicinski 	return LIBBPF_PERF_EVENT_CONT;
108f412eed9SJakub Kicinski }
109f412eed9SJakub Kicinski 
do_event_pipe(int argc,char ** argv)1109b190f18SAndrii Nakryiko int do_event_pipe(int argc, char **argv)
111f412eed9SJakub Kicinski {
1129b190f18SAndrii Nakryiko 	struct perf_event_attr perf_attr = {
113e3687510SJakub Kicinski 		.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_TIME,
114f412eed9SJakub Kicinski 		.type = PERF_TYPE_SOFTWARE,
115f412eed9SJakub Kicinski 		.config = PERF_COUNT_SW_BPF_OUTPUT,
1169b190f18SAndrii Nakryiko 		.sample_period = 1,
1179b190f18SAndrii Nakryiko 		.wakeup_events = 1,
118f412eed9SJakub Kicinski 	};
119f412eed9SJakub Kicinski 	struct bpf_map_info map_info = {};
120164b04f2SAndrii Nakryiko 	LIBBPF_OPTS(perf_buffer_raw_opts, opts);
1219b190f18SAndrii Nakryiko 	struct event_pipe_ctx ctx = {
1229b190f18SAndrii Nakryiko 		.all_cpus = true,
1239b190f18SAndrii Nakryiko 		.cpu = -1,
1249b190f18SAndrii Nakryiko 		.idx = -1,
1259b190f18SAndrii Nakryiko 	};
1269b190f18SAndrii Nakryiko 	struct perf_buffer *pb;
127f412eed9SJakub Kicinski 	__u32 map_info_len;
1289b190f18SAndrii Nakryiko 	int err, map_fd;
129f412eed9SJakub Kicinski 
130f412eed9SJakub Kicinski 	map_info_len = sizeof(map_info);
131f412eed9SJakub Kicinski 	map_fd = map_parse_fd_and_info(&argc, &argv, &map_info, &map_info_len);
132f412eed9SJakub Kicinski 	if (map_fd < 0)
133f412eed9SJakub Kicinski 		return -1;
134f412eed9SJakub Kicinski 
135f412eed9SJakub Kicinski 	if (map_info.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
136f412eed9SJakub Kicinski 		p_err("map is not a perf event array");
137f412eed9SJakub Kicinski 		goto err_close_map;
138f412eed9SJakub Kicinski 	}
139f412eed9SJakub Kicinski 
140f412eed9SJakub Kicinski 	while (argc) {
141785e76d7SQuentin Monnet 		if (argc < 2) {
142f412eed9SJakub Kicinski 			BAD_ARG();
143785e76d7SQuentin Monnet 			goto err_close_map;
144785e76d7SQuentin Monnet 		}
145f412eed9SJakub Kicinski 
146f412eed9SJakub Kicinski 		if (is_prefix(*argv, "cpu")) {
147f412eed9SJakub Kicinski 			char *endptr;
148f412eed9SJakub Kicinski 
149f412eed9SJakub Kicinski 			NEXT_ARG();
1509b190f18SAndrii Nakryiko 			ctx.cpu = strtoul(*argv, &endptr, 0);
151f412eed9SJakub Kicinski 			if (*endptr) {
1529def249dSQuentin Monnet 				p_err("can't parse %s as CPU ID", *argv);
153f412eed9SJakub Kicinski 				goto err_close_map;
154f412eed9SJakub Kicinski 			}
155f412eed9SJakub Kicinski 
156f412eed9SJakub Kicinski 			NEXT_ARG();
157f412eed9SJakub Kicinski 		} else if (is_prefix(*argv, "index")) {
158f412eed9SJakub Kicinski 			char *endptr;
159f412eed9SJakub Kicinski 
160f412eed9SJakub Kicinski 			NEXT_ARG();
1619b190f18SAndrii Nakryiko 			ctx.idx = strtoul(*argv, &endptr, 0);
162f412eed9SJakub Kicinski 			if (*endptr) {
1639def249dSQuentin Monnet 				p_err("can't parse %s as index", *argv);
164f412eed9SJakub Kicinski 				goto err_close_map;
165f412eed9SJakub Kicinski 			}
166f412eed9SJakub Kicinski 
167f412eed9SJakub Kicinski 			NEXT_ARG();
168f412eed9SJakub Kicinski 		} else {
169f412eed9SJakub Kicinski 			BAD_ARG();
170785e76d7SQuentin Monnet 			goto err_close_map;
171f412eed9SJakub Kicinski 		}
172f412eed9SJakub Kicinski 
1739b190f18SAndrii Nakryiko 		ctx.all_cpus = false;
174f412eed9SJakub Kicinski 	}
175f412eed9SJakub Kicinski 
1769b190f18SAndrii Nakryiko 	if (!ctx.all_cpus) {
1779b190f18SAndrii Nakryiko 		if (ctx.idx == -1 || ctx.cpu == -1) {
178f412eed9SJakub Kicinski 			p_err("cpu and index must be specified together");
179f412eed9SJakub Kicinski 			goto err_close_map;
180f412eed9SJakub Kicinski 		}
181f412eed9SJakub Kicinski 	} else {
1829b190f18SAndrii Nakryiko 		ctx.cpu = 0;
1839b190f18SAndrii Nakryiko 		ctx.idx = 0;
184f412eed9SJakub Kicinski 	}
185f412eed9SJakub Kicinski 
1869b190f18SAndrii Nakryiko 	opts.cpu_cnt = ctx.all_cpus ? 0 : 1;
1879b190f18SAndrii Nakryiko 	opts.cpus = &ctx.cpu;
1889b190f18SAndrii Nakryiko 	opts.map_keys = &ctx.idx;
189164b04f2SAndrii Nakryiko 	pb = perf_buffer__new_raw(map_fd, MMAP_PAGE_CNT, &perf_attr,
190164b04f2SAndrii Nakryiko 				  print_bpf_output, &ctx, &opts);
191*3ca2fb49STianyi Liu 	if (!pb) {
1929b190f18SAndrii Nakryiko 		p_err("failed to create perf buffer: %s (%d)",
193*3ca2fb49STianyi Liu 		      strerror(errno), errno);
194f412eed9SJakub Kicinski 		goto err_close_map;
195f412eed9SJakub Kicinski 	}
196f412eed9SJakub Kicinski 
197f412eed9SJakub Kicinski 	signal(SIGINT, int_exit);
198f412eed9SJakub Kicinski 	signal(SIGHUP, int_exit);
199f412eed9SJakub Kicinski 	signal(SIGTERM, int_exit);
200f412eed9SJakub Kicinski 
201f412eed9SJakub Kicinski 	if (json_output)
202f412eed9SJakub Kicinski 		jsonw_start_array(json_wtr);
203f412eed9SJakub Kicinski 
204f412eed9SJakub Kicinski 	while (!stop) {
2059b190f18SAndrii Nakryiko 		err = perf_buffer__poll(pb, 200);
2069b190f18SAndrii Nakryiko 		if (err < 0 && err != -EINTR) {
2079b190f18SAndrii Nakryiko 			p_err("perf buffer polling failed: %s (%d)",
208*3ca2fb49STianyi Liu 			      strerror(errno), errno);
2099b190f18SAndrii Nakryiko 			goto err_close_pb;
210f412eed9SJakub Kicinski 		}
2119b190f18SAndrii Nakryiko 	}
212f412eed9SJakub Kicinski 
213f412eed9SJakub Kicinski 	if (json_output)
214f412eed9SJakub Kicinski 		jsonw_end_array(json_wtr);
215f412eed9SJakub Kicinski 
2169b190f18SAndrii Nakryiko 	perf_buffer__free(pb);
217f412eed9SJakub Kicinski 	close(map_fd);
218f412eed9SJakub Kicinski 
219f412eed9SJakub Kicinski 	return 0;
220f412eed9SJakub Kicinski 
2219b190f18SAndrii Nakryiko err_close_pb:
2229b190f18SAndrii Nakryiko 	perf_buffer__free(pb);
223f412eed9SJakub Kicinski err_close_map:
224f412eed9SJakub Kicinski 	close(map_fd);
225f412eed9SJakub Kicinski 	return -1;
226f412eed9SJakub Kicinski }
227