Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 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 <libgen.h> |
| 8 | #include <assert.h> |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <unistd.h> |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 12 | #ifdef CONFIG_ZLIB |
| 13 | #include <zlib.h> |
| 14 | #endif |
| 15 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 16 | #include "flist.h" |
| 17 | #include "fio.h" |
| 18 | #include "verify.h" |
| 19 | #include "trim.h" |
Jens Axboe | 243bfe1 | 2014-04-02 15:46:58 -0600 | [diff] [blame] | 20 | #include "filelock.h" |
Jens Axboe | 65f93ca | 2014-07-03 14:16:25 -0600 | [diff] [blame] | 21 | #include "lib/tp.h" |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 22 | |
| 23 | static const char iolog_ver2[] = "fio version 2 iolog"; |
| 24 | |
| 25 | void queue_io_piece(struct thread_data *td, struct io_piece *ipo) |
| 26 | { |
| 27 | flist_add_tail(&ipo->list, &td->io_log_list); |
| 28 | td->total_io_size += ipo->len; |
| 29 | } |
| 30 | |
Jens Axboe | 0cbbc39 | 2014-09-30 16:04:12 -0600 | [diff] [blame] | 31 | void log_io_u(const struct thread_data *td, const struct io_u *io_u) |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 32 | { |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 33 | if (!td->o.write_iolog_file) |
| 34 | return; |
| 35 | |
| 36 | fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name, |
Jens Axboe | 059c406 | 2014-09-16 20:07:59 -0600 | [diff] [blame] | 37 | io_ddir_name(io_u->ddir), |
| 38 | io_u->offset, io_u->buflen); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void log_file(struct thread_data *td, struct fio_file *f, |
| 42 | enum file_log_act what) |
| 43 | { |
| 44 | const char *act[] = { "add", "open", "close" }; |
| 45 | |
| 46 | assert(what < 3); |
| 47 | |
| 48 | if (!td->o.write_iolog_file) |
| 49 | return; |
| 50 | |
| 51 | |
| 52 | /* |
| 53 | * this happens on the pre-open/close done before the job starts |
| 54 | */ |
| 55 | if (!td->iolog_f) |
| 56 | return; |
| 57 | |
| 58 | fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]); |
| 59 | } |
| 60 | |
| 61 | static void iolog_delay(struct thread_data *td, unsigned long delay) |
| 62 | { |
Jens Axboe | f15b9e5 | 2014-11-21 14:33:33 -0700 | [diff] [blame] | 63 | uint64_t usec = utime_since_now(&td->last_issue); |
| 64 | uint64_t this_delay; |
| 65 | struct timeval tv; |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 66 | |
Jens Axboe | f15b9e5 | 2014-11-21 14:33:33 -0700 | [diff] [blame] | 67 | if (delay < td->time_offset) { |
| 68 | td->time_offset = 0; |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | delay -= td->time_offset; |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 73 | if (delay < usec) |
| 74 | return; |
| 75 | |
| 76 | delay -= usec; |
| 77 | |
Jens Axboe | f15b9e5 | 2014-11-21 14:33:33 -0700 | [diff] [blame] | 78 | fio_gettime(&tv, NULL); |
Jens Axboe | 30b1867 | 2014-04-11 00:03:32 +0200 | [diff] [blame] | 79 | while (delay && !td->terminate) { |
| 80 | this_delay = delay; |
| 81 | if (this_delay > 500000) |
| 82 | this_delay = 500000; |
| 83 | |
| 84 | usec_sleep(td, this_delay); |
| 85 | delay -= this_delay; |
| 86 | } |
Jens Axboe | f15b9e5 | 2014-11-21 14:33:33 -0700 | [diff] [blame] | 87 | |
| 88 | usec = utime_since_now(&tv); |
| 89 | if (usec > delay) |
| 90 | td->time_offset = usec - delay; |
| 91 | else |
| 92 | td->time_offset = 0; |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static int ipo_special(struct thread_data *td, struct io_piece *ipo) |
| 96 | { |
| 97 | struct fio_file *f; |
| 98 | int ret; |
| 99 | |
| 100 | /* |
| 101 | * Not a special ipo |
| 102 | */ |
| 103 | if (ipo->ddir != DDIR_INVAL) |
| 104 | return 0; |
| 105 | |
| 106 | f = td->files[ipo->fileno]; |
| 107 | |
| 108 | switch (ipo->file_action) { |
| 109 | case FIO_LOG_OPEN_FILE: |
| 110 | ret = td_io_open_file(td, f); |
| 111 | if (!ret) |
| 112 | break; |
| 113 | td_verror(td, ret, "iolog open file"); |
| 114 | return -1; |
| 115 | case FIO_LOG_CLOSE_FILE: |
| 116 | td_io_close_file(td, f); |
| 117 | break; |
| 118 | case FIO_LOG_UNLINK_FILE: |
Castor Fu | 9187a26 | 2014-08-19 09:28:53 -0700 | [diff] [blame] | 119 | td_io_unlink_file(td, f); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 120 | break; |
| 121 | default: |
| 122 | log_err("fio: bad file action %d\n", ipo->file_action); |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | int read_iolog_get(struct thread_data *td, struct io_u *io_u) |
| 130 | { |
| 131 | struct io_piece *ipo; |
| 132 | unsigned long elapsed; |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 133 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 134 | while (!flist_empty(&td->io_log_list)) { |
| 135 | int ret; |
| 136 | |
Jens Axboe | 12dbd06 | 2014-07-03 21:19:57 -0600 | [diff] [blame] | 137 | ipo = flist_first_entry(&td->io_log_list, struct io_piece, list); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 138 | flist_del(&ipo->list); |
| 139 | remove_trim_entry(td, ipo); |
| 140 | |
| 141 | ret = ipo_special(td, ipo); |
| 142 | if (ret < 0) { |
| 143 | free(ipo); |
| 144 | break; |
| 145 | } else if (ret > 0) { |
| 146 | free(ipo); |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | io_u->ddir = ipo->ddir; |
| 151 | if (ipo->ddir != DDIR_WAIT) { |
| 152 | io_u->offset = ipo->offset; |
| 153 | io_u->buflen = ipo->len; |
| 154 | io_u->file = td->files[ipo->fileno]; |
| 155 | get_file(io_u->file); |
| 156 | dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset, |
| 157 | io_u->buflen, io_u->file->file_name); |
| 158 | if (ipo->delay) |
| 159 | iolog_delay(td, ipo->delay); |
| 160 | } else { |
| 161 | elapsed = mtime_since_genesis(); |
| 162 | if (ipo->delay > elapsed) |
| 163 | usec_sleep(td, (ipo->delay - elapsed) * 1000); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | free(ipo); |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 167 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 168 | if (io_u->ddir != DDIR_WAIT) |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | td->done = 1; |
| 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | void prune_io_piece_log(struct thread_data *td) |
| 177 | { |
| 178 | struct io_piece *ipo; |
| 179 | struct rb_node *n; |
| 180 | |
| 181 | while ((n = rb_first(&td->io_hist_tree)) != NULL) { |
| 182 | ipo = rb_entry(n, struct io_piece, rb_node); |
| 183 | rb_erase(n, &td->io_hist_tree); |
| 184 | remove_trim_entry(td, ipo); |
| 185 | td->io_hist_len--; |
| 186 | free(ipo); |
| 187 | } |
| 188 | |
| 189 | while (!flist_empty(&td->io_hist_list)) { |
Jens Axboe | 6d63908 | 2014-11-07 15:41:24 -0700 | [diff] [blame] | 190 | ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 191 | flist_del(&ipo->list); |
| 192 | remove_trim_entry(td, ipo); |
| 193 | td->io_hist_len--; |
| 194 | free(ipo); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * log a successful write, so we can unwind the log for verify |
| 200 | */ |
| 201 | void log_io_piece(struct thread_data *td, struct io_u *io_u) |
| 202 | { |
| 203 | struct rb_node **p, *parent; |
| 204 | struct io_piece *ipo, *__ipo; |
| 205 | |
| 206 | ipo = malloc(sizeof(struct io_piece)); |
| 207 | init_ipo(ipo); |
| 208 | ipo->file = io_u->file; |
| 209 | ipo->offset = io_u->offset; |
| 210 | ipo->len = io_u->buflen; |
Juan Casse | da0a7bd | 2013-09-17 14:06:12 -0700 | [diff] [blame] | 211 | ipo->numberio = io_u->numberio; |
Jens Axboe | f940128 | 2014-02-06 12:17:37 -0700 | [diff] [blame] | 212 | ipo->flags = IP_F_IN_FLIGHT; |
| 213 | |
| 214 | io_u->ipo = ipo; |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 215 | |
| 216 | if (io_u_should_trim(td, io_u)) { |
| 217 | flist_add_tail(&ipo->trim_list, &td->trim_list); |
| 218 | td->trim_entries++; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * We don't need to sort the entries, if: |
| 223 | * |
| 224 | * Sequential writes, or |
| 225 | * Random writes that lay out the file as it goes along |
| 226 | * |
| 227 | * For both these cases, just reading back data in the order we |
| 228 | * wrote it out is the fastest. |
| 229 | * |
| 230 | * One exception is if we don't have a random map AND we are doing |
| 231 | * verifies, in that case we need to check for duplicate blocks and |
| 232 | * drop the old one, which we rely on the rb insert/lookup for |
| 233 | * handling. |
| 234 | */ |
Puthikorn Voravootivat | c4b6117 | 2014-02-05 10:17:11 -0700 | [diff] [blame] | 235 | if (((!td->o.verifysort) || !td_random(td) || !td->o.overwrite) && |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 236 | (file_randommap(td, ipo->file) || td->o.verify == VERIFY_NONE)) { |
| 237 | INIT_FLIST_HEAD(&ipo->list); |
| 238 | flist_add_tail(&ipo->list, &td->io_hist_list); |
| 239 | ipo->flags |= IP_F_ONLIST; |
| 240 | td->io_hist_len++; |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | RB_CLEAR_NODE(&ipo->rb_node); |
| 245 | |
| 246 | /* |
| 247 | * Sort the entry into the verification list |
| 248 | */ |
| 249 | restart: |
| 250 | p = &td->io_hist_tree.rb_node; |
| 251 | parent = NULL; |
| 252 | while (*p) { |
Justin Eno | 3809413 | 2015-01-29 12:59:22 -0800 | [diff] [blame] | 253 | int overlap = 0; |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 254 | parent = *p; |
| 255 | |
| 256 | __ipo = rb_entry(parent, struct io_piece, rb_node); |
| 257 | if (ipo->file < __ipo->file) |
| 258 | p = &(*p)->rb_left; |
| 259 | else if (ipo->file > __ipo->file) |
| 260 | p = &(*p)->rb_right; |
Justin Eno | 3809413 | 2015-01-29 12:59:22 -0800 | [diff] [blame] | 261 | else if (ipo->offset < __ipo->offset) { |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 262 | p = &(*p)->rb_left; |
Justin Eno | 3809413 | 2015-01-29 12:59:22 -0800 | [diff] [blame] | 263 | overlap = ipo->offset + ipo->len > __ipo->offset; |
| 264 | } |
| 265 | else if (ipo->offset > __ipo->offset) { |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 266 | p = &(*p)->rb_right; |
Justin Eno | 3809413 | 2015-01-29 12:59:22 -0800 | [diff] [blame] | 267 | overlap = __ipo->offset + __ipo->len > ipo->offset; |
| 268 | } |
| 269 | else |
| 270 | overlap = 1; |
| 271 | |
| 272 | if (overlap) { |
Jens Axboe | 885ac62 | 2012-04-04 14:11:58 -0600 | [diff] [blame] | 273 | dprint(FD_IO, "iolog: overlap %llu/%lu, %llu/%lu", |
| 274 | __ipo->offset, __ipo->len, |
| 275 | ipo->offset, ipo->len); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 276 | td->io_hist_len--; |
| 277 | rb_erase(parent, &td->io_hist_tree); |
| 278 | remove_trim_entry(td, __ipo); |
| 279 | free(__ipo); |
| 280 | goto restart; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | rb_link_node(&ipo->rb_node, parent, p); |
| 285 | rb_insert_color(&ipo->rb_node, &td->io_hist_tree); |
| 286 | ipo->flags |= IP_F_ONRB; |
| 287 | td->io_hist_len++; |
| 288 | } |
| 289 | |
Jens Axboe | 890b665 | 2014-05-06 19:06:51 -0600 | [diff] [blame] | 290 | void unlog_io_piece(struct thread_data *td, struct io_u *io_u) |
| 291 | { |
| 292 | struct io_piece *ipo = io_u->ipo; |
| 293 | |
| 294 | if (!ipo) |
| 295 | return; |
| 296 | |
| 297 | if (ipo->flags & IP_F_ONRB) |
| 298 | rb_erase(&ipo->rb_node, &td->io_hist_tree); |
| 299 | else if (ipo->flags & IP_F_ONLIST) |
| 300 | flist_del(&ipo->list); |
| 301 | |
| 302 | free(ipo); |
| 303 | io_u->ipo = NULL; |
| 304 | td->io_hist_len--; |
| 305 | } |
| 306 | |
Jens Axboe | 0cbbc39 | 2014-09-30 16:04:12 -0600 | [diff] [blame] | 307 | void trim_io_piece(struct thread_data *td, const struct io_u *io_u) |
Jens Axboe | 890b665 | 2014-05-06 19:06:51 -0600 | [diff] [blame] | 308 | { |
| 309 | struct io_piece *ipo = io_u->ipo; |
| 310 | |
| 311 | if (!ipo) |
| 312 | return; |
| 313 | |
| 314 | ipo->len = io_u->xfer_buflen - io_u->resid; |
| 315 | } |
| 316 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 317 | void write_iolog_close(struct thread_data *td) |
| 318 | { |
| 319 | fflush(td->iolog_f); |
| 320 | fclose(td->iolog_f); |
| 321 | free(td->iolog_buf); |
| 322 | td->iolog_f = NULL; |
| 323 | td->iolog_buf = NULL; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Read version 2 iolog data. It is enhanced to include per-file logging, |
| 328 | * syncs, etc. |
| 329 | */ |
| 330 | static int read_iolog2(struct thread_data *td, FILE *f) |
| 331 | { |
| 332 | unsigned long long offset; |
| 333 | unsigned int bytes; |
| 334 | int reads, writes, waits, fileno = 0, file_action = 0; /* stupid gcc */ |
| 335 | char *fname, *act; |
| 336 | char *str, *p; |
| 337 | enum fio_ddir rw; |
| 338 | |
| 339 | free_release_files(td); |
| 340 | |
| 341 | /* |
| 342 | * Read in the read iolog and store it, reuse the infrastructure |
| 343 | * for doing verifications. |
| 344 | */ |
| 345 | str = malloc(4096); |
| 346 | fname = malloc(256+16); |
| 347 | act = malloc(256+16); |
| 348 | |
| 349 | reads = writes = waits = 0; |
| 350 | while ((p = fgets(str, 4096, f)) != NULL) { |
| 351 | struct io_piece *ipo; |
| 352 | int r; |
| 353 | |
| 354 | r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset, |
| 355 | &bytes); |
| 356 | if (r == 4) { |
| 357 | /* |
| 358 | * Check action first |
| 359 | */ |
| 360 | if (!strcmp(act, "wait")) |
| 361 | rw = DDIR_WAIT; |
| 362 | else if (!strcmp(act, "read")) |
| 363 | rw = DDIR_READ; |
| 364 | else if (!strcmp(act, "write")) |
| 365 | rw = DDIR_WRITE; |
| 366 | else if (!strcmp(act, "sync")) |
| 367 | rw = DDIR_SYNC; |
| 368 | else if (!strcmp(act, "datasync")) |
| 369 | rw = DDIR_DATASYNC; |
| 370 | else if (!strcmp(act, "trim")) |
| 371 | rw = DDIR_TRIM; |
| 372 | else { |
| 373 | log_err("fio: bad iolog file action: %s\n", |
| 374 | act); |
| 375 | continue; |
| 376 | } |
Nikolaus Jeremic | 033ace1 | 2013-02-18 19:44:41 +0100 | [diff] [blame] | 377 | fileno = get_fileno(td, fname); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 378 | } else if (r == 2) { |
| 379 | rw = DDIR_INVAL; |
| 380 | if (!strcmp(act, "add")) { |
Jens Axboe | 5903e7b | 2014-02-26 13:42:13 -0800 | [diff] [blame] | 381 | fileno = add_file(td, fname, 0, 1); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 382 | file_action = FIO_LOG_ADD_FILE; |
| 383 | continue; |
| 384 | } else if (!strcmp(act, "open")) { |
| 385 | fileno = get_fileno(td, fname); |
| 386 | file_action = FIO_LOG_OPEN_FILE; |
| 387 | } else if (!strcmp(act, "close")) { |
| 388 | fileno = get_fileno(td, fname); |
| 389 | file_action = FIO_LOG_CLOSE_FILE; |
| 390 | } else { |
| 391 | log_err("fio: bad iolog file action: %s\n", |
| 392 | act); |
| 393 | continue; |
| 394 | } |
| 395 | } else { |
| 396 | log_err("bad iolog2: %s", p); |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | if (rw == DDIR_READ) |
| 401 | reads++; |
| 402 | else if (rw == DDIR_WRITE) { |
| 403 | /* |
| 404 | * Don't add a write for ro mode |
| 405 | */ |
| 406 | if (read_only) |
| 407 | continue; |
| 408 | writes++; |
| 409 | } else if (rw == DDIR_WAIT) { |
| 410 | waits++; |
| 411 | } else if (rw == DDIR_INVAL) { |
| 412 | } else if (!ddir_sync(rw)) { |
| 413 | log_err("bad ddir: %d\n", rw); |
| 414 | continue; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Make note of file |
| 419 | */ |
| 420 | ipo = malloc(sizeof(*ipo)); |
| 421 | init_ipo(ipo); |
| 422 | ipo->ddir = rw; |
| 423 | if (rw == DDIR_WAIT) { |
| 424 | ipo->delay = offset; |
| 425 | } else { |
| 426 | ipo->offset = offset; |
| 427 | ipo->len = bytes; |
Jens Axboe | 42793d9 | 2014-04-08 21:18:37 -0600 | [diff] [blame] | 428 | if (rw != DDIR_INVAL && bytes > td->o.max_bs[rw]) |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 429 | td->o.max_bs[rw] = bytes; |
| 430 | ipo->fileno = fileno; |
| 431 | ipo->file_action = file_action; |
Jens Axboe | ecf5369 | 2014-06-09 19:56:51 -0600 | [diff] [blame] | 432 | td->o.size += bytes; |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 433 | } |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 434 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 435 | queue_io_piece(td, ipo); |
| 436 | } |
| 437 | |
| 438 | free(str); |
| 439 | free(act); |
| 440 | free(fname); |
| 441 | |
| 442 | if (writes && read_only) { |
| 443 | log_err("fio: <%s> skips replay of %d writes due to" |
| 444 | " read-only\n", td->o.name, writes); |
| 445 | writes = 0; |
| 446 | } |
| 447 | |
| 448 | if (!reads && !writes && !waits) |
| 449 | return 1; |
| 450 | else if (reads && !writes) |
| 451 | td->o.td_ddir = TD_DDIR_READ; |
| 452 | else if (!reads && writes) |
| 453 | td->o.td_ddir = TD_DDIR_WRITE; |
| 454 | else |
| 455 | td->o.td_ddir = TD_DDIR_RW; |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * open iolog, check version, and call appropriate parser |
| 462 | */ |
| 463 | static int init_iolog_read(struct thread_data *td) |
| 464 | { |
| 465 | char buffer[256], *p; |
| 466 | FILE *f; |
| 467 | int ret; |
| 468 | |
| 469 | f = fopen(td->o.read_iolog_file, "r"); |
| 470 | if (!f) { |
| 471 | perror("fopen read iolog"); |
| 472 | return 1; |
| 473 | } |
| 474 | |
| 475 | p = fgets(buffer, sizeof(buffer), f); |
| 476 | if (!p) { |
| 477 | td_verror(td, errno, "iolog read"); |
| 478 | log_err("fio: unable to read iolog\n"); |
| 479 | fclose(f); |
| 480 | return 1; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * version 2 of the iolog stores a specific string as the |
| 485 | * first line, check for that |
| 486 | */ |
| 487 | if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2))) |
| 488 | ret = read_iolog2(td, f); |
| 489 | else { |
| 490 | log_err("fio: iolog version 1 is no longer supported\n"); |
| 491 | ret = 1; |
| 492 | } |
| 493 | |
| 494 | fclose(f); |
| 495 | return ret; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Set up a log for storing io patterns. |
| 500 | */ |
| 501 | static int init_iolog_write(struct thread_data *td) |
| 502 | { |
| 503 | struct fio_file *ff; |
| 504 | FILE *f; |
| 505 | unsigned int i; |
| 506 | |
| 507 | f = fopen(td->o.write_iolog_file, "a"); |
| 508 | if (!f) { |
| 509 | perror("fopen write iolog"); |
| 510 | return 1; |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * That's it for writing, setup a log buffer and we're done. |
| 515 | */ |
| 516 | td->iolog_f = f; |
| 517 | td->iolog_buf = malloc(8192); |
| 518 | setvbuf(f, td->iolog_buf, _IOFBF, 8192); |
| 519 | |
| 520 | /* |
| 521 | * write our version line |
| 522 | */ |
| 523 | if (fprintf(f, "%s\n", iolog_ver2) < 0) { |
| 524 | perror("iolog init\n"); |
| 525 | return 1; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * add all known files |
| 530 | */ |
| 531 | for_each_file(td, ff, i) |
| 532 | log_file(td, ff, FIO_LOG_ADD_FILE); |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | int init_iolog(struct thread_data *td) |
| 538 | { |
| 539 | int ret = 0; |
| 540 | |
| 541 | if (td->o.read_iolog_file) { |
Jens Axboe | d95b34a | 2013-11-21 09:55:49 -0700 | [diff] [blame] | 542 | int need_swap; |
| 543 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 544 | /* |
| 545 | * Check if it's a blktrace file and load that if possible. |
| 546 | * Otherwise assume it's a normal log file and load that. |
| 547 | */ |
Jens Axboe | d95b34a | 2013-11-21 09:55:49 -0700 | [diff] [blame] | 548 | if (is_blktrace(td->o.read_iolog_file, &need_swap)) |
| 549 | ret = load_blktrace(td, td->o.read_iolog_file, need_swap); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 550 | else |
| 551 | ret = init_iolog_read(td); |
| 552 | } else if (td->o.write_iolog_file) |
| 553 | ret = init_iolog_write(td); |
| 554 | |
Jens Axboe | f01b34a | 2013-11-21 11:13:12 -0700 | [diff] [blame] | 555 | if (ret) |
| 556 | td_verror(td, EINVAL, "failed initializing iolog"); |
| 557 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 558 | return ret; |
| 559 | } |
| 560 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 561 | void setup_log(struct io_log **log, struct log_params *p, |
| 562 | const char *filename) |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 563 | { |
Jens Axboe | 6a27ed9 | 2014-09-30 10:08:08 -0600 | [diff] [blame] | 564 | struct io_log *l; |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 565 | |
Jens Axboe | 6a27ed9 | 2014-09-30 10:08:08 -0600 | [diff] [blame] | 566 | l = calloc(1, sizeof(*l)); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 567 | l->nr_samples = 0; |
| 568 | l->max_samples = 1024; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 569 | l->log_type = p->log_type; |
| 570 | l->log_offset = p->log_offset; |
| 571 | l->log_gz = p->log_gz; |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 572 | l->log_gz_store = p->log_gz_store; |
Jens Axboe | ccefd5f | 2014-06-30 20:59:03 -0600 | [diff] [blame] | 573 | l->log = malloc(l->max_samples * log_entry_sz(l)); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 574 | l->avg_msec = p->avg_msec; |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 575 | l->filename = strdup(filename); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 576 | l->td = p->td; |
| 577 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 578 | if (l->log_offset) |
Jens Axboe | 238934c | 2014-07-03 18:01:11 -0600 | [diff] [blame] | 579 | l->log_ddir_mask = LOG_OFFSET_SAMPLE_BIT; |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 580 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 581 | INIT_FLIST_HEAD(&l->chunk_list); |
| 582 | |
| 583 | if (l->log_gz && !p->td) |
| 584 | l->log_gz = 0; |
| 585 | else if (l->log_gz) { |
| 586 | pthread_mutex_init(&l->chunk_lock, NULL); |
| 587 | p->td->flags |= TD_F_COMPRESS_LOG; |
| 588 | } |
| 589 | |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 590 | *log = l; |
| 591 | } |
| 592 | |
Jens Axboe | 2e80228 | 2014-04-02 21:47:27 -0600 | [diff] [blame] | 593 | #ifdef CONFIG_SETVBUF |
| 594 | static void *set_file_buffer(FILE *f) |
| 595 | { |
| 596 | size_t size = 1048576; |
| 597 | void *buf; |
| 598 | |
| 599 | buf = malloc(size); |
| 600 | setvbuf(f, buf, _IOFBF, size); |
| 601 | return buf; |
| 602 | } |
| 603 | |
| 604 | static void clear_file_buffer(void *buf) |
| 605 | { |
| 606 | free(buf); |
| 607 | } |
| 608 | #else |
| 609 | static void *set_file_buffer(FILE *f) |
| 610 | { |
| 611 | return NULL; |
| 612 | } |
| 613 | |
| 614 | static void clear_file_buffer(void *buf) |
| 615 | { |
| 616 | } |
| 617 | #endif |
| 618 | |
Jens Axboe | 633d825 | 2014-07-02 13:36:34 -0600 | [diff] [blame] | 619 | void free_log(struct io_log *log) |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 620 | { |
| 621 | free(log->log); |
| 622 | free(log->filename); |
| 623 | free(log); |
| 624 | } |
| 625 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 626 | static void flush_samples(FILE *f, void *samples, uint64_t sample_size) |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 627 | { |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 628 | struct io_sample *s; |
| 629 | int log_offset; |
| 630 | uint64_t i, nr_samples; |
| 631 | |
| 632 | if (!sample_size) |
| 633 | return; |
| 634 | |
| 635 | s = __get_sample(samples, 0, 0); |
Jens Axboe | 238934c | 2014-07-03 18:01:11 -0600 | [diff] [blame] | 636 | log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0; |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 637 | |
| 638 | nr_samples = sample_size / __log_entry_sz(log_offset); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 639 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 640 | for (i = 0; i < nr_samples; i++) { |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 641 | s = __get_sample(samples, log_offset, i); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 642 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 643 | if (!log_offset) { |
Jens Axboe | ccefd5f | 2014-06-30 20:59:03 -0600 | [diff] [blame] | 644 | fprintf(f, "%lu, %lu, %u, %u\n", |
| 645 | (unsigned long) s->time, |
| 646 | (unsigned long) s->val, |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 647 | io_sample_ddir(s), s->bs); |
Jens Axboe | ccefd5f | 2014-06-30 20:59:03 -0600 | [diff] [blame] | 648 | } else { |
| 649 | struct io_sample_offset *so = (void *) s; |
| 650 | |
| 651 | fprintf(f, "%lu, %lu, %u, %u, %llu\n", |
| 652 | (unsigned long) s->time, |
| 653 | (unsigned long) s->val, |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 654 | io_sample_ddir(s), s->bs, |
Jens Axboe | ccefd5f | 2014-06-30 20:59:03 -0600 | [diff] [blame] | 655 | (unsigned long long) so->offset); |
| 656 | } |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 657 | } |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | #ifdef CONFIG_ZLIB |
Jens Axboe | 8ef5133 | 2014-07-03 17:56:55 -0600 | [diff] [blame] | 661 | |
| 662 | struct iolog_flush_data { |
| 663 | struct tp_work work; |
| 664 | struct io_log *log; |
| 665 | void *samples; |
| 666 | uint64_t nr_samples; |
| 667 | }; |
| 668 | |
| 669 | struct iolog_compress { |
| 670 | struct flist_head list; |
| 671 | void *buf; |
| 672 | size_t len; |
| 673 | unsigned int seq; |
Jens Axboe | 8ef5133 | 2014-07-03 17:56:55 -0600 | [diff] [blame] | 674 | }; |
| 675 | |
| 676 | #define GZ_CHUNK 131072 |
| 677 | |
| 678 | static struct iolog_compress *get_new_chunk(unsigned int seq) |
| 679 | { |
| 680 | struct iolog_compress *c; |
| 681 | |
| 682 | c = malloc(sizeof(*c)); |
| 683 | INIT_FLIST_HEAD(&c->list); |
| 684 | c->buf = malloc(GZ_CHUNK); |
| 685 | c->len = 0; |
| 686 | c->seq = seq; |
Jens Axboe | 8ef5133 | 2014-07-03 17:56:55 -0600 | [diff] [blame] | 687 | return c; |
| 688 | } |
| 689 | |
| 690 | static void free_chunk(struct iolog_compress *ic) |
| 691 | { |
Jens Axboe | 375daae | 2014-07-03 17:58:49 -0600 | [diff] [blame] | 692 | free(ic->buf); |
| 693 | free(ic); |
Jens Axboe | 8ef5133 | 2014-07-03 17:56:55 -0600 | [diff] [blame] | 694 | } |
| 695 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 696 | static int z_stream_init(z_stream *stream, int gz_hdr) |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 697 | { |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 698 | int wbits = 15; |
| 699 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 700 | stream->zalloc = Z_NULL; |
| 701 | stream->zfree = Z_NULL; |
| 702 | stream->opaque = Z_NULL; |
| 703 | stream->next_in = Z_NULL; |
| 704 | |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 705 | /* |
| 706 | * zlib magic - add 32 for auto-detection of gz header or not, |
| 707 | * if we decide to store files in a gzip friendly format. |
| 708 | */ |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 709 | if (gz_hdr) |
| 710 | wbits += 32; |
| 711 | |
| 712 | if (inflateInit2(stream, wbits) != Z_OK) |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 713 | return 1; |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 718 | struct inflate_chunk_iter { |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 719 | unsigned int seq; |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 720 | int err; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 721 | void *buf; |
| 722 | size_t buf_size; |
| 723 | size_t buf_used; |
| 724 | size_t chunk_sz; |
| 725 | }; |
| 726 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 727 | static void finish_chunk(z_stream *stream, FILE *f, |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 728 | struct inflate_chunk_iter *iter) |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 729 | { |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 730 | int ret; |
| 731 | |
| 732 | ret = inflateEnd(stream); |
| 733 | if (ret != Z_OK) |
| 734 | log_err("fio: failed to end log inflation (%d)\n", ret); |
| 735 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 736 | flush_samples(f, iter->buf, iter->buf_used); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 737 | free(iter->buf); |
| 738 | iter->buf = NULL; |
| 739 | iter->buf_size = iter->buf_used = 0; |
| 740 | } |
| 741 | |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 742 | /* |
| 743 | * Iterative chunk inflation. Handles cases where we cross into a new |
| 744 | * sequence, doing flush finish of previous chunk if needed. |
| 745 | */ |
| 746 | static size_t inflate_chunk(struct iolog_compress *ic, int gz_hdr, FILE *f, |
| 747 | z_stream *stream, struct inflate_chunk_iter *iter) |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 748 | { |
Jens Axboe | 4f3fe6c | 2014-07-09 23:01:03 +0200 | [diff] [blame] | 749 | size_t ret; |
| 750 | |
| 751 | dprint(FD_COMPRESS, "inflate chunk size=%lu, seq=%u", |
| 752 | (unsigned long) ic->len, ic->seq); |
| 753 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 754 | if (ic->seq != iter->seq) { |
| 755 | if (iter->seq) |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 756 | finish_chunk(stream, f, iter); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 757 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 758 | z_stream_init(stream, gz_hdr); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 759 | iter->seq = ic->seq; |
| 760 | } |
| 761 | |
| 762 | stream->avail_in = ic->len; |
| 763 | stream->next_in = ic->buf; |
| 764 | |
| 765 | if (!iter->buf_size) { |
| 766 | iter->buf_size = iter->chunk_sz; |
| 767 | iter->buf = malloc(iter->buf_size); |
| 768 | } |
| 769 | |
| 770 | while (stream->avail_in) { |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 771 | size_t this_out = iter->buf_size - iter->buf_used; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 772 | int err; |
| 773 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 774 | stream->avail_out = this_out; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 775 | stream->next_out = iter->buf + iter->buf_used; |
| 776 | |
| 777 | err = inflate(stream, Z_NO_FLUSH); |
| 778 | if (err < 0) { |
| 779 | log_err("fio: failed inflating log: %d\n", err); |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 780 | iter->err = err; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 781 | break; |
| 782 | } |
| 783 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 784 | iter->buf_used += this_out - stream->avail_out; |
| 785 | |
| 786 | if (!stream->avail_out) { |
| 787 | iter->buf_size += iter->chunk_sz; |
| 788 | iter->buf = realloc(iter->buf, iter->buf_size); |
| 789 | continue; |
| 790 | } |
| 791 | |
| 792 | if (err == Z_STREAM_END) |
| 793 | break; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 794 | } |
| 795 | |
Jens Axboe | 4f3fe6c | 2014-07-09 23:01:03 +0200 | [diff] [blame] | 796 | ret = (void *) stream->next_in - ic->buf; |
| 797 | |
| 798 | dprint(FD_COMPRESS, "inflated to size=%lu\n", (unsigned long) ret); |
| 799 | |
| 800 | return ret; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 801 | } |
| 802 | |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 803 | /* |
| 804 | * Inflate stored compressed chunks, or write them directly to the log |
| 805 | * file if so instructed. |
| 806 | */ |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 807 | static int inflate_gz_chunks(struct io_log *log, FILE *f) |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 808 | { |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 809 | struct inflate_chunk_iter iter = { .chunk_sz = log->log_gz, }; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 810 | z_stream stream; |
| 811 | |
| 812 | while (!flist_empty(&log->chunk_list)) { |
| 813 | struct iolog_compress *ic; |
| 814 | |
Jens Axboe | 12dbd06 | 2014-07-03 21:19:57 -0600 | [diff] [blame] | 815 | ic = flist_first_entry(&log->chunk_list, struct iolog_compress, list); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 816 | flist_del(&ic->list); |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 817 | |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 818 | if (log->log_gz_store) { |
| 819 | size_t ret; |
| 820 | |
Jens Axboe | 4f3fe6c | 2014-07-09 23:01:03 +0200 | [diff] [blame] | 821 | dprint(FD_COMPRESS, "log write chunk size=%lu, " |
| 822 | "seq=%u\n", (unsigned long) ic->len, ic->seq); |
| 823 | |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 824 | ret = fwrite(ic->buf, ic->len, 1, f); |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 825 | if (ret != 1 || ferror(f)) { |
| 826 | iter.err = errno; |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 827 | log_err("fio: error writing compressed log\n"); |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 828 | } |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 829 | } else |
| 830 | inflate_chunk(ic, log->log_gz_store, f, &stream, &iter); |
Jens Axboe | 375daae | 2014-07-03 17:58:49 -0600 | [diff] [blame] | 831 | |
| 832 | free_chunk(ic); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | if (iter.seq) { |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 836 | finish_chunk(&stream, f, &iter); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 837 | free(iter.buf); |
| 838 | } |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 839 | |
| 840 | return iter.err; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 841 | } |
| 842 | |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 843 | /* |
| 844 | * Open compressed log file and decompress the stored chunks and |
| 845 | * write them to stdout. The chunks are stored sequentially in the |
| 846 | * file, so we iterate over them and do them one-by-one. |
| 847 | */ |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 848 | int iolog_file_inflate(const char *file) |
| 849 | { |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 850 | struct inflate_chunk_iter iter = { .chunk_sz = 64 * 1024 * 1024, }; |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 851 | struct iolog_compress ic; |
| 852 | z_stream stream; |
| 853 | struct stat sb; |
Jens Axboe | 51e7384 | 2014-07-03 15:32:07 -0600 | [diff] [blame] | 854 | ssize_t ret; |
Jens Axboe | 66cf52d | 2014-07-03 21:57:29 -0600 | [diff] [blame] | 855 | size_t total; |
| 856 | void *buf; |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 857 | FILE *f; |
| 858 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 859 | f = fopen(file, "r"); |
| 860 | if (!f) { |
| 861 | perror("fopen"); |
| 862 | return 1; |
| 863 | } |
| 864 | |
Jens Axboe | 51e7384 | 2014-07-03 15:32:07 -0600 | [diff] [blame] | 865 | if (stat(file, &sb) < 0) { |
| 866 | fclose(f); |
| 867 | perror("stat"); |
| 868 | return 1; |
| 869 | } |
| 870 | |
Jens Axboe | 66cf52d | 2014-07-03 21:57:29 -0600 | [diff] [blame] | 871 | ic.buf = buf = malloc(sb.st_size); |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 872 | ic.len = sb.st_size; |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 873 | ic.seq = 1; |
| 874 | |
| 875 | ret = fread(ic.buf, ic.len, 1, f); |
| 876 | if (ret < 0) { |
| 877 | perror("fread"); |
| 878 | fclose(f); |
Jens Axboe | 90e390e | 2015-02-19 08:47:34 -0800 | [diff] [blame] | 879 | free(buf); |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 880 | return 1; |
| 881 | } else if (ret != 1) { |
| 882 | log_err("fio: short read on reading log\n"); |
| 883 | fclose(f); |
Jens Axboe | 90e390e | 2015-02-19 08:47:34 -0800 | [diff] [blame] | 884 | free(buf); |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 885 | return 1; |
| 886 | } |
| 887 | |
| 888 | fclose(f); |
| 889 | |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 890 | /* |
| 891 | * Each chunk will return Z_STREAM_END. We don't know how many |
| 892 | * chunks are in the file, so we just keep looping and incrementing |
| 893 | * the sequence number until we have consumed the whole compressed |
| 894 | * file. |
| 895 | */ |
Jens Axboe | 66cf52d | 2014-07-03 21:57:29 -0600 | [diff] [blame] | 896 | total = ic.len; |
| 897 | do { |
Jens Axboe | 0f7f9a9 | 2014-11-06 09:21:10 -0700 | [diff] [blame] | 898 | size_t iret; |
Jens Axboe | 66cf52d | 2014-07-03 21:57:29 -0600 | [diff] [blame] | 899 | |
Jens Axboe | 0f7f9a9 | 2014-11-06 09:21:10 -0700 | [diff] [blame] | 900 | iret = inflate_chunk(&ic, 1, stdout, &stream, &iter); |
| 901 | total -= iret; |
Jens Axboe | 66cf52d | 2014-07-03 21:57:29 -0600 | [diff] [blame] | 902 | if (!total) |
| 903 | break; |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 904 | if (iter.err) |
| 905 | break; |
Jens Axboe | 66cf52d | 2014-07-03 21:57:29 -0600 | [diff] [blame] | 906 | |
| 907 | ic.seq++; |
Jens Axboe | 0f7f9a9 | 2014-11-06 09:21:10 -0700 | [diff] [blame] | 908 | ic.len -= iret; |
| 909 | ic.buf += iret; |
Jens Axboe | 66cf52d | 2014-07-03 21:57:29 -0600 | [diff] [blame] | 910 | } while (1); |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 911 | |
| 912 | if (iter.seq) { |
| 913 | finish_chunk(&stream, stdout, &iter); |
| 914 | free(iter.buf); |
| 915 | } |
| 916 | |
Jens Axboe | 66cf52d | 2014-07-03 21:57:29 -0600 | [diff] [blame] | 917 | free(buf); |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 918 | return iter.err; |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 919 | } |
| 920 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 921 | #else |
| 922 | |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 923 | static int inflate_gz_chunks(struct io_log *log, FILE *f) |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 924 | { |
Jens Axboe | 1540fa7 | 2014-07-08 14:18:13 +0200 | [diff] [blame] | 925 | return 0; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 926 | } |
| 927 | |
Jens Axboe | b0adca3 | 2014-07-08 14:24:54 +0200 | [diff] [blame] | 928 | int iolog_file_inflate(const char *file) |
| 929 | { |
| 930 | log_err("fio: log inflation not possible without zlib\n"); |
| 931 | return 1; |
| 932 | } |
| 933 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 934 | #endif |
| 935 | |
| 936 | void flush_log(struct io_log *log) |
| 937 | { |
| 938 | void *buf; |
| 939 | FILE *f; |
| 940 | |
| 941 | f = fopen(log->filename, "w"); |
| 942 | if (!f) { |
| 943 | perror("fopen log"); |
| 944 | return; |
| 945 | } |
| 946 | |
| 947 | buf = set_file_buffer(f); |
| 948 | |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 949 | inflate_gz_chunks(log, f); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 950 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 951 | flush_samples(f, log->log, log->nr_samples * log_entry_sz(log)); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 952 | |
| 953 | fclose(f); |
Jens Axboe | 2e80228 | 2014-04-02 21:47:27 -0600 | [diff] [blame] | 954 | clear_file_buffer(buf); |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 955 | } |
| 956 | |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 957 | static int finish_log(struct thread_data *td, struct io_log *log, int trylock) |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 958 | { |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 959 | if (td->tp_data) |
| 960 | iolog_flush(log, 1); |
| 961 | |
Jens Axboe | 243bfe1 | 2014-04-02 15:46:58 -0600 | [diff] [blame] | 962 | if (trylock) { |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 963 | if (fio_trylock_file(log->filename)) |
Jens Axboe | 243bfe1 | 2014-04-02 15:46:58 -0600 | [diff] [blame] | 964 | return 1; |
| 965 | } else |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 966 | fio_lock_file(log->filename); |
Jens Axboe | 243bfe1 | 2014-04-02 15:46:58 -0600 | [diff] [blame] | 967 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 968 | if (td->client_type == FIO_CLIENT_TYPE_GUI) |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 969 | fio_send_iolog(td, log, log->filename); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 970 | else |
| 971 | flush_log(log); |
Jens Axboe | 243bfe1 | 2014-04-02 15:46:58 -0600 | [diff] [blame] | 972 | |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 973 | fio_unlock_file(log->filename); |
Jens Axboe | 633d825 | 2014-07-02 13:36:34 -0600 | [diff] [blame] | 974 | free_log(log); |
Jens Axboe | 243bfe1 | 2014-04-02 15:46:58 -0600 | [diff] [blame] | 975 | return 0; |
Jens Axboe | ac9b910 | 2011-10-01 15:01:08 -0400 | [diff] [blame] | 976 | } |
| 977 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 978 | #ifdef CONFIG_ZLIB |
| 979 | |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 980 | /* |
| 981 | * Invoked from our compress helper thread, when logging would have exceeded |
| 982 | * the specified memory limitation. Compresses the previously stored |
| 983 | * entries. |
| 984 | */ |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 985 | static int gz_work(struct tp_work *work) |
| 986 | { |
| 987 | struct iolog_flush_data *data; |
| 988 | struct iolog_compress *c; |
| 989 | struct flist_head list; |
| 990 | unsigned int seq; |
| 991 | z_stream stream; |
| 992 | size_t total = 0; |
Jens Axboe | aba6b60 | 2014-07-03 14:17:45 -0600 | [diff] [blame] | 993 | int ret; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 994 | |
| 995 | INIT_FLIST_HEAD(&list); |
| 996 | |
| 997 | data = container_of(work, struct iolog_flush_data, work); |
| 998 | |
| 999 | stream.zalloc = Z_NULL; |
| 1000 | stream.zfree = Z_NULL; |
| 1001 | stream.opaque = Z_NULL; |
| 1002 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 1003 | ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION); |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 1004 | if (ret != Z_OK) { |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 1005 | log_err("fio: failed to init gz stream\n"); |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | seq = ++data->log->chunk_seq; |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 1010 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 1011 | stream.next_in = (void *) data->samples; |
| 1012 | stream.avail_in = data->nr_samples * log_entry_sz(data->log); |
| 1013 | |
Jens Axboe | 4f3fe6c | 2014-07-09 23:01:03 +0200 | [diff] [blame] | 1014 | dprint(FD_COMPRESS, "deflate input size=%lu, seq=%u\n", |
| 1015 | (unsigned long) stream.avail_in, seq); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 1016 | do { |
| 1017 | c = get_new_chunk(seq); |
| 1018 | stream.avail_out = GZ_CHUNK; |
| 1019 | stream.next_out = c->buf; |
| 1020 | ret = deflate(&stream, Z_NO_FLUSH); |
| 1021 | if (ret < 0) { |
| 1022 | log_err("fio: deflate log (%d)\n", ret); |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 1023 | free_chunk(c); |
| 1024 | goto err; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | c->len = GZ_CHUNK - stream.avail_out; |
| 1028 | flist_add_tail(&c->list, &list); |
| 1029 | total += c->len; |
| 1030 | } while (stream.avail_in); |
| 1031 | |
| 1032 | stream.next_out = c->buf + c->len; |
| 1033 | stream.avail_out = GZ_CHUNK - c->len; |
| 1034 | |
| 1035 | ret = deflate(&stream, Z_FINISH); |
| 1036 | if (ret == Z_STREAM_END) |
| 1037 | c->len = GZ_CHUNK - stream.avail_out; |
| 1038 | else { |
| 1039 | do { |
| 1040 | c = get_new_chunk(seq); |
| 1041 | stream.avail_out = GZ_CHUNK; |
| 1042 | stream.next_out = c->buf; |
| 1043 | ret = deflate(&stream, Z_FINISH); |
| 1044 | c->len = GZ_CHUNK - stream.avail_out; |
Jens Axboe | 4f3fe6c | 2014-07-09 23:01:03 +0200 | [diff] [blame] | 1045 | total += c->len; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 1046 | flist_add_tail(&c->list, &list); |
| 1047 | } while (ret != Z_STREAM_END); |
| 1048 | } |
| 1049 | |
Jens Axboe | 4f3fe6c | 2014-07-09 23:01:03 +0200 | [diff] [blame] | 1050 | dprint(FD_COMPRESS, "deflated to size=%lu\n", (unsigned long) total); |
| 1051 | |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 1052 | ret = deflateEnd(&stream); |
| 1053 | if (ret != Z_OK) |
| 1054 | log_err("fio: deflateEnd %d\n", ret); |
| 1055 | |
| 1056 | free(data->samples); |
| 1057 | |
| 1058 | if (!flist_empty(&list)) { |
| 1059 | pthread_mutex_lock(&data->log->chunk_lock); |
| 1060 | flist_splice_tail(&list, &data->log->chunk_list); |
| 1061 | pthread_mutex_unlock(&data->log->chunk_lock); |
| 1062 | } |
| 1063 | |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 1064 | ret = 0; |
| 1065 | done: |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 1066 | if (work->wait) { |
| 1067 | work->done = 1; |
| 1068 | pthread_cond_signal(&work->cv); |
| 1069 | } else |
| 1070 | free(data); |
| 1071 | |
Jens Axboe | d465064 | 2014-07-08 14:14:23 +0200 | [diff] [blame] | 1072 | return ret; |
| 1073 | err: |
| 1074 | while (!flist_empty(&list)) { |
| 1075 | c = flist_first_entry(list.next, struct iolog_compress, list); |
| 1076 | flist_del(&c->list); |
| 1077 | free_chunk(c); |
| 1078 | } |
| 1079 | ret = 1; |
| 1080 | goto done; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 1081 | } |
| 1082 | |
Jens Axboe | c5971cd | 2014-07-08 09:44:51 +0200 | [diff] [blame] | 1083 | /* |
| 1084 | * Queue work item to compress the existing log entries. We copy the |
| 1085 | * samples, and reset the log sample count to 0 (so the logging will |
| 1086 | * continue to use the memory associated with the log). If called with |
| 1087 | * wait == 1, will not return until the log compression has completed. |
| 1088 | */ |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 1089 | int iolog_flush(struct io_log *log, int wait) |
| 1090 | { |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 1091 | struct tp_data *tdat = log->td->tp_data; |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 1092 | struct iolog_flush_data *data; |
| 1093 | size_t sample_size; |
| 1094 | |
| 1095 | data = malloc(sizeof(*data)); |
| 1096 | if (!data) |
| 1097 | return 1; |
| 1098 | |
| 1099 | data->log = log; |
| 1100 | |
| 1101 | sample_size = log->nr_samples * log_entry_sz(log); |
| 1102 | data->samples = malloc(sample_size); |
| 1103 | if (!data->samples) { |
| 1104 | free(data); |
| 1105 | return 1; |
| 1106 | } |
| 1107 | |
| 1108 | memcpy(data->samples, log->log, sample_size); |
| 1109 | data->nr_samples = log->nr_samples; |
| 1110 | data->work.fn = gz_work; |
| 1111 | log->nr_samples = 0; |
| 1112 | |
| 1113 | if (wait) { |
| 1114 | pthread_mutex_init(&data->work.lock, NULL); |
| 1115 | pthread_cond_init(&data->work.cv, NULL); |
| 1116 | data->work.wait = 1; |
| 1117 | } else |
| 1118 | data->work.wait = 0; |
| 1119 | |
Jens Axboe | c9a5f39 | 2014-07-09 09:52:20 +0200 | [diff] [blame] | 1120 | data->work.prio = 1; |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 1121 | tp_queue_work(tdat, &data->work); |
Jens Axboe | 38a812d | 2014-07-03 09:10:39 -0600 | [diff] [blame] | 1122 | |
| 1123 | if (wait) { |
| 1124 | pthread_mutex_lock(&data->work.lock); |
| 1125 | while (!data->work.done) |
| 1126 | pthread_cond_wait(&data->work.cv, &data->work.lock); |
| 1127 | pthread_mutex_unlock(&data->work.lock); |
| 1128 | free(data); |
| 1129 | } |
| 1130 | |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
| 1134 | #else |
| 1135 | |
| 1136 | int iolog_flush(struct io_log *log, int wait) |
| 1137 | { |
| 1138 | return 1; |
| 1139 | } |
| 1140 | |
| 1141 | #endif |
| 1142 | |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1143 | static int write_iops_log(struct thread_data *td, int try) |
| 1144 | { |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 1145 | struct io_log *log = td->iops_log; |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1146 | |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 1147 | if (!log) |
| 1148 | return 0; |
| 1149 | |
| 1150 | return finish_log(td, log, try); |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | static int write_slat_log(struct thread_data *td, int try) |
| 1154 | { |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 1155 | struct io_log *log = td->slat_log; |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1156 | |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 1157 | if (!log) |
| 1158 | return 0; |
| 1159 | |
| 1160 | return finish_log(td, log, try); |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | static int write_clat_log(struct thread_data *td, int try) |
| 1164 | { |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 1165 | struct io_log *log = td->clat_log; |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1166 | |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 1167 | if (!log) |
| 1168 | return 0; |
| 1169 | |
| 1170 | return finish_log(td, log, try); |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | static int write_lat_log(struct thread_data *td, int try) |
| 1174 | { |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 1175 | struct io_log *log = td->lat_log; |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1176 | |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 1177 | if (!log) |
| 1178 | return 0; |
| 1179 | |
| 1180 | return finish_log(td, log, try); |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | static int write_bandw_log(struct thread_data *td, int try) |
| 1184 | { |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 1185 | struct io_log *log = td->bw_log; |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1186 | |
Jens Axboe | 987c4f4 | 2014-07-01 16:29:12 -0600 | [diff] [blame] | 1187 | if (!log) |
| 1188 | return 0; |
| 1189 | |
| 1190 | return finish_log(td, log, try); |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | enum { |
| 1194 | BW_LOG_MASK = 1, |
| 1195 | LAT_LOG_MASK = 2, |
| 1196 | SLAT_LOG_MASK = 4, |
| 1197 | CLAT_LOG_MASK = 8, |
| 1198 | IOPS_LOG_MASK = 16, |
| 1199 | |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1200 | ALL_LOG_NR = 5, |
| 1201 | }; |
| 1202 | |
| 1203 | struct log_type { |
| 1204 | unsigned int mask; |
| 1205 | int (*fn)(struct thread_data *, int); |
| 1206 | }; |
| 1207 | |
| 1208 | static struct log_type log_types[] = { |
| 1209 | { |
| 1210 | .mask = BW_LOG_MASK, |
| 1211 | .fn = write_bandw_log, |
| 1212 | }, |
| 1213 | { |
| 1214 | .mask = LAT_LOG_MASK, |
| 1215 | .fn = write_lat_log, |
| 1216 | }, |
| 1217 | { |
| 1218 | .mask = SLAT_LOG_MASK, |
| 1219 | .fn = write_slat_log, |
| 1220 | }, |
| 1221 | { |
| 1222 | .mask = CLAT_LOG_MASK, |
| 1223 | .fn = write_clat_log, |
| 1224 | }, |
| 1225 | { |
| 1226 | .mask = IOPS_LOG_MASK, |
| 1227 | .fn = write_iops_log, |
| 1228 | }, |
| 1229 | }; |
| 1230 | |
| 1231 | void fio_writeout_logs(struct thread_data *td) |
| 1232 | { |
Jens Axboe | ea5409f | 2014-04-02 20:07:28 -0600 | [diff] [blame] | 1233 | unsigned int log_mask = 0; |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1234 | unsigned int log_left = ALL_LOG_NR; |
| 1235 | int old_state, i; |
| 1236 | |
| 1237 | old_state = td_bump_runstate(td, TD_FINISHING); |
| 1238 | |
| 1239 | finalize_logs(td); |
| 1240 | |
| 1241 | while (log_left) { |
| 1242 | int prev_log_left = log_left; |
| 1243 | |
| 1244 | for (i = 0; i < ALL_LOG_NR && log_left; i++) { |
| 1245 | struct log_type *lt = &log_types[i]; |
| 1246 | int ret; |
| 1247 | |
Jens Axboe | ea5409f | 2014-04-02 20:07:28 -0600 | [diff] [blame] | 1248 | if (!(log_mask & lt->mask)) { |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1249 | ret = lt->fn(td, log_left != 1); |
| 1250 | if (!ret) { |
| 1251 | log_left--; |
Jens Axboe | ea5409f | 2014-04-02 20:07:28 -0600 | [diff] [blame] | 1252 | log_mask |= lt->mask; |
Jens Axboe | 905e3d4 | 2014-04-02 20:01:27 -0600 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | if (prev_log_left == log_left) |
| 1258 | usleep(5000); |
| 1259 | } |
| 1260 | |
| 1261 | td_restore_runstate(td, old_state); |
| 1262 | } |