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