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.
9 #include <bpf/libbpf.h>
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
31 static volatile bool stop
;
33 struct event_ring_info
{
40 struct perf_event_sample
{
41 struct perf_event_header header
;
47 struct perf_event_lost
{
48 struct perf_event_header header
;
53 static void int_exit(int signo
)
55 fprintf(stderr
, "Stopping...\n");
59 struct event_pipe_ctx
{
65 static enum bpf_perf_event_ret
66 print_bpf_output(void *private_data
, int cpu
, struct perf_event_header
*event
)
68 struct perf_event_sample
*e
= container_of(event
,
69 struct perf_event_sample
,
71 struct perf_event_lost
*lost
= container_of(event
,
72 struct perf_event_lost
,
74 struct event_pipe_ctx
*ctx
= private_data
;
75 int idx
= ctx
->all_cpus
? cpu
: ctx
->idx
;
78 jsonw_start_object(json_wtr
);
79 jsonw_name(json_wtr
, "type");
80 jsonw_uint(json_wtr
, e
->header
.type
);
81 jsonw_name(json_wtr
, "cpu");
82 jsonw_uint(json_wtr
, cpu
);
83 jsonw_name(json_wtr
, "index");
84 jsonw_uint(json_wtr
, idx
);
85 if (e
->header
.type
== PERF_RECORD_SAMPLE
) {
86 jsonw_name(json_wtr
, "timestamp");
87 jsonw_uint(json_wtr
, e
->time
);
88 jsonw_name(json_wtr
, "data");
89 print_data_json(e
->data
, e
->size
);
90 } else if (e
->header
.type
== PERF_RECORD_LOST
) {
91 jsonw_name(json_wtr
, "lost");
92 jsonw_start_object(json_wtr
);
93 jsonw_name(json_wtr
, "id");
94 jsonw_uint(json_wtr
, lost
->id
);
95 jsonw_name(json_wtr
, "count");
96 jsonw_uint(json_wtr
, lost
->lost
);
97 jsonw_end_object(json_wtr
);
99 jsonw_end_object(json_wtr
);
101 if (e
->header
.type
== PERF_RECORD_SAMPLE
) {
102 printf("== @%lld.%09lld CPU: %d index: %d =====\n",
103 e
->time
/ 1000000000ULL, e
->time
% 1000000000ULL,
105 fprint_hex(stdout
, e
->data
, e
->size
, " ");
107 } else if (e
->header
.type
== PERF_RECORD_LOST
) {
108 printf("lost %lld events\n", lost
->lost
);
110 printf("unknown event type=%d size=%d\n",
111 e
->header
.type
, e
->header
.size
);
115 return LIBBPF_PERF_EVENT_CONT
;
118 int do_event_pipe(int argc
, char **argv
)
120 struct perf_event_attr perf_attr
= {
121 .sample_type
= PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
,
122 .type
= PERF_TYPE_SOFTWARE
,
123 .config
= PERF_COUNT_SW_BPF_OUTPUT
,
127 struct bpf_map_info map_info
= {};
128 struct perf_buffer_raw_opts opts
= {};
129 struct event_pipe_ctx ctx
= {
134 struct perf_buffer
*pb
;
138 map_info_len
= sizeof(map_info
);
139 map_fd
= map_parse_fd_and_info(&argc
, &argv
, &map_info
, &map_info_len
);
143 if (map_info
.type
!= BPF_MAP_TYPE_PERF_EVENT_ARRAY
) {
144 p_err("map is not a perf event array");
154 if (is_prefix(*argv
, "cpu")) {
158 ctx
.cpu
= strtoul(*argv
, &endptr
, 0);
160 p_err("can't parse %s as CPU ID", *argv
);
165 } else if (is_prefix(*argv
, "index")) {
169 ctx
.idx
= strtoul(*argv
, &endptr
, 0);
171 p_err("can't parse %s as index", *argv
);
181 ctx
.all_cpus
= false;
185 if (ctx
.idx
== -1 || ctx
.cpu
== -1) {
186 p_err("cpu and index must be specified together");
194 opts
.attr
= &perf_attr
;
195 opts
.event_cb
= print_bpf_output
;
197 opts
.cpu_cnt
= ctx
.all_cpus
? 0 : 1;
198 opts
.cpus
= &ctx
.cpu
;
199 opts
.map_keys
= &ctx
.idx
;
201 pb
= perf_buffer__new_raw(map_fd
, MMAP_PAGE_CNT
, &opts
);
202 err
= libbpf_get_error(pb
);
204 p_err("failed to create perf buffer: %s (%d)",
209 signal(SIGINT
, int_exit
);
210 signal(SIGHUP
, int_exit
);
211 signal(SIGTERM
, int_exit
);
214 jsonw_start_array(json_wtr
);
217 err
= perf_buffer__poll(pb
, 200);
218 if (err
< 0 && err
!= -EINTR
) {
219 p_err("perf buffer polling failed: %s (%d)",
226 jsonw_end_array(json_wtr
);
228 perf_buffer__free(pb
);
234 perf_buffer__free(pb
);