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