1f74599f7SThomas Graf #include <linux/unistd.h> 2f74599f7SThomas Graf #include <linux/bpf.h> 3f74599f7SThomas Graf 4f74599f7SThomas Graf #include <stdlib.h> 5f74599f7SThomas Graf #include <stdio.h> 6f74599f7SThomas Graf #include <unistd.h> 7f74599f7SThomas Graf #include <string.h> 8f74599f7SThomas Graf #include <errno.h> 9f74599f7SThomas Graf #include <arpa/inet.h> 10f74599f7SThomas Graf 11f74599f7SThomas Graf #include "libbpf.h" 12f74599f7SThomas Graf #include "bpf_util.h" 13f74599f7SThomas Graf 14f74599f7SThomas Graf #define MAX_INDEX 64 15f74599f7SThomas Graf #define MAX_STARS 38 16f74599f7SThomas Graf 17*d40fc181SJoe Stringer char bpf_log_buf[BPF_LOG_BUF_SIZE]; 18*d40fc181SJoe Stringer 19f74599f7SThomas Graf static void stars(char *str, long val, long max, int width) 20f74599f7SThomas Graf { 21f74599f7SThomas Graf int i; 22f74599f7SThomas Graf 23f74599f7SThomas Graf for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++) 24f74599f7SThomas Graf str[i] = '*'; 25f74599f7SThomas Graf if (val > max) 26f74599f7SThomas Graf str[i - 1] = '+'; 27f74599f7SThomas Graf str[i] = '\0'; 28f74599f7SThomas Graf } 29f74599f7SThomas Graf 30f74599f7SThomas Graf int main(int argc, char **argv) 31f74599f7SThomas Graf { 32f74599f7SThomas Graf unsigned int nr_cpus = bpf_num_possible_cpus(); 33f74599f7SThomas Graf const char *map_filename = "/sys/fs/bpf/tc/globals/lwt_len_hist_map"; 34f74599f7SThomas Graf uint64_t values[nr_cpus], sum, max_value = 0, data[MAX_INDEX] = {}; 35f74599f7SThomas Graf uint64_t key = 0, next_key, max_key = 0; 36f74599f7SThomas Graf char starstr[MAX_STARS]; 37f74599f7SThomas Graf int i, map_fd; 38f74599f7SThomas Graf 39f74599f7SThomas Graf map_fd = bpf_obj_get(map_filename); 40f74599f7SThomas Graf if (map_fd < 0) { 41f74599f7SThomas Graf fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n", 42f74599f7SThomas Graf map_filename, strerror(errno), errno); 43f74599f7SThomas Graf return -1; 44f74599f7SThomas Graf } 45f74599f7SThomas Graf 46*d40fc181SJoe Stringer while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) { 47f74599f7SThomas Graf if (next_key >= MAX_INDEX) { 48f74599f7SThomas Graf fprintf(stderr, "Key %lu out of bounds\n", next_key); 49f74599f7SThomas Graf continue; 50f74599f7SThomas Graf } 51f74599f7SThomas Graf 52*d40fc181SJoe Stringer bpf_map_lookup_elem(map_fd, &next_key, values); 53f74599f7SThomas Graf 54f74599f7SThomas Graf sum = 0; 55f74599f7SThomas Graf for (i = 0; i < nr_cpus; i++) 56f74599f7SThomas Graf sum += values[i]; 57f74599f7SThomas Graf 58f74599f7SThomas Graf data[next_key] = sum; 59f74599f7SThomas Graf if (sum && next_key > max_key) 60f74599f7SThomas Graf max_key = next_key; 61f74599f7SThomas Graf 62f74599f7SThomas Graf if (sum > max_value) 63f74599f7SThomas Graf max_value = sum; 64f74599f7SThomas Graf 65f74599f7SThomas Graf key = next_key; 66f74599f7SThomas Graf } 67f74599f7SThomas Graf 68f74599f7SThomas Graf for (i = 1; i <= max_key + 1; i++) { 69f74599f7SThomas Graf stars(starstr, data[i - 1], max_value, MAX_STARS); 70f74599f7SThomas Graf printf("%8ld -> %-8ld : %-8ld |%-*s|\n", 71f74599f7SThomas Graf (1l << i) >> 1, (1l << i) - 1, data[i - 1], 72f74599f7SThomas Graf MAX_STARS, starstr); 73f74599f7SThomas Graf } 74f74599f7SThomas Graf 75f74599f7SThomas Graf close(map_fd); 76f74599f7SThomas Graf 77f74599f7SThomas Graf return 0; 78f74599f7SThomas Graf } 79