6 void write_iolog_put(struct thread_data *td, struct io_u *io_u)
8 fprintf(td->iolog_f, "%u,%llu,%lu\n", io_u->ddir, io_u->offset, io_u->buflen);
11 static void iolog_delay(struct thread_data *td, unsigned long delay)
13 unsigned long usec = utime_since_now(&td->last_issue);
21 * less than 100 usec delay, just regard it as noise
26 usec_sleep(td, delay);
29 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
33 if (!list_empty(&td->io_log_list)) {
34 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
36 io_u->offset = ipo->offset;
37 io_u->buflen = ipo->len;
38 io_u->ddir = ipo->ddir;
39 io_u->file = ipo->file;
42 iolog_delay(td, ipo->delay);
45 * work around, this needs a format change to work for > 1 file
48 io_u->file = &td->files[0];
56 void prune_io_piece_log(struct thread_data *td)
61 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
62 ipo = rb_entry(n, struct io_piece, rb_node);
63 rb_erase(n, &td->io_hist_tree);
69 * log a successful write, so we can unwind the log for verify
71 void log_io_piece(struct thread_data *td, struct io_u *io_u)
73 struct rb_node **p, *parent;
74 struct io_piece *ipo, *__ipo;
76 ipo = malloc(sizeof(struct io_piece));
77 ipo->file = io_u->file;
78 ipo->offset = io_u->offset;
79 ipo->len = io_u->buflen;
82 * We don't need to sort the entries, if:
84 * Sequential writes, or
85 * Random writes that lay out the file as it goes along
87 * For both these cases, just reading back data in the order we
88 * wrote it out is the fastest.
90 if (!td_random(td) || !td->o.overwrite ||
91 (io_u->file->flags & FIO_FILE_NOSORT)) {
92 INIT_LIST_HEAD(&ipo->list);
93 list_add_tail(&ipo->list, &td->io_hist_list);
97 RB_CLEAR_NODE(&ipo->rb_node);
98 p = &td->io_hist_tree.rb_node;
102 * Sort the entry into the verification list
107 __ipo = rb_entry(parent, struct io_piece, rb_node);
108 if (ipo->offset <= __ipo->offset)
114 rb_link_node(&ipo->rb_node, parent, p);
115 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
118 void write_iolog_close(struct thread_data *td)
126 * Open a stored log and read in the entries.
128 static int init_iolog_read(struct thread_data *td)
130 unsigned long long offset;
134 int rw, reads, writes;
136 f = fopen(td->o.read_iolog_file, "r");
138 perror("fopen read iolog");
143 * Read in the read iolog and store it, reuse the infrastructure
144 * for doing verifications.
148 while ((p = fgets(str, 4096, f)) != NULL) {
149 struct io_piece *ipo;
151 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
152 log_err("bad iolog: %s\n", p);
157 else if (rw == DDIR_WRITE)
159 else if (rw != DDIR_SYNC) {
160 log_err("bad ddir: %d\n", rw);
164 ipo = malloc(sizeof(*ipo));
165 memset(ipo, 0, sizeof(*ipo));
166 INIT_LIST_HEAD(&ipo->list);
167 ipo->offset = offset;
169 ipo->ddir = (enum fio_ddir) rw;
170 if (bytes > td->o.max_bs[rw])
171 td->o.max_bs[rw] = bytes;
172 list_add_tail(&ipo->list, &td->io_log_list);
178 if (!reads && !writes)
180 else if (reads && !writes)
181 td->o.td_ddir = TD_DDIR_READ;
182 else if (!reads && writes)
183 td->o.td_ddir = TD_DDIR_WRITE;
185 td->o.td_ddir = TD_DDIR_RW;
191 * Setup a log for storing io patterns.
193 static int init_iolog_write(struct thread_data *td)
197 if (td->o.nr_files > 1) {
198 log_err("fio: write_iolog only works with 1 file currently\n");
202 f = fopen(td->o.write_iolog_file, "w+");
204 perror("fopen write iolog");
209 * That's it for writing, setup a log buffer and we're done.
212 td->iolog_buf = malloc(8192);
213 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
217 int init_iolog(struct thread_data *td)
221 if (td->io_ops->flags & FIO_DISKLESSIO)
224 if (td->o.read_iolog_file) {
226 * Check if it's a blktrace file and load that if possible.
227 * Otherwise assume it's a normal log file and load that.
229 if (is_blktrace(td->o.read_iolog_file))
230 ret = load_blktrace(td, td->o.read_iolog_file);
232 ret = init_iolog_read(td);
233 } else if (td->o.write_iolog_file)
234 ret = init_iolog_write(td);
239 void setup_log(struct io_log **log)
241 struct io_log *l = malloc(sizeof(*l));
244 l->max_samples = 1024;
245 l->log = malloc(l->max_samples * sizeof(struct io_sample));
249 void __finish_log(struct io_log *log, const char *name)
254 f = fopen(name, "w");
260 for (i = 0; i < log->nr_samples; i++)
261 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
268 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
272 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
273 __finish_log(log, file_name);