blob: 790d3a1e32c9d4c046bb17be0f10f44a74f5fef3 [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
43 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
Jens Axboe3c39a372006-06-06 20:56:12 +020044}
45
Jens Axboea61edde2007-05-15 14:29:58 +020046static void iolog_delay(struct thread_data *td, unsigned long delay)
47{
48 unsigned long usec = utime_since_now(&td->last_issue);
49
50 if (delay < usec)
51 return;
52
53 delay -= usec;
54
55 /*
56 * less than 100 usec delay, just regard it as noise
57 */
58 if (delay < 100)
59 return;
60
61 usec_sleep(td, delay);
62}
63
Jens Axboef7182732008-03-10 13:57:58 +010064static int ipo_special(struct thread_data *td, struct io_piece *ipo)
65{
66 struct fio_file *f;
67 int ret;
68
69 /*
70 * Not a special ipo
71 */
72 if (ipo->ddir != DDIR_INVAL)
73 return 0;
74
75 f = td->files[ipo->fileno];
76
77 switch (ipo->file_action) {
78 case FIO_LOG_OPEN_FILE:
79 ret = td_io_open_file(td, f);
80 if (!ret) {
81 free(ipo);
82 break;
83 }
84 td_verror(td, ret, "iolog open file");
85 return -1;
86 case FIO_LOG_CLOSE_FILE:
87 td_io_close_file(td, f);
88 break;
89 case FIO_LOG_UNLINK_FILE:
90 unlink(f->file_name);
91 break;
92 default:
93 log_err("fio: bad file action %d\n", ipo->file_action);
94 break;
95 }
96
97 return 1;
98}
99
Jens Axboe3c39a372006-06-06 20:56:12 +0200100int read_iolog_get(struct thread_data *td, struct io_u *io_u)
101{
102 struct io_piece *ipo;
103
Jens Axboeb3f4b4f2007-07-23 11:23:26 +0200104 while (!list_empty(&td->io_log_list)) {
Jens Axboef7182732008-03-10 13:57:58 +0100105 int ret;
106
Jens Axboe3c39a372006-06-06 20:56:12 +0200107 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
108 list_del(&ipo->list);
Jens Axboea61edde2007-05-15 14:29:58 +0200109
Jens Axboef7182732008-03-10 13:57:58 +0100110 ret = ipo_special(td, ipo);
111 if (ret < 0) {
112 free(ipo);
113 break;
114 } else if (ret > 0) {
115 free(ipo);
116 continue;
Jens Axboef29b25a2007-07-23 08:56:43 +0200117 }
118
Jens Axboe429f6672007-07-23 10:38:43 +0200119 io_u->offset = ipo->offset;
120 io_u->buflen = ipo->len;
121 io_u->ddir = ipo->ddir;
Jens Axboe126d65c2008-03-01 18:04:31 +0100122 io_u->file = td->files[ipo->fileno];
Jens Axboe429f6672007-07-23 10:38:43 +0200123 get_file(io_u->file);
124
Jens Axboeee56ad52008-02-01 10:30:20 +0100125 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
126 io_u->buflen, io_u->file->file_name);
127
Jens Axboea61edde2007-05-15 14:29:58 +0200128 if (ipo->delay)
129 iolog_delay(td, ipo->delay);
130
Jens Axboe3c39a372006-06-06 20:56:12 +0200131 free(ipo);
132 return 0;
133 }
134
Jens Axboe20e354e2007-07-23 14:36:16 +0200135 td->done = 1;
Jens Axboe3c39a372006-06-06 20:56:12 +0200136 return 1;
137}
138
139void prune_io_piece_log(struct thread_data *td)
140{
141 struct io_piece *ipo;
Jens Axboe4b878982007-03-26 09:32:22 +0200142 struct rb_node *n;
Jens Axboe3c39a372006-06-06 20:56:12 +0200143
Jens Axboe4b878982007-03-26 09:32:22 +0200144 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);
Jens Axboe3c39a372006-06-06 20:56:12 +0200147 free(ipo);
148 }
Jens Axboe8ce9cd32008-02-21 09:52:35 +0100149
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);
153 free(ipo);
154 }
Jens Axboe3c39a372006-06-06 20:56:12 +0200155}
156
157/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100158 * log a successful write, so we can unwind the log for verify
Jens Axboe3c39a372006-06-06 20:56:12 +0200159 */
160void log_io_piece(struct thread_data *td, struct io_u *io_u)
161{
Jens Axboe8de8f042007-03-27 10:36:12 +0200162 struct rb_node **p, *parent;
Jens Axboe4b878982007-03-26 09:32:22 +0200163 struct io_piece *ipo, *__ipo;
Jens Axboe3c39a372006-06-06 20:56:12 +0200164
Jens Axboe4b878982007-03-26 09:32:22 +0200165 ipo = malloc(sizeof(struct io_piece));
Jens Axboe53cdc682006-10-18 11:50:58 +0200166 ipo->file = io_u->file;
Jens Axboe3c39a372006-06-06 20:56:12 +0200167 ipo->offset = io_u->offset;
168 ipo->len = io_u->buflen;
169
170 /*
Jens Axboe8de8f042007-03-27 10:36:12 +0200171 * We don't need to sort the entries, if:
172 *
173 * Sequential writes, or
174 * Random writes that lay out the file as it goes along
175 *
176 * For both these cases, just reading back data in the order we
177 * wrote it out is the fastest.
178 */
Jens Axboe9b23c9f2008-02-21 09:51:32 +0100179 if (!td_random(td) || !td->o.overwrite) {
Jens Axboe8de8f042007-03-27 10:36:12 +0200180 INIT_LIST_HEAD(&ipo->list);
181 list_add_tail(&ipo->list, &td->io_hist_list);
182 return;
183 }
184
185 RB_CLEAR_NODE(&ipo->rb_node);
186 p = &td->io_hist_tree.rb_node;
187 parent = NULL;
188
189 /*
Jens Axboe4b878982007-03-26 09:32:22 +0200190 * Sort the entry into the verification list
Jens Axboe3c39a372006-06-06 20:56:12 +0200191 */
Jens Axboe4b878982007-03-26 09:32:22 +0200192 while (*p) {
193 parent = *p;
Jens Axboe3c39a372006-06-06 20:56:12 +0200194
Jens Axboe4b878982007-03-26 09:32:22 +0200195 __ipo = rb_entry(parent, struct io_piece, rb_node);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200196 if (ipo->offset <= __ipo->offset)
Jens Axboe4b878982007-03-26 09:32:22 +0200197 p = &(*p)->rb_left;
Jens Axboe4b878982007-03-26 09:32:22 +0200198 else
Jens Axboebb5d7d02007-03-27 10:21:25 +0200199 p = &(*p)->rb_right;
Jens Axboe3c39a372006-06-06 20:56:12 +0200200 }
201
Jens Axboe4b878982007-03-26 09:32:22 +0200202 rb_link_node(&ipo->rb_node, parent, p);
203 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +0200204}
205
206void write_iolog_close(struct thread_data *td)
207{
208 fflush(td->iolog_f);
209 fclose(td->iolog_f);
210 free(td->iolog_buf);
Jens Axboef29b25a2007-07-23 08:56:43 +0200211 td->iolog_f = NULL;
212 td->iolog_buf = NULL;
Jens Axboe3c39a372006-06-06 20:56:12 +0200213}
214
Jens Axboefb71fbd2006-10-20 09:15:46 +0200215/*
Jens Axboef29b25a2007-07-23 08:56:43 +0200216 * Read version 2 iolog data. It is enhanced to include per-file logging,
217 * syncs, etc.
Jens Axboefb71fbd2006-10-20 09:15:46 +0200218 */
Jens Axboef29b25a2007-07-23 08:56:43 +0200219static int read_iolog2(struct thread_data *td, FILE *f)
220{
221 unsigned long long offset;
222 unsigned int bytes;
Jens Axboe53fa9b62007-07-23 11:25:39 +0200223 int reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
Jens Axboef29b25a2007-07-23 08:56:43 +0200224 char *fname, *act;
225 char *str, *p;
Jens Axboe53fa9b62007-07-23 11:25:39 +0200226 enum fio_ddir rw;
Jens Axboef29b25a2007-07-23 08:56:43 +0200227
228 free_release_files(td);
229
230 /*
231 * Read in the read iolog and store it, reuse the infrastructure
232 * for doing verifications.
233 */
234 str = malloc(4096);
235 fname = malloc(256+16);
236 act = malloc(256+16);
237
238 reads = writes = 0;
239 while ((p = fgets(str, 4096, f)) != NULL) {
240 struct io_piece *ipo;
241 int r;
242
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100243 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset,
244 &bytes);
Jens Axboef29b25a2007-07-23 08:56:43 +0200245 if (r == 4) {
246 /*
247 * Check action first
248 */
249 if (!strcmp(act, "read"))
250 rw = DDIR_READ;
251 else if (!strcmp(act, "write"))
252 rw = DDIR_WRITE;
253 else if (!strcmp(act, "sync"))
254 rw = DDIR_SYNC;
255 else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100256 log_err("fio: bad iolog file action: %s\n",
257 act);
Jens Axboef29b25a2007-07-23 08:56:43 +0200258 continue;
259 }
260 } else if (r == 2) {
261 rw = DDIR_INVAL;
262 if (!strcmp(act, "add")) {
263 td->o.nr_files++;
264 fileno = add_file(td, fname);
265 file_action = FIO_LOG_ADD_FILE;
266 continue;
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;
273 } else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100274 log_err("fio: bad iolog file action: %s\n",
275 act);
Jens Axboef29b25a2007-07-23 08:56:43 +0200276 continue;
277 }
278 } else {
279 log_err("bad iolog2: %s", p);
280 continue;
281 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100282
Jens Axboef29b25a2007-07-23 08:56:43 +0200283 if (rw == DDIR_READ)
284 reads++;
Jens Axboe4241ea82007-09-12 08:18:36 +0200285 else if (rw == DDIR_WRITE) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200286 /*
287 * Don't add a write for ro mode
288 */
289 if (read_only)
290 continue;
Jens Axboeed4aa702008-03-07 13:39:59 +0100291 writes++;
Jens Axboe4241ea82007-09-12 08:18:36 +0200292 } else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
Jens Axboef29b25a2007-07-23 08:56:43 +0200293 log_err("bad ddir: %d\n", rw);
294 continue;
295 }
296
297 /*
298 * Make note of file
299 */
300 ipo = malloc(sizeof(*ipo));
301 memset(ipo, 0, sizeof(*ipo));
302 INIT_LIST_HEAD(&ipo->list);
303 ipo->offset = offset;
304 ipo->len = bytes;
Jens Axboe53fa9b62007-07-23 11:25:39 +0200305 ipo->ddir = rw;
Jens Axboef29b25a2007-07-23 08:56:43 +0200306 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;
311 }
Jens Axboe691c8fb2008-03-07 14:26:26 +0100312 queue_io_piece(td, ipo);
Jens Axboef29b25a2007-07-23 08:56:43 +0200313 }
314
315 free(str);
316 free(act);
317 free(fname);
318
Jens Axboe4241ea82007-09-12 08:18:36 +0200319 if (writes && read_only) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100320 log_err("fio: <%s> skips replay of %d writes due to"
321 " read-only\n", td->o.name, writes);
Jens Axboe4241ea82007-09-12 08:18:36 +0200322 writes = 0;
323 }
324
Jens Axboef29b25a2007-07-23 08:56:43 +0200325 if (!reads && !writes)
326 return 1;
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;
331 else
332 td->o.td_ddir = TD_DDIR_RW;
333
334 return 0;
335}
336
337/*
Jens Axboef29b25a2007-07-23 08:56:43 +0200338 * open iolog, check version, and call appropriate parser
339 */
340static int init_iolog_read(struct thread_data *td)
341{
342 char buffer[256], *p;
343 FILE *f;
344 int ret;
345
346 f = fopen(td->o.read_iolog_file, "r");
347 if (!f) {
348 perror("fopen read iolog");
349 return 1;
350 }
351
352 p = fgets(buffer, sizeof(buffer), f);
353 if (!p) {
354 td_verror(td, errno, "iolog read");
355 log_err("fio: unable to read iolog\n");
356 return 1;
357 }
358
359 /*
360 * version 2 of the iolog stores a specific string as the
361 * first line, check for that
362 */
363 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
364 ret = read_iolog2(td, f);
365 else {
Jens Axboeaec2de22008-04-24 12:44:42 +0200366 log_err("fio: iolog version 1 is no longer supported\n");
367 ret = 1;
Jens Axboef29b25a2007-07-23 08:56:43 +0200368 }
369
370 fclose(f);
371 return ret;
372}
373
374/*
Jens Axboefb71fbd2006-10-20 09:15:46 +0200375 * Setup a log for storing io patterns.
376 */
377static int init_iolog_write(struct thread_data *td)
378{
Jens Axboef29b25a2007-07-23 08:56:43 +0200379 struct fio_file *ff;
Jens Axboe076efc72006-10-27 11:24:25 +0200380 FILE *f;
Jens Axboef29b25a2007-07-23 08:56:43 +0200381 unsigned int i;
Jens Axboe733ed592007-04-25 14:24:12 +0200382
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100383 f = fopen(td->o.write_iolog_file, "w+");
Jens Axboefb71fbd2006-10-20 09:15:46 +0200384 if (!f) {
385 perror("fopen write iolog");
386 return 1;
387 }
388
389 /*
390 * That's it for writing, setup a log buffer and we're done.
391 */
392 td->iolog_f = f;
393 td->iolog_buf = malloc(8192);
394 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
Jens Axboef29b25a2007-07-23 08:56:43 +0200395
396 /*
397 * write our version line
398 */
399 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
400 perror("iolog init\n");
401 return 1;
402 }
403
404 /*
405 * add all known files
406 */
407 for_each_file(td, ff, i)
408 log_file(td, ff, FIO_LOG_ADD_FILE);
409
Jens Axboefb71fbd2006-10-20 09:15:46 +0200410 return 0;
411}
412
413int init_iolog(struct thread_data *td)
414{
Jens Axboeb4a6a592006-10-20 13:54:47 +0200415 int ret = 0;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200416
Jens Axboeba0fbe12007-03-09 14:34:23 +0100417 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200418 return 0;
419
Jens Axboefb7b71a2007-05-15 08:44:04 +0200420 if (td->o.read_iolog_file) {
421 /*
422 * Check if it's a blktrace file and load that if possible.
423 * Otherwise assume it's a normal log file and load that.
424 */
425 if (is_blktrace(td->o.read_iolog_file))
426 ret = load_blktrace(td, td->o.read_iolog_file);
427 else
428 ret = init_iolog_read(td);
429 } else if (td->o.write_iolog_file)
Jens Axboeb4a6a592006-10-20 13:54:47 +0200430 ret = init_iolog_write(td);
431
Jens Axboe1e97cce2006-12-05 11:44:16 +0100432 return ret;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200433}
434
Jens Axboe8914a9d2006-06-07 11:14:56 +0200435void setup_log(struct io_log **log)
436{
437 struct io_log *l = malloc(sizeof(*l));
438
439 l->nr_samples = 0;
440 l->max_samples = 1024;
441 l->log = malloc(l->max_samples * sizeof(struct io_sample));
442 *log = l;
443}
444
Jens Axboebb3884d2007-01-17 17:23:11 +1100445void __finish_log(struct io_log *log, const char *name)
Jens Axboe8914a9d2006-06-07 11:14:56 +0200446{
Jens Axboe8914a9d2006-06-07 11:14:56 +0200447 unsigned int i;
Jens Axboebb3884d2007-01-17 17:23:11 +1100448 FILE *f;
Jens Axboe8914a9d2006-06-07 11:14:56 +0200449
Jens Axboebb3884d2007-01-17 17:23:11 +1100450 f = fopen(name, "w");
Jens Axboe8914a9d2006-06-07 11:14:56 +0200451 if (!f) {
452 perror("fopen log");
453 return;
454 }
455
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100456 for (i = 0; i < log->nr_samples; i++) {
457 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val,
458 log->log[i].ddir);
459 }
Jens Axboe8914a9d2006-06-07 11:14:56 +0200460
461 fclose(f);
462 free(log->log);
463 free(log);
464}
Jens Axboebb3884d2007-01-17 17:23:11 +1100465
466void finish_log(struct thread_data *td, struct io_log *log, const char *name)
467{
Jens Axboe748b23a2008-05-07 14:28:22 +0200468 char file_name[256], *p;
Jens Axboebb3884d2007-01-17 17:23:11 +1100469
Jens Axboe84f83172008-04-04 23:15:19 +0200470 snprintf(file_name, 200, "%s_%s.log", td->o.name, name);
Jens Axboe748b23a2008-05-07 14:28:22 +0200471 p = basename(file_name);
472 __finish_log(log, p);
Jens Axboebb3884d2007-01-17 17:23:11 +1100473}