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