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