Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/unistd.h>
0003 #include <linux/bpf.h>
0004 
0005 #include <stdlib.h>
0006 #include <stdio.h>
0007 #include <unistd.h>
0008 #include <string.h>
0009 #include <errno.h>
0010 #include <arpa/inet.h>
0011 
0012 #include <bpf/bpf.h>
0013 #include "bpf_util.h"
0014 
0015 #define MAX_INDEX 64
0016 #define MAX_STARS 38
0017 
0018 static void stars(char *str, long val, long max, int width)
0019 {
0020     int i;
0021 
0022     for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
0023         str[i] = '*';
0024     if (val > max)
0025         str[i - 1] = '+';
0026     str[i] = '\0';
0027 }
0028 
0029 int main(int argc, char **argv)
0030 {
0031     unsigned int nr_cpus = bpf_num_possible_cpus();
0032     const char *map_filename = "/sys/fs/bpf/tc/globals/lwt_len_hist_map";
0033     uint64_t values[nr_cpus], sum, max_value = 0, data[MAX_INDEX] = {};
0034     uint64_t key = 0, next_key, max_key = 0;
0035     char starstr[MAX_STARS];
0036     int i, map_fd;
0037 
0038     map_fd = bpf_obj_get(map_filename);
0039     if (map_fd < 0) {
0040         fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
0041             map_filename, strerror(errno), errno);
0042         return -1;
0043     }
0044 
0045     while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
0046         if (next_key >= MAX_INDEX) {
0047             fprintf(stderr, "Key %lu out of bounds\n", next_key);
0048             continue;
0049         }
0050 
0051         bpf_map_lookup_elem(map_fd, &next_key, values);
0052 
0053         sum = 0;
0054         for (i = 0; i < nr_cpus; i++)
0055             sum += values[i];
0056 
0057         data[next_key] = sum;
0058         if (sum && next_key > max_key)
0059             max_key = next_key;
0060 
0061         if (sum > max_value)
0062             max_value = sum;
0063 
0064         key = next_key;
0065     }
0066 
0067     for (i = 1; i <= max_key + 1; i++) {
0068         stars(starstr, data[i - 1], max_value, MAX_STARS);
0069         printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
0070                (1l << i) >> 1, (1l << i) - 1, data[i - 1],
0071                MAX_STARS, starstr);
0072     }
0073 
0074     close(map_fd);
0075 
0076     return 0;
0077 }