2 * Code related to writing an iolog of what a thread is doing, and to
3 * later read that back and replay
11 static const char iolog_ver2[] = "fio version 2 iolog";
13 void queue_io_piece(struct thread_data *td, struct io_piece *ipo)
15 list_add_tail(&ipo->list, &td->io_log_list);
16 td->total_io_size += ipo->len;
19 void log_io_u(struct thread_data *td, struct io_u *io_u)
21 const char *act[] = { "read", "write", "sync" };
23 assert(io_u->ddir < 3);
25 if (!td->o.write_iolog_file)
28 fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name,
29 act[io_u->ddir], io_u->offset,
33 void log_file(struct thread_data *td, struct fio_file *f,
34 enum file_log_act what)
36 const char *act[] = { "add", "open", "close" };
40 if (!td->o.write_iolog_file)
43 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
46 static void iolog_delay(struct thread_data *td, unsigned long delay)
48 unsigned long usec = utime_since_now(&td->last_issue);
56 * less than 100 usec delay, just regard it as noise
61 usec_sleep(td, delay);
64 static int ipo_special(struct thread_data *td, struct io_piece *ipo)
72 if (ipo->ddir != DDIR_INVAL)
75 f = td->files[ipo->fileno];
77 switch (ipo->file_action) {
78 case FIO_LOG_OPEN_FILE:
79 ret = td_io_open_file(td, f);
84 td_verror(td, ret, "iolog open file");
86 case FIO_LOG_CLOSE_FILE:
87 td_io_close_file(td, f);
89 case FIO_LOG_UNLINK_FILE:
93 log_err("fio: bad file action %d\n", ipo->file_action);
100 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
102 struct io_piece *ipo;
104 while (!list_empty(&td->io_log_list)) {
107 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
108 list_del(&ipo->list);
110 ret = ipo_special(td, ipo);
114 } else if (ret > 0) {
119 io_u->offset = ipo->offset;
120 io_u->buflen = ipo->len;
121 io_u->ddir = ipo->ddir;
122 io_u->file = td->files[ipo->fileno];
123 get_file(io_u->file);
125 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
126 io_u->buflen, io_u->file->file_name);
129 iolog_delay(td, ipo->delay);
139 void prune_io_piece_log(struct thread_data *td)
141 struct io_piece *ipo;
144 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
145 ipo = rb_entry(n, struct io_piece, rb_node);
146 rb_erase(n, &td->io_hist_tree);
150 while (!list_empty(&td->io_hist_list)) {
151 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
152 list_del(&ipo->list);
158 * log a successful write, so we can unwind the log for verify
160 void log_io_piece(struct thread_data *td, struct io_u *io_u)
162 struct rb_node **p, *parent;
163 struct io_piece *ipo, *__ipo;
165 ipo = malloc(sizeof(struct io_piece));
166 ipo->file = io_u->file;
167 ipo->offset = io_u->offset;
168 ipo->len = io_u->buflen;
171 * We don't need to sort the entries, if:
173 * Sequential writes, or
174 * Random writes that lay out the file as it goes along
176 * For both these cases, just reading back data in the order we
177 * wrote it out is the fastest.
179 if (!td_random(td) || !td->o.overwrite) {
180 INIT_LIST_HEAD(&ipo->list);
181 list_add_tail(&ipo->list, &td->io_hist_list);
185 RB_CLEAR_NODE(&ipo->rb_node);
186 p = &td->io_hist_tree.rb_node;
190 * Sort the entry into the verification list
195 __ipo = rb_entry(parent, struct io_piece, rb_node);
196 if (ipo->offset <= __ipo->offset)
202 rb_link_node(&ipo->rb_node, parent, p);
203 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
206 void write_iolog_close(struct thread_data *td)
212 td->iolog_buf = NULL;
216 * Read version 2 iolog data. It is enhanced to include per-file logging,
219 static int read_iolog2(struct thread_data *td, FILE *f)
221 unsigned long long offset;
223 int reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
228 free_release_files(td);
231 * Read in the read iolog and store it, reuse the infrastructure
232 * for doing verifications.
235 fname = malloc(256+16);
236 act = malloc(256+16);
239 while ((p = fgets(str, 4096, f)) != NULL) {
240 struct io_piece *ipo;
243 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset,
249 if (!strcmp(act, "read"))
251 else if (!strcmp(act, "write"))
253 else if (!strcmp(act, "sync"))
256 log_err("fio: bad iolog file action: %s\n",
262 if (!strcmp(act, "add")) {
264 fileno = add_file(td, fname);
265 file_action = FIO_LOG_ADD_FILE;
267 } else if (!strcmp(act, "open")) {
268 fileno = get_fileno(td, fname);
269 file_action = FIO_LOG_OPEN_FILE;
270 } else if (!strcmp(act, "close")) {
271 fileno = get_fileno(td, fname);
272 file_action = FIO_LOG_CLOSE_FILE;
274 log_err("fio: bad iolog file action: %s\n",
279 log_err("bad iolog2: %s", p);
285 else if (rw == DDIR_WRITE) {
287 * Don't add a write for ro mode
292 } else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
293 log_err("bad ddir: %d\n", rw);
300 ipo = malloc(sizeof(*ipo));
301 memset(ipo, 0, sizeof(*ipo));
302 INIT_LIST_HEAD(&ipo->list);
303 ipo->offset = offset;
306 if (bytes > td->o.max_bs[rw])
307 td->o.max_bs[rw] = bytes;
308 if (rw == DDIR_INVAL) {
309 ipo->fileno = fileno;
310 ipo->file_action = file_action;
312 queue_io_piece(td, ipo);
319 if (writes && read_only) {
320 log_err("fio: <%s> skips replay of %d writes due to"
321 " read-only\n", td->o.name, writes);
325 if (!reads && !writes)
327 else if (reads && !writes)
328 td->o.td_ddir = TD_DDIR_READ;
329 else if (!reads && writes)
330 td->o.td_ddir = TD_DDIR_WRITE;
332 td->o.td_ddir = TD_DDIR_RW;
338 * Read version 1 iolog data.
340 static int read_iolog(struct thread_data *td, FILE *f)
342 log_err("fio: iolog version 1 is no longer supported\n");
347 * open iolog, check version, and call appropriate parser
349 static int init_iolog_read(struct thread_data *td)
351 char buffer[256], *p;
355 f = fopen(td->o.read_iolog_file, "r");
357 perror("fopen read iolog");
361 p = fgets(buffer, sizeof(buffer), f);
363 td_verror(td, errno, "iolog read");
364 log_err("fio: unable to read iolog\n");
369 * version 2 of the iolog stores a specific string as the
370 * first line, check for that
372 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
373 ret = read_iolog2(td, f);
376 * seek back to the beginning
378 if (fseek(f, 0, SEEK_SET) < 0) {
379 td_verror(td, errno, "iolog read");
380 log_err("fio: unable to read iolog\n");
384 ret = read_iolog(td, f);
392 * Setup a log for storing io patterns.
394 static int init_iolog_write(struct thread_data *td)
400 f = fopen(td->o.write_iolog_file, "w+");
402 perror("fopen write iolog");
407 * That's it for writing, setup a log buffer and we're done.
410 td->iolog_buf = malloc(8192);
411 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
414 * write our version line
416 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
417 perror("iolog init\n");
422 * add all known files
424 for_each_file(td, ff, i)
425 log_file(td, ff, FIO_LOG_ADD_FILE);
430 int init_iolog(struct thread_data *td)
434 if (td->io_ops->flags & FIO_DISKLESSIO)
437 if (td->o.read_iolog_file) {
439 * Check if it's a blktrace file and load that if possible.
440 * Otherwise assume it's a normal log file and load that.
442 if (is_blktrace(td->o.read_iolog_file))
443 ret = load_blktrace(td, td->o.read_iolog_file);
445 ret = init_iolog_read(td);
446 } else if (td->o.write_iolog_file)
447 ret = init_iolog_write(td);
452 void setup_log(struct io_log **log)
454 struct io_log *l = malloc(sizeof(*l));
457 l->max_samples = 1024;
458 l->log = malloc(l->max_samples * sizeof(struct io_sample));
462 void __finish_log(struct io_log *log, const char *name)
467 f = fopen(name, "w");
473 for (i = 0; i < log->nr_samples; i++) {
474 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val,
483 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
487 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
488 __finish_log(log, file_name);