[PATCH] Convert some options to use check_strstore() if they only dupe the arg
[fio.git] / log.c
CommitLineData
3c39a379
JA
1#include <stdio.h>
2#include <stdlib.h>
3#include "list.h"
4#include "fio.h"
5
6void write_iolog_put(struct thread_data *td, struct io_u *io_u)
7{
8 fprintf(td->iolog_f, "%d,%llu,%u\n", io_u->ddir, io_u->offset, io_u->buflen);
9}
10
11int read_iolog_get(struct thread_data *td, struct io_u *io_u)
12{
13 struct io_piece *ipo;
14
15 if (!list_empty(&td->io_log_list)) {
16 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
17 list_del(&ipo->list);
18 io_u->offset = ipo->offset;
19 io_u->buflen = ipo->len;
20 io_u->ddir = ipo->ddir;
21 free(ipo);
22 return 0;
23 }
24
25 return 1;
26}
27
28void prune_io_piece_log(struct thread_data *td)
29{
30 struct io_piece *ipo;
31
32 while (!list_empty(&td->io_hist_list)) {
33 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
34
35 list_del(&ipo->list);
36 free(ipo);
37 }
38}
39
40/*
41 * log a succesful write, so we can unwind the log for verify
42 */
43void log_io_piece(struct thread_data *td, struct io_u *io_u)
44{
45 struct io_piece *ipo = malloc(sizeof(struct io_piece));
46 struct list_head *entry;
47
48 INIT_LIST_HEAD(&ipo->list);
49 ipo->offset = io_u->offset;
50 ipo->len = io_u->buflen;
51
52 /*
53 * for random io where the writes extend the file, it will typically
54 * be laid out with the block scattered as written. it's faster to
55 * read them in in that order again, so don't sort
56 */
57 if (td->sequential || !td->overwrite) {
58 list_add_tail(&ipo->list, &td->io_hist_list);
59 return;
60 }
61
62 /*
63 * for random io, sort the list so verify will run faster
64 */
65 entry = &td->io_hist_list;
66 while ((entry = entry->prev) != &td->io_hist_list) {
67 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
68
69 if (__ipo->offset < ipo->offset)
70 break;
71 }
72
73 list_add(&ipo->list, entry);
74}
75
76void write_iolog_close(struct thread_data *td)
77{
78 fflush(td->iolog_f);
79 fclose(td->iolog_f);
80 free(td->iolog_buf);
81}
82
83int init_iolog(struct thread_data *td)
84{
85 unsigned long long offset;
86 unsigned int bytes;
87 char *str, *p;
88 FILE *f;
89 int rw, i, reads, writes;
90
91 if (!td->read_iolog && !td->write_iolog)
92 return 0;
93
94 if (td->read_iolog)
95 f = fopen(td->iolog_file, "r");
96 else
97 f = fopen(td->iolog_file, "w");
98
99 if (!f) {
100 perror("fopen iolog");
101 printf("file %s, %d/%d\n", td->iolog_file, td->read_iolog, td->write_iolog);
102 return 1;
103 }
104
105 /*
106 * That's it for writing, setup a log buffer and we're done.
107 */
108 if (td->write_iolog) {
109 td->iolog_f = f;
110 td->iolog_buf = malloc(8192);
111 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
112 return 0;
113 }
114
115 /*
116 * Read in the read iolog and store it, reuse the infrastructure
117 * for doing verifications.
118 */
119 str = malloc(4096);
120 reads = writes = i = 0;
121 while ((p = fgets(str, 4096, f)) != NULL) {
122 struct io_piece *ipo;
123
124 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
125 fprintf(stderr, "bad iolog: %s\n", p);
126 continue;
127 }
128 if (rw == DDIR_READ)
129 reads++;
130 else if (rw == DDIR_WRITE)
131 writes++;
132 else {
133 fprintf(stderr, "bad ddir: %d\n", rw);
134 continue;
135 }
136
137 ipo = malloc(sizeof(*ipo));
138 INIT_LIST_HEAD(&ipo->list);
139 ipo->offset = offset;
140 ipo->len = bytes;
141 if (bytes > td->max_bs)
142 td->max_bs = bytes;
143 ipo->ddir = rw;
144 list_add_tail(&ipo->list, &td->io_log_list);
145 i++;
146 }
147
148 free(str);
149 fclose(f);
150
151 if (!i)
152 return 1;
153
154 if (reads && !writes)
155 td->ddir = DDIR_READ;
156 else if (!reads && writes)
157 td->ddir = DDIR_READ;
158 else
159 td->iomix = 1;
160
161 return 0;
162}