blob: f6fbaeb26c7e1c023055ada3252bf59201cc32c1 [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
13void log_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboe3c39a372006-06-06 20:56:12 +020014{
Jens Axboef29b25a2007-07-23 08:56:43 +020015 const char *act[] = { "read", "write", "sync" };
16
17 assert(io_u->ddir < 3);
18
19 if (!td->o.write_iolog_file)
20 return;
21
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);
23}
24
25void log_file(struct thread_data *td, struct fio_file *f,
26 enum file_log_act what)
27{
28 const char *act[] = { "add", "open", "close" };
29
30 assert(what < 3);
31
32 if (!td->o.write_iolog_file)
33 return;
34
35 fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
Jens Axboe3c39a372006-06-06 20:56:12 +020036}
37
Jens Axboea61edde2007-05-15 14:29:58 +020038static void iolog_delay(struct thread_data *td, unsigned long delay)
39{
40 unsigned long usec = utime_since_now(&td->last_issue);
41
42 if (delay < usec)
43 return;
44
45 delay -= usec;
46
47 /*
48 * less than 100 usec delay, just regard it as noise
49 */
50 if (delay < 100)
51 return;
52
53 usec_sleep(td, delay);
54}
55
Jens Axboe3c39a372006-06-06 20:56:12 +020056int read_iolog_get(struct thread_data *td, struct io_u *io_u)
57{
58 struct io_piece *ipo;
59
Jens Axboeb3f4b4f2007-07-23 11:23:26 +020060 while (!list_empty(&td->io_log_list)) {
Jens Axboe3c39a372006-06-06 20:56:12 +020061 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
62 list_del(&ipo->list);
Jens Axboea61edde2007-05-15 14:29:58 +020063
Jens Axboef29b25a2007-07-23 08:56:43 +020064 /*
65 * invalid ddir, this is a file action
66 */
Jens Axboe429f6672007-07-23 10:38:43 +020067 if (ipo->ddir == DDIR_INVAL) {
Jens Axboe126d65c2008-03-01 18:04:31 +010068 struct fio_file *f = td->files[ipo->fileno];
Jens Axboef29b25a2007-07-23 08:56:43 +020069
70 if (ipo->file_action == FIO_LOG_OPEN_FILE) {
71 assert(!td_io_open_file(td, f));
72 free(ipo);
Jens Axboeb3f4b4f2007-07-23 11:23:26 +020073 continue;
Jens Axboef29b25a2007-07-23 08:56:43 +020074 } else if (ipo->file_action == FIO_LOG_CLOSE_FILE) {
75 td_io_close_file(td, f);
76 free(ipo);
Jens Axboeb3f4b4f2007-07-23 11:23:26 +020077 continue;
Jens Axboef29b25a2007-07-23 08:56:43 +020078 }
79 }
80
Jens Axboe429f6672007-07-23 10:38:43 +020081 io_u->offset = ipo->offset;
82 io_u->buflen = ipo->len;
83 io_u->ddir = ipo->ddir;
Jens Axboe126d65c2008-03-01 18:04:31 +010084 io_u->file = td->files[ipo->fileno];
Jens Axboe429f6672007-07-23 10:38:43 +020085 get_file(io_u->file);
86
Jens Axboeee56ad52008-02-01 10:30:20 +010087 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
88 io_u->buflen, io_u->file->file_name);
89
Jens Axboea61edde2007-05-15 14:29:58 +020090 if (ipo->delay)
91 iolog_delay(td, ipo->delay);
92
Jens Axboe3c39a372006-06-06 20:56:12 +020093 free(ipo);
94 return 0;
95 }
96
Jens Axboe20e354e2007-07-23 14:36:16 +020097 td->done = 1;
Jens Axboe3c39a372006-06-06 20:56:12 +020098 return 1;
99}
100
101void prune_io_piece_log(struct thread_data *td)
102{
103 struct io_piece *ipo;
Jens Axboe4b878982007-03-26 09:32:22 +0200104 struct rb_node *n;
Jens Axboe3c39a372006-06-06 20:56:12 +0200105
Jens Axboe4b878982007-03-26 09:32:22 +0200106 while ((n = rb_first(&td->io_hist_tree)) != NULL) {
107 ipo = rb_entry(n, struct io_piece, rb_node);
108 rb_erase(n, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +0200109 free(ipo);
110 }
Jens Axboe8ce9cd32008-02-21 09:52:35 +0100111
112 while (!list_empty(&td->io_hist_list)) {
113 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
114 list_del(&ipo->list);
115 free(ipo);
116 }
Jens Axboe3c39a372006-06-06 20:56:12 +0200117}
118
119/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100120 * log a successful write, so we can unwind the log for verify
Jens Axboe3c39a372006-06-06 20:56:12 +0200121 */
122void log_io_piece(struct thread_data *td, struct io_u *io_u)
123{
Jens Axboe8de8f042007-03-27 10:36:12 +0200124 struct rb_node **p, *parent;
Jens Axboe4b878982007-03-26 09:32:22 +0200125 struct io_piece *ipo, *__ipo;
Jens Axboe3c39a372006-06-06 20:56:12 +0200126
Jens Axboe4b878982007-03-26 09:32:22 +0200127 ipo = malloc(sizeof(struct io_piece));
Jens Axboe53cdc682006-10-18 11:50:58 +0200128 ipo->file = io_u->file;
Jens Axboe3c39a372006-06-06 20:56:12 +0200129 ipo->offset = io_u->offset;
130 ipo->len = io_u->buflen;
131
132 /*
Jens Axboe8de8f042007-03-27 10:36:12 +0200133 * We don't need to sort the entries, if:
134 *
135 * Sequential writes, or
136 * Random writes that lay out the file as it goes along
137 *
138 * For both these cases, just reading back data in the order we
139 * wrote it out is the fastest.
140 */
Jens Axboe9b23c9f2008-02-21 09:51:32 +0100141 if (!td_random(td) || !td->o.overwrite) {
Jens Axboe8de8f042007-03-27 10:36:12 +0200142 INIT_LIST_HEAD(&ipo->list);
143 list_add_tail(&ipo->list, &td->io_hist_list);
144 return;
145 }
146
147 RB_CLEAR_NODE(&ipo->rb_node);
148 p = &td->io_hist_tree.rb_node;
149 parent = NULL;
150
151 /*
Jens Axboe4b878982007-03-26 09:32:22 +0200152 * Sort the entry into the verification list
Jens Axboe3c39a372006-06-06 20:56:12 +0200153 */
Jens Axboe4b878982007-03-26 09:32:22 +0200154 while (*p) {
155 parent = *p;
Jens Axboe3c39a372006-06-06 20:56:12 +0200156
Jens Axboe4b878982007-03-26 09:32:22 +0200157 __ipo = rb_entry(parent, struct io_piece, rb_node);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200158 if (ipo->offset <= __ipo->offset)
Jens Axboe4b878982007-03-26 09:32:22 +0200159 p = &(*p)->rb_left;
Jens Axboe4b878982007-03-26 09:32:22 +0200160 else
Jens Axboebb5d7d02007-03-27 10:21:25 +0200161 p = &(*p)->rb_right;
Jens Axboe3c39a372006-06-06 20:56:12 +0200162 }
163
Jens Axboe4b878982007-03-26 09:32:22 +0200164 rb_link_node(&ipo->rb_node, parent, p);
165 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +0200166}
167
168void write_iolog_close(struct thread_data *td)
169{
170 fflush(td->iolog_f);
171 fclose(td->iolog_f);
172 free(td->iolog_buf);
Jens Axboef29b25a2007-07-23 08:56:43 +0200173 td->iolog_f = NULL;
174 td->iolog_buf = NULL;
Jens Axboe3c39a372006-06-06 20:56:12 +0200175}
176
Jens Axboefb71fbd2006-10-20 09:15:46 +0200177/*
Jens Axboef29b25a2007-07-23 08:56:43 +0200178 * Read version 2 iolog data. It is enhanced to include per-file logging,
179 * syncs, etc.
Jens Axboefb71fbd2006-10-20 09:15:46 +0200180 */
Jens Axboef29b25a2007-07-23 08:56:43 +0200181static int read_iolog2(struct thread_data *td, FILE *f)
182{
183 unsigned long long offset;
184 unsigned int bytes;
Jens Axboe53fa9b62007-07-23 11:25:39 +0200185 int reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
Jens Axboef29b25a2007-07-23 08:56:43 +0200186 char *fname, *act;
187 char *str, *p;
Jens Axboe53fa9b62007-07-23 11:25:39 +0200188 enum fio_ddir rw;
Jens Axboef29b25a2007-07-23 08:56:43 +0200189
190 free_release_files(td);
191
192 /*
193 * Read in the read iolog and store it, reuse the infrastructure
194 * for doing verifications.
195 */
196 str = malloc(4096);
197 fname = malloc(256+16);
198 act = malloc(256+16);
199
200 reads = writes = 0;
201 while ((p = fgets(str, 4096, f)) != NULL) {
202 struct io_piece *ipo;
203 int r;
204
205 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset, &bytes);
206 if (r == 4) {
207 /*
208 * Check action first
209 */
210 if (!strcmp(act, "read"))
211 rw = DDIR_READ;
212 else if (!strcmp(act, "write"))
213 rw = DDIR_WRITE;
214 else if (!strcmp(act, "sync"))
215 rw = DDIR_SYNC;
216 else {
217 log_err("fio: bad iolog file action: %s\n",act);
218 continue;
219 }
220 } else if (r == 2) {
221 rw = DDIR_INVAL;
222 if (!strcmp(act, "add")) {
223 td->o.nr_files++;
224 fileno = add_file(td, fname);
225 file_action = FIO_LOG_ADD_FILE;
226 continue;
227 } else if (!strcmp(act, "open")) {
228 fileno = get_fileno(td, fname);
229 file_action = FIO_LOG_OPEN_FILE;
230 } else if (!strcmp(act, "close")) {
231 fileno = get_fileno(td, fname);
232 file_action = FIO_LOG_CLOSE_FILE;
233 } else {
234 log_err("fio: bad iolog file action: %s\n",act);
235 continue;
236 }
237 } else {
238 log_err("bad iolog2: %s", p);
239 continue;
240 }
241
242 if (rw == DDIR_READ)
243 reads++;
Jens Axboe4241ea82007-09-12 08:18:36 +0200244 else if (rw == DDIR_WRITE) {
Jens Axboef29b25a2007-07-23 08:56:43 +0200245 writes++;
Jens Axboe4241ea82007-09-12 08:18:36 +0200246 /*
247 * Don't add a write for ro mode
248 */
249 if (read_only)
250 continue;
251 } else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
Jens Axboef29b25a2007-07-23 08:56:43 +0200252 log_err("bad ddir: %d\n", rw);
253 continue;
254 }
255
256 /*
257 * Make note of file
258 */
259 ipo = malloc(sizeof(*ipo));
260 memset(ipo, 0, sizeof(*ipo));
261 INIT_LIST_HEAD(&ipo->list);
262 ipo->offset = offset;
263 ipo->len = bytes;
Jens Axboe53fa9b62007-07-23 11:25:39 +0200264 ipo->ddir = rw;
Jens Axboef29b25a2007-07-23 08:56:43 +0200265 if (bytes > td->o.max_bs[rw])
266 td->o.max_bs[rw] = bytes;
267 if (rw == DDIR_INVAL) {
268 ipo->fileno = fileno;
269 ipo->file_action = file_action;
270 }
271 list_add_tail(&ipo->list, &td->io_log_list);
Jens Axboeea966f82007-09-03 10:09:37 +0200272 td->total_io_size += bytes;
Jens Axboef29b25a2007-07-23 08:56:43 +0200273 }
274
275 free(str);
276 free(act);
277 free(fname);
278
Jens Axboe4241ea82007-09-12 08:18:36 +0200279 if (writes && read_only) {
280 log_err("fio: <%s> skips replay of %d writes due to read-only\n", td->o.name, writes);
281 writes = 0;
282 }
283
Jens Axboef29b25a2007-07-23 08:56:43 +0200284 if (!reads && !writes)
285 return 1;
286 else if (reads && !writes)
287 td->o.td_ddir = TD_DDIR_READ;
288 else if (!reads && writes)
289 td->o.td_ddir = TD_DDIR_WRITE;
290 else
291 td->o.td_ddir = TD_DDIR_RW;
292
293 return 0;
294}
295
296/*
297 * Read version 1 iolog data.
298 */
299static int read_iolog(struct thread_data *td, FILE *f)
Jens Axboe3c39a372006-06-06 20:56:12 +0200300{
301 unsigned long long offset;
302 unsigned int bytes;
303 char *str, *p;
Jens Axboe21bd2982007-07-23 11:26:27 +0200304 int reads, writes;
gurudas pai59b9ddf2007-10-22 22:10:39 +0200305 int rw;
Jens Axboe3c39a372006-06-06 20:56:12 +0200306
Jens Axboe3c39a372006-06-06 20:56:12 +0200307 /*
Jens Axboe3c39a372006-06-06 20:56:12 +0200308 * Read in the read iolog and store it, reuse the infrastructure
309 * for doing verifications.
310 */
311 str = malloc(4096);
Jens Axboefb71fbd2006-10-20 09:15:46 +0200312 reads = writes = 0;
Jens Axboe3c39a372006-06-06 20:56:12 +0200313 while ((p = fgets(str, 4096, f)) != NULL) {
314 struct io_piece *ipo;
315
316 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200317 log_err("bad iolog: %s\n", p);
Jens Axboe3c39a372006-06-06 20:56:12 +0200318 continue;
319 }
320 if (rw == DDIR_READ)
321 reads++;
Jens Axboe4241ea82007-09-12 08:18:36 +0200322 else if (rw == DDIR_WRITE) {
Jens Axboe3c39a372006-06-06 20:56:12 +0200323 writes++;
Jens Axboe4241ea82007-09-12 08:18:36 +0200324 /*
325 * Don't add a write for ro mode
326 */
327 if (read_only)
328 continue;
329 } else if (rw != DDIR_SYNC) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200330 log_err("bad ddir: %d\n", rw);
Jens Axboe3c39a372006-06-06 20:56:12 +0200331 continue;
332 }
333
334 ipo = malloc(sizeof(*ipo));
Jens Axboe733ed592007-04-25 14:24:12 +0200335 memset(ipo, 0, sizeof(*ipo));
Jens Axboe3c39a372006-06-06 20:56:12 +0200336 INIT_LIST_HEAD(&ipo->list);
337 ipo->offset = offset;
338 ipo->len = bytes;
gurudas pai59b9ddf2007-10-22 22:10:39 +0200339 ipo->ddir = (enum fio_ddir) rw;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100340 if (bytes > td->o.max_bs[rw])
341 td->o.max_bs[rw] = bytes;
Jens Axboe3c39a372006-06-06 20:56:12 +0200342 list_add_tail(&ipo->list, &td->io_log_list);
Jens Axboeea966f82007-09-03 10:09:37 +0200343 td->total_io_size += bytes;
Jens Axboe3c39a372006-06-06 20:56:12 +0200344 }
345
346 free(str);
Jens Axboe3c39a372006-06-06 20:56:12 +0200347
Jens Axboe4241ea82007-09-12 08:18:36 +0200348 if (writes && read_only) {
349 log_err("fio: <%s> skips replay of %d writes due to read-only\n", td->o.name, writes);
350 writes = 0;
351 }
352
Jens Axboefb71fbd2006-10-20 09:15:46 +0200353 if (!reads && !writes)
Jens Axboe3c39a372006-06-06 20:56:12 +0200354 return 1;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200355 else if (reads && !writes)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100356 td->o.td_ddir = TD_DDIR_READ;
Jens Axboe3c39a372006-06-06 20:56:12 +0200357 else if (!reads && writes)
Jens Axboe36361eb2007-05-15 11:12:19 +0200358 td->o.td_ddir = TD_DDIR_WRITE;
Jens Axboe3c39a372006-06-06 20:56:12 +0200359 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100360 td->o.td_ddir = TD_DDIR_RW;
Jens Axboe3c39a372006-06-06 20:56:12 +0200361
362 return 0;
363}
Jens Axboe8914a9d2006-06-07 11:14:56 +0200364
Jens Axboefb71fbd2006-10-20 09:15:46 +0200365/*
Jens Axboef29b25a2007-07-23 08:56:43 +0200366 * open iolog, check version, and call appropriate parser
367 */
368static int init_iolog_read(struct thread_data *td)
369{
370 char buffer[256], *p;
371 FILE *f;
372 int ret;
373
374 f = fopen(td->o.read_iolog_file, "r");
375 if (!f) {
376 perror("fopen read iolog");
377 return 1;
378 }
379
380 p = fgets(buffer, sizeof(buffer), f);
381 if (!p) {
382 td_verror(td, errno, "iolog read");
383 log_err("fio: unable to read iolog\n");
384 return 1;
385 }
386
387 /*
388 * version 2 of the iolog stores a specific string as the
389 * first line, check for that
390 */
391 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
392 ret = read_iolog2(td, f);
393 else {
394 /*
395 * seek back to the beginning
396 */
397 if (fseek(f, 0, SEEK_SET) < 0) {
398 td_verror(td, errno, "iolog read");
399 log_err("fio: unable to read iolog\n");
400 return 1;
401 }
402
403 ret = read_iolog(td, f);
404 }
405
406 fclose(f);
407 return ret;
408}
409
410/*
Jens Axboefb71fbd2006-10-20 09:15:46 +0200411 * Setup a log for storing io patterns.
412 */
413static int init_iolog_write(struct thread_data *td)
414{
Jens Axboef29b25a2007-07-23 08:56:43 +0200415 struct fio_file *ff;
Jens Axboe076efc72006-10-27 11:24:25 +0200416 FILE *f;
Jens Axboef29b25a2007-07-23 08:56:43 +0200417 unsigned int i;
Jens Axboe733ed592007-04-25 14:24:12 +0200418
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100419 f = fopen(td->o.write_iolog_file, "w+");
Jens Axboefb71fbd2006-10-20 09:15:46 +0200420 if (!f) {
421 perror("fopen write iolog");
422 return 1;
423 }
424
425 /*
426 * That's it for writing, setup a log buffer and we're done.
427 */
428 td->iolog_f = f;
429 td->iolog_buf = malloc(8192);
430 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
Jens Axboef29b25a2007-07-23 08:56:43 +0200431
432 /*
433 * write our version line
434 */
435 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
436 perror("iolog init\n");
437 return 1;
438 }
439
440 /*
441 * add all known files
442 */
443 for_each_file(td, ff, i)
444 log_file(td, ff, FIO_LOG_ADD_FILE);
445
Jens Axboefb71fbd2006-10-20 09:15:46 +0200446 return 0;
447}
448
449int init_iolog(struct thread_data *td)
450{
Jens Axboeb4a6a592006-10-20 13:54:47 +0200451 int ret = 0;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200452
Jens Axboeba0fbe12007-03-09 14:34:23 +0100453 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200454 return 0;
455
Jens Axboefb7b71a2007-05-15 08:44:04 +0200456 if (td->o.read_iolog_file) {
457 /*
458 * Check if it's a blktrace file and load that if possible.
459 * Otherwise assume it's a normal log file and load that.
460 */
461 if (is_blktrace(td->o.read_iolog_file))
462 ret = load_blktrace(td, td->o.read_iolog_file);
463 else
464 ret = init_iolog_read(td);
465 } else if (td->o.write_iolog_file)
Jens Axboeb4a6a592006-10-20 13:54:47 +0200466 ret = init_iolog_write(td);
467
Jens Axboe1e97cce2006-12-05 11:44:16 +0100468 return ret;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200469}
470
Jens Axboe8914a9d2006-06-07 11:14:56 +0200471void setup_log(struct io_log **log)
472{
473 struct io_log *l = malloc(sizeof(*l));
474
475 l->nr_samples = 0;
476 l->max_samples = 1024;
477 l->log = malloc(l->max_samples * sizeof(struct io_sample));
478 *log = l;
479}
480
Jens Axboebb3884d2007-01-17 17:23:11 +1100481void __finish_log(struct io_log *log, const char *name)
Jens Axboe8914a9d2006-06-07 11:14:56 +0200482{
Jens Axboe8914a9d2006-06-07 11:14:56 +0200483 unsigned int i;
Jens Axboebb3884d2007-01-17 17:23:11 +1100484 FILE *f;
Jens Axboe8914a9d2006-06-07 11:14:56 +0200485
Jens Axboebb3884d2007-01-17 17:23:11 +1100486 f = fopen(name, "w");
Jens Axboe8914a9d2006-06-07 11:14:56 +0200487 if (!f) {
488 perror("fopen log");
489 return;
490 }
491
492 for (i = 0; i < log->nr_samples; i++)
493 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
494
495 fclose(f);
496 free(log->log);
497 free(log);
498}
Jens Axboebb3884d2007-01-17 17:23:11 +1100499
500void finish_log(struct thread_data *td, struct io_log *log, const char *name)
501{
502 char file_name[256];
503
504 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
505 __finish_log(log, file_name);
506}