Commit | Line | Data |
---|---|---|
d956a2cd JA |
1 | /* |
2 | * block queue tracing parse application | |
3 | * | |
4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> | |
46e37c55 | 5 | * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> |
d956a2cd JA |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
d0ca268b JA |
22 | #include <sys/types.h> |
23 | #include <sys/stat.h> | |
24 | #include <unistd.h> | |
25 | #include <stdio.h> | |
26 | #include <fcntl.h> | |
27 | #include <stdlib.h> | |
8fc0abbc | 28 | #include <string.h> |
d5396421 | 29 | #include <getopt.h> |
412819ce JA |
30 | #include <errno.h> |
31 | #include <signal.h> | |
d69db225 | 32 | #include <locale.h> |
6e0073ed | 33 | #include <libgen.h> |
d0ca268b | 34 | |
8fc0abbc JA |
35 | #include "blktrace.h" |
36 | #include "rbtree.h" | |
bf0720af | 37 | #include "jhash.h" |
d0ca268b | 38 | |
a5981e2e | 39 | static char blkparse_version[] = "0.99.3"; |
52724a0e | 40 | |
492da111 AB |
41 | struct skip_info { |
42 | unsigned long start, end; | |
43 | struct skip_info *prev, *next; | |
44 | }; | |
45 | ||
e7c9f3ff | 46 | struct per_dev_info { |
f7bd1a9b | 47 | dev_t dev; |
e7c9f3ff NS |
48 | char *name; |
49 | ||
50 | int backwards; | |
51 | unsigned long long events; | |
20ed6177 | 52 | unsigned long long first_reported_time; |
e7c9f3ff | 53 | unsigned long long last_reported_time; |
287fa3d6 | 54 | unsigned long long last_read_time; |
e7c9f3ff | 55 | struct io_stats io_stats; |
2990e589 JA |
56 | unsigned long skips; |
57 | unsigned long long seq_skips; | |
649c7b66 JA |
58 | unsigned int max_depth[2]; |
59 | unsigned int cur_depth[2]; | |
e7c9f3ff | 60 | |
f7bd1a9b JA |
61 | struct rb_root rb_track; |
62 | ||
73877e12 | 63 | int nfiles; |
e7c9f3ff | 64 | int ncpus; |
824c2b39 JA |
65 | |
66 | unsigned long *cpu_map; | |
67 | unsigned int cpu_map_max; | |
68 | ||
e7c9f3ff NS |
69 | struct per_cpu_info *cpus; |
70 | }; | |
71 | ||
2990e589 JA |
72 | /* |
73 | * some duplicated effort here, we can unify this hash and the ppi hash later | |
74 | */ | |
75 | struct process_pid_map { | |
76 | pid_t pid; | |
77 | char comm[16]; | |
78 | struct process_pid_map *hash_next, *list_next; | |
79 | }; | |
80 | ||
81 | #define PPM_HASH_SHIFT (8) | |
82 | #define PPM_HASH_SIZE (1 << PPM_HASH_SHIFT) | |
83 | #define PPM_HASH_MASK (PPM_HASH_SIZE - 1) | |
84 | static struct process_pid_map *ppm_hash_table[PPM_HASH_SIZE]; | |
85 | ||
152f6476 | 86 | struct per_process_info { |
2990e589 | 87 | struct process_pid_map *ppm; |
152f6476 JA |
88 | struct io_stats io_stats; |
89 | struct per_process_info *hash_next, *list_next; | |
715d8021 | 90 | int more_than_one; |
50adc0ba JA |
91 | |
92 | /* | |
93 | * individual io stats | |
94 | */ | |
b9d40d6f JA |
95 | unsigned long long longest_allocation_wait[2]; |
96 | unsigned long long longest_dispatch_wait[2]; | |
97 | unsigned long long longest_completion_wait[2]; | |
d0ca268b JA |
98 | }; |
99 | ||
152f6476 | 100 | #define PPI_HASH_SHIFT (8) |
bf0720af JA |
101 | #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT) |
102 | #define PPI_HASH_MASK (PPI_HASH_SIZE - 1) | |
103 | static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE]; | |
152f6476 | 104 | static struct per_process_info *ppi_list; |
886ecf0e | 105 | static int ppi_list_entries; |
152f6476 | 106 | |
234db09d | 107 | #define S_OPTS "a:A:b:D:d:f:F:hi:o:Oqstw:vV" |
d5396421 | 108 | static struct option l_opts[] = { |
98f8386b AB |
109 | { |
110 | .name = "act-mask", | |
111 | .has_arg = required_argument, | |
112 | .flag = NULL, | |
113 | .val = 'a' | |
114 | }, | |
115 | { | |
116 | .name = "set-mask", | |
117 | .has_arg = required_argument, | |
118 | .flag = NULL, | |
119 | .val = 'A' | |
120 | }, | |
d5396421 | 121 | { |
234db09d | 122 | .name = "batch", |
428683db | 123 | .has_arg = required_argument, |
d5396421 | 124 | .flag = NULL, |
234db09d | 125 | .val = 'b' |
d5396421 JA |
126 | }, |
127 | { | |
234db09d | 128 | .name = "input-directory", |
428683db | 129 | .has_arg = required_argument, |
d5396421 | 130 | .flag = NULL, |
234db09d | 131 | .val = 'D' |
d5396421 | 132 | }, |
79f19470 | 133 | { |
234db09d | 134 | .name = "dump-binary", |
428683db | 135 | .has_arg = required_argument, |
79f19470 | 136 | .flag = NULL, |
234db09d | 137 | .val = 'd' |
79f19470 | 138 | }, |
152f6476 | 139 | { |
234db09d AB |
140 | .name = "format", |
141 | .has_arg = required_argument, | |
152f6476 | 142 | .flag = NULL, |
234db09d | 143 | .val = 'f' |
152f6476 | 144 | }, |
7997c5b0 | 145 | { |
234db09d AB |
146 | .name = "format-spec", |
147 | .has_arg = required_argument, | |
7997c5b0 | 148 | .flag = NULL, |
234db09d | 149 | .val = 'F' |
7997c5b0 | 150 | }, |
1e1c60f1 | 151 | { |
234db09d | 152 | .name = "hash-by-name", |
428683db | 153 | .has_arg = no_argument, |
1e1c60f1 | 154 | .flag = NULL, |
234db09d | 155 | .val = 'h' |
1e1c60f1 | 156 | }, |
46e6968b | 157 | { |
234db09d | 158 | .name = "input", |
428683db | 159 | .has_arg = required_argument, |
46e6968b | 160 | .flag = NULL, |
234db09d | 161 | .val = 'i' |
46e6968b | 162 | }, |
ab197ca7 | 163 | { |
234db09d | 164 | .name = "output", |
428683db | 165 | .has_arg = required_argument, |
ab197ca7 | 166 | .flag = NULL, |
234db09d | 167 | .val = 'o' |
ab197ca7 AB |
168 | }, |
169 | { | |
234db09d AB |
170 | .name = "no-text-output", |
171 | .has_arg = no_argument, | |
ab197ca7 | 172 | .flag = NULL, |
234db09d | 173 | .val = 'O' |
ab197ca7 | 174 | }, |
bf0720af | 175 | { |
234db09d | 176 | .name = "quiet", |
bf0720af JA |
177 | .has_arg = no_argument, |
178 | .flag = NULL, | |
234db09d | 179 | .val = 'q' |
bf0720af | 180 | }, |
7d1c0411 | 181 | { |
234db09d | 182 | .name = "per-program-stats", |
7d1c0411 JA |
183 | .has_arg = no_argument, |
184 | .flag = NULL, | |
234db09d | 185 | .val = 's' |
7d1c0411 | 186 | }, |
52724a0e | 187 | { |
234db09d | 188 | .name = "track-ios", |
52724a0e JA |
189 | .has_arg = no_argument, |
190 | .flag = NULL, | |
234db09d | 191 | .val = 't' |
52724a0e | 192 | }, |
d1d7f15f | 193 | { |
234db09d | 194 | .name = "stopwatch", |
d1d7f15f JA |
195 | .has_arg = required_argument, |
196 | .flag = NULL, | |
234db09d | 197 | .val = 'w' |
d1d7f15f | 198 | }, |
a2594911 | 199 | { |
234db09d AB |
200 | .name = "verbose", |
201 | .has_arg = no_argument, | |
a2594911 | 202 | .flag = NULL, |
234db09d AB |
203 | .val = 'v' |
204 | }, | |
205 | { | |
206 | .name = "version", | |
207 | .has_arg = no_argument, | |
208 | .flag = NULL, | |
209 | .val = 'V' | |
a2594911 | 210 | }, |
71ef8b7c JA |
211 | { |
212 | .name = NULL, | |
213 | } | |
d5396421 JA |
214 | }; |
215 | ||
7997c5b0 JA |
216 | /* |
217 | * for sorting the displayed output | |
218 | */ | |
8fc0abbc JA |
219 | struct trace { |
220 | struct blk_io_trace *bit; | |
221 | struct rb_node rb_node; | |
cb2a1a62 | 222 | struct trace *next; |
a43c1c17 | 223 | unsigned long read_sequence; |
8fc0abbc JA |
224 | }; |
225 | ||
cb2a1a62 | 226 | static struct rb_root rb_sort_root; |
a649216c JA |
227 | static unsigned long rb_sort_entries; |
228 | ||
cb2a1a62 JA |
229 | static struct trace *trace_list; |
230 | ||
d36421e4 JA |
231 | /* |
232 | * allocation cache | |
233 | */ | |
234 | static struct blk_io_trace *bit_alloc_list; | |
235 | static struct trace *t_alloc_list; | |
236 | ||
7997c5b0 JA |
237 | /* |
238 | * for tracking individual ios | |
239 | */ | |
240 | struct io_track { | |
241 | struct rb_node rb_node; | |
242 | ||
2990e589 | 243 | struct process_pid_map *ppm; |
7997c5b0 | 244 | __u64 sector; |
95c15013 | 245 | unsigned long long allocation_time; |
7997c5b0 JA |
246 | unsigned long long queue_time; |
247 | unsigned long long dispatch_time; | |
248 | unsigned long long completion_time; | |
249 | }; | |
250 | ||
e7c9f3ff NS |
251 | static int ndevices; |
252 | static struct per_dev_info *devices; | |
253 | static char *get_dev_name(struct per_dev_info *, char *, int); | |
210824c3 | 254 | static int trace_rb_insert_last(struct per_dev_info *, struct trace *); |
d0ca268b | 255 | |
71d5d4c9 | 256 | FILE *ofp = NULL; |
e7c9f3ff | 257 | static char *output_name; |
d1d7f15f | 258 | static char *input_dir; |
e7c9f3ff NS |
259 | |
260 | static unsigned long long genesis_time; | |
287fa3d6 | 261 | static unsigned long long last_allowed_time; |
46e6968b | 262 | static unsigned long long stopwatch_start; /* start from zero by default */ |
bc171579 | 263 | static unsigned long long stopwatch_end = -1ULL; /* "infinity" */ |
a43c1c17 | 264 | static unsigned long read_sequence; |
152f6476 JA |
265 | |
266 | static int per_process_stats; | |
cbc927b6 | 267 | static int per_device_and_cpu_stats = 1; |
7997c5b0 | 268 | static int track_ios; |
bf0720af | 269 | static int ppi_hash_by_pid = 1; |
57ea8602 | 270 | static int verbose; |
98f8386b | 271 | static unsigned int act_mask = -1U; |
cbc927b6 | 272 | static int stats_printed; |
86368eb5 | 273 | int data_is_native = -1; |
d0ca268b | 274 | |
346d8a74 | 275 | static FILE *dump_fp; |
a2594911 AB |
276 | static char *dump_binary; |
277 | ||
1d24fc14 JA |
278 | static unsigned int t_alloc_cache; |
279 | static unsigned int bit_alloc_cache; | |
280 | ||
7d747d22 | 281 | #define RB_BATCH_DEFAULT (512) |
e820abd7 | 282 | static unsigned int rb_batch = RB_BATCH_DEFAULT; |
79f19470 | 283 | |
e7c9f3ff | 284 | static int pipeline; |
67076cbc | 285 | static char *pipename; |
e7c9f3ff | 286 | |
234db09d AB |
287 | static int text_output = 1; |
288 | ||
412819ce JA |
289 | #define is_done() (*(volatile int *)(&done)) |
290 | static volatile int done; | |
291 | ||
7bd4fd0a OK |
292 | struct timespec abs_start_time; |
293 | static unsigned long long start_timestamp; | |
294 | ||
bf0720af JA |
295 | #define JHASH_RANDOM (0x3af5f2ee) |
296 | ||
824c2b39 JA |
297 | #define CPUS_PER_LONG (8 * sizeof(unsigned long)) |
298 | #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG) | |
299 | #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1)) | |
300 | ||
a2594911 AB |
301 | static void output_binary(void *buf, int len) |
302 | { | |
303 | if (dump_binary) { | |
346d8a74 AB |
304 | size_t n = fwrite(buf, len, 1, dump_fp); |
305 | if (n != 1) { | |
a2594911 | 306 | perror(dump_binary); |
346d8a74 | 307 | fclose(dump_fp); |
a2594911 AB |
308 | dump_binary = NULL; |
309 | } | |
310 | } | |
311 | } | |
312 | ||
210824c3 JA |
313 | static void resize_cpu_info(struct per_dev_info *pdi, int cpu) |
314 | { | |
315 | struct per_cpu_info *cpus = pdi->cpus; | |
316 | int ncpus = pdi->ncpus; | |
317 | int new_count = cpu + 1; | |
318 | int new_space, size; | |
319 | char *new_start; | |
320 | ||
321 | size = new_count * sizeof(struct per_cpu_info); | |
322 | cpus = realloc(cpus, size); | |
323 | if (!cpus) { | |
324 | char name[20]; | |
325 | fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n", | |
326 | get_dev_name(pdi, name, sizeof(name)), size); | |
327 | exit(1); | |
328 | } | |
329 | ||
330 | new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info)); | |
331 | new_space = (new_count - ncpus) * sizeof(struct per_cpu_info); | |
332 | memset(new_start, 0, new_space); | |
333 | ||
334 | pdi->ncpus = new_count; | |
335 | pdi->cpus = cpus; | |
336 | ||
337 | for (new_count = 0; new_count < pdi->ncpus; new_count++) { | |
338 | struct per_cpu_info *pci = &pdi->cpus[new_count]; | |
339 | ||
340 | if (!pci->fd) { | |
341 | pci->fd = -1; | |
342 | memset(&pci->rb_last, 0, sizeof(pci->rb_last)); | |
343 | pci->rb_last_entries = 0; | |
344 | pci->last_sequence = -1; | |
345 | } | |
346 | } | |
347 | } | |
348 | ||
349 | static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu) | |
350 | { | |
351 | struct per_cpu_info *pci; | |
352 | ||
353 | if (cpu >= pdi->ncpus) | |
354 | resize_cpu_info(pdi, cpu); | |
355 | ||
356 | pci = &pdi->cpus[cpu]; | |
357 | pci->cpu = cpu; | |
358 | return pci; | |
359 | } | |
360 | ||
361 | ||
362 | static int resize_devices(char *name) | |
363 | { | |
364 | int size = (ndevices + 1) * sizeof(struct per_dev_info); | |
365 | ||
366 | devices = realloc(devices, size); | |
367 | if (!devices) { | |
368 | fprintf(stderr, "Out of memory, device %s (%d)\n", name, size); | |
369 | return 1; | |
370 | } | |
371 | memset(&devices[ndevices], 0, sizeof(struct per_dev_info)); | |
372 | devices[ndevices].name = name; | |
373 | ndevices++; | |
374 | return 0; | |
375 | } | |
376 | ||
377 | static struct per_dev_info *get_dev_info(dev_t dev) | |
378 | { | |
379 | struct per_dev_info *pdi; | |
380 | int i; | |
381 | ||
382 | for (i = 0; i < ndevices; i++) { | |
383 | if (!devices[i].dev) | |
384 | devices[i].dev = dev; | |
385 | if (devices[i].dev == dev) | |
386 | return &devices[i]; | |
387 | } | |
388 | ||
389 | if (resize_devices(NULL)) | |
390 | return NULL; | |
391 | ||
392 | pdi = &devices[ndevices - 1]; | |
393 | pdi->dev = dev; | |
394 | pdi->first_reported_time = 0; | |
395 | pdi->last_read_time = 0; | |
210824c3 JA |
396 | |
397 | return pdi; | |
398 | } | |
399 | ||
66930177 | 400 | static void insert_skip(struct per_cpu_info *pci, unsigned long start, |
492da111 AB |
401 | unsigned long end) |
402 | { | |
403 | struct skip_info *sip; | |
404 | ||
66930177 | 405 | for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) { |
492da111 AB |
406 | if (end == (sip->start - 1)) { |
407 | sip->start = start; | |
408 | return; | |
409 | } else if (start == (sip->end + 1)) { | |
410 | sip->end = end; | |
411 | return; | |
412 | } | |
413 | } | |
414 | ||
415 | sip = malloc(sizeof(struct skip_info)); | |
416 | sip->start = start; | |
417 | sip->end = end; | |
418 | sip->prev = sip->next = NULL; | |
66930177 JA |
419 | if (pci->skips_tail == NULL) |
420 | pci->skips_head = pci->skips_tail = sip; | |
492da111 | 421 | else { |
66930177 JA |
422 | sip->prev = pci->skips_tail; |
423 | pci->skips_tail->next = sip; | |
424 | pci->skips_tail = sip; | |
492da111 AB |
425 | } |
426 | } | |
427 | ||
66930177 | 428 | static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip) |
492da111 AB |
429 | { |
430 | if (sip->prev == NULL) { | |
431 | if (sip->next == NULL) | |
66930177 | 432 | pci->skips_head = pci->skips_tail = NULL; |
492da111 | 433 | else { |
66930177 | 434 | pci->skips_head = sip->next; |
492da111 AB |
435 | sip->next->prev = NULL; |
436 | } | |
437 | } else if (sip->next == NULL) { | |
66930177 | 438 | pci->skips_tail = sip->prev; |
492da111 AB |
439 | sip->prev->next = NULL; |
440 | } else { | |
441 | sip->prev->next = sip->next; | |
442 | sip->next->prev = sip->prev; | |
443 | } | |
444 | ||
445 | sip->prev = sip->next = NULL; | |
446 | free(sip); | |
447 | } | |
448 | ||
449 | #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end)) | |
66930177 | 450 | static int check_current_skips(struct per_cpu_info *pci, unsigned long seq) |
492da111 AB |
451 | { |
452 | struct skip_info *sip; | |
453 | ||
66930177 JA |
454 | for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) { |
455 | if (IN_SKIP(sip, seq)) { | |
492da111 AB |
456 | if (sip->start == seq) { |
457 | if (sip->end == seq) | |
66930177 | 458 | remove_sip(pci, sip); |
492da111 AB |
459 | else |
460 | sip->start += 1; | |
461 | } else if (sip->end == seq) | |
462 | sip->end -= 1; | |
463 | else { | |
464 | sip->end = seq - 1; | |
66930177 | 465 | insert_skip(pci, seq + 1, sip->end); |
492da111 AB |
466 | } |
467 | return 1; | |
468 | } | |
469 | } | |
66930177 | 470 | |
492da111 AB |
471 | return 0; |
472 | } | |
473 | ||
474 | static void collect_pdi_skips(struct per_dev_info *pdi) | |
475 | { | |
476 | struct skip_info *sip; | |
66930177 | 477 | int cpu; |
492da111 AB |
478 | |
479 | pdi->skips = 0; | |
480 | pdi->seq_skips = 0; | |
66930177 JA |
481 | |
482 | for (cpu = 0; cpu < pdi->ncpus; cpu++) { | |
483 | struct per_cpu_info *pci = &pdi->cpus[cpu]; | |
484 | ||
485 | for (sip = pci->skips_head; sip != NULL; sip = sip->next) { | |
486 | pdi->skips++; | |
487 | pdi->seq_skips += (sip->end - sip->start + 1); | |
488 | if (verbose) | |
489 | fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n", | |
490 | MAJOR(pdi->dev), MINOR(pdi->dev), | |
491 | sip->start, sip->end); | |
492 | } | |
492da111 AB |
493 | } |
494 | } | |
495 | ||
824c2b39 JA |
496 | static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu) |
497 | { | |
498 | if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) { | |
499 | int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1); | |
500 | unsigned long *map = malloc(new_max / sizeof(long)); | |
501 | ||
502 | memset(map, 0, new_max / sizeof(long)); | |
503 | ||
504 | if (pdi->cpu_map) { | |
505 | memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long)); | |
506 | free(pdi->cpu_map); | |
507 | } | |
508 | ||
509 | pdi->cpu_map = map; | |
510 | pdi->cpu_map_max = new_max; | |
511 | } | |
512 | ||
513 | pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu)); | |
514 | } | |
515 | ||
516 | static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu) | |
517 | { | |
518 | pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu)); | |
519 | } | |
520 | ||
521 | static inline int cpu_is_online(struct per_dev_info *pdi, int cpu) | |
522 | { | |
523 | return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0; | |
524 | } | |
525 | ||
bfc70ad5 JA |
526 | static inline int ppm_hash_pid(pid_t pid) |
527 | { | |
528 | return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK; | |
529 | } | |
530 | ||
531 | static struct process_pid_map *find_ppm(pid_t pid) | |
532 | { | |
533 | const int hash_idx = ppm_hash_pid(pid); | |
534 | struct process_pid_map *ppm; | |
535 | ||
536 | ppm = ppm_hash_table[hash_idx]; | |
537 | while (ppm) { | |
538 | if (ppm->pid == pid) | |
539 | return ppm; | |
540 | ||
541 | ppm = ppm->hash_next; | |
542 | } | |
543 | ||
544 | return NULL; | |
545 | } | |
546 | ||
ebe2d1aa | 547 | static struct process_pid_map *add_ppm_hash(pid_t pid, const char *name) |
bfc70ad5 JA |
548 | { |
549 | const int hash_idx = ppm_hash_pid(pid); | |
550 | struct process_pid_map *ppm; | |
551 | ||
552 | ppm = find_ppm(pid); | |
248eac8f JA |
553 | if (!ppm) { |
554 | ppm = malloc(sizeof(*ppm)); | |
555 | memset(ppm, 0, sizeof(*ppm)); | |
556 | ppm->pid = pid; | |
557 | strcpy(ppm->comm, name); | |
558 | ppm->hash_next = ppm_hash_table[hash_idx]; | |
559 | ppm_hash_table[hash_idx] = ppm; | |
bfc70ad5 | 560 | } |
ebe2d1aa JA |
561 | |
562 | return ppm; | |
bfc70ad5 JA |
563 | } |
564 | ||
7bd4fd0a OK |
565 | static void handle_notify(struct blk_io_trace *bit) |
566 | { | |
567 | void *payload = (caddr_t) bit + sizeof(*bit); | |
568 | __u32 two32[2]; | |
569 | ||
570 | switch (bit->action) { | |
571 | case BLK_TN_PROCESS: | |
572 | add_ppm_hash(bit->pid, payload); | |
573 | break; | |
574 | ||
575 | case BLK_TN_TIMESTAMP: | |
576 | if (bit->pdu_len != sizeof(two32)) | |
577 | return; | |
578 | memcpy(two32, payload, sizeof(two32)); | |
579 | if (!data_is_native) { | |
580 | two32[0] = be32_to_cpu(two32[0]); | |
581 | two32[1] = be32_to_cpu(two32[1]); | |
582 | } | |
583 | start_timestamp = bit->time; | |
584 | abs_start_time.tv_sec = two32[0]; | |
585 | abs_start_time.tv_nsec = two32[1]; | |
586 | if (abs_start_time.tv_nsec < 0) { | |
587 | abs_start_time.tv_sec--; | |
588 | abs_start_time.tv_nsec += 1000000000; | |
589 | } | |
590 | ||
591 | break; | |
592 | ||
1a15f6a8 AB |
593 | case BLK_TN_MESSAGE: |
594 | if (bit->pdu_len > 0) { | |
595 | char msg[bit->pdu_len+1]; | |
596 | ||
597 | memcpy(msg, (char *)payload, bit->pdu_len); | |
598 | msg[bit->pdu_len] = '\0'; | |
599 | ||
600 | fprintf(ofp, | |
601 | "%3d,%-3d %2d %8s %5d.%09lu %5u %2s %3s %s\n", | |
602 | MAJOR(bit->device), MINOR(bit->device), | |
603 | bit->cpu, "0", (int) SECONDS(bit->time), | |
604 | (unsigned long) NANO_SECONDS(bit->time), | |
605 | 0, "m", "N", msg); | |
606 | } | |
607 | break; | |
608 | ||
7bd4fd0a OK |
609 | default: |
610 | /* Ignore unknown notify events */ | |
611 | ; | |
612 | } | |
613 | } | |
614 | ||
bfc70ad5 JA |
615 | char *find_process_name(pid_t pid) |
616 | { | |
617 | struct process_pid_map *ppm = find_ppm(pid); | |
618 | ||
619 | if (ppm) | |
620 | return ppm->comm; | |
621 | ||
622 | return NULL; | |
623 | } | |
624 | ||
9e4cd1b8 | 625 | static inline int ppi_hash_pid(pid_t pid) |
bf0720af JA |
626 | { |
627 | return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK; | |
628 | } | |
629 | ||
630 | static inline int ppi_hash_name(const char *name) | |
152f6476 | 631 | { |
bf0720af JA |
632 | return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK; |
633 | } | |
634 | ||
635 | static inline int ppi_hash(struct per_process_info *ppi) | |
636 | { | |
2990e589 JA |
637 | struct process_pid_map *ppm = ppi->ppm; |
638 | ||
bf0720af | 639 | if (ppi_hash_by_pid) |
2990e589 | 640 | return ppi_hash_pid(ppm->pid); |
bf0720af | 641 | |
2990e589 | 642 | return ppi_hash_name(ppm->comm); |
152f6476 JA |
643 | } |
644 | ||
bfc70ad5 | 645 | static inline void add_ppi_to_hash(struct per_process_info *ppi) |
152f6476 | 646 | { |
bf0720af | 647 | const int hash_idx = ppi_hash(ppi); |
152f6476 | 648 | |
bf0720af JA |
649 | ppi->hash_next = ppi_hash_table[hash_idx]; |
650 | ppi_hash_table[hash_idx] = ppi; | |
152f6476 JA |
651 | } |
652 | ||
bfc70ad5 | 653 | static inline void add_ppi_to_list(struct per_process_info *ppi) |
152f6476 JA |
654 | { |
655 | ppi->list_next = ppi_list; | |
656 | ppi_list = ppi; | |
886ecf0e | 657 | ppi_list_entries++; |
152f6476 JA |
658 | } |
659 | ||
bfc70ad5 | 660 | static struct per_process_info *find_ppi_by_name(char *name) |
bf0720af JA |
661 | { |
662 | const int hash_idx = ppi_hash_name(name); | |
663 | struct per_process_info *ppi; | |
664 | ||
665 | ppi = ppi_hash_table[hash_idx]; | |
666 | while (ppi) { | |
2990e589 JA |
667 | struct process_pid_map *ppm = ppi->ppm; |
668 | ||
669 | if (!strcmp(ppm->comm, name)) | |
bf0720af JA |
670 | return ppi; |
671 | ||
672 | ppi = ppi->hash_next; | |
673 | } | |
674 | ||
675 | return NULL; | |
676 | } | |
677 | ||
9e4cd1b8 | 678 | static struct per_process_info *find_ppi_by_pid(pid_t pid) |
152f6476 | 679 | { |
bf0720af | 680 | const int hash_idx = ppi_hash_pid(pid); |
152f6476 JA |
681 | struct per_process_info *ppi; |
682 | ||
bf0720af | 683 | ppi = ppi_hash_table[hash_idx]; |
152f6476 | 684 | while (ppi) { |
2990e589 JA |
685 | struct process_pid_map *ppm = ppi->ppm; |
686 | ||
687 | if (ppm->pid == pid) | |
152f6476 JA |
688 | return ppi; |
689 | ||
690 | ppi = ppi->hash_next; | |
691 | } | |
692 | ||
693 | return NULL; | |
694 | } | |
695 | ||
9e4cd1b8 | 696 | static struct per_process_info *find_ppi(pid_t pid) |
bf0720af | 697 | { |
715d8021 | 698 | struct per_process_info *ppi; |
bfc70ad5 | 699 | char *name; |
715d8021 | 700 | |
bf0720af | 701 | if (ppi_hash_by_pid) |
bfc70ad5 JA |
702 | return find_ppi_by_pid(pid); |
703 | ||
704 | name = find_process_name(pid); | |
705 | if (!name) | |
706 | return NULL; | |
bf0720af | 707 | |
bfc70ad5 | 708 | ppi = find_ppi_by_name(name); |
2990e589 | 709 | if (ppi && ppi->ppm->pid != pid) |
715d8021 JA |
710 | ppi->more_than_one = 1; |
711 | ||
712 | return ppi; | |
bf0720af JA |
713 | } |
714 | ||
210824c3 JA |
715 | /* |
716 | * struct trace and blktrace allocation cache, we do potentially | |
717 | * millions of mallocs for these structures while only using at most | |
718 | * a few thousand at the time | |
719 | */ | |
720 | static inline void t_free(struct trace *t) | |
721 | { | |
722 | if (t_alloc_cache < 1024) { | |
723 | t->next = t_alloc_list; | |
724 | t_alloc_list = t; | |
725 | t_alloc_cache++; | |
726 | } else | |
727 | free(t); | |
728 | } | |
729 | ||
730 | static inline struct trace *t_alloc(void) | |
731 | { | |
732 | struct trace *t = t_alloc_list; | |
733 | ||
734 | if (t) { | |
735 | t_alloc_list = t->next; | |
736 | t_alloc_cache--; | |
737 | return t; | |
738 | } | |
739 | ||
740 | return malloc(sizeof(*t)); | |
741 | } | |
742 | ||
743 | static inline void bit_free(struct blk_io_trace *bit) | |
744 | { | |
745 | if (bit_alloc_cache < 1024 && !bit->pdu_len) { | |
746 | /* | |
747 | * abuse a 64-bit field for a next pointer for the free item | |
748 | */ | |
749 | bit->time = (__u64) (unsigned long) bit_alloc_list; | |
750 | bit_alloc_list = (struct blk_io_trace *) bit; | |
751 | bit_alloc_cache++; | |
752 | } else | |
753 | free(bit); | |
754 | } | |
755 | ||
756 | static inline struct blk_io_trace *bit_alloc(void) | |
757 | { | |
758 | struct blk_io_trace *bit = bit_alloc_list; | |
759 | ||
760 | if (bit) { | |
761 | bit_alloc_list = (struct blk_io_trace *) (unsigned long) \ | |
762 | bit->time; | |
763 | bit_alloc_cache--; | |
764 | return bit; | |
765 | } | |
766 | ||
767 | return malloc(sizeof(*bit)); | |
768 | } | |
769 | ||
770 | static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t) | |
771 | { | |
772 | struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu); | |
773 | ||
774 | rb_erase(&t->rb_node, &pci->rb_last); | |
775 | pci->rb_last_entries--; | |
776 | ||
777 | bit_free(t->bit); | |
778 | t_free(t); | |
779 | } | |
780 | ||
781 | static void put_trace(struct per_dev_info *pdi, struct trace *t) | |
782 | { | |
783 | rb_erase(&t->rb_node, &rb_sort_root); | |
784 | rb_sort_entries--; | |
785 | ||
786 | trace_rb_insert_last(pdi, t); | |
787 | } | |
788 | ||
89482da6 | 789 | static inline int trace_rb_insert(struct trace *t, struct rb_root *root) |
7997c5b0 | 790 | { |
2a1b3424 | 791 | struct rb_node **p = &root->rb_node; |
7997c5b0 JA |
792 | struct rb_node *parent = NULL; |
793 | struct trace *__t; | |
794 | ||
795 | while (*p) { | |
796 | parent = *p; | |
2a1b3424 | 797 | |
7997c5b0 JA |
798 | __t = rb_entry(parent, struct trace, rb_node); |
799 | ||
89482da6 JA |
800 | if (t->bit->time < __t->bit->time) |
801 | p = &(*p)->rb_left; | |
802 | else if (t->bit->time > __t->bit->time) | |
803 | p = &(*p)->rb_right; | |
804 | else if (t->bit->device < __t->bit->device) | |
e7c9f3ff NS |
805 | p = &(*p)->rb_left; |
806 | else if (t->bit->device > __t->bit->device) | |
807 | p = &(*p)->rb_right; | |
dcf0f7ed JA |
808 | else if (t->bit->sequence < __t->bit->sequence) |
809 | p = &(*p)->rb_left; | |
0b07f23e | 810 | else /* >= sequence */ |
dcf0f7ed | 811 | p = &(*p)->rb_right; |
7997c5b0 JA |
812 | } |
813 | ||
814 | rb_link_node(&t->rb_node, parent, p); | |
2a1b3424 | 815 | rb_insert_color(&t->rb_node, root); |
7997c5b0 JA |
816 | return 0; |
817 | } | |
818 | ||
2a1b3424 | 819 | static inline int trace_rb_insert_sort(struct trace *t) |
e3556946 | 820 | { |
89482da6 | 821 | if (!trace_rb_insert(t, &rb_sort_root)) { |
2a1b3424 JA |
822 | rb_sort_entries++; |
823 | return 0; | |
824 | } | |
825 | ||
826 | return 1; | |
827 | } | |
828 | ||
210824c3 | 829 | static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t) |
2a1b3424 | 830 | { |
210824c3 JA |
831 | struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu); |
832 | ||
833 | if (trace_rb_insert(t, &pci->rb_last)) | |
834 | return 1; | |
835 | ||
836 | pci->rb_last_entries++; | |
837 | ||
838 | if (pci->rb_last_entries > rb_batch * pdi->nfiles) { | |
839 | struct rb_node *n = rb_first(&pci->rb_last); | |
840 | ||
841 | t = rb_entry(n, struct trace, rb_node); | |
842 | __put_trace_last(pdi, t); | |
2a1b3424 JA |
843 | } |
844 | ||
210824c3 | 845 | return 0; |
2a1b3424 JA |
846 | } |
847 | ||
848 | static struct trace *trace_rb_find(dev_t device, unsigned long sequence, | |
849 | struct rb_root *root, int order) | |
850 | { | |
851 | struct rb_node *n = root->rb_node; | |
852 | struct rb_node *prev = NULL; | |
e3556946 JA |
853 | struct trace *__t; |
854 | ||
2a1b3424 JA |
855 | while (n) { |
856 | __t = rb_entry(n, struct trace, rb_node); | |
857 | prev = n; | |
e3556946 | 858 | |
0583b6a2 | 859 | if (device < __t->bit->device) |
2a1b3424 | 860 | n = n->rb_left; |
0583b6a2 | 861 | else if (device > __t->bit->device) |
2a1b3424 | 862 | n = n->rb_right; |
0583b6a2 | 863 | else if (sequence < __t->bit->sequence) |
2a1b3424 | 864 | n = n->rb_left; |
e3556946 | 865 | else if (sequence > __t->bit->sequence) |
2a1b3424 | 866 | n = n->rb_right; |
e3556946 JA |
867 | else |
868 | return __t; | |
869 | } | |
870 | ||
2a1b3424 JA |
871 | /* |
872 | * hack - the list may not be sequence ordered because some | |
873 | * events don't have sequence and time matched. so we end up | |
874 | * being a little off in the rb lookup here, because we don't | |
875 | * know the time we are looking for. compensate by browsing | |
876 | * a little ahead from the last entry to find the match | |
877 | */ | |
878 | if (order && prev) { | |
879 | int max = 5; | |
880 | ||
881 | while (((n = rb_next(prev)) != NULL) && max--) { | |
882 | __t = rb_entry(n, struct trace, rb_node); | |
492da111 | 883 | |
2a1b3424 JA |
884 | if (__t->bit->device == device && |
885 | __t->bit->sequence == sequence) | |
886 | return __t; | |
887 | ||
888 | prev = n; | |
889 | } | |
890 | } | |
492da111 | 891 | |
e3556946 JA |
892 | return NULL; |
893 | } | |
894 | ||
2a1b3424 | 895 | static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi, |
210824c3 | 896 | struct per_cpu_info *pci, |
2a1b3424 JA |
897 | unsigned long seq) |
898 | { | |
210824c3 | 899 | return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0); |
2a1b3424 JA |
900 | } |
901 | ||
f7bd1a9b | 902 | static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot) |
7997c5b0 | 903 | { |
f7bd1a9b | 904 | struct rb_node **p = &pdi->rb_track.rb_node; |
7997c5b0 JA |
905 | struct rb_node *parent = NULL; |
906 | struct io_track *__iot; | |
907 | ||
908 | while (*p) { | |
909 | parent = *p; | |
7997c5b0 JA |
910 | __iot = rb_entry(parent, struct io_track, rb_node); |
911 | ||
f7bd1a9b | 912 | if (iot->sector < __iot->sector) |
7997c5b0 JA |
913 | p = &(*p)->rb_left; |
914 | else if (iot->sector > __iot->sector) | |
915 | p = &(*p)->rb_right; | |
916 | else { | |
e7c9f3ff | 917 | fprintf(stderr, |
ab197ca7 AB |
918 | "sector alias (%Lu) on device %d,%d!\n", |
919 | (unsigned long long) iot->sector, | |
f7bd1a9b | 920 | MAJOR(pdi->dev), MINOR(pdi->dev)); |
7997c5b0 JA |
921 | return 1; |
922 | } | |
923 | } | |
924 | ||
925 | rb_link_node(&iot->rb_node, parent, p); | |
f7bd1a9b | 926 | rb_insert_color(&iot->rb_node, &pdi->rb_track); |
7997c5b0 JA |
927 | return 0; |
928 | } | |
929 | ||
f7bd1a9b | 930 | static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector) |
7997c5b0 | 931 | { |
f7bd1a9b | 932 | struct rb_node *n = pdi->rb_track.rb_node; |
7997c5b0 JA |
933 | struct io_track *__iot; |
934 | ||
2a1b3424 JA |
935 | while (n) { |
936 | __iot = rb_entry(n, struct io_track, rb_node); | |
7997c5b0 | 937 | |
f7bd1a9b | 938 | if (sector < __iot->sector) |
2a1b3424 | 939 | n = n->rb_left; |
7997c5b0 | 940 | else if (sector > __iot->sector) |
2a1b3424 | 941 | n = n->rb_right; |
7997c5b0 JA |
942 | else |
943 | return __iot; | |
944 | } | |
945 | ||
946 | return NULL; | |
947 | } | |
948 | ||
9e4cd1b8 | 949 | static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid, |
bfc70ad5 | 950 | __u64 sector) |
7997c5b0 | 951 | { |
916b5501 | 952 | struct io_track *iot; |
7997c5b0 | 953 | |
f7bd1a9b | 954 | iot = __find_track(pdi, sector); |
7997c5b0 JA |
955 | if (!iot) { |
956 | iot = malloc(sizeof(*iot)); | |
2990e589 | 957 | iot->ppm = find_ppm(pid); |
ebe2d1aa JA |
958 | if (!iot->ppm) |
959 | iot->ppm = add_ppm_hash(pid, "unknown"); | |
7997c5b0 | 960 | iot->sector = sector; |
f7bd1a9b | 961 | track_rb_insert(pdi, iot); |
7997c5b0 JA |
962 | } |
963 | ||
964 | return iot; | |
965 | } | |
966 | ||
f7bd1a9b JA |
967 | static void log_track_frontmerge(struct per_dev_info *pdi, |
968 | struct blk_io_trace *t) | |
2e3e8ded JA |
969 | { |
970 | struct io_track *iot; | |
971 | ||
972 | if (!track_ios) | |
973 | return; | |
2e3e8ded | 974 | |
ae957cbc | 975 | iot = __find_track(pdi, t->sector + t_sec(t)); |
cb2a1a62 | 976 | if (!iot) { |
57ea8602 JA |
977 | if (verbose) |
978 | fprintf(stderr, "merge not found for (%d,%d): %llu\n", | |
979 | MAJOR(pdi->dev), MINOR(pdi->dev), | |
980 | (unsigned long long) t->sector + t_sec(t)); | |
cb2a1a62 | 981 | return; |
2e3e8ded | 982 | } |
cb2a1a62 | 983 | |
f7bd1a9b | 984 | rb_erase(&iot->rb_node, &pdi->rb_track); |
ae957cbc | 985 | iot->sector -= t_sec(t); |
f7bd1a9b | 986 | track_rb_insert(pdi, iot); |
2e3e8ded JA |
987 | } |
988 | ||
f7bd1a9b | 989 | static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t) |
2e3e8ded JA |
990 | { |
991 | struct io_track *iot; | |
992 | ||
993 | if (!track_ios) | |
994 | return; | |
995 | ||
bfc70ad5 | 996 | iot = find_track(pdi, t->pid, t->sector); |
95c15013 JA |
997 | iot->allocation_time = t->time; |
998 | } | |
999 | ||
753f9091 JA |
1000 | static inline int is_remapper(struct per_dev_info *pdi) |
1001 | { | |
1002 | int major = MAJOR(pdi->dev); | |
1003 | ||
1004 | return (major == 253 || major == 9); | |
1005 | } | |
1006 | ||
1007 | /* | |
1008 | * for md/dm setups, the interesting cycle is Q -> C. So track queueing | |
1009 | * time here, as dispatch time | |
1010 | */ | |
1011 | static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t) | |
1012 | { | |
1013 | struct io_track *iot; | |
1014 | ||
1015 | if (!track_ios) | |
1016 | return; | |
1017 | if (!is_remapper(pdi)) | |
1018 | return; | |
1019 | ||
bfc70ad5 | 1020 | iot = find_track(pdi, t->pid, t->sector); |
753f9091 JA |
1021 | iot->dispatch_time = t->time; |
1022 | } | |
1023 | ||
95c15013 | 1024 | /* |
b6076a9b | 1025 | * return time between rq allocation and insertion |
95c15013 | 1026 | */ |
f7bd1a9b JA |
1027 | static unsigned long long log_track_insert(struct per_dev_info *pdi, |
1028 | struct blk_io_trace *t) | |
95c15013 | 1029 | { |
50adc0ba | 1030 | unsigned long long elapsed; |
95c15013 JA |
1031 | struct io_track *iot; |
1032 | ||
1033 | if (!track_ios) | |
1034 | return -1; | |
1035 | ||
bfc70ad5 | 1036 | iot = find_track(pdi, t->pid, t->sector); |
2e3e8ded | 1037 | iot->queue_time = t->time; |
acd70d21 JA |
1038 | |
1039 | if (!iot->allocation_time) | |
1040 | return -1; | |
1041 | ||
50adc0ba JA |
1042 | elapsed = iot->queue_time - iot->allocation_time; |
1043 | ||
1044 | if (per_process_stats) { | |
2990e589 | 1045 | struct per_process_info *ppi = find_ppi(iot->ppm->pid); |
b9d40d6f | 1046 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
50adc0ba | 1047 | |
b9d40d6f JA |
1048 | if (ppi && elapsed > ppi->longest_allocation_wait[w]) |
1049 | ppi->longest_allocation_wait[w] = elapsed; | |
50adc0ba JA |
1050 | } |
1051 | ||
1052 | return elapsed; | |
2e3e8ded JA |
1053 | } |
1054 | ||
1055 | /* | |
1056 | * return time between queue and issue | |
1057 | */ | |
f7bd1a9b JA |
1058 | static unsigned long long log_track_issue(struct per_dev_info *pdi, |
1059 | struct blk_io_trace *t) | |
2e3e8ded | 1060 | { |
50adc0ba | 1061 | unsigned long long elapsed; |
2e3e8ded JA |
1062 | struct io_track *iot; |
1063 | ||
1064 | if (!track_ios) | |
1065 | return -1; | |
1066 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) | |
1067 | return -1; | |
1068 | ||
f7bd1a9b | 1069 | iot = __find_track(pdi, t->sector); |
cb2a1a62 | 1070 | if (!iot) { |
57ea8602 JA |
1071 | if (verbose) |
1072 | fprintf(stderr, "issue not found for (%d,%d): %llu\n", | |
1073 | MAJOR(pdi->dev), MINOR(pdi->dev), | |
1074 | (unsigned long long) t->sector); | |
2e3e8ded | 1075 | return -1; |
cb2a1a62 | 1076 | } |
2e3e8ded JA |
1077 | |
1078 | iot->dispatch_time = t->time; | |
50adc0ba JA |
1079 | elapsed = iot->dispatch_time - iot->queue_time; |
1080 | ||
1081 | if (per_process_stats) { | |
2990e589 | 1082 | struct per_process_info *ppi = find_ppi(iot->ppm->pid); |
b9d40d6f | 1083 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
50adc0ba | 1084 | |
b9d40d6f JA |
1085 | if (ppi && elapsed > ppi->longest_dispatch_wait[w]) |
1086 | ppi->longest_dispatch_wait[w] = elapsed; | |
50adc0ba JA |
1087 | } |
1088 | ||
1089 | return elapsed; | |
2e3e8ded JA |
1090 | } |
1091 | ||
1092 | /* | |
1093 | * return time between dispatch and complete | |
1094 | */ | |
f7bd1a9b JA |
1095 | static unsigned long long log_track_complete(struct per_dev_info *pdi, |
1096 | struct blk_io_trace *t) | |
2e3e8ded JA |
1097 | { |
1098 | unsigned long long elapsed; | |
1099 | struct io_track *iot; | |
1100 | ||
1101 | if (!track_ios) | |
1102 | return -1; | |
2e3e8ded | 1103 | |
f7bd1a9b | 1104 | iot = __find_track(pdi, t->sector); |
cb2a1a62 | 1105 | if (!iot) { |
57ea8602 JA |
1106 | if (verbose) |
1107 | fprintf(stderr,"complete not found for (%d,%d): %llu\n", | |
1108 | MAJOR(pdi->dev), MINOR(pdi->dev), | |
1109 | (unsigned long long) t->sector); | |
2e3e8ded | 1110 | return -1; |
cb2a1a62 | 1111 | } |
2e3e8ded JA |
1112 | |
1113 | iot->completion_time = t->time; | |
1114 | elapsed = iot->completion_time - iot->dispatch_time; | |
1115 | ||
50adc0ba | 1116 | if (per_process_stats) { |
2990e589 | 1117 | struct per_process_info *ppi = find_ppi(iot->ppm->pid); |
b9d40d6f | 1118 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
50adc0ba | 1119 | |
b9d40d6f JA |
1120 | if (ppi && elapsed > ppi->longest_completion_wait[w]) |
1121 | ppi->longest_completion_wait[w] = elapsed; | |
50adc0ba JA |
1122 | } |
1123 | ||
2e3e8ded JA |
1124 | /* |
1125 | * kill the trace, we don't need it after completion | |
1126 | */ | |
f7bd1a9b | 1127 | rb_erase(&iot->rb_node, &pdi->rb_track); |
2e3e8ded JA |
1128 | free(iot); |
1129 | ||
1130 | return elapsed; | |
1131 | } | |
1132 | ||
1133 | ||
9e4cd1b8 | 1134 | static struct io_stats *find_process_io_stats(pid_t pid) |
152f6476 | 1135 | { |
bfc70ad5 | 1136 | struct per_process_info *ppi = find_ppi(pid); |
152f6476 JA |
1137 | |
1138 | if (!ppi) { | |
1139 | ppi = malloc(sizeof(*ppi)); | |
1140 | memset(ppi, 0, sizeof(*ppi)); | |
2990e589 | 1141 | ppi->ppm = find_ppm(pid); |
ebe2d1aa JA |
1142 | if (!ppi->ppm) |
1143 | ppi->ppm = add_ppm_hash(pid, "unknown"); | |
bfc70ad5 JA |
1144 | add_ppi_to_hash(ppi); |
1145 | add_ppi_to_list(ppi); | |
152f6476 JA |
1146 | } |
1147 | ||
1148 | return &ppi->io_stats; | |
1149 | } | |
1150 | ||
e7c9f3ff NS |
1151 | static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size) |
1152 | { | |
1153 | if (pdi->name) | |
1154 | snprintf(buffer, size, "%s", pdi->name); | |
1155 | else | |
f7bd1a9b | 1156 | snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev)); |
e7c9f3ff NS |
1157 | return buffer; |
1158 | } | |
1159 | ||
e7c9f3ff | 1160 | static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit) |
cfab07eb AB |
1161 | { |
1162 | unsigned long long this = bit->time; | |
e7c9f3ff | 1163 | unsigned long long last = pdi->last_reported_time; |
cfab07eb | 1164 | |
e7c9f3ff NS |
1165 | pdi->backwards = (this < last) ? 'B' : ' '; |
1166 | pdi->last_reported_time = this; | |
cfab07eb AB |
1167 | } |
1168 | ||
fb2ec796 JA |
1169 | static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t, |
1170 | int rw) | |
d0ca268b | 1171 | { |
fb2ec796 | 1172 | if (rw) { |
152f6476 | 1173 | ios->mwrites++; |
fb2ec796 JA |
1174 | ios->mwrite_kb += t_kb(t); |
1175 | } else { | |
152f6476 | 1176 | ios->mreads++; |
fb2ec796 JA |
1177 | ios->mread_kb += t_kb(t); |
1178 | } | |
152f6476 JA |
1179 | } |
1180 | ||
1181 | static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci, | |
1182 | int rw) | |
1183 | { | |
fb2ec796 | 1184 | __account_m(&pci->io_stats, t, rw); |
152f6476 JA |
1185 | |
1186 | if (per_process_stats) { | |
bfc70ad5 | 1187 | struct io_stats *ios = find_process_io_stats(t->pid); |
152f6476 | 1188 | |
fb2ec796 | 1189 | __account_m(ios, t, rw); |
d0ca268b JA |
1190 | } |
1191 | } | |
1192 | ||
801646d6 CS |
1193 | static inline void __account_pc_queue(struct io_stats *ios, |
1194 | struct blk_io_trace *t, int rw) | |
1195 | { | |
1196 | if (rw) { | |
1197 | ios->qwrites_pc++; | |
1198 | ios->qwrite_kb_pc += t_kb(t); | |
1199 | } else { | |
1200 | ios->qreads_pc++; | |
1201 | ios->qread_kb += t_kb(t); | |
1202 | } | |
1203 | } | |
1204 | ||
1205 | static inline void account_pc_queue(struct blk_io_trace *t, | |
1206 | struct per_cpu_info *pci, int rw) | |
1207 | { | |
1208 | __account_pc_queue(&pci->io_stats, t, rw); | |
1209 | ||
1210 | if (per_process_stats) { | |
1211 | struct io_stats *ios = find_process_io_stats(t->pid); | |
1212 | ||
1213 | __account_pc_queue(ios, t, rw); | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | static inline void __account_pc_issue(struct io_stats *ios, int rw, | |
1218 | unsigned int bytes) | |
1219 | { | |
1220 | if (rw) { | |
1221 | ios->iwrites_pc++; | |
1222 | ios->iwrite_kb_pc += bytes >> 10; | |
1223 | } else { | |
1224 | ios->ireads_pc++; | |
1225 | ios->iread_kb_pc += bytes >> 10; | |
1226 | } | |
1227 | } | |
1228 | ||
1229 | static inline void account_pc_issue(struct blk_io_trace *t, | |
1230 | struct per_cpu_info *pci, int rw) | |
1231 | { | |
1232 | __account_pc_issue(&pci->io_stats, rw, t->bytes); | |
1233 | ||
1234 | if (per_process_stats) { | |
1235 | struct io_stats *ios = find_process_io_stats(t->pid); | |
1236 | ||
1237 | __account_pc_issue(ios, rw, t->bytes); | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | static inline void __account_pc_requeue(struct io_stats *ios, | |
1242 | struct blk_io_trace *t, int rw) | |
1243 | { | |
1244 | if (rw) { | |
1245 | ios->wrqueue_pc++; | |
1246 | ios->iwrite_kb_pc -= t_kb(t); | |
1247 | } else { | |
1248 | ios->rrqueue_pc++; | |
1249 | ios->iread_kb_pc -= t_kb(t); | |
1250 | } | |
1251 | } | |
1252 | ||
1253 | static inline void account_pc_requeue(struct blk_io_trace *t, | |
1254 | struct per_cpu_info *pci, int rw) | |
1255 | { | |
1256 | __account_pc_requeue(&pci->io_stats, t, rw); | |
1257 | ||
1258 | if (per_process_stats) { | |
1259 | struct io_stats *ios = find_process_io_stats(t->pid); | |
1260 | ||
1261 | __account_pc_requeue(ios, t, rw); | |
1262 | } | |
1263 | } | |
1264 | ||
1265 | static inline void __account_pc_c(struct io_stats *ios, int rw) | |
1266 | { | |
1267 | if (rw) | |
1268 | ios->cwrites_pc++; | |
1269 | else | |
1270 | ios->creads_pc++; | |
1271 | } | |
1272 | ||
1273 | static inline void account_pc_c(struct blk_io_trace *t, | |
1274 | struct per_cpu_info *pci, int rw) | |
1275 | { | |
1276 | __account_pc_c(&pci->io_stats, rw); | |
1277 | ||
1278 | if (per_process_stats) { | |
1279 | struct io_stats *ios = find_process_io_stats(t->pid); | |
1280 | ||
1281 | __account_pc_c(ios, rw); | |
1282 | } | |
1283 | } | |
1284 | ||
b6076a9b JA |
1285 | static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t, |
1286 | int rw) | |
d0ca268b JA |
1287 | { |
1288 | if (rw) { | |
152f6476 | 1289 | ios->qwrites++; |
ae957cbc | 1290 | ios->qwrite_kb += t_kb(t); |
d0ca268b | 1291 | } else { |
152f6476 | 1292 | ios->qreads++; |
ae957cbc | 1293 | ios->qread_kb += t_kb(t); |
152f6476 JA |
1294 | } |
1295 | } | |
1296 | ||
b6076a9b JA |
1297 | static inline void account_queue(struct blk_io_trace *t, |
1298 | struct per_cpu_info *pci, int rw) | |
152f6476 | 1299 | { |
b6076a9b | 1300 | __account_queue(&pci->io_stats, t, rw); |
152f6476 JA |
1301 | |
1302 | if (per_process_stats) { | |
bfc70ad5 | 1303 | struct io_stats *ios = find_process_io_stats(t->pid); |
152f6476 | 1304 | |
b6076a9b | 1305 | __account_queue(ios, t, rw); |
d0ca268b JA |
1306 | } |
1307 | } | |
1308 | ||
e21dc4dd | 1309 | static inline void __account_c(struct io_stats *ios, int rw, int bytes) |
d0ca268b JA |
1310 | { |
1311 | if (rw) { | |
152f6476 JA |
1312 | ios->cwrites++; |
1313 | ios->cwrite_kb += bytes >> 10; | |
d0ca268b | 1314 | } else { |
152f6476 JA |
1315 | ios->creads++; |
1316 | ios->cread_kb += bytes >> 10; | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci, | |
1321 | int rw, int bytes) | |
1322 | { | |
1323 | __account_c(&pci->io_stats, rw, bytes); | |
1324 | ||
1325 | if (per_process_stats) { | |
bfc70ad5 | 1326 | struct io_stats *ios = find_process_io_stats(t->pid); |
152f6476 JA |
1327 | |
1328 | __account_c(ios, rw, bytes); | |
d0ca268b JA |
1329 | } |
1330 | } | |
1331 | ||
b6076a9b JA |
1332 | static inline void __account_issue(struct io_stats *ios, int rw, |
1333 | unsigned int bytes) | |
afd2d7ad | 1334 | { |
1335 | if (rw) { | |
152f6476 JA |
1336 | ios->iwrites++; |
1337 | ios->iwrite_kb += bytes >> 10; | |
afd2d7ad | 1338 | } else { |
152f6476 JA |
1339 | ios->ireads++; |
1340 | ios->iread_kb += bytes >> 10; | |
afd2d7ad | 1341 | } |
1342 | } | |
1343 | ||
b6076a9b JA |
1344 | static inline void account_issue(struct blk_io_trace *t, |
1345 | struct per_cpu_info *pci, int rw) | |
d0ca268b | 1346 | { |
b6076a9b | 1347 | __account_issue(&pci->io_stats, rw, t->bytes); |
152f6476 JA |
1348 | |
1349 | if (per_process_stats) { | |
bfc70ad5 | 1350 | struct io_stats *ios = find_process_io_stats(t->pid); |
d5396421 | 1351 | |
b6076a9b | 1352 | __account_issue(ios, rw, t->bytes); |
152f6476 JA |
1353 | } |
1354 | } | |
1355 | ||
06639b27 JA |
1356 | static inline void __account_unplug(struct io_stats *ios, int timer) |
1357 | { | |
1358 | if (timer) | |
1359 | ios->timer_unplugs++; | |
1360 | else | |
1361 | ios->io_unplugs++; | |
1362 | } | |
1363 | ||
1364 | static inline void account_unplug(struct blk_io_trace *t, | |
1365 | struct per_cpu_info *pci, int timer) | |
1366 | { | |
1367 | __account_unplug(&pci->io_stats, timer); | |
1368 | ||
1369 | if (per_process_stats) { | |
bfc70ad5 | 1370 | struct io_stats *ios = find_process_io_stats(t->pid); |
06639b27 JA |
1371 | |
1372 | __account_unplug(ios, timer); | |
1373 | } | |
1374 | } | |
1375 | ||
4054070a JA |
1376 | static inline void __account_requeue(struct io_stats *ios, |
1377 | struct blk_io_trace *t, int rw) | |
1378 | { | |
1379 | if (rw) { | |
1380 | ios->wrqueue++; | |
1381 | ios->iwrite_kb -= t_kb(t); | |
1382 | } else { | |
1383 | ios->rrqueue++; | |
1384 | ios->iread_kb -= t_kb(t); | |
1385 | } | |
1386 | } | |
1387 | ||
1388 | static inline void account_requeue(struct blk_io_trace *t, | |
1389 | struct per_cpu_info *pci, int rw) | |
1390 | { | |
1391 | __account_requeue(&pci->io_stats, t, rw); | |
1392 | ||
1393 | if (per_process_stats) { | |
bfc70ad5 | 1394 | struct io_stats *ios = find_process_io_stats(t->pid); |
4054070a JA |
1395 | |
1396 | __account_requeue(ios, t, rw); | |
1397 | } | |
1398 | } | |
1399 | ||
f7bd1a9b JA |
1400 | static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci, |
1401 | struct blk_io_trace *t, char *act) | |
ab197ca7 | 1402 | { |
f7bd1a9b | 1403 | process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL); |
ab197ca7 AB |
1404 | } |
1405 | ||
f7bd1a9b JA |
1406 | static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci, |
1407 | struct blk_io_trace *t, char *act) | |
b6076a9b | 1408 | { |
f7bd1a9b | 1409 | process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL); |
b6076a9b JA |
1410 | } |
1411 | ||
ab197ca7 AB |
1412 | static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t, |
1413 | char *act) | |
1414 | { | |
b6076a9b | 1415 | process_fmt(act, pci, t, -1, 0, NULL); |
ab197ca7 | 1416 | } |
2e3e8ded | 1417 | |
f7bd1a9b JA |
1418 | static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci, |
1419 | struct blk_io_trace *t, char *act) | |
ab197ca7 | 1420 | { |
f7bd1a9b | 1421 | process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL); |
d0ca268b JA |
1422 | } |
1423 | ||
f7bd1a9b JA |
1424 | static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci, |
1425 | struct blk_io_trace *t, char *act) | |
d0ca268b | 1426 | { |
a01516de | 1427 | if (act[0] == 'F') |
f7bd1a9b | 1428 | log_track_frontmerge(pdi, t); |
2e3e8ded | 1429 | |
ab197ca7 | 1430 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
d0ca268b JA |
1431 | } |
1432 | ||
dfe34da1 | 1433 | static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 1434 | char *act) |
dfe34da1 | 1435 | { |
ab197ca7 | 1436 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
dfe34da1 JA |
1437 | } |
1438 | ||
d5396421 | 1439 | static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 1440 | char *act) |
d0ca268b | 1441 | { |
ab197ca7 | 1442 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
d0ca268b JA |
1443 | } |
1444 | ||
ab197ca7 | 1445 | static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 1446 | char *act) |
67e14fdc | 1447 | { |
ab197ca7 | 1448 | process_fmt(act, pci, t, -1ULL, 0, NULL); |
67e14fdc JA |
1449 | } |
1450 | ||
93f1c611 JA |
1451 | static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t, |
1452 | char *act) | |
1453 | { | |
1454 | process_fmt(act, pci, t, -1ULL, 0, NULL); | |
1455 | } | |
1456 | ||
ab197ca7 | 1457 | static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act) |
d0ca268b | 1458 | { |
ab197ca7 | 1459 | unsigned char *buf = (unsigned char *) t + sizeof(*t); |
d0ca268b | 1460 | |
ab197ca7 | 1461 | process_fmt(act, pci, t, -1ULL, t->pdu_len, buf); |
d0ca268b JA |
1462 | } |
1463 | ||
c82a8c9d CS |
1464 | static void dump_trace_pc(struct blk_io_trace *t, struct per_dev_info *pdi, |
1465 | struct per_cpu_info *pci) | |
d0ca268b | 1466 | { |
c82a8c9d | 1467 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
56f2af81 JA |
1468 | int act = t->action & 0xffff; |
1469 | ||
1470 | switch (act) { | |
d0ca268b | 1471 | case __BLK_TA_QUEUE: |
3639a11e | 1472 | log_generic(pci, t, "Q"); |
801646d6 | 1473 | account_pc_queue(t, pci, w); |
d0ca268b JA |
1474 | break; |
1475 | case __BLK_TA_GETRQ: | |
3639a11e | 1476 | log_generic(pci, t, "G"); |
d0ca268b JA |
1477 | break; |
1478 | case __BLK_TA_SLEEPRQ: | |
3639a11e | 1479 | log_generic(pci, t, "S"); |
d0ca268b JA |
1480 | break; |
1481 | case __BLK_TA_REQUEUE: | |
c82a8c9d CS |
1482 | /* |
1483 | * can happen if we miss traces, don't let it go | |
1484 | * below zero | |
1485 | */ | |
1486 | if (pdi->cur_depth[w]) | |
1487 | pdi->cur_depth[w]--; | |
801646d6 | 1488 | account_pc_requeue(t, pci, w); |
3639a11e | 1489 | log_generic(pci, t, "R"); |
d0ca268b JA |
1490 | break; |
1491 | case __BLK_TA_ISSUE: | |
801646d6 | 1492 | account_pc_issue(t, pci, w); |
c82a8c9d CS |
1493 | pdi->cur_depth[w]++; |
1494 | if (pdi->cur_depth[w] > pdi->max_depth[w]) | |
1495 | pdi->max_depth[w] = pdi->cur_depth[w]; | |
ab197ca7 | 1496 | log_pc(pci, t, "D"); |
d0ca268b JA |
1497 | break; |
1498 | case __BLK_TA_COMPLETE: | |
c82a8c9d CS |
1499 | if (pdi->cur_depth[w]) |
1500 | pdi->cur_depth[w]--; | |
3639a11e | 1501 | log_pc(pci, t, "C"); |
801646d6 | 1502 | account_pc_c(t, pci, w); |
d0ca268b | 1503 | break; |
56f2af81 JA |
1504 | case __BLK_TA_INSERT: |
1505 | log_pc(pci, t, "I"); | |
1506 | break; | |
d0ca268b | 1507 | default: |
56f2af81 | 1508 | fprintf(stderr, "Bad pc action %x\n", act); |
87b72777 | 1509 | break; |
d0ca268b | 1510 | } |
d0ca268b JA |
1511 | } |
1512 | ||
f7bd1a9b JA |
1513 | static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi, |
1514 | struct per_cpu_info *pci) | |
d0ca268b | 1515 | { |
649c7b66 | 1516 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
7997c5b0 | 1517 | int act = t->action & 0xffff; |
d0ca268b | 1518 | |
7997c5b0 | 1519 | switch (act) { |
d0ca268b | 1520 | case __BLK_TA_QUEUE: |
753f9091 | 1521 | log_track_queue(pdi, t); |
b6076a9b | 1522 | account_queue(t, pci, w); |
3639a11e | 1523 | log_queue(pci, t, "Q"); |
d0ca268b | 1524 | break; |
b6076a9b | 1525 | case __BLK_TA_INSERT: |
f7bd1a9b | 1526 | log_insert(pdi, pci, t, "I"); |
b6076a9b | 1527 | break; |
d0ca268b | 1528 | case __BLK_TA_BACKMERGE: |
152f6476 | 1529 | account_m(t, pci, w); |
f7bd1a9b | 1530 | log_merge(pdi, pci, t, "M"); |
d0ca268b JA |
1531 | break; |
1532 | case __BLK_TA_FRONTMERGE: | |
152f6476 | 1533 | account_m(t, pci, w); |
f7bd1a9b | 1534 | log_merge(pdi, pci, t, "F"); |
d0ca268b JA |
1535 | break; |
1536 | case __BLK_TA_GETRQ: | |
f7bd1a9b | 1537 | log_track_getrq(pdi, t); |
3639a11e | 1538 | log_generic(pci, t, "G"); |
d0ca268b JA |
1539 | break; |
1540 | case __BLK_TA_SLEEPRQ: | |
3639a11e | 1541 | log_generic(pci, t, "S"); |
d0ca268b JA |
1542 | break; |
1543 | case __BLK_TA_REQUEUE: | |
65f2deb5 JA |
1544 | /* |
1545 | * can happen if we miss traces, don't let it go | |
1546 | * below zero | |
1547 | */ | |
1548 | if (pdi->cur_depth[w]) | |
1549 | pdi->cur_depth[w]--; | |
4054070a | 1550 | account_requeue(t, pci, w); |
3639a11e | 1551 | log_queue(pci, t, "R"); |
d0ca268b JA |
1552 | break; |
1553 | case __BLK_TA_ISSUE: | |
b6076a9b | 1554 | account_issue(t, pci, w); |
649c7b66 JA |
1555 | pdi->cur_depth[w]++; |
1556 | if (pdi->cur_depth[w] > pdi->max_depth[w]) | |
1557 | pdi->max_depth[w] = pdi->cur_depth[w]; | |
f7bd1a9b | 1558 | log_issue(pdi, pci, t, "D"); |
d0ca268b JA |
1559 | break; |
1560 | case __BLK_TA_COMPLETE: | |
65f2deb5 JA |
1561 | if (pdi->cur_depth[w]) |
1562 | pdi->cur_depth[w]--; | |
152f6476 | 1563 | account_c(t, pci, w, t->bytes); |
f7bd1a9b | 1564 | log_complete(pdi, pci, t, "C"); |
d0ca268b | 1565 | break; |
88b1a526 | 1566 | case __BLK_TA_PLUG: |
3639a11e | 1567 | log_action(pci, t, "P"); |
88b1a526 | 1568 | break; |
3639a11e | 1569 | case __BLK_TA_UNPLUG_IO: |
06639b27 | 1570 | account_unplug(t, pci, 0); |
3639a11e JA |
1571 | log_unplug(pci, t, "U"); |
1572 | break; | |
1573 | case __BLK_TA_UNPLUG_TIMER: | |
06639b27 | 1574 | account_unplug(t, pci, 1); |
3639a11e | 1575 | log_unplug(pci, t, "UT"); |
88b1a526 | 1576 | break; |
93f1c611 JA |
1577 | case __BLK_TA_SPLIT: |
1578 | log_split(pci, t, "X"); | |
1579 | break; | |
1580 | case __BLK_TA_BOUNCE: | |
1581 | log_generic(pci, t, "B"); | |
1582 | break; | |
a8f30e64 JA |
1583 | case __BLK_TA_REMAP: |
1584 | log_generic(pci, t, "A"); | |
1585 | break; | |
d0ca268b JA |
1586 | default: |
1587 | fprintf(stderr, "Bad fs action %x\n", t->action); | |
1f79c4a0 | 1588 | break; |
d0ca268b | 1589 | } |
d0ca268b JA |
1590 | } |
1591 | ||
ff3a732c JA |
1592 | static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci, |
1593 | struct per_dev_info *pdi) | |
d0ca268b | 1594 | { |
234db09d | 1595 | if (text_output) { |
1a15f6a8 AB |
1596 | if (t->action == BLK_TN_MESSAGE) |
1597 | handle_notify(t); | |
1598 | else if (t->action & BLK_TC_ACT(BLK_TC_PC)) | |
c82a8c9d | 1599 | dump_trace_pc(t, pdi, pci); |
234db09d AB |
1600 | else |
1601 | dump_trace_fs(t, pdi, pci); | |
1602 | } | |
87b72777 | 1603 | |
20ed6177 JA |
1604 | if (!pdi->events) |
1605 | pdi->first_reported_time = t->time; | |
1606 | ||
e7c9f3ff | 1607 | pdi->events++; |
a2594911 AB |
1608 | |
1609 | output_binary(t, sizeof(*t) + t->pdu_len); | |
d0ca268b JA |
1610 | } |
1611 | ||
4c523165 JA |
1612 | /* |
1613 | * print in a proper way, not too small and not too big. if more than | |
1614 | * 1000,000K, turn into M and so on | |
1615 | */ | |
1616 | static char *size_cnv(char *dst, unsigned long long num, int in_kb) | |
1617 | { | |
da19e768 | 1618 | char suff[] = { '\0', 'K', 'M', 'G', 'P' }; |
0dc3602c | 1619 | unsigned int i = 0; |
4c523165 JA |
1620 | |
1621 | if (in_kb) | |
1622 | i++; | |
1623 | ||
0dc3602c | 1624 | while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) { |
4c523165 JA |
1625 | i++; |
1626 | num /= 1000; | |
1627 | } | |
1628 | ||
1629 | sprintf(dst, "%'8Lu%c", num, suff[i]); | |
1630 | return dst; | |
1631 | } | |
1632 | ||
649c7b66 JA |
1633 | static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios, |
1634 | char *msg) | |
5c017e4b | 1635 | { |
4c523165 JA |
1636 | static char x[256], y[256]; |
1637 | ||
152f6476 JA |
1638 | fprintf(ofp, "%s\n", msg); |
1639 | ||
4c523165 JA |
1640 | fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1)); |
1641 | fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1)); | |
4c523165 JA |
1642 | fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1)); |
1643 | fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1)); | |
4054070a JA |
1644 | fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0)); |
1645 | fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0)); | |
4c523165 JA |
1646 | fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1)); |
1647 | fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1)); | |
fb2ec796 JA |
1648 | fprintf(ofp, " Read Merges: %s, %siB\t", size_cnv(x, ios->mreads, 0), size_cnv(y, ios->mread_kb, 1)); |
1649 | fprintf(ofp, " Write Merges: %s, %siB\n", size_cnv(x, ios->mwrites, 0), size_cnv(y, ios->mwrite_kb, 1)); | |
649c7b66 JA |
1650 | if (pdi) { |
1651 | fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' '); | |
1652 | fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]); | |
1653 | } | |
801646d6 CS |
1654 | if (ios->qreads_pc || ios->qwrites_pc || ios->ireads_pc || ios->iwrites_pc || |
1655 | ios->rrqueue_pc || ios->wrqueue_pc || ios->creads_pc || ios->cwrites_pc) { | |
1656 | fprintf(ofp, " PC Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads_pc, 0), size_cnv(y, ios->qread_kb_pc, 1)); | |
1657 | fprintf(ofp, " PC Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites_pc, 0), size_cnv(y, ios->qwrite_kb_pc, 1)); | |
1658 | fprintf(ofp, " PC Read Disp.: %s, %siB\t", size_cnv(x, ios->ireads_pc, 0), size_cnv(y, ios->iread_kb_pc, 1)); | |
1659 | fprintf(ofp, " PC Write Disp.: %s, %siB\n", size_cnv(x, ios->iwrites_pc, 0), size_cnv(y, ios->iwrite_kb_pc, 1)); | |
1660 | fprintf(ofp, " PC Reads Req.: %s\t\t", size_cnv(x, ios->rrqueue_pc, 0)); | |
1661 | fprintf(ofp, " PC Writes Req.: %s\n", size_cnv(x, ios->wrqueue_pc, 0)); | |
1662 | fprintf(ofp, " PC Reads Compl.: %s\t\t", size_cnv(x, ios->creads_pc, 0)); | |
1663 | fprintf(ofp, " PC Writes Compl.: %s\n", size_cnv(x, ios->cwrites, 0)); | |
1664 | } | |
06639b27 JA |
1665 | fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' '); |
1666 | fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs); | |
5c017e4b JA |
1667 | } |
1668 | ||
50adc0ba JA |
1669 | static void dump_wait_stats(struct per_process_info *ppi) |
1670 | { | |
b9d40d6f JA |
1671 | unsigned long rawait = ppi->longest_allocation_wait[0] / 1000; |
1672 | unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000; | |
1673 | unsigned long rcwait = ppi->longest_completion_wait[0] / 1000; | |
1674 | unsigned long wawait = ppi->longest_allocation_wait[1] / 1000; | |
1675 | unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000; | |
1676 | unsigned long wcwait = ppi->longest_completion_wait[1] / 1000; | |
1677 | ||
1678 | fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' '); | |
1679 | fprintf(ofp, " Allocation wait: %'8lu\n", wawait); | |
1680 | fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' '); | |
1681 | fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait); | |
1682 | fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' '); | |
1683 | fprintf(ofp, " Completion wait: %'8lu\n", wcwait); | |
50adc0ba JA |
1684 | } |
1685 | ||
886ecf0e JA |
1686 | static int ppi_name_compare(const void *p1, const void *p2) |
1687 | { | |
1688 | struct per_process_info *ppi1 = *((struct per_process_info **) p1); | |
1689 | struct per_process_info *ppi2 = *((struct per_process_info **) p2); | |
1690 | int res; | |
1691 | ||
2990e589 | 1692 | res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm); |
886ecf0e | 1693 | if (!res) |
2990e589 | 1694 | res = ppi1->ppm->pid > ppi2->ppm->pid; |
886ecf0e JA |
1695 | |
1696 | return res; | |
1697 | } | |
1698 | ||
1699 | static void sort_process_list(void) | |
1700 | { | |
1701 | struct per_process_info **ppis; | |
1702 | struct per_process_info *ppi; | |
1703 | int i = 0; | |
1704 | ||
1705 | ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *)); | |
1706 | ||
1707 | ppi = ppi_list; | |
1708 | while (ppi) { | |
06e6f286 | 1709 | ppis[i++] = ppi; |
886ecf0e JA |
1710 | ppi = ppi->list_next; |
1711 | } | |
1712 | ||
06e6f286 | 1713 | qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare); |
886ecf0e JA |
1714 | |
1715 | i = ppi_list_entries - 1; | |
1716 | ppi_list = NULL; | |
1717 | while (i >= 0) { | |
1718 | ppi = ppis[i]; | |
1719 | ||
1720 | ppi->list_next = ppi_list; | |
1721 | ppi_list = ppi; | |
1722 | i--; | |
1723 | } | |
50c38702 JA |
1724 | |
1725 | free(ppis); | |
886ecf0e JA |
1726 | } |
1727 | ||
152f6476 JA |
1728 | static void show_process_stats(void) |
1729 | { | |
1730 | struct per_process_info *ppi; | |
1731 | ||
886ecf0e JA |
1732 | sort_process_list(); |
1733 | ||
152f6476 JA |
1734 | ppi = ppi_list; |
1735 | while (ppi) { | |
2990e589 | 1736 | struct process_pid_map *ppm = ppi->ppm; |
ce8b6b4f JA |
1737 | char name[64]; |
1738 | ||
715d8021 | 1739 | if (ppi->more_than_one) |
2990e589 | 1740 | sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid); |
715d8021 | 1741 | else |
2990e589 | 1742 | sprintf(name, "%s (%u)", ppm->comm, ppm->pid); |
bf0720af | 1743 | |
649c7b66 | 1744 | dump_io_stats(NULL, &ppi->io_stats, name); |
50adc0ba | 1745 | dump_wait_stats(ppi); |
152f6476 JA |
1746 | ppi = ppi->list_next; |
1747 | } | |
1748 | ||
1749 | fprintf(ofp, "\n"); | |
1750 | } | |
1751 | ||
e7c9f3ff | 1752 | static void show_device_and_cpu_stats(void) |
d0ca268b | 1753 | { |
e7c9f3ff NS |
1754 | struct per_dev_info *pdi; |
1755 | struct per_cpu_info *pci; | |
1756 | struct io_stats total, *ios; | |
20ed6177 | 1757 | unsigned long long rrate, wrate, msec; |
e7c9f3ff NS |
1758 | int i, j, pci_events; |
1759 | char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3]; | |
1760 | char name[32]; | |
8a82e321 | 1761 | double ratio; |
e7c9f3ff NS |
1762 | |
1763 | for (pdi = devices, i = 0; i < ndevices; i++, pdi++) { | |
1764 | ||
1765 | memset(&total, 0, sizeof(total)); | |
1766 | pci_events = 0; | |
1767 | ||
1768 | if (i > 0) | |
1769 | fprintf(ofp, "\n"); | |
1770 | ||
1771 | for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) { | |
1772 | if (!pci->nelems) | |
1773 | continue; | |
1774 | ||
1775 | ios = &pci->io_stats; | |
1776 | total.qreads += ios->qreads; | |
1777 | total.qwrites += ios->qwrites; | |
1778 | total.creads += ios->creads; | |
1779 | total.cwrites += ios->cwrites; | |
1780 | total.mreads += ios->mreads; | |
1781 | total.mwrites += ios->mwrites; | |
1782 | total.ireads += ios->ireads; | |
1783 | total.iwrites += ios->iwrites; | |
4054070a JA |
1784 | total.rrqueue += ios->rrqueue; |
1785 | total.wrqueue += ios->wrqueue; | |
e7c9f3ff NS |
1786 | total.qread_kb += ios->qread_kb; |
1787 | total.qwrite_kb += ios->qwrite_kb; | |
1788 | total.cread_kb += ios->cread_kb; | |
1789 | total.cwrite_kb += ios->cwrite_kb; | |
1790 | total.iread_kb += ios->iread_kb; | |
1791 | total.iwrite_kb += ios->iwrite_kb; | |
fb2ec796 JA |
1792 | total.mread_kb += ios->mread_kb; |
1793 | total.mwrite_kb += ios->mwrite_kb; | |
801646d6 CS |
1794 | |
1795 | total.qreads_pc += ios->qreads_pc; | |
1796 | total.qwrites_pc += ios->qwrites_pc; | |
1797 | total.creads_pc += ios->creads_pc; | |
1798 | total.cwrites_pc += ios->cwrites_pc; | |
1799 | total.ireads_pc += ios->ireads_pc; | |
1800 | total.iwrites_pc += ios->iwrites_pc; | |
1801 | total.rrqueue_pc += ios->rrqueue_pc; | |
1802 | total.wrqueue_pc += ios->wrqueue_pc; | |
1803 | total.qread_kb_pc += ios->qread_kb_pc; | |
1804 | total.qwrite_kb_pc += ios->qwrite_kb_pc; | |
1805 | total.iread_kb_pc += ios->iread_kb_pc; | |
1806 | total.iwrite_kb_pc += ios->iwrite_kb_pc; | |
1807 | ||
06639b27 JA |
1808 | total.timer_unplugs += ios->timer_unplugs; |
1809 | total.io_unplugs += ios->io_unplugs; | |
e7c9f3ff NS |
1810 | |
1811 | snprintf(line, sizeof(line) - 1, "CPU%d (%s):", | |
1812 | j, get_dev_name(pdi, name, sizeof(name))); | |
649c7b66 | 1813 | dump_io_stats(pdi, ios, line); |
e7c9f3ff NS |
1814 | pci_events++; |
1815 | } | |
5c017e4b | 1816 | |
e7c9f3ff NS |
1817 | if (pci_events > 1) { |
1818 | fprintf(ofp, "\n"); | |
1819 | snprintf(line, sizeof(line) - 1, "Total (%s):", | |
1820 | get_dev_name(pdi, name, sizeof(name))); | |
649c7b66 | 1821 | dump_io_stats(NULL, &total, line); |
e7c9f3ff | 1822 | } |
d0ca268b | 1823 | |
20ed6177 | 1824 | wrate = rrate = 0; |
20ed6177 JA |
1825 | msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000; |
1826 | if (msec) { | |
1827 | rrate = 1000 * total.cread_kb / msec; | |
1828 | wrate = 1000 * total.cwrite_kb / msec; | |
1829 | } | |
1830 | ||
dce0f678 AB |
1831 | fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n", |
1832 | rrate, wrate); | |
1833 | fprintf(ofp, "Events (%s): %'Lu entries\n", | |
1834 | get_dev_name(pdi, line, sizeof(line)), pdi->events); | |
492da111 AB |
1835 | |
1836 | collect_pdi_skips(pdi); | |
8a82e321 MZ |
1837 | if (!pdi->skips && !pdi->events) |
1838 | ratio = 0.0; | |
1839 | else | |
1840 | ratio = 100.0 * ((double)pdi->seq_skips / | |
1841 | (double)(pdi->events + pdi->seq_skips)); | |
492da111 | 1842 | fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n", |
8a82e321 | 1843 | pdi->skips, pdi->seq_skips, ratio); |
e7c9f3ff | 1844 | } |
d0ca268b JA |
1845 | } |
1846 | ||
4f0ae44f JA |
1847 | static void find_genesis(void) |
1848 | { | |
1849 | struct trace *t = trace_list; | |
1850 | ||
1851 | genesis_time = -1ULL; | |
1852 | while (t != NULL) { | |
1853 | if (t->bit->time < genesis_time) | |
1854 | genesis_time = t->bit->time; | |
1855 | ||
1856 | t = t->next; | |
1857 | } | |
7bd4fd0a OK |
1858 | |
1859 | /* The time stamp record will usually be the first | |
1860 | * record in the trace, but not always. | |
1861 | */ | |
1862 | if (start_timestamp | |
1863 | && start_timestamp != genesis_time) { | |
1864 | long delta = genesis_time - start_timestamp; | |
1865 | ||
1866 | abs_start_time.tv_sec += SECONDS(delta); | |
1867 | abs_start_time.tv_nsec += NANO_SECONDS(delta); | |
1868 | if (abs_start_time.tv_nsec < 0) { | |
1869 | abs_start_time.tv_nsec += 1000000000; | |
1870 | abs_start_time.tv_sec -= 1; | |
1871 | } else | |
1872 | if (abs_start_time.tv_nsec > 1000000000) { | |
1873 | abs_start_time.tv_nsec -= 1000000000; | |
1874 | abs_start_time.tv_sec += 1; | |
1875 | } | |
1876 | } | |
4f0ae44f JA |
1877 | } |
1878 | ||
7f4d89e6 | 1879 | static inline int check_stopwatch(struct blk_io_trace *bit) |
4f0ae44f | 1880 | { |
7f4d89e6 JA |
1881 | if (bit->time < stopwatch_end && |
1882 | bit->time >= stopwatch_start) | |
4f0ae44f JA |
1883 | return 0; |
1884 | ||
1885 | return 1; | |
1886 | } | |
1887 | ||
53c68c88 JA |
1888 | /* |
1889 | * return youngest entry read | |
1890 | */ | |
1891 | static int sort_entries(unsigned long long *youngest) | |
4f0ae44f | 1892 | { |
210824c3 JA |
1893 | struct per_dev_info *pdi = NULL; |
1894 | struct per_cpu_info *pci = NULL; | |
4f0ae44f | 1895 | struct trace *t; |
4f0ae44f JA |
1896 | |
1897 | if (!genesis_time) | |
1898 | find_genesis(); | |
1899 | ||
d6222db8 | 1900 | *youngest = 0; |
4f0ae44f JA |
1901 | while ((t = trace_list) != NULL) { |
1902 | struct blk_io_trace *bit = t->bit; | |
1903 | ||
1904 | trace_list = t->next; | |
1905 | ||
7f4d89e6 | 1906 | bit->time -= genesis_time; |
4f0ae44f | 1907 | |
d6222db8 JA |
1908 | if (bit->time < *youngest || !*youngest) |
1909 | *youngest = bit->time; | |
1910 | ||
210824c3 JA |
1911 | if (!pdi || pdi->dev != bit->device) { |
1912 | pdi = get_dev_info(bit->device); | |
1913 | pci = NULL; | |
1914 | } | |
1915 | ||
1916 | if (!pci || pci->cpu != bit->cpu) | |
1917 | pci = get_cpu_info(pdi, bit->cpu); | |
1918 | ||
1919 | if (bit->sequence < pci->smallest_seq_read) | |
1920 | pci->smallest_seq_read = bit->sequence; | |
774a1a10 | 1921 | |
7f4d89e6 | 1922 | if (check_stopwatch(bit)) { |
4f0ae44f JA |
1923 | bit_free(bit); |
1924 | t_free(t); | |
1925 | continue; | |
1926 | } | |
1927 | ||
2a1b3424 | 1928 | if (trace_rb_insert_sort(t)) |
53c68c88 | 1929 | return -1; |
4f0ae44f JA |
1930 | } |
1931 | ||
53c68c88 | 1932 | return 0; |
4f0ae44f JA |
1933 | } |
1934 | ||
824c2b39 JA |
1935 | /* |
1936 | * to continue, we must have traces from all online cpus in the tree | |
1937 | */ | |
1938 | static int check_cpu_map(struct per_dev_info *pdi) | |
1939 | { | |
1940 | unsigned long *cpu_map; | |
1941 | struct rb_node *n; | |
1942 | struct trace *__t; | |
1943 | unsigned int i; | |
1944 | int ret, cpu; | |
1945 | ||
1946 | /* | |
1947 | * create a map of the cpus we have traces for | |
1948 | */ | |
1949 | cpu_map = malloc(pdi->cpu_map_max / sizeof(long)); | |
1950 | n = rb_first(&rb_sort_root); | |
1951 | while (n) { | |
1952 | __t = rb_entry(n, struct trace, rb_node); | |
1953 | cpu = __t->bit->cpu; | |
1954 | ||
1955 | cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu)); | |
1956 | n = rb_next(n); | |
1957 | } | |
1958 | ||
1959 | /* | |
b1c8e614 JA |
1960 | * we can't continue if pdi->cpu_map has entries set that we don't |
1961 | * have in the sort rbtree. the opposite is not a problem, though | |
824c2b39 JA |
1962 | */ |
1963 | ret = 0; | |
1964 | for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) { | |
1965 | if (pdi->cpu_map[i] & ~(cpu_map[i])) { | |
1966 | ret = 1; | |
1967 | break; | |
1968 | } | |
1969 | } | |
1970 | ||
1971 | free(cpu_map); | |
1972 | return ret; | |
1973 | } | |
1974 | ||
a141a7cd | 1975 | static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force) |
2a1b3424 | 1976 | { |
1ca323a5 | 1977 | struct blk_io_trace *bit = t->bit; |
210824c3 JA |
1978 | unsigned long expected_sequence; |
1979 | struct per_cpu_info *pci; | |
1ca323a5 | 1980 | struct trace *__t; |
492da111 | 1981 | |
210824c3 JA |
1982 | pci = get_cpu_info(pdi, bit->cpu); |
1983 | expected_sequence = pci->last_sequence + 1; | |
1984 | ||
774a1a10 | 1985 | if (!expected_sequence) { |
774a1a10 JA |
1986 | /* |
1987 | * 1 should be the first entry, just allow it | |
1988 | */ | |
1989 | if (bit->sequence == 1) | |
1990 | return 0; | |
210824c3 | 1991 | if (bit->sequence == pci->smallest_seq_read) |
79ee9704 | 1992 | return 0; |
774a1a10 | 1993 | |
824c2b39 | 1994 | return check_cpu_map(pdi); |
774a1a10 | 1995 | } |
2a1b3424 JA |
1996 | |
1997 | if (bit->sequence == expected_sequence) | |
1998 | return 0; | |
1999 | ||
2a1b3424 | 2000 | /* |
1c7c54aa JA |
2001 | * we may not have seen that sequence yet. if we are not doing |
2002 | * the final run, break and wait for more entries. | |
1c24add6 | 2003 | */ |
210824c3 JA |
2004 | if (expected_sequence < pci->smallest_seq_read) { |
2005 | __t = trace_rb_find_last(pdi, pci, expected_sequence); | |
1ca323a5 | 2006 | if (!__t) |
1c7c54aa | 2007 | goto skip; |
2a1b3424 | 2008 | |
1ca323a5 | 2009 | __put_trace_last(pdi, __t); |
2a1b3424 | 2010 | return 0; |
a141a7cd JA |
2011 | } else if (!force) { |
2012 | return 1; | |
0b07f23e | 2013 | } else { |
1c7c54aa | 2014 | skip: |
66930177 | 2015 | if (check_current_skips(pci, bit->sequence)) |
492da111 AB |
2016 | return 0; |
2017 | ||
965eca2d | 2018 | if (expected_sequence < bit->sequence) |
66930177 | 2019 | insert_skip(pci, expected_sequence, bit->sequence - 1); |
1c7c54aa JA |
2020 | return 0; |
2021 | } | |
2a1b3424 JA |
2022 | } |
2023 | ||
a649216c | 2024 | static void show_entries_rb(int force) |
8fc0abbc | 2025 | { |
1f7afa72 JA |
2026 | struct per_dev_info *pdi = NULL; |
2027 | struct per_cpu_info *pci = NULL; | |
8fc0abbc | 2028 | struct blk_io_trace *bit; |
3aabcd89 | 2029 | struct rb_node *n; |
8fc0abbc | 2030 | struct trace *t; |
1f7afa72 | 2031 | |
7d747d22 | 2032 | while ((n = rb_first(&rb_sort_root)) != NULL) { |
dd90748f | 2033 | if (is_done() && !force && !pipeline) |
1f7afa72 | 2034 | break; |
8fc0abbc JA |
2035 | |
2036 | t = rb_entry(n, struct trace, rb_node); | |
2037 | bit = t->bit; | |
2038 | ||
a43c1c17 JA |
2039 | if (read_sequence - t->read_sequence < 1 && !force) |
2040 | break; | |
2041 | ||
210824c3 | 2042 | if (!pdi || pdi->dev != bit->device) { |
287fa3d6 | 2043 | pdi = get_dev_info(bit->device); |
210824c3 JA |
2044 | pci = NULL; |
2045 | } | |
1f7afa72 | 2046 | |
e7c9f3ff NS |
2047 | if (!pdi) { |
2048 | fprintf(stderr, "Unknown device ID? (%d,%d)\n", | |
2049 | MAJOR(bit->device), MINOR(bit->device)); | |
2050 | break; | |
2051 | } | |
1f7afa72 | 2052 | |
a141a7cd JA |
2053 | if (check_sequence(pdi, t, force)) |
2054 | break; | |
cb2a1a62 | 2055 | |
a141a7cd JA |
2056 | if (!force && bit->time > last_allowed_time) |
2057 | break; | |
8fc0abbc | 2058 | |
4f0ae44f | 2059 | check_time(pdi, bit); |
8fc0abbc | 2060 | |
4f0ae44f JA |
2061 | if (!pci || pci->cpu != bit->cpu) |
2062 | pci = get_cpu_info(pdi, bit->cpu); | |
287fa3d6 | 2063 | |
210824c3 JA |
2064 | pci->last_sequence = bit->sequence; |
2065 | ||
cbc927b6 JA |
2066 | pci->nelems++; |
2067 | ||
66930177 | 2068 | if (bit->action & (act_mask << BLK_TC_SHIFT)) |
98f8386b | 2069 | dump_trace(bit, pci, pdi); |
87b72777 | 2070 | |
2a1b3424 | 2071 | put_trace(pdi, t); |
cb2a1a62 | 2072 | } |
8fc0abbc JA |
2073 | } |
2074 | ||
c0e0dbc2 | 2075 | static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock) |
1f79c4a0 JA |
2076 | { |
2077 | int ret, bytes_left, fl; | |
2078 | void *p; | |
2079 | ||
c0e0dbc2 JA |
2080 | if (block != *fdblock) { |
2081 | fl = fcntl(fd, F_GETFL); | |
1f79c4a0 | 2082 | |
c0e0dbc2 JA |
2083 | if (!block) { |
2084 | *fdblock = 0; | |
2085 | fcntl(fd, F_SETFL, fl | O_NONBLOCK); | |
2086 | } else { | |
2087 | *fdblock = 1; | |
2088 | fcntl(fd, F_SETFL, fl & ~O_NONBLOCK); | |
2089 | } | |
2090 | } | |
1f79c4a0 JA |
2091 | |
2092 | bytes_left = bytes; | |
2093 | p = buffer; | |
2094 | while (bytes_left > 0) { | |
2095 | ret = read(fd, p, bytes_left); | |
2096 | if (!ret) | |
2097 | return 1; | |
2098 | else if (ret < 0) { | |
db7e0552 | 2099 | if (errno != EAGAIN) { |
1f79c4a0 | 2100 | perror("read"); |
db7e0552 JA |
2101 | return -1; |
2102 | } | |
a649216c | 2103 | |
5c0f40f7 JA |
2104 | /* |
2105 | * never do partial reads. we can return if we | |
2106 | * didn't read anything and we should not block, | |
2107 | * otherwise wait for data | |
2108 | */ | |
2109 | if ((bytes_left == bytes) && !block) | |
2110 | return 1; | |
2111 | ||
2112 | usleep(10); | |
2113 | continue; | |
1f79c4a0 JA |
2114 | } else { |
2115 | p += ret; | |
2116 | bytes_left -= ret; | |
2117 | } | |
2118 | } | |
2119 | ||
2120 | return 0; | |
2121 | } | |
2122 | ||
017d1660 JA |
2123 | static inline __u16 get_pdulen(struct blk_io_trace *bit) |
2124 | { | |
2125 | if (data_is_native) | |
2126 | return bit->pdu_len; | |
2127 | ||
2128 | return __bswap_16(bit->pdu_len); | |
2129 | } | |
2130 | ||
2131 | static inline __u32 get_magic(struct blk_io_trace *bit) | |
2132 | { | |
2133 | if (data_is_native) | |
2134 | return bit->magic; | |
2135 | ||
2136 | return __bswap_32(bit->magic); | |
2137 | } | |
2138 | ||
c0e0dbc2 | 2139 | static int read_events(int fd, int always_block, int *fdblock) |
cb2a1a62 | 2140 | { |
287fa3d6 | 2141 | struct per_dev_info *pdi = NULL; |
e820abd7 | 2142 | unsigned int events = 0; |
7d747d22 JA |
2143 | |
2144 | while (!is_done() && events < rb_batch) { | |
2145 | struct blk_io_trace *bit; | |
2146 | struct trace *t; | |
db7e0552 | 2147 | int pdu_len, should_block, ret; |
7d747d22 JA |
2148 | __u32 magic; |
2149 | ||
d36421e4 | 2150 | bit = bit_alloc(); |
cb2a1a62 | 2151 | |
c0e0dbc2 JA |
2152 | should_block = !events || always_block; |
2153 | ||
db7e0552 JA |
2154 | ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock); |
2155 | if (ret) { | |
eb9bd4e9 | 2156 | bit_free(bit); |
db7e0552 JA |
2157 | if (!events && ret < 0) |
2158 | events = ret; | |
cb2a1a62 | 2159 | break; |
eb9bd4e9 | 2160 | } |
cb2a1a62 | 2161 | |
017d1660 JA |
2162 | /* |
2163 | * look at first trace to check whether we need to convert | |
2164 | * data in the future | |
2165 | */ | |
9e4cd1b8 | 2166 | if (data_is_native == -1 && check_data_endianness(bit->magic)) |
017d1660 JA |
2167 | break; |
2168 | ||
2169 | magic = get_magic(bit); | |
7d747d22 JA |
2170 | if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { |
2171 | fprintf(stderr, "Bad magic %x\n", magic); | |
2172 | break; | |
2173 | } | |
2174 | ||
017d1660 | 2175 | pdu_len = get_pdulen(bit); |
7d747d22 JA |
2176 | if (pdu_len) { |
2177 | void *ptr = realloc(bit, sizeof(*bit) + pdu_len); | |
2178 | ||
c0e0dbc2 | 2179 | if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) { |
eb9bd4e9 | 2180 | bit_free(ptr); |
7d747d22 | 2181 | break; |
eb9bd4e9 | 2182 | } |
7d747d22 JA |
2183 | |
2184 | bit = ptr; | |
2185 | } | |
2186 | ||
d6222db8 JA |
2187 | trace_to_cpu(bit); |
2188 | ||
2189 | if (verify_trace(bit)) { | |
2190 | bit_free(bit); | |
2191 | continue; | |
2192 | } | |
2193 | ||
bfc70ad5 JA |
2194 | /* |
2195 | * not a real trace, so grab and handle it here | |
2196 | */ | |
1a15f6a8 | 2197 | if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) { |
7bd4fd0a | 2198 | handle_notify(bit); |
a2594911 | 2199 | output_binary(bit, sizeof(*bit) + bit->pdu_len); |
bfc70ad5 JA |
2200 | continue; |
2201 | } | |
2202 | ||
d36421e4 | 2203 | t = t_alloc(); |
cb2a1a62 JA |
2204 | memset(t, 0, sizeof(*t)); |
2205 | t->bit = bit; | |
a43c1c17 | 2206 | t->read_sequence = read_sequence; |
cb2a1a62 | 2207 | |
7d747d22 JA |
2208 | t->next = trace_list; |
2209 | trace_list = t; | |
1f7afa72 | 2210 | |
f7bd1a9b | 2211 | if (!pdi || pdi->dev != bit->device) |
287fa3d6 JA |
2212 | pdi = get_dev_info(bit->device); |
2213 | ||
2214 | if (bit->time > pdi->last_read_time) | |
2215 | pdi->last_read_time = bit->time; | |
2216 | ||
7d747d22 | 2217 | events++; |
cb2a1a62 JA |
2218 | } |
2219 | ||
7d747d22 | 2220 | return events; |
cb2a1a62 JA |
2221 | } |
2222 | ||
70317a16 AB |
2223 | /* |
2224 | * Managing input streams | |
2225 | */ | |
2226 | ||
2227 | struct ms_stream { | |
2228 | struct ms_stream *next; | |
2229 | struct trace *first, *last; | |
73877e12 | 2230 | struct per_dev_info *pdi; |
70317a16 AB |
2231 | unsigned int cpu; |
2232 | }; | |
d0ca268b | 2233 | |
70317a16 | 2234 | #define MS_HASH(d, c) ((MAJOR(d) & 0xff) ^ (MINOR(d) & 0xff) ^ (cpu & 0xff)) |
73877e12 | 2235 | |
70317a16 AB |
2236 | struct ms_stream *ms_head; |
2237 | struct ms_stream *ms_hash[256]; | |
87b72777 | 2238 | |
70317a16 AB |
2239 | static void ms_sort(struct ms_stream *msp); |
2240 | static int ms_prime(struct ms_stream *msp); | |
2241 | ||
2242 | static inline struct trace *ms_peek(struct ms_stream *msp) | |
2243 | { | |
2244 | return (msp == NULL) ? NULL : msp->first; | |
2245 | } | |
d0ca268b | 2246 | |
70317a16 AB |
2247 | static inline __u64 ms_peek_time(struct ms_stream *msp) |
2248 | { | |
2249 | return ms_peek(msp)->bit->time; | |
2250 | } | |
d1d7f15f | 2251 | |
70317a16 AB |
2252 | static inline void ms_resort(struct ms_stream *msp) |
2253 | { | |
2254 | if (msp->next && ms_peek_time(msp) > ms_peek_time(msp->next)) { | |
2255 | ms_head = msp->next; | |
2256 | msp->next = NULL; | |
2257 | ms_sort(msp); | |
2258 | } | |
2259 | } | |
e7c9f3ff | 2260 | |
70317a16 AB |
2261 | static inline void ms_deq(struct ms_stream *msp) |
2262 | { | |
2263 | msp->first = msp->first->next; | |
2264 | if (!msp->first) { | |
2265 | msp->last = NULL; | |
2266 | if (!ms_prime(msp)) { | |
2267 | ms_head = msp->next; | |
2268 | msp->next = NULL; | |
2269 | return; | |
d0ca268b | 2270 | } |
d5396421 JA |
2271 | } |
2272 | ||
70317a16 AB |
2273 | ms_resort(msp); |
2274 | } | |
53c68c88 | 2275 | |
70317a16 AB |
2276 | static void ms_sort(struct ms_stream *msp) |
2277 | { | |
2278 | __u64 msp_t = ms_peek_time(msp); | |
2279 | struct ms_stream *this_msp = ms_head; | |
d5396421 | 2280 | |
70317a16 AB |
2281 | if (this_msp == NULL) |
2282 | ms_head = msp; | |
2283 | else if (msp_t < ms_peek_time(this_msp)) { | |
2284 | msp->next = this_msp; | |
2285 | ms_head = msp; | |
2286 | } | |
2287 | else { | |
2288 | while (this_msp->next && ms_peek_time(this_msp->next) < msp_t) | |
2289 | this_msp = this_msp->next; | |
73877e12 | 2290 | |
70317a16 AB |
2291 | msp->next = this_msp->next; |
2292 | this_msp->next = msp; | |
2293 | } | |
2294 | } | |
d5396421 | 2295 | |
70317a16 AB |
2296 | static int ms_prime(struct ms_stream *msp) |
2297 | { | |
2298 | __u32 magic; | |
2299 | unsigned int i; | |
2300 | struct trace *t; | |
2301 | struct per_dev_info *pdi = msp->pdi; | |
2302 | struct per_cpu_info *pci = get_cpu_info(pdi, msp->cpu); | |
2303 | struct blk_io_trace *bit = NULL; | |
2304 | int ret, pdu_len, ndone = 0; | |
d5396421 | 2305 | |
70317a16 AB |
2306 | for (i = 0; !is_done() && pci->fd >= 0 && i < rb_batch; i++) { |
2307 | bit = bit_alloc(); | |
2308 | ret = read_data(pci->fd, bit, sizeof(*bit), 1, &pci->fdblock); | |
2309 | if (ret) | |
2310 | goto err; | |
51128a28 | 2311 | |
70317a16 AB |
2312 | if (data_is_native == -1 && check_data_endianness(bit->magic)) |
2313 | goto err; | |
210824c3 | 2314 | |
70317a16 AB |
2315 | magic = get_magic(bit); |
2316 | if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { | |
2317 | fprintf(stderr, "Bad magic %x\n", magic); | |
2318 | goto err; | |
d5396421 | 2319 | |
70317a16 | 2320 | } |
d5396421 | 2321 | |
70317a16 AB |
2322 | pdu_len = get_pdulen(bit); |
2323 | if (pdu_len) { | |
2324 | void *ptr = realloc(bit, sizeof(*bit) + pdu_len); | |
2325 | ret = read_data(pci->fd, ptr + sizeof(*bit), pdu_len, | |
2326 | 1, &pci->fdblock); | |
2327 | if (ret) { | |
2328 | free(ptr); | |
4eb899a6 | 2329 | bit = NULL; |
70317a16 | 2330 | goto err; |
7d747d22 | 2331 | } |
70317a16 AB |
2332 | |
2333 | bit = ptr; | |
2ff323b0 | 2334 | } |
d5396421 | 2335 | |
70317a16 AB |
2336 | trace_to_cpu(bit); |
2337 | if (verify_trace(bit)) | |
2338 | goto err; | |
53c68c88 | 2339 | |
1a15f6a8 | 2340 | if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) { |
7bd4fd0a | 2341 | handle_notify(bit); |
70317a16 AB |
2342 | output_binary(bit, sizeof(*bit) + bit->pdu_len); |
2343 | bit_free(bit); | |
287fa3d6 | 2344 | |
70317a16 AB |
2345 | i -= 1; |
2346 | continue; | |
2347 | } | |
cb2a1a62 | 2348 | |
70317a16 AB |
2349 | if (bit->time > pdi->last_read_time) |
2350 | pdi->last_read_time = bit->time; | |
d5396421 | 2351 | |
70317a16 AB |
2352 | t = t_alloc(); |
2353 | memset(t, 0, sizeof(*t)); | |
2354 | t->bit = bit; | |
2355 | ||
2356 | if (msp->first == NULL) | |
2357 | msp->first = msp->last = t; | |
2358 | else { | |
2359 | msp->last->next = t; | |
2360 | msp->last = t; | |
2361 | } | |
2362 | ||
2363 | ndone++; | |
2364 | } | |
2365 | ||
2366 | return ndone; | |
2367 | ||
2368 | err: | |
2369 | if (bit) bit_free(bit); | |
2370 | ||
2371 | cpu_mark_offline(pdi, pci->cpu); | |
2372 | close(pci->fd); | |
2373 | pci->fd = -1; | |
2374 | ||
2375 | return ndone; | |
2376 | } | |
2377 | ||
2378 | static struct ms_stream *ms_alloc(struct per_dev_info *pdi, int cpu) | |
2379 | { | |
2380 | struct ms_stream *msp = malloc(sizeof(*msp)); | |
2381 | ||
2382 | msp->next = NULL; | |
2383 | msp->first = msp->last = NULL; | |
2384 | msp->pdi = pdi; | |
2385 | msp->cpu = cpu; | |
2386 | ||
2387 | if (ms_prime(msp)) | |
2388 | ms_sort(msp); | |
2389 | ||
2390 | return msp; | |
2391 | } | |
2392 | ||
2393 | static int setup_file(struct per_dev_info *pdi, int cpu) | |
2394 | { | |
2395 | int len = 0; | |
2396 | struct stat st; | |
2397 | char *p, *dname; | |
2398 | struct per_cpu_info *pci = get_cpu_info(pdi, cpu); | |
2399 | ||
2400 | pci->cpu = cpu; | |
2401 | pci->fdblock = -1; | |
2402 | ||
2403 | p = strdup(pdi->name); | |
2404 | dname = dirname(p); | |
2405 | if (strcmp(dname, ".")) { | |
2406 | input_dir = dname; | |
2407 | p = strdup(pdi->name); | |
2408 | strcpy(pdi->name, basename(p)); | |
2409 | } | |
2410 | free(p); | |
2411 | ||
2412 | if (input_dir) | |
2413 | len = sprintf(pci->fname, "%s/", input_dir); | |
2414 | ||
2415 | snprintf(pci->fname + len, sizeof(pci->fname)-1-len, | |
2416 | "%s.blktrace.%d", pdi->name, pci->cpu); | |
8afe3d7d | 2417 | if (stat(pci->fname, &st) < 0) |
70317a16 | 2418 | return 0; |
8afe3d7d AB |
2419 | if (!st.st_size) |
2420 | return 1; | |
70317a16 AB |
2421 | |
2422 | pci->fd = open(pci->fname, O_RDONLY); | |
2423 | if (pci->fd < 0) { | |
2424 | perror(pci->fname); | |
2425 | return 0; | |
2426 | } | |
2427 | ||
2428 | printf("Input file %s added\n", pci->fname); | |
2429 | cpu_mark_online(pdi, pci->cpu); | |
2430 | ||
2431 | pdi->nfiles++; | |
2432 | ms_alloc(pdi, pci->cpu); | |
2433 | ||
2434 | return 1; | |
2435 | } | |
2436 | ||
2437 | static int handle(struct ms_stream *msp) | |
2438 | { | |
2439 | struct trace *t; | |
2440 | struct per_dev_info *pdi; | |
2441 | struct per_cpu_info *pci; | |
2442 | struct blk_io_trace *bit; | |
2443 | ||
2444 | t = ms_peek(msp); | |
70317a16 AB |
2445 | |
2446 | bit = t->bit; | |
2447 | pdi = msp->pdi; | |
2448 | pci = get_cpu_info(pdi, msp->cpu); | |
2449 | pci->nelems++; | |
8091de93 | 2450 | bit->time -= genesis_time; |
7072ee3f LU |
2451 | |
2452 | if (t->bit->time > stopwatch_end) | |
2453 | return 0; | |
2454 | ||
8091de93 | 2455 | pdi->last_reported_time = bit->time; |
7072ee3f LU |
2456 | if ((bit->action & (act_mask << BLK_TC_SHIFT))&& |
2457 | t->bit->time >= stopwatch_start) | |
70317a16 AB |
2458 | dump_trace(bit, pci, pdi); |
2459 | ||
2460 | ms_deq(msp); | |
2461 | ||
2462 | if (text_output) | |
2463 | trace_rb_insert_last(pdi, t); | |
2464 | else { | |
2465 | bit_free(t->bit); | |
2466 | t_free(t); | |
2467 | } | |
2468 | ||
2469 | return 1; | |
2470 | } | |
2471 | ||
7d340756 MZ |
2472 | /* |
2473 | * Check if we need to sanitize the name. We allow 'foo', or if foo.blktrace.X | |
2474 | * is given, then strip back down to 'foo' to avoid missing files. | |
2475 | */ | |
2476 | static int name_fixup(char *name) | |
2477 | { | |
2478 | char *b; | |
2479 | ||
2480 | if (!name) | |
2481 | return 1; | |
2482 | ||
2483 | b = strstr(name, ".blktrace."); | |
2484 | if (b) | |
2485 | *b = '\0'; | |
2486 | ||
2487 | return 0; | |
2488 | } | |
2489 | ||
70317a16 AB |
2490 | static int do_file(void) |
2491 | { | |
7d340756 | 2492 | int i, cpu, ret; |
70317a16 AB |
2493 | struct per_dev_info *pdi; |
2494 | ||
2495 | /* | |
2496 | * first prepare all files for reading | |
2497 | */ | |
2498 | for (i = 0; i < ndevices; i++) { | |
2499 | pdi = &devices[i]; | |
7d340756 MZ |
2500 | ret = name_fixup(pdi->name); |
2501 | if (ret) | |
2502 | return ret; | |
2503 | ||
70317a16 AB |
2504 | for (cpu = 0; setup_file(pdi, cpu); cpu++) |
2505 | ; | |
2506 | } | |
2507 | ||
8091de93 AB |
2508 | /* |
2509 | * Get the initial time stamp | |
2510 | */ | |
2511 | if (ms_head) | |
2512 | genesis_time = ms_peek_time(ms_head); | |
2513 | ||
70317a16 AB |
2514 | /* |
2515 | * Keep processing traces while any are left | |
2516 | */ | |
2517 | while (!is_done() && ms_head && handle(ms_head)) | |
2518 | ; | |
a649216c | 2519 | |
7d747d22 | 2520 | return 0; |
412819ce | 2521 | } |
d5396421 | 2522 | |
67076cbc | 2523 | static void do_pipe(int fd) |
412819ce | 2524 | { |
53c68c88 | 2525 | unsigned long long youngest; |
67076cbc | 2526 | int events, fdblock; |
d5396421 | 2527 | |
be925321 | 2528 | last_allowed_time = -1ULL; |
c0e0dbc2 | 2529 | fdblock = -1; |
db7e0552 | 2530 | while ((events = read_events(fd, 0, &fdblock)) > 0) { |
4ab42801 | 2531 | read_sequence++; |
412819ce | 2532 | |
210824c3 | 2533 | #if 0 |
0b07f23e | 2534 | smallest_seq_read = -1U; |
210824c3 | 2535 | #endif |
0b07f23e | 2536 | |
53c68c88 JA |
2537 | if (sort_entries(&youngest)) |
2538 | break; | |
2539 | ||
2540 | if (youngest > stopwatch_end) | |
2ff323b0 JA |
2541 | break; |
2542 | ||
763d936e | 2543 | show_entries_rb(0); |
0b07f23e | 2544 | } |
d5396421 | 2545 | |
a649216c JA |
2546 | if (rb_sort_entries) |
2547 | show_entries_rb(1); | |
67076cbc JA |
2548 | } |
2549 | ||
2550 | static int do_fifo(void) | |
2551 | { | |
2552 | int fd; | |
2553 | ||
2554 | if (!strcmp(pipename, "-")) | |
2555 | fd = dup(STDIN_FILENO); | |
2556 | else | |
2557 | fd = open(pipename, O_RDONLY); | |
2558 | ||
2559 | if (fd == -1) { | |
2560 | perror("dup stdin"); | |
2561 | return -1; | |
2562 | } | |
a649216c | 2563 | |
67076cbc | 2564 | do_pipe(fd); |
d5396421 | 2565 | close(fd); |
d5396421 JA |
2566 | return 0; |
2567 | } | |
d0ca268b | 2568 | |
cbc927b6 | 2569 | static void show_stats(void) |
412819ce | 2570 | { |
cbc927b6 JA |
2571 | if (!ofp) |
2572 | return; | |
2573 | if (stats_printed) | |
2574 | return; | |
2575 | ||
2576 | stats_printed = 1; | |
2577 | ||
2578 | if (per_process_stats) | |
2579 | show_process_stats(); | |
2580 | ||
2581 | if (per_device_and_cpu_stats) | |
2582 | show_device_and_cpu_stats(); | |
2583 | ||
152f6476 | 2584 | fflush(ofp); |
412819ce JA |
2585 | } |
2586 | ||
e820abd7 | 2587 | static void handle_sigint(__attribute__((__unused__)) int sig) |
412819ce JA |
2588 | { |
2589 | done = 1; | |
412819ce JA |
2590 | } |
2591 | ||
46e6968b NS |
2592 | /* |
2593 | * Extract start and duration times from a string, allowing | |
2594 | * us to specify a time interval of interest within a trace. | |
2595 | * Format: "duration" (start is zero) or "start:duration". | |
2596 | */ | |
2597 | static int find_stopwatch_interval(char *string) | |
2598 | { | |
2599 | double value; | |
2600 | char *sp; | |
2601 | ||
2602 | value = strtod(string, &sp); | |
2603 | if (sp == string) { | |
2604 | fprintf(stderr,"Invalid stopwatch timer: %s\n", string); | |
2605 | return 1; | |
2606 | } | |
2607 | if (*sp == ':') { | |
2608 | stopwatch_start = DOUBLE_TO_NANO_ULL(value); | |
2609 | string = sp + 1; | |
2610 | value = strtod(string, &sp); | |
2611 | if (sp == string || *sp != '\0') { | |
2612 | fprintf(stderr,"Invalid stopwatch duration time: %s\n", | |
2613 | string); | |
2614 | return 1; | |
2615 | } | |
2616 | } else if (*sp != '\0') { | |
2617 | fprintf(stderr,"Invalid stopwatch start timer: %s\n", string); | |
2618 | return 1; | |
2619 | } | |
1b928247 JA |
2620 | stopwatch_end = DOUBLE_TO_NANO_ULL(value); |
2621 | if (stopwatch_end <= stopwatch_start) { | |
2622 | fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n", | |
2623 | stopwatch_start, stopwatch_end); | |
2624 | return 1; | |
2625 | } | |
2626 | ||
46e6968b NS |
2627 | return 0; |
2628 | } | |
2629 | ||
67076cbc JA |
2630 | static int is_pipe(const char *str) |
2631 | { | |
2632 | struct stat st; | |
2633 | ||
2634 | if (!strcmp(str, "-")) | |
2635 | return 1; | |
2636 | if (!stat(str, &st) && S_ISFIFO(st.st_mode)) | |
2637 | return 1; | |
2638 | ||
2639 | return 0; | |
2640 | } | |
2641 | ||
234db09d AB |
2642 | #define S_OPTS "a:A:b:D:d:f:F:hi:o:Oqstw:vV" |
2643 | static char usage_str[] = "\n\n" \ | |
2644 | "-i <file> | --input=<file>\n" \ | |
2645 | "[ -a <action field> | --act-mask=<action field> ]\n" \ | |
2646 | "[ -A <action mask> | --set-mask=<action mask> ]\n" \ | |
2647 | "[ -b <traces> | --batch=<traces> ]\n" \ | |
2648 | "[ -d <file> | --dump-binary=<file> ]\n" \ | |
2649 | "[ -D <dir> | --input-directory=<dir> ]\n" \ | |
2650 | "[ -f <format> | --format=<format> ]\n" \ | |
2651 | "[ -F <spec> | --format-spec=<spec> ]\n" \ | |
2652 | "[ -h | --hash-by-name ]\n" \ | |
2653 | "[ -o <file> | --output=<file> ]\n" \ | |
2654 | "[ -O | --no-text-output ]\n" \ | |
2655 | "[ -q | --quiet ]\n" \ | |
2656 | "[ -s | --per-program-stats ]\n" \ | |
2657 | "[ -t | --track-ios ]\n" \ | |
2658 | "[ -w <time> | --stopwatch=<time> ]\n" \ | |
2659 | "[ -v | --verbose ]\n" \ | |
2660 | "[ -V | --version ]\n\n" \ | |
2661 | "\t-b stdin read batching\n" \ | |
2662 | "\t-d Output file. If specified, binary data is written to file\n" \ | |
d1d7f15f | 2663 | "\t-D Directory to prepend to input file names\n" \ |
234db09d AB |
2664 | "\t-f Output format. Customize the output format. The format field\n" \ |
2665 | "\t identifies can be found in the documentation\n" \ | |
2666 | "\t-F Format specification. Can be found in the documentation\n" \ | |
2667 | "\t-h Hash processes by name, not pid\n" \ | |
2668 | "\t-i Input file containing trace data, or '-' for stdin\n" \ | |
52724a0e | 2669 | "\t-o Output file. If not given, output is stdout\n" \ |
234db09d AB |
2670 | "\t-O Do NOT output text data\n" \ |
2671 | "\t-q Quiet. Don't display any stats at the end of the trace\n" \ | |
52724a0e JA |
2672 | "\t-s Show per-program io statistics\n" \ |
2673 | "\t-t Track individual ios. Will tell you the time a request took\n" \ | |
2674 | "\t to get queued, to get dispatched, and to get completed\n" \ | |
52724a0e JA |
2675 | "\t-w Only parse data between the given time interval in seconds.\n" \ |
2676 | "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \ | |
57ea8602 JA |
2677 | "\t-v More verbose for marginal errors\n" \ |
2678 | "\t-V Print program version info\n\n"; | |
52724a0e | 2679 | |
1f79c4a0 JA |
2680 | static void usage(char *prog) |
2681 | { | |
52724a0e | 2682 | fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str); |
1f79c4a0 JA |
2683 | } |
2684 | ||
d5396421 JA |
2685 | int main(int argc, char *argv[]) |
2686 | { | |
98f8386b | 2687 | int i, c, ret, mode; |
98f8386b | 2688 | int act_mask_tmp = 0; |
234db09d | 2689 | char *ofp_buffer = NULL; |
346d8a74 | 2690 | char *bin_ofp_buffer = NULL; |
d5396421 JA |
2691 | |
2692 | while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) { | |
2693 | switch (c) { | |
98f8386b AB |
2694 | case 'a': |
2695 | i = find_mask_map(optarg); | |
2696 | if (i < 0) { | |
2697 | fprintf(stderr,"Invalid action mask %s\n", | |
2698 | optarg); | |
2699 | return 1; | |
2700 | } | |
2701 | act_mask_tmp |= i; | |
2702 | break; | |
2703 | ||
2704 | case 'A': | |
2705 | if ((sscanf(optarg, "%x", &i) != 1) || | |
2706 | !valid_act_opt(i)) { | |
2707 | fprintf(stderr, | |
2708 | "Invalid set action mask %s/0x%x\n", | |
2709 | optarg, i); | |
2710 | return 1; | |
2711 | } | |
2712 | act_mask_tmp = i; | |
2713 | break; | |
d5396421 | 2714 | case 'i': |
67076cbc | 2715 | if (is_pipe(optarg) && !pipeline) { |
e7c9f3ff | 2716 | pipeline = 1; |
67076cbc JA |
2717 | pipename = strdup(optarg); |
2718 | } else if (resize_devices(optarg) != 0) | |
e7c9f3ff | 2719 | return 1; |
d5396421 | 2720 | break; |
d1d7f15f JA |
2721 | case 'D': |
2722 | input_dir = optarg; | |
2723 | break; | |
d5396421 | 2724 | case 'o': |
66efebf8 | 2725 | output_name = optarg; |
d5396421 | 2726 | break; |
234db09d AB |
2727 | case 'O': |
2728 | text_output = 0; | |
2729 | break; | |
79f19470 JA |
2730 | case 'b': |
2731 | rb_batch = atoi(optarg); | |
2732 | if (rb_batch <= 0) | |
2733 | rb_batch = RB_BATCH_DEFAULT; | |
2734 | break; | |
152f6476 JA |
2735 | case 's': |
2736 | per_process_stats = 1; | |
2737 | break; | |
7997c5b0 JA |
2738 | case 't': |
2739 | track_ios = 1; | |
2740 | break; | |
1e1c60f1 NS |
2741 | case 'q': |
2742 | per_device_and_cpu_stats = 0; | |
2743 | break; | |
46e6968b NS |
2744 | case 'w': |
2745 | if (find_stopwatch_interval(optarg) != 0) | |
2746 | return 1; | |
2747 | break; | |
ab197ca7 AB |
2748 | case 'f': |
2749 | set_all_format_specs(optarg); | |
2750 | break; | |
2751 | case 'F': | |
2752 | if (add_format_spec(optarg) != 0) | |
2753 | return 1; | |
2754 | break; | |
d915dee6 | 2755 | case 'h': |
715d8021 | 2756 | ppi_hash_by_pid = 0; |
bf0720af | 2757 | break; |
52724a0e | 2758 | case 'v': |
57ea8602 JA |
2759 | verbose++; |
2760 | break; | |
2761 | case 'V': | |
52724a0e JA |
2762 | printf("%s version %s\n", argv[0], blkparse_version); |
2763 | return 0; | |
a2594911 AB |
2764 | case 'd': |
2765 | dump_binary = optarg; | |
2766 | break; | |
d5396421 | 2767 | default: |
1f79c4a0 | 2768 | usage(argv[0]); |
d5396421 JA |
2769 | return 1; |
2770 | } | |
d0ca268b JA |
2771 | } |
2772 | ||
e7c9f3ff | 2773 | while (optind < argc) { |
67076cbc | 2774 | if (is_pipe(argv[optind]) && !pipeline) { |
e7c9f3ff | 2775 | pipeline = 1; |
00cd3044 | 2776 | pipename = strdup(argv[optind]); |
67076cbc | 2777 | } else if (resize_devices(argv[optind]) != 0) |
e7c9f3ff NS |
2778 | return 1; |
2779 | optind++; | |
2780 | } | |
2781 | ||
2782 | if (!pipeline && !ndevices) { | |
1f79c4a0 | 2783 | usage(argv[0]); |
d5396421 JA |
2784 | return 1; |
2785 | } | |
2786 | ||
98f8386b AB |
2787 | if (act_mask_tmp != 0) |
2788 | act_mask = act_mask_tmp; | |
2789 | ||
7997c5b0 | 2790 | memset(&rb_sort_root, 0, sizeof(rb_sort_root)); |
412819ce JA |
2791 | |
2792 | signal(SIGINT, handle_sigint); | |
2793 | signal(SIGHUP, handle_sigint); | |
2794 | signal(SIGTERM, handle_sigint); | |
d5396421 | 2795 | |
d69db225 JA |
2796 | setlocale(LC_NUMERIC, "en_US"); |
2797 | ||
234db09d AB |
2798 | if (text_output) { |
2799 | if (!output_name) { | |
2800 | ofp = fdopen(STDOUT_FILENO, "w"); | |
2801 | mode = _IOLBF; | |
2802 | } else { | |
2803 | char ofname[128]; | |
152f6476 | 2804 | |
234db09d AB |
2805 | snprintf(ofname, sizeof(ofname) - 1, "%s", output_name); |
2806 | ofp = fopen(ofname, "w"); | |
2807 | mode = _IOFBF; | |
2808 | } | |
152f6476 | 2809 | |
234db09d AB |
2810 | if (!ofp) { |
2811 | perror("fopen"); | |
2812 | return 1; | |
2813 | } | |
152f6476 | 2814 | |
234db09d AB |
2815 | ofp_buffer = malloc(4096); |
2816 | if (setvbuf(ofp, ofp_buffer, mode, 4096)) { | |
2817 | perror("setvbuf"); | |
2818 | return 1; | |
2819 | } | |
152f6476 JA |
2820 | } |
2821 | ||
a2594911 | 2822 | if (dump_binary) { |
346d8a74 AB |
2823 | dump_fp = fopen(dump_binary, "w"); |
2824 | if (!dump_fp) { | |
a2594911 AB |
2825 | perror(dump_binary); |
2826 | dump_binary = NULL; | |
2827 | return 1; | |
2828 | } | |
346d8a74 AB |
2829 | bin_ofp_buffer = malloc(128 * 1024); |
2830 | if (setvbuf(dump_fp, bin_ofp_buffer, _IOFBF, 128 * 1024)) { | |
2831 | perror("setvbuf binary"); | |
2832 | return 1; | |
2833 | } | |
a2594911 AB |
2834 | } |
2835 | ||
e7c9f3ff | 2836 | if (pipeline) |
67076cbc | 2837 | ret = do_fifo(); |
d5396421 JA |
2838 | else |
2839 | ret = do_file(); | |
2840 | ||
fb863d7c MZ |
2841 | if (!ret) |
2842 | show_stats(); | |
2843 | ||
8091de93 AB |
2844 | if (ofp_buffer) { |
2845 | fflush(ofp); | |
234db09d | 2846 | free(ofp_buffer); |
8091de93 AB |
2847 | } |
2848 | if (bin_ofp_buffer) { | |
2849 | fflush(dump_fp); | |
346d8a74 | 2850 | free(bin_ofp_buffer); |
8091de93 | 2851 | } |
d5396421 | 2852 | return ret; |
d0ca268b | 2853 | } |