Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of version 2 of the GNU General Public |
| 5 | * License as published by the Free Software Foundation. |
| 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <signal.h> |
| 10 | #include <unistd.h> |
| 11 | #include <stdbool.h> |
| 12 | #include <string.h> |
| 13 | #include <linux/bpf.h> |
Jesper Dangaard Brouer | 55de170 | 2017-05-02 14:31:50 +0200 | [diff] [blame] | 14 | #include <sys/resource.h> |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 15 | |
Jakub Kicinski | 2bf3e2e | 2018-05-14 22:35:02 -0700 | [diff] [blame] | 16 | #include <bpf/bpf.h> |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 17 | #include "bpf_load.h" |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 18 | #include "bpf_util.h" |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 19 | |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 20 | #define SLOTS 100 |
| 21 | |
| 22 | static void clear_stats(int fd) |
| 23 | { |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 24 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
Alexei Starovoitov | 3059303 | 2016-02-01 22:39:58 -0800 | [diff] [blame] | 25 | __u64 values[nr_cpus]; |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 26 | __u32 key; |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 27 | |
Alexei Starovoitov | 3059303 | 2016-02-01 22:39:58 -0800 | [diff] [blame] | 28 | memset(values, 0, sizeof(values)); |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 29 | for (key = 0; key < SLOTS; key++) |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 30 | bpf_map_update_elem(fd, &key, values, BPF_ANY); |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | const char *color[] = { |
| 34 | "\033[48;5;255m", |
| 35 | "\033[48;5;252m", |
| 36 | "\033[48;5;250m", |
| 37 | "\033[48;5;248m", |
| 38 | "\033[48;5;246m", |
| 39 | "\033[48;5;244m", |
| 40 | "\033[48;5;242m", |
| 41 | "\033[48;5;240m", |
| 42 | "\033[48;5;238m", |
| 43 | "\033[48;5;236m", |
| 44 | "\033[48;5;234m", |
| 45 | "\033[48;5;232m", |
| 46 | }; |
| 47 | const int num_colors = ARRAY_SIZE(color); |
| 48 | |
| 49 | const char nocolor[] = "\033[00m"; |
| 50 | |
| 51 | const char *sym[] = { |
| 52 | " ", |
| 53 | " ", |
| 54 | ".", |
| 55 | ".", |
| 56 | "*", |
| 57 | "*", |
| 58 | "o", |
| 59 | "o", |
| 60 | "O", |
| 61 | "O", |
| 62 | "#", |
| 63 | "#", |
| 64 | }; |
| 65 | |
| 66 | bool full_range = false; |
| 67 | bool text_only = false; |
| 68 | |
| 69 | static void print_banner(void) |
| 70 | { |
| 71 | if (full_range) |
| 72 | printf("|1ns |10ns |100ns |1us |10us |100us" |
| 73 | " |1ms |10ms |100ms |1s |10s\n"); |
| 74 | else |
| 75 | printf("|1us |10us |100us |1ms |10ms " |
| 76 | "|100ms |1s |10s\n"); |
| 77 | } |
| 78 | |
| 79 | static void print_hist(int fd) |
| 80 | { |
Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 81 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 82 | __u64 total_events = 0; |
Alexei Starovoitov | 3059303 | 2016-02-01 22:39:58 -0800 | [diff] [blame] | 83 | long values[nr_cpus]; |
| 84 | __u64 max_cnt = 0; |
| 85 | __u64 cnt[SLOTS]; |
| 86 | __u64 value; |
| 87 | __u32 key; |
| 88 | int i; |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 89 | |
| 90 | for (key = 0; key < SLOTS; key++) { |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 91 | bpf_map_lookup_elem(fd, &key, values); |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 92 | value = 0; |
Alexei Starovoitov | 3059303 | 2016-02-01 22:39:58 -0800 | [diff] [blame] | 93 | for (i = 0; i < nr_cpus; i++) |
| 94 | value += values[i]; |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 95 | cnt[key] = value; |
| 96 | total_events += value; |
| 97 | if (value > max_cnt) |
| 98 | max_cnt = value; |
| 99 | } |
| 100 | clear_stats(fd); |
| 101 | for (key = full_range ? 0 : 29; key < SLOTS; key++) { |
| 102 | int c = num_colors * cnt[key] / (max_cnt + 1); |
| 103 | |
| 104 | if (text_only) |
| 105 | printf("%s", sym[c]); |
| 106 | else |
| 107 | printf("%s %s", color[c], nocolor); |
| 108 | } |
| 109 | printf(" # %lld\n", total_events); |
| 110 | } |
| 111 | |
| 112 | int main(int ac, char **argv) |
| 113 | { |
Jesper Dangaard Brouer | 55de170 | 2017-05-02 14:31:50 +0200 | [diff] [blame] | 114 | struct rlimit r = {1024*1024, RLIM_INFINITY}; |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 115 | char filename[256]; |
| 116 | int i; |
| 117 | |
| 118 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
| 119 | |
Jesper Dangaard Brouer | 55de170 | 2017-05-02 14:31:50 +0200 | [diff] [blame] | 120 | if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
| 121 | perror("setrlimit(RLIMIT_MEMLOCK)"); |
| 122 | return 1; |
| 123 | } |
| 124 | |
Alexei Starovoitov | 5c7fc2d | 2015-03-25 12:49:25 -0700 | [diff] [blame] | 125 | if (load_bpf_file(filename)) { |
| 126 | printf("%s", bpf_log_buf); |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | for (i = 1; i < ac; i++) { |
| 131 | if (strcmp(argv[i], "-a") == 0) { |
| 132 | full_range = true; |
| 133 | } else if (strcmp(argv[i], "-t") == 0) { |
| 134 | text_only = true; |
| 135 | } else if (strcmp(argv[i], "-h") == 0) { |
| 136 | printf("Usage:\n" |
| 137 | " -a display wider latency range\n" |
| 138 | " -t text only\n"); |
| 139 | return 1; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | printf(" heatmap of IO latency\n"); |
| 144 | if (text_only) |
| 145 | printf(" %s", sym[num_colors - 1]); |
| 146 | else |
| 147 | printf(" %s %s", color[num_colors - 1], nocolor); |
| 148 | printf(" - many events with this latency\n"); |
| 149 | |
| 150 | if (text_only) |
| 151 | printf(" %s", sym[0]); |
| 152 | else |
| 153 | printf(" %s %s", color[0], nocolor); |
| 154 | printf(" - few events\n"); |
| 155 | |
| 156 | for (i = 0; ; i++) { |
| 157 | if (i % 20 == 0) |
| 158 | print_banner(); |
| 159 | print_hist(map_fd[1]); |
| 160 | sleep(2); |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |