blob: 92bcebfcca5248961b3bf1dfa74f3240199c5089 [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 Axboef29b25a2007-07-23 08:56:43 +020068 struct fio_file *f = &td->files[ipo->fileno];
69
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;
84 io_u->file = &td->files[ipo->fileno];
85 get_file(io_u->file);
86
Jens Axboea61edde2007-05-15 14:29:58 +020087 if (ipo->delay)
88 iolog_delay(td, ipo->delay);
89
Jens Axboe3c39a372006-06-06 20:56:12 +020090 free(ipo);
91 return 0;
92 }
93
94 return 1;
95}
96
97void prune_io_piece_log(struct thread_data *td)
98{
99 struct io_piece *ipo;
Jens Axboe4b878982007-03-26 09:32:22 +0200100 struct rb_node *n;
Jens Axboe3c39a372006-06-06 20:56:12 +0200101
Jens Axboe4b878982007-03-26 09:32:22 +0200102 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);
Jens Axboe3c39a372006-06-06 20:56:12 +0200105 free(ipo);
106 }
107}
108
109/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100110 * log a successful write, so we can unwind the log for verify
Jens Axboe3c39a372006-06-06 20:56:12 +0200111 */
112void log_io_piece(struct thread_data *td, struct io_u *io_u)
113{
Jens Axboe8de8f042007-03-27 10:36:12 +0200114 struct rb_node **p, *parent;
Jens Axboe4b878982007-03-26 09:32:22 +0200115 struct io_piece *ipo, *__ipo;
Jens Axboe3c39a372006-06-06 20:56:12 +0200116
Jens Axboe4b878982007-03-26 09:32:22 +0200117 ipo = malloc(sizeof(struct io_piece));
Jens Axboe53cdc682006-10-18 11:50:58 +0200118 ipo->file = io_u->file;
Jens Axboe3c39a372006-06-06 20:56:12 +0200119 ipo->offset = io_u->offset;
120 ipo->len = io_u->buflen;
121
122 /*
Jens Axboe8de8f042007-03-27 10:36:12 +0200123 * We don't need to sort the entries, if:
124 *
125 * Sequential writes, or
126 * Random writes that lay out the file as it goes along
127 *
128 * For both these cases, just reading back data in the order we
129 * wrote it out is the fastest.
130 */
Jens Axboe160b9662007-03-27 10:59:49 +0200131 if (!td_random(td) || !td->o.overwrite ||
132 (io_u->file->flags & FIO_FILE_NOSORT)) {
Jens Axboe8de8f042007-03-27 10:36:12 +0200133 INIT_LIST_HEAD(&ipo->list);
134 list_add_tail(&ipo->list, &td->io_hist_list);
135 return;
136 }
137
138 RB_CLEAR_NODE(&ipo->rb_node);
139 p = &td->io_hist_tree.rb_node;
140 parent = NULL;
141
142 /*
Jens Axboe4b878982007-03-26 09:32:22 +0200143 * Sort the entry into the verification list
Jens Axboe3c39a372006-06-06 20:56:12 +0200144 */
Jens Axboe4b878982007-03-26 09:32:22 +0200145 while (*p) {
146 parent = *p;
Jens Axboe3c39a372006-06-06 20:56:12 +0200147
Jens Axboe4b878982007-03-26 09:32:22 +0200148 __ipo = rb_entry(parent, struct io_piece, rb_node);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200149 if (ipo->offset <= __ipo->offset)
Jens Axboe4b878982007-03-26 09:32:22 +0200150 p = &(*p)->rb_left;
Jens Axboe4b878982007-03-26 09:32:22 +0200151 else
Jens Axboebb5d7d02007-03-27 10:21:25 +0200152 p = &(*p)->rb_right;
Jens Axboe3c39a372006-06-06 20:56:12 +0200153 }
154
Jens Axboe4b878982007-03-26 09:32:22 +0200155 rb_link_node(&ipo->rb_node, parent, p);
156 rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
Jens Axboe3c39a372006-06-06 20:56:12 +0200157}
158
159void write_iolog_close(struct thread_data *td)
160{
161 fflush(td->iolog_f);
162 fclose(td->iolog_f);
163 free(td->iolog_buf);
Jens Axboef29b25a2007-07-23 08:56:43 +0200164 td->iolog_f = NULL;
165 td->iolog_buf = NULL;
Jens Axboe3c39a372006-06-06 20:56:12 +0200166}
167
Jens Axboefb71fbd2006-10-20 09:15:46 +0200168/*
Jens Axboef29b25a2007-07-23 08:56:43 +0200169 * Read version 2 iolog data. It is enhanced to include per-file logging,
170 * syncs, etc.
Jens Axboefb71fbd2006-10-20 09:15:46 +0200171 */
Jens Axboef29b25a2007-07-23 08:56:43 +0200172static int read_iolog2(struct thread_data *td, FILE *f)
173{
174 unsigned long long offset;
175 unsigned int bytes;
176 int rw, reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
177 char *fname, *act;
178 char *str, *p;
179
180 free_release_files(td);
181
182 /*
183 * Read in the read iolog and store it, reuse the infrastructure
184 * for doing verifications.
185 */
186 str = malloc(4096);
187 fname = malloc(256+16);
188 act = malloc(256+16);
189
190 reads = writes = 0;
191 while ((p = fgets(str, 4096, f)) != NULL) {
192 struct io_piece *ipo;
193 int r;
194
195 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset, &bytes);
196 if (r == 4) {
197 /*
198 * Check action first
199 */
200 if (!strcmp(act, "read"))
201 rw = DDIR_READ;
202 else if (!strcmp(act, "write"))
203 rw = DDIR_WRITE;
204 else if (!strcmp(act, "sync"))
205 rw = DDIR_SYNC;
206 else {
207 log_err("fio: bad iolog file action: %s\n",act);
208 continue;
209 }
210 } else if (r == 2) {
211 rw = DDIR_INVAL;
212 if (!strcmp(act, "add")) {
213 td->o.nr_files++;
214 fileno = add_file(td, fname);
215 file_action = FIO_LOG_ADD_FILE;
216 continue;
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;
223 } else {
224 log_err("fio: bad iolog file action: %s\n",act);
225 continue;
226 }
227 } else {
228 log_err("bad iolog2: %s", p);
229 continue;
230 }
231
232 if (rw == DDIR_READ)
233 reads++;
234 else if (rw == DDIR_WRITE)
235 writes++;
236 else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
237 log_err("bad ddir: %d\n", rw);
238 continue;
239 }
240
241 /*
242 * Make note of file
243 */
244 ipo = malloc(sizeof(*ipo));
245 memset(ipo, 0, sizeof(*ipo));
246 INIT_LIST_HEAD(&ipo->list);
247 ipo->offset = offset;
248 ipo->len = bytes;
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;
255 }
256 list_add_tail(&ipo->list, &td->io_log_list);
257 }
258
259 free(str);
260 free(act);
261 free(fname);
262
263 if (!reads && !writes)
264 return 1;
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;
269 else
270 td->o.td_ddir = TD_DDIR_RW;
271
272 return 0;
273}
274
275/*
276 * Read version 1 iolog data.
277 */
278static int read_iolog(struct thread_data *td, FILE *f)
Jens Axboe3c39a372006-06-06 20:56:12 +0200279{
280 unsigned long long offset;
281 unsigned int bytes;
282 char *str, *p;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200283 int rw, reads, writes;
Jens Axboe3c39a372006-06-06 20:56:12 +0200284
Jens Axboe3c39a372006-06-06 20:56:12 +0200285 /*
Jens Axboe3c39a372006-06-06 20:56:12 +0200286 * Read in the read iolog and store it, reuse the infrastructure
287 * for doing verifications.
288 */
289 str = malloc(4096);
Jens Axboefb71fbd2006-10-20 09:15:46 +0200290 reads = writes = 0;
Jens Axboe3c39a372006-06-06 20:56:12 +0200291 while ((p = fgets(str, 4096, f)) != NULL) {
292 struct io_piece *ipo;
293
294 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200295 log_err("bad iolog: %s\n", p);
Jens Axboe3c39a372006-06-06 20:56:12 +0200296 continue;
297 }
298 if (rw == DDIR_READ)
299 reads++;
300 else if (rw == DDIR_WRITE)
301 writes++;
Jens Axboec38e9462007-03-27 08:48:48 +0200302 else if (rw != DDIR_SYNC) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200303 log_err("bad ddir: %d\n", rw);
Jens Axboe3c39a372006-06-06 20:56:12 +0200304 continue;
305 }
306
307 ipo = malloc(sizeof(*ipo));
Jens Axboe733ed592007-04-25 14:24:12 +0200308 memset(ipo, 0, sizeof(*ipo));
Jens Axboe3c39a372006-06-06 20:56:12 +0200309 INIT_LIST_HEAD(&ipo->list);
310 ipo->offset = offset;
311 ipo->len = bytes;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100312 ipo->ddir = (enum fio_ddir) rw;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100313 if (bytes > td->o.max_bs[rw])
314 td->o.max_bs[rw] = bytes;
Jens Axboe3c39a372006-06-06 20:56:12 +0200315 list_add_tail(&ipo->list, &td->io_log_list);
Jens Axboe3c39a372006-06-06 20:56:12 +0200316 }
317
318 free(str);
Jens Axboe3c39a372006-06-06 20:56:12 +0200319
Jens Axboefb71fbd2006-10-20 09:15:46 +0200320 if (!reads && !writes)
Jens Axboe3c39a372006-06-06 20:56:12 +0200321 return 1;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200322 else if (reads && !writes)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100323 td->o.td_ddir = TD_DDIR_READ;
Jens Axboe3c39a372006-06-06 20:56:12 +0200324 else if (!reads && writes)
Jens Axboe36361eb2007-05-15 11:12:19 +0200325 td->o.td_ddir = TD_DDIR_WRITE;
Jens Axboe3c39a372006-06-06 20:56:12 +0200326 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100327 td->o.td_ddir = TD_DDIR_RW;
Jens Axboe3c39a372006-06-06 20:56:12 +0200328
329 return 0;
330}
Jens Axboe8914a9d2006-06-07 11:14:56 +0200331
Jens Axboefb71fbd2006-10-20 09:15:46 +0200332/*
Jens Axboef29b25a2007-07-23 08:56:43 +0200333 * open iolog, check version, and call appropriate parser
334 */
335static int init_iolog_read(struct thread_data *td)
336{
337 char buffer[256], *p;
338 FILE *f;
339 int ret;
340
341 f = fopen(td->o.read_iolog_file, "r");
342 if (!f) {
343 perror("fopen read iolog");
344 return 1;
345 }
346
347 p = fgets(buffer, sizeof(buffer), f);
348 if (!p) {
349 td_verror(td, errno, "iolog read");
350 log_err("fio: unable to read iolog\n");
351 return 1;
352 }
353
354 /*
355 * version 2 of the iolog stores a specific string as the
356 * first line, check for that
357 */
358 if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2)))
359 ret = read_iolog2(td, f);
360 else {
361 /*
362 * seek back to the beginning
363 */
364 if (fseek(f, 0, SEEK_SET) < 0) {
365 td_verror(td, errno, "iolog read");
366 log_err("fio: unable to read iolog\n");
367 return 1;
368 }
369
370 ret = read_iolog(td, f);
371 }
372
373 fclose(f);
374 return ret;
375}
376
377/*
Jens Axboefb71fbd2006-10-20 09:15:46 +0200378 * Setup a log for storing io patterns.
379 */
380static int init_iolog_write(struct thread_data *td)
381{
Jens Axboef29b25a2007-07-23 08:56:43 +0200382 struct fio_file *ff;
Jens Axboe076efc72006-10-27 11:24:25 +0200383 FILE *f;
Jens Axboef29b25a2007-07-23 08:56:43 +0200384 unsigned int i;
Jens Axboe733ed592007-04-25 14:24:12 +0200385
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100386 f = fopen(td->o.write_iolog_file, "w+");
Jens Axboefb71fbd2006-10-20 09:15:46 +0200387 if (!f) {
388 perror("fopen write iolog");
389 return 1;
390 }
391
392 /*
393 * That's it for writing, setup a log buffer and we're done.
394 */
395 td->iolog_f = f;
396 td->iolog_buf = malloc(8192);
397 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
Jens Axboef29b25a2007-07-23 08:56:43 +0200398
399 /*
400 * write our version line
401 */
402 if (fprintf(f, "%s\n", iolog_ver2) < 0) {
403 perror("iolog init\n");
404 return 1;
405 }
406
407 /*
408 * add all known files
409 */
410 for_each_file(td, ff, i)
411 log_file(td, ff, FIO_LOG_ADD_FILE);
412
Jens Axboefb71fbd2006-10-20 09:15:46 +0200413 return 0;
414}
415
416int init_iolog(struct thread_data *td)
417{
Jens Axboeb4a6a592006-10-20 13:54:47 +0200418 int ret = 0;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200419
Jens Axboeba0fbe12007-03-09 14:34:23 +0100420 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200421 return 0;
422
Jens Axboefb7b71a2007-05-15 08:44:04 +0200423 if (td->o.read_iolog_file) {
424 /*
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.
427 */
428 if (is_blktrace(td->o.read_iolog_file))
429 ret = load_blktrace(td, td->o.read_iolog_file);
430 else
431 ret = init_iolog_read(td);
432 } else if (td->o.write_iolog_file)
Jens Axboeb4a6a592006-10-20 13:54:47 +0200433 ret = init_iolog_write(td);
434
Jens Axboe1e97cce2006-12-05 11:44:16 +0100435 return ret;
Jens Axboefb71fbd2006-10-20 09:15:46 +0200436}
437
Jens Axboe8914a9d2006-06-07 11:14:56 +0200438void setup_log(struct io_log **log)
439{
440 struct io_log *l = malloc(sizeof(*l));
441
442 l->nr_samples = 0;
443 l->max_samples = 1024;
444 l->log = malloc(l->max_samples * sizeof(struct io_sample));
445 *log = l;
446}
447
Jens Axboebb3884d2007-01-17 17:23:11 +1100448void __finish_log(struct io_log *log, const char *name)
Jens Axboe8914a9d2006-06-07 11:14:56 +0200449{
Jens Axboe8914a9d2006-06-07 11:14:56 +0200450 unsigned int i;
Jens Axboebb3884d2007-01-17 17:23:11 +1100451 FILE *f;
Jens Axboe8914a9d2006-06-07 11:14:56 +0200452
Jens Axboebb3884d2007-01-17 17:23:11 +1100453 f = fopen(name, "w");
Jens Axboe8914a9d2006-06-07 11:14:56 +0200454 if (!f) {
455 perror("fopen log");
456 return;
457 }
458
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);
461
462 fclose(f);
463 free(log->log);
464 free(log);
465}
Jens Axboebb3884d2007-01-17 17:23:11 +1100466
467void finish_log(struct thread_data *td, struct io_log *log, const char *name)
468{
469 char file_name[256];
470
471 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
472 __finish_log(log, file_name);
473}