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