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