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