[PATCH] Split the io_u io handling out of fio.c
[fio.git] / io_u.c
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <time.h>
6 #include <assert.h>
7
8 #include "fio.h"
9 #include "os.h"
10
11 /*
12  * The ->file_map[] contains a map of blocks we have or have not done io
13  * to yet. Used to make sure we cover the entire range in a fair fashion.
14  */
15 static int random_map_free(struct thread_data *td, struct fio_file *f,
16                            unsigned long long block)
17 {
18         unsigned int idx = RAND_MAP_IDX(td, f, block);
19         unsigned int bit = RAND_MAP_BIT(td, f, block);
20
21         return (f->file_map[idx] & (1UL << bit)) == 0;
22 }
23
24 /*
25  * Return the next free block in the map.
26  */
27 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
28                                unsigned long long *b)
29 {
30         int i;
31
32         *b = 0;
33         i = 0;
34         while ((*b) * td->min_bs < f->file_size) {
35                 if (f->file_map[i] != -1UL) {
36                         *b += ffz(f->file_map[i]);
37                         return 0;
38                 }
39
40                 *b += BLOCKS_PER_MAP;
41                 i++;
42         }
43
44         return 1;
45 }
46
47 /*
48  * For random io, generate a random new block and see if it's used. Repeat
49  * until we find a free one. For sequential io, just return the end of
50  * the last io issued.
51  */
52 static int get_next_offset(struct thread_data *td, struct fio_file *f,
53                            unsigned long long *offset)
54 {
55         unsigned long long b, rb;
56         long r;
57
58         if (!td->sequential) {
59                 unsigned long long max_blocks = td->io_size / td->min_bs;
60                 int loops = 50;
61
62                 do {
63                         r = os_random_long(&td->random_state);
64                         b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
65                         rb = b + (f->file_offset / td->min_bs);
66                         loops--;
67                 } while (!random_map_free(td, f, rb) && loops);
68
69                 if (!loops) {
70                         if (get_next_free_block(td, f, &b))
71                                 return 1;
72                 }
73         } else
74                 b = f->last_pos / td->min_bs;
75
76         *offset = (b * td->min_bs) + f->file_offset;
77         if (*offset > f->file_size)
78                 return 1;
79
80         return 0;
81 }
82
83 static unsigned int get_next_buflen(struct thread_data *td)
84 {
85         unsigned int buflen;
86         long r;
87
88         if (td->min_bs == td->max_bs)
89                 buflen = td->min_bs;
90         else {
91                 r = os_random_long(&td->bsrange_state);
92                 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
93                 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
94         }
95
96         if (buflen > td->io_size - td->this_io_bytes[td->ddir]) {
97                 /*
98                  * if using direct/raw io, we may not be able to
99                  * shrink the size. so just fail it.
100                  */
101                 if (td->io_ops->flags & FIO_RAWIO)
102                         return 0;
103
104                 buflen = td->io_size - td->this_io_bytes[td->ddir];
105         }
106
107         return buflen;
108 }
109
110 /*
111  * Return the data direction for the next io_u. If the job is a
112  * mixed read/write workload, check the rwmix cycle and switch if
113  * necessary.
114  */
115 static int get_rw_ddir(struct thread_data *td)
116 {
117         if (td_rw(td)) {
118                 struct timeval now;
119                 unsigned long elapsed;
120
121                 gettimeofday(&now, NULL);
122                 elapsed = mtime_since_now(&td->rwmix_switch);
123
124                 /*
125                  * Check if it's time to seed a new data direction.
126                  */
127                 if (elapsed >= td->rwmixcycle) {
128                         unsigned int v;
129                         long r;
130
131                         r = os_random_long(&td->rwmix_state);
132                         v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
133                         if (v < td->rwmixread)
134                                 td->rwmix_ddir = DDIR_READ;
135                         else
136                                 td->rwmix_ddir = DDIR_WRITE;
137                         memcpy(&td->rwmix_switch, &now, sizeof(now));
138                 }
139                 return td->rwmix_ddir;
140         } else if (td_read(td))
141                 return DDIR_READ;
142         else
143                 return DDIR_WRITE;
144 }
145
146 /*
147  * Mark a given offset as used in the map.
148  */
149 static void mark_random_map(struct thread_data *td, struct fio_file *f,
150                             struct io_u *io_u)
151 {
152         unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
153         unsigned int blocks = 0;
154
155         while (blocks < (io_u->buflen / td->min_bs)) {
156                 unsigned int idx, bit;
157
158                 if (!random_map_free(td, f, block))
159                         break;
160
161                 idx = RAND_MAP_IDX(td, f, block);
162                 bit = RAND_MAP_BIT(td, f, block);
163
164                 assert(idx < f->num_maps);
165
166                 f->file_map[idx] |= (1UL << bit);
167                 block++;
168                 blocks++;
169         }
170
171         if ((blocks * td->min_bs) < io_u->buflen)
172                 io_u->buflen = blocks * td->min_bs;
173 }
174
175 void put_io_u(struct thread_data *td, struct io_u *io_u)
176 {
177         io_u->file = NULL;
178         list_del(&io_u->list);
179         list_add(&io_u->list, &td->io_u_freelist);
180         td->cur_depth--;
181 }
182
183 static int fill_io_u(struct thread_data *td, struct fio_file *f,
184                      struct io_u *io_u)
185 {
186         /*
187          * If using an iolog, grab next piece if any available.
188          */
189         if (td->read_iolog)
190                 return read_iolog_get(td, io_u);
191
192         /*
193          * No log, let the seq/rand engine retrieve the next position.
194          */
195         if (!get_next_offset(td, f, &io_u->offset)) {
196                 io_u->buflen = get_next_buflen(td);
197
198                 if (io_u->buflen) {
199                         io_u->ddir = get_rw_ddir(td);
200
201                         /*
202                          * If using a write iolog, store this entry.
203                          */
204                         if (td->write_iolog)
205                                 write_iolog_put(td, io_u);
206
207                         io_u->file = f;
208                         return 0;
209                 }
210         }
211
212         return 1;
213 }
214
215 struct io_u *__get_io_u(struct thread_data *td)
216 {
217         struct io_u *io_u = NULL;
218
219         if (!queue_full(td)) {
220                 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
221
222                 io_u->error = 0;
223                 io_u->resid = 0;
224                 list_del(&io_u->list);
225                 list_add(&io_u->list, &td->io_u_busylist);
226                 td->cur_depth++;
227         }
228
229         return io_u;
230 }
231
232 /*
233  * Return an io_u to be processed. Gets a buflen and offset, sets direction,
234  * etc. The returned io_u is fully ready to be prepped and submitted.
235  */
236 struct io_u *get_io_u(struct thread_data *td, struct fio_file *f)
237 {
238         struct io_u *io_u;
239
240         io_u = __get_io_u(td);
241         if (!io_u)
242                 return NULL;
243
244         if (td->zone_bytes >= td->zone_size) {
245                 td->zone_bytes = 0;
246                 f->last_pos += td->zone_skip;
247         }
248
249         if (fill_io_u(td, f, io_u)) {
250                 put_io_u(td, io_u);
251                 return NULL;
252         }
253
254         if (io_u->buflen + io_u->offset > f->file_size) {
255                 if (td->io_ops->flags & FIO_RAWIO) {
256                         put_io_u(td, io_u);
257                         return NULL;
258                 }
259
260                 io_u->buflen = f->file_size - io_u->offset;
261         }
262
263         if (!io_u->buflen) {
264                 put_io_u(td, io_u);
265                 return NULL;
266         }
267
268         if (!td->read_iolog && !td->sequential)
269                 mark_random_map(td, f, io_u);
270
271         f->last_pos += io_u->buflen;
272
273         if (td->verify != VERIFY_NONE)
274                 populate_verify_io_u(td, io_u);
275
276         if (td_io_prep(td, io_u)) {
277                 put_io_u(td, io_u);
278                 return NULL;
279         }
280
281         gettimeofday(&io_u->start_time, NULL);
282         return io_u;
283 }
284
285 void io_completed(struct thread_data *td, struct io_u *io_u,
286                   struct io_completion_data *icd)
287 {
288         struct timeval e;
289         unsigned long msec;
290
291         gettimeofday(&e, NULL);
292
293         if (!io_u->error) {
294                 unsigned int bytes = io_u->buflen - io_u->resid;
295                 const int idx = io_u->ddir;
296
297                 td->io_blocks[idx]++;
298                 td->io_bytes[idx] += bytes;
299                 td->zone_bytes += bytes;
300                 td->this_io_bytes[idx] += bytes;
301
302                 msec = mtime_since(&io_u->issue_time, &e);
303
304                 add_clat_sample(td, idx, msec);
305                 add_bw_sample(td, idx);
306
307                 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
308                         log_io_piece(td, io_u);
309
310                 icd->bytes_done[idx] += bytes;
311         } else
312                 icd->error = io_u->error;
313 }
314
315 void ios_completed(struct thread_data *td, struct io_completion_data *icd)
316 {
317         struct io_u *io_u;
318         int i;
319
320         icd->error = 0;
321         icd->bytes_done[0] = icd->bytes_done[1] = 0;
322
323         for (i = 0; i < icd->nr; i++) {
324                 io_u = td->io_ops->event(td, i);
325
326                 io_completed(td, io_u, icd);
327                 put_io_u(td, io_u);
328         }
329 }
330
331