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