Commit | Line | Data |
---|---|---|
d956a2cd JA |
1 | /* |
2 | * block queue tracing parse application | |
3 | * | |
4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
d0ca268b JA |
21 | #include <sys/types.h> |
22 | #include <sys/stat.h> | |
23 | #include <unistd.h> | |
24 | #include <stdio.h> | |
25 | #include <fcntl.h> | |
26 | #include <stdlib.h> | |
8fc0abbc | 27 | #include <string.h> |
d5396421 | 28 | #include <getopt.h> |
412819ce JA |
29 | #include <errno.h> |
30 | #include <signal.h> | |
d69db225 | 31 | #include <locale.h> |
46e6968b | 32 | #include <limits.h> |
d0ca268b | 33 | |
8fc0abbc JA |
34 | #include "blktrace.h" |
35 | #include "rbtree.h" | |
bf0720af | 36 | #include "jhash.h" |
d0ca268b | 37 | |
52724a0e JA |
38 | static char blkparse_version[] = "0.90"; |
39 | ||
e7c9f3ff | 40 | struct per_dev_info { |
f7bd1a9b | 41 | dev_t dev; |
e7c9f3ff NS |
42 | char *name; |
43 | ||
44 | int backwards; | |
45 | unsigned long long events; | |
46 | unsigned long long last_reported_time; | |
287fa3d6 | 47 | unsigned long long last_read_time; |
e7c9f3ff | 48 | struct io_stats io_stats; |
cb2a1a62 | 49 | unsigned long last_sequence; |
dcf0f7ed | 50 | unsigned long skips; |
e7c9f3ff | 51 | |
2a1b3424 JA |
52 | struct rb_root rb_last; |
53 | unsigned long rb_last_entries; | |
54 | ||
f7bd1a9b JA |
55 | struct rb_root rb_track; |
56 | ||
73877e12 | 57 | int nfiles; |
e7c9f3ff NS |
58 | int ncpus; |
59 | struct per_cpu_info *cpus; | |
60 | }; | |
61 | ||
152f6476 JA |
62 | struct per_process_info { |
63 | char name[16]; | |
64 | __u32 pid; | |
65 | struct io_stats io_stats; | |
66 | struct per_process_info *hash_next, *list_next; | |
715d8021 | 67 | int more_than_one; |
50adc0ba JA |
68 | |
69 | /* | |
70 | * individual io stats | |
71 | */ | |
b9d40d6f JA |
72 | unsigned long long longest_allocation_wait[2]; |
73 | unsigned long long longest_dispatch_wait[2]; | |
74 | unsigned long long longest_completion_wait[2]; | |
d0ca268b JA |
75 | }; |
76 | ||
152f6476 | 77 | #define PPI_HASH_SHIFT (8) |
bf0720af JA |
78 | #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT) |
79 | #define PPI_HASH_MASK (PPI_HASH_SIZE - 1) | |
80 | static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE]; | |
152f6476 | 81 | static struct per_process_info *ppi_list; |
886ecf0e | 82 | static int ppi_list_entries; |
152f6476 | 83 | |
bf0720af | 84 | #define S_OPTS "i:o:b:stqw:f:F:vn" |
d5396421 JA |
85 | static struct option l_opts[] = { |
86 | { | |
87 | .name = "input", | |
428683db | 88 | .has_arg = required_argument, |
d5396421 JA |
89 | .flag = NULL, |
90 | .val = 'i' | |
91 | }, | |
92 | { | |
93 | .name = "output", | |
428683db | 94 | .has_arg = required_argument, |
d5396421 JA |
95 | .flag = NULL, |
96 | .val = 'o' | |
97 | }, | |
79f19470 JA |
98 | { |
99 | .name = "batch", | |
428683db | 100 | .has_arg = required_argument, |
79f19470 JA |
101 | .flag = NULL, |
102 | .val = 'b' | |
103 | }, | |
152f6476 JA |
104 | { |
105 | .name = "per program stats", | |
428683db | 106 | .has_arg = no_argument, |
152f6476 JA |
107 | .flag = NULL, |
108 | .val = 's' | |
109 | }, | |
7997c5b0 JA |
110 | { |
111 | .name = "track ios", | |
428683db | 112 | .has_arg = no_argument, |
7997c5b0 JA |
113 | .flag = NULL, |
114 | .val = 't' | |
115 | }, | |
1e1c60f1 NS |
116 | { |
117 | .name = "quiet", | |
428683db | 118 | .has_arg = no_argument, |
1e1c60f1 NS |
119 | .flag = NULL, |
120 | .val = 'q' | |
121 | }, | |
46e6968b NS |
122 | { |
123 | .name = "stopwatch", | |
428683db | 124 | .has_arg = required_argument, |
46e6968b NS |
125 | .flag = NULL, |
126 | .val = 'w' | |
127 | }, | |
ab197ca7 AB |
128 | { |
129 | .name = "format", | |
428683db | 130 | .has_arg = required_argument, |
ab197ca7 AB |
131 | .flag = NULL, |
132 | .val = 'f' | |
133 | }, | |
134 | { | |
135 | .name = "format-spec", | |
428683db | 136 | .has_arg = required_argument, |
ab197ca7 AB |
137 | .flag = NULL, |
138 | .val = 'F' | |
139 | }, | |
bf0720af JA |
140 | { |
141 | .name = "hash by name", | |
142 | .has_arg = no_argument, | |
143 | .flag = NULL, | |
144 | .val = 'n' | |
145 | }, | |
52724a0e JA |
146 | { |
147 | .name = "version", | |
148 | .has_arg = no_argument, | |
149 | .flag = NULL, | |
150 | .val = 'v' | |
151 | }, | |
d5396421 JA |
152 | }; |
153 | ||
7997c5b0 JA |
154 | /* |
155 | * for sorting the displayed output | |
156 | */ | |
8fc0abbc JA |
157 | struct trace { |
158 | struct blk_io_trace *bit; | |
159 | struct rb_node rb_node; | |
cb2a1a62 | 160 | struct trace *next; |
8fc0abbc JA |
161 | }; |
162 | ||
cb2a1a62 | 163 | static struct rb_root rb_sort_root; |
a649216c JA |
164 | static unsigned long rb_sort_entries; |
165 | ||
cb2a1a62 JA |
166 | static struct trace *trace_list; |
167 | ||
d36421e4 JA |
168 | /* |
169 | * allocation cache | |
170 | */ | |
171 | static struct blk_io_trace *bit_alloc_list; | |
172 | static struct trace *t_alloc_list; | |
173 | ||
7997c5b0 JA |
174 | /* |
175 | * for tracking individual ios | |
176 | */ | |
177 | struct io_track { | |
178 | struct rb_node rb_node; | |
179 | ||
180 | __u64 sector; | |
181 | __u32 pid; | |
bf0720af | 182 | char comm[16]; |
95c15013 | 183 | unsigned long long allocation_time; |
7997c5b0 JA |
184 | unsigned long long queue_time; |
185 | unsigned long long dispatch_time; | |
186 | unsigned long long completion_time; | |
187 | }; | |
188 | ||
e7c9f3ff NS |
189 | static int ndevices; |
190 | static struct per_dev_info *devices; | |
191 | static char *get_dev_name(struct per_dev_info *, char *, int); | |
d0ca268b | 192 | |
71d5d4c9 | 193 | FILE *ofp = NULL; |
e7c9f3ff NS |
194 | static char *output_name; |
195 | ||
196 | static unsigned long long genesis_time; | |
287fa3d6 | 197 | static unsigned long long last_allowed_time; |
46e6968b NS |
198 | static unsigned long long stopwatch_start; /* start from zero by default */ |
199 | static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */ | |
152f6476 JA |
200 | |
201 | static int per_process_stats; | |
7997c5b0 | 202 | static int track_ios; |
bf0720af | 203 | static int ppi_hash_by_pid = 1; |
d0ca268b | 204 | |
7d747d22 | 205 | #define RB_BATCH_DEFAULT (512) |
79f19470 JA |
206 | static int rb_batch = RB_BATCH_DEFAULT; |
207 | ||
e7c9f3ff NS |
208 | static int pipeline; |
209 | ||
412819ce JA |
210 | #define is_done() (*(volatile int *)(&done)) |
211 | static volatile int done; | |
212 | ||
bf0720af JA |
213 | #define JHASH_RANDOM (0x3af5f2ee) |
214 | ||
215 | static inline int ppi_hash_pid(__u32 pid) | |
216 | { | |
217 | return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK; | |
218 | } | |
219 | ||
220 | static inline int ppi_hash_name(const char *name) | |
152f6476 | 221 | { |
bf0720af JA |
222 | return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK; |
223 | } | |
224 | ||
225 | static inline int ppi_hash(struct per_process_info *ppi) | |
226 | { | |
227 | if (ppi_hash_by_pid) | |
228 | return ppi_hash_pid(ppi->pid); | |
229 | ||
bf0720af | 230 | return ppi_hash_name(ppi->name); |
152f6476 JA |
231 | } |
232 | ||
233 | static inline void add_process_to_hash(struct per_process_info *ppi) | |
234 | { | |
bf0720af | 235 | const int hash_idx = ppi_hash(ppi); |
152f6476 | 236 | |
bf0720af JA |
237 | ppi->hash_next = ppi_hash_table[hash_idx]; |
238 | ppi_hash_table[hash_idx] = ppi; | |
152f6476 JA |
239 | } |
240 | ||
241 | static inline void add_process_to_list(struct per_process_info *ppi) | |
242 | { | |
243 | ppi->list_next = ppi_list; | |
244 | ppi_list = ppi; | |
886ecf0e | 245 | ppi_list_entries++; |
152f6476 JA |
246 | } |
247 | ||
bf0720af JA |
248 | static struct per_process_info *find_process_by_name(char *name) |
249 | { | |
250 | const int hash_idx = ppi_hash_name(name); | |
251 | struct per_process_info *ppi; | |
252 | ||
253 | ppi = ppi_hash_table[hash_idx]; | |
254 | while (ppi) { | |
255 | if (!strcmp(ppi->name, name)) | |
256 | return ppi; | |
257 | ||
258 | ppi = ppi->hash_next; | |
259 | } | |
260 | ||
261 | return NULL; | |
262 | } | |
263 | ||
152f6476 JA |
264 | static struct per_process_info *find_process_by_pid(__u32 pid) |
265 | { | |
bf0720af | 266 | const int hash_idx = ppi_hash_pid(pid); |
152f6476 JA |
267 | struct per_process_info *ppi; |
268 | ||
bf0720af | 269 | ppi = ppi_hash_table[hash_idx]; |
152f6476 JA |
270 | while (ppi) { |
271 | if (ppi->pid == pid) | |
272 | return ppi; | |
273 | ||
274 | ppi = ppi->hash_next; | |
275 | } | |
276 | ||
277 | return NULL; | |
278 | } | |
279 | ||
bf0720af JA |
280 | static struct per_process_info *find_process(__u32 pid, char *name) |
281 | { | |
715d8021 JA |
282 | struct per_process_info *ppi; |
283 | ||
bf0720af JA |
284 | if (ppi_hash_by_pid) |
285 | return find_process_by_pid(pid); | |
286 | ||
715d8021 JA |
287 | ppi = find_process_by_name(name); |
288 | if (ppi && ppi->pid != pid) | |
289 | ppi->more_than_one = 1; | |
290 | ||
291 | return ppi; | |
bf0720af JA |
292 | } |
293 | ||
23211fdf JA |
294 | static inline int trace_rb_insert(struct trace *t, struct rb_root *root, |
295 | int check_time) | |
7997c5b0 | 296 | { |
2a1b3424 | 297 | struct rb_node **p = &root->rb_node; |
7997c5b0 JA |
298 | struct rb_node *parent = NULL; |
299 | struct trace *__t; | |
300 | ||
301 | while (*p) { | |
302 | parent = *p; | |
2a1b3424 | 303 | |
7997c5b0 JA |
304 | __t = rb_entry(parent, struct trace, rb_node); |
305 | ||
23211fdf JA |
306 | if (check_time) { |
307 | if (t->bit->time < __t->bit->time) { | |
308 | p = &(*p)->rb_left; | |
309 | continue; | |
310 | } else if (t->bit->time > __t->bit->time) { | |
311 | p = &(*p)->rb_right; | |
312 | continue; | |
313 | } | |
314 | } | |
315 | if (t->bit->device < __t->bit->device) | |
e7c9f3ff NS |
316 | p = &(*p)->rb_left; |
317 | else if (t->bit->device > __t->bit->device) | |
318 | p = &(*p)->rb_right; | |
dcf0f7ed JA |
319 | else if (t->bit->sequence < __t->bit->sequence) |
320 | p = &(*p)->rb_left; | |
321 | else if (t->bit->sequence > __t->bit->sequence) | |
322 | p = &(*p)->rb_right; | |
e7c9f3ff NS |
323 | else if (t->bit->device == __t->bit->device) { |
324 | fprintf(stderr, | |
325 | "sequence alias (%d) on device %d,%d!\n", | |
326 | t->bit->sequence, | |
327 | MAJOR(t->bit->device), MINOR(t->bit->device)); | |
7997c5b0 JA |
328 | return 1; |
329 | } | |
330 | } | |
331 | ||
332 | rb_link_node(&t->rb_node, parent, p); | |
2a1b3424 | 333 | rb_insert_color(&t->rb_node, root); |
7997c5b0 JA |
334 | return 0; |
335 | } | |
336 | ||
2a1b3424 | 337 | static inline int trace_rb_insert_sort(struct trace *t) |
e3556946 | 338 | { |
23211fdf | 339 | if (!trace_rb_insert(t, &rb_sort_root, 1)) { |
2a1b3424 JA |
340 | rb_sort_entries++; |
341 | return 0; | |
342 | } | |
343 | ||
344 | return 1; | |
345 | } | |
346 | ||
347 | static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t) | |
348 | { | |
23211fdf | 349 | if (!trace_rb_insert(t, &pdi->rb_last, 1)) { |
2a1b3424 JA |
350 | pdi->rb_last_entries++; |
351 | return 0; | |
352 | } | |
353 | ||
354 | return 1; | |
355 | } | |
356 | ||
357 | static struct trace *trace_rb_find(dev_t device, unsigned long sequence, | |
358 | struct rb_root *root, int order) | |
359 | { | |
360 | struct rb_node *n = root->rb_node; | |
361 | struct rb_node *prev = NULL; | |
e3556946 JA |
362 | struct trace *__t; |
363 | ||
2a1b3424 JA |
364 | while (n) { |
365 | __t = rb_entry(n, struct trace, rb_node); | |
366 | prev = n; | |
e3556946 | 367 | |
0583b6a2 | 368 | if (device < __t->bit->device) |
2a1b3424 | 369 | n = n->rb_left; |
0583b6a2 | 370 | else if (device > __t->bit->device) |
2a1b3424 | 371 | n = n->rb_right; |
0583b6a2 | 372 | else if (sequence < __t->bit->sequence) |
2a1b3424 | 373 | n = n->rb_left; |
e3556946 | 374 | else if (sequence > __t->bit->sequence) |
2a1b3424 | 375 | n = n->rb_right; |
e3556946 JA |
376 | else |
377 | return __t; | |
378 | } | |
379 | ||
2a1b3424 JA |
380 | /* |
381 | * hack - the list may not be sequence ordered because some | |
382 | * events don't have sequence and time matched. so we end up | |
383 | * being a little off in the rb lookup here, because we don't | |
384 | * know the time we are looking for. compensate by browsing | |
385 | * a little ahead from the last entry to find the match | |
386 | */ | |
387 | if (order && prev) { | |
388 | int max = 5; | |
389 | ||
390 | while (((n = rb_next(prev)) != NULL) && max--) { | |
391 | __t = rb_entry(n, struct trace, rb_node); | |
392 | ||
393 | if (__t->bit->device == device && | |
394 | __t->bit->sequence == sequence) | |
395 | return __t; | |
396 | ||
397 | prev = n; | |
398 | } | |
399 | } | |
400 | ||
e3556946 JA |
401 | return NULL; |
402 | } | |
403 | ||
2a1b3424 JA |
404 | static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq) |
405 | { | |
406 | return trace_rb_find(dev, seq, &rb_sort_root, 1); | |
407 | } | |
408 | ||
409 | static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi, | |
410 | unsigned long seq) | |
411 | { | |
f7bd1a9b | 412 | return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0); |
2a1b3424 JA |
413 | } |
414 | ||
f7bd1a9b | 415 | static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot) |
7997c5b0 | 416 | { |
f7bd1a9b | 417 | struct rb_node **p = &pdi->rb_track.rb_node; |
7997c5b0 JA |
418 | struct rb_node *parent = NULL; |
419 | struct io_track *__iot; | |
420 | ||
421 | while (*p) { | |
422 | parent = *p; | |
7997c5b0 JA |
423 | __iot = rb_entry(parent, struct io_track, rb_node); |
424 | ||
f7bd1a9b | 425 | if (iot->sector < __iot->sector) |
7997c5b0 JA |
426 | p = &(*p)->rb_left; |
427 | else if (iot->sector > __iot->sector) | |
428 | p = &(*p)->rb_right; | |
429 | else { | |
e7c9f3ff | 430 | fprintf(stderr, |
ab197ca7 AB |
431 | "sector alias (%Lu) on device %d,%d!\n", |
432 | (unsigned long long) iot->sector, | |
f7bd1a9b | 433 | MAJOR(pdi->dev), MINOR(pdi->dev)); |
7997c5b0 JA |
434 | return 1; |
435 | } | |
436 | } | |
437 | ||
438 | rb_link_node(&iot->rb_node, parent, p); | |
f7bd1a9b | 439 | rb_insert_color(&iot->rb_node, &pdi->rb_track); |
7997c5b0 JA |
440 | return 0; |
441 | } | |
442 | ||
f7bd1a9b | 443 | static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector) |
7997c5b0 | 444 | { |
f7bd1a9b | 445 | struct rb_node *n = pdi->rb_track.rb_node; |
7997c5b0 JA |
446 | struct io_track *__iot; |
447 | ||
2a1b3424 JA |
448 | while (n) { |
449 | __iot = rb_entry(n, struct io_track, rb_node); | |
7997c5b0 | 450 | |
f7bd1a9b | 451 | if (sector < __iot->sector) |
2a1b3424 | 452 | n = n->rb_left; |
7997c5b0 | 453 | else if (sector > __iot->sector) |
2a1b3424 | 454 | n = n->rb_right; |
7997c5b0 JA |
455 | else |
456 | return __iot; | |
457 | } | |
458 | ||
459 | return NULL; | |
460 | } | |
461 | ||
f7bd1a9b JA |
462 | static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid, |
463 | char *comm, __u64 sector) | |
7997c5b0 | 464 | { |
916b5501 | 465 | struct io_track *iot; |
7997c5b0 | 466 | |
f7bd1a9b | 467 | iot = __find_track(pdi, sector); |
7997c5b0 JA |
468 | if (!iot) { |
469 | iot = malloc(sizeof(*iot)); | |
50adc0ba | 470 | iot->pid = pid; |
bf0720af | 471 | memcpy(iot->comm, comm, sizeof(iot->comm)); |
7997c5b0 | 472 | iot->sector = sector; |
f7bd1a9b | 473 | track_rb_insert(pdi, iot); |
7997c5b0 JA |
474 | } |
475 | ||
476 | return iot; | |
477 | } | |
478 | ||
f7bd1a9b JA |
479 | static void log_track_frontmerge(struct per_dev_info *pdi, |
480 | struct blk_io_trace *t) | |
2e3e8ded JA |
481 | { |
482 | struct io_track *iot; | |
483 | ||
484 | if (!track_ios) | |
485 | return; | |
2e3e8ded | 486 | |
f7bd1a9b | 487 | iot = __find_track(pdi, t->sector + (t->bytes >> 9)); |
cb2a1a62 | 488 | if (!iot) { |
acd70d21 | 489 | fprintf(stderr, "merge not found for (%d,%d): %llu\n", |
f7bd1a9b | 490 | MAJOR(pdi->dev), MINOR(pdi->dev), |
acd70d21 | 491 | t->sector + (t->bytes >> 9)); |
cb2a1a62 | 492 | return; |
2e3e8ded | 493 | } |
cb2a1a62 | 494 | |
f7bd1a9b | 495 | rb_erase(&iot->rb_node, &pdi->rb_track); |
a01516de | 496 | iot->sector -= t->bytes >> 9; |
f7bd1a9b | 497 | track_rb_insert(pdi, iot); |
2e3e8ded JA |
498 | } |
499 | ||
f7bd1a9b | 500 | static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t) |
2e3e8ded JA |
501 | { |
502 | struct io_track *iot; | |
503 | ||
504 | if (!track_ios) | |
505 | return; | |
506 | ||
f7bd1a9b | 507 | iot = find_track(pdi, t->pid, t->comm, t->sector); |
95c15013 JA |
508 | iot->allocation_time = t->time; |
509 | } | |
510 | ||
95c15013 | 511 | /* |
b6076a9b | 512 | * return time between rq allocation and insertion |
95c15013 | 513 | */ |
f7bd1a9b JA |
514 | static unsigned long long log_track_insert(struct per_dev_info *pdi, |
515 | struct blk_io_trace *t) | |
95c15013 | 516 | { |
50adc0ba | 517 | unsigned long long elapsed; |
95c15013 JA |
518 | struct io_track *iot; |
519 | ||
520 | if (!track_ios) | |
521 | return -1; | |
522 | ||
f7bd1a9b | 523 | iot = find_track(pdi, t->pid, t->comm, t->sector); |
2e3e8ded | 524 | iot->queue_time = t->time; |
acd70d21 JA |
525 | |
526 | if (!iot->allocation_time) | |
527 | return -1; | |
528 | ||
50adc0ba JA |
529 | elapsed = iot->queue_time - iot->allocation_time; |
530 | ||
531 | if (per_process_stats) { | |
bf0720af | 532 | struct per_process_info *ppi = find_process(iot->pid,iot->comm); |
b9d40d6f | 533 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
50adc0ba | 534 | |
b9d40d6f JA |
535 | if (ppi && elapsed > ppi->longest_allocation_wait[w]) |
536 | ppi->longest_allocation_wait[w] = elapsed; | |
50adc0ba JA |
537 | } |
538 | ||
539 | return elapsed; | |
2e3e8ded JA |
540 | } |
541 | ||
542 | /* | |
543 | * return time between queue and issue | |
544 | */ | |
f7bd1a9b JA |
545 | static unsigned long long log_track_issue(struct per_dev_info *pdi, |
546 | struct blk_io_trace *t) | |
2e3e8ded | 547 | { |
50adc0ba | 548 | unsigned long long elapsed; |
2e3e8ded JA |
549 | struct io_track *iot; |
550 | ||
551 | if (!track_ios) | |
552 | return -1; | |
553 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) | |
554 | return -1; | |
555 | ||
f7bd1a9b | 556 | iot = __find_track(pdi, t->sector); |
cb2a1a62 | 557 | if (!iot) { |
acd70d21 | 558 | fprintf(stderr, "issue not found for (%d,%d): %llu\n", |
f7bd1a9b | 559 | MAJOR(pdi->dev), MINOR(pdi->dev), t->sector); |
2e3e8ded | 560 | return -1; |
cb2a1a62 | 561 | } |
2e3e8ded JA |
562 | |
563 | iot->dispatch_time = t->time; | |
50adc0ba JA |
564 | elapsed = iot->dispatch_time - iot->queue_time; |
565 | ||
566 | if (per_process_stats) { | |
bf0720af | 567 | struct per_process_info *ppi = find_process(iot->pid,iot->comm); |
b9d40d6f | 568 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
50adc0ba | 569 | |
b9d40d6f JA |
570 | if (ppi && elapsed > ppi->longest_dispatch_wait[w]) |
571 | ppi->longest_dispatch_wait[w] = elapsed; | |
50adc0ba JA |
572 | } |
573 | ||
574 | return elapsed; | |
2e3e8ded JA |
575 | } |
576 | ||
577 | /* | |
578 | * return time between dispatch and complete | |
579 | */ | |
f7bd1a9b JA |
580 | static unsigned long long log_track_complete(struct per_dev_info *pdi, |
581 | struct blk_io_trace *t) | |
2e3e8ded JA |
582 | { |
583 | unsigned long long elapsed; | |
584 | struct io_track *iot; | |
585 | ||
586 | if (!track_ios) | |
587 | return -1; | |
588 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) | |
589 | return -1; | |
590 | ||
f7bd1a9b | 591 | iot = __find_track(pdi, t->sector); |
cb2a1a62 | 592 | if (!iot) { |
acd70d21 | 593 | fprintf(stderr, "complete not found for (%d,%d): %llu\n", |
f7bd1a9b | 594 | MAJOR(pdi->dev), MINOR(pdi->dev), t->sector); |
2e3e8ded | 595 | return -1; |
cb2a1a62 | 596 | } |
2e3e8ded JA |
597 | |
598 | iot->completion_time = t->time; | |
599 | elapsed = iot->completion_time - iot->dispatch_time; | |
600 | ||
50adc0ba | 601 | if (per_process_stats) { |
bf0720af | 602 | struct per_process_info *ppi = find_process(iot->pid,iot->comm); |
b9d40d6f | 603 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
50adc0ba | 604 | |
b9d40d6f JA |
605 | if (ppi && elapsed > ppi->longest_completion_wait[w]) |
606 | ppi->longest_completion_wait[w] = elapsed; | |
50adc0ba JA |
607 | } |
608 | ||
2e3e8ded JA |
609 | /* |
610 | * kill the trace, we don't need it after completion | |
611 | */ | |
f7bd1a9b | 612 | rb_erase(&iot->rb_node, &pdi->rb_track); |
2e3e8ded JA |
613 | free(iot); |
614 | ||
615 | return elapsed; | |
616 | } | |
617 | ||
618 | ||
152f6476 JA |
619 | static struct io_stats *find_process_io_stats(__u32 pid, char *name) |
620 | { | |
bf0720af | 621 | struct per_process_info *ppi = find_process(pid, name); |
152f6476 JA |
622 | |
623 | if (!ppi) { | |
624 | ppi = malloc(sizeof(*ppi)); | |
625 | memset(ppi, 0, sizeof(*ppi)); | |
bf0720af | 626 | memcpy(ppi->name, name, 16); |
152f6476 JA |
627 | ppi->pid = pid; |
628 | add_process_to_hash(ppi); | |
629 | add_process_to_list(ppi); | |
630 | } | |
631 | ||
632 | return &ppi->io_stats; | |
633 | } | |
634 | ||
e7c9f3ff | 635 | static void resize_cpu_info(struct per_dev_info *pdi, int cpu) |
a718bd37 | 636 | { |
e7c9f3ff NS |
637 | struct per_cpu_info *cpus = pdi->cpus; |
638 | int ncpus = pdi->ncpus; | |
639 | int new_count = cpu + 1; | |
640 | int new_space, size; | |
a718bd37 NS |
641 | char *new_start; |
642 | ||
e7c9f3ff NS |
643 | size = new_count * sizeof(struct per_cpu_info); |
644 | cpus = realloc(cpus, size); | |
645 | if (!cpus) { | |
646 | char name[20]; | |
647 | fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n", | |
648 | get_dev_name(pdi, name, sizeof(name)), size); | |
a718bd37 NS |
649 | exit(1); |
650 | } | |
651 | ||
e7c9f3ff NS |
652 | new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info)); |
653 | new_space = (new_count - ncpus) * sizeof(struct per_cpu_info); | |
a718bd37 | 654 | memset(new_start, 0, new_space); |
e7c9f3ff NS |
655 | |
656 | pdi->ncpus = new_count; | |
657 | pdi->cpus = cpus; | |
658 | } | |
cb2a1a62 | 659 | |
e7c9f3ff NS |
660 | static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu) |
661 | { | |
cb2a1a62 JA |
662 | struct per_cpu_info *pci; |
663 | ||
e7c9f3ff NS |
664 | if (cpu >= pdi->ncpus) |
665 | resize_cpu_info(pdi, cpu); | |
cb2a1a62 JA |
666 | |
667 | pci = &pdi->cpus[cpu]; | |
668 | pci->cpu = cpu; | |
669 | return pci; | |
a718bd37 NS |
670 | } |
671 | ||
e7c9f3ff NS |
672 | |
673 | static int resize_devices(char *name) | |
a718bd37 | 674 | { |
e7c9f3ff | 675 | int size = (ndevices + 1) * sizeof(struct per_dev_info); |
c499bf38 | 676 | |
e7c9f3ff NS |
677 | devices = realloc(devices, size); |
678 | if (!devices) { | |
679 | fprintf(stderr, "Out of memory, device %s (%d)\n", name, size); | |
680 | return 1; | |
681 | } | |
682 | memset(&devices[ndevices], 0, sizeof(struct per_dev_info)); | |
683 | devices[ndevices].name = name; | |
684 | ndevices++; | |
685 | return 0; | |
686 | } | |
a718bd37 | 687 | |
f7bd1a9b | 688 | static struct per_dev_info *get_dev_info(dev_t dev) |
e7c9f3ff | 689 | { |
cb2a1a62 | 690 | struct per_dev_info *pdi; |
e7c9f3ff | 691 | int i; |
c499bf38 | 692 | |
7d747d22 | 693 | for (i = 0; i < ndevices; i++) { |
f7bd1a9b JA |
694 | if (!devices[i].dev) |
695 | devices[i].dev = dev; | |
696 | if (devices[i].dev == dev) | |
e7c9f3ff | 697 | return &devices[i]; |
7d747d22 | 698 | } |
cb2a1a62 | 699 | |
2a1b3424 | 700 | if (resize_devices(NULL)) |
e7c9f3ff | 701 | return NULL; |
cb2a1a62 JA |
702 | |
703 | pdi = &devices[ndevices - 1]; | |
f7bd1a9b | 704 | pdi->dev = dev; |
d6222db8 | 705 | pdi->last_sequence = -1; |
287fa3d6 | 706 | pdi->last_read_time = 0; |
2a1b3424 JA |
707 | memset(&pdi->rb_last, 0, sizeof(pdi->rb_last)); |
708 | pdi->rb_last_entries = 0; | |
cb2a1a62 | 709 | return pdi; |
a718bd37 NS |
710 | } |
711 | ||
e7c9f3ff NS |
712 | static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size) |
713 | { | |
714 | if (pdi->name) | |
715 | snprintf(buffer, size, "%s", pdi->name); | |
716 | else | |
f7bd1a9b | 717 | snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev)); |
e7c9f3ff NS |
718 | return buffer; |
719 | } | |
720 | ||
e7c9f3ff | 721 | static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit) |
cfab07eb AB |
722 | { |
723 | unsigned long long this = bit->time; | |
e7c9f3ff | 724 | unsigned long long last = pdi->last_reported_time; |
cfab07eb | 725 | |
e7c9f3ff NS |
726 | pdi->backwards = (this < last) ? 'B' : ' '; |
727 | pdi->last_reported_time = this; | |
cfab07eb AB |
728 | } |
729 | ||
152f6476 JA |
730 | static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t, |
731 | int rw) | |
d0ca268b JA |
732 | { |
733 | if (rw) { | |
152f6476 JA |
734 | ios->mwrites++; |
735 | ios->qwrite_kb += t->bytes >> 10; | |
d0ca268b | 736 | } else { |
152f6476 JA |
737 | ios->mreads++; |
738 | ios->qread_kb += t->bytes >> 10; | |
739 | } | |
740 | } | |
741 | ||
742 | static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci, | |
743 | int rw) | |
744 | { | |
745 | __account_m(&pci->io_stats, t, rw); | |
746 | ||
747 | if (per_process_stats) { | |
748 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); | |
749 | ||
750 | __account_m(ios, t, rw); | |
d0ca268b JA |
751 | } |
752 | } | |
753 | ||
b6076a9b JA |
754 | static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t, |
755 | int rw) | |
d0ca268b JA |
756 | { |
757 | if (rw) { | |
152f6476 JA |
758 | ios->qwrites++; |
759 | ios->qwrite_kb += t->bytes >> 10; | |
d0ca268b | 760 | } else { |
152f6476 JA |
761 | ios->qreads++; |
762 | ios->qread_kb += t->bytes >> 10; | |
763 | } | |
764 | } | |
765 | ||
b6076a9b JA |
766 | static inline void account_queue(struct blk_io_trace *t, |
767 | struct per_cpu_info *pci, int rw) | |
152f6476 | 768 | { |
b6076a9b | 769 | __account_queue(&pci->io_stats, t, rw); |
152f6476 JA |
770 | |
771 | if (per_process_stats) { | |
772 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); | |
773 | ||
b6076a9b | 774 | __account_queue(ios, t, rw); |
d0ca268b JA |
775 | } |
776 | } | |
777 | ||
152f6476 | 778 | static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes) |
d0ca268b JA |
779 | { |
780 | if (rw) { | |
152f6476 JA |
781 | ios->cwrites++; |
782 | ios->cwrite_kb += bytes >> 10; | |
d0ca268b | 783 | } else { |
152f6476 JA |
784 | ios->creads++; |
785 | ios->cread_kb += bytes >> 10; | |
786 | } | |
787 | } | |
788 | ||
789 | static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci, | |
790 | int rw, int bytes) | |
791 | { | |
792 | __account_c(&pci->io_stats, rw, bytes); | |
793 | ||
794 | if (per_process_stats) { | |
795 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); | |
796 | ||
797 | __account_c(ios, rw, bytes); | |
d0ca268b JA |
798 | } |
799 | } | |
800 | ||
b6076a9b JA |
801 | static inline void __account_issue(struct io_stats *ios, int rw, |
802 | unsigned int bytes) | |
afd2d7ad | 803 | { |
804 | if (rw) { | |
152f6476 JA |
805 | ios->iwrites++; |
806 | ios->iwrite_kb += bytes >> 10; | |
afd2d7ad | 807 | } else { |
152f6476 JA |
808 | ios->ireads++; |
809 | ios->iread_kb += bytes >> 10; | |
afd2d7ad | 810 | } |
811 | } | |
812 | ||
b6076a9b JA |
813 | static inline void account_issue(struct blk_io_trace *t, |
814 | struct per_cpu_info *pci, int rw) | |
d0ca268b | 815 | { |
b6076a9b | 816 | __account_issue(&pci->io_stats, rw, t->bytes); |
152f6476 JA |
817 | |
818 | if (per_process_stats) { | |
819 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); | |
d5396421 | 820 | |
b6076a9b | 821 | __account_issue(ios, rw, t->bytes); |
152f6476 JA |
822 | } |
823 | } | |
824 | ||
06639b27 JA |
825 | static inline void __account_unplug(struct io_stats *ios, int timer) |
826 | { | |
827 | if (timer) | |
828 | ios->timer_unplugs++; | |
829 | else | |
830 | ios->io_unplugs++; | |
831 | } | |
832 | ||
833 | static inline void account_unplug(struct blk_io_trace *t, | |
834 | struct per_cpu_info *pci, int timer) | |
835 | { | |
836 | __account_unplug(&pci->io_stats, timer); | |
837 | ||
838 | if (per_process_stats) { | |
839 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); | |
840 | ||
841 | __account_unplug(ios, timer); | |
842 | } | |
843 | } | |
844 | ||
f7bd1a9b JA |
845 | static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci, |
846 | struct blk_io_trace *t, char *act) | |
ab197ca7 | 847 | { |
f7bd1a9b | 848 | process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL); |
ab197ca7 AB |
849 | } |
850 | ||
f7bd1a9b JA |
851 | static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci, |
852 | struct blk_io_trace *t, char *act) | |
b6076a9b | 853 | { |
f7bd1a9b | 854 | process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL); |
b6076a9b JA |
855 | } |
856 | ||
ab197ca7 AB |
857 | static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t, |
858 | char *act) | |
859 | { | |
b6076a9b | 860 | process_fmt(act, pci, t, -1, 0, NULL); |
ab197ca7 | 861 | } |
2e3e8ded | 862 | |
f7bd1a9b JA |
863 | static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci, |
864 | struct blk_io_trace *t, char *act) | |
ab197ca7 | 865 | { |
f7bd1a9b | 866 | process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL); |
d0ca268b JA |
867 | } |
868 | ||
f7bd1a9b JA |
869 | static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci, |
870 | struct blk_io_trace *t, char *act) | |
d0ca268b | 871 | { |
a01516de | 872 | if (act[0] == 'F') |
f7bd1a9b | 873 | log_track_frontmerge(pdi, t); |
2e3e8ded | 874 | |
ab197ca7 | 875 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
d0ca268b JA |
876 | } |
877 | ||
dfe34da1 | 878 | static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 879 | char *act) |
dfe34da1 | 880 | { |
ab197ca7 | 881 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
dfe34da1 JA |
882 | } |
883 | ||
d5396421 | 884 | static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 885 | char *act) |
d0ca268b | 886 | { |
ab197ca7 | 887 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
d0ca268b JA |
888 | } |
889 | ||
ab197ca7 | 890 | static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 891 | char *act) |
67e14fdc | 892 | { |
ab197ca7 | 893 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
67e14fdc JA |
894 | } |
895 | ||
93f1c611 JA |
896 | static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t, |
897 | char *act) | |
898 | { | |
899 | process_fmt(act, pci, t, -1ULL, 0, NULL); | |
900 | } | |
901 | ||
ab197ca7 | 902 | static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act) |
d0ca268b | 903 | { |
ab197ca7 | 904 | unsigned char *buf = (unsigned char *) t + sizeof(*t); |
d0ca268b | 905 | |
ab197ca7 | 906 | process_fmt(act, pci, t, -1ULL, t->pdu_len, buf); |
d0ca268b JA |
907 | } |
908 | ||
ff3a732c | 909 | static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci) |
d0ca268b | 910 | { |
56f2af81 JA |
911 | int act = t->action & 0xffff; |
912 | ||
913 | switch (act) { | |
d0ca268b | 914 | case __BLK_TA_QUEUE: |
3639a11e | 915 | log_generic(pci, t, "Q"); |
d0ca268b JA |
916 | break; |
917 | case __BLK_TA_GETRQ: | |
3639a11e | 918 | log_generic(pci, t, "G"); |
d0ca268b JA |
919 | break; |
920 | case __BLK_TA_SLEEPRQ: | |
3639a11e | 921 | log_generic(pci, t, "S"); |
d0ca268b JA |
922 | break; |
923 | case __BLK_TA_REQUEUE: | |
3639a11e | 924 | log_generic(pci, t, "R"); |
d0ca268b JA |
925 | break; |
926 | case __BLK_TA_ISSUE: | |
ab197ca7 | 927 | log_pc(pci, t, "D"); |
d0ca268b JA |
928 | break; |
929 | case __BLK_TA_COMPLETE: | |
3639a11e | 930 | log_pc(pci, t, "C"); |
d0ca268b | 931 | break; |
56f2af81 JA |
932 | case __BLK_TA_INSERT: |
933 | log_pc(pci, t, "I"); | |
934 | break; | |
d0ca268b | 935 | default: |
56f2af81 | 936 | fprintf(stderr, "Bad pc action %x\n", act); |
87b72777 | 937 | break; |
d0ca268b | 938 | } |
d0ca268b JA |
939 | } |
940 | ||
f7bd1a9b JA |
941 | static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi, |
942 | struct per_cpu_info *pci) | |
d0ca268b JA |
943 | { |
944 | int w = t->action & BLK_TC_ACT(BLK_TC_WRITE); | |
7997c5b0 | 945 | int act = t->action & 0xffff; |
d0ca268b | 946 | |
7997c5b0 | 947 | switch (act) { |
d0ca268b | 948 | case __BLK_TA_QUEUE: |
b6076a9b | 949 | account_queue(t, pci, w); |
3639a11e | 950 | log_queue(pci, t, "Q"); |
d0ca268b | 951 | break; |
b6076a9b | 952 | case __BLK_TA_INSERT: |
f7bd1a9b | 953 | log_insert(pdi, pci, t, "I"); |
b6076a9b | 954 | break; |
d0ca268b | 955 | case __BLK_TA_BACKMERGE: |
152f6476 | 956 | account_m(t, pci, w); |
f7bd1a9b | 957 | log_merge(pdi, pci, t, "M"); |
d0ca268b JA |
958 | break; |
959 | case __BLK_TA_FRONTMERGE: | |
152f6476 | 960 | account_m(t, pci, w); |
f7bd1a9b | 961 | log_merge(pdi, pci, t, "F"); |
d0ca268b JA |
962 | break; |
963 | case __BLK_TA_GETRQ: | |
f7bd1a9b | 964 | log_track_getrq(pdi, t); |
3639a11e | 965 | log_generic(pci, t, "G"); |
d0ca268b JA |
966 | break; |
967 | case __BLK_TA_SLEEPRQ: | |
3639a11e | 968 | log_generic(pci, t, "S"); |
d0ca268b JA |
969 | break; |
970 | case __BLK_TA_REQUEUE: | |
152f6476 | 971 | account_c(t, pci, w, -t->bytes); |
3639a11e | 972 | log_queue(pci, t, "R"); |
d0ca268b JA |
973 | break; |
974 | case __BLK_TA_ISSUE: | |
b6076a9b | 975 | account_issue(t, pci, w); |
f7bd1a9b | 976 | log_issue(pdi, pci, t, "D"); |
d0ca268b JA |
977 | break; |
978 | case __BLK_TA_COMPLETE: | |
152f6476 | 979 | account_c(t, pci, w, t->bytes); |
f7bd1a9b | 980 | log_complete(pdi, pci, t, "C"); |
d0ca268b | 981 | break; |
88b1a526 | 982 | case __BLK_TA_PLUG: |
3639a11e | 983 | log_action(pci, t, "P"); |
88b1a526 | 984 | break; |
3639a11e | 985 | case __BLK_TA_UNPLUG_IO: |
06639b27 | 986 | account_unplug(t, pci, 0); |
3639a11e JA |
987 | log_unplug(pci, t, "U"); |
988 | break; | |
989 | case __BLK_TA_UNPLUG_TIMER: | |
06639b27 | 990 | account_unplug(t, pci, 1); |
3639a11e | 991 | log_unplug(pci, t, "UT"); |
88b1a526 | 992 | break; |
93f1c611 JA |
993 | case __BLK_TA_SPLIT: |
994 | log_split(pci, t, "X"); | |
995 | break; | |
996 | case __BLK_TA_BOUNCE: | |
997 | log_generic(pci, t, "B"); | |
998 | break; | |
d0ca268b JA |
999 | default: |
1000 | fprintf(stderr, "Bad fs action %x\n", t->action); | |
1f79c4a0 | 1001 | break; |
d0ca268b | 1002 | } |
d0ca268b JA |
1003 | } |
1004 | ||
ff3a732c JA |
1005 | static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci, |
1006 | struct per_dev_info *pdi) | |
d0ca268b JA |
1007 | { |
1008 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) | |
ff3a732c | 1009 | dump_trace_pc(t, pci); |
d0ca268b | 1010 | else |
f7bd1a9b | 1011 | dump_trace_fs(t, pdi, pci); |
87b72777 | 1012 | |
e7c9f3ff | 1013 | pdi->events++; |
d0ca268b JA |
1014 | } |
1015 | ||
152f6476 | 1016 | static void dump_io_stats(struct io_stats *ios, char *msg) |
5c017e4b | 1017 | { |
152f6476 JA |
1018 | fprintf(ofp, "%s\n", msg); |
1019 | ||
1020 | fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb); | |
1021 | fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb); | |
0a6b8fc4 | 1022 | |
152f6476 JA |
1023 | fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb); |
1024 | fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb); | |
1025 | fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb); | |
1026 | fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb); | |
1027 | fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' '); | |
152f6476 | 1028 | fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites); |
06639b27 JA |
1029 | fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' '); |
1030 | fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs); | |
5c017e4b JA |
1031 | } |
1032 | ||
50adc0ba JA |
1033 | static void dump_wait_stats(struct per_process_info *ppi) |
1034 | { | |
b9d40d6f JA |
1035 | unsigned long rawait = ppi->longest_allocation_wait[0] / 1000; |
1036 | unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000; | |
1037 | unsigned long rcwait = ppi->longest_completion_wait[0] / 1000; | |
1038 | unsigned long wawait = ppi->longest_allocation_wait[1] / 1000; | |
1039 | unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000; | |
1040 | unsigned long wcwait = ppi->longest_completion_wait[1] / 1000; | |
1041 | ||
1042 | fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' '); | |
1043 | fprintf(ofp, " Allocation wait: %'8lu\n", wawait); | |
1044 | fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' '); | |
1045 | fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait); | |
1046 | fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' '); | |
1047 | fprintf(ofp, " Completion wait: %'8lu\n", wcwait); | |
50adc0ba JA |
1048 | } |
1049 | ||
886ecf0e JA |
1050 | static int ppi_name_compare(const void *p1, const void *p2) |
1051 | { | |
1052 | struct per_process_info *ppi1 = *((struct per_process_info **) p1); | |
1053 | struct per_process_info *ppi2 = *((struct per_process_info **) p2); | |
1054 | int res; | |
1055 | ||
1056 | res = strverscmp(ppi1->name, ppi2->name); | |
1057 | if (!res) | |
06e6f286 | 1058 | res = ppi1->pid > ppi2->pid; |
886ecf0e JA |
1059 | |
1060 | return res; | |
1061 | } | |
1062 | ||
1063 | static void sort_process_list(void) | |
1064 | { | |
1065 | struct per_process_info **ppis; | |
1066 | struct per_process_info *ppi; | |
1067 | int i = 0; | |
1068 | ||
1069 | ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *)); | |
1070 | ||
1071 | ppi = ppi_list; | |
1072 | while (ppi) { | |
06e6f286 | 1073 | ppis[i++] = ppi; |
886ecf0e JA |
1074 | ppi = ppi->list_next; |
1075 | } | |
1076 | ||
06e6f286 | 1077 | qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare); |
886ecf0e JA |
1078 | |
1079 | i = ppi_list_entries - 1; | |
1080 | ppi_list = NULL; | |
1081 | while (i >= 0) { | |
1082 | ppi = ppis[i]; | |
1083 | ||
1084 | ppi->list_next = ppi_list; | |
1085 | ppi_list = ppi; | |
1086 | i--; | |
1087 | } | |
50c38702 JA |
1088 | |
1089 | free(ppis); | |
886ecf0e JA |
1090 | } |
1091 | ||
152f6476 JA |
1092 | static void show_process_stats(void) |
1093 | { | |
1094 | struct per_process_info *ppi; | |
1095 | ||
886ecf0e JA |
1096 | sort_process_list(); |
1097 | ||
152f6476 JA |
1098 | ppi = ppi_list; |
1099 | while (ppi) { | |
ce8b6b4f JA |
1100 | char name[64]; |
1101 | ||
715d8021 | 1102 | if (ppi->more_than_one) |
bf0720af | 1103 | sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid); |
715d8021 JA |
1104 | else |
1105 | sprintf(name, "%s (%u)", ppi->name, ppi->pid); | |
bf0720af | 1106 | |
ce8b6b4f | 1107 | dump_io_stats(&ppi->io_stats, name); |
50adc0ba | 1108 | dump_wait_stats(ppi); |
152f6476 JA |
1109 | ppi = ppi->list_next; |
1110 | } | |
1111 | ||
1112 | fprintf(ofp, "\n"); | |
1113 | } | |
1114 | ||
e7c9f3ff | 1115 | static void show_device_and_cpu_stats(void) |
d0ca268b | 1116 | { |
e7c9f3ff NS |
1117 | struct per_dev_info *pdi; |
1118 | struct per_cpu_info *pci; | |
1119 | struct io_stats total, *ios; | |
1120 | int i, j, pci_events; | |
1121 | char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3]; | |
1122 | char name[32]; | |
1123 | ||
1124 | for (pdi = devices, i = 0; i < ndevices; i++, pdi++) { | |
1125 | ||
1126 | memset(&total, 0, sizeof(total)); | |
1127 | pci_events = 0; | |
1128 | ||
1129 | if (i > 0) | |
1130 | fprintf(ofp, "\n"); | |
1131 | ||
1132 | for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) { | |
1133 | if (!pci->nelems) | |
1134 | continue; | |
1135 | ||
1136 | ios = &pci->io_stats; | |
1137 | total.qreads += ios->qreads; | |
1138 | total.qwrites += ios->qwrites; | |
1139 | total.creads += ios->creads; | |
1140 | total.cwrites += ios->cwrites; | |
1141 | total.mreads += ios->mreads; | |
1142 | total.mwrites += ios->mwrites; | |
1143 | total.ireads += ios->ireads; | |
1144 | total.iwrites += ios->iwrites; | |
1145 | total.qread_kb += ios->qread_kb; | |
1146 | total.qwrite_kb += ios->qwrite_kb; | |
1147 | total.cread_kb += ios->cread_kb; | |
1148 | total.cwrite_kb += ios->cwrite_kb; | |
1149 | total.iread_kb += ios->iread_kb; | |
1150 | total.iwrite_kb += ios->iwrite_kb; | |
06639b27 JA |
1151 | total.timer_unplugs += ios->timer_unplugs; |
1152 | total.io_unplugs += ios->io_unplugs; | |
e7c9f3ff NS |
1153 | |
1154 | snprintf(line, sizeof(line) - 1, "CPU%d (%s):", | |
1155 | j, get_dev_name(pdi, name, sizeof(name))); | |
1156 | dump_io_stats(ios, line); | |
1157 | pci_events++; | |
1158 | } | |
5c017e4b | 1159 | |
e7c9f3ff NS |
1160 | if (pci_events > 1) { |
1161 | fprintf(ofp, "\n"); | |
1162 | snprintf(line, sizeof(line) - 1, "Total (%s):", | |
1163 | get_dev_name(pdi, name, sizeof(name))); | |
1164 | dump_io_stats(&total, line); | |
1165 | } | |
d0ca268b | 1166 | |
dcf0f7ed | 1167 | fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n", |
0aa8a62d | 1168 | get_dev_name(pdi, line, sizeof(line)), pdi->events, |
dcf0f7ed | 1169 | pdi->skips); |
e7c9f3ff | 1170 | } |
d0ca268b JA |
1171 | } |
1172 | ||
d36421e4 JA |
1173 | /* |
1174 | * struct trace and blktrace allocation cache, we do potentially | |
1175 | * millions of mallocs for these structures while only using at most | |
1176 | * a few thousand at the time | |
1177 | */ | |
1178 | static inline void t_free(struct trace *t) | |
1179 | { | |
1180 | t->next = t_alloc_list; | |
1181 | t_alloc_list = t; | |
1182 | } | |
1183 | ||
1184 | static inline struct trace *t_alloc(void) | |
1185 | { | |
1186 | struct trace *t = t_alloc_list; | |
1187 | ||
1188 | if (t) { | |
1189 | t_alloc_list = t->next; | |
1190 | return t; | |
1191 | } | |
1192 | ||
1193 | return malloc(sizeof(*t)); | |
1194 | } | |
1195 | ||
1196 | static inline void bit_free(struct blk_io_trace *bit) | |
1197 | { | |
1198 | /* | |
1199 | * abuse a 64-bit field for a next pointer for the free item | |
1200 | */ | |
0c39425c | 1201 | bit->time = (__u64) (unsigned long) bit_alloc_list; |
d36421e4 JA |
1202 | bit_alloc_list = (struct blk_io_trace *) bit; |
1203 | } | |
1204 | ||
1205 | static inline struct blk_io_trace *bit_alloc(void) | |
1206 | { | |
1207 | struct blk_io_trace *bit = bit_alloc_list; | |
1208 | ||
1209 | if (bit) { | |
0c39425c NS |
1210 | bit_alloc_list = (struct blk_io_trace *) (unsigned long) \ |
1211 | bit->time; | |
d36421e4 JA |
1212 | return bit; |
1213 | } | |
1214 | ||
1215 | return malloc(sizeof(*bit)); | |
1216 | } | |
1217 | ||
4f0ae44f JA |
1218 | static void find_genesis(void) |
1219 | { | |
1220 | struct trace *t = trace_list; | |
1221 | ||
1222 | genesis_time = -1ULL; | |
1223 | while (t != NULL) { | |
1224 | if (t->bit->time < genesis_time) | |
1225 | genesis_time = t->bit->time; | |
1226 | ||
1227 | t = t->next; | |
1228 | } | |
1229 | } | |
1230 | ||
7f4d89e6 | 1231 | static inline int check_stopwatch(struct blk_io_trace *bit) |
4f0ae44f | 1232 | { |
7f4d89e6 JA |
1233 | if (bit->time < stopwatch_end && |
1234 | bit->time >= stopwatch_start) | |
4f0ae44f JA |
1235 | return 0; |
1236 | ||
1237 | return 1; | |
1238 | } | |
1239 | ||
53c68c88 JA |
1240 | /* |
1241 | * return youngest entry read | |
1242 | */ | |
1243 | static int sort_entries(unsigned long long *youngest) | |
4f0ae44f JA |
1244 | { |
1245 | struct trace *t; | |
4f0ae44f JA |
1246 | |
1247 | if (!genesis_time) | |
1248 | find_genesis(); | |
1249 | ||
d6222db8 | 1250 | *youngest = 0; |
4f0ae44f JA |
1251 | while ((t = trace_list) != NULL) { |
1252 | struct blk_io_trace *bit = t->bit; | |
1253 | ||
1254 | trace_list = t->next; | |
1255 | ||
7f4d89e6 | 1256 | bit->time -= genesis_time; |
4f0ae44f | 1257 | |
d6222db8 JA |
1258 | if (bit->time < *youngest || !*youngest) |
1259 | *youngest = bit->time; | |
1260 | ||
7f4d89e6 | 1261 | if (check_stopwatch(bit)) { |
4f0ae44f JA |
1262 | bit_free(bit); |
1263 | t_free(t); | |
1264 | continue; | |
1265 | } | |
1266 | ||
2a1b3424 | 1267 | if (trace_rb_insert_sort(t)) |
53c68c88 | 1268 | return -1; |
4f0ae44f JA |
1269 | } |
1270 | ||
53c68c88 | 1271 | return 0; |
4f0ae44f JA |
1272 | } |
1273 | ||
2a1b3424 JA |
1274 | static inline void put_trace(struct per_dev_info *pdi, struct trace *t) |
1275 | { | |
1276 | rb_erase(&t->rb_node, &rb_sort_root); | |
1277 | rb_sort_entries--; | |
1278 | ||
1279 | trace_rb_insert_last(pdi, t); | |
1280 | ||
1281 | if (pdi->rb_last_entries > 1024) { | |
1282 | struct rb_node *n = rb_first(&pdi->rb_last); | |
1283 | ||
1284 | t = rb_entry(n, struct trace, rb_node); | |
1285 | rb_erase(n, &pdi->rb_last); | |
1286 | pdi->rb_last_entries--; | |
1287 | ||
1288 | bit_free(t->bit); | |
1289 | t_free(t); | |
1290 | } | |
1291 | } | |
1292 | ||
1293 | static int check_sequence(struct per_dev_info *pdi, struct blk_io_trace *bit, | |
1294 | int force) | |
1295 | { | |
1296 | unsigned long expected_sequence = pdi->last_sequence + 1; | |
1297 | struct trace *t; | |
d6222db8 JA |
1298 | |
1299 | /* | |
1300 | * first entry, always ok | |
1301 | */ | |
1302 | if (!expected_sequence) | |
1303 | return 0; | |
2a1b3424 JA |
1304 | |
1305 | if (bit->sequence == expected_sequence) | |
1306 | return 0; | |
1307 | ||
1308 | if (bit->sequence < expected_sequence && | |
1309 | bit->time > pdi->last_reported_time) | |
1310 | return 0; | |
1311 | ||
1312 | /* | |
1313 | * the wanted sequence is really there, continue | |
1314 | * because this means that the log time is earlier | |
1315 | * on the trace we have now */ | |
f7bd1a9b | 1316 | t = trace_rb_find_sort(pdi->dev, expected_sequence); |
2a1b3424 JA |
1317 | if (t) |
1318 | return 0; | |
1319 | ||
1320 | t = trace_rb_find_last(pdi, expected_sequence); | |
1321 | if (t) | |
1322 | return 0; | |
1323 | ||
1324 | /* | |
1325 | * unless this is the last run, break and wait for more entries | |
1326 | */ | |
1327 | if (!force) | |
1328 | return 1; | |
1329 | ||
f7bd1a9b JA |
1330 | fprintf(stderr, "(%d,%d): skipping %lu -> %u\n", MAJOR(pdi->dev), |
1331 | MINOR(pdi->dev), pdi->last_sequence, bit->sequence); | |
2a1b3424 JA |
1332 | pdi->skips++; |
1333 | return 0; | |
1334 | } | |
1335 | ||
a649216c | 1336 | static void show_entries_rb(int force) |
8fc0abbc | 1337 | { |
1f7afa72 JA |
1338 | struct per_dev_info *pdi = NULL; |
1339 | struct per_cpu_info *pci = NULL; | |
8fc0abbc | 1340 | struct blk_io_trace *bit; |
3aabcd89 | 1341 | struct rb_node *n; |
8fc0abbc | 1342 | struct trace *t; |
1f7afa72 | 1343 | |
7d747d22 | 1344 | while ((n = rb_first(&rb_sort_root)) != NULL) { |
1f7afa72 JA |
1345 | if (done) |
1346 | break; | |
8fc0abbc JA |
1347 | |
1348 | t = rb_entry(n, struct trace, rb_node); | |
1349 | bit = t->bit; | |
1350 | ||
f7bd1a9b | 1351 | if (!pdi || pdi->dev != bit->device) |
287fa3d6 | 1352 | pdi = get_dev_info(bit->device); |
1f7afa72 | 1353 | |
e7c9f3ff NS |
1354 | if (!pdi) { |
1355 | fprintf(stderr, "Unknown device ID? (%d,%d)\n", | |
1356 | MAJOR(bit->device), MINOR(bit->device)); | |
1357 | break; | |
1358 | } | |
1f7afa72 | 1359 | |
2a1b3424 JA |
1360 | if (check_sequence(pdi, bit, force)) |
1361 | break; | |
cb2a1a62 | 1362 | |
a649216c | 1363 | if (!force && bit->time > last_allowed_time) |
46e6968b | 1364 | break; |
8fc0abbc | 1365 | |
be925321 JA |
1366 | pdi->last_sequence = bit->sequence; |
1367 | ||
4f0ae44f | 1368 | check_time(pdi, bit); |
8fc0abbc | 1369 | |
4f0ae44f JA |
1370 | if (!pci || pci->cpu != bit->cpu) |
1371 | pci = get_cpu_info(pdi, bit->cpu); | |
287fa3d6 | 1372 | |
4f0ae44f | 1373 | dump_trace(bit, pci, pdi); |
87b72777 | 1374 | |
2a1b3424 | 1375 | put_trace(pdi, t); |
cb2a1a62 | 1376 | } |
8fc0abbc JA |
1377 | } |
1378 | ||
1f79c4a0 JA |
1379 | static int read_data(int fd, void *buffer, int bytes, int block) |
1380 | { | |
1381 | int ret, bytes_left, fl; | |
1382 | void *p; | |
1383 | ||
1384 | fl = fcntl(fd, F_GETFL); | |
1385 | ||
1386 | if (!block) | |
1387 | fcntl(fd, F_SETFL, fl | O_NONBLOCK); | |
1388 | else | |
1389 | fcntl(fd, F_SETFL, fl & ~O_NONBLOCK); | |
1390 | ||
1391 | bytes_left = bytes; | |
1392 | p = buffer; | |
1393 | while (bytes_left > 0) { | |
1394 | ret = read(fd, p, bytes_left); | |
1395 | if (!ret) | |
1396 | return 1; | |
1397 | else if (ret < 0) { | |
1398 | if (errno != EAGAIN) | |
1399 | perror("read"); | |
a649216c | 1400 | |
1f79c4a0 JA |
1401 | return -1; |
1402 | } else { | |
1403 | p += ret; | |
1404 | bytes_left -= ret; | |
1405 | } | |
1406 | } | |
1407 | ||
1408 | return 0; | |
1409 | } | |
1410 | ||
a649216c | 1411 | static int read_events(int fd, int always_block) |
cb2a1a62 | 1412 | { |
287fa3d6 | 1413 | struct per_dev_info *pdi = NULL; |
7d747d22 JA |
1414 | int events = 0; |
1415 | ||
1416 | while (!is_done() && events < rb_batch) { | |
1417 | struct blk_io_trace *bit; | |
1418 | struct trace *t; | |
1419 | int pdu_len; | |
1420 | __u32 magic; | |
1421 | ||
d36421e4 | 1422 | bit = bit_alloc(); |
cb2a1a62 | 1423 | |
a649216c | 1424 | if (read_data(fd, bit, sizeof(*bit), !events || always_block)) |
cb2a1a62 JA |
1425 | break; |
1426 | ||
7d747d22 JA |
1427 | magic = be32_to_cpu(bit->magic); |
1428 | if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { | |
1429 | fprintf(stderr, "Bad magic %x\n", magic); | |
1430 | break; | |
1431 | } | |
1432 | ||
1433 | pdu_len = be16_to_cpu(bit->pdu_len); | |
1434 | if (pdu_len) { | |
1435 | void *ptr = realloc(bit, sizeof(*bit) + pdu_len); | |
1436 | ||
1437 | if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1)) | |
1438 | break; | |
1439 | ||
1440 | bit = ptr; | |
1441 | } | |
1442 | ||
d6222db8 JA |
1443 | trace_to_cpu(bit); |
1444 | ||
1445 | if (verify_trace(bit)) { | |
1446 | bit_free(bit); | |
1447 | continue; | |
1448 | } | |
1449 | ||
d36421e4 | 1450 | t = t_alloc(); |
cb2a1a62 JA |
1451 | memset(t, 0, sizeof(*t)); |
1452 | t->bit = bit; | |
1453 | ||
7d747d22 JA |
1454 | t->next = trace_list; |
1455 | trace_list = t; | |
1f7afa72 | 1456 | |
f7bd1a9b | 1457 | if (!pdi || pdi->dev != bit->device) |
287fa3d6 JA |
1458 | pdi = get_dev_info(bit->device); |
1459 | ||
1460 | if (bit->time > pdi->last_read_time) | |
1461 | pdi->last_read_time = bit->time; | |
1462 | ||
7d747d22 | 1463 | events++; |
cb2a1a62 JA |
1464 | } |
1465 | ||
7d747d22 | 1466 | return events; |
cb2a1a62 JA |
1467 | } |
1468 | ||
d5396421 | 1469 | static int do_file(void) |
d0ca268b | 1470 | { |
7d747d22 | 1471 | struct per_cpu_info *pci; |
73877e12 NS |
1472 | struct per_dev_info *pdi; |
1473 | int i, j, events, events_added; | |
d0ca268b | 1474 | |
7d747d22 JA |
1475 | /* |
1476 | * first prepare all files for reading | |
1477 | */ | |
e8741a4a | 1478 | for (i = 0; i < ndevices; i++) { |
73877e12 NS |
1479 | pdi = &devices[i]; |
1480 | pdi->nfiles = 0; | |
d6222db8 | 1481 | pdi->last_sequence = -1; |
73877e12 | 1482 | |
74feaeb5 | 1483 | for (j = 0;; j++) { |
e7c9f3ff | 1484 | struct stat st; |
87b72777 | 1485 | |
e7c9f3ff NS |
1486 | pci = get_cpu_info(pdi, j); |
1487 | pci->cpu = j; | |
7d747d22 | 1488 | pci->fd = -1; |
d0ca268b | 1489 | |
e7c9f3ff | 1490 | snprintf(pci->fname, sizeof(pci->fname)-1, |
7d747d22 | 1491 | "%s.blktrace.%d", pdi->name, pci->cpu); |
e7c9f3ff NS |
1492 | if (stat(pci->fname, &st) < 0) |
1493 | break; | |
74feaeb5 AB |
1494 | if (st.st_size) { |
1495 | pci->fd = open(pci->fname, O_RDONLY); | |
1496 | if (pci->fd < 0) { | |
1497 | perror(pci->fname); | |
1498 | continue; | |
1499 | } | |
e7c9f3ff NS |
1500 | } |
1501 | ||
7d747d22 | 1502 | printf("Input file %s added\n", pci->fname); |
73877e12 | 1503 | pdi->nfiles++; |
d0ca268b | 1504 | } |
d5396421 JA |
1505 | } |
1506 | ||
7d747d22 JA |
1507 | /* |
1508 | * now loop over the files reading in the data | |
1509 | */ | |
412819ce | 1510 | do { |
53c68c88 JA |
1511 | unsigned long long youngest; |
1512 | ||
7d747d22 | 1513 | events_added = 0; |
287fa3d6 | 1514 | last_allowed_time = -1ULL; |
d5396421 | 1515 | |
7d747d22 | 1516 | for (i = 0; i < ndevices; i++) { |
73877e12 NS |
1517 | pdi = &devices[i]; |
1518 | ||
1519 | for (j = 0; j < pdi->nfiles; j++) { | |
d5396421 | 1520 | |
73877e12 | 1521 | pci = get_cpu_info(pdi, j); |
d5396421 | 1522 | |
7d747d22 JA |
1523 | if (pci->fd == -1) |
1524 | continue; | |
51128a28 | 1525 | |
a649216c | 1526 | events = read_events(pci->fd, 1); |
7d747d22 JA |
1527 | if (!events) { |
1528 | close(pci->fd); | |
1529 | pci->fd = -1; | |
1530 | continue; | |
1531 | } | |
d5396421 | 1532 | |
287fa3d6 JA |
1533 | if (pdi->last_read_time < last_allowed_time) |
1534 | last_allowed_time = pdi->last_read_time; | |
d5396421 | 1535 | |
7d747d22 JA |
1536 | events_added += events; |
1537 | } | |
2ff323b0 | 1538 | } |
d5396421 | 1539 | |
53c68c88 JA |
1540 | if (sort_entries(&youngest)) |
1541 | break; | |
1542 | ||
1543 | if (youngest > stopwatch_end) | |
287fa3d6 JA |
1544 | break; |
1545 | ||
a649216c | 1546 | show_entries_rb(0); |
cb2a1a62 | 1547 | |
7d747d22 | 1548 | } while (events_added); |
d5396421 | 1549 | |
a649216c JA |
1550 | if (rb_sort_entries) |
1551 | show_entries_rb(1); | |
1552 | ||
7d747d22 | 1553 | return 0; |
412819ce | 1554 | } |
d5396421 | 1555 | |
412819ce JA |
1556 | static int do_stdin(void) |
1557 | { | |
53c68c88 | 1558 | unsigned long long youngest; |
412819ce | 1559 | int fd; |
d5396421 | 1560 | |
be925321 | 1561 | last_allowed_time = -1ULL; |
1f79c4a0 | 1562 | fd = dup(STDIN_FILENO); |
412819ce JA |
1563 | do { |
1564 | int events; | |
d5396421 | 1565 | |
a649216c | 1566 | events = read_events(fd, 0); |
412819ce JA |
1567 | if (!events) |
1568 | break; | |
1569 | ||
53c68c88 JA |
1570 | if (sort_entries(&youngest)) |
1571 | break; | |
1572 | ||
1573 | if (youngest > stopwatch_end) | |
2ff323b0 JA |
1574 | break; |
1575 | ||
a649216c | 1576 | show_entries_rb(0); |
d5396421 JA |
1577 | } while (1); |
1578 | ||
a649216c JA |
1579 | if (rb_sort_entries) |
1580 | show_entries_rb(1); | |
1581 | ||
d5396421 | 1582 | close(fd); |
d5396421 JA |
1583 | return 0; |
1584 | } | |
d0ca268b | 1585 | |
1f79c4a0 | 1586 | static void flush_output(void) |
412819ce | 1587 | { |
152f6476 | 1588 | fflush(ofp); |
412819ce JA |
1589 | } |
1590 | ||
1f79c4a0 | 1591 | static void handle_sigint(int sig) |
412819ce JA |
1592 | { |
1593 | done = 1; | |
1594 | flush_output(); | |
1595 | } | |
1596 | ||
46e6968b NS |
1597 | /* |
1598 | * Extract start and duration times from a string, allowing | |
1599 | * us to specify a time interval of interest within a trace. | |
1600 | * Format: "duration" (start is zero) or "start:duration". | |
1601 | */ | |
1602 | static int find_stopwatch_interval(char *string) | |
1603 | { | |
1604 | double value; | |
1605 | char *sp; | |
1606 | ||
1607 | value = strtod(string, &sp); | |
1608 | if (sp == string) { | |
1609 | fprintf(stderr,"Invalid stopwatch timer: %s\n", string); | |
1610 | return 1; | |
1611 | } | |
1612 | if (*sp == ':') { | |
1613 | stopwatch_start = DOUBLE_TO_NANO_ULL(value); | |
1614 | string = sp + 1; | |
1615 | value = strtod(string, &sp); | |
1616 | if (sp == string || *sp != '\0') { | |
1617 | fprintf(stderr,"Invalid stopwatch duration time: %s\n", | |
1618 | string); | |
1619 | return 1; | |
1620 | } | |
1621 | } else if (*sp != '\0') { | |
1622 | fprintf(stderr,"Invalid stopwatch start timer: %s\n", string); | |
1623 | return 1; | |
1624 | } | |
1b928247 JA |
1625 | stopwatch_end = DOUBLE_TO_NANO_ULL(value); |
1626 | if (stopwatch_end <= stopwatch_start) { | |
1627 | fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n", | |
1628 | stopwatch_start, stopwatch_end); | |
1629 | return 1; | |
1630 | } | |
1631 | ||
46e6968b NS |
1632 | return 0; |
1633 | } | |
1634 | ||
52724a0e JA |
1635 | static char usage_str[] = \ |
1636 | "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \ | |
1637 | "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \ | |
1638 | "\t-i Input file containing trace data, or '-' for stdin\n" \ | |
1639 | "\t-o Output file. If not given, output is stdout\n" \ | |
1640 | "\t-b stdin read batching\n" \ | |
1641 | "\t-s Show per-program io statistics\n" \ | |
bf0720af | 1642 | "\t-n Hash processes by name, not pid\n" \ |
52724a0e JA |
1643 | "\t-t Track individual ios. Will tell you the time a request took\n" \ |
1644 | "\t to get queued, to get dispatched, and to get completed\n" \ | |
1645 | "\t-q Quiet. Don't display any stats at the end of the trace\n" \ | |
1646 | "\t-w Only parse data between the given time interval in seconds.\n" \ | |
1647 | "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \ | |
1648 | "\t -f Output format. Customize the output format. The format field\n" \ | |
1649 | "\t identifies can be found in the documentation\n" \ | |
1650 | "\t-F Format specification. Can be found in the documentation\n" \ | |
1651 | "\t-v Print program version info\n\n"; | |
1652 | ||
1f79c4a0 JA |
1653 | static void usage(char *prog) |
1654 | { | |
52724a0e | 1655 | fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str); |
1f79c4a0 JA |
1656 | } |
1657 | ||
d5396421 JA |
1658 | int main(int argc, char *argv[]) |
1659 | { | |
152f6476 | 1660 | char *ofp_buffer; |
a66877e6 | 1661 | int c, ret, mode; |
1e1c60f1 | 1662 | int per_device_and_cpu_stats = 1; |
d5396421 JA |
1663 | |
1664 | while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) { | |
1665 | switch (c) { | |
1666 | case 'i': | |
e7c9f3ff NS |
1667 | if (!strcmp(optarg, "-") && !pipeline) |
1668 | pipeline = 1; | |
1669 | else if (resize_devices(optarg) != 0) | |
1670 | return 1; | |
d5396421 JA |
1671 | break; |
1672 | case 'o': | |
66efebf8 | 1673 | output_name = optarg; |
d5396421 | 1674 | break; |
79f19470 JA |
1675 | case 'b': |
1676 | rb_batch = atoi(optarg); | |
1677 | if (rb_batch <= 0) | |
1678 | rb_batch = RB_BATCH_DEFAULT; | |
1679 | break; | |
152f6476 JA |
1680 | case 's': |
1681 | per_process_stats = 1; | |
1682 | break; | |
7997c5b0 JA |
1683 | case 't': |
1684 | track_ios = 1; | |
1685 | break; | |
1e1c60f1 NS |
1686 | case 'q': |
1687 | per_device_and_cpu_stats = 0; | |
1688 | break; | |
46e6968b NS |
1689 | case 'w': |
1690 | if (find_stopwatch_interval(optarg) != 0) | |
1691 | return 1; | |
1692 | break; | |
ab197ca7 AB |
1693 | case 'f': |
1694 | set_all_format_specs(optarg); | |
1695 | break; | |
1696 | case 'F': | |
1697 | if (add_format_spec(optarg) != 0) | |
1698 | return 1; | |
1699 | break; | |
bf0720af | 1700 | case 'n': |
715d8021 | 1701 | ppi_hash_by_pid = 0; |
bf0720af | 1702 | break; |
52724a0e JA |
1703 | case 'v': |
1704 | printf("%s version %s\n", argv[0], blkparse_version); | |
1705 | return 0; | |
d5396421 | 1706 | default: |
1f79c4a0 | 1707 | usage(argv[0]); |
d5396421 JA |
1708 | return 1; |
1709 | } | |
d0ca268b JA |
1710 | } |
1711 | ||
e7c9f3ff NS |
1712 | while (optind < argc) { |
1713 | if (!strcmp(argv[optind], "-") && !pipeline) | |
1714 | pipeline = 1; | |
1715 | else if (resize_devices(argv[optind]) != 0) | |
1716 | return 1; | |
1717 | optind++; | |
1718 | } | |
1719 | ||
1720 | if (!pipeline && !ndevices) { | |
1f79c4a0 | 1721 | usage(argv[0]); |
d5396421 JA |
1722 | return 1; |
1723 | } | |
1724 | ||
7997c5b0 | 1725 | memset(&rb_sort_root, 0, sizeof(rb_sort_root)); |
412819ce JA |
1726 | |
1727 | signal(SIGINT, handle_sigint); | |
1728 | signal(SIGHUP, handle_sigint); | |
1729 | signal(SIGTERM, handle_sigint); | |
d5396421 | 1730 | |
d69db225 JA |
1731 | setlocale(LC_NUMERIC, "en_US"); |
1732 | ||
a66877e6 | 1733 | if (!output_name) { |
152f6476 | 1734 | ofp = fdopen(STDOUT_FILENO, "w"); |
a66877e6 JA |
1735 | mode = _IOLBF; |
1736 | } else { | |
152f6476 JA |
1737 | char ofname[128]; |
1738 | ||
7a9690c0 | 1739 | snprintf(ofname, sizeof(ofname) - 1, "%s", output_name); |
152f6476 | 1740 | ofp = fopen(ofname, "w"); |
a66877e6 | 1741 | mode = _IOFBF; |
152f6476 JA |
1742 | } |
1743 | ||
1744 | if (!ofp) { | |
1745 | perror("fopen"); | |
1746 | return 1; | |
1747 | } | |
1748 | ||
1749 | ofp_buffer = malloc(4096); | |
a66877e6 | 1750 | if (setvbuf(ofp, ofp_buffer, mode, 4096)) { |
152f6476 JA |
1751 | perror("setvbuf"); |
1752 | return 1; | |
1753 | } | |
1754 | ||
e7c9f3ff | 1755 | if (pipeline) |
d5396421 JA |
1756 | ret = do_stdin(); |
1757 | else | |
1758 | ret = do_file(); | |
1759 | ||
152f6476 JA |
1760 | if (per_process_stats) |
1761 | show_process_stats(); | |
1762 | ||
1e1c60f1 NS |
1763 | if (per_device_and_cpu_stats) |
1764 | show_device_and_cpu_stats(); | |
152f6476 | 1765 | |
412819ce | 1766 | flush_output(); |
d5396421 | 1767 | return ret; |
d0ca268b | 1768 | } |