| 1 | /* |
| 2 | * Code related to writing an iolog of what a thread is doing, and to |
| 3 | * later read that back and replay |
| 4 | */ |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <assert.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <unistd.h> |
| 11 | #ifdef CONFIG_ZLIB |
| 12 | #include <zlib.h> |
| 13 | #endif |
| 14 | |
| 15 | #include "flist.h" |
| 16 | #include "fio.h" |
| 17 | #include "trim.h" |
| 18 | #include "filelock.h" |
| 19 | #include "smalloc.h" |
| 20 | #include "blktrace.h" |
| 21 | #include "pshared.h" |
| 22 | #include "lib/roundup.h" |
| 23 | |
| 24 | #include <netinet/in.h> |
| 25 | #include <netinet/tcp.h> |
| 26 | #include <arpa/inet.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/socket.h> |
| 29 | #include <sys/un.h> |
| 30 | |
| 31 | static int iolog_flush(struct io_log *log); |
| 32 | |
| 33 | static const char iolog_ver2[] = "fio version 2 iolog"; |
| 34 | static const char iolog_ver3[] = "fio version 3 iolog"; |
| 35 | |
| 36 | void queue_io_piece(struct thread_data *td, struct io_piece *ipo) |
| 37 | { |
| 38 | flist_add_tail(&ipo->list, &td->io_log_list); |
| 39 | td->total_io_size += ipo->len; |
| 40 | } |
| 41 | |
| 42 | void log_io_u(const struct thread_data *td, const struct io_u *io_u) |
| 43 | { |
| 44 | struct timespec now; |
| 45 | |
| 46 | if (!td->o.write_iolog_file) |
| 47 | return; |
| 48 | |
| 49 | fio_gettime(&now, NULL); |
| 50 | fprintf(td->iolog_f, "%llu %s %s %llu %llu\n", |
| 51 | (unsigned long long) utime_since_now(&td->io_log_start_time), |
| 52 | io_u->file->file_name, io_ddir_name(io_u->ddir), io_u->offset, |
| 53 | io_u->buflen); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | void log_file(struct thread_data *td, struct fio_file *f, |
| 58 | enum file_log_act what) |
| 59 | { |
| 60 | const char *act[] = { "add", "open", "close" }; |
| 61 | struct timespec now; |
| 62 | |
| 63 | assert(what < 3); |
| 64 | |
| 65 | if (!td->o.write_iolog_file) |
| 66 | return; |
| 67 | |
| 68 | |
| 69 | /* |
| 70 | * this happens on the pre-open/close done before the job starts |
| 71 | */ |
| 72 | if (!td->iolog_f) |
| 73 | return; |
| 74 | |
| 75 | fio_gettime(&now, NULL); |
| 76 | fprintf(td->iolog_f, "%llu %s %s\n", |
| 77 | (unsigned long long) utime_since_now(&td->io_log_start_time), |
| 78 | f->file_name, act[what]); |
| 79 | } |
| 80 | |
| 81 | static void iolog_delay(struct thread_data *td, unsigned long delay) |
| 82 | { |
| 83 | uint64_t usec = utime_since_now(&td->last_issue); |
| 84 | unsigned long orig_delay = delay; |
| 85 | struct timespec ts; |
| 86 | int ret = 0; |
| 87 | |
| 88 | if (delay < td->time_offset) { |
| 89 | td->time_offset = 0; |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | delay -= td->time_offset; |
| 94 | if (delay < usec) |
| 95 | return; |
| 96 | |
| 97 | delay -= usec; |
| 98 | |
| 99 | fio_gettime(&ts, NULL); |
| 100 | |
| 101 | while (delay && !td->terminate) { |
| 102 | ret = io_u_queued_complete(td, 0); |
| 103 | if (ret < 0) |
| 104 | td_verror(td, -ret, "io_u_queued_complete"); |
| 105 | if (td->flags & TD_F_REGROW_LOGS) |
| 106 | regrow_logs(td); |
| 107 | if (utime_since_now(&ts) > delay) |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | usec = utime_since_now(&ts); |
| 112 | if (usec > orig_delay) |
| 113 | td->time_offset = usec - orig_delay; |
| 114 | else |
| 115 | td->time_offset = 0; |
| 116 | } |
| 117 | |
| 118 | static int ipo_special(struct thread_data *td, struct io_piece *ipo) |
| 119 | { |
| 120 | struct fio_file *f; |
| 121 | int ret; |
| 122 | |
| 123 | /* |
| 124 | * Not a special ipo |
| 125 | */ |
| 126 | if (ipo->ddir != DDIR_INVAL) |
| 127 | return 0; |
| 128 | |
| 129 | f = td->files[ipo->fileno]; |
| 130 | |
| 131 | if (ipo->delay) |
| 132 | iolog_delay(td, ipo->delay); |
| 133 | if (fio_fill_issue_time(td)) |
| 134 | fio_gettime(&td->last_issue, NULL); |
| 135 | switch (ipo->file_action) { |
| 136 | case FIO_LOG_OPEN_FILE: |
| 137 | if (td->o.replay_redirect && fio_file_open(f)) { |
| 138 | dprint(FD_FILE, "iolog: ignoring re-open of file %s\n", |
| 139 | f->file_name); |
| 140 | break; |
| 141 | } |
| 142 | ret = td_io_open_file(td, f); |
| 143 | if (!ret) |
| 144 | break; |
| 145 | td_verror(td, ret, "iolog open file"); |
| 146 | return -1; |
| 147 | case FIO_LOG_CLOSE_FILE: |
| 148 | td_io_close_file(td, f); |
| 149 | break; |
| 150 | case FIO_LOG_UNLINK_FILE: |
| 151 | td_io_unlink_file(td, f); |
| 152 | break; |
| 153 | case FIO_LOG_ADD_FILE: |
| 154 | /* |
| 155 | * Nothing to do |
| 156 | */ |
| 157 | break; |
| 158 | default: |
| 159 | log_err("fio: bad file action %d\n", ipo->file_action); |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | return 1; |
| 164 | } |
| 165 | |
| 166 | static bool read_iolog(struct thread_data *td); |
| 167 | |
| 168 | unsigned long long delay_since_ttime(const struct thread_data *td, |
| 169 | unsigned long long time) |
| 170 | { |
| 171 | double tmp; |
| 172 | double scale; |
| 173 | const unsigned long long *last_ttime = &td->io_log_last_ttime; |
| 174 | |
| 175 | if (!*last_ttime || td->o.no_stall || time < *last_ttime) |
| 176 | return 0; |
| 177 | else if (td->o.replay_time_scale == 100) |
| 178 | return time - *last_ttime; |
| 179 | |
| 180 | |
| 181 | scale = (double) 100.0 / (double) td->o.replay_time_scale; |
| 182 | tmp = time - *last_ttime; |
| 183 | return tmp * scale; |
| 184 | } |
| 185 | |
| 186 | int read_iolog_get(struct thread_data *td, struct io_u *io_u) |
| 187 | { |
| 188 | struct io_piece *ipo; |
| 189 | unsigned long elapsed; |
| 190 | |
| 191 | while (!flist_empty(&td->io_log_list)) { |
| 192 | int ret; |
| 193 | |
| 194 | if (td->o.read_iolog_chunked) { |
| 195 | if (td->io_log_checkmark == td->io_log_current) { |
| 196 | if (td->io_log_blktrace) { |
| 197 | if (!read_blktrace(td)) |
| 198 | return 1; |
| 199 | } else { |
| 200 | if (!read_iolog(td)) |
| 201 | return 1; |
| 202 | } |
| 203 | } |
| 204 | td->io_log_current--; |
| 205 | } |
| 206 | ipo = flist_first_entry(&td->io_log_list, struct io_piece, list); |
| 207 | flist_del(&ipo->list); |
| 208 | remove_trim_entry(td, ipo); |
| 209 | |
| 210 | ret = ipo_special(td, ipo); |
| 211 | if (ret < 0) { |
| 212 | free(ipo); |
| 213 | break; |
| 214 | } else if (ret > 0) { |
| 215 | free(ipo); |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | io_u->ddir = ipo->ddir; |
| 220 | if (ipo->ddir != DDIR_WAIT) { |
| 221 | io_u->offset = ipo->offset; |
| 222 | io_u->verify_offset = ipo->offset; |
| 223 | io_u->buflen = ipo->len; |
| 224 | io_u->file = td->files[ipo->fileno]; |
| 225 | get_file(io_u->file); |
| 226 | dprint(FD_IO, "iolog: get %llu/%llu/%s\n", io_u->offset, |
| 227 | io_u->buflen, io_u->file->file_name); |
| 228 | if (ipo->delay) |
| 229 | iolog_delay(td, ipo->delay); |
| 230 | } else { |
| 231 | elapsed = mtime_since_genesis(); |
| 232 | if (ipo->delay > elapsed) |
| 233 | usec_sleep(td, (ipo->delay - elapsed) * 1000); |
| 234 | } |
| 235 | |
| 236 | free(ipo); |
| 237 | |
| 238 | if (io_u->ddir != DDIR_WAIT) |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | td->done = 1; |
| 243 | return 1; |
| 244 | } |
| 245 | |
| 246 | void prune_io_piece_log(struct thread_data *td) |
| 247 | { |
| 248 | struct io_piece *ipo; |
| 249 | struct fio_rb_node *n; |
| 250 | |
| 251 | while ((n = rb_first(&td->io_hist_tree)) != NULL) { |
| 252 | ipo = rb_entry(n, struct io_piece, rb_node); |
| 253 | rb_erase(n, &td->io_hist_tree); |
| 254 | remove_trim_entry(td, ipo); |
| 255 | td->io_hist_len--; |
| 256 | free(ipo); |
| 257 | } |
| 258 | |
| 259 | while (!flist_empty(&td->io_hist_list)) { |
| 260 | ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list); |
| 261 | flist_del(&ipo->list); |
| 262 | remove_trim_entry(td, ipo); |
| 263 | td->io_hist_len--; |
| 264 | free(ipo); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * log a successful write, so we can unwind the log for verify |
| 270 | */ |
| 271 | void log_io_piece(struct thread_data *td, struct io_u *io_u) |
| 272 | { |
| 273 | struct fio_rb_node **p, *parent; |
| 274 | struct io_piece *ipo, *__ipo; |
| 275 | |
| 276 | ipo = calloc(1, sizeof(struct io_piece)); |
| 277 | init_ipo(ipo); |
| 278 | ipo->file = io_u->file; |
| 279 | ipo->offset = io_u->offset; |
| 280 | ipo->len = io_u->buflen; |
| 281 | ipo->numberio = io_u->numberio; |
| 282 | ipo->flags = IP_F_IN_FLIGHT; |
| 283 | |
| 284 | io_u->ipo = ipo; |
| 285 | |
| 286 | if (io_u_should_trim(td, io_u)) { |
| 287 | flist_add_tail(&ipo->trim_list, &td->trim_list); |
| 288 | td->trim_entries++; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Only sort writes if we don't have a random map in which case we need |
| 293 | * to check for duplicate blocks and drop the old one, which we rely on |
| 294 | * the rb insert/lookup for handling. |
| 295 | */ |
| 296 | if (file_randommap(td, ipo->file)) { |
| 297 | INIT_FLIST_HEAD(&ipo->list); |
| 298 | flist_add_tail(&ipo->list, &td->io_hist_list); |
| 299 | ipo->flags |= IP_F_ONLIST; |
| 300 | td->io_hist_len++; |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | RB_CLEAR_NODE(&ipo->rb_node); |
| 305 | |
| 306 | /* |
| 307 | * Sort the entry into the verification list |
| 308 | */ |
| 309 | restart: |
| 310 | p = &td->io_hist_tree.rb_node; |
| 311 | parent = NULL; |
| 312 | while (*p) { |
| 313 | int overlap = 0; |
| 314 | parent = *p; |
| 315 | |
| 316 | __ipo = rb_entry(parent, struct io_piece, rb_node); |
| 317 | if (ipo->file < __ipo->file) |
| 318 | p = &(*p)->rb_left; |
| 319 | else if (ipo->file > __ipo->file) |
| 320 | p = &(*p)->rb_right; |
| 321 | else if (ipo->offset < __ipo->offset) { |
| 322 | p = &(*p)->rb_left; |
| 323 | overlap = ipo->offset + ipo->len > __ipo->offset; |
| 324 | } |
| 325 | else if (ipo->offset > __ipo->offset) { |
| 326 | p = &(*p)->rb_right; |
| 327 | overlap = __ipo->offset + __ipo->len > ipo->offset; |
| 328 | } |
| 329 | else |
| 330 | overlap = 1; |
| 331 | |
| 332 | if (overlap) { |
| 333 | dprint(FD_IO, "iolog: overlap %llu/%lu, %llu/%lu\n", |
| 334 | __ipo->offset, __ipo->len, |
| 335 | ipo->offset, ipo->len); |
| 336 | td->io_hist_len--; |
| 337 | rb_erase(parent, &td->io_hist_tree); |
| 338 | remove_trim_entry(td, __ipo); |
| 339 | if (!(__ipo->flags & IP_F_IN_FLIGHT)) |
| 340 | free(__ipo); |
| 341 | goto restart; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | rb_link_node(&ipo->rb_node, parent, p); |
| 346 | rb_insert_color(&ipo->rb_node, &td->io_hist_tree); |
| 347 | ipo->flags |= IP_F_ONRB; |
| 348 | td->io_hist_len++; |
| 349 | } |
| 350 | |
| 351 | void unlog_io_piece(struct thread_data *td, struct io_u *io_u) |
| 352 | { |
| 353 | struct io_piece *ipo = io_u->ipo; |
| 354 | |
| 355 | if (td->ts.nr_block_infos) { |
| 356 | uint32_t *info = io_u_block_info(td, io_u); |
| 357 | if (BLOCK_INFO_STATE(*info) < BLOCK_STATE_TRIM_FAILURE) { |
| 358 | if (io_u->ddir == DDIR_TRIM) |
| 359 | *info = BLOCK_INFO_SET_STATE(*info, |
| 360 | BLOCK_STATE_TRIM_FAILURE); |
| 361 | else if (io_u->ddir == DDIR_WRITE) |
| 362 | *info = BLOCK_INFO_SET_STATE(*info, |
| 363 | BLOCK_STATE_WRITE_FAILURE); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if (!ipo) |
| 368 | return; |
| 369 | |
| 370 | if (ipo->flags & IP_F_ONRB) |
| 371 | rb_erase(&ipo->rb_node, &td->io_hist_tree); |
| 372 | else if (ipo->flags & IP_F_ONLIST) |
| 373 | flist_del(&ipo->list); |
| 374 | |
| 375 | free(ipo); |
| 376 | io_u->ipo = NULL; |
| 377 | td->io_hist_len--; |
| 378 | } |
| 379 | |
| 380 | void trim_io_piece(const struct io_u *io_u) |
| 381 | { |
| 382 | struct io_piece *ipo = io_u->ipo; |
| 383 | |
| 384 | if (!ipo) |
| 385 | return; |
| 386 | |
| 387 | ipo->len = io_u->xfer_buflen - io_u->resid; |
| 388 | } |
| 389 | |
| 390 | void write_iolog_close(struct thread_data *td) |
| 391 | { |
| 392 | if (!td->iolog_f) |
| 393 | return; |
| 394 | |
| 395 | fflush(td->iolog_f); |
| 396 | fclose(td->iolog_f); |
| 397 | free(td->iolog_buf); |
| 398 | td->iolog_f = NULL; |
| 399 | td->iolog_buf = NULL; |
| 400 | } |
| 401 | |
| 402 | int64_t iolog_items_to_fetch(struct thread_data *td) |
| 403 | { |
| 404 | struct timespec now; |
| 405 | uint64_t elapsed; |
| 406 | uint64_t for_1s; |
| 407 | int64_t items_to_fetch; |
| 408 | |
| 409 | if (!td->io_log_highmark) |
| 410 | return 10; |
| 411 | |
| 412 | |
| 413 | fio_gettime(&now, NULL); |
| 414 | elapsed = ntime_since(&td->io_log_highmark_time, &now); |
| 415 | if (elapsed) { |
| 416 | for_1s = (td->io_log_highmark - td->io_log_current) * 1000000000 / elapsed; |
| 417 | items_to_fetch = for_1s - td->io_log_current; |
| 418 | if (items_to_fetch < 0) |
| 419 | items_to_fetch = 0; |
| 420 | } else |
| 421 | items_to_fetch = 0; |
| 422 | |
| 423 | td->io_log_highmark = td->io_log_current + items_to_fetch; |
| 424 | td->io_log_checkmark = (td->io_log_highmark + 1) / 2; |
| 425 | fio_gettime(&td->io_log_highmark_time, NULL); |
| 426 | |
| 427 | return items_to_fetch; |
| 428 | } |
| 429 | |
| 430 | #define io_act(_td, _r) (((_td)->io_log_version == 3 && (r) == 5) || \ |
| 431 | ((_td)->io_log_version == 2 && (r) == 4)) |
| 432 | #define file_act(_td, _r) (((_td)->io_log_version == 3 && (r) == 3) || \ |
| 433 | ((_td)->io_log_version == 2 && (r) == 2)) |
| 434 | |
| 435 | /* |
| 436 | * Read version 2 and 3 iolog data. It is enhanced to include per-file logging, |
| 437 | * syncs, etc. |
| 438 | */ |
| 439 | static bool read_iolog(struct thread_data *td) |
| 440 | { |
| 441 | unsigned long long offset; |
| 442 | unsigned int bytes; |
| 443 | unsigned long long delay = 0; |
| 444 | int reads, writes, trims, waits, fileno = 0, file_action = 0; /* stupid gcc */ |
| 445 | char *rfname, *fname, *act; |
| 446 | char *str, *p; |
| 447 | enum fio_ddir rw; |
| 448 | bool realloc = false; |
| 449 | int64_t items_to_fetch = 0; |
| 450 | int syncs; |
| 451 | |
| 452 | if (td->o.read_iolog_chunked) { |
| 453 | items_to_fetch = iolog_items_to_fetch(td); |
| 454 | if (!items_to_fetch) |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Read in the read iolog and store it, reuse the infrastructure |
| 460 | * for doing verifications. |
| 461 | */ |
| 462 | str = malloc(4096); |
| 463 | rfname = fname = malloc(256+16); |
| 464 | act = malloc(256+16); |
| 465 | |
| 466 | syncs = reads = writes = trims = waits = 0; |
| 467 | while ((p = fgets(str, 4096, td->io_log_rfile)) != NULL) { |
| 468 | struct io_piece *ipo; |
| 469 | int r; |
| 470 | unsigned long long ttime; |
| 471 | |
| 472 | if (td->io_log_version == 3) { |
| 473 | r = sscanf(p, "%llu %256s %256s %llu %u", &ttime, rfname, act, |
| 474 | &offset, &bytes); |
| 475 | delay = delay_since_ttime(td, ttime); |
| 476 | td->io_log_last_ttime = ttime; |
| 477 | /* |
| 478 | * "wait" is not allowed with version 3 |
| 479 | */ |
| 480 | if (!strcmp(act, "wait")) { |
| 481 | log_err("iolog: ignoring wait command with" |
| 482 | " version 3 for file %s\n", fname); |
| 483 | continue; |
| 484 | } |
| 485 | } else /* version 2 */ |
| 486 | r = sscanf(p, "%256s %256s %llu %u", rfname, act, &offset, &bytes); |
| 487 | |
| 488 | if (td->o.replay_redirect) |
| 489 | fname = td->o.replay_redirect; |
| 490 | |
| 491 | if (io_act(td, r)) { |
| 492 | /* |
| 493 | * Check action first |
| 494 | */ |
| 495 | if (!strcmp(act, "wait")) |
| 496 | rw = DDIR_WAIT; |
| 497 | else if (!strcmp(act, "read")) { |
| 498 | if (td->o.replay_skip & (1u << DDIR_READ)) |
| 499 | continue; |
| 500 | rw = DDIR_READ; |
| 501 | } else if (!strcmp(act, "write")) { |
| 502 | if (td->o.replay_skip & (1u << DDIR_WRITE)) |
| 503 | continue; |
| 504 | rw = DDIR_WRITE; |
| 505 | } else if (!strcmp(act, "sync")) { |
| 506 | if (td->o.replay_skip & (1u << DDIR_SYNC)) |
| 507 | continue; |
| 508 | rw = DDIR_SYNC; |
| 509 | } else if (!strcmp(act, "datasync")) |
| 510 | rw = DDIR_DATASYNC; |
| 511 | else if (!strcmp(act, "trim")) { |
| 512 | if (td->o.replay_skip & (1u << DDIR_TRIM)) |
| 513 | continue; |
| 514 | rw = DDIR_TRIM; |
| 515 | } else { |
| 516 | log_err("fio: bad iolog file action: %s\n", |
| 517 | act); |
| 518 | continue; |
| 519 | } |
| 520 | fileno = get_fileno(td, fname); |
| 521 | } else if (file_act(td, r)) { |
| 522 | rw = DDIR_INVAL; |
| 523 | if (!strcmp(act, "add")) { |
| 524 | if (td->o.replay_redirect && |
| 525 | get_fileno(td, fname) != -1) { |
| 526 | dprint(FD_FILE, "iolog: ignoring" |
| 527 | " re-add of file %s\n", fname); |
| 528 | } else { |
| 529 | fileno = add_file(td, fname, td->subjob_number, 1); |
| 530 | file_action = FIO_LOG_ADD_FILE; |
| 531 | } |
| 532 | } else if (!strcmp(act, "open")) { |
| 533 | fileno = get_fileno(td, fname); |
| 534 | file_action = FIO_LOG_OPEN_FILE; |
| 535 | } else if (!strcmp(act, "close")) { |
| 536 | fileno = get_fileno(td, fname); |
| 537 | file_action = FIO_LOG_CLOSE_FILE; |
| 538 | } else { |
| 539 | log_err("fio: bad iolog file action: %s\n", |
| 540 | act); |
| 541 | continue; |
| 542 | } |
| 543 | } else { |
| 544 | log_err("bad iolog%d: %s\n", td->io_log_version, p); |
| 545 | continue; |
| 546 | } |
| 547 | |
| 548 | if (rw == DDIR_READ) |
| 549 | reads++; |
| 550 | else if (rw == DDIR_WRITE) { |
| 551 | /* |
| 552 | * Don't add a write for ro mode |
| 553 | */ |
| 554 | if (read_only) |
| 555 | continue; |
| 556 | writes++; |
| 557 | } else if (rw == DDIR_TRIM) { |
| 558 | /* |
| 559 | * Don't add a trim for ro mode |
| 560 | */ |
| 561 | if (read_only) |
| 562 | continue; |
| 563 | trims++; |
| 564 | } else if (rw == DDIR_WAIT) { |
| 565 | if (td->o.no_stall) |
| 566 | continue; |
| 567 | waits++; |
| 568 | } else if (rw == DDIR_INVAL) { |
| 569 | } else if (ddir_sync(rw)) { |
| 570 | syncs++; |
| 571 | } else { |
| 572 | log_err("bad ddir: %d\n", rw); |
| 573 | continue; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Make note of file |
| 578 | */ |
| 579 | ipo = calloc(1, sizeof(*ipo)); |
| 580 | init_ipo(ipo); |
| 581 | ipo->ddir = rw; |
| 582 | if (td->io_log_version == 3) |
| 583 | ipo->delay = delay; |
| 584 | if (rw == DDIR_WAIT) { |
| 585 | ipo->delay = offset; |
| 586 | } else { |
| 587 | if (td->o.replay_scale) |
| 588 | ipo->offset = offset / td->o.replay_scale; |
| 589 | else |
| 590 | ipo->offset = offset; |
| 591 | ipo_bytes_align(td->o.replay_align, ipo); |
| 592 | |
| 593 | ipo->len = bytes; |
| 594 | if (rw != DDIR_INVAL && bytes > td->o.max_bs[rw]) { |
| 595 | realloc = true; |
| 596 | td->o.max_bs[rw] = bytes; |
| 597 | } |
| 598 | ipo->fileno = fileno; |
| 599 | ipo->file_action = file_action; |
| 600 | td->o.size += bytes; |
| 601 | } |
| 602 | |
| 603 | queue_io_piece(td, ipo); |
| 604 | |
| 605 | if (td->o.read_iolog_chunked) { |
| 606 | td->io_log_current++; |
| 607 | items_to_fetch--; |
| 608 | if (items_to_fetch == 0) |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | free(str); |
| 614 | free(act); |
| 615 | free(rfname); |
| 616 | |
| 617 | if (td->o.read_iolog_chunked) { |
| 618 | td->io_log_highmark = td->io_log_current; |
| 619 | td->io_log_checkmark = (td->io_log_highmark + 1) / 2; |
| 620 | fio_gettime(&td->io_log_highmark_time, NULL); |
| 621 | } |
| 622 | |
| 623 | if (writes && read_only) { |
| 624 | log_err("fio: <%s> skips replay of %d writes due to" |
| 625 | " read-only\n", td->o.name, writes); |
| 626 | writes = 0; |
| 627 | } |
| 628 | if (syncs) |
| 629 | td->flags |= TD_F_SYNCS; |
| 630 | |
| 631 | if (td->o.read_iolog_chunked) { |
| 632 | if (td->io_log_current == 0) { |
| 633 | return false; |
| 634 | } |
| 635 | td->o.td_ddir = TD_DDIR_RW; |
| 636 | if (realloc && td->orig_buffer) |
| 637 | { |
| 638 | io_u_quiesce(td); |
| 639 | free_io_mem(td); |
| 640 | if (init_io_u_buffers(td)) |
| 641 | return false; |
| 642 | } |
| 643 | return true; |
| 644 | } |
| 645 | |
| 646 | if (!reads && !writes && !waits && !trims) |
| 647 | return false; |
| 648 | |
| 649 | td->o.td_ddir = 0; |
| 650 | if (reads) |
| 651 | td->o.td_ddir |= TD_DDIR_READ; |
| 652 | if (writes) |
| 653 | td->o.td_ddir |= TD_DDIR_WRITE; |
| 654 | if (trims) |
| 655 | td->o.td_ddir |= TD_DDIR_TRIM; |
| 656 | |
| 657 | return true; |
| 658 | } |
| 659 | |
| 660 | static bool is_socket(const char *path) |
| 661 | { |
| 662 | struct stat buf; |
| 663 | int r; |
| 664 | |
| 665 | r = stat(path, &buf); |
| 666 | if (r == -1) |
| 667 | return false; |
| 668 | |
| 669 | return S_ISSOCK(buf.st_mode); |
| 670 | } |
| 671 | |
| 672 | static int open_socket(const char *path) |
| 673 | { |
| 674 | struct sockaddr_un addr; |
| 675 | int ret, fd; |
| 676 | |
| 677 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 678 | if (fd < 0) |
| 679 | return fd; |
| 680 | |
| 681 | addr.sun_family = AF_UNIX; |
| 682 | if (snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path) >= |
| 683 | sizeof(addr.sun_path)) { |
| 684 | log_err("%s: path name %s is too long for a Unix socket\n", |
| 685 | __func__, path); |
| 686 | } |
| 687 | |
| 688 | ret = connect(fd, (const struct sockaddr *)&addr, strlen(path) + sizeof(addr.sun_family)); |
| 689 | if (!ret) |
| 690 | return fd; |
| 691 | |
| 692 | close(fd); |
| 693 | return -1; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * open iolog, check version, and call appropriate parser |
| 698 | */ |
| 699 | static bool init_iolog_read(struct thread_data *td, char *fname) |
| 700 | { |
| 701 | char buffer[256], *p; |
| 702 | FILE *f = NULL; |
| 703 | |
| 704 | dprint(FD_IO, "iolog: name=%s\n", fname); |
| 705 | |
| 706 | if (is_socket(fname)) { |
| 707 | int fd; |
| 708 | |
| 709 | fd = open_socket(fname); |
| 710 | if (fd >= 0) |
| 711 | f = fdopen(fd, "r"); |
| 712 | } else if (!strcmp(fname, "-")) { |
| 713 | f = stdin; |
| 714 | } else |
| 715 | f = fopen(fname, "r"); |
| 716 | |
| 717 | if (!f) { |
| 718 | perror("fopen read iolog"); |
| 719 | return false; |
| 720 | } |
| 721 | |
| 722 | p = fgets(buffer, sizeof(buffer), f); |
| 723 | if (!p) { |
| 724 | td_verror(td, errno, "iolog read"); |
| 725 | log_err("fio: unable to read iolog\n"); |
| 726 | fclose(f); |
| 727 | return false; |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * versions 2 and 3 of the iolog store a specific string as the |
| 732 | * first line, check for that |
| 733 | */ |
| 734 | if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2))) |
| 735 | td->io_log_version = 2; |
| 736 | else if (!strncmp(iolog_ver3, buffer, strlen(iolog_ver3))) |
| 737 | td->io_log_version = 3; |
| 738 | else { |
| 739 | log_err("fio: iolog version 1 is no longer supported\n"); |
| 740 | fclose(f); |
| 741 | return false; |
| 742 | } |
| 743 | |
| 744 | free_release_files(td); |
| 745 | td->io_log_rfile = f; |
| 746 | return read_iolog(td); |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Set up a log for storing io patterns. |
| 751 | */ |
| 752 | static bool init_iolog_write(struct thread_data *td) |
| 753 | { |
| 754 | struct fio_file *ff; |
| 755 | FILE *f; |
| 756 | unsigned int i; |
| 757 | |
| 758 | f = fopen(td->o.write_iolog_file, "a"); |
| 759 | if (!f) { |
| 760 | perror("fopen write iolog"); |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | * That's it for writing, setup a log buffer and we're done. |
| 766 | */ |
| 767 | td->iolog_f = f; |
| 768 | td->iolog_buf = malloc(8192); |
| 769 | setvbuf(f, td->iolog_buf, _IOFBF, 8192); |
| 770 | fio_gettime(&td->io_log_start_time, NULL); |
| 771 | |
| 772 | /* |
| 773 | * write our version line |
| 774 | */ |
| 775 | if (fprintf(f, "%s\n", iolog_ver3) < 0) { |
| 776 | perror("iolog init\n"); |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * add all known files |
| 782 | */ |
| 783 | for_each_file(td, ff, i) |
| 784 | log_file(td, ff, FIO_LOG_ADD_FILE); |
| 785 | |
| 786 | return true; |
| 787 | } |
| 788 | |
| 789 | bool init_iolog(struct thread_data *td) |
| 790 | { |
| 791 | bool ret; |
| 792 | |
| 793 | if (td->o.read_iolog_file) { |
| 794 | int need_swap; |
| 795 | char * fname = get_name_by_idx(td->o.read_iolog_file, td->subjob_number); |
| 796 | |
| 797 | /* |
| 798 | * Check if it's a blktrace file and load that if possible. |
| 799 | * Otherwise assume it's a normal log file and load that. |
| 800 | */ |
| 801 | if (is_blktrace(fname, &need_swap)) { |
| 802 | td->io_log_blktrace = 1; |
| 803 | ret = init_blktrace_read(td, fname, need_swap); |
| 804 | } else { |
| 805 | td->io_log_blktrace = 0; |
| 806 | ret = init_iolog_read(td, fname); |
| 807 | } |
| 808 | free(fname); |
| 809 | } else if (td->o.write_iolog_file) |
| 810 | ret = init_iolog_write(td); |
| 811 | else |
| 812 | ret = true; |
| 813 | |
| 814 | if (!ret) |
| 815 | td_verror(td, EINVAL, "failed initializing iolog"); |
| 816 | |
| 817 | init_disk_util(td); |
| 818 | |
| 819 | return ret; |
| 820 | } |
| 821 | |
| 822 | void setup_log(struct io_log **log, struct log_params *p, |
| 823 | const char *filename) |
| 824 | { |
| 825 | struct io_log *l; |
| 826 | int i; |
| 827 | struct io_u_plat_entry *entry; |
| 828 | struct flist_head *list; |
| 829 | |
| 830 | l = scalloc(1, sizeof(*l)); |
| 831 | INIT_FLIST_HEAD(&l->io_logs); |
| 832 | l->log_type = p->log_type; |
| 833 | l->log_offset = p->log_offset; |
| 834 | l->log_prio = p->log_prio; |
| 835 | l->log_gz = p->log_gz; |
| 836 | l->log_gz_store = p->log_gz_store; |
| 837 | l->avg_msec = p->avg_msec; |
| 838 | l->hist_msec = p->hist_msec; |
| 839 | l->hist_coarseness = p->hist_coarseness; |
| 840 | l->filename = strdup(filename); |
| 841 | l->td = p->td; |
| 842 | |
| 843 | /* Initialize histogram lists for each r/w direction, |
| 844 | * with initial io_u_plat of all zeros: |
| 845 | */ |
| 846 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
| 847 | list = &l->hist_window[i].list; |
| 848 | INIT_FLIST_HEAD(list); |
| 849 | entry = calloc(1, sizeof(struct io_u_plat_entry)); |
| 850 | flist_add(&entry->list, list); |
| 851 | } |
| 852 | |
| 853 | if (l->td && l->td->o.io_submit_mode != IO_MODE_OFFLOAD) { |
| 854 | unsigned int def_samples = DEF_LOG_ENTRIES; |
| 855 | struct io_logs *__p; |
| 856 | |
| 857 | __p = calloc(1, sizeof(*l->pending)); |
| 858 | if (l->td->o.iodepth > DEF_LOG_ENTRIES) |
| 859 | def_samples = roundup_pow2(l->td->o.iodepth); |
| 860 | __p->max_samples = def_samples; |
| 861 | __p->log = calloc(__p->max_samples, log_entry_sz(l)); |
| 862 | l->pending = __p; |
| 863 | } |
| 864 | |
| 865 | if (l->log_offset) |
| 866 | l->log_ddir_mask = LOG_OFFSET_SAMPLE_BIT; |
| 867 | if (l->log_prio) |
| 868 | l->log_ddir_mask |= LOG_PRIO_SAMPLE_BIT; |
| 869 | /* |
| 870 | * The bandwidth-log option generates agg-read_bw.log, |
| 871 | * agg-write_bw.log and agg-trim_bw.log for which l->td is NULL. |
| 872 | * Check if l->td is valid before dereferencing it. |
| 873 | */ |
| 874 | if (l->td && l->td->o.log_max == IO_LOG_SAMPLE_BOTH) |
| 875 | l->log_ddir_mask |= LOG_AVG_MAX_SAMPLE_BIT; |
| 876 | |
| 877 | INIT_FLIST_HEAD(&l->chunk_list); |
| 878 | |
| 879 | if (l->log_gz && !p->td) |
| 880 | l->log_gz = 0; |
| 881 | else if (l->log_gz || l->log_gz_store) { |
| 882 | mutex_init_pshared(&l->chunk_lock); |
| 883 | mutex_init_pshared(&l->deferred_free_lock); |
| 884 | p->td->flags |= TD_F_COMPRESS_LOG; |
| 885 | } |
| 886 | |
| 887 | *log = l; |
| 888 | } |
| 889 | |
| 890 | #ifdef CONFIG_SETVBUF |
| 891 | static void *set_file_buffer(FILE *f) |
| 892 | { |
| 893 | size_t size = 1048576; |
| 894 | void *buf; |
| 895 | |
| 896 | buf = malloc(size); |
| 897 | setvbuf(f, buf, _IOFBF, size); |
| 898 | return buf; |
| 899 | } |
| 900 | |
| 901 | static void clear_file_buffer(void *buf) |
| 902 | { |
| 903 | free(buf); |
| 904 | } |
| 905 | #else |
| 906 | static void *set_file_buffer(FILE *f) |
| 907 | { |
| 908 | return NULL; |
| 909 | } |
| 910 | |
| 911 | static void clear_file_buffer(void *buf) |
| 912 | { |
| 913 | } |
| 914 | #endif |
| 915 | |
| 916 | void free_log(struct io_log *log) |
| 917 | { |
| 918 | while (!flist_empty(&log->io_logs)) { |
| 919 | struct io_logs *cur_log; |
| 920 | |
| 921 | cur_log = flist_first_entry(&log->io_logs, struct io_logs, list); |
| 922 | flist_del_init(&cur_log->list); |
| 923 | free(cur_log->log); |
| 924 | sfree(cur_log); |
| 925 | } |
| 926 | |
| 927 | if (log->pending) { |
| 928 | free(log->pending->log); |
| 929 | free(log->pending); |
| 930 | log->pending = NULL; |
| 931 | } |
| 932 | |
| 933 | free(log->pending); |
| 934 | free(log->filename); |
| 935 | sfree(log); |
| 936 | } |
| 937 | |
| 938 | uint64_t hist_sum(int j, int stride, uint64_t *io_u_plat, |
| 939 | uint64_t *io_u_plat_last) |
| 940 | { |
| 941 | uint64_t sum; |
| 942 | int k; |
| 943 | |
| 944 | if (io_u_plat_last) { |
| 945 | for (k = sum = 0; k < stride; k++) |
| 946 | sum += io_u_plat[j + k] - io_u_plat_last[j + k]; |
| 947 | } else { |
| 948 | for (k = sum = 0; k < stride; k++) |
| 949 | sum += io_u_plat[j + k]; |
| 950 | } |
| 951 | |
| 952 | return sum; |
| 953 | } |
| 954 | |
| 955 | static void flush_hist_samples(FILE *f, int hist_coarseness, void *samples, |
| 956 | uint64_t sample_size) |
| 957 | { |
| 958 | struct io_sample *s; |
| 959 | int log_offset; |
| 960 | uint64_t i, j, nr_samples; |
| 961 | struct io_u_plat_entry *entry, *entry_before; |
| 962 | uint64_t *io_u_plat; |
| 963 | uint64_t *io_u_plat_before; |
| 964 | |
| 965 | int stride = 1 << hist_coarseness; |
| 966 | |
| 967 | if (!sample_size) |
| 968 | return; |
| 969 | |
| 970 | s = __get_sample(samples, 0, 0); |
| 971 | log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0; |
| 972 | |
| 973 | nr_samples = sample_size / __log_entry_sz(log_offset); |
| 974 | |
| 975 | for (i = 0; i < nr_samples; i++) { |
| 976 | s = __get_sample(samples, log_offset, i); |
| 977 | |
| 978 | entry = s->data.plat_entry; |
| 979 | io_u_plat = entry->io_u_plat; |
| 980 | |
| 981 | entry_before = flist_first_entry(&entry->list, struct io_u_plat_entry, list); |
| 982 | io_u_plat_before = entry_before->io_u_plat; |
| 983 | |
| 984 | fprintf(f, "%lu, %u, %llu, ", (unsigned long) s->time, |
| 985 | io_sample_ddir(s), (unsigned long long) s->bs); |
| 986 | for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) { |
| 987 | fprintf(f, "%llu, ", (unsigned long long) |
| 988 | hist_sum(j, stride, io_u_plat, io_u_plat_before)); |
| 989 | } |
| 990 | fprintf(f, "%llu\n", (unsigned long long) |
| 991 | hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, |
| 992 | io_u_plat_before)); |
| 993 | |
| 994 | flist_del(&entry_before->list); |
| 995 | free(entry_before); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | void flush_samples(FILE *f, void *samples, uint64_t sample_size) |
| 1000 | { |
| 1001 | struct io_sample *s; |
| 1002 | int log_offset, log_prio, log_avg_max; |
| 1003 | uint64_t i, nr_samples; |
| 1004 | unsigned int prio_val; |
| 1005 | const char *fmt; |
| 1006 | |
| 1007 | if (!sample_size) |
| 1008 | return; |
| 1009 | |
| 1010 | s = __get_sample(samples, 0, 0); |
| 1011 | log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0; |
| 1012 | log_prio = (s->__ddir & LOG_PRIO_SAMPLE_BIT) != 0; |
| 1013 | log_avg_max = (s->__ddir & LOG_AVG_MAX_SAMPLE_BIT) != 0; |
| 1014 | |
| 1015 | if (log_offset) { |
| 1016 | if (log_prio) { |
| 1017 | if (log_avg_max) |
| 1018 | fmt = "%" PRIu64 ", %" PRId64 ", %" PRId64 ", %u, %llu, %llu, 0x%04x\n"; |
| 1019 | else |
| 1020 | fmt = "%" PRIu64 ", %" PRId64 ", %u, %llu, %llu, 0x%04x\n"; |
| 1021 | } else { |
| 1022 | if (log_avg_max) |
| 1023 | fmt = "%" PRIu64 ", %" PRId64 ", %" PRId64 ", %u, %llu, %llu, %u\n"; |
| 1024 | else |
| 1025 | fmt = "%" PRIu64 ", %" PRId64 ", %u, %llu, %llu, %u\n"; |
| 1026 | } |
| 1027 | } else { |
| 1028 | if (log_prio) { |
| 1029 | if (log_avg_max) |
| 1030 | fmt = "%" PRIu64 ", %" PRId64 ", %" PRId64 ", %u, %llu, 0x%04x\n"; |
| 1031 | else |
| 1032 | fmt = "%" PRIu64 ", %" PRId64 ", %u, %llu, 0x%04x\n"; |
| 1033 | } else { |
| 1034 | if (log_avg_max) |
| 1035 | fmt = "%" PRIu64 ", %" PRId64 ", %" PRId64 ", %u, %llu, %u\n"; |
| 1036 | else |
| 1037 | fmt = "%" PRIu64 ", %" PRId64 ", %u, %llu, %u\n"; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | nr_samples = sample_size / __log_entry_sz(log_offset); |
| 1042 | |
| 1043 | for (i = 0; i < nr_samples; i++) { |
| 1044 | s = __get_sample(samples, log_offset, i); |
| 1045 | |
| 1046 | if (log_prio) |
| 1047 | prio_val = s->priority; |
| 1048 | else |
| 1049 | prio_val = ioprio_value_is_class_rt(s->priority); |
| 1050 | |
| 1051 | if (!log_offset) { |
| 1052 | if (log_avg_max) |
| 1053 | fprintf(f, fmt, |
| 1054 | s->time, |
| 1055 | s->data.val.val0, |
| 1056 | s->data.val.val1, |
| 1057 | io_sample_ddir(s), (unsigned long long) s->bs, |
| 1058 | prio_val); |
| 1059 | else |
| 1060 | fprintf(f, fmt, |
| 1061 | s->time, |
| 1062 | s->data.val.val0, |
| 1063 | io_sample_ddir(s), (unsigned long long) s->bs, |
| 1064 | prio_val); |
| 1065 | } else { |
| 1066 | struct io_sample_offset *so = (void *) s; |
| 1067 | |
| 1068 | if (log_avg_max) |
| 1069 | fprintf(f, fmt, |
| 1070 | s->time, |
| 1071 | s->data.val.val0, |
| 1072 | s->data.val.val1, |
| 1073 | io_sample_ddir(s), (unsigned long long) s->bs, |
| 1074 | (unsigned long long) so->offset, |
| 1075 | prio_val); |
| 1076 | else |
| 1077 | fprintf(f, fmt, |
| 1078 | s->time, |
| 1079 | s->data.val.val0, |
| 1080 | io_sample_ddir(s), (unsigned long long) s->bs, |
| 1081 | (unsigned long long) so->offset, |
| 1082 | prio_val); |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | #ifdef CONFIG_ZLIB |
| 1088 | |
| 1089 | struct iolog_flush_data { |
| 1090 | struct workqueue_work work; |
| 1091 | struct io_log *log; |
| 1092 | void *samples; |
| 1093 | uint32_t nr_samples; |
| 1094 | bool free; |
| 1095 | }; |
| 1096 | |
| 1097 | #define GZ_CHUNK 131072 |
| 1098 | |
| 1099 | static struct iolog_compress *get_new_chunk(unsigned int seq) |
| 1100 | { |
| 1101 | struct iolog_compress *c; |
| 1102 | |
| 1103 | c = malloc(sizeof(*c)); |
| 1104 | INIT_FLIST_HEAD(&c->list); |
| 1105 | c->buf = malloc(GZ_CHUNK); |
| 1106 | c->len = 0; |
| 1107 | c->seq = seq; |
| 1108 | return c; |
| 1109 | } |
| 1110 | |
| 1111 | static void free_chunk(struct iolog_compress *ic) |
| 1112 | { |
| 1113 | free(ic->buf); |
| 1114 | free(ic); |
| 1115 | } |
| 1116 | |
| 1117 | static int z_stream_init(z_stream *stream, int gz_hdr) |
| 1118 | { |
| 1119 | int wbits = 15; |
| 1120 | |
| 1121 | memset(stream, 0, sizeof(*stream)); |
| 1122 | stream->zalloc = Z_NULL; |
| 1123 | stream->zfree = Z_NULL; |
| 1124 | stream->opaque = Z_NULL; |
| 1125 | stream->next_in = Z_NULL; |
| 1126 | |
| 1127 | /* |
| 1128 | * zlib magic - add 32 for auto-detection of gz header or not, |
| 1129 | * if we decide to store files in a gzip friendly format. |
| 1130 | */ |
| 1131 | if (gz_hdr) |
| 1132 | wbits += 32; |
| 1133 | |
| 1134 | if (inflateInit2(stream, wbits) != Z_OK) |
| 1135 | return 1; |
| 1136 | |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | struct inflate_chunk_iter { |
| 1141 | unsigned int seq; |
| 1142 | int err; |
| 1143 | void *buf; |
| 1144 | size_t buf_size; |
| 1145 | size_t buf_used; |
| 1146 | size_t chunk_sz; |
| 1147 | }; |
| 1148 | |
| 1149 | static void finish_chunk(z_stream *stream, FILE *f, |
| 1150 | struct inflate_chunk_iter *iter) |
| 1151 | { |
| 1152 | int ret; |
| 1153 | |
| 1154 | ret = inflateEnd(stream); |
| 1155 | if (ret != Z_OK) |
| 1156 | log_err("fio: failed to end log inflation seq %d (%d)\n", |
| 1157 | iter->seq, ret); |
| 1158 | |
| 1159 | flush_samples(f, iter->buf, iter->buf_used); |
| 1160 | free(iter->buf); |
| 1161 | iter->buf = NULL; |
| 1162 | iter->buf_size = iter->buf_used = 0; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * Iterative chunk inflation. Handles cases where we cross into a new |
| 1167 | * sequence, doing flush finish of previous chunk if needed. |
| 1168 | */ |
| 1169 | static size_t inflate_chunk(struct iolog_compress *ic, int gz_hdr, FILE *f, |
| 1170 | z_stream *stream, struct inflate_chunk_iter *iter) |
| 1171 | { |
| 1172 | size_t ret; |
| 1173 | |
| 1174 | dprint(FD_COMPRESS, "inflate chunk size=%lu, seq=%u\n", |
| 1175 | (unsigned long) ic->len, ic->seq); |
| 1176 | |
| 1177 | if (ic->seq != iter->seq) { |
| 1178 | if (iter->seq) |
| 1179 | finish_chunk(stream, f, iter); |
| 1180 | |
| 1181 | z_stream_init(stream, gz_hdr); |
| 1182 | iter->seq = ic->seq; |
| 1183 | } |
| 1184 | |
| 1185 | stream->avail_in = ic->len; |
| 1186 | stream->next_in = ic->buf; |
| 1187 | |
| 1188 | if (!iter->buf_size) { |
| 1189 | iter->buf_size = iter->chunk_sz; |
| 1190 | iter->buf = malloc(iter->buf_size); |
| 1191 | } |
| 1192 | |
| 1193 | while (stream->avail_in) { |
| 1194 | size_t this_out = iter->buf_size - iter->buf_used; |
| 1195 | int err; |
| 1196 | |
| 1197 | stream->avail_out = this_out; |
| 1198 | stream->next_out = iter->buf + iter->buf_used; |
| 1199 | |
| 1200 | err = inflate(stream, Z_NO_FLUSH); |
| 1201 | if (err < 0) { |
| 1202 | log_err("fio: failed inflating log: %d\n", err); |
| 1203 | iter->err = err; |
| 1204 | break; |
| 1205 | } |
| 1206 | |
| 1207 | iter->buf_used += this_out - stream->avail_out; |
| 1208 | |
| 1209 | if (!stream->avail_out) { |
| 1210 | iter->buf_size += iter->chunk_sz; |
| 1211 | iter->buf = realloc(iter->buf, iter->buf_size); |
| 1212 | continue; |
| 1213 | } |
| 1214 | |
| 1215 | if (err == Z_STREAM_END) |
| 1216 | break; |
| 1217 | } |
| 1218 | |
| 1219 | ret = (void *) stream->next_in - ic->buf; |
| 1220 | |
| 1221 | dprint(FD_COMPRESS, "inflated to size=%lu\n", (unsigned long) iter->buf_size); |
| 1222 | |
| 1223 | return ret; |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * Inflate stored compressed chunks, or write them directly to the log |
| 1228 | * file if so instructed. |
| 1229 | */ |
| 1230 | static int inflate_gz_chunks(struct io_log *log, FILE *f) |
| 1231 | { |
| 1232 | struct inflate_chunk_iter iter = { .chunk_sz = log->log_gz, }; |
| 1233 | z_stream stream; |
| 1234 | |
| 1235 | while (!flist_empty(&log->chunk_list)) { |
| 1236 | struct iolog_compress *ic; |
| 1237 | |
| 1238 | ic = flist_first_entry(&log->chunk_list, struct iolog_compress, list); |
| 1239 | flist_del(&ic->list); |
| 1240 | |
| 1241 | if (log->log_gz_store) { |
| 1242 | size_t ret; |
| 1243 | |
| 1244 | dprint(FD_COMPRESS, "log write chunk size=%lu, " |
| 1245 | "seq=%u\n", (unsigned long) ic->len, ic->seq); |
| 1246 | |
| 1247 | ret = fwrite(ic->buf, ic->len, 1, f); |
| 1248 | if (ret != 1 || ferror(f)) { |
| 1249 | iter.err = errno; |
| 1250 | log_err("fio: error writing compressed log\n"); |
| 1251 | } |
| 1252 | } else |
| 1253 | inflate_chunk(ic, log->log_gz_store, f, &stream, &iter); |
| 1254 | |
| 1255 | free_chunk(ic); |
| 1256 | } |
| 1257 | |
| 1258 | if (iter.seq) { |
| 1259 | finish_chunk(&stream, f, &iter); |
| 1260 | free(iter.buf); |
| 1261 | } |
| 1262 | |
| 1263 | return iter.err; |
| 1264 | } |
| 1265 | |
| 1266 | /* |
| 1267 | * Open compressed log file and decompress the stored chunks and |
| 1268 | * write them to stdout. The chunks are stored sequentially in the |
| 1269 | * file, so we iterate over them and do them one-by-one. |
| 1270 | */ |
| 1271 | int iolog_file_inflate(const char *file) |
| 1272 | { |
| 1273 | struct inflate_chunk_iter iter = { .chunk_sz = 64 * 1024 * 1024, }; |
| 1274 | struct iolog_compress ic; |
| 1275 | z_stream stream; |
| 1276 | struct stat sb; |
| 1277 | size_t ret; |
| 1278 | size_t total; |
| 1279 | void *buf; |
| 1280 | FILE *f; |
| 1281 | |
| 1282 | f = fopen(file, "rb"); |
| 1283 | if (!f) { |
| 1284 | perror("fopen"); |
| 1285 | return 1; |
| 1286 | } |
| 1287 | |
| 1288 | if (stat(file, &sb) < 0) { |
| 1289 | fclose(f); |
| 1290 | perror("stat"); |
| 1291 | return 1; |
| 1292 | } |
| 1293 | |
| 1294 | ic.buf = buf = malloc(sb.st_size); |
| 1295 | ic.len = sb.st_size; |
| 1296 | ic.seq = 1; |
| 1297 | |
| 1298 | ret = fread(ic.buf, ic.len, 1, f); |
| 1299 | if (ret == 0 && ferror(f)) { |
| 1300 | perror("fread"); |
| 1301 | fclose(f); |
| 1302 | free(buf); |
| 1303 | return 1; |
| 1304 | } else if (ferror(f) || (!feof(f) && ret != 1)) { |
| 1305 | log_err("fio: short read on reading log\n"); |
| 1306 | fclose(f); |
| 1307 | free(buf); |
| 1308 | return 1; |
| 1309 | } |
| 1310 | |
| 1311 | fclose(f); |
| 1312 | |
| 1313 | /* |
| 1314 | * Each chunk will return Z_STREAM_END. We don't know how many |
| 1315 | * chunks are in the file, so we just keep looping and incrementing |
| 1316 | * the sequence number until we have consumed the whole compressed |
| 1317 | * file. |
| 1318 | */ |
| 1319 | total = ic.len; |
| 1320 | do { |
| 1321 | size_t iret; |
| 1322 | |
| 1323 | iret = inflate_chunk(&ic, 1, stdout, &stream, &iter); |
| 1324 | total -= iret; |
| 1325 | if (!total) |
| 1326 | break; |
| 1327 | if (iter.err) |
| 1328 | break; |
| 1329 | |
| 1330 | ic.seq++; |
| 1331 | ic.len -= iret; |
| 1332 | ic.buf += iret; |
| 1333 | } while (1); |
| 1334 | |
| 1335 | if (iter.seq) { |
| 1336 | finish_chunk(&stream, stdout, &iter); |
| 1337 | free(iter.buf); |
| 1338 | } |
| 1339 | |
| 1340 | free(buf); |
| 1341 | return iter.err; |
| 1342 | } |
| 1343 | |
| 1344 | #else |
| 1345 | |
| 1346 | static int inflate_gz_chunks(struct io_log *log, FILE *f) |
| 1347 | { |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | int iolog_file_inflate(const char *file) |
| 1352 | { |
| 1353 | log_err("fio: log inflation not possible without zlib\n"); |
| 1354 | return 1; |
| 1355 | } |
| 1356 | |
| 1357 | #endif |
| 1358 | |
| 1359 | void flush_log(struct io_log *log, bool do_append) |
| 1360 | { |
| 1361 | void *buf; |
| 1362 | FILE *f; |
| 1363 | |
| 1364 | /* |
| 1365 | * If log_gz_store is true, we are writing a binary file. |
| 1366 | * Set the mode appropriately (on all platforms) to avoid issues |
| 1367 | * on windows (line-ending conversions, etc.) |
| 1368 | */ |
| 1369 | if (!do_append) |
| 1370 | if (log->log_gz_store) |
| 1371 | f = fopen(log->filename, "wb"); |
| 1372 | else |
| 1373 | f = fopen(log->filename, "w"); |
| 1374 | else |
| 1375 | if (log->log_gz_store) |
| 1376 | f = fopen(log->filename, "ab"); |
| 1377 | else |
| 1378 | f = fopen(log->filename, "a"); |
| 1379 | if (!f) { |
| 1380 | perror("fopen log"); |
| 1381 | return; |
| 1382 | } |
| 1383 | |
| 1384 | buf = set_file_buffer(f); |
| 1385 | |
| 1386 | inflate_gz_chunks(log, f); |
| 1387 | |
| 1388 | while (!flist_empty(&log->io_logs)) { |
| 1389 | struct io_logs *cur_log; |
| 1390 | |
| 1391 | cur_log = flist_first_entry(&log->io_logs, struct io_logs, list); |
| 1392 | flist_del_init(&cur_log->list); |
| 1393 | |
| 1394 | if (log->td && log == log->td->clat_hist_log) |
| 1395 | flush_hist_samples(f, log->hist_coarseness, cur_log->log, |
| 1396 | log_sample_sz(log, cur_log)); |
| 1397 | else |
| 1398 | flush_samples(f, cur_log->log, log_sample_sz(log, cur_log)); |
| 1399 | |
| 1400 | sfree(cur_log); |
| 1401 | } |
| 1402 | |
| 1403 | fclose(f); |
| 1404 | clear_file_buffer(buf); |
| 1405 | } |
| 1406 | |
| 1407 | static int finish_log(struct thread_data *td, struct io_log *log, int trylock) |
| 1408 | { |
| 1409 | if (td->flags & TD_F_COMPRESS_LOG) |
| 1410 | iolog_flush(log); |
| 1411 | |
| 1412 | if (trylock) { |
| 1413 | if (fio_trylock_file(log->filename)) |
| 1414 | return 1; |
| 1415 | } else |
| 1416 | fio_lock_file(log->filename); |
| 1417 | |
| 1418 | if (td->client_type == FIO_CLIENT_TYPE_GUI || is_backend) |
| 1419 | fio_send_iolog(td, log, log->filename); |
| 1420 | else |
| 1421 | flush_log(log, !td->o.per_job_logs); |
| 1422 | |
| 1423 | fio_unlock_file(log->filename); |
| 1424 | free_log(log); |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | size_t log_chunk_sizes(struct io_log *log) |
| 1429 | { |
| 1430 | struct flist_head *entry; |
| 1431 | size_t ret; |
| 1432 | |
| 1433 | if (flist_empty(&log->chunk_list)) |
| 1434 | return 0; |
| 1435 | |
| 1436 | ret = 0; |
| 1437 | pthread_mutex_lock(&log->chunk_lock); |
| 1438 | flist_for_each(entry, &log->chunk_list) { |
| 1439 | struct iolog_compress *c; |
| 1440 | |
| 1441 | c = flist_entry(entry, struct iolog_compress, list); |
| 1442 | ret += c->len; |
| 1443 | } |
| 1444 | pthread_mutex_unlock(&log->chunk_lock); |
| 1445 | return ret; |
| 1446 | } |
| 1447 | |
| 1448 | #ifdef CONFIG_ZLIB |
| 1449 | |
| 1450 | static void iolog_put_deferred(struct io_log *log, void *ptr) |
| 1451 | { |
| 1452 | if (!ptr) |
| 1453 | return; |
| 1454 | |
| 1455 | pthread_mutex_lock(&log->deferred_free_lock); |
| 1456 | if (log->deferred < IOLOG_MAX_DEFER) { |
| 1457 | log->deferred_items[log->deferred] = ptr; |
| 1458 | log->deferred++; |
| 1459 | } else if (!fio_did_warn(FIO_WARN_IOLOG_DROP)) |
| 1460 | log_err("fio: had to drop log entry free\n"); |
| 1461 | pthread_mutex_unlock(&log->deferred_free_lock); |
| 1462 | } |
| 1463 | |
| 1464 | static void iolog_free_deferred(struct io_log *log) |
| 1465 | { |
| 1466 | int i; |
| 1467 | |
| 1468 | if (!log->deferred) |
| 1469 | return; |
| 1470 | |
| 1471 | pthread_mutex_lock(&log->deferred_free_lock); |
| 1472 | |
| 1473 | for (i = 0; i < log->deferred; i++) { |
| 1474 | free(log->deferred_items[i]); |
| 1475 | log->deferred_items[i] = NULL; |
| 1476 | } |
| 1477 | |
| 1478 | log->deferred = 0; |
| 1479 | pthread_mutex_unlock(&log->deferred_free_lock); |
| 1480 | } |
| 1481 | |
| 1482 | static int gz_work(struct iolog_flush_data *data) |
| 1483 | { |
| 1484 | struct iolog_compress *c = NULL; |
| 1485 | struct flist_head list; |
| 1486 | unsigned int seq; |
| 1487 | z_stream stream; |
| 1488 | size_t total = 0; |
| 1489 | int ret; |
| 1490 | |
| 1491 | INIT_FLIST_HEAD(&list); |
| 1492 | |
| 1493 | memset(&stream, 0, sizeof(stream)); |
| 1494 | stream.zalloc = Z_NULL; |
| 1495 | stream.zfree = Z_NULL; |
| 1496 | stream.opaque = Z_NULL; |
| 1497 | |
| 1498 | ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION); |
| 1499 | if (ret != Z_OK) { |
| 1500 | log_err("fio: failed to init gz stream\n"); |
| 1501 | goto err; |
| 1502 | } |
| 1503 | |
| 1504 | seq = ++data->log->chunk_seq; |
| 1505 | |
| 1506 | stream.next_in = (void *) data->samples; |
| 1507 | stream.avail_in = data->nr_samples * log_entry_sz(data->log); |
| 1508 | |
| 1509 | dprint(FD_COMPRESS, "deflate input size=%lu, seq=%u, log=%s\n", |
| 1510 | (unsigned long) stream.avail_in, seq, |
| 1511 | data->log->filename); |
| 1512 | do { |
| 1513 | if (c) |
| 1514 | dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq, |
| 1515 | (unsigned long) c->len); |
| 1516 | c = get_new_chunk(seq); |
| 1517 | stream.avail_out = GZ_CHUNK; |
| 1518 | stream.next_out = c->buf; |
| 1519 | ret = deflate(&stream, Z_NO_FLUSH); |
| 1520 | if (ret < 0) { |
| 1521 | log_err("fio: deflate log (%d)\n", ret); |
| 1522 | free_chunk(c); |
| 1523 | goto err; |
| 1524 | } |
| 1525 | |
| 1526 | c->len = GZ_CHUNK - stream.avail_out; |
| 1527 | flist_add_tail(&c->list, &list); |
| 1528 | total += c->len; |
| 1529 | } while (stream.avail_in); |
| 1530 | |
| 1531 | stream.next_out = c->buf + c->len; |
| 1532 | stream.avail_out = GZ_CHUNK - c->len; |
| 1533 | |
| 1534 | ret = deflate(&stream, Z_FINISH); |
| 1535 | if (ret < 0) { |
| 1536 | /* |
| 1537 | * Z_BUF_ERROR is special, it just means we need more |
| 1538 | * output space. We'll handle that below. Treat any other |
| 1539 | * error as fatal. |
| 1540 | */ |
| 1541 | if (ret != Z_BUF_ERROR) { |
| 1542 | log_err("fio: deflate log (%d)\n", ret); |
| 1543 | flist_del(&c->list); |
| 1544 | free_chunk(c); |
| 1545 | goto err; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | total -= c->len; |
| 1550 | c->len = GZ_CHUNK - stream.avail_out; |
| 1551 | total += c->len; |
| 1552 | dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq, (unsigned long) c->len); |
| 1553 | |
| 1554 | if (ret != Z_STREAM_END) { |
| 1555 | do { |
| 1556 | c = get_new_chunk(seq); |
| 1557 | stream.avail_out = GZ_CHUNK; |
| 1558 | stream.next_out = c->buf; |
| 1559 | ret = deflate(&stream, Z_FINISH); |
| 1560 | c->len = GZ_CHUNK - stream.avail_out; |
| 1561 | total += c->len; |
| 1562 | flist_add_tail(&c->list, &list); |
| 1563 | dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq, |
| 1564 | (unsigned long) c->len); |
| 1565 | } while (ret != Z_STREAM_END); |
| 1566 | } |
| 1567 | |
| 1568 | dprint(FD_COMPRESS, "deflated to size=%lu\n", (unsigned long) total); |
| 1569 | |
| 1570 | ret = deflateEnd(&stream); |
| 1571 | if (ret != Z_OK) |
| 1572 | log_err("fio: deflateEnd %d\n", ret); |
| 1573 | |
| 1574 | iolog_put_deferred(data->log, data->samples); |
| 1575 | |
| 1576 | if (!flist_empty(&list)) { |
| 1577 | pthread_mutex_lock(&data->log->chunk_lock); |
| 1578 | flist_splice_tail(&list, &data->log->chunk_list); |
| 1579 | pthread_mutex_unlock(&data->log->chunk_lock); |
| 1580 | } |
| 1581 | |
| 1582 | ret = 0; |
| 1583 | done: |
| 1584 | if (data->free) |
| 1585 | sfree(data); |
| 1586 | return ret; |
| 1587 | err: |
| 1588 | while (!flist_empty(&list)) { |
| 1589 | c = flist_first_entry(list.next, struct iolog_compress, list); |
| 1590 | flist_del(&c->list); |
| 1591 | free_chunk(c); |
| 1592 | } |
| 1593 | ret = 1; |
| 1594 | goto done; |
| 1595 | } |
| 1596 | |
| 1597 | /* |
| 1598 | * Invoked from our compress helper thread, when logging would have exceeded |
| 1599 | * the specified memory limitation. Compresses the previously stored |
| 1600 | * entries. |
| 1601 | */ |
| 1602 | static int gz_work_async(struct submit_worker *sw, struct workqueue_work *work) |
| 1603 | { |
| 1604 | return gz_work(container_of(work, struct iolog_flush_data, work)); |
| 1605 | } |
| 1606 | |
| 1607 | static int gz_init_worker(struct submit_worker *sw) |
| 1608 | { |
| 1609 | struct thread_data *td = sw->wq->td; |
| 1610 | |
| 1611 | if (!fio_option_is_set(&td->o, log_gz_cpumask)) |
| 1612 | return 0; |
| 1613 | |
| 1614 | if (fio_setaffinity(gettid(), td->o.log_gz_cpumask) == -1) { |
| 1615 | log_err("gz: failed to set CPU affinity\n"); |
| 1616 | return 1; |
| 1617 | } |
| 1618 | |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
| 1622 | static struct workqueue_ops log_compress_wq_ops = { |
| 1623 | .fn = gz_work_async, |
| 1624 | .init_worker_fn = gz_init_worker, |
| 1625 | .nice = 1, |
| 1626 | }; |
| 1627 | |
| 1628 | int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out) |
| 1629 | { |
| 1630 | if (!(td->flags & TD_F_COMPRESS_LOG)) |
| 1631 | return 0; |
| 1632 | |
| 1633 | workqueue_init(td, &td->log_compress_wq, &log_compress_wq_ops, 1, sk_out); |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | void iolog_compress_exit(struct thread_data *td) |
| 1638 | { |
| 1639 | if (!(td->flags & TD_F_COMPRESS_LOG)) |
| 1640 | return; |
| 1641 | |
| 1642 | workqueue_exit(&td->log_compress_wq); |
| 1643 | } |
| 1644 | |
| 1645 | /* |
| 1646 | * Queue work item to compress the existing log entries. We reset the |
| 1647 | * current log to a small size, and reference the existing log in the |
| 1648 | * data that we queue for compression. Once compression has been done, |
| 1649 | * this old log is freed. Will not return until the log compression |
| 1650 | * has completed, and will flush all previous logs too |
| 1651 | */ |
| 1652 | static int iolog_flush(struct io_log *log) |
| 1653 | { |
| 1654 | struct iolog_flush_data *data; |
| 1655 | |
| 1656 | workqueue_flush(&log->td->log_compress_wq); |
| 1657 | data = malloc(sizeof(*data)); |
| 1658 | if (!data) |
| 1659 | return 1; |
| 1660 | |
| 1661 | data->log = log; |
| 1662 | data->free = false; |
| 1663 | |
| 1664 | while (!flist_empty(&log->io_logs)) { |
| 1665 | struct io_logs *cur_log; |
| 1666 | |
| 1667 | cur_log = flist_first_entry(&log->io_logs, struct io_logs, list); |
| 1668 | flist_del_init(&cur_log->list); |
| 1669 | |
| 1670 | data->samples = cur_log->log; |
| 1671 | data->nr_samples = cur_log->nr_samples; |
| 1672 | |
| 1673 | sfree(cur_log); |
| 1674 | |
| 1675 | gz_work(data); |
| 1676 | } |
| 1677 | |
| 1678 | free(data); |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log) |
| 1683 | { |
| 1684 | struct iolog_flush_data *data; |
| 1685 | |
| 1686 | data = smalloc(sizeof(*data)); |
| 1687 | if (!data) |
| 1688 | return 1; |
| 1689 | |
| 1690 | data->log = log; |
| 1691 | |
| 1692 | data->samples = cur_log->log; |
| 1693 | data->nr_samples = cur_log->nr_samples; |
| 1694 | data->free = true; |
| 1695 | |
| 1696 | cur_log->nr_samples = cur_log->max_samples = 0; |
| 1697 | cur_log->log = NULL; |
| 1698 | |
| 1699 | workqueue_enqueue(&log->td->log_compress_wq, &data->work); |
| 1700 | |
| 1701 | iolog_free_deferred(log); |
| 1702 | |
| 1703 | return 0; |
| 1704 | } |
| 1705 | #else |
| 1706 | |
| 1707 | static int iolog_flush(struct io_log *log) |
| 1708 | { |
| 1709 | return 1; |
| 1710 | } |
| 1711 | |
| 1712 | int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log) |
| 1713 | { |
| 1714 | return 1; |
| 1715 | } |
| 1716 | |
| 1717 | int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out) |
| 1718 | { |
| 1719 | return 0; |
| 1720 | } |
| 1721 | |
| 1722 | void iolog_compress_exit(struct thread_data *td) |
| 1723 | { |
| 1724 | } |
| 1725 | |
| 1726 | #endif |
| 1727 | |
| 1728 | struct io_logs *iolog_cur_log(struct io_log *log) |
| 1729 | { |
| 1730 | if (flist_empty(&log->io_logs)) |
| 1731 | return NULL; |
| 1732 | |
| 1733 | return flist_last_entry(&log->io_logs, struct io_logs, list); |
| 1734 | } |
| 1735 | |
| 1736 | uint64_t iolog_nr_samples(struct io_log *iolog) |
| 1737 | { |
| 1738 | struct flist_head *entry; |
| 1739 | uint64_t ret = 0; |
| 1740 | |
| 1741 | flist_for_each(entry, &iolog->io_logs) { |
| 1742 | struct io_logs *cur_log; |
| 1743 | |
| 1744 | cur_log = flist_entry(entry, struct io_logs, list); |
| 1745 | ret += cur_log->nr_samples; |
| 1746 | } |
| 1747 | |
| 1748 | return ret; |
| 1749 | } |
| 1750 | |
| 1751 | static int __write_log(struct thread_data *td, struct io_log *log, int try) |
| 1752 | { |
| 1753 | if (log) |
| 1754 | return finish_log(td, log, try); |
| 1755 | |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | static int write_iops_log(struct thread_data *td, int try, bool unit_log) |
| 1760 | { |
| 1761 | int ret; |
| 1762 | |
| 1763 | if (per_unit_log(td->iops_log) != unit_log) |
| 1764 | return 0; |
| 1765 | |
| 1766 | ret = __write_log(td, td->iops_log, try); |
| 1767 | if (!ret) |
| 1768 | td->iops_log = NULL; |
| 1769 | |
| 1770 | return ret; |
| 1771 | } |
| 1772 | |
| 1773 | static int write_slat_log(struct thread_data *td, int try, bool unit_log) |
| 1774 | { |
| 1775 | int ret; |
| 1776 | |
| 1777 | if (!unit_log) |
| 1778 | return 0; |
| 1779 | |
| 1780 | ret = __write_log(td, td->slat_log, try); |
| 1781 | if (!ret) |
| 1782 | td->slat_log = NULL; |
| 1783 | |
| 1784 | return ret; |
| 1785 | } |
| 1786 | |
| 1787 | static int write_clat_log(struct thread_data *td, int try, bool unit_log) |
| 1788 | { |
| 1789 | int ret; |
| 1790 | |
| 1791 | if (!unit_log) |
| 1792 | return 0; |
| 1793 | |
| 1794 | ret = __write_log(td, td->clat_log, try); |
| 1795 | if (!ret) |
| 1796 | td->clat_log = NULL; |
| 1797 | |
| 1798 | return ret; |
| 1799 | } |
| 1800 | |
| 1801 | static int write_clat_hist_log(struct thread_data *td, int try, bool unit_log) |
| 1802 | { |
| 1803 | int ret; |
| 1804 | |
| 1805 | if (!unit_log) |
| 1806 | return 0; |
| 1807 | |
| 1808 | ret = __write_log(td, td->clat_hist_log, try); |
| 1809 | if (!ret) |
| 1810 | td->clat_hist_log = NULL; |
| 1811 | |
| 1812 | return ret; |
| 1813 | } |
| 1814 | |
| 1815 | static int write_lat_log(struct thread_data *td, int try, bool unit_log) |
| 1816 | { |
| 1817 | int ret; |
| 1818 | |
| 1819 | if (!unit_log) |
| 1820 | return 0; |
| 1821 | |
| 1822 | ret = __write_log(td, td->lat_log, try); |
| 1823 | if (!ret) |
| 1824 | td->lat_log = NULL; |
| 1825 | |
| 1826 | return ret; |
| 1827 | } |
| 1828 | |
| 1829 | static int write_bandw_log(struct thread_data *td, int try, bool unit_log) |
| 1830 | { |
| 1831 | int ret; |
| 1832 | |
| 1833 | if (per_unit_log(td->bw_log) != unit_log) |
| 1834 | return 0; |
| 1835 | |
| 1836 | ret = __write_log(td, td->bw_log, try); |
| 1837 | if (!ret) |
| 1838 | td->bw_log = NULL; |
| 1839 | |
| 1840 | return ret; |
| 1841 | } |
| 1842 | |
| 1843 | enum { |
| 1844 | BW_LOG_MASK = 1, |
| 1845 | LAT_LOG_MASK = 2, |
| 1846 | SLAT_LOG_MASK = 4, |
| 1847 | CLAT_LOG_MASK = 8, |
| 1848 | IOPS_LOG_MASK = 16, |
| 1849 | CLAT_HIST_LOG_MASK = 32, |
| 1850 | |
| 1851 | ALL_LOG_NR = 6, |
| 1852 | }; |
| 1853 | |
| 1854 | struct log_type { |
| 1855 | unsigned int mask; |
| 1856 | int (*fn)(struct thread_data *, int, bool); |
| 1857 | }; |
| 1858 | |
| 1859 | static struct log_type log_types[] = { |
| 1860 | { |
| 1861 | .mask = BW_LOG_MASK, |
| 1862 | .fn = write_bandw_log, |
| 1863 | }, |
| 1864 | { |
| 1865 | .mask = LAT_LOG_MASK, |
| 1866 | .fn = write_lat_log, |
| 1867 | }, |
| 1868 | { |
| 1869 | .mask = SLAT_LOG_MASK, |
| 1870 | .fn = write_slat_log, |
| 1871 | }, |
| 1872 | { |
| 1873 | .mask = CLAT_LOG_MASK, |
| 1874 | .fn = write_clat_log, |
| 1875 | }, |
| 1876 | { |
| 1877 | .mask = IOPS_LOG_MASK, |
| 1878 | .fn = write_iops_log, |
| 1879 | }, |
| 1880 | { |
| 1881 | .mask = CLAT_HIST_LOG_MASK, |
| 1882 | .fn = write_clat_hist_log, |
| 1883 | } |
| 1884 | }; |
| 1885 | |
| 1886 | void td_writeout_logs(struct thread_data *td, bool unit_logs) |
| 1887 | { |
| 1888 | unsigned int log_mask = 0; |
| 1889 | unsigned int log_left = ALL_LOG_NR; |
| 1890 | int old_state, i; |
| 1891 | |
| 1892 | old_state = td_bump_runstate(td, TD_FINISHING); |
| 1893 | |
| 1894 | finalize_logs(td, unit_logs); |
| 1895 | |
| 1896 | while (log_left) { |
| 1897 | int prev_log_left = log_left; |
| 1898 | |
| 1899 | for (i = 0; i < ALL_LOG_NR && log_left; i++) { |
| 1900 | struct log_type *lt = &log_types[i]; |
| 1901 | int ret; |
| 1902 | |
| 1903 | if (!(log_mask & lt->mask)) { |
| 1904 | ret = lt->fn(td, log_left != 1, unit_logs); |
| 1905 | if (!ret) { |
| 1906 | log_left--; |
| 1907 | log_mask |= lt->mask; |
| 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | if (prev_log_left == log_left) |
| 1913 | usleep(5000); |
| 1914 | } |
| 1915 | |
| 1916 | td_restore_runstate(td, old_state); |
| 1917 | } |
| 1918 | |
| 1919 | void fio_writeout_logs(bool unit_logs) |
| 1920 | { |
| 1921 | for_each_td(td) { |
| 1922 | td_writeout_logs(td, unit_logs); |
| 1923 | } end_for_each(); |
| 1924 | } |