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 | { |
| 8 | fprintf(td->iolog_f, "%d,%llu,%u\n", io_u->ddir, io_u->offset, io_u->buflen); |
| 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; |
| 21 | free(ipo); |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | return 1; |
| 26 | } |
| 27 | |
| 28 | void prune_io_piece_log(struct thread_data *td) |
| 29 | { |
| 30 | struct io_piece *ipo; |
| 31 | |
| 32 | while (!list_empty(&td->io_hist_list)) { |
| 33 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); |
| 34 | |
| 35 | list_del(&ipo->list); |
| 36 | free(ipo); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * log a succesful write, so we can unwind the log for verify |
| 42 | */ |
| 43 | void log_io_piece(struct thread_data *td, struct io_u *io_u) |
| 44 | { |
| 45 | struct io_piece *ipo = malloc(sizeof(struct io_piece)); |
| 46 | struct list_head *entry; |
| 47 | |
| 48 | INIT_LIST_HEAD(&ipo->list); |
| 49 | ipo->offset = io_u->offset; |
| 50 | ipo->len = io_u->buflen; |
| 51 | |
| 52 | /* |
| 53 | * for random io where the writes extend the file, it will typically |
| 54 | * be laid out with the block scattered as written. it's faster to |
| 55 | * read them in in that order again, so don't sort |
| 56 | */ |
| 57 | if (td->sequential || !td->overwrite) { |
| 58 | list_add_tail(&ipo->list, &td->io_hist_list); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * for random io, sort the list so verify will run faster |
| 64 | */ |
| 65 | entry = &td->io_hist_list; |
| 66 | while ((entry = entry->prev) != &td->io_hist_list) { |
| 67 | struct io_piece *__ipo = list_entry(entry, struct io_piece, list); |
| 68 | |
| 69 | if (__ipo->offset < ipo->offset) |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | list_add(&ipo->list, entry); |
| 74 | } |
| 75 | |
| 76 | void write_iolog_close(struct thread_data *td) |
| 77 | { |
| 78 | fflush(td->iolog_f); |
| 79 | fclose(td->iolog_f); |
| 80 | free(td->iolog_buf); |
| 81 | } |
| 82 | |
| 83 | int init_iolog(struct thread_data *td) |
| 84 | { |
| 85 | unsigned long long offset; |
| 86 | unsigned int bytes; |
| 87 | char *str, *p; |
| 88 | FILE *f; |
| 89 | int rw, i, reads, writes; |
| 90 | |
| 91 | if (!td->read_iolog && !td->write_iolog) |
| 92 | return 0; |
| 93 | |
| 94 | if (td->read_iolog) |
| 95 | f = fopen(td->iolog_file, "r"); |
| 96 | else |
| 97 | f = fopen(td->iolog_file, "w"); |
| 98 | |
| 99 | if (!f) { |
| 100 | perror("fopen iolog"); |
| 101 | printf("file %s, %d/%d\n", td->iolog_file, td->read_iolog, td->write_iolog); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * That's it for writing, setup a log buffer and we're done. |
| 107 | */ |
| 108 | if (td->write_iolog) { |
| 109 | td->iolog_f = f; |
| 110 | td->iolog_buf = malloc(8192); |
| 111 | setvbuf(f, td->iolog_buf, _IOFBF, 8192); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Read in the read iolog and store it, reuse the infrastructure |
| 117 | * for doing verifications. |
| 118 | */ |
| 119 | str = malloc(4096); |
| 120 | reads = writes = i = 0; |
| 121 | while ((p = fgets(str, 4096, f)) != NULL) { |
| 122 | struct io_piece *ipo; |
| 123 | |
| 124 | if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 125 | log_err("bad iolog: %s\n", p); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 126 | continue; |
| 127 | } |
| 128 | if (rw == DDIR_READ) |
| 129 | reads++; |
| 130 | else if (rw == DDIR_WRITE) |
| 131 | writes++; |
| 132 | else { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 133 | log_err("bad ddir: %d\n", rw); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 134 | continue; |
| 135 | } |
| 136 | |
| 137 | ipo = malloc(sizeof(*ipo)); |
| 138 | INIT_LIST_HEAD(&ipo->list); |
| 139 | ipo->offset = offset; |
| 140 | ipo->len = bytes; |
| 141 | if (bytes > td->max_bs) |
| 142 | td->max_bs = bytes; |
| 143 | ipo->ddir = rw; |
| 144 | list_add_tail(&ipo->list, &td->io_log_list); |
| 145 | i++; |
| 146 | } |
| 147 | |
| 148 | free(str); |
| 149 | fclose(f); |
| 150 | |
| 151 | if (!i) |
| 152 | return 1; |
| 153 | |
| 154 | if (reads && !writes) |
| 155 | td->ddir = DDIR_READ; |
| 156 | else if (!reads && writes) |
| 157 | td->ddir = DDIR_READ; |
| 158 | else |
| 159 | td->iomix = 1; |
| 160 | |
| 161 | return 0; |
| 162 | } |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 163 | |
| 164 | int setup_rate(struct thread_data *td) |
| 165 | { |
| 166 | int nr_reads_per_sec; |
| 167 | |
| 168 | if (!td->rate) |
| 169 | return 0; |
| 170 | |
| 171 | if (td->rate < td->ratemin) { |
Jens Axboe | 3b70d7e | 2006-06-08 21:48:46 +0200 | [diff] [blame^] | 172 | log_err("min rate larger than nominal rate\n"); |
Jens Axboe | 8914a9d | 2006-06-07 11:14:56 +0200 | [diff] [blame] | 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | nr_reads_per_sec = (td->rate * 1024) / td->min_bs; |
| 177 | td->rate_usec_cycle = 1000000 / nr_reads_per_sec; |
| 178 | td->rate_pending_usleep = 0; |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | void setup_log(struct io_log **log) |
| 183 | { |
| 184 | struct io_log *l = malloc(sizeof(*l)); |
| 185 | |
| 186 | l->nr_samples = 0; |
| 187 | l->max_samples = 1024; |
| 188 | l->log = malloc(l->max_samples * sizeof(struct io_sample)); |
| 189 | *log = l; |
| 190 | } |
| 191 | |
| 192 | void finish_log(struct thread_data *td, struct io_log *log, const char *name) |
| 193 | { |
| 194 | char file_name[256]; |
| 195 | FILE *f; |
| 196 | unsigned int i; |
| 197 | |
| 198 | snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name); |
| 199 | f = fopen(file_name, "w"); |
| 200 | if (!f) { |
| 201 | perror("fopen log"); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | for (i = 0; i < log->nr_samples; i++) |
| 206 | fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir); |
| 207 | |
| 208 | fclose(f); |
| 209 | free(log->log); |
| 210 | free(log); |
| 211 | } |