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