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