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