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