blob: 8f92e93f065d7b57234bbdbccf8bab875f785fe9 [file] [log] [blame]
Jens Axboef29b25a2007-07-23 08:56:43 +02001/*
2 * Code related to writing an iolog of what a thread is doing, and to
3 * later read that back and replay
4 */
Jens Axboe3c39a372006-06-06 20:56:12 +02005#include <stdio.h>
6#include <stdlib.h>
Jens Axboef29b25a2007-07-23 08:56:43 +02007#include <assert.h>
Jens Axboe3c39a372006-06-06 20:56:12 +02008#include "list.h"
9#include "fio.h"
10
Jens Axboef29b25a2007-07-23 08:56:43 +020011static const char iolog_ver2[] = "fio version 2 iolog";
12
Jens Axboe691c8fb2008-03-07 14:26:26 +010013void queue_io_piece(struct thread_data *td, struct io_piece *ipo)
14{
15 list_add_tail(&ipo->list, &td->io_log_list);
16 td->total_io_size += ipo->len;
17}
18
Jens Axboef29b25a2007-07-23 08:56:43 +020019void log_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboe3c39a372006-06-06 20:56:12 +020020{
Jens Axboef29b25a2007-07-23 08:56:43 +020021 const char *act[] = { "read", "write", "sync" };
22
23 assert(io_u->ddir < 3);
24
25 if (!td->o.write_iolog_file)
26 return;
27
Jens Axboe5ec10ea2008-03-06 15:42:00 +010028 fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name,
29 act[io_u->ddir], io_u->offset,
30 io_u->buflen);
Jens Axboef29b25a2007-07-23 08:56:43 +020031}
32
33void log_file(struct thread_data *td, struct fio_file *f,
34 enum file_log_act what)
35{
36 const char *act[] = { "add", "open", "close" };
37
38 assert(what < 3);
39
40 if (!td->o.write_iolog_file)
41 return;
42
Jens Axboe393ca7e2008-05-15 09:17:42 +020043
44 /*
45 * this happens on the pre-open/close done before the job starts
46 */
47 if (!td->iolog_f)
48 return;
49
Jens Axboef29b25a2007-07-23 08:56:43 +020050 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
Jens Axboe3c39a372006-06-06 20:56:12 +020051}
52
Jens Axboea61edde2007-05-15 14:29:58 +020053static void iolog_delay(struct thread_data *td, unsigned long delay)
54{
55 unsigned long usec = utime_since_now(&td->last_issue);
56
57 if (delay < usec)
58 return;
59
60 delay -= usec;
61
62 /*
63 * less than 100 usec delay, just regard it as noise
64 */
65 if (delay < 100)
66 return;
67
68 usec_sleep(td, delay);
69}
70
Jens Axboef7182732008-03-10 13:57:58 +010071static int ipo_special(struct thread_data *td, struct io_piece *ipo)
72{
73 struct fio_file *f;
74 int ret;
75
76 /*
77 * Not a special ipo
78 */
79 if (ipo->ddir != DDIR_INVAL)
80 return 0;
81
82 f = td->files[ipo->fileno];
83
84 switch (ipo->file_action) {
85 case FIO_LOG_OPEN_FILE:
86 ret = td_io_open_file(td, f);
87 if (!ret) {
88 free(ipo);
89 break;
90 }
91 td_verror(td, ret, "iolog open file");
92 return -1;
93 case FIO_LOG_CLOSE_FILE:
94 td_io_close_file(td, f);
95 break;
96 case FIO_LOG_UNLINK_FILE:
97 unlink(f->file_name);
98 break;
99 default:
100 log_err("fio: bad file action %d\n", ipo->file_action);
101 break;
102 }
103
104 return 1;
105}
106
Jens Axboe3c39a372006-06-06 20:56:12 +0200107int read_iolog_get(struct thread_data *td, struct io_u *io_u)
108{
109 struct io_piece *ipo;
110
Jens Axboeb3f4b4f2007-07-23 11:23:26 +0200111 while (!list_empty(&td->io_log_list)) {
Jens Axboef7182732008-03-10 13:57:58 +0100112 int ret;
113
Jens Axboe3c39a372006-06-06 20:56:12 +0200114 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
115 list_del(&ipo->list);
Jens Axboea61edde2007-05-15 14:29:58 +0200116
Jens Axboef7182732008-03-10 13:57:58 +0100117 ret = ipo_special(td, ipo);
118 if (ret < 0) {
119 free(ipo);
120 break;
121 } else if (ret > 0) {
122 free(ipo);
123 continue;
Jens Axboef29b25a2007-07-23 08:56:43 +0200124 }
125
Jens Axboe429f6672007-07-23 10:38:43 +0200126 io_u->offset = ipo->offset;
127 io_u->buflen = ipo->len;
128 io_u->ddir = ipo->ddir;
Jens Axboe126d65c2008-03-01 18:04:31 +0100129 io_u->file = td->files[ipo->fileno];
Jens Axboe429f6672007-07-23 10:38:43 +0200130 get_file(io_u->file);
131
Jens Axboeee56ad52008-02-01 10:30:20 +0100132 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
133 io_u->buflen, io_u->file->file_name);
134
Jens Axboea61edde2007-05-15 14:29:58 +0200135 if (ipo->delay)
136 iolog_delay(td, ipo->delay);
137
Jens Axboe3c39a372006-06-06 20:56:12 +0200138 free(ipo);
139 return 0;
140 }
141
Jens Axboe20e354e2007-07-23 14:36:16 +0200142 td->done = 1;
Jens Axboe3c39a372006-06-06 20:56:12 +0200143 return 1;
144}
145
146void prune_io_piece_log(struct thread_data *td)
147{
148 struct io_piece *ipo;
Jens Axboe4b878982007-03-26 09:32:22 +0200149 struct rb_node *n;
Jens Axboe3c39a372006-06-06 20:56:12 +0200150
Jens Axboe4b878982007-03-26 09:32:22 +0200151 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
152 ipo = rb_entry(n, struct io_piece, rb_node);
153 rb_erase(n, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +0200154 free(ipo);
155 }
Jens Axboe8ce9cd32008-02-21 09:52:35 +0100156
157 while (!list_empty(&td->io_hist_list)) {
158 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
159 list_del(&ipo->list);
160 free(ipo);
161 }
Jens Axboe3c39a372006-06-06 20:56:12 +0200162}
163
164/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100165 * log a successful write, so we can unwind the log for verify
Jens Axboe3c39a372006-06-06 20:56:12 +0200166 */
167void log_io_piece(struct thread_data *td, struct io_u *io_u)
168{
Jens Axboe8de8f042007-03-27 10:36:12 +0200169 struct rb_node **p, *parent;
Jens Axboe4b878982007-03-26 09:32:22 +0200170 struct io_piece *ipo, *__ipo;
Jens Axboe3c39a372006-06-06 20:56:12 +0200171
Jens Axboe4b878982007-03-26 09:32:22 +0200172 ipo = malloc(sizeof(struct io_piece));
Jens Axboe53cdc682006-10-18 11:50:58 +0200173 ipo->file = io_u->file;
Jens Axboe3c39a372006-06-06 20:56:12 +0200174 ipo->offset = io_u->offset;
175 ipo->len = io_u->buflen;
176
177 /*
Jens Axboe8de8f042007-03-27 10:36:12 +0200178 * We don't need to sort the entries, if:
179 *
180 * Sequential writes, or
181 * Random writes that lay out the file as it goes along
182 *
183 * For both these cases, just reading back data in the order we
184 * wrote it out is the fastest.
185 */
Jens Axboe9b23c9f2008-02-21 09:51:32 +0100186 if (!td_random(td) || !td->o.overwrite) {
Jens Axboe8de8f042007-03-27 10:36:12 +0200187 INIT_LIST_HEAD(&ipo->list);
188 list_add_tail(&ipo->list, &td->io_hist_list);
189 return;
190 }
191
192 RB_CLEAR_NODE(&ipo->rb_node);
193 p = &td->io_hist_tree.rb_node;
194 parent = NULL;
195
196 /*
Jens Axboe4b878982007-03-26 09:32:22 +0200197 * Sort the entry into the verification list
Jens Axboe3c39a372006-06-06 20:56:12 +0200198 */
Jens Axboe4b878982007-03-26 09:32:22 +0200199 while (*p) {
200 parent = *p;
Jens Axboe3c39a372006-06-06 20:56:12 +0200201
Jens Axboe4b878982007-03-26 09:32:22 +0200202 __ipo = rb_entry(parent, struct io_piece, rb_node);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200203 if (ipo->offset <= __ipo->offset)
Jens Axboe4b878982007-03-26 09:32:22 +0200204 p = &(*p)->rb_left;
Jens Axboe4b878982007-03-26 09:32:22 +0200205 else
Jens Axboebb5d7d02007-03-27 10:21:25 +0200206 p = &(*p)->rb_right;
Jens Axboe3c39a372006-06-06 20:56:12 +0200207 }
208
Jens Axboe4b878982007-03-26 09:32:22 +0200209 rb_link_node(&ipo->rb_node, parent, p);
210 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +0200211}
212
213void write_iolog_close(struct thread_data *td)
214{
215 fflush(td->iolog_f);
216 fclose(td->iolog_f);
217 free(td->iolog_buf);
Jens Axboef29b25a2007-07-23 08:56:43 +0200218 td->iolog_f = NULL;
219 td->iolog_buf = NULL;
Jens Axboe3c39a372006-06-06 20:56:12 +0200220}
221
Jens Axboefb71fbd2006-10-20 09:15:46 +0200222/*
Jens Axboef29b25a2007-07-23 08:56:43 +0200223 * Read version 2 iolog data. It is enhanced to include per-file logging,
224 * syncs, etc.
Jens Axboefb71fbd2006-10-20 09:15:46 +0200225 */
Jens Axboef29b25a2007-07-23 08:56:43 +0200226static int read_iolog2(struct thread_data *td, FILE *f)
227{
228 unsigned long long offset;
229 unsigned int bytes;
Jens Axboe53fa9b62007-07-23 11:25:39 +0200230 int reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
Jens Axboef29b25a2007-07-23 08:56:43 +0200231 char *fname, *act;
232 char *str, *p;
Jens Axboe53fa9b62007-07-23 11:25:39 +0200233 enum fio_ddir rw;
Jens Axboef29b25a2007-07-23 08:56:43 +0200234
235 free_release_files(td);
236
237 /*
238 * Read in the read iolog and store it, reuse the infrastructure
239 * for doing verifications.
240 */
241 str = malloc(4096);
242 fname = malloc(256+16);
243 act = malloc(256+16);
244
245 reads = writes = 0;
246 while ((p = fgets(str, 4096, f)) != NULL) {
247 struct io_piece *ipo;
248 int r;
249
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100250 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset,
251 &bytes);
Jens Axboef29b25a2007-07-23 08:56:43 +0200252 if (r == 4) {
253 /*
254 * Check action first
255 */
256 if (!strcmp(act, "read"))
257 rw = DDIR_READ;
258 else if (!strcmp(act, "write"))
259 rw = DDIR_WRITE;
260 else if (!strcmp(act, "sync"))
261 rw = DDIR_SYNC;
262 else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100263 log_err("fio: bad iolog file action: %s\n",
264 act);
Jens Axboef29b25a2007-07-23 08:56:43 +0200265 continue;
266 }
267 } else if (r == 2) {
268 rw = DDIR_INVAL;
269 if (!strcmp(act, "add")) {
270 td->o.nr_files++;
271 fileno = add_file(td, fname);
272 file_action = FIO_LOG_ADD_FILE;
273 continue;
274 } else if (!strcmp(act, "open")) {
275 fileno = get_fileno(td, fname);
276 file_action = FIO_LOG_OPEN_FILE;
277 } else if (!strcmp(act, "close")) {
278 fileno = get_fileno(td, fname);
279 file_action = FIO_LOG_CLOSE_FILE;
280 } else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100281 log_err("fio: bad iolog file action: %s\n",
282 act);
Jens Axboef29b25a2007-07-23 08:56:43 +0200283 continue;
284 }
285 } else {
286 log_err("bad iolog2: %s", p);
287 continue;
288 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100289
Jens Axboef29b25a2007-07-23 08:56:43 +0200290 if (rw == DDIR_READ)
291 reads++;
Jens Axboe4241ea82007-09-12 08:18:36 +0200292 else if (rw == DDIR_WRITE) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200293 /*
294 * Don't add a write for ro mode
295 */
296 if (read_only)
297 continue;
Jens Axboeed4aa702008-03-07 13:39:59 +0100298 writes++;
Jens Axboe4241ea82007-09-12 08:18:36 +0200299 } else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
Jens Axboef29b25a2007-07-23 08:56:43 +0200300 log_err("bad ddir: %d\n", rw);
301 continue;
302 }
303
304 /*
305 * Make note of file
306 */
307 ipo = malloc(sizeof(*ipo));
308 memset(ipo, 0, sizeof(*ipo));
309 INIT_LIST_HEAD(&ipo->list);
310 ipo->offset = offset;
311 ipo->len = bytes;
Jens Axboe53fa9b62007-07-23 11:25:39 +0200312 ipo->ddir = rw;
Jens Axboef29b25a2007-07-23 08:56:43 +0200313 if (bytes > td->o.max_bs[rw])
314 td->o.max_bs[rw] = bytes;
315 if (rw == DDIR_INVAL) {
316 ipo->fileno = fileno;
317 ipo->file_action = file_action;
318 }
Jens Axboe691c8fb2008-03-07 14:26:26 +0100319 queue_io_piece(td, ipo);
Jens Axboef29b25a2007-07-23 08:56:43 +0200320 }
321
322 free(str);
323 free(act);
324 free(fname);
325
Jens Axboe4241ea82007-09-12 08:18:36 +0200326 if (writes && read_only) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100327 log_err("fio: <%s> skips replay of %d writes due to"
328 " read-only\n", td->o.name, writes);
Jens Axboe4241ea82007-09-12 08:18:36 +0200329 writes = 0;
330 }
331
Jens Axboef29b25a2007-07-23 08:56:43 +0200332 if (!reads && !writes)
333 return 1;
334 else if (reads && !writes)
335 td->o.td_ddir = TD_DDIR_READ;
336 else if (!reads && writes)
337 td->o.td_ddir = TD_DDIR_WRITE;
338 else
339 td->o.td_ddir = TD_DDIR_RW;
340
341 return 0;
342}
343
344/*
Jens Axboef29b25a2007-07-23 08:56:43 +0200345 * open iolog, check version, and call appropriate parser
346 */
347static int init_iolog_read(struct thread_data *td)
348{
349 char buffer[256], *p;
350 FILE *f;
351 int ret;
352
353 f = fopen(td->o.read_iolog_file, "r");
354 if (!f) {
355 perror("fopen read iolog");
356 return 1;
357 }
358
359 p = fgets(buffer, sizeof(buffer), f);
360 if (!p) {
361 td_verror(td, errno, "iolog read");
362 log_err("fio: unable to read iolog\n");
363 return 1;
364 }
365
366 /*
367 * version 2 of the iolog stores a specific string as the
368 * first line, check for that
369 */
370 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
371 ret = read_iolog2(td, f);
372 else {
Jens Axboeaec2de22008-04-24 12:44:42 +0200373 log_err("fio: iolog version 1 is no longer supported\n");
374 ret = 1;
Jens Axboef29b25a2007-07-23 08:56:43 +0200375 }
376
377 fclose(f);
378 return ret;
379}
380
381/*
Jens Axboefb71fbd2006-10-20 09:15:46 +0200382 * Setup a log for storing io patterns.
383 */
384static int init_iolog_write(struct thread_data *td)
385{
Jens Axboef29b25a2007-07-23 08:56:43 +0200386 struct fio_file *ff;
Jens Axboe076efc72006-10-27 11:24:25 +0200387 FILE *f;
Jens Axboef29b25a2007-07-23 08:56:43 +0200388 unsigned int i;
Jens Axboe733ed592007-04-25 14:24:12 +0200389
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100390 f = fopen(td->o.write_iolog_file, "w+");
Jens Axboefb71fbd2006-10-20 09:15:46 +0200391 if (!f) {
392 perror("fopen write iolog");
393 return 1;
394 }
395
396 /*
397 * That's it for writing, setup a log buffer and we're done.
398 */
399 td->iolog_f = f;
400 td->iolog_buf = malloc(8192);
401 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
Jens Axboef29b25a2007-07-23 08:56:43 +0200402
403 /*
404 * write our version line
405 */
406 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
407 perror("iolog init\n");
408 return 1;
409 }
410
411 /*
412 * add all known files
413 */
414 for_each_file(td, ff, i)
415 log_file(td, ff, FIO_LOG_ADD_FILE);
416
Jens Axboefb71fbd2006-10-20 09:15:46 +0200417 return 0;
418}
419
420int init_iolog(struct thread_data *td)
421{
Jens Axboeb4a6a592006-10-20 13:54:47 +0200422 int ret = 0;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200423
Jens Axboeba0fbe12007-03-09 14:34:23 +0100424 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200425 return 0;
426
Jens Axboefb7b71a2007-05-15 08:44:04 +0200427 if (td->o.read_iolog_file) {
428 /*
429 * Check if it's a blktrace file and load that if possible.
430 * Otherwise assume it's a normal log file and load that.
431 */
432 if (is_blktrace(td->o.read_iolog_file))
433 ret = load_blktrace(td, td->o.read_iolog_file);
434 else
435 ret = init_iolog_read(td);
436 } else if (td->o.write_iolog_file)
Jens Axboeb4a6a592006-10-20 13:54:47 +0200437 ret = init_iolog_write(td);
438
Jens Axboe1e97cce2006-12-05 11:44:16 +0100439 return ret;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200440}
441
Jens Axboe8914a9d2006-06-07 11:14:56 +0200442void setup_log(struct io_log **log)
443{
444 struct io_log *l = malloc(sizeof(*l));
445
446 l->nr_samples = 0;
447 l->max_samples = 1024;
448 l->log = malloc(l->max_samples * sizeof(struct io_sample));
449 *log = l;
450}
451
Jens Axboebb3884d2007-01-17 17:23:11 +1100452void __finish_log(struct io_log *log, const char *name)
Jens Axboe8914a9d2006-06-07 11:14:56 +0200453{
Jens Axboe8914a9d2006-06-07 11:14:56 +0200454 unsigned int i;
Jens Axboebb3884d2007-01-17 17:23:11 +1100455 FILE *f;
Jens Axboe8914a9d2006-06-07 11:14:56 +0200456
Jens Axboebb3884d2007-01-17 17:23:11 +1100457 f = fopen(name, "w");
Jens Axboe8914a9d2006-06-07 11:14:56 +0200458 if (!f) {
459 perror("fopen log");
460 return;
461 }
462
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100463 for (i = 0; i < log->nr_samples; i++) {
464 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val,
465 log->log[i].ddir);
466 }
Jens Axboe8914a9d2006-06-07 11:14:56 +0200467
468 fclose(f);
469 free(log->log);
470 free(log);
471}
Jens Axboebb3884d2007-01-17 17:23:11 +1100472
473void finish_log(struct thread_data *td, struct io_log *log, const char *name)
474{
Jens Axboe748b23a2008-05-07 14:28:22 +0200475 char file_name[256], *p;
Jens Axboebb3884d2007-01-17 17:23:11 +1100476
Jens Axboe84f83172008-04-04 23:15:19 +0200477 snprintf(file_name, 200, "%s_%s.log", td->o.name, name);
Jens Axboe748b23a2008-05-07 14:28:22 +0200478 p = basename(file_name);
479 __finish_log(log, p);
Jens Axboebb3884d2007-01-17 17:23:11 +1100480}