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> |
d0ca268b | 33 | |
8fc0abbc JA |
34 | #include "blktrace.h" |
35 | #include "rbtree.h" | |
d0ca268b | 36 | |
2e3e8ded JA |
37 | #define SECONDS(x) ((unsigned long long)(x) / 1000000000) |
38 | #define NANO_SECONDS(x) ((unsigned long long)(x) % 1000000000) | |
46e6968b | 39 | #define DOUBLE_TO_NANO_ULL(d) ((unsigned long long)((d) * 1000000000)) |
cfab07eb | 40 | |
e7c9f3ff NS |
41 | #define MINORBITS 20 |
42 | #define MINORMASK ((1U << MINORBITS) - 1) | |
43 | #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS)) | |
44 | #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK)) | |
45 | ||
46 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
d5396421 | 47 | |
152f6476 JA |
48 | struct io_stats { |
49 | unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites; | |
50 | unsigned long ireads, iwrites; | |
51 | unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb; | |
52 | unsigned long long iread_kb, iwrite_kb; | |
06639b27 | 53 | unsigned long io_unplugs, timer_unplugs; |
152f6476 JA |
54 | }; |
55 | ||
d5396421 | 56 | struct per_cpu_info { |
d0ca268b JA |
57 | int cpu; |
58 | int nelems; | |
d0ca268b JA |
59 | |
60 | int fd; | |
87b72777 | 61 | char fname[128]; |
d0ca268b | 62 | |
152f6476 JA |
63 | struct io_stats io_stats; |
64 | }; | |
8fc0abbc | 65 | |
e7c9f3ff NS |
66 | struct per_dev_info { |
67 | dev_t id; | |
68 | char *name; | |
69 | ||
70 | int backwards; | |
71 | unsigned long long events; | |
72 | unsigned long long last_reported_time; | |
73 | struct io_stats io_stats; | |
74 | ||
75 | int ncpus; | |
76 | struct per_cpu_info *cpus; | |
77 | }; | |
78 | ||
152f6476 JA |
79 | struct per_process_info { |
80 | char name[16]; | |
81 | __u32 pid; | |
82 | struct io_stats io_stats; | |
83 | struct per_process_info *hash_next, *list_next; | |
50adc0ba JA |
84 | |
85 | /* | |
86 | * individual io stats | |
87 | */ | |
b9d40d6f JA |
88 | unsigned long long longest_allocation_wait[2]; |
89 | unsigned long long longest_dispatch_wait[2]; | |
90 | unsigned long long longest_completion_wait[2]; | |
d0ca268b JA |
91 | }; |
92 | ||
152f6476 JA |
93 | #define PPI_HASH_SHIFT (8) |
94 | static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT]; | |
95 | static struct per_process_info *ppi_list; | |
96 | ||
46e6968b | 97 | #define S_OPTS "i:o:b:stqw:" |
d5396421 JA |
98 | static struct option l_opts[] = { |
99 | { | |
100 | .name = "input", | |
101 | .has_arg = 1, | |
102 | .flag = NULL, | |
103 | .val = 'i' | |
104 | }, | |
105 | { | |
106 | .name = "output", | |
107 | .has_arg = 1, | |
108 | .flag = NULL, | |
109 | .val = 'o' | |
110 | }, | |
79f19470 JA |
111 | { |
112 | .name = "batch", | |
113 | .has_arg = 1, | |
114 | .flag = NULL, | |
115 | .val = 'b' | |
116 | }, | |
152f6476 JA |
117 | { |
118 | .name = "per program stats", | |
119 | .has_arg = 0, | |
120 | .flag = NULL, | |
121 | .val = 's' | |
122 | }, | |
7997c5b0 JA |
123 | { |
124 | .name = "track ios", | |
125 | .has_arg = 0, | |
126 | .flag = NULL, | |
127 | .val = 't' | |
128 | }, | |
1e1c60f1 NS |
129 | { |
130 | .name = "quiet", | |
131 | .has_arg = 0, | |
132 | .flag = NULL, | |
133 | .val = 'q' | |
134 | }, | |
46e6968b NS |
135 | { |
136 | .name = "stopwatch", | |
137 | .has_arg = 1, | |
138 | .flag = NULL, | |
139 | .val = 'w' | |
140 | }, | |
d5396421 JA |
141 | { |
142 | .name = NULL, | |
143 | .has_arg = 0, | |
144 | .flag = NULL, | |
145 | .val = 0 | |
146 | } | |
147 | }; | |
148 | ||
7997c5b0 JA |
149 | static struct rb_root rb_sort_root; |
150 | static struct rb_root rb_track_root; | |
8fc0abbc | 151 | |
7997c5b0 JA |
152 | /* |
153 | * for sorting the displayed output | |
154 | */ | |
8fc0abbc JA |
155 | struct trace { |
156 | struct blk_io_trace *bit; | |
157 | struct rb_node rb_node; | |
158 | }; | |
159 | ||
7997c5b0 JA |
160 | /* |
161 | * for tracking individual ios | |
162 | */ | |
163 | struct io_track { | |
164 | struct rb_node rb_node; | |
165 | ||
e7c9f3ff | 166 | dev_t device; |
7997c5b0 JA |
167 | __u64 sector; |
168 | __u32 pid; | |
95c15013 | 169 | unsigned long long allocation_time; |
7997c5b0 JA |
170 | unsigned long long queue_time; |
171 | unsigned long long dispatch_time; | |
172 | unsigned long long completion_time; | |
173 | }; | |
174 | ||
e7c9f3ff NS |
175 | static int ndevices; |
176 | static struct per_dev_info *devices; | |
177 | static char *get_dev_name(struct per_dev_info *, char *, int); | |
d0ca268b | 178 | |
152f6476 | 179 | static FILE *ofp; |
e7c9f3ff NS |
180 | static char *output_name; |
181 | ||
182 | static unsigned long long genesis_time; | |
46e6968b NS |
183 | static unsigned long long stopwatch_start; /* start from zero by default */ |
184 | static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */ | |
152f6476 JA |
185 | |
186 | static int per_process_stats; | |
7997c5b0 | 187 | static int track_ios; |
d0ca268b | 188 | |
79f19470 JA |
189 | #define RB_BATCH_DEFAULT (1024) |
190 | static int rb_batch = RB_BATCH_DEFAULT; | |
191 | ||
e7c9f3ff NS |
192 | static int pipeline; |
193 | ||
412819ce JA |
194 | #define is_done() (*(volatile int *)(&done)) |
195 | static volatile int done; | |
196 | ||
152f6476 JA |
197 | static inline unsigned long hash_long(unsigned long val) |
198 | { | |
16ef714e JA |
199 | #if __WORDSIZE == 32 |
200 | val *= 0x9e370001UL; | |
201 | #elif __WORDSIZE == 64 | |
202 | val *= 0x9e37fffffffc0001UL; | |
203 | #else | |
204 | #error unknown word size | |
205 | #endif | |
206 | ||
207 | return val >> (__WORDSIZE - PPI_HASH_SHIFT); | |
152f6476 JA |
208 | } |
209 | ||
210 | static inline void add_process_to_hash(struct per_process_info *ppi) | |
211 | { | |
212 | const int hash_idx = hash_long(ppi->pid); | |
213 | ||
214 | ppi->hash_next = ppi_hash[hash_idx]; | |
215 | ppi_hash[hash_idx] = ppi; | |
216 | } | |
217 | ||
218 | static inline void add_process_to_list(struct per_process_info *ppi) | |
219 | { | |
220 | ppi->list_next = ppi_list; | |
221 | ppi_list = ppi; | |
222 | } | |
223 | ||
224 | static struct per_process_info *find_process_by_pid(__u32 pid) | |
225 | { | |
226 | const int hash_idx = hash_long(pid); | |
227 | struct per_process_info *ppi; | |
228 | ||
229 | ppi = ppi_hash[hash_idx]; | |
230 | while (ppi) { | |
231 | if (ppi->pid == pid) | |
232 | return ppi; | |
233 | ||
234 | ppi = ppi->hash_next; | |
235 | } | |
236 | ||
237 | return NULL; | |
238 | } | |
239 | ||
7997c5b0 JA |
240 | static inline int trace_rb_insert(struct trace *t) |
241 | { | |
242 | struct rb_node **p = &rb_sort_root.rb_node; | |
243 | struct rb_node *parent = NULL; | |
244 | struct trace *__t; | |
245 | ||
e7c9f3ff NS |
246 | if (genesis_time == 0 || t->bit->time < genesis_time) |
247 | genesis_time = t->bit->time; | |
248 | ||
7997c5b0 JA |
249 | while (*p) { |
250 | parent = *p; | |
251 | __t = rb_entry(parent, struct trace, rb_node); | |
252 | ||
e7c9f3ff NS |
253 | if (t->bit->time < __t->bit->time) |
254 | p = &(*p)->rb_left; | |
255 | else if (t->bit->time > __t->bit->time) | |
256 | p = &(*p)->rb_right; | |
257 | else if (t->bit->device < __t->bit->device) | |
258 | p = &(*p)->rb_left; | |
259 | else if (t->bit->device > __t->bit->device) | |
260 | p = &(*p)->rb_right; | |
261 | else if (t->bit->sequence < __t->bit->sequence) | |
7997c5b0 JA |
262 | p = &(*p)->rb_left; |
263 | else if (t->bit->sequence > __t->bit->sequence) | |
264 | p = &(*p)->rb_right; | |
e7c9f3ff NS |
265 | else if (t->bit->device == __t->bit->device) { |
266 | fprintf(stderr, | |
267 | "sequence alias (%d) on device %d,%d!\n", | |
268 | t->bit->sequence, | |
269 | MAJOR(t->bit->device), MINOR(t->bit->device)); | |
7997c5b0 JA |
270 | return 1; |
271 | } | |
272 | } | |
273 | ||
274 | rb_link_node(&t->rb_node, parent, p); | |
275 | rb_insert_color(&t->rb_node, &rb_sort_root); | |
276 | return 0; | |
277 | } | |
278 | ||
279 | static inline int track_rb_insert(struct io_track *iot) | |
280 | { | |
281 | struct rb_node **p = &rb_track_root.rb_node; | |
282 | struct rb_node *parent = NULL; | |
283 | struct io_track *__iot; | |
284 | ||
285 | while (*p) { | |
286 | parent = *p; | |
e7c9f3ff | 287 | |
7997c5b0 JA |
288 | __iot = rb_entry(parent, struct io_track, rb_node); |
289 | ||
e7c9f3ff NS |
290 | if (iot->device < __iot->device) |
291 | p = &(*p)->rb_left; | |
292 | else if (iot->device > __iot->device) | |
293 | p = &(*p)->rb_right; | |
294 | else if (iot->sector < __iot->sector) | |
7997c5b0 JA |
295 | p = &(*p)->rb_left; |
296 | else if (iot->sector > __iot->sector) | |
297 | p = &(*p)->rb_right; | |
298 | else { | |
e7c9f3ff NS |
299 | fprintf(stderr, |
300 | "sector alias (%llu) on device %d,%d!\n", | |
301 | iot->sector, | |
302 | MAJOR(iot->device), MINOR(iot->device)); | |
7997c5b0 JA |
303 | return 1; |
304 | } | |
305 | } | |
306 | ||
307 | rb_link_node(&iot->rb_node, parent, p); | |
308 | rb_insert_color(&iot->rb_node, &rb_track_root); | |
309 | return 0; | |
310 | } | |
311 | ||
e7c9f3ff | 312 | static struct io_track *__find_track(dev_t device, __u64 sector) |
7997c5b0 JA |
313 | { |
314 | struct rb_node **p = &rb_track_root.rb_node; | |
315 | struct rb_node *parent = NULL; | |
316 | struct io_track *__iot; | |
317 | ||
318 | while (*p) { | |
319 | parent = *p; | |
320 | ||
321 | __iot = rb_entry(parent, struct io_track, rb_node); | |
322 | ||
e7c9f3ff NS |
323 | if (device < __iot->device) |
324 | p = &(*p)->rb_left; | |
325 | else if (device > __iot->device) | |
326 | p = &(*p)->rb_right; | |
327 | else if (sector < __iot->sector) | |
7997c5b0 JA |
328 | p = &(*p)->rb_left; |
329 | else if (sector > __iot->sector) | |
330 | p = &(*p)->rb_right; | |
331 | else | |
332 | return __iot; | |
333 | } | |
334 | ||
335 | return NULL; | |
336 | } | |
337 | ||
e7c9f3ff | 338 | static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector) |
7997c5b0 | 339 | { |
916b5501 | 340 | struct io_track *iot; |
7997c5b0 | 341 | |
e7c9f3ff | 342 | iot = __find_track(device, sector); |
7997c5b0 JA |
343 | if (!iot) { |
344 | iot = malloc(sizeof(*iot)); | |
50adc0ba | 345 | iot->pid = pid; |
e7c9f3ff | 346 | iot->device = device; |
7997c5b0 JA |
347 | iot->sector = sector; |
348 | track_rb_insert(iot); | |
349 | } | |
350 | ||
351 | return iot; | |
352 | } | |
353 | ||
2e3e8ded JA |
354 | static void log_track_merge(struct blk_io_trace *t) |
355 | { | |
356 | struct io_track *iot; | |
357 | ||
358 | if (!track_ios) | |
359 | return; | |
360 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) | |
361 | return; | |
362 | ||
592166ca JA |
363 | /* |
364 | * this can happen if we lose events, so don't print an error | |
365 | */ | |
e7c9f3ff | 366 | iot = __find_track(t->device, t->sector - (t->bytes >> 10)); |
592166ca JA |
367 | if (iot) { |
368 | rb_erase(&iot->rb_node, &rb_track_root); | |
369 | iot->sector -= t->bytes >> 10; | |
370 | track_rb_insert(iot); | |
2e3e8ded | 371 | } |
2e3e8ded JA |
372 | } |
373 | ||
95c15013 | 374 | static void log_track_getrq(struct blk_io_trace *t) |
2e3e8ded JA |
375 | { |
376 | struct io_track *iot; | |
377 | ||
378 | if (!track_ios) | |
379 | return; | |
380 | ||
e7c9f3ff | 381 | iot = find_track(t->pid, t->device, t->sector); |
95c15013 JA |
382 | iot->allocation_time = t->time; |
383 | } | |
384 | ||
385 | ||
386 | /* | |
387 | * return time between rq allocation and queue | |
388 | */ | |
389 | static unsigned long long log_track_queue(struct blk_io_trace *t) | |
390 | { | |
50adc0ba | 391 | unsigned long long elapsed; |
95c15013 JA |
392 | struct io_track *iot; |
393 | ||
394 | if (!track_ios) | |
395 | return -1; | |
396 | ||
e7c9f3ff | 397 | iot = find_track(t->pid, t->device, t->sector); |
2e3e8ded | 398 | iot->queue_time = t->time; |
50adc0ba JA |
399 | elapsed = iot->queue_time - iot->allocation_time; |
400 | ||
401 | if (per_process_stats) { | |
402 | struct per_process_info *ppi = find_process_by_pid(iot->pid); | |
b9d40d6f | 403 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
50adc0ba | 404 | |
b9d40d6f JA |
405 | if (ppi && elapsed > ppi->longest_allocation_wait[w]) |
406 | ppi->longest_allocation_wait[w] = elapsed; | |
50adc0ba JA |
407 | } |
408 | ||
409 | return elapsed; | |
2e3e8ded JA |
410 | } |
411 | ||
412 | /* | |
413 | * return time between queue and issue | |
414 | */ | |
415 | static unsigned long long log_track_issue(struct blk_io_trace *t) | |
416 | { | |
50adc0ba | 417 | unsigned long long elapsed; |
2e3e8ded JA |
418 | struct io_track *iot; |
419 | ||
420 | if (!track_ios) | |
421 | return -1; | |
422 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) | |
423 | return -1; | |
424 | ||
592166ca JA |
425 | /* |
426 | * this can happen if we lose events, so don't print an error | |
427 | */ | |
e7c9f3ff | 428 | iot = __find_track(t->device, t->sector); |
592166ca | 429 | if (!iot) |
2e3e8ded | 430 | return -1; |
2e3e8ded JA |
431 | |
432 | iot->dispatch_time = t->time; | |
50adc0ba JA |
433 | elapsed = iot->dispatch_time - iot->queue_time; |
434 | ||
435 | if (per_process_stats) { | |
436 | struct per_process_info *ppi = find_process_by_pid(iot->pid); | |
b9d40d6f | 437 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
50adc0ba | 438 | |
b9d40d6f JA |
439 | if (ppi && elapsed > ppi->longest_dispatch_wait[w]) |
440 | ppi->longest_dispatch_wait[w] = elapsed; | |
50adc0ba JA |
441 | } |
442 | ||
443 | return elapsed; | |
2e3e8ded JA |
444 | } |
445 | ||
446 | /* | |
447 | * return time between dispatch and complete | |
448 | */ | |
449 | static unsigned long long log_track_complete(struct blk_io_trace *t) | |
450 | { | |
451 | unsigned long long elapsed; | |
452 | struct io_track *iot; | |
453 | ||
454 | if (!track_ios) | |
455 | return -1; | |
456 | if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) | |
457 | return -1; | |
458 | ||
e7c9f3ff | 459 | iot = __find_track(t->device, t->sector); |
592166ca | 460 | if (!iot) |
2e3e8ded | 461 | return -1; |
2e3e8ded JA |
462 | |
463 | iot->completion_time = t->time; | |
464 | elapsed = iot->completion_time - iot->dispatch_time; | |
465 | ||
50adc0ba JA |
466 | if (per_process_stats) { |
467 | struct per_process_info *ppi = find_process_by_pid(iot->pid); | |
b9d40d6f | 468 | int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
50adc0ba | 469 | |
b9d40d6f JA |
470 | if (ppi && elapsed > ppi->longest_completion_wait[w]) |
471 | ppi->longest_completion_wait[w] = elapsed; | |
50adc0ba JA |
472 | } |
473 | ||
2e3e8ded JA |
474 | /* |
475 | * kill the trace, we don't need it after completion | |
476 | */ | |
477 | rb_erase(&iot->rb_node, &rb_track_root); | |
478 | free(iot); | |
479 | ||
480 | return elapsed; | |
481 | } | |
482 | ||
483 | ||
152f6476 JA |
484 | static struct io_stats *find_process_io_stats(__u32 pid, char *name) |
485 | { | |
486 | struct per_process_info *ppi = find_process_by_pid(pid); | |
487 | ||
488 | if (!ppi) { | |
489 | ppi = malloc(sizeof(*ppi)); | |
490 | memset(ppi, 0, sizeof(*ppi)); | |
491 | strncpy(ppi->name, name, sizeof(ppi->name)); | |
492 | ppi->pid = pid; | |
493 | add_process_to_hash(ppi); | |
494 | add_process_to_list(ppi); | |
495 | } | |
496 | ||
497 | return &ppi->io_stats; | |
498 | } | |
499 | ||
e7c9f3ff NS |
500 | |
501 | static void resize_cpu_info(struct per_dev_info *pdi, int cpu) | |
a718bd37 | 502 | { |
e7c9f3ff NS |
503 | struct per_cpu_info *cpus = pdi->cpus; |
504 | int ncpus = pdi->ncpus; | |
505 | int new_count = cpu + 1; | |
506 | int new_space, size; | |
a718bd37 NS |
507 | char *new_start; |
508 | ||
e7c9f3ff NS |
509 | size = new_count * sizeof(struct per_cpu_info); |
510 | cpus = realloc(cpus, size); | |
511 | if (!cpus) { | |
512 | char name[20]; | |
513 | fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n", | |
514 | get_dev_name(pdi, name, sizeof(name)), size); | |
a718bd37 NS |
515 | exit(1); |
516 | } | |
517 | ||
e7c9f3ff NS |
518 | new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info)); |
519 | new_space = (new_count - ncpus) * sizeof(struct per_cpu_info); | |
a718bd37 | 520 | memset(new_start, 0, new_space); |
e7c9f3ff NS |
521 | |
522 | pdi->ncpus = new_count; | |
523 | pdi->cpus = cpus; | |
524 | } | |
525 | ||
526 | static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu) | |
527 | { | |
528 | if (cpu >= pdi->ncpus) | |
529 | resize_cpu_info(pdi, cpu); | |
530 | return &pdi->cpus[cpu]; | |
a718bd37 NS |
531 | } |
532 | ||
e7c9f3ff NS |
533 | |
534 | static int resize_devices(char *name) | |
a718bd37 | 535 | { |
e7c9f3ff | 536 | int size = (ndevices + 1) * sizeof(struct per_dev_info); |
c499bf38 | 537 | |
e7c9f3ff NS |
538 | devices = realloc(devices, size); |
539 | if (!devices) { | |
540 | fprintf(stderr, "Out of memory, device %s (%d)\n", name, size); | |
541 | return 1; | |
542 | } | |
543 | memset(&devices[ndevices], 0, sizeof(struct per_dev_info)); | |
544 | devices[ndevices].name = name; | |
545 | ndevices++; | |
546 | return 0; | |
547 | } | |
a718bd37 | 548 | |
e7c9f3ff NS |
549 | static struct per_dev_info *get_dev_info(dev_t id, int create) |
550 | { | |
551 | int i; | |
c499bf38 | 552 | |
e7c9f3ff NS |
553 | for (i = 0; i < ndevices; i++) |
554 | if (devices[i].id == id) | |
555 | return &devices[i]; | |
556 | if (!create) | |
557 | return NULL; | |
558 | if (resize_devices(NULL) != 0) | |
559 | return NULL; | |
560 | return &devices[ndevices-1]; | |
a718bd37 NS |
561 | } |
562 | ||
e7c9f3ff NS |
563 | static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size) |
564 | { | |
565 | if (pdi->name) | |
566 | snprintf(buffer, size, "%s", pdi->name); | |
567 | else | |
568 | snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id)); | |
569 | return buffer; | |
570 | } | |
571 | ||
572 | ||
573 | static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit) | |
cfab07eb AB |
574 | { |
575 | unsigned long long this = bit->time; | |
e7c9f3ff | 576 | unsigned long long last = pdi->last_reported_time; |
cfab07eb | 577 | |
e7c9f3ff NS |
578 | pdi->backwards = (this < last) ? 'B' : ' '; |
579 | pdi->last_reported_time = this; | |
cfab07eb AB |
580 | } |
581 | ||
e7c9f3ff | 582 | |
152f6476 JA |
583 | static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t, |
584 | int rw) | |
d0ca268b JA |
585 | { |
586 | if (rw) { | |
152f6476 JA |
587 | ios->mwrites++; |
588 | ios->qwrite_kb += t->bytes >> 10; | |
d0ca268b | 589 | } else { |
152f6476 JA |
590 | ios->mreads++; |
591 | ios->qread_kb += t->bytes >> 10; | |
592 | } | |
593 | } | |
594 | ||
595 | static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci, | |
596 | int rw) | |
597 | { | |
598 | __account_m(&pci->io_stats, t, rw); | |
599 | ||
600 | if (per_process_stats) { | |
601 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); | |
602 | ||
603 | __account_m(ios, t, rw); | |
d0ca268b JA |
604 | } |
605 | } | |
606 | ||
152f6476 JA |
607 | static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t, |
608 | int rw) | |
d0ca268b JA |
609 | { |
610 | if (rw) { | |
152f6476 JA |
611 | ios->qwrites++; |
612 | ios->qwrite_kb += t->bytes >> 10; | |
d0ca268b | 613 | } else { |
152f6476 JA |
614 | ios->qreads++; |
615 | ios->qread_kb += t->bytes >> 10; | |
616 | } | |
617 | } | |
618 | ||
619 | static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci, | |
620 | int rw) | |
621 | { | |
622 | __account_q(&pci->io_stats, t, rw); | |
623 | ||
624 | if (per_process_stats) { | |
625 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); | |
626 | ||
627 | __account_q(ios, t, rw); | |
d0ca268b JA |
628 | } |
629 | } | |
630 | ||
152f6476 | 631 | static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes) |
d0ca268b JA |
632 | { |
633 | if (rw) { | |
152f6476 JA |
634 | ios->cwrites++; |
635 | ios->cwrite_kb += bytes >> 10; | |
d0ca268b | 636 | } else { |
152f6476 JA |
637 | ios->creads++; |
638 | ios->cread_kb += bytes >> 10; | |
639 | } | |
640 | } | |
641 | ||
642 | static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci, | |
643 | int rw, int bytes) | |
644 | { | |
645 | __account_c(&pci->io_stats, rw, bytes); | |
646 | ||
647 | if (per_process_stats) { | |
648 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); | |
649 | ||
650 | __account_c(ios, rw, bytes); | |
d0ca268b JA |
651 | } |
652 | } | |
653 | ||
152f6476 | 654 | static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes) |
afd2d7ad | 655 | { |
656 | if (rw) { | |
152f6476 JA |
657 | ios->iwrites++; |
658 | ios->iwrite_kb += bytes >> 10; | |
afd2d7ad | 659 | } else { |
152f6476 JA |
660 | ios->ireads++; |
661 | ios->iread_kb += bytes >> 10; | |
afd2d7ad | 662 | } |
663 | } | |
664 | ||
152f6476 JA |
665 | static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci, |
666 | int rw) | |
d0ca268b | 667 | { |
152f6476 JA |
668 | __account_i(&pci->io_stats, rw, t->bytes); |
669 | ||
670 | if (per_process_stats) { | |
671 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); | |
d5396421 | 672 | |
152f6476 JA |
673 | __account_i(ios, rw, t->bytes); |
674 | } | |
675 | } | |
676 | ||
06639b27 JA |
677 | static inline void __account_unplug(struct io_stats *ios, int timer) |
678 | { | |
679 | if (timer) | |
680 | ios->timer_unplugs++; | |
681 | else | |
682 | ios->io_unplugs++; | |
683 | } | |
684 | ||
685 | static inline void account_unplug(struct blk_io_trace *t, | |
686 | struct per_cpu_info *pci, int timer) | |
687 | { | |
688 | __account_unplug(&pci->io_stats, timer); | |
689 | ||
690 | if (per_process_stats) { | |
691 | struct io_stats *ios = find_process_io_stats(t->pid, t->comm); | |
692 | ||
693 | __account_unplug(ios, timer); | |
694 | } | |
695 | } | |
696 | ||
152f6476 JA |
697 | static void output(struct per_cpu_info *pci, char *s) |
698 | { | |
699 | fprintf(ofp, "%s", s); | |
d0ca268b JA |
700 | } |
701 | ||
3aabcd89 JA |
702 | static char hstring[256]; |
703 | static char tstring[256]; | |
d0ca268b | 704 | |
d5396421 | 705 | static inline char *setup_header(struct per_cpu_info *pci, |
3639a11e | 706 | struct blk_io_trace *t, char *act) |
d0ca268b JA |
707 | { |
708 | int w = t->action & BLK_TC_ACT(BLK_TC_WRITE); | |
709 | int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER); | |
710 | int s = t->action & BLK_TC_ACT(BLK_TC_SYNC); | |
711 | char rwbs[4]; | |
712 | int i = 0; | |
713 | ||
714 | if (w) | |
715 | rwbs[i++] = 'W'; | |
716 | else | |
717 | rwbs[i++] = 'R'; | |
718 | if (b) | |
719 | rwbs[i++] = 'B'; | |
720 | if (s) | |
721 | rwbs[i++] = 'S'; | |
722 | ||
723 | rwbs[i] = '\0'; | |
724 | ||
3639a11e | 725 | sprintf(hstring, "%3d,%-3d %2d %8ld %5Lu.%09Lu %5u %2s %3s", |
e7c9f3ff | 726 | MAJOR(t->device), MINOR(t->device), pci->cpu, |
3639a11e | 727 | (unsigned long)t->sequence, SECONDS(t->time), |
cfab07eb | 728 | NANO_SECONDS(t->time), t->pid, act, rwbs); |
d0ca268b JA |
729 | |
730 | return hstring; | |
731 | } | |
732 | ||
d5396421 | 733 | static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 734 | char *act) |
d0ca268b | 735 | { |
2e3e8ded JA |
736 | unsigned long long elapsed = log_track_complete(t); |
737 | ||
738 | if (elapsed != -1ULL) { | |
b9d40d6f | 739 | unsigned long usec = elapsed / 1000; |
2e3e8ded | 740 | |
b9d40d6f | 741 | sprintf(tstring,"%s %Lu + %u (%8lu) [%d]\n", |
2e3e8ded JA |
742 | setup_header(pci, t, act), |
743 | (unsigned long long)t->sector, t->bytes >> 9, | |
744 | usec, t->error); | |
745 | } else { | |
746 | sprintf(tstring,"%s %Lu + %u [%d]\n", setup_header(pci, t, act), | |
747 | (unsigned long long)t->sector, t->bytes >> 9, t->error); | |
748 | } | |
749 | ||
d5396421 | 750 | output(pci, tstring); |
d0ca268b JA |
751 | } |
752 | ||
d5396421 | 753 | static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 754 | char *act) |
d0ca268b | 755 | { |
95c15013 | 756 | unsigned long long elapsed = log_track_queue(t); |
2e3e8ded | 757 | |
95c15013 | 758 | if (elapsed != -1ULL) { |
b9d40d6f | 759 | unsigned long usec = elapsed / 1000; |
95c15013 | 760 | |
b9d40d6f | 761 | sprintf(tstring,"%s %Lu + %u (%8lu) [%s]\n", |
95c15013 JA |
762 | setup_header(pci, t, act), |
763 | (unsigned long long)t->sector, t->bytes >> 9, | |
764 | usec, t->comm); | |
765 | } else { | |
766 | sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act), | |
767 | (unsigned long long)t->sector, t->bytes >> 9, t->comm); | |
768 | } | |
d5396421 | 769 | output(pci, tstring); |
d0ca268b JA |
770 | } |
771 | ||
d5396421 | 772 | static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 773 | char *act) |
d0ca268b | 774 | { |
2e3e8ded JA |
775 | unsigned long long elapsed = log_track_issue(t); |
776 | ||
777 | if (elapsed != -1ULL) { | |
778 | double usec = (double) elapsed / 1000; | |
779 | ||
555d3a31 | 780 | sprintf(tstring,"%s %Lu + %u (%8.2f) [%s]\n", |
2e3e8ded JA |
781 | setup_header(pci, t, act), |
782 | (unsigned long long)t->sector, t->bytes >> 9, | |
783 | usec, t->comm); | |
784 | } else { | |
785 | sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act), | |
786 | (unsigned long long)t->sector, t->bytes >> 9, t->comm); | |
787 | } | |
788 | ||
d5396421 | 789 | output(pci, tstring); |
d0ca268b JA |
790 | } |
791 | ||
d5396421 | 792 | static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 793 | char *act) |
d0ca268b | 794 | { |
2e3e8ded JA |
795 | log_track_merge(t); |
796 | ||
984c63b7 | 797 | sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act), |
2955af9d | 798 | (unsigned long long)t->sector, t->bytes >> 9, t->comm); |
d5396421 | 799 | output(pci, tstring); |
d0ca268b JA |
800 | } |
801 | ||
dfe34da1 | 802 | static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 803 | char *act) |
dfe34da1 JA |
804 | { |
805 | sprintf(tstring,"%s [%s]\n", setup_header(pci, t, act), t->comm); | |
806 | output(pci, tstring); | |
807 | } | |
808 | ||
d5396421 | 809 | static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 810 | char *act) |
d0ca268b | 811 | { |
2955af9d NS |
812 | sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act), |
813 | (unsigned long long)t->sector, t->bytes >> 9, t->comm); | |
d5396421 | 814 | output(pci, tstring); |
d0ca268b JA |
815 | } |
816 | ||
67e14fdc | 817 | static int log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t, |
3639a11e | 818 | char *act) |
67e14fdc JA |
819 | { |
820 | __u64 *depth; | |
821 | int len; | |
822 | ||
06639b27 | 823 | len = sprintf(tstring,"%s [%s] ", setup_header(pci, t, act), t->comm); |
3639a11e | 824 | depth = (__u64 *) ((char *) t + sizeof(*t)); |
67e14fdc JA |
825 | sprintf(tstring + len, "%u\n", (unsigned int) be64_to_cpu(*depth)); |
826 | output(pci, tstring); | |
827 | ||
828 | return 0; | |
829 | } | |
830 | ||
3639a11e | 831 | static int log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act) |
d0ca268b | 832 | { |
87b72777 JA |
833 | unsigned char *buf; |
834 | int i; | |
d0ca268b | 835 | |
d5396421 JA |
836 | sprintf(tstring,"%s ", setup_header(pci, t, act)); |
837 | output(pci, tstring); | |
d0ca268b | 838 | |
87b72777 | 839 | buf = (unsigned char *) t + sizeof(*t); |
d0ca268b JA |
840 | for (i = 0; i < t->pdu_len; i++) { |
841 | sprintf(tstring,"%02x ", buf[i]); | |
d5396421 | 842 | output(pci, tstring); |
d0ca268b JA |
843 | } |
844 | ||
3639a11e | 845 | if (act[0] == 'C') { |
2955af9d NS |
846 | sprintf(tstring,"[%d]\n", t->error); |
847 | output(pci, tstring); | |
848 | } else { | |
849 | sprintf(tstring,"[%s]\n", t->comm); | |
d5396421 | 850 | output(pci, tstring); |
d0ca268b | 851 | } |
87b72777 | 852 | return 0; |
d0ca268b JA |
853 | } |
854 | ||
d5396421 | 855 | static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci) |
d0ca268b | 856 | { |
87b72777 JA |
857 | int ret = 0; |
858 | ||
d0ca268b JA |
859 | switch (t->action & 0xffff) { |
860 | case __BLK_TA_QUEUE: | |
3639a11e | 861 | log_generic(pci, t, "Q"); |
d0ca268b JA |
862 | break; |
863 | case __BLK_TA_GETRQ: | |
3639a11e | 864 | log_generic(pci, t, "G"); |
d0ca268b JA |
865 | break; |
866 | case __BLK_TA_SLEEPRQ: | |
3639a11e | 867 | log_generic(pci, t, "S"); |
d0ca268b JA |
868 | break; |
869 | case __BLK_TA_REQUEUE: | |
3639a11e | 870 | log_generic(pci, t, "R"); |
d0ca268b JA |
871 | break; |
872 | case __BLK_TA_ISSUE: | |
3639a11e | 873 | ret = log_pc(pci, t, "D"); |
d0ca268b JA |
874 | break; |
875 | case __BLK_TA_COMPLETE: | |
3639a11e | 876 | log_pc(pci, t, "C"); |
d0ca268b JA |
877 | break; |
878 | default: | |
879 | fprintf(stderr, "Bad pc action %x\n", t->action); | |
87b72777 JA |
880 | ret = 1; |
881 | break; | |
d0ca268b JA |
882 | } |
883 | ||
87b72777 | 884 | return ret; |
d0ca268b JA |
885 | } |
886 | ||
d5396421 | 887 | static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci) |
d0ca268b JA |
888 | { |
889 | int w = t->action & BLK_TC_ACT(BLK_TC_WRITE); | |
7997c5b0 | 890 | int act = t->action & 0xffff; |
d0ca268b | 891 | |
7997c5b0 | 892 | switch (act) { |
d0ca268b | 893 | case __BLK_TA_QUEUE: |
152f6476 | 894 | account_q(t, pci, w); |
3639a11e | 895 | log_queue(pci, t, "Q"); |
d0ca268b JA |
896 | break; |
897 | case __BLK_TA_BACKMERGE: | |
152f6476 | 898 | account_m(t, pci, w); |
3639a11e | 899 | log_merge(pci, t, "M"); |
d0ca268b JA |
900 | break; |
901 | case __BLK_TA_FRONTMERGE: | |
152f6476 | 902 | account_m(t, pci, w); |
3639a11e | 903 | log_merge(pci, t, "F"); |
d0ca268b JA |
904 | break; |
905 | case __BLK_TA_GETRQ: | |
95c15013 | 906 | log_track_getrq(t); |
3639a11e | 907 | log_generic(pci, t, "G"); |
d0ca268b JA |
908 | break; |
909 | case __BLK_TA_SLEEPRQ: | |
3639a11e | 910 | log_generic(pci, t, "S"); |
d0ca268b JA |
911 | break; |
912 | case __BLK_TA_REQUEUE: | |
152f6476 | 913 | account_c(t, pci, w, -t->bytes); |
3639a11e | 914 | log_queue(pci, t, "R"); |
d0ca268b JA |
915 | break; |
916 | case __BLK_TA_ISSUE: | |
152f6476 | 917 | account_i(t, pci, w); |
3639a11e | 918 | log_issue(pci, t, "D"); |
d0ca268b JA |
919 | break; |
920 | case __BLK_TA_COMPLETE: | |
152f6476 | 921 | account_c(t, pci, w, t->bytes); |
3639a11e | 922 | log_complete(pci, t, "C"); |
d0ca268b | 923 | break; |
88b1a526 | 924 | case __BLK_TA_PLUG: |
3639a11e | 925 | log_action(pci, t, "P"); |
88b1a526 | 926 | break; |
3639a11e | 927 | case __BLK_TA_UNPLUG_IO: |
06639b27 | 928 | account_unplug(t, pci, 0); |
3639a11e JA |
929 | log_unplug(pci, t, "U"); |
930 | break; | |
931 | case __BLK_TA_UNPLUG_TIMER: | |
06639b27 | 932 | account_unplug(t, pci, 1); |
3639a11e | 933 | log_unplug(pci, t, "UT"); |
88b1a526 | 934 | break; |
d0ca268b JA |
935 | default: |
936 | fprintf(stderr, "Bad fs action %x\n", t->action); | |
1f79c4a0 | 937 | break; |
d0ca268b | 938 | } |
d0ca268b JA |
939 | } |
940 | ||
e7c9f3ff NS |
941 | static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci, |
942 | struct per_dev_info *pdi) | |
d0ca268b | 943 | { |
87b72777 JA |
944 | int ret = 0; |
945 | ||
d0ca268b | 946 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) |
d5396421 | 947 | ret = dump_trace_pc(t, pci); |
d0ca268b | 948 | else |
d5396421 | 949 | dump_trace_fs(t, pci); |
87b72777 | 950 | |
e7c9f3ff | 951 | pdi->events++; |
87b72777 | 952 | return ret; |
d0ca268b JA |
953 | } |
954 | ||
152f6476 | 955 | static void dump_io_stats(struct io_stats *ios, char *msg) |
5c017e4b | 956 | { |
152f6476 JA |
957 | fprintf(ofp, "%s\n", msg); |
958 | ||
959 | fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb); | |
960 | fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb); | |
0a6b8fc4 | 961 | |
152f6476 JA |
962 | fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb); |
963 | fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb); | |
964 | fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb); | |
965 | fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb); | |
966 | fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' '); | |
152f6476 | 967 | fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites); |
06639b27 JA |
968 | fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' '); |
969 | fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs); | |
5c017e4b JA |
970 | } |
971 | ||
50adc0ba JA |
972 | static void dump_wait_stats(struct per_process_info *ppi) |
973 | { | |
b9d40d6f JA |
974 | unsigned long rawait = ppi->longest_allocation_wait[0] / 1000; |
975 | unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000; | |
976 | unsigned long rcwait = ppi->longest_completion_wait[0] / 1000; | |
977 | unsigned long wawait = ppi->longest_allocation_wait[1] / 1000; | |
978 | unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000; | |
979 | unsigned long wcwait = ppi->longest_completion_wait[1] / 1000; | |
980 | ||
981 | fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' '); | |
982 | fprintf(ofp, " Allocation wait: %'8lu\n", wawait); | |
983 | fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' '); | |
984 | fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait); | |
985 | fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' '); | |
986 | fprintf(ofp, " Completion wait: %'8lu\n", wcwait); | |
50adc0ba JA |
987 | } |
988 | ||
152f6476 JA |
989 | static void show_process_stats(void) |
990 | { | |
991 | struct per_process_info *ppi; | |
992 | ||
993 | ppi = ppi_list; | |
994 | while (ppi) { | |
995 | dump_io_stats(&ppi->io_stats, ppi->name); | |
50adc0ba | 996 | dump_wait_stats(ppi); |
152f6476 JA |
997 | ppi = ppi->list_next; |
998 | } | |
999 | ||
1000 | fprintf(ofp, "\n"); | |
1001 | } | |
1002 | ||
e7c9f3ff | 1003 | static void show_device_and_cpu_stats(void) |
d0ca268b | 1004 | { |
e7c9f3ff NS |
1005 | struct per_dev_info *pdi; |
1006 | struct per_cpu_info *pci; | |
1007 | struct io_stats total, *ios; | |
1008 | int i, j, pci_events; | |
1009 | char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3]; | |
1010 | char name[32]; | |
1011 | ||
1012 | for (pdi = devices, i = 0; i < ndevices; i++, pdi++) { | |
1013 | ||
1014 | memset(&total, 0, sizeof(total)); | |
1015 | pci_events = 0; | |
1016 | ||
1017 | if (i > 0) | |
1018 | fprintf(ofp, "\n"); | |
1019 | ||
1020 | for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) { | |
1021 | if (!pci->nelems) | |
1022 | continue; | |
1023 | ||
1024 | ios = &pci->io_stats; | |
1025 | total.qreads += ios->qreads; | |
1026 | total.qwrites += ios->qwrites; | |
1027 | total.creads += ios->creads; | |
1028 | total.cwrites += ios->cwrites; | |
1029 | total.mreads += ios->mreads; | |
1030 | total.mwrites += ios->mwrites; | |
1031 | total.ireads += ios->ireads; | |
1032 | total.iwrites += ios->iwrites; | |
1033 | total.qread_kb += ios->qread_kb; | |
1034 | total.qwrite_kb += ios->qwrite_kb; | |
1035 | total.cread_kb += ios->cread_kb; | |
1036 | total.cwrite_kb += ios->cwrite_kb; | |
1037 | total.iread_kb += ios->iread_kb; | |
1038 | total.iwrite_kb += ios->iwrite_kb; | |
06639b27 JA |
1039 | total.timer_unplugs += ios->timer_unplugs; |
1040 | total.io_unplugs += ios->io_unplugs; | |
e7c9f3ff NS |
1041 | |
1042 | snprintf(line, sizeof(line) - 1, "CPU%d (%s):", | |
1043 | j, get_dev_name(pdi, name, sizeof(name))); | |
1044 | dump_io_stats(ios, line); | |
1045 | pci_events++; | |
1046 | } | |
5c017e4b | 1047 | |
e7c9f3ff NS |
1048 | if (pci_events > 1) { |
1049 | fprintf(ofp, "\n"); | |
1050 | snprintf(line, sizeof(line) - 1, "Total (%s):", | |
1051 | get_dev_name(pdi, name, sizeof(name))); | |
1052 | dump_io_stats(&total, line); | |
1053 | } | |
d0ca268b | 1054 | |
e7c9f3ff NS |
1055 | fprintf(ofp, "Events (%s): %'Lu\n", |
1056 | get_dev_name(pdi, line, sizeof(line)), pdi->events); | |
1057 | } | |
d0ca268b JA |
1058 | } |
1059 | ||
2ff323b0 JA |
1060 | static struct blk_io_trace *find_trace(void *p, unsigned long offset, int nr) |
1061 | { | |
1062 | unsigned long max_offset = min(offset,nr * sizeof(struct blk_io_trace)); | |
1063 | unsigned long off; | |
1064 | struct blk_io_trace *bit; | |
1065 | __u32 magic; | |
1066 | ||
1067 | for (off = 0; off < max_offset; off++) { | |
1068 | bit = p + off; | |
1069 | ||
1070 | magic = be32_to_cpu(bit->magic); | |
1071 | if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) | |
1072 | return bit; | |
1073 | } | |
1074 | ||
1075 | return NULL; | |
1076 | } | |
1077 | ||
e7c9f3ff NS |
1078 | static int sort_entries(void *traces, unsigned long offset, int nr, |
1079 | struct per_dev_info *fpdi, struct per_cpu_info *fpci) | |
8fc0abbc | 1080 | { |
e7c9f3ff | 1081 | struct per_dev_info *pdi; |
412819ce | 1082 | struct per_cpu_info *pci; |
8fc0abbc JA |
1083 | struct blk_io_trace *bit; |
1084 | struct trace *t; | |
1085 | void *start = traces; | |
8fc0abbc | 1086 | |
6fe4709e | 1087 | while (traces - start <= offset - sizeof(*bit)) { |
412819ce JA |
1088 | if (!nr) |
1089 | break; | |
1090 | ||
2ff323b0 JA |
1091 | bit = find_trace(traces, offset - (traces - start), nr); |
1092 | if (!bit) | |
1093 | break; | |
6fe4709e | 1094 | |
8fc0abbc | 1095 | t = malloc(sizeof(*t)); |
e7c9f3ff NS |
1096 | if (!t) { |
1097 | fprintf(stderr, "Out of memory, seq %d on dev %d,%d\n", | |
1098 | bit->sequence, | |
1099 | MAJOR(bit->device), MINOR(bit->device)); | |
1100 | return -1; | |
1101 | } | |
8fc0abbc JA |
1102 | t->bit = bit; |
1103 | memset(&t->rb_node, 0, sizeof(t->rb_node)); | |
1104 | ||
6fe4709e JA |
1105 | trace_to_cpu(bit); |
1106 | ||
e7c9f3ff NS |
1107 | if (verify_trace(bit)) { |
1108 | free(t); | |
66fa7233 | 1109 | break; |
e7c9f3ff | 1110 | } |
66fa7233 | 1111 | |
e7c9f3ff NS |
1112 | pdi = fpdi ? fpdi : get_dev_info(bit->device, 1); |
1113 | pdi->id = bit->device; | |
1114 | pci = fpci ? fpci : get_cpu_info(pdi, bit->cpu); | |
1115 | pci->cpu = bit->cpu; | |
412819ce JA |
1116 | pci->nelems++; |
1117 | ||
e7c9f3ff NS |
1118 | if (trace_rb_insert(t)) { |
1119 | free(t); | |
8fc0abbc | 1120 | return -1; |
e7c9f3ff | 1121 | } |
8fc0abbc JA |
1122 | |
1123 | traces += sizeof(*bit) + bit->pdu_len; | |
412819ce | 1124 | nr--; |
6fe4709e | 1125 | } |
8fc0abbc | 1126 | |
e7c9f3ff | 1127 | return 0; |
8fc0abbc JA |
1128 | } |
1129 | ||
412819ce JA |
1130 | static void free_entries_rb(void) |
1131 | { | |
1132 | struct rb_node *n; | |
1133 | ||
7997c5b0 | 1134 | while ((n = rb_first(&rb_sort_root)) != NULL) { |
412819ce JA |
1135 | struct trace *t = rb_entry(n, struct trace, rb_node); |
1136 | ||
7997c5b0 | 1137 | rb_erase(&t->rb_node, &rb_sort_root); |
412819ce JA |
1138 | free(t); |
1139 | } | |
1140 | } | |
1141 | ||
d5396421 | 1142 | static void show_entries_rb(void) |
8fc0abbc | 1143 | { |
e7c9f3ff | 1144 | struct per_dev_info *pdi; |
8fc0abbc | 1145 | struct blk_io_trace *bit; |
3aabcd89 | 1146 | struct rb_node *n; |
8fc0abbc JA |
1147 | struct trace *t; |
1148 | int cpu; | |
1149 | ||
7997c5b0 | 1150 | n = rb_first(&rb_sort_root); |
3aabcd89 JA |
1151 | if (!n) |
1152 | return; | |
8fc0abbc | 1153 | |
3aabcd89 | 1154 | do { |
8fc0abbc JA |
1155 | t = rb_entry(n, struct trace, rb_node); |
1156 | bit = t->bit; | |
1157 | ||
e7c9f3ff NS |
1158 | pdi = get_dev_info(bit->device, 0); |
1159 | if (!pdi) { | |
1160 | fprintf(stderr, "Unknown device ID? (%d,%d)\n", | |
1161 | MAJOR(bit->device), MINOR(bit->device)); | |
1162 | break; | |
1163 | } | |
d5396421 | 1164 | cpu = bit->cpu; |
e7c9f3ff NS |
1165 | if (cpu > pdi->ncpus) { |
1166 | fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n", | |
1167 | cpu, MAJOR(bit->device), MINOR(bit->device)); | |
87b72777 | 1168 | break; |
8fc0abbc JA |
1169 | } |
1170 | ||
cfab07eb | 1171 | bit->time -= genesis_time; |
46e6968b NS |
1172 | if (bit->time < stopwatch_start) |
1173 | continue; | |
1174 | if (bit->time >= stopwatch_end) | |
1175 | break; | |
8fc0abbc | 1176 | |
e7c9f3ff | 1177 | check_time(pdi, bit); |
8fc0abbc | 1178 | |
e7c9f3ff | 1179 | if (dump_trace(bit, &pdi->cpus[cpu], pdi)) |
87b72777 JA |
1180 | break; |
1181 | ||
8fc0abbc JA |
1182 | } while ((n = rb_next(n)) != NULL); |
1183 | } | |
1184 | ||
1f79c4a0 JA |
1185 | static int read_data(int fd, void *buffer, int bytes, int block) |
1186 | { | |
1187 | int ret, bytes_left, fl; | |
1188 | void *p; | |
1189 | ||
1190 | fl = fcntl(fd, F_GETFL); | |
1191 | ||
1192 | if (!block) | |
1193 | fcntl(fd, F_SETFL, fl | O_NONBLOCK); | |
1194 | else | |
1195 | fcntl(fd, F_SETFL, fl & ~O_NONBLOCK); | |
1196 | ||
1197 | bytes_left = bytes; | |
1198 | p = buffer; | |
1199 | while (bytes_left > 0) { | |
1200 | ret = read(fd, p, bytes_left); | |
1201 | if (!ret) | |
1202 | return 1; | |
1203 | else if (ret < 0) { | |
1204 | if (errno != EAGAIN) | |
1205 | perror("read"); | |
1206 | return -1; | |
1207 | } else { | |
1208 | p += ret; | |
1209 | bytes_left -= ret; | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | return 0; | |
1214 | } | |
1215 | ||
d5396421 | 1216 | static int do_file(void) |
d0ca268b | 1217 | { |
e7c9f3ff NS |
1218 | struct per_dev_info *pdi; |
1219 | int i, j, nfiles = 0; | |
d0ca268b | 1220 | |
e7c9f3ff NS |
1221 | for (pdi = devices, i = 0; i < ndevices; i++, pdi++) { |
1222 | for (j = 0;; j++, nfiles++) { | |
1223 | struct per_cpu_info *pci; | |
1224 | struct stat st; | |
1225 | void *tb; | |
87b72777 | 1226 | |
e7c9f3ff NS |
1227 | pci = get_cpu_info(pdi, j); |
1228 | pci->cpu = j; | |
d0ca268b | 1229 | |
e7c9f3ff NS |
1230 | snprintf(pci->fname, sizeof(pci->fname)-1, |
1231 | "%s_out.%d", pdi->name, j); | |
1232 | if (stat(pci->fname, &st) < 0) | |
1233 | break; | |
1234 | if (!st.st_size) | |
1235 | continue; | |
1236 | ||
1237 | printf("Processing %s\n", pci->fname); | |
1238 | ||
1239 | tb = malloc(st.st_size); | |
1240 | if (!tb) { | |
1241 | fprintf(stderr, "Out of memory, skip file %s\n", | |
1242 | pci->fname); | |
1243 | continue; | |
1244 | } | |
1245 | ||
1246 | pci->fd = open(pci->fname, O_RDONLY); | |
1247 | if (pci->fd < 0) { | |
1248 | perror(pci->fname); | |
1249 | free(tb); | |
1250 | continue; | |
1251 | } | |
1252 | ||
1253 | if (read_data(pci->fd, tb, st.st_size, 1)) { | |
1254 | close(pci->fd); | |
1255 | free(tb); | |
1256 | continue; | |
1257 | } | |
1258 | ||
1259 | if (sort_entries(tb, st.st_size, ~0U, pdi, pci) == -1) { | |
1260 | close(pci->fd); | |
1261 | free(tb); | |
1262 | continue; | |
1263 | } | |
1264 | ||
1265 | printf("Completed %s (CPU%d %d, entries)\n", | |
1266 | pci->fname, j, pci->nelems); | |
1267 | close(pci->fd); | |
d0ca268b | 1268 | } |
d5396421 JA |
1269 | } |
1270 | ||
1271 | if (!nfiles) { | |
1272 | fprintf(stderr, "No files found\n"); | |
1273 | return 1; | |
1274 | } | |
1275 | ||
1276 | show_entries_rb(); | |
d5396421 JA |
1277 | return 0; |
1278 | } | |
1279 | ||
2ff323b0 | 1280 | static void resize_buffer(void **buffer, long *size, long offset) |
d5396421 | 1281 | { |
2ff323b0 | 1282 | long old_size = *size; |
d5396421 | 1283 | |
51128a28 JA |
1284 | if (*size == 0) |
1285 | *size = 64 * sizeof(struct blk_io_trace); | |
1286 | ||
2ff323b0 JA |
1287 | *size *= 2; |
1288 | *buffer = realloc(*buffer, *size); | |
51128a28 JA |
1289 | |
1290 | if (old_size) | |
1291 | memset(*buffer + offset, 0, *size - old_size); | |
412819ce | 1292 | } |
d5396421 | 1293 | |
0a94916b | 1294 | static int read_sort_events(int fd, void **buffer, long *max_offset) |
412819ce | 1295 | { |
0a94916b | 1296 | long offset; |
412819ce | 1297 | int events; |
d5396421 | 1298 | |
51128a28 | 1299 | events = offset = 0; |
412819ce JA |
1300 | do { |
1301 | struct blk_io_trace *t; | |
1302 | int pdu_len; | |
51128a28 | 1303 | __u32 magic; |
d5396421 | 1304 | |
0a94916b JA |
1305 | if (*max_offset - offset < sizeof(*t)) |
1306 | resize_buffer(buffer, max_offset, offset); | |
d5396421 | 1307 | |
c80c18a7 JA |
1308 | if (read_data(fd, *buffer + offset, sizeof(*t), !events)) |
1309 | break; | |
d5396421 | 1310 | |
412819ce JA |
1311 | t = *buffer + offset; |
1312 | offset += sizeof(*t); | |
d5396421 | 1313 | |
51128a28 JA |
1314 | magic = be32_to_cpu(t->magic); |
1315 | if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { | |
1316 | fprintf(stderr, "Bad magic %x\n", magic); | |
1317 | break; | |
1318 | } | |
1319 | ||
412819ce | 1320 | pdu_len = be16_to_cpu(t->pdu_len); |
2ff323b0 | 1321 | if (pdu_len) { |
51128a28 | 1322 | if (*max_offset - offset <= pdu_len) |
0a94916b | 1323 | resize_buffer(buffer, max_offset, offset); |
d5396421 | 1324 | |
2ff323b0 JA |
1325 | if (read_data(fd, *buffer + offset, pdu_len, 1)) |
1326 | break; | |
d5396421 | 1327 | |
2ff323b0 JA |
1328 | offset += pdu_len; |
1329 | } | |
d5396421 | 1330 | |
412819ce | 1331 | events++; |
79f19470 | 1332 | } while (!is_done() && events < rb_batch); |
d5396421 | 1333 | |
412819ce JA |
1334 | return events; |
1335 | } | |
d5396421 | 1336 | |
412819ce JA |
1337 | static int do_stdin(void) |
1338 | { | |
1339 | int fd; | |
51128a28 | 1340 | void *ptr = NULL; |
0a94916b | 1341 | long max_offset; |
d5396421 | 1342 | |
1f79c4a0 | 1343 | fd = dup(STDIN_FILENO); |
0a94916b | 1344 | max_offset = 0; |
412819ce JA |
1345 | do { |
1346 | int events; | |
d5396421 | 1347 | |
0a94916b | 1348 | events = read_sort_events(fd, &ptr, &max_offset); |
412819ce JA |
1349 | if (!events) |
1350 | break; | |
1351 | ||
e7c9f3ff | 1352 | if (sort_entries(ptr, ~0UL, events, NULL, NULL) == -1) |
2ff323b0 JA |
1353 | break; |
1354 | ||
412819ce JA |
1355 | show_entries_rb(); |
1356 | free_entries_rb(); | |
d5396421 JA |
1357 | } while (1); |
1358 | ||
51128a28 JA |
1359 | if (ptr) |
1360 | free(ptr); | |
1361 | ||
d5396421 | 1362 | close(fd); |
d5396421 JA |
1363 | return 0; |
1364 | } | |
d0ca268b | 1365 | |
1f79c4a0 | 1366 | static void flush_output(void) |
412819ce | 1367 | { |
152f6476 | 1368 | fflush(ofp); |
412819ce JA |
1369 | } |
1370 | ||
1f79c4a0 | 1371 | static void handle_sigint(int sig) |
412819ce JA |
1372 | { |
1373 | done = 1; | |
1374 | flush_output(); | |
1375 | } | |
1376 | ||
46e6968b NS |
1377 | /* |
1378 | * Extract start and duration times from a string, allowing | |
1379 | * us to specify a time interval of interest within a trace. | |
1380 | * Format: "duration" (start is zero) or "start:duration". | |
1381 | */ | |
1382 | static int find_stopwatch_interval(char *string) | |
1383 | { | |
1384 | double value; | |
1385 | char *sp; | |
1386 | ||
1387 | value = strtod(string, &sp); | |
1388 | if (sp == string) { | |
1389 | fprintf(stderr,"Invalid stopwatch timer: %s\n", string); | |
1390 | return 1; | |
1391 | } | |
1392 | if (*sp == ':') { | |
1393 | stopwatch_start = DOUBLE_TO_NANO_ULL(value); | |
1394 | string = sp + 1; | |
1395 | value = strtod(string, &sp); | |
1396 | if (sp == string || *sp != '\0') { | |
1397 | fprintf(stderr,"Invalid stopwatch duration time: %s\n", | |
1398 | string); | |
1399 | return 1; | |
1400 | } | |
1401 | } else if (*sp != '\0') { | |
1402 | fprintf(stderr,"Invalid stopwatch start timer: %s\n", string); | |
1403 | return 1; | |
1404 | } | |
1405 | stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value); | |
1406 | return 0; | |
1407 | } | |
1408 | ||
1f79c4a0 JA |
1409 | static void usage(char *prog) |
1410 | { | |
46e6968b NS |
1411 | fprintf(stderr, "Usage: %s " |
1412 | "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n", | |
1413 | prog); | |
1f79c4a0 JA |
1414 | } |
1415 | ||
d5396421 JA |
1416 | int main(int argc, char *argv[]) |
1417 | { | |
152f6476 | 1418 | char *ofp_buffer; |
a66877e6 | 1419 | int c, ret, mode; |
1e1c60f1 | 1420 | int per_device_and_cpu_stats = 1; |
d5396421 JA |
1421 | |
1422 | while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) { | |
1423 | switch (c) { | |
1424 | case 'i': | |
e7c9f3ff NS |
1425 | if (!strcmp(optarg, "-") && !pipeline) |
1426 | pipeline = 1; | |
1427 | else if (resize_devices(optarg) != 0) | |
1428 | return 1; | |
d5396421 JA |
1429 | break; |
1430 | case 'o': | |
66efebf8 | 1431 | output_name = optarg; |
d5396421 | 1432 | break; |
79f19470 JA |
1433 | case 'b': |
1434 | rb_batch = atoi(optarg); | |
1435 | if (rb_batch <= 0) | |
1436 | rb_batch = RB_BATCH_DEFAULT; | |
1437 | break; | |
152f6476 JA |
1438 | case 's': |
1439 | per_process_stats = 1; | |
1440 | break; | |
7997c5b0 JA |
1441 | case 't': |
1442 | track_ios = 1; | |
1443 | break; | |
1e1c60f1 NS |
1444 | case 'q': |
1445 | per_device_and_cpu_stats = 0; | |
1446 | break; | |
46e6968b NS |
1447 | case 'w': |
1448 | if (find_stopwatch_interval(optarg) != 0) | |
1449 | return 1; | |
1450 | break; | |
d5396421 | 1451 | default: |
1f79c4a0 | 1452 | usage(argv[0]); |
d5396421 JA |
1453 | return 1; |
1454 | } | |
d0ca268b JA |
1455 | } |
1456 | ||
e7c9f3ff NS |
1457 | while (optind < argc) { |
1458 | if (!strcmp(argv[optind], "-") && !pipeline) | |
1459 | pipeline = 1; | |
1460 | else if (resize_devices(argv[optind]) != 0) | |
1461 | return 1; | |
1462 | optind++; | |
1463 | } | |
1464 | ||
1465 | if (!pipeline && !ndevices) { | |
1f79c4a0 | 1466 | usage(argv[0]); |
d5396421 JA |
1467 | return 1; |
1468 | } | |
1469 | ||
7997c5b0 JA |
1470 | memset(&rb_sort_root, 0, sizeof(rb_sort_root)); |
1471 | memset(&rb_track_root, 0, sizeof(rb_track_root)); | |
412819ce JA |
1472 | |
1473 | signal(SIGINT, handle_sigint); | |
1474 | signal(SIGHUP, handle_sigint); | |
1475 | signal(SIGTERM, handle_sigint); | |
d5396421 | 1476 | |
d69db225 JA |
1477 | setlocale(LC_NUMERIC, "en_US"); |
1478 | ||
a66877e6 | 1479 | if (!output_name) { |
152f6476 | 1480 | ofp = fdopen(STDOUT_FILENO, "w"); |
a66877e6 JA |
1481 | mode = _IOLBF; |
1482 | } else { | |
152f6476 JA |
1483 | char ofname[128]; |
1484 | ||
1485 | snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name); | |
1486 | ofp = fopen(ofname, "w"); | |
a66877e6 | 1487 | mode = _IOFBF; |
152f6476 JA |
1488 | } |
1489 | ||
1490 | if (!ofp) { | |
1491 | perror("fopen"); | |
1492 | return 1; | |
1493 | } | |
1494 | ||
1495 | ofp_buffer = malloc(4096); | |
a66877e6 | 1496 | if (setvbuf(ofp, ofp_buffer, mode, 4096)) { |
152f6476 JA |
1497 | perror("setvbuf"); |
1498 | return 1; | |
1499 | } | |
1500 | ||
e7c9f3ff | 1501 | if (pipeline) |
d5396421 JA |
1502 | ret = do_stdin(); |
1503 | else | |
1504 | ret = do_file(); | |
1505 | ||
152f6476 JA |
1506 | if (per_process_stats) |
1507 | show_process_stats(); | |
1508 | ||
1e1c60f1 NS |
1509 | if (per_device_and_cpu_stats) |
1510 | show_device_and_cpu_stats(); | |
152f6476 | 1511 | |
412819ce | 1512 | flush_output(); |
d5396421 | 1513 | return ret; |
d0ca268b | 1514 | } |