Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [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 | */ |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 7 | #include <assert.h> |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 8 | #include "list.h" |
| 9 | #include "fio.h" |
| 10 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 11 | static const char iolog_ver2[] = "fio version 2 iolog"; |
| 12 | |
| 13 | void log_io_u(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 14 | { |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 15 | const char *act[] = { "read", "write", "sync" }; |
| 16 | |
| 17 | assert(io_u->ddir < 3); |
| 18 | |
| 19 | if (!td->o.write_iolog_file) |
| 20 | return; |
| 21 | |
| 22 | fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name, act[io_u->ddir], io_u->offset, io_u->buflen); |
| 23 | } |
| 24 | |
| 25 | void log_file(struct thread_data *td, struct fio_file *f, |
| 26 | enum file_log_act what) |
| 27 | { |
| 28 | const char *act[] = { "add", "open", "close" }; |
| 29 | |
| 30 | assert(what < 3); |
| 31 | |
| 32 | if (!td->o.write_iolog_file) |
| 33 | return; |
| 34 | |
| 35 | fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Jens Axboe | a61edde | 2007-05-15 14:29:58 +0200 | [diff] [blame] | 38 | static void iolog_delay(struct thread_data *td, unsigned long delay) |
| 39 | { |
| 40 | unsigned long usec = utime_since_now(&td->last_issue); |
| 41 | |
| 42 | if (delay < usec) |
| 43 | return; |
| 44 | |
| 45 | delay -= usec; |
| 46 | |
| 47 | /* |
| 48 | * less than 100 usec delay, just regard it as noise |
| 49 | */ |
| 50 | if (delay < 100) |
| 51 | return; |
| 52 | |
| 53 | usec_sleep(td, delay); |
| 54 | } |
| 55 | |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 56 | int read_iolog_get(struct thread_data *td, struct io_u *io_u) |
| 57 | { |
| 58 | struct io_piece *ipo; |
| 59 | |
Jens Axboe | b3f4b4f | 2007-07-23 11:23:26 +0200 | [diff] [blame] | 60 | while (!list_empty(&td->io_log_list)) { |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 61 | ipo = list_entry(td->io_log_list.next, struct io_piece, list); |
| 62 | list_del(&ipo->list); |
Jens Axboe | a61edde | 2007-05-15 14:29:58 +0200 | [diff] [blame] | 63 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 64 | /* |
| 65 | * invalid ddir, this is a file action |
| 66 | */ |
Jens Axboe | 429f667 | 2007-07-23 10:38:43 +0200 | [diff] [blame] | 67 | if (ipo->ddir == DDIR_INVAL) { |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 68 | struct fio_file *f = &td->files[ipo->fileno]; |
| 69 | |
| 70 | if (ipo->file_action == FIO_LOG_OPEN_FILE) { |
| 71 | assert(!td_io_open_file(td, f)); |
| 72 | free(ipo); |
Jens Axboe | b3f4b4f | 2007-07-23 11:23:26 +0200 | [diff] [blame] | 73 | continue; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 74 | } else if (ipo->file_action == FIO_LOG_CLOSE_FILE) { |
| 75 | td_io_close_file(td, f); |
| 76 | free(ipo); |
Jens Axboe | b3f4b4f | 2007-07-23 11:23:26 +0200 | [diff] [blame] | 77 | continue; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Jens Axboe | 429f667 | 2007-07-23 10:38:43 +0200 | [diff] [blame] | 81 | io_u->offset = ipo->offset; |
| 82 | io_u->buflen = ipo->len; |
| 83 | io_u->ddir = ipo->ddir; |
| 84 | io_u->file = &td->files[ipo->fileno]; |
| 85 | get_file(io_u->file); |
| 86 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 87 | dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset, |
| 88 | io_u->buflen, io_u->file->file_name); |
| 89 | |
Jens Axboe | a61edde | 2007-05-15 14:29:58 +0200 | [diff] [blame] | 90 | if (ipo->delay) |
| 91 | iolog_delay(td, ipo->delay); |
| 92 | |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 93 | free(ipo); |
| 94 | return 0; |
| 95 | } |
| 96 | |
Jens Axboe | 20e354e | 2007-07-23 14:36:16 +0200 | [diff] [blame] | 97 | td->done = 1; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | void prune_io_piece_log(struct thread_data *td) |
| 102 | { |
| 103 | struct io_piece *ipo; |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 104 | struct rb_node *n; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 105 | |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 106 | while ((n = rb_first(&td->io_hist_tree)) != NULL) { |
| 107 | ipo = rb_entry(n, struct io_piece, rb_node); |
| 108 | rb_erase(n, &td->io_hist_tree); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 109 | free(ipo); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* |
Jens Axboe | 34403fb | 2007-03-02 21:43:25 +0100 | [diff] [blame] | 114 | * log a successful write, so we can unwind the log for verify |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 115 | */ |
| 116 | void log_io_piece(struct thread_data *td, struct io_u *io_u) |
| 117 | { |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 118 | struct rb_node **p, *parent; |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 119 | struct io_piece *ipo, *__ipo; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 120 | |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 121 | ipo = malloc(sizeof(struct io_piece)); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 122 | ipo->file = io_u->file; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 123 | ipo->offset = io_u->offset; |
| 124 | ipo->len = io_u->buflen; |
| 125 | |
| 126 | /* |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 127 | * We don't need to sort the entries, if: |
| 128 | * |
| 129 | * Sequential writes, or |
| 130 | * Random writes that lay out the file as it goes along |
| 131 | * |
| 132 | * For both these cases, just reading back data in the order we |
| 133 | * wrote it out is the fastest. |
| 134 | */ |
Jens Axboe | 9b23c9f | 2008-02-21 09:51:32 +0100 | [diff] [blame^] | 135 | if (!td_random(td) || !td->o.overwrite) { |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 136 | INIT_LIST_HEAD(&ipo->list); |
| 137 | list_add_tail(&ipo->list, &td->io_hist_list); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | RB_CLEAR_NODE(&ipo->rb_node); |
| 142 | p = &td->io_hist_tree.rb_node; |
| 143 | parent = NULL; |
| 144 | |
| 145 | /* |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 146 | * Sort the entry into the verification list |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 147 | */ |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 148 | while (*p) { |
| 149 | parent = *p; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 150 | |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 151 | __ipo = rb_entry(parent, struct io_piece, rb_node); |
Jens Axboe | bb5d7d0 | 2007-03-27 10:21:25 +0200 | [diff] [blame] | 152 | if (ipo->offset <= __ipo->offset) |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 153 | p = &(*p)->rb_left; |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 154 | else |
Jens Axboe | bb5d7d0 | 2007-03-27 10:21:25 +0200 | [diff] [blame] | 155 | p = &(*p)->rb_right; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 158 | rb_link_node(&ipo->rb_node, parent, p); |
| 159 | rb_insert_color(&ipo->rb_node, &td->io_hist_tree); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void write_iolog_close(struct thread_data *td) |
| 163 | { |
| 164 | fflush(td->iolog_f); |
| 165 | fclose(td->iolog_f); |
| 166 | free(td->iolog_buf); |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 167 | td->iolog_f = NULL; |
| 168 | td->iolog_buf = NULL; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 171 | /* |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 172 | * Read version 2 iolog data. It is enhanced to include per-file logging, |
| 173 | * syncs, etc. |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 174 | */ |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 175 | static int read_iolog2(struct thread_data *td, FILE *f) |
| 176 | { |
| 177 | unsigned long long offset; |
| 178 | unsigned int bytes; |
Jens Axboe | 53fa9b6 | 2007-07-23 11:25:39 +0200 | [diff] [blame] | 179 | int reads, writes, fileno = 0, file_action = 0; /* stupid gcc */ |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 180 | char *fname, *act; |
| 181 | char *str, *p; |
Jens Axboe | 53fa9b6 | 2007-07-23 11:25:39 +0200 | [diff] [blame] | 182 | enum fio_ddir rw; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 183 | |
| 184 | free_release_files(td); |
| 185 | |
| 186 | /* |
| 187 | * Read in the read iolog and store it, reuse the infrastructure |
| 188 | * for doing verifications. |
| 189 | */ |
| 190 | str = malloc(4096); |
| 191 | fname = malloc(256+16); |
| 192 | act = malloc(256+16); |
| 193 | |
| 194 | reads = writes = 0; |
| 195 | while ((p = fgets(str, 4096, f)) != NULL) { |
| 196 | struct io_piece *ipo; |
| 197 | int r; |
| 198 | |
| 199 | r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset, &bytes); |
| 200 | if (r == 4) { |
| 201 | /* |
| 202 | * Check action first |
| 203 | */ |
| 204 | if (!strcmp(act, "read")) |
| 205 | rw = DDIR_READ; |
| 206 | else if (!strcmp(act, "write")) |
| 207 | rw = DDIR_WRITE; |
| 208 | else if (!strcmp(act, "sync")) |
| 209 | rw = DDIR_SYNC; |
| 210 | else { |
| 211 | log_err("fio: bad iolog file action: %s\n",act); |
| 212 | continue; |
| 213 | } |
| 214 | } else if (r == 2) { |
| 215 | rw = DDIR_INVAL; |
| 216 | if (!strcmp(act, "add")) { |
| 217 | td->o.nr_files++; |
| 218 | fileno = add_file(td, fname); |
| 219 | file_action = FIO_LOG_ADD_FILE; |
| 220 | continue; |
| 221 | } else if (!strcmp(act, "open")) { |
| 222 | fileno = get_fileno(td, fname); |
| 223 | file_action = FIO_LOG_OPEN_FILE; |
| 224 | } else if (!strcmp(act, "close")) { |
| 225 | fileno = get_fileno(td, fname); |
| 226 | file_action = FIO_LOG_CLOSE_FILE; |
| 227 | } else { |
| 228 | log_err("fio: bad iolog file action: %s\n",act); |
| 229 | continue; |
| 230 | } |
| 231 | } else { |
| 232 | log_err("bad iolog2: %s", p); |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | if (rw == DDIR_READ) |
| 237 | reads++; |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 238 | else if (rw == DDIR_WRITE) { |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 239 | writes++; |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 240 | /* |
| 241 | * Don't add a write for ro mode |
| 242 | */ |
| 243 | if (read_only) |
| 244 | continue; |
| 245 | } else if (rw != DDIR_SYNC && rw != DDIR_INVAL) { |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 246 | log_err("bad ddir: %d\n", rw); |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Make note of file |
| 252 | */ |
| 253 | ipo = malloc(sizeof(*ipo)); |
| 254 | memset(ipo, 0, sizeof(*ipo)); |
| 255 | INIT_LIST_HEAD(&ipo->list); |
| 256 | ipo->offset = offset; |
| 257 | ipo->len = bytes; |
Jens Axboe | 53fa9b6 | 2007-07-23 11:25:39 +0200 | [diff] [blame] | 258 | ipo->ddir = rw; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 259 | if (bytes > td->o.max_bs[rw]) |
| 260 | td->o.max_bs[rw] = bytes; |
| 261 | if (rw == DDIR_INVAL) { |
| 262 | ipo->fileno = fileno; |
| 263 | ipo->file_action = file_action; |
| 264 | } |
| 265 | list_add_tail(&ipo->list, &td->io_log_list); |
Jens Axboe | ea966f8 | 2007-09-03 10:09:37 +0200 | [diff] [blame] | 266 | td->total_io_size += bytes; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | free(str); |
| 270 | free(act); |
| 271 | free(fname); |
| 272 | |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 273 | if (writes && read_only) { |
| 274 | log_err("fio: <%s> skips replay of %d writes due to read-only\n", td->o.name, writes); |
| 275 | writes = 0; |
| 276 | } |
| 277 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 278 | if (!reads && !writes) |
| 279 | return 1; |
| 280 | else if (reads && !writes) |
| 281 | td->o.td_ddir = TD_DDIR_READ; |
| 282 | else if (!reads && writes) |
| 283 | td->o.td_ddir = TD_DDIR_WRITE; |
| 284 | else |
| 285 | td->o.td_ddir = TD_DDIR_RW; |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Read version 1 iolog data. |
| 292 | */ |
| 293 | static int read_iolog(struct thread_data *td, FILE *f) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 294 | { |
| 295 | unsigned long long offset; |
| 296 | unsigned int bytes; |
| 297 | char *str, *p; |
Jens Axboe | 21bd298 | 2007-07-23 11:26:27 +0200 | [diff] [blame] | 298 | int reads, writes; |
gurudas pai | 59b9ddf | 2007-10-22 22:10:39 +0200 | [diff] [blame] | 299 | int rw; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 300 | |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 301 | /* |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 302 | * Read in the read iolog and store it, reuse the infrastructure |
| 303 | * for doing verifications. |
| 304 | */ |
| 305 | str = malloc(4096); |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 306 | reads = writes = 0; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 307 | while ((p = fgets(str, 4096, f)) != NULL) { |
| 308 | struct io_piece *ipo; |
| 309 | |
| 310 | if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 311 | log_err("bad iolog: %s\n", p); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 312 | continue; |
| 313 | } |
| 314 | if (rw == DDIR_READ) |
| 315 | reads++; |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 316 | else if (rw == DDIR_WRITE) { |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 317 | writes++; |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 318 | /* |
| 319 | * Don't add a write for ro mode |
| 320 | */ |
| 321 | if (read_only) |
| 322 | continue; |
| 323 | } else if (rw != DDIR_SYNC) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 324 | log_err("bad ddir: %d\n", rw); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 325 | continue; |
| 326 | } |
| 327 | |
| 328 | ipo = malloc(sizeof(*ipo)); |
Jens Axboe | 733ed59 | 2007-04-25 14:24:12 +0200 | [diff] [blame] | 329 | memset(ipo, 0, sizeof(*ipo)); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 330 | INIT_LIST_HEAD(&ipo->list); |
| 331 | ipo->offset = offset; |
| 332 | ipo->len = bytes; |
gurudas pai | 59b9ddf | 2007-10-22 22:10:39 +0200 | [diff] [blame] | 333 | ipo->ddir = (enum fio_ddir) rw; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 334 | if (bytes > td->o.max_bs[rw]) |
| 335 | td->o.max_bs[rw] = bytes; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 336 | list_add_tail(&ipo->list, &td->io_log_list); |
Jens Axboe | ea966f8 | 2007-09-03 10:09:37 +0200 | [diff] [blame] | 337 | td->total_io_size += bytes; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | free(str); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 341 | |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 342 | if (writes && read_only) { |
| 343 | log_err("fio: <%s> skips replay of %d writes due to read-only\n", td->o.name, writes); |
| 344 | writes = 0; |
| 345 | } |
| 346 | |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 347 | if (!reads && !writes) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 348 | return 1; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 349 | else if (reads && !writes) |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 350 | td->o.td_ddir = TD_DDIR_READ; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 351 | else if (!reads && writes) |
Jens Axboe | 36361eb | 2007-05-15 11:12:19 +0200 | [diff] [blame] | 352 | td->o.td_ddir = TD_DDIR_WRITE; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 353 | else |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 354 | td->o.td_ddir = TD_DDIR_RW; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 355 | |
| 356 | return 0; |
| 357 | } |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 358 | |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 359 | /* |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 360 | * open iolog, check version, and call appropriate parser |
| 361 | */ |
| 362 | static int init_iolog_read(struct thread_data *td) |
| 363 | { |
| 364 | char buffer[256], *p; |
| 365 | FILE *f; |
| 366 | int ret; |
| 367 | |
| 368 | f = fopen(td->o.read_iolog_file, "r"); |
| 369 | if (!f) { |
| 370 | perror("fopen read iolog"); |
| 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | p = fgets(buffer, sizeof(buffer), f); |
| 375 | if (!p) { |
| 376 | td_verror(td, errno, "iolog read"); |
| 377 | log_err("fio: unable to read iolog\n"); |
| 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * version 2 of the iolog stores a specific string as the |
| 383 | * first line, check for that |
| 384 | */ |
| 385 | if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2))) |
| 386 | ret = read_iolog2(td, f); |
| 387 | else { |
| 388 | /* |
| 389 | * seek back to the beginning |
| 390 | */ |
| 391 | if (fseek(f, 0, SEEK_SET) < 0) { |
| 392 | td_verror(td, errno, "iolog read"); |
| 393 | log_err("fio: unable to read iolog\n"); |
| 394 | return 1; |
| 395 | } |
| 396 | |
| 397 | ret = read_iolog(td, f); |
| 398 | } |
| 399 | |
| 400 | fclose(f); |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | /* |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 405 | * Setup a log for storing io patterns. |
| 406 | */ |
| 407 | static int init_iolog_write(struct thread_data *td) |
| 408 | { |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 409 | struct fio_file *ff; |
Jens Axboe | 076efc7 | 2006-10-27 11:24:25 +0200 | [diff] [blame] | 410 | FILE *f; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 411 | unsigned int i; |
Jens Axboe | 733ed59 | 2007-04-25 14:24:12 +0200 | [diff] [blame] | 412 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 413 | f = fopen(td->o.write_iolog_file, "w+"); |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 414 | if (!f) { |
| 415 | perror("fopen write iolog"); |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * That's it for writing, setup a log buffer and we're done. |
| 421 | */ |
| 422 | td->iolog_f = f; |
| 423 | td->iolog_buf = malloc(8192); |
| 424 | setvbuf(f, td->iolog_buf, _IOFBF, 8192); |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 425 | |
| 426 | /* |
| 427 | * write our version line |
| 428 | */ |
| 429 | if (fprintf(f, "%s\n", iolog_ver2) < 0) { |
| 430 | perror("iolog init\n"); |
| 431 | return 1; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * add all known files |
| 436 | */ |
| 437 | for_each_file(td, ff, i) |
| 438 | log_file(td, ff, FIO_LOG_ADD_FILE); |
| 439 | |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | int init_iolog(struct thread_data *td) |
| 444 | { |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 445 | int ret = 0; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 446 | |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 447 | if (td->io_ops->flags & FIO_DISKLESSIO) |
Jens Axboe | f48b467 | 2006-10-27 11:30:07 +0200 | [diff] [blame] | 448 | return 0; |
| 449 | |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 450 | if (td->o.read_iolog_file) { |
| 451 | /* |
| 452 | * Check if it's a blktrace file and load that if possible. |
| 453 | * Otherwise assume it's a normal log file and load that. |
| 454 | */ |
| 455 | if (is_blktrace(td->o.read_iolog_file)) |
| 456 | ret = load_blktrace(td, td->o.read_iolog_file); |
| 457 | else |
| 458 | ret = init_iolog_read(td); |
| 459 | } else if (td->o.write_iolog_file) |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 460 | ret = init_iolog_write(td); |
| 461 | |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 462 | return ret; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 463 | } |
| 464 | |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 465 | void setup_log(struct io_log **log) |
| 466 | { |
| 467 | struct io_log *l = malloc(sizeof(*l)); |
| 468 | |
| 469 | l->nr_samples = 0; |
| 470 | l->max_samples = 1024; |
| 471 | l->log = malloc(l->max_samples * sizeof(struct io_sample)); |
| 472 | *log = l; |
| 473 | } |
| 474 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 475 | void __finish_log(struct io_log *log, const char *name) |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 476 | { |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 477 | unsigned int i; |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 478 | FILE *f; |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 479 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 480 | f = fopen(name, "w"); |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 481 | if (!f) { |
| 482 | perror("fopen log"); |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | for (i = 0; i < log->nr_samples; i++) |
| 487 | fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir); |
| 488 | |
| 489 | fclose(f); |
| 490 | free(log->log); |
| 491 | free(log); |
| 492 | } |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 493 | |
| 494 | void finish_log(struct thread_data *td, struct io_log *log, const char *name) |
| 495 | { |
| 496 | char file_name[256]; |
| 497 | |
| 498 | snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name); |
| 499 | __finish_log(log, file_name); |
| 500 | } |