Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include "list.h" |
| 4 | #include "fio.h" |
| 5 | |
| 6 | void write_iolog_put(struct thread_data *td, struct io_u *io_u) |
| 7 | { |
Jens Axboe | a4f4fdd | 2007-02-14 01:16:39 +0100 | [diff] [blame] | 8 | fprintf(td->iolog_f, "%u,%llu,%lu\n", io_u->ddir, io_u->offset, io_u->buflen); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 9 | } |
| 10 | |
Jens Axboe | a61edde | 2007-05-15 14:29:58 +0200 | [diff] [blame] | 11 | static void iolog_delay(struct thread_data *td, unsigned long delay) |
| 12 | { |
| 13 | unsigned long usec = utime_since_now(&td->last_issue); |
| 14 | |
| 15 | if (delay < usec) |
| 16 | return; |
| 17 | |
| 18 | delay -= usec; |
| 19 | |
| 20 | /* |
| 21 | * less than 100 usec delay, just regard it as noise |
| 22 | */ |
| 23 | if (delay < 100) |
| 24 | return; |
| 25 | |
| 26 | usec_sleep(td, delay); |
| 27 | } |
| 28 | |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 29 | int read_iolog_get(struct thread_data *td, struct io_u *io_u) |
| 30 | { |
| 31 | struct io_piece *ipo; |
| 32 | |
| 33 | if (!list_empty(&td->io_log_list)) { |
| 34 | ipo = list_entry(td->io_log_list.next, struct io_piece, list); |
| 35 | list_del(&ipo->list); |
| 36 | io_u->offset = ipo->offset; |
| 37 | io_u->buflen = ipo->len; |
| 38 | io_u->ddir = ipo->ddir; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 39 | io_u->file = ipo->file; |
Jens Axboe | a61edde | 2007-05-15 14:29:58 +0200 | [diff] [blame] | 40 | |
| 41 | if (ipo->delay) |
| 42 | iolog_delay(td, ipo->delay); |
| 43 | |
Jens Axboe | fcb1170 | 2007-04-25 14:11:18 +0200 | [diff] [blame] | 44 | /* |
| 45 | * work around, this needs a format change to work for > 1 file |
| 46 | */ |
| 47 | if (!io_u->file) |
| 48 | io_u->file = &td->files[0]; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 49 | free(ipo); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | void prune_io_piece_log(struct thread_data *td) |
| 57 | { |
| 58 | struct io_piece *ipo; |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 59 | struct rb_node *n; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 60 | |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 61 | while ((n = rb_first(&td->io_hist_tree)) != NULL) { |
| 62 | ipo = rb_entry(n, struct io_piece, rb_node); |
| 63 | rb_erase(n, &td->io_hist_tree); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 64 | free(ipo); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /* |
Jens Axboe | 34403fb | 2007-03-02 21:43:25 +0100 | [diff] [blame] | 69 | * log a successful write, so we can unwind the log for verify |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 70 | */ |
| 71 | void log_io_piece(struct thread_data *td, struct io_u *io_u) |
| 72 | { |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 73 | struct rb_node **p, *parent; |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 74 | struct io_piece *ipo, *__ipo; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 75 | |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 76 | ipo = malloc(sizeof(struct io_piece)); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 77 | ipo->file = io_u->file; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 78 | ipo->offset = io_u->offset; |
| 79 | ipo->len = io_u->buflen; |
| 80 | |
| 81 | /* |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 82 | * We don't need to sort the entries, if: |
| 83 | * |
| 84 | * Sequential writes, or |
| 85 | * Random writes that lay out the file as it goes along |
| 86 | * |
| 87 | * For both these cases, just reading back data in the order we |
| 88 | * wrote it out is the fastest. |
| 89 | */ |
Jens Axboe | 160b966 | 2007-03-27 10:59:49 +0200 | [diff] [blame] | 90 | if (!td_random(td) || !td->o.overwrite || |
| 91 | (io_u->file->flags & FIO_FILE_NOSORT)) { |
Jens Axboe | 8de8f04 | 2007-03-27 10:36:12 +0200 | [diff] [blame] | 92 | INIT_LIST_HEAD(&ipo->list); |
| 93 | list_add_tail(&ipo->list, &td->io_hist_list); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | RB_CLEAR_NODE(&ipo->rb_node); |
| 98 | p = &td->io_hist_tree.rb_node; |
| 99 | parent = NULL; |
| 100 | |
| 101 | /* |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 102 | * Sort the entry into the verification list |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 103 | */ |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 104 | while (*p) { |
| 105 | parent = *p; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 106 | |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 107 | __ipo = rb_entry(parent, struct io_piece, rb_node); |
Jens Axboe | bb5d7d0 | 2007-03-27 10:21:25 +0200 | [diff] [blame] | 108 | if (ipo->offset <= __ipo->offset) |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 109 | p = &(*p)->rb_left; |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 110 | else |
Jens Axboe | bb5d7d0 | 2007-03-27 10:21:25 +0200 | [diff] [blame] | 111 | p = &(*p)->rb_right; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Jens Axboe | 4b87898 | 2007-03-26 09:32:22 +0200 | [diff] [blame] | 114 | rb_link_node(&ipo->rb_node, parent, p); |
| 115 | rb_insert_color(&ipo->rb_node, &td->io_hist_tree); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void write_iolog_close(struct thread_data *td) |
| 119 | { |
| 120 | fflush(td->iolog_f); |
| 121 | fclose(td->iolog_f); |
| 122 | free(td->iolog_buf); |
| 123 | } |
| 124 | |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 125 | /* |
| 126 | * Open a stored log and read in the entries. |
| 127 | */ |
| 128 | static int init_iolog_read(struct thread_data *td) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 129 | { |
| 130 | unsigned long long offset; |
| 131 | unsigned int bytes; |
| 132 | char *str, *p; |
| 133 | FILE *f; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 134 | int rw, reads, writes; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 135 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 136 | f = fopen(td->o.read_iolog_file, "r"); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 137 | if (!f) { |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 138 | perror("fopen read iolog"); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | /* |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 143 | * Read in the read iolog and store it, reuse the infrastructure |
| 144 | * for doing verifications. |
| 145 | */ |
| 146 | str = malloc(4096); |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 147 | reads = writes = 0; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 148 | while ((p = fgets(str, 4096, f)) != NULL) { |
| 149 | struct io_piece *ipo; |
| 150 | |
| 151 | if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 152 | log_err("bad iolog: %s\n", p); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 153 | continue; |
| 154 | } |
| 155 | if (rw == DDIR_READ) |
| 156 | reads++; |
| 157 | else if (rw == DDIR_WRITE) |
| 158 | writes++; |
Jens Axboe | c38e946 | 2007-03-27 08:48:48 +0200 | [diff] [blame] | 159 | else if (rw != DDIR_SYNC) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 160 | log_err("bad ddir: %d\n", rw); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 161 | continue; |
| 162 | } |
| 163 | |
| 164 | ipo = malloc(sizeof(*ipo)); |
Jens Axboe | 733ed59 | 2007-04-25 14:24:12 +0200 | [diff] [blame] | 165 | memset(ipo, 0, sizeof(*ipo)); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 166 | INIT_LIST_HEAD(&ipo->list); |
| 167 | ipo->offset = offset; |
| 168 | ipo->len = bytes; |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 169 | ipo->ddir = (enum fio_ddir) rw; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 170 | if (bytes > td->o.max_bs[rw]) |
| 171 | td->o.max_bs[rw] = bytes; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 172 | list_add_tail(&ipo->list, &td->io_log_list); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | free(str); |
| 176 | fclose(f); |
| 177 | |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 178 | if (!reads && !writes) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 179 | return 1; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 180 | else if (reads && !writes) |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 181 | td->o.td_ddir = TD_DDIR_READ; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 182 | else if (!reads && writes) |
Jens Axboe | 36361eb | 2007-05-15 11:12:19 +0200 | [diff] [blame] | 183 | td->o.td_ddir = TD_DDIR_WRITE; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 184 | else |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 185 | td->o.td_ddir = TD_DDIR_RW; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 186 | |
| 187 | return 0; |
| 188 | } |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 189 | |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 190 | /* |
| 191 | * Setup a log for storing io patterns. |
| 192 | */ |
| 193 | static int init_iolog_write(struct thread_data *td) |
| 194 | { |
Jens Axboe | 076efc7 | 2006-10-27 11:24:25 +0200 | [diff] [blame] | 195 | FILE *f; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 196 | |
Jens Axboe | 733ed59 | 2007-04-25 14:24:12 +0200 | [diff] [blame] | 197 | if (td->o.nr_files > 1) { |
| 198 | log_err("fio: write_iolog only works with 1 file currently\n"); |
| 199 | return 1; |
| 200 | } |
| 201 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 202 | f = fopen(td->o.write_iolog_file, "w+"); |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 203 | if (!f) { |
| 204 | perror("fopen write iolog"); |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * That's it for writing, setup a log buffer and we're done. |
| 210 | */ |
| 211 | td->iolog_f = f; |
| 212 | td->iolog_buf = malloc(8192); |
| 213 | setvbuf(f, td->iolog_buf, _IOFBF, 8192); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | int init_iolog(struct thread_data *td) |
| 218 | { |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 219 | int ret = 0; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 220 | |
Jens Axboe | ba0fbe1 | 2007-03-09 14:34:23 +0100 | [diff] [blame] | 221 | if (td->io_ops->flags & FIO_DISKLESSIO) |
Jens Axboe | f48b467 | 2006-10-27 11:30:07 +0200 | [diff] [blame] | 222 | return 0; |
| 223 | |
Jens Axboe | fb7b71a | 2007-05-15 08:44:04 +0200 | [diff] [blame] | 224 | if (td->o.read_iolog_file) { |
| 225 | /* |
| 226 | * Check if it's a blktrace file and load that if possible. |
| 227 | * Otherwise assume it's a normal log file and load that. |
| 228 | */ |
| 229 | if (is_blktrace(td->o.read_iolog_file)) |
| 230 | ret = load_blktrace(td, td->o.read_iolog_file); |
| 231 | else |
| 232 | ret = init_iolog_read(td); |
| 233 | } else if (td->o.write_iolog_file) |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 234 | ret = init_iolog_write(td); |
| 235 | |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 236 | return ret; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 239 | void setup_log(struct io_log **log) |
| 240 | { |
| 241 | struct io_log *l = malloc(sizeof(*l)); |
| 242 | |
| 243 | l->nr_samples = 0; |
| 244 | l->max_samples = 1024; |
| 245 | l->log = malloc(l->max_samples * sizeof(struct io_sample)); |
| 246 | *log = l; |
| 247 | } |
| 248 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 249 | void __finish_log(struct io_log *log, const char *name) |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 250 | { |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 251 | unsigned int i; |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 252 | FILE *f; |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 253 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 254 | f = fopen(name, "w"); |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 255 | if (!f) { |
| 256 | perror("fopen log"); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | for (i = 0; i < log->nr_samples; i++) |
| 261 | fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir); |
| 262 | |
| 263 | fclose(f); |
| 264 | free(log->log); |
| 265 | free(log); |
| 266 | } |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 267 | |
| 268 | void finish_log(struct thread_data *td, struct io_log *log, const char *name) |
| 269 | { |
| 270 | char file_name[256]; |
| 271 | |
| 272 | snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name); |
| 273 | __finish_log(log, file_name); |
| 274 | } |