Commit 22adedd3 authored by Toke Høiland-Jørgensen's avatar Toke Høiland-Jørgensen Committed by Daniel Borkmann

trace_helpers.c: Add helpers to poll multiple perf FDs for events

Add two new helper functions to trace_helpers that supports polling
multiple perf file descriptors for events. These are used to the XDP
perf_event_output example, which needs to work with one perf fd per CPU.
Reviewed-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: default avatarToke Høiland-Jørgensen <toke@toke.dk>
Acked-by: default avatarSong Liu <songliubraving@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent cc0dff6d
...@@ -88,7 +88,7 @@ static int page_size; ...@@ -88,7 +88,7 @@ static int page_size;
static int page_cnt = 8; static int page_cnt = 8;
static struct perf_event_mmap_page *header; static struct perf_event_mmap_page *header;
int perf_event_mmap(int fd) int perf_event_mmap_header(int fd, struct perf_event_mmap_page **header)
{ {
void *base; void *base;
int mmap_size; int mmap_size;
...@@ -102,10 +102,15 @@ int perf_event_mmap(int fd) ...@@ -102,10 +102,15 @@ int perf_event_mmap(int fd)
return -1; return -1;
} }
header = base; *header = base;
return 0; return 0;
} }
int perf_event_mmap(int fd)
{
return perf_event_mmap_header(fd, &header);
}
static int perf_event_poll(int fd) static int perf_event_poll(int fd)
{ {
struct pollfd pfd = { .fd = fd, .events = POLLIN }; struct pollfd pfd = { .fd = fd, .events = POLLIN };
...@@ -163,3 +168,42 @@ int perf_event_poller(int fd, perf_event_print_fn output_fn) ...@@ -163,3 +168,42 @@ int perf_event_poller(int fd, perf_event_print_fn output_fn)
return ret; return ret;
} }
int perf_event_poller_multi(int *fds, struct perf_event_mmap_page **headers,
int num_fds, perf_event_print_fn output_fn)
{
enum bpf_perf_event_ret ret;
struct pollfd *pfds;
void *buf = NULL;
size_t len = 0;
int i;
pfds = calloc(num_fds, sizeof(*pfds));
if (!pfds)
return LIBBPF_PERF_EVENT_ERROR;
for (i = 0; i < num_fds; i++) {
pfds[i].fd = fds[i];
pfds[i].events = POLLIN;
}
for (;;) {
poll(pfds, num_fds, 1000);
for (i = 0; i < num_fds; i++) {
if (!pfds[i].revents)
continue;
ret = bpf_perf_event_read_simple(headers[i],
page_cnt * page_size,
page_size, &buf, &len,
bpf_perf_event_print,
output_fn);
if (ret != LIBBPF_PERF_EVENT_CONT)
break;
}
}
free(buf);
free(pfds);
return ret;
}
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#define __TRACE_HELPER_H #define __TRACE_HELPER_H
#include <libbpf.h> #include <libbpf.h>
#include <linux/perf_event.h>
struct ksym { struct ksym {
long addr; long addr;
...@@ -16,6 +17,9 @@ long ksym_get_addr(const char *name); ...@@ -16,6 +17,9 @@ long ksym_get_addr(const char *name);
typedef enum bpf_perf_event_ret (*perf_event_print_fn)(void *data, int size); typedef enum bpf_perf_event_ret (*perf_event_print_fn)(void *data, int size);
int perf_event_mmap(int fd); int perf_event_mmap(int fd);
int perf_event_mmap_header(int fd, struct perf_event_mmap_page **header);
/* return LIBBPF_PERF_EVENT_DONE or LIBBPF_PERF_EVENT_ERROR */ /* return LIBBPF_PERF_EVENT_DONE or LIBBPF_PERF_EVENT_ERROR */
int perf_event_poller(int fd, perf_event_print_fn output_fn); int perf_event_poller(int fd, perf_event_print_fn output_fn);
int perf_event_poller_multi(int *fds, struct perf_event_mmap_page **headers,
int num_fds, perf_event_print_fn output_fn);
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment