1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
10 #include <sys/resource.h>
13 #include <bpf/libbpf.h>
18 static void clear_stats(int fd
)
20 unsigned int nr_cpus
= bpf_num_possible_cpus();
21 __u64 values
[nr_cpus
];
24 memset(values
, 0, sizeof(values
));
25 for (key
= 0; key
< SLOTS
; key
++)
26 bpf_map_update_elem(fd
, &key
, values
, BPF_ANY
);
29 const char *color
[] = {
43 const int num_colors
= ARRAY_SIZE(color
);
45 const char nocolor
[] = "\033[00m";
62 bool full_range
= false;
63 bool text_only
= false;
65 static void print_banner(void)
68 printf("|1ns |10ns |100ns |1us |10us |100us"
69 " |1ms |10ms |100ms |1s |10s\n");
71 printf("|1us |10us |100us |1ms |10ms "
75 static void print_hist(int fd
)
77 unsigned int nr_cpus
= bpf_num_possible_cpus();
78 __u64 total_events
= 0;
86 for (key
= 0; key
< SLOTS
; key
++) {
87 bpf_map_lookup_elem(fd
, &key
, values
);
89 for (i
= 0; i
< nr_cpus
; i
++)
92 total_events
+= value
;
97 for (key
= full_range
? 0 : 29; key
< SLOTS
; key
++) {
98 int c
= num_colors
* cnt
[key
] / (max_cnt
+ 1);
101 printf("%s", sym
[c
]);
103 printf("%s %s", color
[c
], nocolor
);
105 printf(" # %lld\n", total_events
);
108 int main(int ac
, char **argv
)
110 struct bpf_link
*links
[2];
111 struct bpf_program
*prog
;
112 struct bpf_object
*obj
;
114 int map_fd
, i
, j
= 0;
116 for (i
= 1; i
< ac
; i
++) {
117 if (strcmp(argv
[i
], "-a") == 0) {
119 } else if (strcmp(argv
[i
], "-t") == 0) {
121 } else if (strcmp(argv
[i
], "-h") == 0) {
123 " -a display wider latency range\n"
129 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
130 obj
= bpf_object__open_file(filename
, NULL
);
131 if (libbpf_get_error(obj
)) {
132 fprintf(stderr
, "ERROR: opening BPF object file failed\n");
136 /* load BPF program */
137 if (bpf_object__load(obj
)) {
138 fprintf(stderr
, "ERROR: loading BPF object file failed\n");
142 map_fd
= bpf_object__find_map_fd_by_name(obj
, "lat_map");
144 fprintf(stderr
, "ERROR: finding a map in obj file failed\n");
148 bpf_object__for_each_program(prog
, obj
) {
149 links
[j
] = bpf_program__attach(prog
);
150 if (libbpf_get_error(links
[j
])) {
151 fprintf(stderr
, "ERROR: bpf_program__attach failed\n");
158 printf(" heatmap of IO latency\n");
160 printf(" %s", sym
[num_colors
- 1]);
162 printf(" %s %s", color
[num_colors
- 1], nocolor
);
163 printf(" - many events with this latency\n");
166 printf(" %s", sym
[0]);
168 printf(" %s %s", color
[0], nocolor
);
169 printf(" - few events\n");
179 for (j
--; j
>= 0; j
--)
180 bpf_link__destroy(links
[j
]);
182 bpf_object__close(obj
);