| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com |
|---|
| 2 | | - * |
|---|
| 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. |
|---|
| 6 | 3 | */ |
|---|
| 7 | 4 | #include <stdio.h> |
|---|
| 8 | 5 | #include <stdlib.h> |
|---|
| .. | .. |
|---|
| 10 | 7 | #include <unistd.h> |
|---|
| 11 | 8 | #include <stdbool.h> |
|---|
| 12 | 9 | #include <string.h> |
|---|
| 13 | | -#include <linux/bpf.h> |
|---|
| 14 | 10 | #include <sys/resource.h> |
|---|
| 15 | 11 | |
|---|
| 16 | 12 | #include <bpf/bpf.h> |
|---|
| 17 | | -#include "bpf_load.h" |
|---|
| 13 | +#include <bpf/libbpf.h> |
|---|
| 18 | 14 | #include "bpf_util.h" |
|---|
| 19 | | - |
|---|
| 20 | | -#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
|---|
| 21 | 15 | |
|---|
| 22 | 16 | #define SLOTS 100 |
|---|
| 23 | 17 | |
|---|
| .. | .. |
|---|
| 113 | 107 | |
|---|
| 114 | 108 | int main(int ac, char **argv) |
|---|
| 115 | 109 | { |
|---|
| 116 | | - struct rlimit r = {1024*1024, RLIM_INFINITY}; |
|---|
| 110 | + struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
|---|
| 111 | + struct bpf_link *links[2]; |
|---|
| 112 | + struct bpf_program *prog; |
|---|
| 113 | + struct bpf_object *obj; |
|---|
| 117 | 114 | char filename[256]; |
|---|
| 118 | | - int i; |
|---|
| 119 | | - |
|---|
| 120 | | - snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
|---|
| 121 | | - |
|---|
| 122 | | - if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
|---|
| 123 | | - perror("setrlimit(RLIMIT_MEMLOCK)"); |
|---|
| 124 | | - return 1; |
|---|
| 125 | | - } |
|---|
| 126 | | - |
|---|
| 127 | | - if (load_bpf_file(filename)) { |
|---|
| 128 | | - printf("%s", bpf_log_buf); |
|---|
| 129 | | - return 1; |
|---|
| 130 | | - } |
|---|
| 115 | + int map_fd, i, j = 0; |
|---|
| 131 | 116 | |
|---|
| 132 | 117 | for (i = 1; i < ac; i++) { |
|---|
| 133 | 118 | if (strcmp(argv[i], "-a") == 0) { |
|---|
| .. | .. |
|---|
| 140 | 125 | " -t text only\n"); |
|---|
| 141 | 126 | return 1; |
|---|
| 142 | 127 | } |
|---|
| 128 | + } |
|---|
| 129 | + |
|---|
| 130 | + if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
|---|
| 131 | + perror("setrlimit(RLIMIT_MEMLOCK)"); |
|---|
| 132 | + return 1; |
|---|
| 133 | + } |
|---|
| 134 | + |
|---|
| 135 | + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
|---|
| 136 | + obj = bpf_object__open_file(filename, NULL); |
|---|
| 137 | + if (libbpf_get_error(obj)) { |
|---|
| 138 | + fprintf(stderr, "ERROR: opening BPF object file failed\n"); |
|---|
| 139 | + return 0; |
|---|
| 140 | + } |
|---|
| 141 | + |
|---|
| 142 | + /* load BPF program */ |
|---|
| 143 | + if (bpf_object__load(obj)) { |
|---|
| 144 | + fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
|---|
| 145 | + goto cleanup; |
|---|
| 146 | + } |
|---|
| 147 | + |
|---|
| 148 | + map_fd = bpf_object__find_map_fd_by_name(obj, "lat_map"); |
|---|
| 149 | + if (map_fd < 0) { |
|---|
| 150 | + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
|---|
| 151 | + goto cleanup; |
|---|
| 152 | + } |
|---|
| 153 | + |
|---|
| 154 | + bpf_object__for_each_program(prog, obj) { |
|---|
| 155 | + links[j] = bpf_program__attach(prog); |
|---|
| 156 | + if (libbpf_get_error(links[j])) { |
|---|
| 157 | + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); |
|---|
| 158 | + links[j] = NULL; |
|---|
| 159 | + goto cleanup; |
|---|
| 160 | + } |
|---|
| 161 | + j++; |
|---|
| 143 | 162 | } |
|---|
| 144 | 163 | |
|---|
| 145 | 164 | printf(" heatmap of IO latency\n"); |
|---|
| .. | .. |
|---|
| 158 | 177 | for (i = 0; ; i++) { |
|---|
| 159 | 178 | if (i % 20 == 0) |
|---|
| 160 | 179 | print_banner(); |
|---|
| 161 | | - print_hist(map_fd[1]); |
|---|
| 180 | + print_hist(map_fd); |
|---|
| 162 | 181 | sleep(2); |
|---|
| 163 | 182 | } |
|---|
| 164 | 183 | |
|---|
| 184 | +cleanup: |
|---|
| 185 | + for (j--; j >= 0; j--) |
|---|
| 186 | + bpf_link__destroy(links[j]); |
|---|
| 187 | + |
|---|
| 188 | + bpf_object__close(obj); |
|---|
| 165 | 189 | return 0; |
|---|
| 166 | 190 | } |
|---|