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