1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 // Copyright (c) 2020 Wenbo Zhang
3 //
4 // Based on biopattern(8) from BPF-Perf-Tools-Book by Brendan Gregg.
5 // 17-Jun-2020 Wenbo Zhang Created this.
6 #include <argp.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <time.h>
11 #include <bpf/libbpf.h>
12 #include <bpf/bpf.h>
13 #include "biopattern.h"
14 #include "biopattern.skel.h"
15 #include "btf_helpers.h"
16 #include "trace_helpers.h"
17
18 static struct env {
19 char *disk;
20 time_t interval;
21 bool timestamp;
22 bool verbose;
23 int times;
24 } env = {
25 .interval = 99999999,
26 .times = 99999999,
27 };
28
29 static volatile bool exiting;
30
31 const char *argp_program_version = "biopattern 0.1";
32 const char *argp_program_bug_address =
33 "https://github.com/iovisor/bcc/tree/master/libbpf-tools";
34 const char argp_program_doc[] =
35 "Show block device I/O pattern.\n"
36 "\n"
37 "USAGE: biopattern [--help] [-T] [-d DISK] [interval] [count]\n"
38 "\n"
39 "EXAMPLES:\n"
40 " biopattern # show block I/O pattern\n"
41 " biopattern 1 10 # print 1 second summaries, 10 times\n"
42 " biopattern -T 1 # 1s summaries with timestamps\n"
43 " biopattern -d sdc # trace sdc only\n";
44
45 static const struct argp_option opts[] = {
46 { "timestamp", 'T', NULL, 0, "Include timestamp on output" },
47 { "disk", 'd', "DISK", 0, "Trace this disk only" },
48 { "verbose", 'v', NULL, 0, "Verbose debug output" },
49 { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
50 {},
51 };
52
parse_arg(int key,char * arg,struct argp_state * state)53 static error_t parse_arg(int key, char *arg, struct argp_state *state)
54 {
55 static int pos_args;
56
57 switch (key) {
58 case 'h':
59 argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
60 break;
61 case 'v':
62 env.verbose = true;
63 break;
64 case 'd':
65 env.disk = arg;
66 if (strlen(arg) + 1 > DISK_NAME_LEN) {
67 fprintf(stderr, "invaild disk name: too long\n");
68 argp_usage(state);
69 }
70 break;
71 case 'T':
72 env.timestamp = true;
73 break;
74 case ARGP_KEY_ARG:
75 errno = 0;
76 if (pos_args == 0) {
77 env.interval = strtol(arg, NULL, 10);
78 if (errno) {
79 fprintf(stderr, "invalid internal\n");
80 argp_usage(state);
81 }
82 } else if (pos_args == 1) {
83 env.times = strtol(arg, NULL, 10);
84 if (errno) {
85 fprintf(stderr, "invalid times\n");
86 argp_usage(state);
87 }
88 } else {
89 fprintf(stderr,
90 "unrecognized positional argument: %s\n", arg);
91 argp_usage(state);
92 }
93 pos_args++;
94 break;
95 default:
96 return ARGP_ERR_UNKNOWN;
97 }
98 return 0;
99 }
100
libbpf_print_fn(enum libbpf_print_level level,const char * format,va_list args)101 static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
102 {
103 if (level == LIBBPF_DEBUG && !env.verbose)
104 return 0;
105 return vfprintf(stderr, format, args);
106 }
107
sig_handler(int sig)108 static void sig_handler(int sig)
109 {
110 exiting = true;
111 }
112
print_map(struct bpf_map * counters,struct partitions * partitions)113 static int print_map(struct bpf_map *counters, struct partitions *partitions)
114 {
115 __u32 total, lookup_key = -1, next_key;
116 int err, fd = bpf_map__fd(counters);
117 const struct partition *partition;
118 struct counter counter;
119 struct tm *tm;
120 char ts[32];
121 time_t t;
122
123 while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
124 err = bpf_map_lookup_elem(fd, &next_key, &counter);
125 if (err < 0) {
126 fprintf(stderr, "failed to lookup counters: %d\n", err);
127 return -1;
128 }
129 lookup_key = next_key;
130 total = counter.sequential + counter.random;
131 if (!total)
132 continue;
133 if (env.timestamp) {
134 time(&t);
135 tm = localtime(&t);
136 strftime(ts, sizeof(ts), "%H:%M:%S", tm);
137 printf("%-9s ", ts);
138 }
139 partition = partitions__get_by_dev(partitions, next_key);
140 printf("%-7s %5ld %5ld %8d %10lld\n",
141 partition ? partition->name : "Unknown",
142 counter.random * 100L / total,
143 counter.sequential * 100L / total, total,
144 counter.bytes / 1024);
145 }
146
147 lookup_key = -1;
148 while (!bpf_map_get_next_key(fd, &lookup_key, &next_key)) {
149 err = bpf_map_delete_elem(fd, &next_key);
150 if (err < 0) {
151 fprintf(stderr, "failed to cleanup counters: %d\n", err);
152 return -1;
153 }
154 lookup_key = next_key;
155 }
156
157 return 0;
158 }
159
main(int argc,char ** argv)160 int main(int argc, char **argv)
161 {
162 LIBBPF_OPTS(bpf_object_open_opts, open_opts);
163 struct partitions *partitions = NULL;
164 const struct partition *partition;
165 static const struct argp argp = {
166 .options = opts,
167 .parser = parse_arg,
168 .doc = argp_program_doc,
169 };
170 struct biopattern_bpf *obj;
171 int err;
172
173 err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
174 if (err)
175 return err;
176
177 libbpf_set_print(libbpf_print_fn);
178
179 err = ensure_core_btf(&open_opts);
180 if (err) {
181 fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err));
182 return 1;
183 }
184
185 obj = biopattern_bpf__open_opts(&open_opts);
186 if (!obj) {
187 fprintf(stderr, "failed to open BPF object\n");
188 return 1;
189 }
190
191 partitions = partitions__load();
192 if (!partitions) {
193 fprintf(stderr, "failed to load partitions info\n");
194 goto cleanup;
195 }
196
197 /* initialize global data (filtering options) */
198 if (env.disk) {
199 partition = partitions__get_by_name(partitions, env.disk);
200 if (!partition) {
201 fprintf(stderr, "invaild partition name: not exist\n");
202 goto cleanup;
203 }
204 obj->rodata->filter_dev = true;
205 obj->rodata->targ_dev = partition->dev;
206 }
207
208 err = biopattern_bpf__load(obj);
209 if (err) {
210 fprintf(stderr, "failed to load BPF object: %d\n", err);
211 goto cleanup;
212 }
213
214 err = biopattern_bpf__attach(obj);
215 if (err) {
216 fprintf(stderr, "failed to attach BPF programs\n");
217 goto cleanup;
218 }
219
220 signal(SIGINT, sig_handler);
221
222 printf("Tracing block device I/O requested seeks... Hit Ctrl-C to "
223 "end.\n");
224 if (env.timestamp)
225 printf("%-9s ", "TIME");
226 printf("%-7s %5s %5s %8s %10s\n", "DISK", "%RND", "%SEQ",
227 "COUNT", "KBYTES");
228
229 /* main: poll */
230 while (1) {
231 sleep(env.interval);
232
233 err = print_map(obj->maps.counters, partitions);
234 if (err)
235 break;
236
237 if (exiting || --env.times == 0)
238 break;
239 }
240
241 cleanup:
242 biopattern_bpf__destroy(obj);
243 partitions__free(partitions);
244 cleanup_core_btf(&open_opts);
245
246 return err != 0;
247 }
248