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 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
15 if (!list_empty(&td->io_log_list)) {
16 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
18 io_u->offset = ipo->offset;
19 io_u->buflen = ipo->len;
20 io_u->ddir = ipo->ddir;
21 io_u->file = ipo->file;
23 * work around, this needs a format change to work for > 1 file
26 io_u->file = &td->files[0];
34 void prune_io_piece_log(struct thread_data *td)
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);
47 * log a successful write, so we can unwind the log for verify
49 void log_io_piece(struct thread_data *td, struct io_u *io_u)
51 struct rb_node **p, *parent;
52 struct io_piece *ipo, *__ipo;
54 ipo = malloc(sizeof(struct io_piece));
55 ipo->file = io_u->file;
56 ipo->offset = io_u->offset;
57 ipo->len = io_u->buflen;
60 * We don't need to sort the entries, if:
62 * Sequential writes, or
63 * Random writes that lay out the file as it goes along
65 * For both these cases, just reading back data in the order we
66 * wrote it out is the fastest.
68 if (!td_random(td) || !td->o.overwrite ||
69 (io_u->file->flags & FIO_FILE_NOSORT)) {
70 INIT_LIST_HEAD(&ipo->list);
71 list_add_tail(&ipo->list, &td->io_hist_list);
75 RB_CLEAR_NODE(&ipo->rb_node);
76 p = &td->io_hist_tree.rb_node;
80 * Sort the entry into the verification list
85 __ipo = rb_entry(parent, struct io_piece, rb_node);
86 if (ipo->offset <= __ipo->offset)
92 rb_link_node(&ipo->rb_node, parent, p);
93 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
96 void write_iolog_close(struct thread_data *td)
104 * Open a stored log and read in the entries.
106 static int init_iolog_read(struct thread_data *td)
108 unsigned long long offset;
112 int rw, reads, writes;
114 f = fopen(td->o.read_iolog_file, "r");
116 perror("fopen read iolog");
121 * Read in the read iolog and store it, reuse the infrastructure
122 * for doing verifications.
126 while ((p = fgets(str, 4096, f)) != NULL) {
127 struct io_piece *ipo;
129 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
130 log_err("bad iolog: %s\n", p);
135 else if (rw == DDIR_WRITE)
137 else if (rw != DDIR_SYNC) {
138 log_err("bad ddir: %d\n", rw);
142 ipo = malloc(sizeof(*ipo));
143 memset(ipo, 0, sizeof(*ipo));
144 INIT_LIST_HEAD(&ipo->list);
145 ipo->offset = offset;
147 ipo->ddir = (enum fio_ddir) rw;
148 if (bytes > td->o.max_bs[rw])
149 td->o.max_bs[rw] = bytes;
150 list_add_tail(&ipo->list, &td->io_log_list);
156 if (!reads && !writes)
158 else if (reads && !writes)
159 td->o.td_ddir = TD_DDIR_READ;
160 else if (!reads && writes)
161 td->o.td_ddir = TD_DDIR_WRITE;
163 td->o.td_ddir = TD_DDIR_RW;
169 * Setup a log for storing io patterns.
171 static int init_iolog_write(struct thread_data *td)
175 if (td->o.nr_files > 1) {
176 log_err("fio: write_iolog only works with 1 file currently\n");
180 f = fopen(td->o.write_iolog_file, "w+");
182 perror("fopen write iolog");
187 * That's it for writing, setup a log buffer and we're done.
190 td->iolog_buf = malloc(8192);
191 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
195 int init_iolog(struct thread_data *td)
199 if (td->io_ops->flags & FIO_DISKLESSIO)
202 if (td->o.read_iolog_file) {
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.
207 if (is_blktrace(td->o.read_iolog_file))
208 ret = load_blktrace(td, td->o.read_iolog_file);
210 ret = init_iolog_read(td);
211 } else if (td->o.write_iolog_file)
212 ret = init_iolog_write(td);
217 void setup_log(struct io_log **log)
219 struct io_log *l = malloc(sizeof(*l));
222 l->max_samples = 1024;
223 l->log = malloc(l->max_samples * sizeof(struct io_sample));
227 void __finish_log(struct io_log *log, const char *name)
232 f = fopen(name, "w");
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);
246 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
250 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
251 __finish_log(log, file_name);