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