.. | .. |
---|
4 | 4 | #include <assert.h> |
---|
5 | 5 | #include <fcntl.h> |
---|
6 | 6 | #include <linux/perf_event.h> |
---|
7 | | -#include <linux/bpf.h> |
---|
8 | 7 | #include <sched.h> |
---|
9 | 8 | #include <stdio.h> |
---|
10 | 9 | #include <stdlib.h> |
---|
.. | .. |
---|
15 | 14 | #include <sys/wait.h> |
---|
16 | 15 | #include <unistd.h> |
---|
17 | 16 | |
---|
18 | | -#include "bpf_load.h" |
---|
19 | 17 | #include <bpf/bpf.h> |
---|
| 18 | +#include <bpf/libbpf.h> |
---|
20 | 19 | #include "perf-sys.h" |
---|
21 | 20 | |
---|
22 | 21 | #define SAMPLE_PERIOD 0x7fffffffffffffffULL |
---|
| 22 | + |
---|
| 23 | +/* counters, values, values2 */ |
---|
| 24 | +static int map_fd[3]; |
---|
23 | 25 | |
---|
24 | 26 | static void check_on_cpu(int cpu, struct perf_event_attr *attr) |
---|
25 | 27 | { |
---|
.. | .. |
---|
174 | 176 | int main(int argc, char **argv) |
---|
175 | 177 | { |
---|
176 | 178 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
---|
| 179 | + struct bpf_link *links[2]; |
---|
| 180 | + struct bpf_program *prog; |
---|
| 181 | + struct bpf_object *obj; |
---|
177 | 182 | char filename[256]; |
---|
178 | | - |
---|
179 | | - snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
---|
| 183 | + int i = 0; |
---|
180 | 184 | |
---|
181 | 185 | setrlimit(RLIMIT_MEMLOCK, &r); |
---|
182 | | - if (load_bpf_file(filename)) { |
---|
183 | | - printf("%s", bpf_log_buf); |
---|
184 | | - return 1; |
---|
| 186 | + |
---|
| 187 | + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
---|
| 188 | + obj = bpf_object__open_file(filename, NULL); |
---|
| 189 | + if (libbpf_get_error(obj)) { |
---|
| 190 | + fprintf(stderr, "ERROR: opening BPF object file failed\n"); |
---|
| 191 | + return 0; |
---|
| 192 | + } |
---|
| 193 | + |
---|
| 194 | + /* load BPF program */ |
---|
| 195 | + if (bpf_object__load(obj)) { |
---|
| 196 | + fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
---|
| 197 | + goto cleanup; |
---|
| 198 | + } |
---|
| 199 | + |
---|
| 200 | + map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counters"); |
---|
| 201 | + map_fd[1] = bpf_object__find_map_fd_by_name(obj, "values"); |
---|
| 202 | + map_fd[2] = bpf_object__find_map_fd_by_name(obj, "values2"); |
---|
| 203 | + if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0) { |
---|
| 204 | + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
---|
| 205 | + goto cleanup; |
---|
| 206 | + } |
---|
| 207 | + |
---|
| 208 | + bpf_object__for_each_program(prog, obj) { |
---|
| 209 | + links[i] = bpf_program__attach(prog); |
---|
| 210 | + if (libbpf_get_error(links[i])) { |
---|
| 211 | + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); |
---|
| 212 | + links[i] = NULL; |
---|
| 213 | + goto cleanup; |
---|
| 214 | + } |
---|
| 215 | + i++; |
---|
185 | 216 | } |
---|
186 | 217 | |
---|
187 | 218 | test_bpf_perf_event(); |
---|
| 219 | + |
---|
| 220 | +cleanup: |
---|
| 221 | + for (i--; i >= 0; i--) |
---|
| 222 | + bpf_link__destroy(links[i]); |
---|
| 223 | + |
---|
| 224 | + bpf_object__close(obj); |
---|
188 | 225 | return 0; |
---|
189 | 226 | } |
---|