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 | |
| 11 | int read_iolog_get(struct thread_data *td, struct io_u *io_u) |
| 12 | { |
| 13 | struct io_piece *ipo; |
| 14 | |
| 15 | if (!list_empty(&td->io_log_list)) { |
| 16 | ipo = list_entry(td->io_log_list.next, struct io_piece, list); |
| 17 | list_del(&ipo->list); |
| 18 | io_u->offset = ipo->offset; |
| 19 | io_u->buflen = ipo->len; |
| 20 | io_u->ddir = ipo->ddir; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 21 | io_u->file = ipo->file; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 22 | free(ipo); |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | return 1; |
| 27 | } |
| 28 | |
| 29 | void prune_io_piece_log(struct thread_data *td) |
| 30 | { |
| 31 | struct io_piece *ipo; |
| 32 | |
| 33 | while (!list_empty(&td->io_hist_list)) { |
| 34 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); |
| 35 | |
| 36 | list_del(&ipo->list); |
| 37 | free(ipo); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * log a succesful write, so we can unwind the log for verify |
| 43 | */ |
| 44 | void log_io_piece(struct thread_data *td, struct io_u *io_u) |
| 45 | { |
| 46 | struct io_piece *ipo = malloc(sizeof(struct io_piece)); |
| 47 | struct list_head *entry; |
| 48 | |
| 49 | INIT_LIST_HEAD(&ipo->list); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 50 | ipo->file = io_u->file; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 51 | ipo->offset = io_u->offset; |
| 52 | ipo->len = io_u->buflen; |
| 53 | |
| 54 | /* |
| 55 | * for random io where the writes extend the file, it will typically |
| 56 | * be laid out with the block scattered as written. it's faster to |
| 57 | * read them in in that order again, so don't sort |
| 58 | */ |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 59 | if (!td_random(td) || !td->overwrite) { |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 60 | list_add_tail(&ipo->list, &td->io_hist_list); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * for random io, sort the list so verify will run faster |
| 66 | */ |
| 67 | entry = &td->io_hist_list; |
| 68 | while ((entry = entry->prev) != &td->io_hist_list) { |
| 69 | struct io_piece *__ipo = list_entry(entry, struct io_piece, list); |
| 70 | |
| 71 | if (__ipo->offset < ipo->offset) |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | list_add(&ipo->list, entry); |
| 76 | } |
| 77 | |
| 78 | void write_iolog_close(struct thread_data *td) |
| 79 | { |
| 80 | fflush(td->iolog_f); |
| 81 | fclose(td->iolog_f); |
| 82 | free(td->iolog_buf); |
| 83 | } |
| 84 | |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 85 | /* |
| 86 | * Open a stored log and read in the entries. |
| 87 | */ |
| 88 | static int init_iolog_read(struct thread_data *td) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 89 | { |
| 90 | unsigned long long offset; |
| 91 | unsigned int bytes; |
| 92 | char *str, *p; |
| 93 | FILE *f; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 94 | int rw, reads, writes; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 95 | |
Jens Axboe | 076efc7 | 2006-10-27 11:24:25 +0200 | [diff] [blame] | 96 | f = fopen(td->read_iolog_file, "r"); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 97 | if (!f) { |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 98 | perror("fopen read iolog"); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | /* |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 103 | * Read in the read iolog and store it, reuse the infrastructure |
| 104 | * for doing verifications. |
| 105 | */ |
| 106 | str = malloc(4096); |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 107 | reads = writes = 0; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 108 | while ((p = fgets(str, 4096, f)) != NULL) { |
| 109 | struct io_piece *ipo; |
| 110 | |
| 111 | if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 112 | log_err("bad iolog: %s\n", p); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 113 | continue; |
| 114 | } |
| 115 | if (rw == DDIR_READ) |
| 116 | reads++; |
| 117 | else if (rw == DDIR_WRITE) |
| 118 | writes++; |
| 119 | else { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 120 | log_err("bad ddir: %d\n", rw); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 121 | continue; |
| 122 | } |
| 123 | |
| 124 | ipo = malloc(sizeof(*ipo)); |
| 125 | INIT_LIST_HEAD(&ipo->list); |
| 126 | ipo->offset = offset; |
| 127 | ipo->len = bytes; |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 128 | ipo->ddir = (enum fio_ddir) rw; |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 129 | if (bytes > td->max_bs[rw]) |
| 130 | td->max_bs[rw] = bytes; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 131 | list_add_tail(&ipo->list, &td->io_log_list); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | free(str); |
| 135 | fclose(f); |
| 136 | |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 137 | if (!reads && !writes) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 138 | return 1; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 139 | else if (reads && !writes) |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 140 | td->td_ddir = TD_DDIR_READ; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 141 | else if (!reads && writes) |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 142 | td->td_ddir = TD_DDIR_READ; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 143 | else |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 144 | td->td_ddir = TD_DDIR_RW; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 145 | |
| 146 | return 0; |
| 147 | } |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 148 | |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 149 | /* |
| 150 | * Setup a log for storing io patterns. |
| 151 | */ |
| 152 | static int init_iolog_write(struct thread_data *td) |
| 153 | { |
Jens Axboe | 076efc7 | 2006-10-27 11:24:25 +0200 | [diff] [blame] | 154 | FILE *f; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 155 | |
Jens Axboe | 076efc7 | 2006-10-27 11:24:25 +0200 | [diff] [blame] | 156 | f = fopen(td->write_iolog_file, "w+"); |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 157 | if (!f) { |
| 158 | perror("fopen write iolog"); |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * That's it for writing, setup a log buffer and we're done. |
| 164 | */ |
| 165 | td->iolog_f = f; |
| 166 | td->iolog_buf = malloc(8192); |
| 167 | setvbuf(f, td->iolog_buf, _IOFBF, 8192); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | int init_iolog(struct thread_data *td) |
| 172 | { |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 173 | int ret = 0; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 174 | |
Jens Axboe | f48b467 | 2006-10-27 11:30:07 +0200 | [diff] [blame] | 175 | if (td->io_ops->flags & FIO_CPUIO) |
| 176 | return 0; |
| 177 | |
Jens Axboe | 076efc7 | 2006-10-27 11:24:25 +0200 | [diff] [blame] | 178 | if (td->read_iolog_file) |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 179 | ret = init_iolog_read(td); |
Jens Axboe | 076efc7 | 2006-10-27 11:24:25 +0200 | [diff] [blame] | 180 | else if (td->write_iolog_file) |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 181 | ret = init_iolog_write(td); |
| 182 | |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 183 | return ret; |
Jens Axboe | fb71fbd | 2006-10-20 09:15:46 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 186 | int setup_rate(struct thread_data *td) |
| 187 | { |
Jens Axboe | fb44a18 | 2007-01-13 12:29:13 +0100 | [diff] [blame] | 188 | unsigned long long rate; |
| 189 | int nr_reads_per_msec; |
Jens Axboe | c3852ae | 2007-02-23 10:47:46 +0100 | [diff] [blame] | 190 | unsigned int bs; |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 191 | |
| 192 | if (!td->rate) |
| 193 | return 0; |
| 194 | |
| 195 | if (td->rate < td->ratemin) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame] | 196 | log_err("min rate larger than nominal rate\n"); |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 197 | return -1; |
| 198 | } |
| 199 | |
Jens Axboe | c3852ae | 2007-02-23 10:47:46 +0100 | [diff] [blame] | 200 | if (td_rw(td)) |
| 201 | bs = td->rw_min_bs; |
| 202 | else if (td_read(td)) |
| 203 | bs = td->min_bs[DDIR_READ]; |
| 204 | else |
| 205 | bs = td->min_bs[DDIR_WRITE]; |
| 206 | |
Jens Axboe | fb44a18 | 2007-01-13 12:29:13 +0100 | [diff] [blame] | 207 | rate = td->rate; |
Jens Axboe | c3852ae | 2007-02-23 10:47:46 +0100 | [diff] [blame] | 208 | nr_reads_per_msec = (rate * 1024 * 1000) / bs; |
Jens Axboe | fb44a18 | 2007-01-13 12:29:13 +0100 | [diff] [blame] | 209 | if (!nr_reads_per_msec) { |
| 210 | log_err("rate lower than supported\n"); |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec; |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 215 | td->rate_pending_usleep = 0; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | void setup_log(struct io_log **log) |
| 220 | { |
| 221 | struct io_log *l = malloc(sizeof(*l)); |
| 222 | |
| 223 | l->nr_samples = 0; |
| 224 | l->max_samples = 1024; |
| 225 | l->log = malloc(l->max_samples * sizeof(struct io_sample)); |
| 226 | *log = l; |
| 227 | } |
| 228 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 229 | void __finish_log(struct io_log *log, const char *name) |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 230 | { |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 231 | unsigned int i; |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 232 | FILE *f; |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 233 | |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 234 | f = fopen(name, "w"); |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 235 | if (!f) { |
| 236 | perror("fopen log"); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | for (i = 0; i < log->nr_samples; i++) |
| 241 | fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir); |
| 242 | |
| 243 | fclose(f); |
| 244 | free(log->log); |
| 245 | free(log); |
| 246 | } |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 247 | |
| 248 | void finish_log(struct thread_data *td, struct io_log *log, const char *name) |
| 249 | { |
| 250 | char file_name[256]; |
| 251 | |
| 252 | snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name); |
| 253 | __finish_log(log, file_name); |
| 254 | } |