Commit | Line | Data |
---|---|---|
3c39a379 JA |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include "list.h" | |
4 | #include "fio.h" | |
5 | ||
6 | void write_iolog_put(struct thread_data *td, struct io_u *io_u) | |
7 | { | |
a4f4fdd7 | 8 | fprintf(td->iolog_f, "%u,%llu,%lu\n", io_u->ddir, io_u->offset, io_u->buflen); |
3c39a379 JA |
9 | } |
10 | ||
11 | int 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; | |
53cdc686 | 21 | io_u->file = ipo->file; |
3c39a379 JA |
22 | free(ipo); |
23 | return 0; | |
24 | } | |
25 | ||
26 | return 1; | |
27 | } | |
28 | ||
29 | void prune_io_piece_log(struct thread_data *td) | |
30 | { | |
31 | struct io_piece *ipo; | |
32 | ||
33 | while (!list_empty(&td->io_hist_list)) { | |
34 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
35 | ||
36 | list_del(&ipo->list); | |
37 | free(ipo); | |
38 | } | |
39 | } | |
40 | ||
41 | /* | |
34403fb1 | 42 | * log a successful write, so we can unwind the log for verify |
3c39a379 JA |
43 | */ |
44 | void log_io_piece(struct thread_data *td, struct io_u *io_u) | |
45 | { | |
46 | struct io_piece *ipo = malloc(sizeof(struct io_piece)); | |
47 | struct list_head *entry; | |
48 | ||
49 | INIT_LIST_HEAD(&ipo->list); | |
53cdc686 | 50 | ipo->file = io_u->file; |
3c39a379 JA |
51 | ipo->offset = io_u->offset; |
52 | ipo->len = io_u->buflen; | |
53 | ||
54 | /* | |
55 | * for random io where the writes extend the file, it will typically | |
56 | * be laid out with the block scattered as written. it's faster to | |
57 | * read them in in that order again, so don't sort | |
58 | */ | |
2dc1bbeb | 59 | if (!td_random(td) || !td->o.overwrite) { |
3c39a379 JA |
60 | list_add_tail(&ipo->list, &td->io_hist_list); |
61 | return; | |
62 | } | |
63 | ||
64 | /* | |
65 | * for random io, sort the list so verify will run faster | |
66 | */ | |
67 | entry = &td->io_hist_list; | |
68 | while ((entry = entry->prev) != &td->io_hist_list) { | |
69 | struct io_piece *__ipo = list_entry(entry, struct io_piece, list); | |
70 | ||
71 | if (__ipo->offset < ipo->offset) | |
72 | break; | |
73 | } | |
74 | ||
75 | list_add(&ipo->list, entry); | |
76 | } | |
77 | ||
78 | void write_iolog_close(struct thread_data *td) | |
79 | { | |
80 | fflush(td->iolog_f); | |
81 | fclose(td->iolog_f); | |
82 | free(td->iolog_buf); | |
83 | } | |
84 | ||
fb71fbd7 JA |
85 | /* |
86 | * Open a stored log and read in the entries. | |
87 | */ | |
88 | static int init_iolog_read(struct thread_data *td) | |
3c39a379 JA |
89 | { |
90 | unsigned long long offset; | |
91 | unsigned int bytes; | |
92 | char *str, *p; | |
93 | FILE *f; | |
fb71fbd7 | 94 | int rw, reads, writes; |
3c39a379 | 95 | |
2dc1bbeb | 96 | f = fopen(td->o.read_iolog_file, "r"); |
3c39a379 | 97 | if (!f) { |
fb71fbd7 | 98 | perror("fopen read iolog"); |
3c39a379 JA |
99 | return 1; |
100 | } | |
101 | ||
3c39a379 JA |
102 | /* |
103 | * Read in the read iolog and store it, reuse the infrastructure | |
104 | * for doing verifications. | |
105 | */ | |
106 | str = malloc(4096); | |
fb71fbd7 | 107 | reads = writes = 0; |
3c39a379 JA |
108 | while ((p = fgets(str, 4096, f)) != NULL) { |
109 | struct io_piece *ipo; | |
110 | ||
111 | if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) { | |
3b70d7e5 | 112 | log_err("bad iolog: %s\n", p); |
3c39a379 JA |
113 | continue; |
114 | } | |
115 | if (rw == DDIR_READ) | |
116 | reads++; | |
117 | else if (rw == DDIR_WRITE) | |
118 | writes++; | |
119 | else { | |
3b70d7e5 | 120 | log_err("bad ddir: %d\n", rw); |
3c39a379 JA |
121 | continue; |
122 | } | |
123 | ||
124 | ipo = malloc(sizeof(*ipo)); | |
125 | INIT_LIST_HEAD(&ipo->list); | |
126 | ipo->offset = offset; | |
127 | ipo->len = bytes; | |
1e97cce9 | 128 | ipo->ddir = (enum fio_ddir) rw; |
2dc1bbeb JA |
129 | if (bytes > td->o.max_bs[rw]) |
130 | td->o.max_bs[rw] = bytes; | |
3c39a379 | 131 | list_add_tail(&ipo->list, &td->io_log_list); |
3c39a379 JA |
132 | } |
133 | ||
134 | free(str); | |
135 | fclose(f); | |
136 | ||
fb71fbd7 | 137 | if (!reads && !writes) |
3c39a379 | 138 | return 1; |
fb71fbd7 | 139 | else if (reads && !writes) |
2dc1bbeb | 140 | td->o.td_ddir = TD_DDIR_READ; |
3c39a379 | 141 | else if (!reads && writes) |
2dc1bbeb | 142 | td->o.td_ddir = TD_DDIR_READ; |
3c39a379 | 143 | else |
2dc1bbeb | 144 | td->o.td_ddir = TD_DDIR_RW; |
3c39a379 JA |
145 | |
146 | return 0; | |
147 | } | |
8914a9d8 | 148 | |
fb71fbd7 JA |
149 | /* |
150 | * Setup a log for storing io patterns. | |
151 | */ | |
152 | static int init_iolog_write(struct thread_data *td) | |
153 | { | |
076efc7c | 154 | FILE *f; |
fb71fbd7 | 155 | |
2dc1bbeb | 156 | f = fopen(td->o.write_iolog_file, "w+"); |
fb71fbd7 JA |
157 | if (!f) { |
158 | perror("fopen write iolog"); | |
159 | return 1; | |
160 | } | |
161 | ||
162 | /* | |
163 | * That's it for writing, setup a log buffer and we're done. | |
164 | */ | |
165 | td->iolog_f = f; | |
166 | td->iolog_buf = malloc(8192); | |
167 | setvbuf(f, td->iolog_buf, _IOFBF, 8192); | |
168 | return 0; | |
169 | } | |
170 | ||
171 | int init_iolog(struct thread_data *td) | |
172 | { | |
b4a6a59a JA |
173 | int ret = 0; |
174 | ||
ba0fbe10 | 175 | if (td->io_ops->flags & FIO_DISKLESSIO) |
f48b467c JA |
176 | return 0; |
177 | ||
2dc1bbeb | 178 | if (td->o.read_iolog_file) |
b4a6a59a | 179 | ret = init_iolog_read(td); |
2dc1bbeb | 180 | else if (td->o.write_iolog_file) |
b4a6a59a | 181 | ret = init_iolog_write(td); |
fb71fbd7 | 182 | |
1e97cce9 | 183 | return ret; |
fb71fbd7 JA |
184 | } |
185 | ||
8914a9d8 JA |
186 | void setup_log(struct io_log **log) |
187 | { | |
188 | struct io_log *l = malloc(sizeof(*l)); | |
189 | ||
190 | l->nr_samples = 0; | |
191 | l->max_samples = 1024; | |
192 | l->log = malloc(l->max_samples * sizeof(struct io_sample)); | |
193 | *log = l; | |
194 | } | |
195 | ||
bb3884d8 | 196 | void __finish_log(struct io_log *log, const char *name) |
8914a9d8 | 197 | { |
8914a9d8 | 198 | unsigned int i; |
bb3884d8 | 199 | FILE *f; |
8914a9d8 | 200 | |
bb3884d8 | 201 | f = fopen(name, "w"); |
8914a9d8 JA |
202 | if (!f) { |
203 | perror("fopen log"); | |
204 | return; | |
205 | } | |
206 | ||
207 | for (i = 0; i < log->nr_samples; i++) | |
208 | fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir); | |
209 | ||
210 | fclose(f); | |
211 | free(log->log); | |
212 | free(log); | |
213 | } | |
bb3884d8 JA |
214 | |
215 | void finish_log(struct thread_data *td, struct io_log *log, const char *name) | |
216 | { | |
217 | char file_name[256]; | |
218 | ||
219 | snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name); | |
220 | __finish_log(log, file_name); | |
221 | } |