.. | .. |
---|
1 | | -// SPDX-License-Identifier: GPL-2.0-only |
---|
| 1 | +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) |
---|
2 | 2 | /* Copyright (C) 2018 Netronome Systems, Inc. */ |
---|
3 | 3 | /* This program is free software; you can redistribute it and/or |
---|
4 | 4 | * modify it under the terms of version 2 of the GNU General Public |
---|
.. | .. |
---|
6 | 6 | */ |
---|
7 | 7 | #include <errno.h> |
---|
8 | 8 | #include <fcntl.h> |
---|
9 | | -#include <libbpf.h> |
---|
| 9 | +#include <bpf/libbpf.h> |
---|
10 | 10 | #include <poll.h> |
---|
11 | 11 | #include <signal.h> |
---|
12 | 12 | #include <stdbool.h> |
---|
.. | .. |
---|
21 | 21 | #include <sys/mman.h> |
---|
22 | 22 | #include <sys/syscall.h> |
---|
23 | 23 | |
---|
24 | | -#include <bpf.h> |
---|
| 24 | +#include <bpf/bpf.h> |
---|
25 | 25 | #include <perf-sys.h> |
---|
26 | 26 | |
---|
27 | 27 | #include "main.h" |
---|
28 | 28 | |
---|
29 | 29 | #define MMAP_PAGE_CNT 16 |
---|
30 | 30 | |
---|
31 | | -static bool stop; |
---|
| 31 | +static volatile bool stop; |
---|
32 | 32 | |
---|
33 | 33 | struct event_ring_info { |
---|
34 | 34 | int fd; |
---|
.. | .. |
---|
39 | 39 | |
---|
40 | 40 | struct perf_event_sample { |
---|
41 | 41 | struct perf_event_header header; |
---|
42 | | - u64 time; |
---|
| 42 | + __u64 time; |
---|
43 | 43 | __u32 size; |
---|
44 | 44 | unsigned char data[]; |
---|
| 45 | +}; |
---|
| 46 | + |
---|
| 47 | +struct perf_event_lost { |
---|
| 48 | + struct perf_event_header header; |
---|
| 49 | + __u64 id; |
---|
| 50 | + __u64 lost; |
---|
45 | 51 | }; |
---|
46 | 52 | |
---|
47 | 53 | static void int_exit(int signo) |
---|
.. | .. |
---|
50 | 56 | stop = true; |
---|
51 | 57 | } |
---|
52 | 58 | |
---|
53 | | -static enum bpf_perf_event_ret print_bpf_output(void *event, void *priv) |
---|
| 59 | +struct event_pipe_ctx { |
---|
| 60 | + bool all_cpus; |
---|
| 61 | + int cpu; |
---|
| 62 | + int idx; |
---|
| 63 | +}; |
---|
| 64 | + |
---|
| 65 | +static enum bpf_perf_event_ret |
---|
| 66 | +print_bpf_output(void *private_data, int cpu, struct perf_event_header *event) |
---|
54 | 67 | { |
---|
55 | | - struct event_ring_info *ring = priv; |
---|
56 | | - struct perf_event_sample *e = event; |
---|
57 | | - struct { |
---|
58 | | - struct perf_event_header header; |
---|
59 | | - __u64 id; |
---|
60 | | - __u64 lost; |
---|
61 | | - } *lost = event; |
---|
| 68 | + struct perf_event_sample *e = container_of(event, |
---|
| 69 | + struct perf_event_sample, |
---|
| 70 | + header); |
---|
| 71 | + struct perf_event_lost *lost = container_of(event, |
---|
| 72 | + struct perf_event_lost, |
---|
| 73 | + header); |
---|
| 74 | + struct event_pipe_ctx *ctx = private_data; |
---|
| 75 | + int idx = ctx->all_cpus ? cpu : ctx->idx; |
---|
62 | 76 | |
---|
63 | 77 | if (json_output) { |
---|
64 | 78 | jsonw_start_object(json_wtr); |
---|
65 | 79 | jsonw_name(json_wtr, "type"); |
---|
66 | 80 | jsonw_uint(json_wtr, e->header.type); |
---|
67 | 81 | jsonw_name(json_wtr, "cpu"); |
---|
68 | | - jsonw_uint(json_wtr, ring->cpu); |
---|
| 82 | + jsonw_uint(json_wtr, cpu); |
---|
69 | 83 | jsonw_name(json_wtr, "index"); |
---|
70 | | - jsonw_uint(json_wtr, ring->key); |
---|
| 84 | + jsonw_uint(json_wtr, idx); |
---|
71 | 85 | if (e->header.type == PERF_RECORD_SAMPLE) { |
---|
72 | 86 | jsonw_name(json_wtr, "timestamp"); |
---|
73 | 87 | jsonw_uint(json_wtr, e->time); |
---|
.. | .. |
---|
87 | 101 | if (e->header.type == PERF_RECORD_SAMPLE) { |
---|
88 | 102 | printf("== @%lld.%09lld CPU: %d index: %d =====\n", |
---|
89 | 103 | e->time / 1000000000ULL, e->time % 1000000000ULL, |
---|
90 | | - ring->cpu, ring->key); |
---|
| 104 | + cpu, idx); |
---|
91 | 105 | fprint_hex(stdout, e->data, e->size, " "); |
---|
92 | 106 | printf("\n"); |
---|
93 | 107 | } else if (e->header.type == PERF_RECORD_LOST) { |
---|
.. | .. |
---|
101 | 115 | return LIBBPF_PERF_EVENT_CONT; |
---|
102 | 116 | } |
---|
103 | 117 | |
---|
104 | | -static void |
---|
105 | | -perf_event_read(struct event_ring_info *ring, void **buf, size_t *buf_len) |
---|
| 118 | +int do_event_pipe(int argc, char **argv) |
---|
106 | 119 | { |
---|
107 | | - enum bpf_perf_event_ret ret; |
---|
108 | | - |
---|
109 | | - ret = bpf_perf_event_read_simple(ring->mem, |
---|
110 | | - MMAP_PAGE_CNT * get_page_size(), |
---|
111 | | - get_page_size(), buf, buf_len, |
---|
112 | | - print_bpf_output, ring); |
---|
113 | | - if (ret != LIBBPF_PERF_EVENT_CONT) { |
---|
114 | | - fprintf(stderr, "perf read loop failed with %d\n", ret); |
---|
115 | | - stop = true; |
---|
116 | | - } |
---|
117 | | -} |
---|
118 | | - |
---|
119 | | -static int perf_mmap_size(void) |
---|
120 | | -{ |
---|
121 | | - return get_page_size() * (MMAP_PAGE_CNT + 1); |
---|
122 | | -} |
---|
123 | | - |
---|
124 | | -static void *perf_event_mmap(int fd) |
---|
125 | | -{ |
---|
126 | | - int mmap_size = perf_mmap_size(); |
---|
127 | | - void *base; |
---|
128 | | - |
---|
129 | | - base = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
---|
130 | | - if (base == MAP_FAILED) { |
---|
131 | | - p_err("event mmap failed: %s\n", strerror(errno)); |
---|
132 | | - return NULL; |
---|
133 | | - } |
---|
134 | | - |
---|
135 | | - return base; |
---|
136 | | -} |
---|
137 | | - |
---|
138 | | -static void perf_event_unmap(void *mem) |
---|
139 | | -{ |
---|
140 | | - if (munmap(mem, perf_mmap_size())) |
---|
141 | | - fprintf(stderr, "Can't unmap ring memory!\n"); |
---|
142 | | -} |
---|
143 | | - |
---|
144 | | -static int bpf_perf_event_open(int map_fd, int key, int cpu) |
---|
145 | | -{ |
---|
146 | | - struct perf_event_attr attr = { |
---|
| 120 | + struct perf_event_attr perf_attr = { |
---|
147 | 121 | .sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_TIME, |
---|
148 | 122 | .type = PERF_TYPE_SOFTWARE, |
---|
149 | 123 | .config = PERF_COUNT_SW_BPF_OUTPUT, |
---|
| 124 | + .sample_period = 1, |
---|
| 125 | + .wakeup_events = 1, |
---|
150 | 126 | }; |
---|
151 | | - int pmu_fd; |
---|
152 | | - |
---|
153 | | - pmu_fd = sys_perf_event_open(&attr, -1, cpu, -1, 0); |
---|
154 | | - if (pmu_fd < 0) { |
---|
155 | | - p_err("failed to open perf event %d for CPU %d", key, cpu); |
---|
156 | | - return -1; |
---|
157 | | - } |
---|
158 | | - |
---|
159 | | - if (bpf_map_update_elem(map_fd, &key, &pmu_fd, BPF_ANY)) { |
---|
160 | | - p_err("failed to update map for event %d for CPU %d", key, cpu); |
---|
161 | | - goto err_close; |
---|
162 | | - } |
---|
163 | | - if (ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0)) { |
---|
164 | | - p_err("failed to enable event %d for CPU %d", key, cpu); |
---|
165 | | - goto err_close; |
---|
166 | | - } |
---|
167 | | - |
---|
168 | | - return pmu_fd; |
---|
169 | | - |
---|
170 | | -err_close: |
---|
171 | | - close(pmu_fd); |
---|
172 | | - return -1; |
---|
173 | | -} |
---|
174 | | - |
---|
175 | | -int do_event_pipe(int argc, char **argv) |
---|
176 | | -{ |
---|
177 | | - int i, nfds, map_fd, index = -1, cpu = -1; |
---|
178 | 127 | struct bpf_map_info map_info = {}; |
---|
179 | | - struct event_ring_info *rings; |
---|
180 | | - size_t tmp_buf_sz = 0; |
---|
181 | | - void *tmp_buf = NULL; |
---|
182 | | - struct pollfd *pfds; |
---|
| 128 | + struct perf_buffer_raw_opts opts = {}; |
---|
| 129 | + struct event_pipe_ctx ctx = { |
---|
| 130 | + .all_cpus = true, |
---|
| 131 | + .cpu = -1, |
---|
| 132 | + .idx = -1, |
---|
| 133 | + }; |
---|
| 134 | + struct perf_buffer *pb; |
---|
183 | 135 | __u32 map_info_len; |
---|
184 | | - bool do_all = true; |
---|
| 136 | + int err, map_fd; |
---|
185 | 137 | |
---|
186 | 138 | map_info_len = sizeof(map_info); |
---|
187 | 139 | map_fd = map_parse_fd_and_info(&argc, &argv, &map_info, &map_info_len); |
---|
.. | .. |
---|
203 | 155 | char *endptr; |
---|
204 | 156 | |
---|
205 | 157 | NEXT_ARG(); |
---|
206 | | - cpu = strtoul(*argv, &endptr, 0); |
---|
| 158 | + ctx.cpu = strtoul(*argv, &endptr, 0); |
---|
207 | 159 | if (*endptr) { |
---|
208 | 160 | p_err("can't parse %s as CPU ID", *argv); |
---|
209 | 161 | goto err_close_map; |
---|
.. | .. |
---|
214 | 166 | char *endptr; |
---|
215 | 167 | |
---|
216 | 168 | NEXT_ARG(); |
---|
217 | | - index = strtoul(*argv, &endptr, 0); |
---|
| 169 | + ctx.idx = strtoul(*argv, &endptr, 0); |
---|
218 | 170 | if (*endptr) { |
---|
219 | 171 | p_err("can't parse %s as index", *argv); |
---|
220 | 172 | goto err_close_map; |
---|
.. | .. |
---|
226 | 178 | goto err_close_map; |
---|
227 | 179 | } |
---|
228 | 180 | |
---|
229 | | - do_all = false; |
---|
| 181 | + ctx.all_cpus = false; |
---|
230 | 182 | } |
---|
231 | 183 | |
---|
232 | | - if (!do_all) { |
---|
233 | | - if (index == -1 || cpu == -1) { |
---|
| 184 | + if (!ctx.all_cpus) { |
---|
| 185 | + if (ctx.idx == -1 || ctx.cpu == -1) { |
---|
234 | 186 | p_err("cpu and index must be specified together"); |
---|
235 | 187 | goto err_close_map; |
---|
236 | 188 | } |
---|
237 | | - |
---|
238 | | - nfds = 1; |
---|
239 | 189 | } else { |
---|
240 | | - nfds = min(get_possible_cpus(), map_info.max_entries); |
---|
241 | | - cpu = 0; |
---|
242 | | - index = 0; |
---|
| 190 | + ctx.cpu = 0; |
---|
| 191 | + ctx.idx = 0; |
---|
243 | 192 | } |
---|
244 | 193 | |
---|
245 | | - rings = calloc(nfds, sizeof(rings[0])); |
---|
246 | | - if (!rings) |
---|
| 194 | + opts.attr = &perf_attr; |
---|
| 195 | + opts.event_cb = print_bpf_output; |
---|
| 196 | + opts.ctx = &ctx; |
---|
| 197 | + opts.cpu_cnt = ctx.all_cpus ? 0 : 1; |
---|
| 198 | + opts.cpus = &ctx.cpu; |
---|
| 199 | + opts.map_keys = &ctx.idx; |
---|
| 200 | + |
---|
| 201 | + pb = perf_buffer__new_raw(map_fd, MMAP_PAGE_CNT, &opts); |
---|
| 202 | + err = libbpf_get_error(pb); |
---|
| 203 | + if (err) { |
---|
| 204 | + p_err("failed to create perf buffer: %s (%d)", |
---|
| 205 | + strerror(err), err); |
---|
247 | 206 | goto err_close_map; |
---|
248 | | - |
---|
249 | | - pfds = calloc(nfds, sizeof(pfds[0])); |
---|
250 | | - if (!pfds) |
---|
251 | | - goto err_free_rings; |
---|
252 | | - |
---|
253 | | - for (i = 0; i < nfds; i++) { |
---|
254 | | - rings[i].cpu = cpu + i; |
---|
255 | | - rings[i].key = index + i; |
---|
256 | | - |
---|
257 | | - rings[i].fd = bpf_perf_event_open(map_fd, rings[i].key, |
---|
258 | | - rings[i].cpu); |
---|
259 | | - if (rings[i].fd < 0) |
---|
260 | | - goto err_close_fds_prev; |
---|
261 | | - |
---|
262 | | - rings[i].mem = perf_event_mmap(rings[i].fd); |
---|
263 | | - if (!rings[i].mem) |
---|
264 | | - goto err_close_fds_current; |
---|
265 | | - |
---|
266 | | - pfds[i].fd = rings[i].fd; |
---|
267 | | - pfds[i].events = POLLIN; |
---|
268 | 207 | } |
---|
269 | 208 | |
---|
270 | 209 | signal(SIGINT, int_exit); |
---|
.. | .. |
---|
275 | 214 | jsonw_start_array(json_wtr); |
---|
276 | 215 | |
---|
277 | 216 | while (!stop) { |
---|
278 | | - poll(pfds, nfds, 200); |
---|
279 | | - for (i = 0; i < nfds; i++) |
---|
280 | | - perf_event_read(&rings[i], &tmp_buf, &tmp_buf_sz); |
---|
| 217 | + err = perf_buffer__poll(pb, 200); |
---|
| 218 | + if (err < 0 && err != -EINTR) { |
---|
| 219 | + p_err("perf buffer polling failed: %s (%d)", |
---|
| 220 | + strerror(err), err); |
---|
| 221 | + goto err_close_pb; |
---|
| 222 | + } |
---|
281 | 223 | } |
---|
282 | | - free(tmp_buf); |
---|
283 | 224 | |
---|
284 | 225 | if (json_output) |
---|
285 | 226 | jsonw_end_array(json_wtr); |
---|
286 | 227 | |
---|
287 | | - for (i = 0; i < nfds; i++) { |
---|
288 | | - perf_event_unmap(rings[i].mem); |
---|
289 | | - close(rings[i].fd); |
---|
290 | | - } |
---|
291 | | - free(pfds); |
---|
292 | | - free(rings); |
---|
| 228 | + perf_buffer__free(pb); |
---|
293 | 229 | close(map_fd); |
---|
294 | 230 | |
---|
295 | 231 | return 0; |
---|
296 | 232 | |
---|
297 | | -err_close_fds_prev: |
---|
298 | | - while (i--) { |
---|
299 | | - perf_event_unmap(rings[i].mem); |
---|
300 | | -err_close_fds_current: |
---|
301 | | - close(rings[i].fd); |
---|
302 | | - } |
---|
303 | | - free(pfds); |
---|
304 | | -err_free_rings: |
---|
305 | | - free(rings); |
---|
| 233 | +err_close_pb: |
---|
| 234 | + perf_buffer__free(pb); |
---|
306 | 235 | err_close_map: |
---|
307 | 236 | close(map_fd); |
---|
308 | 237 | return -1; |
---|