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 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
68 while (!list_empty(&td->io_log_list)) {
69 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
73 * invalid ddir, this is a file action
75 if (ipo->ddir == DDIR_INVAL) {
76 struct fio_file *f = td->files[ipo->fileno];
78 if (ipo->file_action == FIO_LOG_OPEN_FILE) {
81 ret = td_io_open_file(td, f);
86 td_verror(td, ret, "iolog open file");
88 } else if (ipo->file_action == FIO_LOG_CLOSE_FILE) {
89 td_io_close_file(td, f);
95 io_u->offset = ipo->offset;
96 io_u->buflen = ipo->len;
97 io_u->ddir = ipo->ddir;
98 io_u->file = td->files[ipo->fileno];
101 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
102 io_u->buflen, io_u->file->file_name);
105 iolog_delay(td, ipo->delay);
115 void prune_io_piece_log(struct thread_data *td)
117 struct io_piece *ipo;
120 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
121 ipo = rb_entry(n, struct io_piece, rb_node);
122 rb_erase(n, &td->io_hist_tree);
126 while (!list_empty(&td->io_hist_list)) {
127 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
128 list_del(&ipo->list);
134 * log a successful write, so we can unwind the log for verify
136 void log_io_piece(struct thread_data *td, struct io_u *io_u)
138 struct rb_node **p, *parent;
139 struct io_piece *ipo, *__ipo;
141 ipo = malloc(sizeof(struct io_piece));
142 ipo->file = io_u->file;
143 ipo->offset = io_u->offset;
144 ipo->len = io_u->buflen;
147 * We don't need to sort the entries, if:
149 * Sequential writes, or
150 * Random writes that lay out the file as it goes along
152 * For both these cases, just reading back data in the order we
153 * wrote it out is the fastest.
155 if (!td_random(td) || !td->o.overwrite) {
156 INIT_LIST_HEAD(&ipo->list);
157 list_add_tail(&ipo->list, &td->io_hist_list);
161 RB_CLEAR_NODE(&ipo->rb_node);
162 p = &td->io_hist_tree.rb_node;
166 * Sort the entry into the verification list
171 __ipo = rb_entry(parent, struct io_piece, rb_node);
172 if (ipo->offset <= __ipo->offset)
178 rb_link_node(&ipo->rb_node, parent, p);
179 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
182 void write_iolog_close(struct thread_data *td)
188 td->iolog_buf = NULL;
192 * Read version 2 iolog data. It is enhanced to include per-file logging,
195 static int read_iolog2(struct thread_data *td, FILE *f)
197 unsigned long long offset;
199 int reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
204 free_release_files(td);
207 * Read in the read iolog and store it, reuse the infrastructure
208 * for doing verifications.
211 fname = malloc(256+16);
212 act = malloc(256+16);
215 while ((p = fgets(str, 4096, f)) != NULL) {
216 struct io_piece *ipo;
219 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset,
225 if (!strcmp(act, "read"))
227 else if (!strcmp(act, "write"))
229 else if (!strcmp(act, "sync"))
232 log_err("fio: bad iolog file action: %s\n",
238 if (!strcmp(act, "add")) {
240 fileno = add_file(td, fname);
241 file_action = FIO_LOG_ADD_FILE;
243 } else if (!strcmp(act, "open")) {
244 fileno = get_fileno(td, fname);
245 file_action = FIO_LOG_OPEN_FILE;
246 } else if (!strcmp(act, "close")) {
247 fileno = get_fileno(td, fname);
248 file_action = FIO_LOG_CLOSE_FILE;
250 log_err("fio: bad iolog file action: %s\n",
255 log_err("bad iolog2: %s", p);
261 else if (rw == DDIR_WRITE) {
263 * Don't add a write for ro mode
268 } else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
269 log_err("bad ddir: %d\n", rw);
276 ipo = malloc(sizeof(*ipo));
277 memset(ipo, 0, sizeof(*ipo));
278 INIT_LIST_HEAD(&ipo->list);
279 ipo->offset = offset;
282 if (bytes > td->o.max_bs[rw])
283 td->o.max_bs[rw] = bytes;
284 if (rw == DDIR_INVAL) {
285 ipo->fileno = fileno;
286 ipo->file_action = file_action;
288 queue_io_piece(td, ipo);
295 if (writes && read_only) {
296 log_err("fio: <%s> skips replay of %d writes due to"
297 " read-only\n", td->o.name, writes);
301 if (!reads && !writes)
303 else if (reads && !writes)
304 td->o.td_ddir = TD_DDIR_READ;
305 else if (!reads && writes)
306 td->o.td_ddir = TD_DDIR_WRITE;
308 td->o.td_ddir = TD_DDIR_RW;
314 * Read version 1 iolog data.
316 static int read_iolog(struct thread_data *td, FILE *f)
318 log_err("fio: iolog version 1 is no longer supported\n");
323 * open iolog, check version, and call appropriate parser
325 static int init_iolog_read(struct thread_data *td)
327 char buffer[256], *p;
331 f = fopen(td->o.read_iolog_file, "r");
333 perror("fopen read iolog");
337 p = fgets(buffer, sizeof(buffer), f);
339 td_verror(td, errno, "iolog read");
340 log_err("fio: unable to read iolog\n");
345 * version 2 of the iolog stores a specific string as the
346 * first line, check for that
348 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
349 ret = read_iolog2(td, f);
352 * seek back to the beginning
354 if (fseek(f, 0, SEEK_SET) < 0) {
355 td_verror(td, errno, "iolog read");
356 log_err("fio: unable to read iolog\n");
360 ret = read_iolog(td, f);
368 * Setup a log for storing io patterns.
370 static int init_iolog_write(struct thread_data *td)
376 f = fopen(td->o.write_iolog_file, "w+");
378 perror("fopen write iolog");
383 * That's it for writing, setup a log buffer and we're done.
386 td->iolog_buf = malloc(8192);
387 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
390 * write our version line
392 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
393 perror("iolog init\n");
398 * add all known files
400 for_each_file(td, ff, i)
401 log_file(td, ff, FIO_LOG_ADD_FILE);
406 int init_iolog(struct thread_data *td)
410 if (td->io_ops->flags & FIO_DISKLESSIO)
413 if (td->o.read_iolog_file) {
415 * Check if it's a blktrace file and load that if possible.
416 * Otherwise assume it's a normal log file and load that.
418 if (is_blktrace(td->o.read_iolog_file))
419 ret = load_blktrace(td, td->o.read_iolog_file);
421 ret = init_iolog_read(td);
422 } else if (td->o.write_iolog_file)
423 ret = init_iolog_write(td);
428 void setup_log(struct io_log **log)
430 struct io_log *l = malloc(sizeof(*l));
433 l->max_samples = 1024;
434 l->log = malloc(l->max_samples * sizeof(struct io_sample));
438 void __finish_log(struct io_log *log, const char *name)
443 f = fopen(name, "w");
449 for (i = 0; i < log->nr_samples; i++) {
450 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val,
459 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
463 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
464 __finish_log(log, file_name);