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