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 rlimit r = {1024*1024, RLIM_INFINITY};
111 struct bpf_link *links[2];
112 struct bpf_program *prog;
113 struct bpf_object *obj;
115 int map_fd, i, j = 0;
117 for (i = 1; i < ac; i++) {
118 if (strcmp(argv[i], "-a") == 0) {
120 } else if (strcmp(argv[i], "-t") == 0) {
122 } else if (strcmp(argv[i], "-h") == 0) {
124 " -a display wider latency range\n"
130 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
131 perror("setrlimit(RLIMIT_MEMLOCK)");
135 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
136 obj = bpf_object__open_file(filename, NULL);
137 if (libbpf_get_error(obj)) {
138 fprintf(stderr, "ERROR: opening BPF object file failed\n");
142 /* load BPF program */
143 if (bpf_object__load(obj)) {
144 fprintf(stderr, "ERROR: loading BPF object file failed\n");
148 map_fd = bpf_object__find_map_fd_by_name(obj, "lat_map");
150 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
154 bpf_object__for_each_program(prog, obj) {
155 links[j] = bpf_program__attach(prog);
156 if (libbpf_get_error(links[j])) {
157 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
164 printf(" heatmap of IO latency\n");
166 printf(" %s", sym[num_colors - 1]);
168 printf(" %s %s", color[num_colors - 1], nocolor);
169 printf(" - many events with this latency\n");
172 printf(" %s", sym[0]);
174 printf(" %s %s", color[0], nocolor);
175 printf(" - few events\n");
185 for (j--; j >= 0; j--)
186 bpf_link__destroy(links[j]);
188 bpf_object__close(obj);