Commit | Line | Data |
---|---|---|
40bafa33 JA |
1 | #include <stdio.h> |
2 | #include <stdio.h> | |
3 | #include <unistd.h> | |
4 | #include <inttypes.h> | |
5 | #include <assert.h> | |
6 | ||
7 | #include "../io_ddir.h" | |
8 | #include "../flist.h" | |
9 | #include "../hash.h" | |
10 | #include "../fifo.h" | |
11 | #include "../blktrace_api.h" | |
12 | #include "../os/os.h" | |
13 | #include "../log.h" | |
14 | #include "../lib/linux-dev-lookup.h" | |
15 | ||
16 | #define TRACE_FIFO_SIZE 8192 | |
17 | ||
18 | static unsigned int rt_threshold = 1000000; | |
19 | static unsigned int ios_threshold = 10; | |
20 | static int output_ascii = 1; | |
35661615 | 21 | static char *filename; |
40bafa33 JA |
22 | |
23 | struct bs { | |
24 | unsigned int bs; | |
25 | unsigned int nr; | |
26 | int merges; | |
27 | }; | |
28 | ||
29 | struct trace_file { | |
30 | char *name; | |
31 | int major, minor; | |
32 | }; | |
33 | ||
34 | struct btrace_out { | |
35 | unsigned long ios[DDIR_RWDIR_CNT]; | |
36 | unsigned long rw_bs[DDIR_RWDIR_CNT]; | |
37 | unsigned long merges[DDIR_RWDIR_CNT]; | |
38 | ||
39 | uint64_t last_end[DDIR_RWDIR_CNT]; | |
40 | uint64_t seq[DDIR_RWDIR_CNT]; | |
41 | ||
42 | struct bs *bs[DDIR_RWDIR_CNT]; | |
43 | unsigned int nr_bs[DDIR_RWDIR_CNT]; | |
44 | ||
45 | int inflight; | |
46 | unsigned int depth; | |
47 | uint64_t first_ttime; | |
48 | uint64_t last_ttime; | |
cea475cd | 49 | uint64_t kb; |
40bafa33 | 50 | |
40bafa33 JA |
51 | uint64_t start_delay; |
52 | }; | |
53 | ||
54 | struct btrace_pid { | |
55 | struct flist_head hash_list; | |
56 | struct flist_head pid_list; | |
57 | pid_t pid; | |
65ff9cd5 JA |
58 | |
59 | struct trace_file *files; | |
60 | int nr_files; | |
61 | unsigned int last_major, last_minor; | |
62 | ||
40bafa33 JA |
63 | struct btrace_out o; |
64 | }; | |
65 | ||
66 | struct inflight { | |
67 | struct flist_head list; | |
68 | struct btrace_pid *p; | |
69 | uint64_t end_sector; | |
70 | }; | |
71 | ||
72 | #define PID_HASH_BITS 10 | |
73 | #define PID_HASH_SIZE (1U << PID_HASH_BITS) | |
74 | ||
75 | static struct flist_head pid_hash[PID_HASH_SIZE]; | |
76 | static FLIST_HEAD(pid_list); | |
77 | ||
17bf0853 JA |
78 | #define INFLIGHT_HASH_BITS 8 |
79 | #define INFLIGHT_HASH_SIZE (1U << INFLIGHT_HASH_BITS) | |
80 | static struct flist_head inflight_hash[INFLIGHT_HASH_SIZE]; | |
40bafa33 JA |
81 | |
82 | static uint64_t first_ttime = -1ULL; | |
83 | ||
84 | static struct inflight *inflight_find(uint64_t sector) | |
85 | { | |
17bf0853 | 86 | struct flist_head *inflight_list; |
40bafa33 JA |
87 | struct flist_head *e; |
88 | ||
17bf0853 JA |
89 | inflight_list = &inflight_hash[hash_long(sector, INFLIGHT_HASH_BITS)]; |
90 | ||
91 | flist_for_each(e, inflight_list) { | |
40bafa33 JA |
92 | struct inflight *i = flist_entry(e, struct inflight, list); |
93 | ||
94 | if (i->end_sector == sector) | |
95 | return i; | |
96 | } | |
97 | ||
98 | return NULL; | |
99 | } | |
100 | ||
101 | static void inflight_remove(struct inflight *i) | |
102 | { | |
103 | struct btrace_out *o = &i->p->o; | |
104 | ||
105 | o->inflight--; | |
106 | assert(o->inflight >= 0); | |
107 | flist_del(&i->list); | |
108 | free(i); | |
109 | } | |
110 | ||
17bf0853 | 111 | static void __inflight_add(struct inflight *i) |
40bafa33 | 112 | { |
17bf0853 JA |
113 | struct flist_head *list; |
114 | ||
115 | list = &inflight_hash[hash_long(i->end_sector, INFLIGHT_HASH_BITS)]; | |
116 | flist_add_tail(&i->list, list); | |
40bafa33 JA |
117 | } |
118 | ||
119 | static void inflight_add(struct btrace_pid *p, uint64_t sector, uint32_t len) | |
120 | { | |
121 | struct btrace_out *o = &p->o; | |
122 | struct inflight *i; | |
123 | ||
124 | i = calloc(1, sizeof(*i)); | |
125 | i->p = p; | |
126 | o->inflight++; | |
127 | o->depth = max((int) o->depth, o->inflight); | |
128 | i->end_sector = sector + (len >> 9); | |
17bf0853 JA |
129 | __inflight_add(i); |
130 | } | |
131 | ||
132 | static void inflight_merge(struct inflight *i, int rw, unsigned int size) | |
133 | { | |
134 | i->p->o.merges[rw]++; | |
135 | if (size) { | |
136 | i->end_sector += (size >> 9); | |
137 | flist_del(&i->list); | |
138 | __inflight_add(i); | |
139 | } | |
40bafa33 JA |
140 | } |
141 | ||
142 | /* | |
143 | * fifo refill frontend, to avoid reading data in trace sized bites | |
144 | */ | |
145 | static int refill_fifo(struct fifo *fifo, int fd) | |
146 | { | |
147 | char buf[TRACE_FIFO_SIZE]; | |
148 | unsigned int total; | |
149 | int ret; | |
150 | ||
151 | total = sizeof(buf); | |
152 | if (total > fifo_room(fifo)) | |
153 | total = fifo_room(fifo); | |
154 | ||
155 | ret = read(fd, buf, total); | |
156 | if (ret < 0) { | |
157 | perror("read refill"); | |
158 | return -1; | |
159 | } | |
160 | ||
161 | if (ret > 0) | |
162 | ret = fifo_put(fifo, buf, ret); | |
163 | ||
164 | return ret; | |
165 | } | |
166 | ||
167 | /* | |
168 | * Retrieve 'len' bytes from the fifo, refilling if necessary. | |
169 | */ | |
170 | static int trace_fifo_get(struct fifo *fifo, int fd, void *buf, | |
171 | unsigned int len) | |
172 | { | |
173 | if (fifo_len(fifo) < len) { | |
174 | int ret = refill_fifo(fifo, fd); | |
175 | ||
176 | if (ret < 0) | |
177 | return ret; | |
178 | } | |
179 | ||
180 | return fifo_get(fifo, buf, len); | |
181 | } | |
182 | ||
183 | /* | |
184 | * Just discard the pdu by seeking past it. | |
185 | */ | |
186 | static int discard_pdu(struct fifo *fifo, int fd, struct blk_io_trace *t) | |
187 | { | |
188 | if (t->pdu_len == 0) | |
189 | return 0; | |
190 | ||
191 | return trace_fifo_get(fifo, fd, NULL, t->pdu_len); | |
192 | } | |
193 | ||
194 | static void handle_trace_notify(struct blk_io_trace *t) | |
195 | { | |
196 | switch (t->action) { | |
197 | case BLK_TN_PROCESS: | |
198 | //printf("got process notify: %x, %d\n", t->action, t->pid); | |
199 | break; | |
200 | case BLK_TN_TIMESTAMP: | |
201 | //printf("got timestamp notify: %x, %d\n", t->action, t->pid); | |
202 | break; | |
203 | case BLK_TN_MESSAGE: | |
204 | break; | |
205 | default: | |
17bf0853 | 206 | log_err("unknown trace act %x\n", t->action); |
40bafa33 JA |
207 | break; |
208 | } | |
209 | } | |
210 | ||
211 | static void __add_bs(struct btrace_out *o, unsigned int len, int rw) | |
212 | { | |
213 | o->bs[rw] = realloc(o->bs[rw], (o->nr_bs[rw] + 1) * sizeof(struct bs)); | |
214 | o->bs[rw][o->nr_bs[rw]].bs = len; | |
215 | o->bs[rw][o->nr_bs[rw]].nr = 1; | |
216 | o->nr_bs[rw]++; | |
217 | } | |
218 | ||
219 | static void add_bs(struct btrace_out *o, unsigned int len, int rw) | |
220 | { | |
221 | struct bs *bs = o->bs[rw]; | |
222 | int i; | |
223 | ||
224 | if (!o->nr_bs[rw]) { | |
225 | __add_bs(o, len, rw); | |
226 | return; | |
227 | } | |
228 | ||
229 | for (i = 0; i < o->nr_bs[rw]; i++) { | |
230 | if (bs[i].bs == len) { | |
231 | bs[i].nr++; | |
232 | return; | |
233 | } | |
234 | } | |
235 | ||
236 | __add_bs(o, len, rw); | |
237 | } | |
238 | ||
239 | #define FMINORBITS 20 | |
240 | #define FMINORMASK ((1U << FMINORBITS) - 1) | |
241 | #define FMAJOR(dev) ((unsigned int) ((dev) >> FMINORBITS)) | |
242 | #define FMINOR(dev) ((unsigned int) ((dev) & FMINORMASK)) | |
243 | ||
65ff9cd5 | 244 | static void btrace_add_file(struct btrace_pid *p, uint32_t devno) |
40bafa33 JA |
245 | { |
246 | unsigned int maj = FMAJOR(devno); | |
247 | unsigned int min = FMINOR(devno); | |
248 | struct trace_file *f; | |
249 | unsigned int i; | |
250 | char dev[256]; | |
251 | ||
35661615 JA |
252 | if (filename) |
253 | return; | |
65ff9cd5 | 254 | if (p->last_major == maj && p->last_minor == min) |
40bafa33 JA |
255 | return; |
256 | ||
65ff9cd5 JA |
257 | p->last_major = maj; |
258 | p->last_minor = min; | |
40bafa33 JA |
259 | |
260 | /* | |
261 | * check for this file in our list | |
262 | */ | |
65ff9cd5 JA |
263 | for (i = 0; i < p->nr_files; i++) { |
264 | f = &p->files[i]; | |
40bafa33 JA |
265 | |
266 | if (f->major == maj && f->minor == min) | |
267 | return; | |
268 | } | |
269 | ||
270 | strcpy(dev, "/dev"); | |
271 | if (!blktrace_lookup_device(NULL, dev, maj, min)) { | |
272 | log_err("fio: failed to find device %u/%u\n", maj, min); | |
273 | return; | |
274 | } | |
275 | ||
65ff9cd5 JA |
276 | p->files = realloc(p->files, (p->nr_files + 1) * sizeof(*f)); |
277 | f = &p->files[p->nr_files]; | |
40bafa33 JA |
278 | f->name = strdup(dev); |
279 | f->major = maj; | |
280 | f->minor = min; | |
65ff9cd5 | 281 | p->nr_files++; |
40bafa33 JA |
282 | } |
283 | ||
65ff9cd5 | 284 | static void handle_trace_discard(struct blk_io_trace *t, struct btrace_pid *p) |
40bafa33 | 285 | { |
65ff9cd5 JA |
286 | struct btrace_out *o = &p->o; |
287 | ||
288 | btrace_add_file(p, t->device); | |
40bafa33 JA |
289 | |
290 | if (o->first_ttime == -1ULL) | |
291 | o->first_ttime = t->time; | |
292 | ||
293 | o->ios[DDIR_TRIM]++; | |
294 | add_bs(o, t->bytes, DDIR_TRIM); | |
295 | } | |
296 | ||
65ff9cd5 | 297 | static void handle_trace_fs(struct blk_io_trace *t, struct btrace_pid *p) |
40bafa33 | 298 | { |
65ff9cd5 | 299 | struct btrace_out *o = &p->o; |
40bafa33 JA |
300 | int rw; |
301 | ||
65ff9cd5 | 302 | btrace_add_file(p, t->device); |
40bafa33 JA |
303 | |
304 | first_ttime = min(first_ttime, (uint64_t) t->time); | |
305 | ||
306 | if (o->first_ttime == -1ULL) | |
307 | o->first_ttime = t->time; | |
308 | ||
309 | rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; | |
310 | ||
311 | add_bs(o, t->bytes, rw); | |
312 | o->ios[rw]++; | |
313 | ||
314 | if (t->sector == o->last_end[rw] || o->last_end[rw] == -1ULL) | |
315 | o->seq[rw]++; | |
316 | ||
317 | o->last_end[rw] = t->sector + (t->bytes >> 9); | |
318 | } | |
319 | ||
65ff9cd5 | 320 | static void handle_queue_trace(struct blk_io_trace *t, struct btrace_pid *p) |
40bafa33 JA |
321 | { |
322 | if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY)) | |
323 | handle_trace_notify(t); | |
324 | else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD)) | |
65ff9cd5 | 325 | handle_trace_discard(t, p); |
40bafa33 | 326 | else |
65ff9cd5 | 327 | handle_trace_fs(t, p); |
40bafa33 JA |
328 | } |
329 | ||
330 | static void handle_trace(struct blk_io_trace *t, struct btrace_pid *p) | |
331 | { | |
332 | unsigned int act = t->action & 0xffff; | |
333 | ||
334 | if (act == __BLK_TA_QUEUE) { | |
335 | inflight_add(p, t->sector, t->bytes); | |
65ff9cd5 | 336 | handle_queue_trace(t, p); |
40bafa33 JA |
337 | } else if (act == __BLK_TA_REQUEUE) { |
338 | p->o.inflight--; | |
339 | } else if (act == __BLK_TA_BACKMERGE) { | |
340 | struct inflight *i; | |
341 | ||
342 | i = inflight_find(t->sector + (t->bytes >> 9)); | |
343 | if (i) | |
344 | inflight_remove(i); | |
345 | ||
346 | i = inflight_find(t->sector); | |
347 | if (i) { | |
348 | int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; | |
349 | ||
350 | inflight_merge(i, rw, t->bytes); | |
351 | } | |
352 | } else if (act == __BLK_TA_FRONTMERGE) { | |
353 | struct inflight *i; | |
354 | ||
355 | i = inflight_find(t->sector + (t->bytes >> 9)); | |
356 | if (i) | |
357 | inflight_remove(i); | |
358 | ||
359 | i = inflight_find(t->sector); | |
360 | if (i) { | |
361 | int rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; | |
362 | ||
363 | inflight_merge(i, rw, 0); | |
364 | } | |
365 | } else if (act == __BLK_TA_COMPLETE) { | |
366 | struct inflight *i; | |
367 | ||
368 | i = inflight_find(t->sector + (t->bytes >> 9)); | |
cea475cd JA |
369 | if (i) { |
370 | i->p->o.kb += (t->bytes >> 10); | |
40bafa33 | 371 | inflight_remove(i); |
cea475cd | 372 | } |
40bafa33 JA |
373 | } |
374 | } | |
375 | ||
376 | static void byteswap_trace(struct blk_io_trace *t) | |
377 | { | |
378 | t->magic = fio_swap32(t->magic); | |
379 | t->sequence = fio_swap32(t->sequence); | |
380 | t->time = fio_swap64(t->time); | |
381 | t->sector = fio_swap64(t->sector); | |
382 | t->bytes = fio_swap32(t->bytes); | |
383 | t->action = fio_swap32(t->action); | |
384 | t->pid = fio_swap32(t->pid); | |
385 | t->device = fio_swap32(t->device); | |
386 | t->cpu = fio_swap32(t->cpu); | |
387 | t->error = fio_swap16(t->error); | |
388 | t->pdu_len = fio_swap16(t->pdu_len); | |
389 | } | |
390 | ||
391 | static struct btrace_pid *pid_hash_find(pid_t pid, struct flist_head *list) | |
392 | { | |
393 | struct flist_head *e; | |
394 | struct btrace_pid *p; | |
395 | ||
396 | flist_for_each(e, list) { | |
397 | p = flist_entry(e, struct btrace_pid, hash_list); | |
398 | if (p->pid == pid) | |
399 | return p; | |
400 | } | |
401 | ||
402 | return NULL; | |
403 | } | |
404 | ||
405 | static struct btrace_pid *pid_hash_get(pid_t pid) | |
406 | { | |
407 | struct flist_head *hash_list; | |
408 | struct btrace_pid *p; | |
409 | ||
410 | hash_list = &pid_hash[hash_long(pid, PID_HASH_BITS)]; | |
411 | ||
412 | p = pid_hash_find(pid, hash_list); | |
413 | if (!p) { | |
414 | int i; | |
415 | ||
416 | p = calloc(1, sizeof(*p)); | |
417 | p->o.first_ttime = -1ULL; | |
418 | p->o.last_ttime = -1ULL; | |
419 | ||
420 | for (i = 0; i < DDIR_RWDIR_CNT; i++) | |
421 | p->o.last_end[i] = -1ULL; | |
422 | ||
423 | p->pid = pid; | |
424 | flist_add_tail(&p->hash_list, hash_list); | |
425 | flist_add_tail(&p->pid_list, &pid_list); | |
426 | } | |
427 | ||
428 | return p; | |
429 | } | |
430 | ||
431 | /* | |
432 | * Load a blktrace file by reading all the blk_io_trace entries, and storing | |
433 | * them as io_pieces like the fio text version would do. | |
434 | */ | |
435 | static int load_blktrace(const char *filename, int need_swap) | |
436 | { | |
437 | struct btrace_pid *p; | |
438 | unsigned long traces; | |
439 | struct blk_io_trace t; | |
440 | struct fifo *fifo; | |
441 | int fd; | |
442 | ||
443 | fd = open(filename, O_RDONLY); | |
444 | if (fd < 0) { | |
445 | perror("open trace file\n"); | |
446 | return 1; | |
447 | } | |
448 | ||
449 | fifo = fifo_alloc(TRACE_FIFO_SIZE); | |
450 | ||
451 | traces = 0; | |
452 | do { | |
453 | int ret = trace_fifo_get(fifo, fd, &t, sizeof(t)); | |
454 | ||
455 | if (ret < 0) | |
456 | goto err; | |
457 | else if (!ret) | |
458 | break; | |
459 | else if (ret < (int) sizeof(t)) { | |
17bf0853 | 460 | log_err("fio: short fifo get\n"); |
40bafa33 JA |
461 | break; |
462 | } | |
463 | ||
464 | if (need_swap) | |
465 | byteswap_trace(&t); | |
466 | ||
467 | if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { | |
17bf0853 | 468 | log_err("fio: bad magic in blktrace data: %x\n", t.magic); |
40bafa33 JA |
469 | goto err; |
470 | } | |
471 | if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) { | |
17bf0853 | 472 | log_err("fio: bad blktrace version %d\n", t.magic & 0xff); |
40bafa33 JA |
473 | goto err; |
474 | } | |
475 | ret = discard_pdu(fifo, fd, &t); | |
476 | if (ret < 0) { | |
17bf0853 | 477 | log_err("blktrace lseek\n"); |
40bafa33 JA |
478 | goto err; |
479 | } else if (t.pdu_len != ret) { | |
17bf0853 | 480 | log_err("fio: discarded %d of %d\n", ret, t.pdu_len); |
40bafa33 JA |
481 | goto err; |
482 | } | |
483 | ||
484 | p = pid_hash_get(t.pid); | |
485 | handle_trace(&t, p); | |
486 | p->o.last_ttime = t.time; | |
487 | traces++; | |
488 | } while (1); | |
489 | ||
490 | fifo_free(fifo); | |
491 | close(fd); | |
492 | ||
493 | if (output_ascii) | |
494 | printf("Traces loaded: %lu\n", traces); | |
495 | ||
496 | return 0; | |
497 | err: | |
498 | close(fd); | |
499 | fifo_free(fifo); | |
500 | return 1; | |
501 | } | |
502 | ||
503 | static int bs_cmp(const void *ba, const void *bb) | |
504 | { | |
505 | const struct bs *bsa = ba; | |
506 | const struct bs *bsb = bb; | |
507 | ||
508 | return bsb->nr - bsa->nr; | |
509 | } | |
510 | ||
511 | static void __output_p_ascii(struct btrace_pid *p, unsigned long *ios) | |
512 | { | |
513 | const char *msg[] = { "reads", "writes", "trims" }; | |
514 | struct btrace_out *o = &p->o; | |
cea475cd | 515 | unsigned long total, usec; |
40bafa33 JA |
516 | int i, j; |
517 | ||
518 | printf("[pid:\t%u]\n", p->pid); | |
519 | ||
520 | total = ddir_rw_sum(o->ios); | |
521 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { | |
522 | float perc; | |
523 | ||
524 | if (!o->ios[i]) | |
525 | continue; | |
526 | ||
527 | ios[i] += o->ios[i] + o->merges[i]; | |
528 | printf("%s\n", msg[i]); | |
529 | perc = ((float) o->ios[i] * 100.0) / (float) total; | |
530 | printf("\tios: %lu (perc=%3.2f%%)\n", o->ios[i], perc); | |
531 | perc = ((float) o->merges[i] * 100.0) / (float) total; | |
532 | printf("\tmerges: %lu (perc=%3.2f%%)\n", o->merges[i], perc); | |
533 | perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i]; | |
534 | printf("\tseq: %lu (perc=%3.2f%%)\n", o->seq[i], perc); | |
535 | ||
536 | for (j = 0; j < o->nr_bs[i]; j++) { | |
537 | struct bs *bs = &o->bs[i][j]; | |
538 | ||
539 | perc = (((float) bs->nr * 100.0) / (float) o->ios[i]); | |
540 | printf("\tbs=%u, perc=%3.2f%%\n", bs->bs, perc); | |
541 | } | |
542 | } | |
543 | ||
544 | printf("depth:\t%u\n", o->depth); | |
cea475cd JA |
545 | usec = (o->last_ttime - o->first_ttime) / 1000ULL; |
546 | printf("usec:\t%lu (delay=%llu)\n", usec, (unsigned long long) o->start_delay); | |
547 | printf("rate:\t%.2fKB/sec\n", ((float) o->kb * 1000.0) / ((float) usec / 1000.0)); | |
40bafa33 JA |
548 | |
549 | printf("files:\t"); | |
65ff9cd5 JA |
550 | for (i = 0; i < p->nr_files; i++) |
551 | printf("%s,", p->files[i].name); | |
40bafa33 JA |
552 | printf("\n"); |
553 | ||
554 | printf("\n"); | |
555 | } | |
556 | ||
557 | static int __output_p_fio(struct btrace_pid *p, unsigned long *ios) | |
558 | { | |
559 | struct btrace_out *o = &p->o; | |
560 | unsigned long total; | |
1a8cad44 | 561 | unsigned long long time; |
40bafa33 JA |
562 | float perc; |
563 | int i, j; | |
564 | ||
565 | if ((o->ios[0] + o->ios[1]) && o->ios[2]) { | |
566 | log_err("fio: trace has both read/write and trim\n"); | |
567 | return 1; | |
568 | } | |
569 | ||
570 | printf("[pid%u]\n", p->pid); | |
571 | printf("direct=1\n"); | |
572 | if (o->depth == 1) | |
573 | printf("ioengine=sync\n"); | |
574 | else | |
575 | printf("ioengine=libaio\niodepth=%u\n", o->depth); | |
576 | ||
577 | if (o->ios[0] && !o->ios[1]) | |
578 | printf("rw=randread\n"); | |
579 | else if (!o->ios[0] && o->ios[1]) | |
580 | printf("rw=randwrite\n"); | |
581 | else if (o->ios[2]) | |
582 | printf("rw=randtrim\n"); | |
583 | else { | |
584 | printf("rw=randrw\n"); | |
585 | total = ddir_rw_sum(o->ios); | |
586 | perc = ((float) o->ios[0] * 100.0) / (float) total; | |
587 | printf("rwmixread=%u\n", (int) (perc + 0.99)); | |
588 | } | |
589 | ||
35661615 | 590 | printf("percentage_random="); |
40bafa33 JA |
591 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
592 | if (o->seq[i] && o->ios[i]) { | |
593 | perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i]; | |
594 | if (perc >= 99.0) | |
595 | perc = 100.0; | |
596 | } else | |
597 | perc = 100.0; | |
598 | ||
599 | if (i) | |
600 | printf(","); | |
35661615 | 601 | perc = 100.0 - perc; |
40bafa33 JA |
602 | printf("%u", (int) perc); |
603 | } | |
604 | printf("\n"); | |
605 | ||
606 | printf("filename="); | |
65ff9cd5 | 607 | for (i = 0; i < p->nr_files; i++) { |
40bafa33 JA |
608 | if (i) |
609 | printf(":"); | |
65ff9cd5 | 610 | printf("%s", p->files[i].name); |
40bafa33 JA |
611 | } |
612 | printf("\n"); | |
613 | ||
614 | printf("startdelay=%llus\n", o->start_delay / 1000000ULL); | |
615 | ||
1a8cad44 JA |
616 | time = o->last_ttime - o->first_ttime; |
617 | time = (time + 1000000000ULL - 1) / 1000000000ULL; | |
618 | printf("runtime=%llus\n", time); | |
619 | ||
40bafa33 JA |
620 | printf("bssplit="); |
621 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { | |
622 | ||
623 | if (i && o->nr_bs[i - 1] && o->nr_bs[i]) | |
624 | printf(","); | |
625 | ||
626 | for (j = 0; j < o->nr_bs[i]; j++) { | |
627 | struct bs *bs = &o->bs[i][j]; | |
628 | ||
629 | perc = (((float) bs->nr * 100.0) / (float) o->ios[i]); | |
630 | if (perc < 1.00) | |
631 | continue; | |
632 | if (j) | |
633 | printf(":"); | |
634 | if (j + 1 == o->nr_bs[i]) | |
635 | printf("%u/", bs->bs); | |
636 | else | |
637 | printf("%u/%u", bs->bs, (int) perc); | |
638 | } | |
639 | } | |
640 | printf("\n\n"); | |
641 | ||
642 | return 0; | |
643 | } | |
644 | ||
645 | static int __output_p(struct btrace_pid *p, unsigned long *ios) | |
646 | { | |
647 | struct btrace_out *o = &p->o; | |
648 | int i, ret = 0; | |
649 | ||
650 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { | |
651 | if (o->nr_bs[i] <= 1) | |
652 | continue; | |
653 | qsort(o->bs[i], o->nr_bs[i], sizeof(struct bs), bs_cmp); | |
654 | } | |
655 | ||
35661615 | 656 | if (filename) { |
65ff9cd5 JA |
657 | p->files = malloc(sizeof(struct trace_file)); |
658 | p->nr_files++; | |
659 | p->files[0].name = filename; | |
35661615 JA |
660 | } |
661 | ||
40bafa33 JA |
662 | if (output_ascii) |
663 | __output_p_ascii(p, ios); | |
664 | else | |
665 | ret = __output_p_fio(p, ios); | |
666 | ||
667 | return ret; | |
668 | } | |
669 | ||
670 | static int prune_entry(struct btrace_out *o) | |
671 | { | |
672 | uint64_t time; | |
673 | ||
674 | if (ddir_rw_sum(o->ios) < ios_threshold) | |
675 | return 1; | |
676 | ||
677 | time = (o->last_ttime - o->first_ttime) / 1000ULL; | |
678 | if (time < rt_threshold) | |
679 | return 1; | |
680 | ||
681 | return 0; | |
682 | } | |
683 | ||
684 | static int entry_cmp(void *priv, struct flist_head *a, struct flist_head *b) | |
685 | { | |
686 | struct btrace_pid *pa = flist_entry(a, struct btrace_pid, pid_list); | |
687 | struct btrace_pid *pb = flist_entry(b, struct btrace_pid, pid_list); | |
688 | ||
689 | return ddir_rw_sum(pb->o.ios) - ddir_rw_sum(pa->o.ios); | |
690 | } | |
691 | ||
17bf0853 JA |
692 | static void free_p(struct btrace_pid *p) |
693 | { | |
694 | struct btrace_out *o = &p->o; | |
695 | int i; | |
696 | ||
65ff9cd5 JA |
697 | for (i = 0; i < p->nr_files; i++) { |
698 | if (p->files[i].name && p->files[i].name != filename) | |
699 | free(p->files[i].name); | |
17bf0853 JA |
700 | } |
701 | ||
702 | for (i = 0; i < DDIR_RWDIR_CNT; i++) | |
703 | free(o->bs[i]); | |
704 | ||
65ff9cd5 | 705 | free(p->files); |
17bf0853 JA |
706 | flist_del(&p->pid_list); |
707 | flist_del(&p->hash_list); | |
708 | free(p); | |
709 | } | |
710 | ||
40bafa33 JA |
711 | static int output_p(void) |
712 | { | |
713 | unsigned long ios[DDIR_RWDIR_CNT]; | |
714 | struct flist_head *e, *tmp; | |
715 | int ret = 0; | |
716 | ||
717 | flist_for_each_safe(e, tmp, &pid_list) { | |
718 | struct btrace_pid *p; | |
719 | ||
720 | p = flist_entry(e, struct btrace_pid, pid_list); | |
721 | if (prune_entry(&p->o)) { | |
17bf0853 | 722 | free_p(p); |
40bafa33 JA |
723 | continue; |
724 | } | |
725 | p->o.start_delay = (p->o.first_ttime / 1000ULL) - first_ttime; | |
726 | } | |
727 | ||
728 | memset(ios, 0, sizeof(ios)); | |
729 | ||
730 | flist_sort(NULL, &pid_list, entry_cmp); | |
731 | ||
732 | flist_for_each(e, &pid_list) { | |
733 | struct btrace_pid *p; | |
734 | ||
735 | p = flist_entry(e, struct btrace_pid, pid_list); | |
736 | ret |= __output_p(p, ios); | |
737 | } | |
738 | ||
739 | if (output_ascii) | |
740 | printf("Total: reads=%lu, writes=%lu\n", ios[0], ios[1]); | |
741 | ||
742 | return ret; | |
743 | } | |
744 | ||
745 | static int usage(char *argv[]) | |
746 | { | |
17bf0853 JA |
747 | log_err("%s: <blktrace bin file>\n", argv[0]); |
748 | log_err("\t-t\tUsec threshold to ignore task\n"); | |
749 | log_err("\t-n\tNumber IOS threshold to ignore task\n"); | |
750 | log_err("\t-f\tFio job file output\n"); | |
751 | log_err("\t-d\tUse this file/device for replay\n"); | |
40bafa33 JA |
752 | return 1; |
753 | } | |
754 | ||
17bf0853 | 755 | static int trace_needs_swap(const char *trace_file, int *swap) |
40bafa33 | 756 | { |
40bafa33 | 757 | struct blk_io_trace t; |
17bf0853 JA |
758 | int fd, ret; |
759 | ||
760 | *swap = -1; | |
761 | ||
762 | fd = open(trace_file, O_RDONLY); | |
763 | if (fd < 0) { | |
764 | perror("open"); | |
765 | return 1; | |
766 | } | |
767 | ||
768 | ret = read(fd, &t, sizeof(t)); | |
769 | if (ret < 0) { | |
770 | perror("read"); | |
771 | return 1; | |
772 | } else if (ret != sizeof(t)) { | |
773 | log_err("fio: short read on trace file\n"); | |
774 | return 1; | |
775 | } | |
776 | ||
777 | close(fd); | |
778 | ||
779 | if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) | |
780 | *swap = 0; | |
781 | else { | |
782 | /* | |
783 | * Maybe it needs to be endian swapped... | |
784 | */ | |
785 | t.magic = fio_swap32(t.magic); | |
786 | if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) | |
787 | *swap = 1; | |
788 | } | |
789 | ||
790 | if (*swap == -1) { | |
791 | log_err("fio: blktrace appears corrupt\n"); | |
792 | return 1; | |
793 | } | |
794 | ||
795 | return 0; | |
796 | } | |
797 | ||
798 | int main(int argc, char *argv[]) | |
799 | { | |
800 | int need_swap, i, c; | |
40bafa33 JA |
801 | |
802 | if (argc < 2) | |
803 | return usage(argv); | |
804 | ||
35661615 | 805 | while ((c = getopt(argc, argv, "t:n:fd:")) != -1) { |
40bafa33 JA |
806 | switch (c) { |
807 | case 't': | |
808 | rt_threshold = atoi(optarg); | |
809 | break; | |
810 | case 'n': | |
811 | ios_threshold = atoi(optarg); | |
812 | break; | |
813 | case 'f': | |
814 | output_ascii = 0; | |
815 | break; | |
35661615 JA |
816 | case 'd': |
817 | filename = strdup(optarg); | |
818 | break; | |
40bafa33 JA |
819 | case '?': |
820 | default: | |
821 | return usage(argv); | |
822 | } | |
823 | } | |
824 | ||
825 | if (argc == optind) | |
826 | return usage(argv); | |
827 | ||
17bf0853 | 828 | if (trace_needs_swap(argv[optind], &need_swap)) |
40bafa33 | 829 | return 1; |
40bafa33 JA |
830 | |
831 | for (i = 0; i < PID_HASH_SIZE; i++) | |
832 | INIT_FLIST_HEAD(&pid_hash[i]); | |
17bf0853 JA |
833 | for (i = 0; i < INFLIGHT_HASH_SIZE; i++) |
834 | INIT_FLIST_HEAD(&inflight_hash[i]); | |
40bafa33 JA |
835 | |
836 | load_blktrace(argv[optind], need_swap); | |
837 | first_ttime /= 1000ULL; | |
838 | ||
839 | return output_p(); | |
840 | } |