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