1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
3 * Copyright (c) 2015 BMW Car IT GmbH
9 #include <bpf/libbpf.h>
12 #define MAX_ENTRIES 20
17 long data[MAX_ENTRIES];
21 static struct cpu_hist cpu_hist[MAX_CPU];
23 static void stars(char *str, long val, long max, int width)
27 for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
34 static void print_hist(void)
36 char starstr[MAX_STARS];
37 struct cpu_hist *hist;
43 for (j = 0; j < MAX_CPU; j++) {
46 /* ignore CPUs without data (maybe offline?) */
50 printf("CPU %d\n", j);
51 printf(" latency : count distribution\n");
52 for (i = 1; i <= MAX_ENTRIES; i++) {
53 stars(starstr, hist->data[i - 1], hist->max, MAX_STARS);
54 printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
55 (1l << i) >> 1, (1l << i) - 1,
56 hist->data[i - 1], MAX_STARS, starstr);
61 static void get_data(int fd)
66 for (i = 0; i < MAX_CPU; i++)
69 for (c = 0; c < MAX_CPU; c++) {
70 for (i = 0; i < MAX_ENTRIES; i++) {
71 key = c * MAX_ENTRIES + i;
72 bpf_map_lookup_elem(fd, &key, &value);
74 cpu_hist[c].data[i] = value;
75 if (value > cpu_hist[c].max)
76 cpu_hist[c].max = value;
81 int main(int argc, char **argv)
83 struct bpf_link *links[2];
84 struct bpf_program *prog;
85 struct bpf_object *obj;
89 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
90 obj = bpf_object__open_file(filename, NULL);
91 if (libbpf_get_error(obj)) {
92 fprintf(stderr, "ERROR: opening BPF object file failed\n");
96 /* load BPF program */
97 if (bpf_object__load(obj)) {
98 fprintf(stderr, "ERROR: loading BPF object file failed\n");
102 map_fd = bpf_object__find_map_fd_by_name(obj, "my_lat");
104 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
108 bpf_object__for_each_program(prog, obj) {
109 links[i] = bpf_program__attach(prog);
110 if (libbpf_get_error(links[i])) {
111 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
125 for (i--; i >= 0; i--)
126 bpf_link__destroy(links[i]);
128 bpf_object__close(obj);