| .. | .. |
|---|
| 9 | 9 | #include <string.h> |
|---|
| 10 | 10 | #include <unistd.h> |
|---|
| 11 | 11 | #include <fcntl.h> |
|---|
| 12 | | -#include <linux/bpf.h> |
|---|
| 13 | 12 | #include <locale.h> |
|---|
| 14 | 13 | #include <sys/types.h> |
|---|
| 15 | 14 | #include <sys/stat.h> |
|---|
| .. | .. |
|---|
| 18 | 17 | #include <sys/wait.h> |
|---|
| 19 | 18 | |
|---|
| 20 | 19 | #include <bpf/bpf.h> |
|---|
| 21 | | -#include "bpf_load.h" |
|---|
| 20 | +#include <bpf/libbpf.h> |
|---|
| 21 | + |
|---|
| 22 | +static int cstate_map_fd, pstate_map_fd; |
|---|
| 22 | 23 | |
|---|
| 23 | 24 | #define MAX_CPU 8 |
|---|
| 24 | 25 | #define MAX_PSTATE_ENTRIES 5 |
|---|
| .. | .. |
|---|
| 181 | 182 | { |
|---|
| 182 | 183 | cpu_stat_inject_cpu_idle_event(); |
|---|
| 183 | 184 | cpu_stat_inject_cpu_frequency_event(); |
|---|
| 184 | | - cpu_stat_update(map_fd[1], map_fd[2]); |
|---|
| 185 | + cpu_stat_update(cstate_map_fd, pstate_map_fd); |
|---|
| 185 | 186 | cpu_stat_print(); |
|---|
| 186 | 187 | exit(0); |
|---|
| 187 | 188 | } |
|---|
| 188 | 189 | |
|---|
| 189 | 190 | int main(int argc, char **argv) |
|---|
| 190 | 191 | { |
|---|
| 192 | + struct bpf_link *link = NULL; |
|---|
| 193 | + struct bpf_program *prog; |
|---|
| 194 | + struct bpf_object *obj; |
|---|
| 191 | 195 | char filename[256]; |
|---|
| 192 | 196 | int ret; |
|---|
| 193 | 197 | |
|---|
| 194 | 198 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
|---|
| 199 | + obj = bpf_object__open_file(filename, NULL); |
|---|
| 200 | + if (libbpf_get_error(obj)) { |
|---|
| 201 | + fprintf(stderr, "ERROR: opening BPF object file failed\n"); |
|---|
| 202 | + return 0; |
|---|
| 203 | + } |
|---|
| 195 | 204 | |
|---|
| 196 | | - if (load_bpf_file(filename)) { |
|---|
| 197 | | - printf("%s", bpf_log_buf); |
|---|
| 198 | | - return 1; |
|---|
| 205 | + prog = bpf_object__find_program_by_name(obj, "bpf_prog1"); |
|---|
| 206 | + if (!prog) { |
|---|
| 207 | + printf("finding a prog in obj file failed\n"); |
|---|
| 208 | + goto cleanup; |
|---|
| 209 | + } |
|---|
| 210 | + |
|---|
| 211 | + /* load BPF program */ |
|---|
| 212 | + if (bpf_object__load(obj)) { |
|---|
| 213 | + fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
|---|
| 214 | + goto cleanup; |
|---|
| 215 | + } |
|---|
| 216 | + |
|---|
| 217 | + cstate_map_fd = bpf_object__find_map_fd_by_name(obj, "cstate_duration"); |
|---|
| 218 | + pstate_map_fd = bpf_object__find_map_fd_by_name(obj, "pstate_duration"); |
|---|
| 219 | + if (cstate_map_fd < 0 || pstate_map_fd < 0) { |
|---|
| 220 | + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
|---|
| 221 | + goto cleanup; |
|---|
| 222 | + } |
|---|
| 223 | + |
|---|
| 224 | + link = bpf_program__attach(prog); |
|---|
| 225 | + if (libbpf_get_error(link)) { |
|---|
| 226 | + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); |
|---|
| 227 | + link = NULL; |
|---|
| 228 | + goto cleanup; |
|---|
| 199 | 229 | } |
|---|
| 200 | 230 | |
|---|
| 201 | 231 | ret = cpu_stat_inject_cpu_idle_event(); |
|---|
| .. | .. |
|---|
| 210 | 240 | signal(SIGTERM, int_exit); |
|---|
| 211 | 241 | |
|---|
| 212 | 242 | while (1) { |
|---|
| 213 | | - cpu_stat_update(map_fd[1], map_fd[2]); |
|---|
| 243 | + cpu_stat_update(cstate_map_fd, pstate_map_fd); |
|---|
| 214 | 244 | cpu_stat_print(); |
|---|
| 215 | 245 | sleep(5); |
|---|
| 216 | 246 | } |
|---|
| 217 | 247 | |
|---|
| 248 | +cleanup: |
|---|
| 249 | + bpf_link__destroy(link); |
|---|
| 250 | + bpf_object__close(obj); |
|---|
| 218 | 251 | return 0; |
|---|
| 219 | 252 | } |
|---|