0001
0002 #ifndef __PERF_BPF_COUNTER_H
0003 #define __PERF_BPF_COUNTER_H 1
0004
0005 #include <linux/list.h>
0006 #include <sys/resource.h>
0007 #include <bpf/bpf.h>
0008 #include <bpf/btf.h>
0009 #include <bpf/libbpf.h>
0010
0011 struct evsel;
0012 struct target;
0013 struct bpf_counter;
0014
0015 typedef int (*bpf_counter_evsel_op)(struct evsel *evsel);
0016 typedef int (*bpf_counter_evsel_target_op)(struct evsel *evsel,
0017 struct target *target);
0018 typedef int (*bpf_counter_evsel_install_pe_op)(struct evsel *evsel,
0019 int cpu_map_idx,
0020 int fd);
0021
0022 struct bpf_counter_ops {
0023 bpf_counter_evsel_target_op load;
0024 bpf_counter_evsel_op enable;
0025 bpf_counter_evsel_op disable;
0026 bpf_counter_evsel_op read;
0027 bpf_counter_evsel_op destroy;
0028 bpf_counter_evsel_install_pe_op install_pe;
0029 };
0030
0031 struct bpf_counter {
0032 void *skel;
0033 struct list_head list;
0034 };
0035
0036 #ifdef HAVE_BPF_SKEL
0037
0038 int bpf_counter__load(struct evsel *evsel, struct target *target);
0039 int bpf_counter__enable(struct evsel *evsel);
0040 int bpf_counter__disable(struct evsel *evsel);
0041 int bpf_counter__read(struct evsel *evsel);
0042 void bpf_counter__destroy(struct evsel *evsel);
0043 int bpf_counter__install_pe(struct evsel *evsel, int cpu_map_idx, int fd);
0044
0045 #else
0046
0047 #include <linux/err.h>
0048
0049 static inline int bpf_counter__load(struct evsel *evsel __maybe_unused,
0050 struct target *target __maybe_unused)
0051 {
0052 return 0;
0053 }
0054
0055 static inline int bpf_counter__enable(struct evsel *evsel __maybe_unused)
0056 {
0057 return 0;
0058 }
0059
0060 static inline int bpf_counter__disable(struct evsel *evsel __maybe_unused)
0061 {
0062 return 0;
0063 }
0064
0065 static inline int bpf_counter__read(struct evsel *evsel __maybe_unused)
0066 {
0067 return -EAGAIN;
0068 }
0069
0070 static inline void bpf_counter__destroy(struct evsel *evsel __maybe_unused)
0071 {
0072 }
0073
0074 static inline int bpf_counter__install_pe(struct evsel *evsel __maybe_unused,
0075 int cpu __maybe_unused,
0076 int fd __maybe_unused)
0077 {
0078 return 0;
0079 }
0080
0081 #endif
0082
0083 static inline void set_max_rlimit(void)
0084 {
0085 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
0086
0087 setrlimit(RLIMIT_MEMLOCK, &rinf);
0088 }
0089
0090 static inline __u32 bpf_link_get_id(int fd)
0091 {
0092 struct bpf_link_info link_info = { .id = 0, };
0093 __u32 link_info_len = sizeof(link_info);
0094
0095 bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
0096 return link_info.id;
0097 }
0098
0099 static inline __u32 bpf_link_get_prog_id(int fd)
0100 {
0101 struct bpf_link_info link_info = { .id = 0, };
0102 __u32 link_info_len = sizeof(link_info);
0103
0104 bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len);
0105 return link_info.prog_id;
0106 }
0107
0108 static inline __u32 bpf_map_get_id(int fd)
0109 {
0110 struct bpf_map_info map_info = { .id = 0, };
0111 __u32 map_info_len = sizeof(map_info);
0112
0113 bpf_obj_get_info_by_fd(fd, &map_info, &map_info_len);
0114 return map_info.id;
0115 }
0116
0117
0118 static inline int bperf_trigger_reading(int prog_fd, int cpu)
0119 {
0120 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
0121 .ctx_in = NULL,
0122 .ctx_size_in = 0,
0123 .flags = BPF_F_TEST_RUN_ON_CPU,
0124 .cpu = cpu,
0125 .retval = 0,
0126 );
0127
0128 return bpf_prog_test_run_opts(prog_fd, &opts);
0129 }
0130
0131 #endif