1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2018 Netronome Systems, Inc. */
3 /* This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
18 #include <linux/bpf.h>
19 #include <linux/perf_event.h>
20 #include <sys/ioctl.h>
22 #include <sys/syscall.h>
29 #define MMAP_PAGE_CNT 16
33 struct event_ring_info
{
40 struct perf_event_sample
{
41 struct perf_event_header header
;
47 static void int_exit(int signo
)
49 fprintf(stderr
, "Stopping...\n");
53 static enum bpf_perf_event_ret
54 print_bpf_output(struct perf_event_header
*event
, void *private_data
)
56 struct perf_event_sample
*e
= container_of(event
, struct perf_event_sample
,
58 struct event_ring_info
*ring
= private_data
;
60 struct perf_event_header header
;
63 } *lost
= (typeof(lost
))event
;
66 jsonw_start_object(json_wtr
);
67 jsonw_name(json_wtr
, "type");
68 jsonw_uint(json_wtr
, e
->header
.type
);
69 jsonw_name(json_wtr
, "cpu");
70 jsonw_uint(json_wtr
, ring
->cpu
);
71 jsonw_name(json_wtr
, "index");
72 jsonw_uint(json_wtr
, ring
->key
);
73 if (e
->header
.type
== PERF_RECORD_SAMPLE
) {
74 jsonw_name(json_wtr
, "timestamp");
75 jsonw_uint(json_wtr
, e
->time
);
76 jsonw_name(json_wtr
, "data");
77 print_data_json(e
->data
, e
->size
);
78 } else if (e
->header
.type
== PERF_RECORD_LOST
) {
79 jsonw_name(json_wtr
, "lost");
80 jsonw_start_object(json_wtr
);
81 jsonw_name(json_wtr
, "id");
82 jsonw_uint(json_wtr
, lost
->id
);
83 jsonw_name(json_wtr
, "count");
84 jsonw_uint(json_wtr
, lost
->lost
);
85 jsonw_end_object(json_wtr
);
87 jsonw_end_object(json_wtr
);
89 if (e
->header
.type
== PERF_RECORD_SAMPLE
) {
90 printf("== @%lld.%09lld CPU: %d index: %d =====\n",
91 e
->time
/ 1000000000ULL, e
->time
% 1000000000ULL,
92 ring
->cpu
, ring
->key
);
93 fprint_hex(stdout
, e
->data
, e
->size
, " ");
95 } else if (e
->header
.type
== PERF_RECORD_LOST
) {
96 printf("lost %lld events\n", lost
->lost
);
98 printf("unknown event type=%d size=%d\n",
99 e
->header
.type
, e
->header
.size
);
103 return LIBBPF_PERF_EVENT_CONT
;
107 perf_event_read(struct event_ring_info
*ring
, void **buf
, size_t *buf_len
)
109 enum bpf_perf_event_ret ret
;
111 ret
= bpf_perf_event_read_simple(ring
->mem
,
112 MMAP_PAGE_CNT
* get_page_size(),
113 get_page_size(), buf
, buf_len
,
114 print_bpf_output
, ring
);
115 if (ret
!= LIBBPF_PERF_EVENT_CONT
) {
116 fprintf(stderr
, "perf read loop failed with %d\n", ret
);
121 static int perf_mmap_size(void)
123 return get_page_size() * (MMAP_PAGE_CNT
+ 1);
126 static void *perf_event_mmap(int fd
)
128 int mmap_size
= perf_mmap_size();
131 base
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
132 if (base
== MAP_FAILED
) {
133 p_err("event mmap failed: %s\n", strerror(errno
));
140 static void perf_event_unmap(void *mem
)
142 if (munmap(mem
, perf_mmap_size()))
143 fprintf(stderr
, "Can't unmap ring memory!\n");
146 static int bpf_perf_event_open(int map_fd
, int key
, int cpu
)
148 struct perf_event_attr attr
= {
149 .sample_type
= PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
,
150 .type
= PERF_TYPE_SOFTWARE
,
151 .config
= PERF_COUNT_SW_BPF_OUTPUT
,
155 pmu_fd
= sys_perf_event_open(&attr
, -1, cpu
, -1, 0);
157 p_err("failed to open perf event %d for CPU %d", key
, cpu
);
161 if (bpf_map_update_elem(map_fd
, &key
, &pmu_fd
, BPF_ANY
)) {
162 p_err("failed to update map for event %d for CPU %d", key
, cpu
);
165 if (ioctl(pmu_fd
, PERF_EVENT_IOC_ENABLE
, 0)) {
166 p_err("failed to enable event %d for CPU %d", key
, cpu
);
177 int do_event_pipe(int argc
, char **argv
)
179 int i
, nfds
, map_fd
, index
= -1, cpu
= -1;
180 struct bpf_map_info map_info
= {};
181 struct event_ring_info
*rings
;
182 size_t tmp_buf_sz
= 0;
183 void *tmp_buf
= NULL
;
188 map_info_len
= sizeof(map_info
);
189 map_fd
= map_parse_fd_and_info(&argc
, &argv
, &map_info
, &map_info_len
);
193 if (map_info
.type
!= BPF_MAP_TYPE_PERF_EVENT_ARRAY
) {
194 p_err("map is not a perf event array");
204 if (is_prefix(*argv
, "cpu")) {
208 cpu
= strtoul(*argv
, &endptr
, 0);
210 p_err("can't parse %s as CPU ID", **argv
);
215 } else if (is_prefix(*argv
, "index")) {
219 index
= strtoul(*argv
, &endptr
, 0);
221 p_err("can't parse %s as index", **argv
);
235 if (index
== -1 || cpu
== -1) {
236 p_err("cpu and index must be specified together");
242 nfds
= min(get_possible_cpus(), map_info
.max_entries
);
247 rings
= calloc(nfds
, sizeof(rings
[0]));
251 pfds
= calloc(nfds
, sizeof(pfds
[0]));
255 for (i
= 0; i
< nfds
; i
++) {
256 rings
[i
].cpu
= cpu
+ i
;
257 rings
[i
].key
= index
+ i
;
259 rings
[i
].fd
= bpf_perf_event_open(map_fd
, rings
[i
].key
,
262 goto err_close_fds_prev
;
264 rings
[i
].mem
= perf_event_mmap(rings
[i
].fd
);
266 goto err_close_fds_current
;
268 pfds
[i
].fd
= rings
[i
].fd
;
269 pfds
[i
].events
= POLLIN
;
272 signal(SIGINT
, int_exit
);
273 signal(SIGHUP
, int_exit
);
274 signal(SIGTERM
, int_exit
);
277 jsonw_start_array(json_wtr
);
280 poll(pfds
, nfds
, 200);
281 for (i
= 0; i
< nfds
; i
++)
282 perf_event_read(&rings
[i
], &tmp_buf
, &tmp_buf_sz
);
287 jsonw_end_array(json_wtr
);
289 for (i
= 0; i
< nfds
; i
++) {
290 perf_event_unmap(rings
[i
].mem
);
301 perf_event_unmap(rings
[i
].mem
);
302 err_close_fds_current
: