blob: a6d1f1d04c593c72c3f4d6f01bbb78b10b525f0e [file] [log] [blame]
Jens Axboe10ba5352006-10-20 11:39:27 +02001#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <signal.h>
5#include <time.h>
Jens Axboe0c6e7512007-02-22 11:19:39 +01006#include <assert.h>
Jens Axboe10ba5352006-10-20 11:39:27 +02007
8#include "fio.h"
Jens Axboe5973caf2008-05-21 19:52:35 +02009#include "hash.h"
Jens Axboe697a6062008-05-31 00:04:45 +020010#include "lib/ffz.h"
Jens Axboe10ba5352006-10-20 11:39:27 +020011
Jens Axboe5945b9b2007-02-22 20:33:01 +010012/*
13 * Change this define to play with the timeout handling
14 */
15#undef FIO_USE_TIMEOUT
16
Jens Axboe97601022007-02-18 12:47:29 +010017struct io_completion_data {
18 int nr; /* input */
Jens Axboe97601022007-02-18 12:47:29 +010019
20 int error; /* output */
21 unsigned long bytes_done[2]; /* output */
22 struct timeval time; /* output */
23};
24
Jens Axboe10ba5352006-10-20 11:39:27 +020025/*
26 * The ->file_map[] contains a map of blocks we have or have not done io
27 * to yet. Used to make sure we cover the entire range in a fair fashion.
28 */
Jens Axboeaec2de22008-04-24 12:44:42 +020029static int random_map_free(struct fio_file *f, const unsigned long long block)
Jens Axboe10ba5352006-10-20 11:39:27 +020030{
Jens Axboeaec2de22008-04-24 12:44:42 +020031 unsigned int idx = RAND_MAP_IDX(f, block);
32 unsigned int bit = RAND_MAP_BIT(f, block);
Jens Axboe10ba5352006-10-20 11:39:27 +020033
Jens Axboe84422ac2008-02-19 20:10:26 +010034 dprint(FD_RANDOM, "free: b=%llu, idx=%u, bit=%u\n", block, idx, bit);
35
Jens Axboede605662008-05-31 00:21:12 +020036 return (f->file_map[idx] & (1 << bit)) == 0;
Jens Axboe10ba5352006-10-20 11:39:27 +020037}
38
39/*
Jens Axboedf415582006-10-20 11:41:03 +020040 * Mark a given offset as used in the map.
41 */
Jens Axboe9bf20612007-03-01 09:33:57 +010042static void mark_random_map(struct thread_data *td, struct io_u *io_u)
Jens Axboedf415582006-10-20 11:41:03 +020043{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010044 unsigned int min_bs = td->o.rw_min_bs;
Jens Axboe9bf20612007-03-01 09:33:57 +010045 struct fio_file *f = io_u->file;
Jens Axboea00735e2006-11-03 08:58:08 +010046 unsigned long long block;
47 unsigned int blocks;
Jens Axboec685b5b2007-02-10 20:02:28 +010048 unsigned int nr_blocks;
Jens Axboedf415582006-10-20 11:41:03 +020049
Jens Axboeb9c5b642008-02-18 14:13:08 +010050 block = (io_u->offset - f->file_offset) / (unsigned long long) min_bs;
Jens Axboea00735e2006-11-03 08:58:08 +010051 blocks = 0;
Jens Axboec685b5b2007-02-10 20:02:28 +010052 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
53
54 while (blocks < nr_blocks) {
Jens Axboedf415582006-10-20 11:41:03 +020055 unsigned int idx, bit;
56
Jens Axboe1e3d53a2007-03-22 19:01:48 +010057 /*
58 * If we have a mixed random workload, we may
59 * encounter blocks we already did IO to.
60 */
Jens Axboeaec2de22008-04-24 12:44:42 +020061 if ((td->o.ddir_nr == 1) && !random_map_free(f, block))
Jens Axboedf415582006-10-20 11:41:03 +020062 break;
63
Jens Axboeaec2de22008-04-24 12:44:42 +020064 idx = RAND_MAP_IDX(f, block);
65 bit = RAND_MAP_BIT(f, block);
Jens Axboedf415582006-10-20 11:41:03 +020066
Jens Axboe0032bf92007-02-13 17:39:56 +010067 fio_assert(td, idx < f->num_maps);
Jens Axboedf415582006-10-20 11:41:03 +020068
Jens Axboede605662008-05-31 00:21:12 +020069 f->file_map[idx] |= (1 << bit);
Jens Axboedf415582006-10-20 11:41:03 +020070 block++;
71 blocks++;
72 }
73
Jens Axboea00735e2006-11-03 08:58:08 +010074 if ((blocks * min_bs) < io_u->buflen)
75 io_u->buflen = blocks * min_bs;
Jens Axboedf415582006-10-20 11:41:03 +020076}
77
Jens Axboe2ba1c292008-02-01 13:16:38 +010078static inline unsigned long long last_block(struct thread_data *td,
Jens Axboe4ba66132008-02-05 10:05:50 +010079 struct fio_file *f,
80 enum fio_ddir ddir)
Jens Axboe2ba1c292008-02-01 13:16:38 +010081{
82 unsigned long long max_blocks;
Jens Axboed9dd70f2008-05-23 12:37:23 +020083 unsigned long long max_size;
Jens Axboe2ba1c292008-02-01 13:16:38 +010084
Jens Axboed9dd70f2008-05-23 12:37:23 +020085 /*
86 * Hmm, should we make sure that ->io_size <= ->real_file_size?
87 */
88 max_size = f->io_size;
89 if (max_size > f->real_file_size)
90 max_size = f->real_file_size;
91
92 max_blocks = max_size / (unsigned long long) td->o.min_bs[ddir];
Jens Axboe2ba1c292008-02-01 13:16:38 +010093 if (!max_blocks)
94 return 0;
95
Jens Axboe67778e82008-05-15 09:20:08 +020096 return max_blocks;
Jens Axboe2ba1c292008-02-01 13:16:38 +010097}
98
Jens Axboedf415582006-10-20 11:41:03 +020099/*
Jens Axboe10ba5352006-10-20 11:39:27 +0200100 * Return the next free block in the map.
101 */
102static int get_next_free_block(struct thread_data *td, struct fio_file *f,
Jens Axboe4ba66132008-02-05 10:05:50 +0100103 enum fio_ddir ddir, unsigned long long *b)
Jens Axboe10ba5352006-10-20 11:39:27 +0200104{
Jens Axboe84422ac2008-02-19 20:10:26 +0100105 unsigned long long min_bs = td->o.rw_min_bs;
Jens Axboe10ba5352006-10-20 11:39:27 +0200106 int i;
107
Jens Axboec685b5b2007-02-10 20:02:28 +0100108 i = f->last_free_lookup;
109 *b = (i * BLOCKS_PER_MAP);
Jens Axboe84422ac2008-02-19 20:10:26 +0100110 while ((*b) * min_bs < f->real_file_size) {
Jens Axboede605662008-05-31 00:21:12 +0200111 if (f->file_map[i] != (unsigned int) -1) {
Jens Axboe697a6062008-05-31 00:04:45 +0200112 *b += ffz(f->file_map[i]);
Jens Axboe4ba66132008-02-05 10:05:50 +0100113 if (*b > last_block(td, f, ddir))
Jens Axboe2ba1c292008-02-01 13:16:38 +0100114 break;
Jens Axboec685b5b2007-02-10 20:02:28 +0100115 f->last_free_lookup = i;
Jens Axboe10ba5352006-10-20 11:39:27 +0200116 return 0;
117 }
118
119 *b += BLOCKS_PER_MAP;
120 i++;
121 }
122
Jens Axboe2ba1c292008-02-01 13:16:38 +0100123 dprint(FD_IO, "failed finding a free block\n");
Jens Axboe10ba5352006-10-20 11:39:27 +0200124 return 1;
125}
126
Jens Axboeec4015d2007-03-23 08:04:27 +0100127static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
Jens Axboe4ba66132008-02-05 10:05:50 +0100128 enum fio_ddir ddir, unsigned long long *b)
Jens Axboeec4015d2007-03-23 08:04:27 +0100129{
Jens Axboe84422ac2008-02-19 20:10:26 +0100130 unsigned long long r;
Jens Axboeec4015d2007-03-23 08:04:27 +0100131 int loops = 5;
132
133 do {
134 r = os_random_long(&td->random_state);
Jens Axboe84422ac2008-02-19 20:10:26 +0100135 dprint(FD_RANDOM, "off rand %llu\n", r);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100136 *b = (last_block(td, f, ddir) - 1)
137 * (r / ((unsigned long long) RAND_MAX + 1.0));
Jens Axboe2ba1c292008-02-01 13:16:38 +0100138
Jens Axboe43c63a72007-05-21 13:23:30 +0200139 /*
140 * if we are not maintaining a random map, we are done.
141 */
Jens Axboe303032a2008-03-26 10:11:10 +0100142 if (!file_randommap(td, f))
Jens Axboe43c63a72007-05-21 13:23:30 +0200143 return 0;
144
145 /*
Jens Axboe84422ac2008-02-19 20:10:26 +0100146 * calculate map offset and check if it's free
Jens Axboe43c63a72007-05-21 13:23:30 +0200147 */
Jens Axboeaec2de22008-04-24 12:44:42 +0200148 if (random_map_free(f, *b))
Jens Axboe43c63a72007-05-21 13:23:30 +0200149 return 0;
150
Jens Axboe84422ac2008-02-19 20:10:26 +0100151 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
152 *b);
Jens Axboe43c63a72007-05-21 13:23:30 +0200153 } while (--loops);
Jens Axboeec4015d2007-03-23 08:04:27 +0100154
155 /*
Jens Axboe43c63a72007-05-21 13:23:30 +0200156 * we get here, if we didn't suceed in looking up a block. generate
157 * a random start offset into the filemap, and find the first free
158 * block from there.
Jens Axboeec4015d2007-03-23 08:04:27 +0100159 */
Jens Axboe43c63a72007-05-21 13:23:30 +0200160 loops = 10;
161 do {
162 f->last_free_lookup = (f->num_maps - 1) * (r / (RAND_MAX+1.0));
Jens Axboe4ba66132008-02-05 10:05:50 +0100163 if (!get_next_free_block(td, f, ddir, b))
Jens Axboe43c63a72007-05-21 13:23:30 +0200164 return 0;
Jens Axboeec4015d2007-03-23 08:04:27 +0100165
Jens Axboe43c63a72007-05-21 13:23:30 +0200166 r = os_random_long(&td->random_state);
167 } while (--loops);
168
169 /*
170 * that didn't work either, try exhaustive search from the start
171 */
172 f->last_free_lookup = 0;
Jens Axboe4ba66132008-02-05 10:05:50 +0100173 return get_next_free_block(td, f, ddir, b);
Jens Axboeec4015d2007-03-23 08:04:27 +0100174}
175
Jens Axboe10ba5352006-10-20 11:39:27 +0200176/*
177 * For random io, generate a random new block and see if it's used. Repeat
178 * until we find a free one. For sequential io, just return the end of
179 * the last io issued.
180 */
Jens Axboe9bf20612007-03-01 09:33:57 +0100181static int get_next_offset(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200182{
Jens Axboe9bf20612007-03-01 09:33:57 +0100183 struct fio_file *f = io_u->file;
Jens Axboeec4015d2007-03-23 08:04:27 +0100184 unsigned long long b;
Jens Axboe4ba66132008-02-05 10:05:50 +0100185 enum fio_ddir ddir = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200186
Jens Axboeec4015d2007-03-23 08:04:27 +0100187 if (td_random(td) && (td->o.ddir_nr && !--td->ddir_nr)) {
188 td->ddir_nr = td->o.ddir_nr;
Jens Axboe10ba5352006-10-20 11:39:27 +0200189
Jens Axboe4ba66132008-02-05 10:05:50 +0100190 if (get_next_rand_offset(td, f, ddir, &b))
Jens Axboebca4ed42007-02-12 05:13:23 +0100191 return 1;
Jens Axboe43063a12007-03-27 20:21:24 +0200192 } else {
ljzhang,Yaxin Hu,Jianchao Tang0c3d7682007-07-26 10:59:25 +0200193 if (f->last_pos >= f->real_file_size) {
Jens Axboe4ba66132008-02-05 10:05:50 +0100194 if (!td_random(td) ||
195 get_next_rand_offset(td, f, ddir, &b))
ljzhang,Yaxin Hu,Jianchao Tang0c3d7682007-07-26 10:59:25 +0200196 return 1;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200197 } else
Jens Axboe4ba66132008-02-05 10:05:50 +0100198 b = (f->last_pos - f->file_offset) / td->o.min_bs[ddir];
Jens Axboe43063a12007-03-27 20:21:24 +0200199 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200200
Jens Axboe009bd842008-05-15 10:19:46 +0200201 io_u->offset = b * td->o.min_bs[ddir];
202 if (io_u->offset >= f->io_size) {
203 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
204 io_u->offset, f->io_size);
205 return 1;
206 }
207
208 io_u->offset += f->file_offset;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100209 if (io_u->offset >= f->real_file_size) {
210 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
211 io_u->offset, f->real_file_size);
Jens Axboe10ba5352006-10-20 11:39:27 +0200212 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100213 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200214
215 return 0;
216}
217
Jens Axboe9bf20612007-03-01 09:33:57 +0100218static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200219{
Jens Axboebca4ed42007-02-12 05:13:23 +0100220 const int ddir = io_u->ddir;
Jens Axboefc4398f2008-05-23 13:28:59 +0200221 unsigned int buflen = buflen; /* silence dumb gcc warning */
Jens Axboe10ba5352006-10-20 11:39:27 +0200222 long r;
223
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100224 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
225 buflen = td->o.min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200226 else {
227 r = os_random_long(&td->bsrange_state);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100228 if (!td->o.bssplit_nr) {
229 buflen = (unsigned int)
230 (1 + (double) (td->o.max_bs[ddir] - 1)
231 * r / (RAND_MAX + 1.0));
232 } else {
Jens Axboe564ca972007-12-14 12:21:19 +0100233 long perc = 0;
234 unsigned int i;
235
236 for (i = 0; i < td->o.bssplit_nr; i++) {
237 struct bssplit *bsp = &td->o.bssplit[i];
238
239 buflen = bsp->bs;
240 perc += bsp->perc;
241 if (r <= ((LONG_MAX / 100L) * perc))
242 break;
243 }
244 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100245 if (!td->o.bs_unaligned) {
246 buflen = (buflen + td->o.min_bs[ddir] - 1)
247 & ~(td->o.min_bs[ddir] - 1);
248 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200249 }
250
Jens Axboe4ba66132008-02-05 10:05:50 +0100251 if (io_u->offset + buflen > io_u->file->real_file_size) {
252 dprint(FD_IO, "lower buflen %u -> %u (ddir=%d)\n", buflen,
253 td->o.min_bs[ddir], ddir);
Jens Axboe6a5e6882007-07-26 10:47:51 +0200254 buflen = td->o.min_bs[ddir];
Jens Axboe4ba66132008-02-05 10:05:50 +0100255 }
Jens Axboe6a5e6882007-07-26 10:47:51 +0200256
Jens Axboe10ba5352006-10-20 11:39:27 +0200257 return buflen;
258}
259
Jens Axboeafe24a52007-03-16 20:27:27 +0100260static void set_rwmix_bytes(struct thread_data *td)
261{
Jens Axboeafe24a52007-03-16 20:27:27 +0100262 unsigned int diff;
263
264 /*
265 * we do time or byte based switch. this is needed because
266 * buffered writes may issue a lot quicker than they complete,
267 * whereas reads do not.
268 */
Jens Axboee47f7992007-03-21 14:05:39 +0100269 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
Jens Axboe04c540d2008-05-28 10:35:26 +0200270 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
Jens Axboee47f7992007-03-21 14:05:39 +0100271}
272
273static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
274{
275 unsigned int v;
276 long r;
277
278 r = os_random_long(&td->rwmix_state);
279 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
Jens Axboe04c540d2008-05-28 10:35:26 +0200280 if (v <= td->o.rwmix[DDIR_READ])
Jens Axboee47f7992007-03-21 14:05:39 +0100281 return DDIR_READ;
282
283 return DDIR_WRITE;
Jens Axboeafe24a52007-03-16 20:27:27 +0100284}
285
Jens Axboe10ba5352006-10-20 11:39:27 +0200286/*
287 * Return the data direction for the next io_u. If the job is a
288 * mixed read/write workload, check the rwmix cycle and switch if
289 * necessary.
290 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100291static enum fio_ddir get_rw_ddir(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200292{
293 if (td_rw(td)) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200294 /*
295 * Check if it's time to seed a new data direction.
296 */
Jens Axboee4928662008-04-07 09:19:46 +0200297 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
Jens Axboee47f7992007-03-21 14:05:39 +0100298 unsigned long long max_bytes;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100299 enum fio_ddir ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200300
Jens Axboee47f7992007-03-21 14:05:39 +0100301 /*
302 * Put a top limit on how many bytes we do for
303 * one data direction, to avoid overflowing the
304 * ranges too much
305 */
306 ddir = get_rand_ddir(td);
307 max_bytes = td->this_io_bytes[ddir];
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100308 if (max_bytes >=
309 (td->o.size * td->o.rwmix[ddir] / 100)) {
Jens Axboee4928662008-04-07 09:19:46 +0200310 if (!td->rw_end_set[ddir])
Jens Axboe38d77ca2007-03-21 14:20:20 +0100311 td->rw_end_set[ddir] = 1;
Jens Axboee4928662008-04-07 09:19:46 +0200312
Jens Axboee47f7992007-03-21 14:05:39 +0100313 ddir ^= 1;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100314 }
Jens Axboee47f7992007-03-21 14:05:39 +0100315
316 if (ddir != td->rwmix_ddir)
317 set_rwmix_bytes(td);
318
319 td->rwmix_ddir = ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200320 }
321 return td->rwmix_ddir;
322 } else if (td_read(td))
323 return DDIR_READ;
324 else
325 return DDIR_WRITE;
326}
327
Jens Axboe60f2c652008-05-16 12:31:36 +0200328static void put_file_log(struct thread_data *td, struct fio_file *f)
329{
330 int ret = put_file(td, f);
331
332 if (ret)
333 td_verror(td, ret, "file close");
334}
335
Jens Axboe10ba5352006-10-20 11:39:27 +0200336void put_io_u(struct thread_data *td, struct io_u *io_u)
337{
Jens Axboe0c6e7512007-02-22 11:19:39 +0100338 assert((io_u->flags & IO_U_F_FREE) == 0);
339 io_u->flags |= IO_U_F_FREE;
340
Jens Axboe60f2c652008-05-16 12:31:36 +0200341 if (io_u->file)
342 put_file_log(td, io_u->file);
Jens Axboe2dbdab72007-04-26 10:21:15 +0200343
Jens Axboe10ba5352006-10-20 11:39:27 +0200344 io_u->file = NULL;
345 list_del(&io_u->list);
346 list_add(&io_u->list, &td->io_u_freelist);
347 td->cur_depth--;
348}
349
Jens Axboe755200a2007-02-19 13:08:12 +0100350void requeue_io_u(struct thread_data *td, struct io_u **io_u)
351{
352 struct io_u *__io_u = *io_u;
353
Jens Axboe465221b2008-05-30 22:07:49 +0200354 dprint(FD_IO, "requeue %p\n", __io_u);
355
Jens Axboe4d2e0f42007-02-24 13:31:57 +0100356 __io_u->flags |= IO_U_F_FREE;
Jens Axboee4f54ad2008-02-04 10:56:07 +0100357 if ((__io_u->flags & IO_U_F_FLIGHT) && (__io_u->ddir != DDIR_SYNC))
358 td->io_issues[__io_u->ddir]--;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100359
Jens Axboe4d2e0f42007-02-24 13:31:57 +0100360 __io_u->flags &= ~IO_U_F_FLIGHT;
361
Jens Axboe755200a2007-02-19 13:08:12 +0100362 list_del(&__io_u->list);
363 list_add_tail(&__io_u->list, &td->io_u_requeues);
364 td->cur_depth--;
365 *io_u = NULL;
366}
367
Jens Axboe9bf20612007-03-01 09:33:57 +0100368static int fill_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200369{
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200370 if (td->io_ops->flags & FIO_NOIO)
371 goto out;
372
Jens Axboe10ba5352006-10-20 11:39:27 +0200373 /*
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200374 * see if it's time to sync
375 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100376 if (td->o.fsync_blocks &&
377 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
378 td->io_issues[DDIR_WRITE] && should_fsync(td)) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200379 io_u->ddir = DDIR_SYNC;
Jens Axboec38e9462007-03-27 08:48:48 +0200380 goto out;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200381 }
382
Jens Axboea00735e2006-11-03 08:58:08 +0100383 io_u->ddir = get_rw_ddir(td);
384
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200385 /*
Jens Axboe48f5abd2007-07-20 13:25:04 +0200386 * See if it's time to switch to a new zone
387 */
388 if (td->zone_bytes >= td->o.zone_size) {
389 td->zone_bytes = 0;
390 io_u->file->last_pos += td->o.zone_skip;
391 td->io_skip_bytes += td->o.zone_skip;
392 }
393
394 /*
Jens Axboec685b5b2007-02-10 20:02:28 +0100395 * No log, let the seq/rand engine retrieve the next buflen and
396 * position.
Jens Axboe10ba5352006-10-20 11:39:27 +0200397 */
Jens Axboe2ba1c292008-02-01 13:16:38 +0100398 if (get_next_offset(td, io_u)) {
399 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100400 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100401 }
Jens Axboec685b5b2007-02-10 20:02:28 +0100402
Jens Axboe9bf20612007-03-01 09:33:57 +0100403 io_u->buflen = get_next_buflen(td, io_u);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100404 if (!io_u->buflen) {
405 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100406 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100407 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200408
Jens Axboe2ba1c292008-02-01 13:16:38 +0100409 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
410 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
Jens Axboe4ba66132008-02-05 10:05:50 +0100411 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
412 io_u->buflen, io_u->file->real_file_size);
Jens Axboe6a5e6882007-07-26 10:47:51 +0200413 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100414 }
Jens Axboe6a5e6882007-07-26 10:47:51 +0200415
Jens Axboebca4ed42007-02-12 05:13:23 +0100416 /*
417 * mark entry before potentially trimming io_u
418 */
Jens Axboe303032a2008-03-26 10:11:10 +0100419 if (td_random(td) && file_randommap(td, io_u->file))
Jens Axboe9bf20612007-03-01 09:33:57 +0100420 mark_random_map(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200421
Jens Axboebca4ed42007-02-12 05:13:23 +0100422 /*
423 * If using a write iolog, store this entry.
424 */
Jens Axboec38e9462007-03-27 08:48:48 +0200425out:
Jens Axboe2ba1c292008-02-01 13:16:38 +0100426 dprint_io_u(io_u, "fill_io_u");
Jens Axboed9d91e32008-01-31 13:25:42 +0100427 td->zone_bytes += io_u->buflen;
Jens Axboef29b25a2007-07-23 08:56:43 +0200428 log_io_u(td, io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100429 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200430}
431
Jens Axboe838bc702008-05-22 13:08:23 +0200432static void __io_u_mark_map(unsigned int *map, unsigned int nr)
433{
434 int index = 0;
435
436 switch (nr) {
437 default:
438 index = 6;
439 break;
440 case 33 ... 64:
441 index = 5;
442 break;
443 case 17 ... 32:
444 index = 4;
445 break;
446 case 9 ... 16:
447 index = 3;
448 break;
449 case 5 ... 8:
450 index = 2;
451 break;
452 case 1 ... 4:
453 index = 1;
454 case 0:
455 break;
456 }
457
458 map[index]++;
459}
460
461void io_u_mark_submit(struct thread_data *td, unsigned int nr)
462{
463 __io_u_mark_map(td->ts.io_u_submit, nr);
464 td->ts.total_submit++;
465}
466
467void io_u_mark_complete(struct thread_data *td, unsigned int nr)
468{
469 __io_u_mark_map(td->ts.io_u_complete, nr);
470 td->ts.total_complete++;
471}
472
Jens Axboed8005752008-05-15 09:49:09 +0200473void io_u_mark_depth(struct thread_data *td, unsigned int nr)
Jens Axboe71619dc2007-01-13 23:56:33 +0100474{
475 int index = 0;
476
477 switch (td->cur_depth) {
478 default:
Jens Axboea783e612007-06-19 09:50:28 +0200479 index = 6;
480 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100481 case 32 ... 63:
Jens Axboea783e612007-06-19 09:50:28 +0200482 index = 5;
483 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100484 case 16 ... 31:
Jens Axboea783e612007-06-19 09:50:28 +0200485 index = 4;
486 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100487 case 8 ... 15:
Jens Axboea783e612007-06-19 09:50:28 +0200488 index = 3;
489 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100490 case 4 ... 7:
Jens Axboea783e612007-06-19 09:50:28 +0200491 index = 2;
492 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100493 case 2 ... 3:
Jens Axboea783e612007-06-19 09:50:28 +0200494 index = 1;
Jens Axboe71619dc2007-01-13 23:56:33 +0100495 case 1:
496 break;
497 }
498
Jens Axboe3bec7ae2008-05-14 21:08:37 +0200499 td->ts.io_u_map[index] += nr;
Jens Axboe71619dc2007-01-13 23:56:33 +0100500}
501
Jens Axboe04a0fea2007-06-19 12:48:41 +0200502static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
503{
504 int index = 0;
505
506 assert(usec < 1000);
507
508 switch (usec) {
509 case 750 ... 999:
510 index = 9;
511 break;
512 case 500 ... 749:
513 index = 8;
514 break;
515 case 250 ... 499:
516 index = 7;
517 break;
518 case 100 ... 249:
519 index = 6;
520 break;
521 case 50 ... 99:
522 index = 5;
523 break;
524 case 20 ... 49:
525 index = 4;
526 break;
527 case 10 ... 19:
528 index = 3;
529 break;
530 case 4 ... 9:
531 index = 2;
532 break;
533 case 2 ... 3:
534 index = 1;
535 case 0 ... 1:
536 break;
537 }
538
539 assert(index < FIO_IO_U_LAT_U_NR);
540 td->ts.io_u_lat_u[index]++;
541}
542
543static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
Jens Axboeec118302007-02-17 04:38:20 +0100544{
545 int index = 0;
546
547 switch (msec) {
548 default:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200549 index = 11;
550 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100551 case 1000 ... 1999:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200552 index = 10;
553 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100554 case 750 ... 999:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200555 index = 9;
556 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100557 case 500 ... 749:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200558 index = 8;
559 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100560 case 250 ... 499:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200561 index = 7;
562 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100563 case 100 ... 249:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200564 index = 6;
565 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100566 case 50 ... 99:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200567 index = 5;
568 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100569 case 20 ... 49:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200570 index = 4;
571 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100572 case 10 ... 19:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200573 index = 3;
574 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100575 case 4 ... 9:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200576 index = 2;
577 break;
Jens Axboeec118302007-02-17 04:38:20 +0100578 case 2 ... 3:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200579 index = 1;
Jens Axboeec118302007-02-17 04:38:20 +0100580 case 0 ... 1:
581 break;
582 }
583
Jens Axboe04a0fea2007-06-19 12:48:41 +0200584 assert(index < FIO_IO_U_LAT_M_NR);
585 td->ts.io_u_lat_m[index]++;
586}
587
588static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
589{
590 if (usec < 1000)
591 io_u_mark_lat_usec(td, usec);
592 else
593 io_u_mark_lat_msec(td, usec / 1000);
Jens Axboeec118302007-02-17 04:38:20 +0100594}
595
Jens Axboe0aabe162007-02-23 08:45:55 +0100596/*
597 * Get next file to service by choosing one at random
598 */
Jens Axboe1c178182007-03-13 13:25:18 +0100599static struct fio_file *get_next_file_rand(struct thread_data *td, int goodf,
600 int badf)
Jens Axboe0aabe162007-02-23 08:45:55 +0100601{
Jens Axboe0aabe162007-02-23 08:45:55 +0100602 struct fio_file *f;
Jens Axboe1c178182007-03-13 13:25:18 +0100603 int fno;
Jens Axboe0aabe162007-02-23 08:45:55 +0100604
605 do {
Jens Axboe7c83c082007-03-01 10:04:15 +0100606 long r = os_random_long(&td->next_file_state);
607
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100608 fno = (unsigned int) ((double) td->o.nr_files
609 * (r / (RAND_MAX + 1.0)));
Jens Axboe126d65c2008-03-01 18:04:31 +0100610 f = td->files[fno];
Jens Axboe059e63c2007-03-27 20:34:47 +0200611 if (f->flags & FIO_FILE_DONE)
612 continue;
Jens Axboe1c178182007-03-13 13:25:18 +0100613
Jens Axboe2ba1c292008-02-01 13:16:38 +0100614 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
615 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
Jens Axboe0aabe162007-02-23 08:45:55 +0100616 return f;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100617 }
Jens Axboe0aabe162007-02-23 08:45:55 +0100618 } while (1);
619}
620
621/*
622 * Get next file to service by doing round robin between all available ones
623 */
Jens Axboe1c178182007-03-13 13:25:18 +0100624static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
625 int badf)
Jens Axboe3d7c3912007-02-19 13:16:12 +0100626{
627 unsigned int old_next_file = td->next_file;
628 struct fio_file *f;
629
630 do {
Jens Axboe126d65c2008-03-01 18:04:31 +0100631 f = td->files[td->next_file];
Jens Axboe3d7c3912007-02-19 13:16:12 +0100632
633 td->next_file++;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100634 if (td->next_file >= td->o.nr_files)
Jens Axboe3d7c3912007-02-19 13:16:12 +0100635 td->next_file = 0;
636
Jens Axboed5ed68e2007-03-28 09:33:43 +0200637 if (f->flags & FIO_FILE_DONE) {
638 f = NULL;
Jens Axboe059e63c2007-03-27 20:34:47 +0200639 continue;
Jens Axboed5ed68e2007-03-28 09:33:43 +0200640 }
Jens Axboe059e63c2007-03-27 20:34:47 +0200641
Jens Axboe1c178182007-03-13 13:25:18 +0100642 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
Jens Axboe3d7c3912007-02-19 13:16:12 +0100643 break;
644
645 f = NULL;
646 } while (td->next_file != old_next_file);
647
Jens Axboe2ba1c292008-02-01 13:16:38 +0100648 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
Jens Axboe3d7c3912007-02-19 13:16:12 +0100649 return f;
650}
651
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100652static struct fio_file *get_next_file(struct thread_data *td)
653{
Jens Axboe1907dbc2007-03-12 11:44:28 +0100654 struct fio_file *f;
655
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100656 assert(td->o.nr_files <= td->files_index);
Jens Axboe1c178182007-03-13 13:25:18 +0100657
Jens Axboe2ba1c292008-02-01 13:16:38 +0100658 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100659 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
660 " nr_files=%d\n", td->nr_open_files,
661 td->nr_done_files,
662 td->o.nr_files);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100663 return NULL;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100664 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100665
Jens Axboe1907dbc2007-03-12 11:44:28 +0100666 f = td->file_service_file;
Jens Axboef11bd942007-03-13 10:16:34 +0100667 if (f && (f->flags & FIO_FILE_OPEN) && td->file_service_left--)
Jens Axboe2ba1c292008-02-01 13:16:38 +0100668 goto out;
Jens Axboe1907dbc2007-03-12 11:44:28 +0100669
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100670 if (td->o.file_service_type == FIO_FSERVICE_RR)
Jens Axboe1c178182007-03-13 13:25:18 +0100671 f = get_next_file_rr(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100672 else
Jens Axboe1c178182007-03-13 13:25:18 +0100673 f = get_next_file_rand(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
Jens Axboe1907dbc2007-03-12 11:44:28 +0100674
675 td->file_service_file = f;
676 td->file_service_left = td->file_service_nr - 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100677out:
678 dprint(FD_FILE, "get_next_file: %p\n", f);
Jens Axboe1907dbc2007-03-12 11:44:28 +0100679 return f;
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100680}
681
Jens Axboe1c178182007-03-13 13:25:18 +0100682static struct fio_file *find_next_new_file(struct thread_data *td)
683{
684 struct fio_file *f;
685
Jens Axboe1020a132007-03-29 11:15:06 +0200686 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files)
687 return NULL;
688
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100689 if (td->o.file_service_type == FIO_FSERVICE_RR)
Jens Axboe1c178182007-03-13 13:25:18 +0100690 f = get_next_file_rr(td, 0, FIO_FILE_OPEN);
691 else
692 f = get_next_file_rand(td, 0, FIO_FILE_OPEN);
693
694 return f;
695}
696
Jens Axboe429f6672007-07-23 10:38:43 +0200697static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
698{
699 struct fio_file *f;
700
701 do {
702 f = get_next_file(td);
703 if (!f)
704 return 1;
705
706set_file:
707 io_u->file = f;
708 get_file(f);
709
710 if (!fill_io_u(td, io_u))
711 break;
712
713 /*
Jens Axboe009bd842008-05-15 10:19:46 +0200714 * optimization to prevent close/open of the same file. This
715 * way we preserve queueing etc.
716 */
717 if (td->o.nr_files == 1 && td->o.time_based) {
Jens Axboe60f2c652008-05-16 12:31:36 +0200718 put_file_log(td, f);
Jens Axboe009bd842008-05-15 10:19:46 +0200719 fio_file_reset(f);
720 goto set_file;
721 }
722
723 /*
Jens Axboe429f6672007-07-23 10:38:43 +0200724 * td_io_close() does a put_file() as well, so no need to
725 * do that here.
726 */
727 io_u->file = NULL;
728 td_io_close_file(td, f);
729 f->flags |= FIO_FILE_DONE;
730 td->nr_done_files++;
731
732 /*
733 * probably not the right place to do this, but see
734 * if we need to open a new file
735 */
736 if (td->nr_open_files < td->o.open_files &&
737 td->o.open_files != td->o.nr_files) {
738 f = find_next_new_file(td);
739
740 if (!f || td_io_open_file(td, f))
741 return 1;
742
743 goto set_file;
744 }
745 } while (1);
746
747 return 0;
748}
749
750
Jens Axboe10ba5352006-10-20 11:39:27 +0200751struct io_u *__get_io_u(struct thread_data *td)
752{
753 struct io_u *io_u = NULL;
754
Jens Axboe755200a2007-02-19 13:08:12 +0100755 if (!list_empty(&td->io_u_requeues))
756 io_u = list_entry(td->io_u_requeues.next, struct io_u, list);
757 else if (!queue_full(td)) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200758 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
759
Jens Axboe6040dab2006-10-24 19:38:15 +0200760 io_u->buflen = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200761 io_u->resid = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100762 io_u->file = NULL;
Jens Axboed7762cf2007-02-23 12:34:57 +0100763 io_u->end_io = NULL;
Jens Axboe755200a2007-02-19 13:08:12 +0100764 }
765
766 if (io_u) {
Jens Axboe0c6e7512007-02-22 11:19:39 +0100767 assert(io_u->flags & IO_U_F_FREE);
768 io_u->flags &= ~IO_U_F_FREE;
769
Jens Axboe755200a2007-02-19 13:08:12 +0100770 io_u->error = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200771 list_del(&io_u->list);
772 list_add(&io_u->list, &td->io_u_busylist);
773 td->cur_depth++;
774 }
775
776 return io_u;
777}
778
779/*
780 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
781 * etc. The returned io_u is fully ready to be prepped and submitted.
782 */
Jens Axboe3d7c3912007-02-19 13:16:12 +0100783struct io_u *get_io_u(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200784{
Jens Axboe3d7c3912007-02-19 13:16:12 +0100785 struct fio_file *f;
Jens Axboe10ba5352006-10-20 11:39:27 +0200786 struct io_u *io_u;
787
788 io_u = __get_io_u(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100789 if (!io_u) {
790 dprint(FD_IO, "__get_io_u failed\n");
Jens Axboe10ba5352006-10-20 11:39:27 +0200791 return NULL;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100792 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200793
Jens Axboe755200a2007-02-19 13:08:12 +0100794 /*
795 * from a requeue, io_u already setup
796 */
797 if (io_u->file)
Jens Axboe77f392b2007-02-19 20:13:09 +0100798 goto out;
Jens Axboe755200a2007-02-19 13:08:12 +0100799
Jens Axboe429f6672007-07-23 10:38:43 +0200800 /*
801 * If using an iolog, grab next piece if any available.
802 */
803 if (td->o.read_iolog_file) {
804 if (read_iolog_get(td, io_u))
805 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100806 } else if (set_io_u_file(td, io_u)) {
807 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
Jens Axboe429f6672007-07-23 10:38:43 +0200808 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100809 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100810
Jens Axboe429f6672007-07-23 10:38:43 +0200811 f = io_u->file;
812 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe97af62c2007-05-22 11:12:13 +0200813
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200814 if (io_u->ddir != DDIR_SYNC) {
Jens Axboed0656a92008-02-01 18:33:23 +0100815 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
Jens Axboe2ba1c292008-02-01 13:16:38 +0100816 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
Jens Axboe429f6672007-07-23 10:38:43 +0200817 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100818 }
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200819
Jens Axboe36167d82007-02-18 05:41:31 +0100820 f->last_pos = io_u->offset + io_u->buflen;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200821
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100822 if (td->o.verify != VERIFY_NONE)
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200823 populate_verify_io_u(td, io_u);
Jens Axboee4dad9c2008-05-28 10:53:44 +0200824 else if (td->o.refill_buffers && io_u->ddir == DDIR_WRITE)
825 io_u_fill_buffer(td, io_u, io_u->xfer_buflen);
Jens Axboe10ba5352006-10-20 11:39:27 +0200826 }
827
Jens Axboe165faf12007-02-07 11:30:37 +0100828 /*
829 * Set io data pointers.
830 */
Jens Axboed460eb32007-04-16 21:39:33 +0200831 io_u->endpos = io_u->offset + io_u->buflen;
Jens Axboecec6b552007-02-06 20:15:38 +0100832 io_u->xfer_buf = io_u->buf;
833 io_u->xfer_buflen = io_u->buflen;
Jens Axboe5973caf2008-05-21 19:52:35 +0200834
Jens Axboe6ac7a332008-03-01 15:22:32 +0100835out:
Jens Axboe429f6672007-07-23 10:38:43 +0200836 if (!td_io_prep(td, io_u)) {
837 fio_gettime(&io_u->start_time, NULL);
838 return io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100839 }
Jens Axboe429f6672007-07-23 10:38:43 +0200840err_put:
Jens Axboe2ba1c292008-02-01 13:16:38 +0100841 dprint(FD_IO, "get_io_u failed\n");
Jens Axboe429f6672007-07-23 10:38:43 +0200842 put_io_u(td, io_u);
843 return NULL;
Jens Axboe10ba5352006-10-20 11:39:27 +0200844}
845
Jens Axboe54517922007-03-05 10:06:06 +0100846void io_u_log_error(struct thread_data *td, struct io_u *io_u)
847{
848 const char *msg[] = { "read", "write", "sync" };
849
850 log_err("fio: io_u error");
851
852 if (io_u->file)
853 log_err(" on file %s", io_u->file->file_name);
854
855 log_err(": %s\n", strerror(io_u->error));
856
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100857 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
858 io_u->offset, io_u->xfer_buflen);
Jens Axboe54517922007-03-05 10:06:06 +0100859
860 if (!td->error)
861 td_verror(td, io_u->error, "io_u error");
862}
863
Jens Axboe97601022007-02-18 12:47:29 +0100864static void io_completed(struct thread_data *td, struct io_u *io_u,
865 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200866{
Jens Axboed85f5112007-06-18 09:41:23 +0200867 unsigned long usec;
Jens Axboe10ba5352006-10-20 11:39:27 +0200868
Jens Axboe2ba1c292008-02-01 13:16:38 +0100869 dprint_io_u(io_u, "io complete");
870
Jens Axboe0c6e7512007-02-22 11:19:39 +0100871 assert(io_u->flags & IO_U_F_FLIGHT);
872 io_u->flags &= ~IO_U_F_FLIGHT;
873
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200874 if (io_u->ddir == DDIR_SYNC) {
875 td->last_was_sync = 1;
876 return;
877 }
878
879 td->last_was_sync = 0;
880
Jens Axboe10ba5352006-10-20 11:39:27 +0200881 if (!io_u->error) {
882 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100883 const enum fio_ddir idx = io_u->ddir;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100884 int ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200885
886 td->io_blocks[idx]++;
887 td->io_bytes[idx] += bytes;
Jens Axboe10ba5352006-10-20 11:39:27 +0200888 td->this_io_bytes[idx] += bytes;
889
Jens Axboed85f5112007-06-18 09:41:23 +0200890 usec = utime_since(&io_u->issue_time, &icd->time);
Jens Axboe10ba5352006-10-20 11:39:27 +0200891
Jens Axboed85f5112007-06-18 09:41:23 +0200892 add_clat_sample(td, idx, usec);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100893 add_bw_sample(td, idx, &icd->time);
Jens Axboe04a0fea2007-06-19 12:48:41 +0200894 io_u_mark_latency(td, usec);
Jens Axboe10ba5352006-10-20 11:39:27 +0200895
Jens Axboe660a1cb2007-04-17 15:37:47 +0200896 if (td_write(td) && idx == DDIR_WRITE &&
Shawn Lewis86705792007-11-21 09:35:41 +0100897 td->o.do_verify &&
Jens Axboe41128402007-03-27 08:32:48 +0200898 td->o.verify != VERIFY_NONE)
Jens Axboe10ba5352006-10-20 11:39:27 +0200899 log_io_piece(td, io_u);
900
901 icd->bytes_done[idx] += bytes;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100902
Jens Axboed7762cf2007-02-23 12:34:57 +0100903 if (io_u->end_io) {
Jens Axboe36690c92007-03-26 10:23:34 +0200904 ret = io_u->end_io(td, io_u);
Jens Axboe3af6ef32007-02-18 06:57:43 +0100905 if (ret && !icd->error)
906 icd->error = ret;
907 }
Jens Axboe54517922007-03-05 10:06:06 +0100908 } else {
Jens Axboe10ba5352006-10-20 11:39:27 +0200909 icd->error = io_u->error;
Jens Axboe54517922007-03-05 10:06:06 +0100910 io_u_log_error(td, io_u);
911 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200912}
913
Jens Axboed7762cf2007-02-23 12:34:57 +0100914static void init_icd(struct io_completion_data *icd, int nr)
Jens Axboe36167d82007-02-18 05:41:31 +0100915{
916 fio_gettime(&icd->time, NULL);
917
Jens Axboe3af6ef32007-02-18 06:57:43 +0100918 icd->nr = nr;
919
Jens Axboe36167d82007-02-18 05:41:31 +0100920 icd->error = 0;
921 icd->bytes_done[0] = icd->bytes_done[1] = 0;
922}
923
Jens Axboe97601022007-02-18 12:47:29 +0100924static void ios_completed(struct thread_data *td,
925 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200926{
927 struct io_u *io_u;
928 int i;
929
Jens Axboe10ba5352006-10-20 11:39:27 +0200930 for (i = 0; i < icd->nr; i++) {
931 io_u = td->io_ops->event(td, i);
932
933 io_completed(td, io_u, icd);
934 put_io_u(td, io_u);
935 }
936}
Jens Axboe97601022007-02-18 12:47:29 +0100937
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100938/*
939 * Complete a single io_u for the sync engines.
940 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100941long io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
Jens Axboe97601022007-02-18 12:47:29 +0100942{
943 struct io_completion_data icd;
944
Jens Axboed7762cf2007-02-23 12:34:57 +0100945 init_icd(&icd, 1);
Jens Axboe97601022007-02-18 12:47:29 +0100946 io_completed(td, io_u, &icd);
947 put_io_u(td, io_u);
948
949 if (!icd.error)
950 return icd.bytes_done[0] + icd.bytes_done[1];
951
Jens Axboe37e974a2007-03-02 15:16:45 +0100952 td_verror(td, icd.error, "io_u_sync_complete");
Jens Axboe97601022007-02-18 12:47:29 +0100953 return -1;
954}
955
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100956/*
957 * Called to complete min_events number of io for the async engines.
958 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100959long io_u_queued_complete(struct thread_data *td, int min_events)
Jens Axboe97601022007-02-18 12:47:29 +0100960{
Jens Axboe97601022007-02-18 12:47:29 +0100961 struct io_completion_data icd;
Jens Axboe00de55e2007-02-20 10:45:57 +0100962 struct timespec *tvp = NULL;
Jens Axboe97601022007-02-18 12:47:29 +0100963 int ret;
Davide Libenzi4d06a332007-03-22 07:43:50 +0100964 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
Jens Axboe97601022007-02-18 12:47:29 +0100965
Jens Axboeb271fe62008-02-04 10:49:41 +0100966 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_events);
967
Davide Libenzi4d06a332007-03-22 07:43:50 +0100968 if (!min_events)
Jens Axboe00de55e2007-02-20 10:45:57 +0100969 tvp = &ts;
Jens Axboe97601022007-02-18 12:47:29 +0100970
Jens Axboe00de55e2007-02-20 10:45:57 +0100971 ret = td_io_getevents(td, min_events, td->cur_depth, tvp);
Jens Axboe97601022007-02-18 12:47:29 +0100972 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100973 td_verror(td, -ret, "td_io_getevents");
Jens Axboe97601022007-02-18 12:47:29 +0100974 return ret;
975 } else if (!ret)
976 return ret;
977
Jens Axboed7762cf2007-02-23 12:34:57 +0100978 init_icd(&icd, ret);
Jens Axboe97601022007-02-18 12:47:29 +0100979 ios_completed(td, &icd);
980 if (!icd.error)
981 return icd.bytes_done[0] + icd.bytes_done[1];
982
Jens Axboe37e974a2007-03-02 15:16:45 +0100983 td_verror(td, icd.error, "io_u_queued_complete");
Jens Axboe97601022007-02-18 12:47:29 +0100984 return -1;
985}
Jens Axboe7e77dd02007-02-20 10:57:34 +0100986
987/*
988 * Call when io_u is really queued, to update the submission latency.
989 */
990void io_u_queued(struct thread_data *td, struct io_u *io_u)
991{
992 unsigned long slat_time;
993
Jens Axboed85f5112007-06-18 09:41:23 +0200994 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100995 add_slat_sample(td, io_u->ddir, slat_time);
996}
Jens Axboe433afcb2007-02-22 10:39:01 +0100997
Jens Axboe5973caf2008-05-21 19:52:35 +0200998/*
999 * "randomly" fill the buffer contents
1000 */
1001void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1002 unsigned int max_bs)
1003{
1004 long *ptr = io_u->buf;
1005
1006 if (!td->o.zero_buffers) {
1007 while ((void *) ptr - io_u->buf < max_bs) {
1008 *ptr = rand() * GOLDEN_RATIO_PRIME;
1009 ptr++;
1010 }
1011 } else
1012 memset(ptr, 0, max_bs);
1013}
1014
Jens Axboe55bc9722007-02-22 11:30:05 +01001015#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +01001016void io_u_set_timeout(struct thread_data *td)
1017{
1018 assert(td->cur_depth);
1019
1020 td->timer.it_interval.tv_sec = 0;
1021 td->timer.it_interval.tv_usec = 0;
1022 td->timer.it_value.tv_sec = IO_U_TIMEOUT + IO_U_TIMEOUT_INC;
1023 td->timer.it_value.tv_usec = 0;
1024 setitimer(ITIMER_REAL, &td->timer, NULL);
1025 fio_gettime(&td->timeout_end, NULL);
1026}
Jens Axboe5945b9b2007-02-22 20:33:01 +01001027
1028static void io_u_dump(struct io_u *io_u)
1029{
1030 unsigned long t_start = mtime_since_now(&io_u->start_time);
1031 unsigned long t_issue = mtime_since_now(&io_u->issue_time);
1032
1033 log_err("io_u=%p, t_start=%lu, t_issue=%lu\n", io_u, t_start, t_issue);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001034 log_err(" buf=%p/%p, len=%lu/%lu, offset=%llu\n", io_u->buf,
1035 io_u->xfer_buf, io_u->buflen,
1036 io_u->xfer_buflen,
1037 io_u->offset);
Jens Axboe5945b9b2007-02-22 20:33:01 +01001038 log_err(" ddir=%d, fname=%s\n", io_u->ddir, io_u->file->file_name);
1039}
Jens Axboe55bc9722007-02-22 11:30:05 +01001040#else
1041void io_u_set_timeout(struct thread_data fio_unused *td)
1042{
1043}
1044#endif
Jens Axboe433afcb2007-02-22 10:39:01 +01001045
Jens Axboe55bc9722007-02-22 11:30:05 +01001046#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +01001047static void io_u_timeout_handler(int fio_unused sig)
1048{
1049 struct thread_data *td, *__td;
1050 pid_t pid = getpid();
Jens Axboe5945b9b2007-02-22 20:33:01 +01001051 struct list_head *entry;
1052 struct io_u *io_u;
Jens Axboe433afcb2007-02-22 10:39:01 +01001053 int i;
1054
1055 log_err("fio: io_u timeout\n");
1056
1057 /*
1058 * TLS would be nice...
1059 */
1060 td = NULL;
1061 for_each_td(__td, i) {
1062 if (__td->pid == pid) {
1063 td = __td;
1064 break;
1065 }
1066 }
1067
1068 if (!td) {
1069 log_err("fio: io_u timeout, can't find job\n");
1070 exit(1);
1071 }
1072
1073 if (!td->cur_depth) {
1074 log_err("fio: timeout without pending work?\n");
1075 return;
1076 }
1077
Jens Axboe15506d02007-03-15 15:04:43 +01001078 log_err("fio: io_u timeout: job=%s, pid=%d\n", td->o.name, td->pid);
Jens Axboe5945b9b2007-02-22 20:33:01 +01001079
1080 list_for_each(entry, &td->io_u_busylist) {
1081 io_u = list_entry(entry, struct io_u, list);
1082
1083 io_u_dump(io_u);
1084 }
1085
1086 td_verror(td, ETIMEDOUT, "io_u timeout");
Jens Axboe433afcb2007-02-22 10:39:01 +01001087 exit(1);
1088}
Jens Axboe55bc9722007-02-22 11:30:05 +01001089#endif
Jens Axboe433afcb2007-02-22 10:39:01 +01001090
1091void io_u_init_timeout(void)
1092{
Jens Axboe55bc9722007-02-22 11:30:05 +01001093#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +01001094 signal(SIGALRM, io_u_timeout_handler);
Jens Axboe55bc9722007-02-22 11:30:05 +01001095#endif
Jens Axboe433afcb2007-02-22 10:39:01 +01001096}