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 log_io_u(struct thread_data *td, struct io_u *io_u)
15 const char *act[] = { "read", "write", "sync" };
17 assert(io_u->ddir < 3);
19 if (!td->o.write_iolog_file)
22 fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name, act[io_u->ddir], io_u->offset, io_u->buflen);
25 void log_file(struct thread_data *td, struct fio_file *f,
26 enum file_log_act what)
28 const char *act[] = { "add", "open", "close" };
32 if (!td->o.write_iolog_file)
35 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
38 static void iolog_delay(struct thread_data *td, unsigned long delay)
40 unsigned long usec = utime_since_now(&td->last_issue);
48 * less than 100 usec delay, just regard it as noise
53 usec_sleep(td, delay);
56 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
60 while (!list_empty(&td->io_log_list)) {
61 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
65 * invalid ddir, this is a file action
67 if (ipo->ddir == DDIR_INVAL) {
68 struct fio_file *f = &td->files[ipo->fileno];
70 if (ipo->file_action == FIO_LOG_OPEN_FILE) {
71 assert(!td_io_open_file(td, f));
74 } else if (ipo->file_action == FIO_LOG_CLOSE_FILE) {
75 td_io_close_file(td, f);
81 io_u->offset = ipo->offset;
82 io_u->buflen = ipo->len;
83 io_u->ddir = ipo->ddir;
84 io_u->file = &td->files[ipo->fileno];
88 iolog_delay(td, ipo->delay);
97 void prune_io_piece_log(struct thread_data *td)
102 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
103 ipo = rb_entry(n, struct io_piece, rb_node);
104 rb_erase(n, &td->io_hist_tree);
110 * log a successful write, so we can unwind the log for verify
112 void log_io_piece(struct thread_data *td, struct io_u *io_u)
114 struct rb_node **p, *parent;
115 struct io_piece *ipo, *__ipo;
117 ipo = malloc(sizeof(struct io_piece));
118 ipo->file = io_u->file;
119 ipo->offset = io_u->offset;
120 ipo->len = io_u->buflen;
123 * We don't need to sort the entries, if:
125 * Sequential writes, or
126 * Random writes that lay out the file as it goes along
128 * For both these cases, just reading back data in the order we
129 * wrote it out is the fastest.
131 if (!td_random(td) || !td->o.overwrite ||
132 (io_u->file->flags & FIO_FILE_NOSORT)) {
133 INIT_LIST_HEAD(&ipo->list);
134 list_add_tail(&ipo->list, &td->io_hist_list);
138 RB_CLEAR_NODE(&ipo->rb_node);
139 p = &td->io_hist_tree.rb_node;
143 * Sort the entry into the verification list
148 __ipo = rb_entry(parent, struct io_piece, rb_node);
149 if (ipo->offset <= __ipo->offset)
155 rb_link_node(&ipo->rb_node, parent, p);
156 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
159 void write_iolog_close(struct thread_data *td)
165 td->iolog_buf = NULL;
169 * Read version 2 iolog data. It is enhanced to include per-file logging,
172 static int read_iolog2(struct thread_data *td, FILE *f)
174 unsigned long long offset;
176 int rw, reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
180 free_release_files(td);
183 * Read in the read iolog and store it, reuse the infrastructure
184 * for doing verifications.
187 fname = malloc(256+16);
188 act = malloc(256+16);
191 while ((p = fgets(str, 4096, f)) != NULL) {
192 struct io_piece *ipo;
195 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset, &bytes);
200 if (!strcmp(act, "read"))
202 else if (!strcmp(act, "write"))
204 else if (!strcmp(act, "sync"))
207 log_err("fio: bad iolog file action: %s\n",act);
212 if (!strcmp(act, "add")) {
214 fileno = add_file(td, fname);
215 file_action = FIO_LOG_ADD_FILE;
217 } else if (!strcmp(act, "open")) {
218 fileno = get_fileno(td, fname);
219 file_action = FIO_LOG_OPEN_FILE;
220 } else if (!strcmp(act, "close")) {
221 fileno = get_fileno(td, fname);
222 file_action = FIO_LOG_CLOSE_FILE;
224 log_err("fio: bad iolog file action: %s\n",act);
228 log_err("bad iolog2: %s", p);
234 else if (rw == DDIR_WRITE)
236 else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
237 log_err("bad ddir: %d\n", rw);
244 ipo = malloc(sizeof(*ipo));
245 memset(ipo, 0, sizeof(*ipo));
246 INIT_LIST_HEAD(&ipo->list);
247 ipo->offset = offset;
249 ipo->ddir = (enum fio_ddir) rw;
250 if (bytes > td->o.max_bs[rw])
251 td->o.max_bs[rw] = bytes;
252 if (rw == DDIR_INVAL) {
253 ipo->fileno = fileno;
254 ipo->file_action = file_action;
256 list_add_tail(&ipo->list, &td->io_log_list);
263 if (!reads && !writes)
265 else if (reads && !writes)
266 td->o.td_ddir = TD_DDIR_READ;
267 else if (!reads && writes)
268 td->o.td_ddir = TD_DDIR_WRITE;
270 td->o.td_ddir = TD_DDIR_RW;
276 * Read version 1 iolog data.
278 static int read_iolog(struct thread_data *td, FILE *f)
280 unsigned long long offset;
283 int rw, reads, writes;
286 * Read in the read iolog and store it, reuse the infrastructure
287 * for doing verifications.
291 while ((p = fgets(str, 4096, f)) != NULL) {
292 struct io_piece *ipo;
294 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
295 log_err("bad iolog: %s\n", p);
300 else if (rw == DDIR_WRITE)
302 else if (rw != DDIR_SYNC) {
303 log_err("bad ddir: %d\n", rw);
307 ipo = malloc(sizeof(*ipo));
308 memset(ipo, 0, sizeof(*ipo));
309 INIT_LIST_HEAD(&ipo->list);
310 ipo->offset = offset;
312 ipo->ddir = (enum fio_ddir) rw;
313 if (bytes > td->o.max_bs[rw])
314 td->o.max_bs[rw] = bytes;
315 list_add_tail(&ipo->list, &td->io_log_list);
320 if (!reads && !writes)
322 else if (reads && !writes)
323 td->o.td_ddir = TD_DDIR_READ;
324 else if (!reads && writes)
325 td->o.td_ddir = TD_DDIR_WRITE;
327 td->o.td_ddir = TD_DDIR_RW;
333 * open iolog, check version, and call appropriate parser
335 static int init_iolog_read(struct thread_data *td)
337 char buffer[256], *p;
341 f = fopen(td->o.read_iolog_file, "r");
343 perror("fopen read iolog");
347 p = fgets(buffer, sizeof(buffer), f);
349 td_verror(td, errno, "iolog read");
350 log_err("fio: unable to read iolog\n");
355 * version 2 of the iolog stores a specific string as the
356 * first line, check for that
358 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
359 ret = read_iolog2(td, f);
362 * seek back to the beginning
364 if (fseek(f, 0, SEEK_SET) < 0) {
365 td_verror(td, errno, "iolog read");
366 log_err("fio: unable to read iolog\n");
370 ret = read_iolog(td, f);
378 * Setup a log for storing io patterns.
380 static int init_iolog_write(struct thread_data *td)
386 f = fopen(td->o.write_iolog_file, "w+");
388 perror("fopen write iolog");
393 * That's it for writing, setup a log buffer and we're done.
396 td->iolog_buf = malloc(8192);
397 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
400 * write our version line
402 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
403 perror("iolog init\n");
408 * add all known files
410 for_each_file(td, ff, i)
411 log_file(td, ff, FIO_LOG_ADD_FILE);
416 int init_iolog(struct thread_data *td)
420 if (td->io_ops->flags & FIO_DISKLESSIO)
423 if (td->o.read_iolog_file) {
425 * Check if it's a blktrace file and load that if possible.
426 * Otherwise assume it's a normal log file and load that.
428 if (is_blktrace(td->o.read_iolog_file))
429 ret = load_blktrace(td, td->o.read_iolog_file);
431 ret = init_iolog_read(td);
432 } else if (td->o.write_iolog_file)
433 ret = init_iolog_write(td);
438 void setup_log(struct io_log **log)
440 struct io_log *l = malloc(sizeof(*l));
443 l->max_samples = 1024;
444 l->log = malloc(l->max_samples * sizeof(struct io_sample));
448 void __finish_log(struct io_log *log, const char *name)
453 f = fopen(name, "w");
459 for (i = 0; i < log->nr_samples; i++)
460 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
467 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
471 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
472 __finish_log(log, file_name);