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