1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
12 #include <bpf/libbpf.h>
17 static void clear_stats(int fd)
19 unsigned int nr_cpus = bpf_num_possible_cpus();
20 __u64 values[nr_cpus];
23 memset(values, 0, sizeof(values));
24 for (key = 0; key < SLOTS; key++)
25 bpf_map_update_elem(fd, &key, values, BPF_ANY);
28 const char *color[] = {
42 const int num_colors = ARRAY_SIZE(color);
44 const char nocolor[] = "\033[00m";
61 bool full_range = false;
62 bool text_only = false;
64 static void print_banner(void)
67 printf("|1ns |10ns |100ns |1us |10us |100us"
68 " |1ms |10ms |100ms |1s |10s\n");
70 printf("|1us |10us |100us |1ms |10ms "
74 static void print_hist(int fd)
76 unsigned int nr_cpus = bpf_num_possible_cpus();
77 __u64 total_events = 0;
85 for (key = 0; key < SLOTS; key++) {
86 bpf_map_lookup_elem(fd, &key, values);
88 for (i = 0; i < nr_cpus; i++)
91 total_events += value;
96 for (key = full_range ? 0 : 29; key < SLOTS; key++) {
97 int c = num_colors * cnt[key] / (max_cnt + 1);
100 printf("%s", sym[c]);
102 printf("%s %s", color[c], nocolor);
104 printf(" # %lld\n", total_events);
107 int main(int ac, char **argv)
109 struct bpf_link *links[2];
110 struct bpf_program *prog;
111 struct bpf_object *obj;
113 int map_fd, i, j = 0;
115 for (i = 1; i < ac; i++) {
116 if (strcmp(argv[i], "-a") == 0) {
118 } else if (strcmp(argv[i], "-t") == 0) {
120 } else if (strcmp(argv[i], "-h") == 0) {
122 " -a display wider latency range\n"
128 snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
129 obj = bpf_object__open_file(filename, NULL);
130 if (libbpf_get_error(obj)) {
131 fprintf(stderr, "ERROR: opening BPF object file failed\n");
135 /* load BPF program */
136 if (bpf_object__load(obj)) {
137 fprintf(stderr, "ERROR: loading BPF object file failed\n");
141 map_fd = bpf_object__find_map_fd_by_name(obj, "lat_map");
143 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
147 bpf_object__for_each_program(prog, obj) {
148 links[j] = bpf_program__attach(prog);
149 if (libbpf_get_error(links[j])) {
150 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
157 printf(" heatmap of IO latency\n");
159 printf(" %s", sym[num_colors - 1]);
161 printf(" %s %s", color[num_colors - 1], nocolor);
162 printf(" - many events with this latency\n");
165 printf(" %s", sym[0]);
167 printf(" %s %s", color[0], nocolor);
168 printf(" - few events\n");
178 for (j--; j >= 0; j--)
179 bpf_link__destroy(links[j]);
181 bpf_object__close(obj);