[PATCH] Add fio_assert()
[fio.git] / io_u.c
CommitLineData
10ba535a
JA
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <signal.h>
5#include <time.h>
10ba535a
JA
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 */
14static 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
df415585
JA
23/*
24 * Mark a given offset as used in the map.
25 */
26static void mark_random_map(struct thread_data *td, struct fio_file *f,
27 struct io_u *io_u)
28{
afdbe580 29 unsigned int min_bs = td->rw_min_bs;
a00735e6
JA
30 unsigned long long block;
31 unsigned int blocks;
c685b5b2 32 unsigned int nr_blocks;
df415585 33
a00735e6
JA
34 block = io_u->offset / (unsigned long long) min_bs;
35 blocks = 0;
c685b5b2
JA
36 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
37
38 while (blocks < nr_blocks) {
df415585
JA
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
0032bf9f
JA
47 idx = f->num_maps;
48 fio_assert(td, idx < f->num_maps);
df415585
JA
49
50 f->file_map[idx] |= (1UL << bit);
51 block++;
52 blocks++;
53 }
54
a00735e6
JA
55 if ((blocks * min_bs) < io_u->buflen)
56 io_u->buflen = blocks * min_bs;
df415585
JA
57}
58
10ba535a
JA
59/*
60 * Return the next free block in the map.
61 */
62static int get_next_free_block(struct thread_data *td, struct fio_file *f,
63 unsigned long long *b)
64{
65 int i;
66
c685b5b2
JA
67 i = f->last_free_lookup;
68 *b = (i * BLOCKS_PER_MAP);
ee88470c 69 while ((*b) * td->rw_min_bs < f->real_file_size) {
10ba535a
JA
70 if (f->file_map[i] != -1UL) {
71 *b += ffz(f->file_map[i]);
c685b5b2 72 f->last_free_lookup = i;
10ba535a
JA
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 */
88static int get_next_offset(struct thread_data *td, struct fio_file *f,
c685b5b2 89 struct io_u *io_u)
10ba535a 90{
c685b5b2 91 const int ddir = io_u->ddir;
10ba535a
JA
92 unsigned long long b, rb;
93 long r;
94
95 if (!td->sequential) {
f697125a 96 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
c685b5b2 97 int loops = 5;
10ba535a
JA
98
99 do {
100 r = os_random_long(&td->random_state);
101 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
bb8895e0
JA
102 if (td->norandommap)
103 break;
a00735e6 104 rb = b + (f->file_offset / td->min_bs[ddir]);
10ba535a
JA
105 loops--;
106 } while (!random_map_free(td, f, rb) && loops);
107
bca4ed4d
JA
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;
10ba535a 114 } else
a00735e6 115 b = f->last_pos / td->min_bs[ddir];
10ba535a 116
c685b5b2 117 io_u->offset = (b * td->min_bs[ddir]) + f->file_offset;
bca4ed4d 118 if (io_u->offset >= f->real_file_size)
10ba535a
JA
119 return 1;
120
121 return 0;
122}
123
bca4ed4d
JA
124static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f,
125 struct io_u *io_u)
10ba535a 126{
bca4ed4d 127 const int ddir = io_u->ddir;
10ba535a
JA
128 unsigned int buflen;
129 long r;
130
a00735e6
JA
131 if (td->min_bs[ddir] == td->max_bs[ddir])
132 buflen = td->min_bs[ddir];
10ba535a
JA
133 else {
134 r = os_random_long(&td->bsrange_state);
1e97cce9 135 buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0));
690adba3 136 if (!td->bs_unaligned)
a00735e6 137 buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1);
10ba535a
JA
138 }
139
bca4ed4d 140 while (buflen + io_u->offset > f->real_file_size) {
bca4ed4d
JA
141 if (buflen == td->min_bs[ddir])
142 return 0;
143
144 buflen = td->min_bs[ddir];
10ba535a
JA
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 */
1e97cce9 155static enum fio_ddir get_rw_ddir(struct thread_data *td)
10ba535a
JA
156{
157 if (td_rw(td)) {
158 struct timeval now;
159 unsigned long elapsed;
160
02bcaa8c 161 fio_gettime(&now, NULL);
10ba535a
JA
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
10ba535a
JA
186void 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
194static 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
87dc1ab1
JA
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
a00735e6
JA
213 io_u->ddir = get_rw_ddir(td);
214
10ba535a 215 /*
c685b5b2
JA
216 * No log, let the seq/rand engine retrieve the next buflen and
217 * position.
10ba535a 218 */
bca4ed4d
JA
219 if (get_next_offset(td, f, io_u))
220 return 1;
10ba535a 221
bca4ed4d
JA
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;
10ba535a
JA
240}
241
71619dc2
JA
242static 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
10ba535a
JA
267struct 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
6040dabc 274 io_u->buflen = 0;
10ba535a
JA
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++;
71619dc2 280 io_u_mark_depth(td);
10ba535a
JA
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 */
290struct 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
ee88470c 308 if (io_u->buflen + io_u->offset > f->real_file_size) {
10ba535a
JA
309 if (td->io_ops->flags & FIO_RAWIO) {
310 put_io_u(td, io_u);
311 return NULL;
312 }
313
ee88470c 314 io_u->buflen = f->real_file_size - io_u->offset;
10ba535a
JA
315 }
316
87dc1ab1
JA
317 if (io_u->ddir != DDIR_SYNC) {
318 if (!io_u->buflen) {
319 put_io_u(td, io_u);
320 return NULL;
321 }
10ba535a 322
87dc1ab1 323 f->last_pos += io_u->buflen;
10ba535a 324
87dc1ab1
JA
325 if (td->verify != VERIFY_NONE)
326 populate_verify_io_u(td, io_u);
327 }
10ba535a
JA
328
329 if (td_io_prep(td, io_u)) {
330 put_io_u(td, io_u);
331 return NULL;
332 }
333
165faf16
JA
334 /*
335 * Set io data pointers.
336 */
cec6b55d
JA
337 io_u->xfer_buf = io_u->buf;
338 io_u->xfer_buflen = io_u->buflen;
165faf16 339
02bcaa8c 340 fio_gettime(&io_u->start_time, NULL);
10ba535a
JA
341 return io_u;
342}
343
344void io_completed(struct thread_data *td, struct io_u *io_u,
345 struct io_completion_data *icd)
346{
10ba535a
JA
347 unsigned long msec;
348
87dc1ab1
JA
349 if (io_u->ddir == DDIR_SYNC) {
350 td->last_was_sync = 1;
351 return;
352 }
353
354 td->last_was_sync = 0;
355
10ba535a
JA
356 if (!io_u->error) {
357 unsigned int bytes = io_u->buflen - io_u->resid;
1e97cce9 358 const enum fio_ddir idx = io_u->ddir;
10ba535a
JA
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
02bcaa8c
JA
365 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
366
367 msec = mtime_since(&io_u->issue_time, &icd->time);
10ba535a
JA
368
369 add_clat_sample(td, idx, msec);
02bcaa8c 370 add_bw_sample(td, idx, &icd->time);
10ba535a
JA
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
380void ios_completed(struct thread_data *td, struct io_completion_data *icd)
381{
382 struct io_u *io_u;
383 int i;
384
02bcaa8c
JA
385 fio_gettime(&icd->time, NULL);
386
10ba535a
JA
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}