summaryrefslogtreecommitdiffstats
path: root/samples/bpf/lwt_len_hist_user.c
blob: 05d783fc5dafbc50393b796b8e0968bc293f5b07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <linux/unistd.h>
#include <linux/bpf.h>

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>

#include "libbpf.h"
#include "bpf_util.h"

#define MAX_INDEX 64
#define MAX_STARS 38

static void stars(char *str, long val, long max, int width)
{
	int i;

	for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
		str[i] = '*';
	if (val > max)
		str[i - 1] = '+';
	str[i] = '\0';
}

int main(int argc, char **argv)
{
	unsigned int nr_cpus = bpf_num_possible_cpus();
	const char *map_filename = "/sys/fs/bpf/tc/globals/lwt_len_hist_map";
	uint64_t values[nr_cpus], sum, max_value = 0, data[MAX_INDEX] = {};
	uint64_t key = 0, next_key, max_key = 0;
	char starstr[MAX_STARS];
	int i, map_fd;

	map_fd = bpf_obj_get(map_filename);
	if (map_fd < 0) {
		fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
			map_filename, strerror(errno), errno);
		return -1;
	}

	while (bpf_get_next_key(map_fd, &key, &next_key) == 0) {
		if (next_key >= MAX_INDEX) {
			fprintf(stderr, "Key %lu out of bounds\n", next_key);
			continue;
		}

		bpf_lookup_elem(map_fd, &next_key, values);

		sum = 0;
		for (i = 0; i < nr_cpus; i++)
			sum += values[i];

		data[next_key] = sum;
		if (sum && next_key > max_key)
			max_key = next_key;

		if (sum > max_value)
			max_value = sum;

		key = next_key;
	}

	for (i = 1; i <= max_key + 1; i++) {
		stars(starstr, data[i - 1], max_value, MAX_STARS);
		printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
		       (1l << i) >> 1, (1l << i) - 1, data[i - 1],
		       MAX_STARS, starstr);
	}

	close(map_fd);

	return 0;
}
OpenPOWER on IntegriCloud