~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/bpf/lwt_len_hist_user.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 static void stars(char *str, long val, long max, int width)
 19 {
 20         int i;
 21 
 22         for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
 23                 str[i] = '*';
 24         if (val > max)
 25                 str[i - 1] = '+';
 26         str[i] = '\0';
 27 }
 28 
 29 int main(int argc, char **argv)
 30 {
 31         unsigned int nr_cpus = bpf_num_possible_cpus();
 32         const char *map_filename = "/sys/fs/bpf/tc/globals/lwt_len_hist_map";
 33         uint64_t values[nr_cpus], sum, max_value = 0, data[MAX_INDEX] = {};
 34         uint64_t key = 0, next_key, max_key = 0;
 35         char starstr[MAX_STARS];
 36         int i, map_fd;
 37 
 38         map_fd = bpf_obj_get(map_filename);
 39         if (map_fd < 0) {
 40                 fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
 41                         map_filename, strerror(errno), errno);
 42                 return -1;
 43         }
 44 
 45         while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
 46                 if (next_key >= MAX_INDEX) {
 47                         fprintf(stderr, "Key %lu out of bounds\n", next_key);
 48                         continue;
 49                 }
 50 
 51                 bpf_map_lookup_elem(map_fd, &next_key, values);
 52 
 53                 sum = 0;
 54                 for (i = 0; i < nr_cpus; i++)
 55                         sum += values[i];
 56 
 57                 data[next_key] = sum;
 58                 if (sum && next_key > max_key)
 59                         max_key = next_key;
 60 
 61                 if (sum > max_value)
 62                         max_value = sum;
 63 
 64                 key = next_key;
 65         }
 66 
 67         for (i = 1; i <= max_key + 1; i++) {
 68                 stars(starstr, data[i - 1], max_value, MAX_STARS);
 69                 printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
 70                        (1l << i) >> 1, (1l << i) - 1, data[i - 1],
 71                        MAX_STARS, starstr);
 72         }
 73 
 74         close(map_fd);
 75 
 76         return 0;
 77 }
 78 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php