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