Add option for refilling IO buffers on each submit
[fio.git] / log.c
1 /*
2  * Code related to writing an iolog of what a thread is doing, and to
3  * later read that back and replay
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <assert.h>
8 #include "list.h"
9 #include "fio.h"
10
11 static const char iolog_ver2[] = "fio version 2 iolog";
12
13 void 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
19 void log_io_u(struct thread_data *td, struct io_u *io_u)
20 {
21         const char *act[] = { "read", "write", "sync" };
22
23         assert(io_u->ddir < 3);
24
25         if (!td->o.write_iolog_file)
26                 return;
27
28         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);
31 }
32
33 void 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
44         /*
45          * this happens on the pre-open/close done before the job starts
46          */
47         if (!td->iolog_f)
48                 return;
49
50         fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]);
51 }
52
53 static 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
71 static 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
107 int read_iolog_get(struct thread_data *td, struct io_u *io_u)
108 {
109         struct io_piece *ipo;
110
111         while (!list_empty(&td->io_log_list)) {
112                 int ret;
113
114                 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
115                 list_del(&ipo->list);
116
117                 ret = ipo_special(td, ipo);
118                 if (ret < 0) {
119                         free(ipo);
120                         break;
121                 } else if (ret > 0) {
122                         free(ipo);
123                         continue;
124                 }
125
126                 io_u->offset = ipo->offset;
127                 io_u->buflen = ipo->len;
128                 io_u->ddir = ipo->ddir;
129                 io_u->file = td->files[ipo->fileno];
130                 get_file(io_u->file);
131
132                 dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset,
133                                         io_u->buflen, io_u->file->file_name);
134
135                 if (ipo->delay)
136                         iolog_delay(td, ipo->delay);
137
138                 free(ipo);
139                 return 0;
140         }
141
142         td->done = 1;
143         return 1;
144 }
145
146 void prune_io_piece_log(struct thread_data *td)
147 {
148         struct io_piece *ipo;
149         struct rb_node *n;
150
151         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);
154                 free(ipo);
155         }
156
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         }
162 }
163
164 /*
165  * log a successful write, so we can unwind the log for verify
166  */
167 void log_io_piece(struct thread_data *td, struct io_u *io_u)
168 {
169         struct rb_node **p, *parent;
170         struct io_piece *ipo, *__ipo;
171
172         ipo = malloc(sizeof(struct io_piece));
173         ipo->file = io_u->file;
174         ipo->offset = io_u->offset;
175         ipo->len = io_u->buflen;
176
177         /*
178          * 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          */
186         if (!td_random(td) || !td->o.overwrite) {
187                 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         /*
197          * Sort the entry into the verification list
198          */
199         while (*p) {
200                 parent = *p;
201
202                 __ipo = rb_entry(parent, struct io_piece, rb_node);
203                 if (ipo->offset <= __ipo->offset)
204                         p = &(*p)->rb_left;
205                 else
206                         p = &(*p)->rb_right;
207         }
208
209         rb_link_node(&ipo->rb_node, parent, p);
210         rb_insert_color(&ipo->rb_node, &td->io_hist_tree);
211 }
212
213 void write_iolog_close(struct thread_data *td)
214 {
215         fflush(td->iolog_f);
216         fclose(td->iolog_f);
217         free(td->iolog_buf);
218         td->iolog_f = NULL;
219         td->iolog_buf = NULL;
220 }
221
222 /*
223  * Read version 2 iolog data. It is enhanced to include per-file logging,
224  * syncs, etc.
225  */
226 static int read_iolog2(struct thread_data *td, FILE *f)
227 {
228         unsigned long long offset;
229         unsigned int bytes;
230         int reads, writes, fileno = 0, file_action = 0; /* stupid gcc */
231         char *fname, *act;
232         char *str, *p;
233         enum fio_ddir rw;
234
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
250                 r = sscanf(p, "%256s %256s %llu %u", fname, act, &offset,
251                                                                         &bytes);
252                 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 {
263                                 log_err("fio: bad iolog file action: %s\n",
264                                                                         act);
265                                 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 {
281                                 log_err("fio: bad iolog file action: %s\n",
282                                                                         act);
283                                 continue;
284                         }
285                 } else {
286                         log_err("bad iolog2: %s", p);
287                         continue;
288                 }
289
290                 if (rw == DDIR_READ)
291                         reads++;
292                 else if (rw == DDIR_WRITE) {
293                         /*
294                          * Don't add a write for ro mode
295                          */
296                         if (read_only)
297                                 continue;
298                         writes++;
299                 } else if (rw != DDIR_SYNC && rw != DDIR_INVAL) {
300                         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;
312                 ipo->ddir = rw;
313                 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                 }
319                 queue_io_piece(td, ipo);
320         }
321
322         free(str);
323         free(act);
324         free(fname);
325
326         if (writes && read_only) {
327                 log_err("fio: <%s> skips replay of %d writes due to"
328                         " read-only\n", td->o.name, writes);
329                 writes = 0;
330         }
331
332         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 /*
345  * open iolog, check version, and call appropriate parser
346  */
347 static 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 {
373                 log_err("fio: iolog version 1 is no longer supported\n");
374                 ret = 1;
375         }
376
377         fclose(f);
378         return ret;
379 }
380
381 /*
382  * Setup a log for storing io patterns.
383  */
384 static int init_iolog_write(struct thread_data *td)
385 {
386         struct fio_file *ff;
387         FILE *f;
388         unsigned int i;
389
390         f = fopen(td->o.write_iolog_file, "w+");
391         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);
402
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
417         return 0;
418 }
419
420 int init_iolog(struct thread_data *td)
421 {
422         int ret = 0;
423
424         if (td->io_ops->flags & FIO_DISKLESSIO)
425                 return 0;
426
427         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)
437                 ret = init_iolog_write(td);
438
439         return ret;
440 }
441
442 void 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
452 void __finish_log(struct io_log *log, const char *name)
453 {
454         unsigned int i;
455         FILE *f;
456
457         f = fopen(name, "w");
458         if (!f) {
459                 perror("fopen log");
460                 return;
461         }
462
463         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         }
467
468         fclose(f);
469         free(log->log);
470         free(log);
471 }
472
473 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
474 {
475         char file_name[256], *p;
476
477         snprintf(file_name, 200, "%s_%s.log", td->o.name, name);
478         p = basename(file_name);
479         __finish_log(log, p);
480 }