blob: 8f7d199d59459cf16520b62a0ef12dcb7e6f6c04 [file] [log] [blame]
Alexei Starovoitov5c7fc2d2015-03-25 12:49:25 -07001/* 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>
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010014
Alexei Starovoitov5c7fc2d2015-03-25 12:49:25 -070015#include "libbpf.h"
16#include "bpf_load.h"
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010017#include "bpf_util.h"
Alexei Starovoitov5c7fc2d2015-03-25 12:49:25 -070018
19#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
20
21#define SLOTS 100
22
23static void clear_stats(int fd)
24{
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010025 unsigned int nr_cpus = bpf_num_possible_cpus();
Alexei Starovoitov30593032016-02-01 22:39:58 -080026 __u64 values[nr_cpus];
Alexei Starovoitov5c7fc2d2015-03-25 12:49:25 -070027 __u32 key;
Alexei Starovoitov5c7fc2d2015-03-25 12:49:25 -070028
Alexei Starovoitov30593032016-02-01 22:39:58 -080029 memset(values, 0, sizeof(values));
Alexei Starovoitov5c7fc2d2015-03-25 12:49:25 -070030 for (key = 0; key < SLOTS; key++)
Joe Stringerd40fc182016-12-14 14:43:38 -080031 bpf_map_update_elem(fd, &key, values, BPF_ANY);
Alexei Starovoitov5c7fc2d2015-03-25 12:49:25 -070032}
33
34const char *color[] = {
35 "\033[48;5;255m",
36 "\033[48;5;252m",
37 "\033[48;5;250m",
38 "\033[48;5;248m",
39 "\033[48;5;246m",
40 "\033[48;5;244m",
41 "\033[48;5;242m",
42 "\033[48;5;240m",
43 "\033[48;5;238m",
44 "\033[48;5;236m",
45 "\033[48;5;234m",
46 "\033[48;5;232m",
47};
48const int num_colors = ARRAY_SIZE(color);
49
50const char nocolor[] = "\033[00m";
51
52const char *sym[] = {
53 " ",
54 " ",
55 ".",
56 ".",
57 "*",
58 "*",
59 "o",
60 "o",
61 "O",
62 "O",
63 "#",
64 "#",
65};
66
67bool full_range = false;
68bool text_only = false;
69
70static void print_banner(void)
71{
72 if (full_range)
73 printf("|1ns |10ns |100ns |1us |10us |100us"
74 " |1ms |10ms |100ms |1s |10s\n");
75 else
76 printf("|1us |10us |100us |1ms |10ms "
77 "|100ms |1s |10s\n");
78}
79
80static void print_hist(int fd)
81{
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010082 unsigned int nr_cpus = bpf_num_possible_cpus();
Alexei Starovoitov5c7fc2d2015-03-25 12:49:25 -070083 __u64 total_events = 0;
Alexei Starovoitov30593032016-02-01 22:39:58 -080084 long values[nr_cpus];
85 __u64 max_cnt = 0;
86 __u64 cnt[SLOTS];
87 __u64 value;
88 __u32 key;
89 int i;
Alexei Starovoitov5c7fc2d2015-03-25 12:49:25 -070090
91 for (key = 0; key < SLOTS; key++) {
Joe Stringerd40fc182016-12-14 14:43:38 -080092 bpf_map_lookup_elem(fd, &key, values);
Alexei Starovoitov5c7fc2d2015-03-25 12:49:25 -070093 value = 0;
Alexei Starovoitov30593032016-02-01 22:39:58 -080094 for (i = 0; i < nr_cpus; i++)
95 value += values[i];
Alexei Starovoitov5c7fc2d2015-03-25 12:49:25 -070096 cnt[key] = value;
97 total_events += value;
98 if (value > max_cnt)
99 max_cnt = value;
100 }
101 clear_stats(fd);
102 for (key = full_range ? 0 : 29; key < SLOTS; key++) {
103 int c = num_colors * cnt[key] / (max_cnt + 1);
104
105 if (text_only)
106 printf("%s", sym[c]);
107 else
108 printf("%s %s", color[c], nocolor);
109 }
110 printf(" # %lld\n", total_events);
111}
112
113int main(int ac, char **argv)
114{
115 char filename[256];
116 int i;
117
118 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
119
120 if (load_bpf_file(filename)) {
121 printf("%s", bpf_log_buf);
122 return 1;
123 }
124
125 for (i = 1; i < ac; i++) {
126 if (strcmp(argv[i], "-a") == 0) {
127 full_range = true;
128 } else if (strcmp(argv[i], "-t") == 0) {
129 text_only = true;
130 } else if (strcmp(argv[i], "-h") == 0) {
131 printf("Usage:\n"
132 " -a display wider latency range\n"
133 " -t text only\n");
134 return 1;
135 }
136 }
137
138 printf(" heatmap of IO latency\n");
139 if (text_only)
140 printf(" %s", sym[num_colors - 1]);
141 else
142 printf(" %s %s", color[num_colors - 1], nocolor);
143 printf(" - many events with this latency\n");
144
145 if (text_only)
146 printf(" %s", sym[0]);
147 else
148 printf(" %s %s", color[0], nocolor);
149 printf(" - few events\n");
150
151 for (i = 0; ; i++) {
152 if (i % 20 == 0)
153 print_banner();
154 print_hist(map_fd[1]);
155 sleep(2);
156 }
157
158 return 0;
159}