blob: c33dddf66f7c1337ed9157ffe5ad52628703945d [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 Axboe10ba5352006-10-20 11:39:27 +020010
Jens Axboe5945b9b2007-02-22 20:33:01 +010011/*
12 * Change this define to play with the timeout handling
13 */
14#undef FIO_USE_TIMEOUT
15
Jens Axboe97601022007-02-18 12:47:29 +010016struct io_completion_data {
17 int nr; /* input */
Jens Axboe97601022007-02-18 12:47:29 +010018
19 int error; /* output */
20 unsigned long bytes_done[2]; /* output */
21 struct timeval time; /* output */
22};
23
Jens Axboe10ba5352006-10-20 11:39:27 +020024/*
25 * The ->file_map[] contains a map of blocks we have or have not done io
26 * to yet. Used to make sure we cover the entire range in a fair fashion.
27 */
Jens Axboeaec2de22008-04-24 12:44:42 +020028static int random_map_free(struct fio_file *f, const unsigned long long block)
Jens Axboe10ba5352006-10-20 11:39:27 +020029{
Jens Axboeaec2de22008-04-24 12:44:42 +020030 unsigned int idx = RAND_MAP_IDX(f, block);
31 unsigned int bit = RAND_MAP_BIT(f, block);
Jens Axboe10ba5352006-10-20 11:39:27 +020032
Jens Axboe84422ac2008-02-19 20:10:26 +010033 dprint(FD_RANDOM, "free: b=%llu, idx=%u, bit=%u\n", block, idx, bit);
34
Jens Axboede605662008-05-31 00:21:12 +020035 return (f->file_map[idx] & (1 << bit)) == 0;
Jens Axboe10ba5352006-10-20 11:39:27 +020036}
37
38/*
Jens Axboedf415582006-10-20 11:41:03 +020039 * Mark a given offset as used in the map.
40 */
Jens Axboe9bf20612007-03-01 09:33:57 +010041static void mark_random_map(struct thread_data *td, struct io_u *io_u)
Jens Axboedf415582006-10-20 11:41:03 +020042{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010043 unsigned int min_bs = td->o.rw_min_bs;
Jens Axboe9bf20612007-03-01 09:33:57 +010044 struct fio_file *f = io_u->file;
Jens Axboea00735e2006-11-03 08:58:08 +010045 unsigned long long block;
Jens Axboe3e3357b2008-06-02 09:53:05 +020046 unsigned int blocks, nr_blocks;
Jens Axboedf415582006-10-20 11:41:03 +020047
Jens Axboeb9c5b642008-02-18 14:13:08 +010048 block = (io_u->offset - f->file_offset) / (unsigned long long) min_bs;
Jens Axboec685b5b2007-02-10 20:02:28 +010049 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
Jens Axboe3e3357b2008-06-02 09:53:05 +020050 blocks = 0;
Jens Axboec685b5b2007-02-10 20:02:28 +010051
Jens Axboe3e3357b2008-06-02 09:53:05 +020052 while (nr_blocks) {
53 unsigned int this_blocks, mask;
Jens Axboedf415582006-10-20 11:41:03 +020054 unsigned int idx, bit;
55
Jens Axboe1e3d53a2007-03-22 19:01:48 +010056 /*
57 * If we have a mixed random workload, we may
58 * encounter blocks we already did IO to.
59 */
Jens Axboeaec2de22008-04-24 12:44:42 +020060 if ((td->o.ddir_nr == 1) && !random_map_free(f, block))
Jens Axboedf415582006-10-20 11:41:03 +020061 break;
62
Jens Axboeaec2de22008-04-24 12:44:42 +020063 idx = RAND_MAP_IDX(f, block);
64 bit = RAND_MAP_BIT(f, block);
Jens Axboedf415582006-10-20 11:41:03 +020065
Jens Axboe0032bf92007-02-13 17:39:56 +010066 fio_assert(td, idx < f->num_maps);
Jens Axboedf415582006-10-20 11:41:03 +020067
Jens Axboe3e3357b2008-06-02 09:53:05 +020068 this_blocks = nr_blocks;
69 if (this_blocks + bit > BLOCKS_PER_MAP)
70 this_blocks = BLOCKS_PER_MAP - bit;
71
72 if (this_blocks == BLOCKS_PER_MAP)
73 mask = -1U;
74 else
75 mask = ((1U << this_blocks) - 1) << bit;
76
77 fio_assert(td, !(f->file_map[idx] & mask));
78 f->file_map[idx] |= mask;
79 nr_blocks -= this_blocks;
80 blocks += this_blocks;
Jens Axboec9dd34b2008-06-02 10:09:43 +020081 block += this_blocks;
Jens Axboedf415582006-10-20 11:41:03 +020082 }
83
Jens Axboea00735e2006-11-03 08:58:08 +010084 if ((blocks * min_bs) < io_u->buflen)
85 io_u->buflen = blocks * min_bs;
Jens Axboedf415582006-10-20 11:41:03 +020086}
87
Jens Axboe3e3357b2008-06-02 09:53:05 +020088static unsigned long long last_block(struct thread_data *td, struct fio_file *f,
89 enum fio_ddir ddir)
Jens Axboe2ba1c292008-02-01 13:16:38 +010090{
91 unsigned long long max_blocks;
Jens Axboed9dd70f2008-05-23 12:37:23 +020092 unsigned long long max_size;
Jens Axboe2ba1c292008-02-01 13:16:38 +010093
Jens Axboed9dd70f2008-05-23 12:37:23 +020094 /*
95 * Hmm, should we make sure that ->io_size <= ->real_file_size?
96 */
97 max_size = f->io_size;
98 if (max_size > f->real_file_size)
99 max_size = f->real_file_size;
100
101 max_blocks = max_size / (unsigned long long) td->o.min_bs[ddir];
Jens Axboe2ba1c292008-02-01 13:16:38 +0100102 if (!max_blocks)
103 return 0;
104
Jens Axboe67778e82008-05-15 09:20:08 +0200105 return max_blocks;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100106}
107
Jens Axboedf415582006-10-20 11:41:03 +0200108/*
Jens Axboe10ba5352006-10-20 11:39:27 +0200109 * Return the next free block in the map.
110 */
111static int get_next_free_block(struct thread_data *td, struct fio_file *f,
Jens Axboe4ba66132008-02-05 10:05:50 +0100112 enum fio_ddir ddir, unsigned long long *b)
Jens Axboe10ba5352006-10-20 11:39:27 +0200113{
Jens Axboe84422ac2008-02-19 20:10:26 +0100114 unsigned long long min_bs = td->o.rw_min_bs;
Jens Axboe10ba5352006-10-20 11:39:27 +0200115 int i;
116
Jens Axboec685b5b2007-02-10 20:02:28 +0100117 i = f->last_free_lookup;
118 *b = (i * BLOCKS_PER_MAP);
Jens Axboe84422ac2008-02-19 20:10:26 +0100119 while ((*b) * min_bs < f->real_file_size) {
Jens Axboede605662008-05-31 00:21:12 +0200120 if (f->file_map[i] != (unsigned int) -1) {
Jens Axboe697a6062008-05-31 00:04:45 +0200121 *b += ffz(f->file_map[i]);
Jens Axboe4ba66132008-02-05 10:05:50 +0100122 if (*b > last_block(td, f, ddir))
Jens Axboe2ba1c292008-02-01 13:16:38 +0100123 break;
Jens Axboec685b5b2007-02-10 20:02:28 +0100124 f->last_free_lookup = i;
Jens Axboe10ba5352006-10-20 11:39:27 +0200125 return 0;
126 }
127
128 *b += BLOCKS_PER_MAP;
129 i++;
130 }
131
Jens Axboe2ba1c292008-02-01 13:16:38 +0100132 dprint(FD_IO, "failed finding a free block\n");
Jens Axboe10ba5352006-10-20 11:39:27 +0200133 return 1;
134}
135
Jens Axboeec4015d2007-03-23 08:04:27 +0100136static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
Jens Axboe4ba66132008-02-05 10:05:50 +0100137 enum fio_ddir ddir, unsigned long long *b)
Jens Axboeec4015d2007-03-23 08:04:27 +0100138{
Jens Axboe84422ac2008-02-19 20:10:26 +0100139 unsigned long long r;
Jens Axboeec4015d2007-03-23 08:04:27 +0100140 int loops = 5;
141
142 do {
143 r = os_random_long(&td->random_state);
Jens Axboe84422ac2008-02-19 20:10:26 +0100144 dprint(FD_RANDOM, "off rand %llu\n", r);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100145 *b = (last_block(td, f, ddir) - 1)
146 * (r / ((unsigned long long) RAND_MAX + 1.0));
Jens Axboe2ba1c292008-02-01 13:16:38 +0100147
Jens Axboe43c63a72007-05-21 13:23:30 +0200148 /*
149 * if we are not maintaining a random map, we are done.
150 */
Jens Axboe303032a2008-03-26 10:11:10 +0100151 if (!file_randommap(td, f))
Jens Axboe43c63a72007-05-21 13:23:30 +0200152 return 0;
153
154 /*
Jens Axboe84422ac2008-02-19 20:10:26 +0100155 * calculate map offset and check if it's free
Jens Axboe43c63a72007-05-21 13:23:30 +0200156 */
Jens Axboeaec2de22008-04-24 12:44:42 +0200157 if (random_map_free(f, *b))
Jens Axboe43c63a72007-05-21 13:23:30 +0200158 return 0;
159
Jens Axboe84422ac2008-02-19 20:10:26 +0100160 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
161 *b);
Jens Axboe43c63a72007-05-21 13:23:30 +0200162 } while (--loops);
Jens Axboeec4015d2007-03-23 08:04:27 +0100163
164 /*
Jens Axboe43c63a72007-05-21 13:23:30 +0200165 * we get here, if we didn't suceed in looking up a block. generate
166 * a random start offset into the filemap, and find the first free
167 * block from there.
Jens Axboeec4015d2007-03-23 08:04:27 +0100168 */
Jens Axboe43c63a72007-05-21 13:23:30 +0200169 loops = 10;
170 do {
171 f->last_free_lookup = (f->num_maps - 1) * (r / (RAND_MAX+1.0));
Jens Axboe4ba66132008-02-05 10:05:50 +0100172 if (!get_next_free_block(td, f, ddir, b))
Jens Axboe43c63a72007-05-21 13:23:30 +0200173 return 0;
Jens Axboeec4015d2007-03-23 08:04:27 +0100174
Jens Axboe43c63a72007-05-21 13:23:30 +0200175 r = os_random_long(&td->random_state);
176 } while (--loops);
177
178 /*
179 * that didn't work either, try exhaustive search from the start
180 */
181 f->last_free_lookup = 0;
Jens Axboe4ba66132008-02-05 10:05:50 +0100182 return get_next_free_block(td, f, ddir, b);
Jens Axboeec4015d2007-03-23 08:04:27 +0100183}
184
Jens Axboe10ba5352006-10-20 11:39:27 +0200185/*
186 * For random io, generate a random new block and see if it's used. Repeat
187 * until we find a free one. For sequential io, just return the end of
188 * the last io issued.
189 */
Jens Axboe9bf20612007-03-01 09:33:57 +0100190static int get_next_offset(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200191{
Jens Axboe9bf20612007-03-01 09:33:57 +0100192 struct fio_file *f = io_u->file;
Jens Axboeec4015d2007-03-23 08:04:27 +0100193 unsigned long long b;
Jens Axboe4ba66132008-02-05 10:05:50 +0100194 enum fio_ddir ddir = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200195
Jens Axboeec4015d2007-03-23 08:04:27 +0100196 if (td_random(td) && (td->o.ddir_nr && !--td->ddir_nr)) {
197 td->ddir_nr = td->o.ddir_nr;
Jens Axboe10ba5352006-10-20 11:39:27 +0200198
Jens Axboe4ba66132008-02-05 10:05:50 +0100199 if (get_next_rand_offset(td, f, ddir, &b))
Jens Axboebca4ed42007-02-12 05:13:23 +0100200 return 1;
Jens Axboe43063a12007-03-27 20:21:24 +0200201 } else {
ljzhang,Yaxin Hu,Jianchao Tang0c3d7682007-07-26 10:59:25 +0200202 if (f->last_pos >= f->real_file_size) {
Jens Axboe4ba66132008-02-05 10:05:50 +0100203 if (!td_random(td) ||
204 get_next_rand_offset(td, f, ddir, &b))
ljzhang,Yaxin Hu,Jianchao Tang0c3d7682007-07-26 10:59:25 +0200205 return 1;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200206 } else
Jens Axboe4ba66132008-02-05 10:05:50 +0100207 b = (f->last_pos - f->file_offset) / td->o.min_bs[ddir];
Jens Axboe43063a12007-03-27 20:21:24 +0200208 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200209
Jens Axboe009bd842008-05-15 10:19:46 +0200210 io_u->offset = b * td->o.min_bs[ddir];
211 if (io_u->offset >= f->io_size) {
212 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
213 io_u->offset, f->io_size);
214 return 1;
215 }
216
217 io_u->offset += f->file_offset;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100218 if (io_u->offset >= f->real_file_size) {
219 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
220 io_u->offset, f->real_file_size);
Jens Axboe10ba5352006-10-20 11:39:27 +0200221 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100222 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200223
224 return 0;
225}
226
Jens Axboe9bf20612007-03-01 09:33:57 +0100227static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200228{
Jens Axboebca4ed42007-02-12 05:13:23 +0100229 const int ddir = io_u->ddir;
Jens Axboefc4398f2008-05-23 13:28:59 +0200230 unsigned int buflen = buflen; /* silence dumb gcc warning */
Jens Axboe10ba5352006-10-20 11:39:27 +0200231 long r;
232
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100233 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
234 buflen = td->o.min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200235 else {
236 r = os_random_long(&td->bsrange_state);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100237 if (!td->o.bssplit_nr) {
238 buflen = (unsigned int)
239 (1 + (double) (td->o.max_bs[ddir] - 1)
240 * r / (RAND_MAX + 1.0));
241 } else {
Jens Axboe564ca972007-12-14 12:21:19 +0100242 long perc = 0;
243 unsigned int i;
244
245 for (i = 0; i < td->o.bssplit_nr; i++) {
246 struct bssplit *bsp = &td->o.bssplit[i];
247
248 buflen = bsp->bs;
249 perc += bsp->perc;
250 if (r <= ((LONG_MAX / 100L) * perc))
251 break;
252 }
253 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100254 if (!td->o.bs_unaligned) {
255 buflen = (buflen + td->o.min_bs[ddir] - 1)
256 & ~(td->o.min_bs[ddir] - 1);
257 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200258 }
259
Jens Axboe4ba66132008-02-05 10:05:50 +0100260 if (io_u->offset + buflen > io_u->file->real_file_size) {
261 dprint(FD_IO, "lower buflen %u -> %u (ddir=%d)\n", buflen,
262 td->o.min_bs[ddir], ddir);
Jens Axboe6a5e6882007-07-26 10:47:51 +0200263 buflen = td->o.min_bs[ddir];
Jens Axboe4ba66132008-02-05 10:05:50 +0100264 }
Jens Axboe6a5e6882007-07-26 10:47:51 +0200265
Jens Axboe10ba5352006-10-20 11:39:27 +0200266 return buflen;
267}
268
Jens Axboeafe24a52007-03-16 20:27:27 +0100269static void set_rwmix_bytes(struct thread_data *td)
270{
Jens Axboeafe24a52007-03-16 20:27:27 +0100271 unsigned int diff;
272
273 /*
274 * we do time or byte based switch. this is needed because
275 * buffered writes may issue a lot quicker than they complete,
276 * whereas reads do not.
277 */
Jens Axboee47f7992007-03-21 14:05:39 +0100278 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
Jens Axboe04c540d2008-05-28 10:35:26 +0200279 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
Jens Axboee47f7992007-03-21 14:05:39 +0100280}
281
282static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
283{
284 unsigned int v;
285 long r;
286
287 r = os_random_long(&td->rwmix_state);
288 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
Jens Axboe04c540d2008-05-28 10:35:26 +0200289 if (v <= td->o.rwmix[DDIR_READ])
Jens Axboee47f7992007-03-21 14:05:39 +0100290 return DDIR_READ;
291
292 return DDIR_WRITE;
Jens Axboeafe24a52007-03-16 20:27:27 +0100293}
294
Jens Axboe10ba5352006-10-20 11:39:27 +0200295/*
296 * Return the data direction for the next io_u. If the job is a
297 * mixed read/write workload, check the rwmix cycle and switch if
298 * necessary.
299 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100300static enum fio_ddir get_rw_ddir(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200301{
302 if (td_rw(td)) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200303 /*
304 * Check if it's time to seed a new data direction.
305 */
Jens Axboee4928662008-04-07 09:19:46 +0200306 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
Jens Axboee47f7992007-03-21 14:05:39 +0100307 unsigned long long max_bytes;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100308 enum fio_ddir ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200309
Jens Axboee47f7992007-03-21 14:05:39 +0100310 /*
311 * Put a top limit on how many bytes we do for
312 * one data direction, to avoid overflowing the
313 * ranges too much
314 */
315 ddir = get_rand_ddir(td);
316 max_bytes = td->this_io_bytes[ddir];
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100317 if (max_bytes >=
318 (td->o.size * td->o.rwmix[ddir] / 100)) {
Jens Axboee4928662008-04-07 09:19:46 +0200319 if (!td->rw_end_set[ddir])
Jens Axboe38d77ca2007-03-21 14:20:20 +0100320 td->rw_end_set[ddir] = 1;
Jens Axboee4928662008-04-07 09:19:46 +0200321
Jens Axboee47f7992007-03-21 14:05:39 +0100322 ddir ^= 1;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100323 }
Jens Axboee47f7992007-03-21 14:05:39 +0100324
325 if (ddir != td->rwmix_ddir)
326 set_rwmix_bytes(td);
327
328 td->rwmix_ddir = ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200329 }
330 return td->rwmix_ddir;
331 } else if (td_read(td))
332 return DDIR_READ;
333 else
334 return DDIR_WRITE;
335}
336
Jens Axboe60f2c652008-05-16 12:31:36 +0200337static void put_file_log(struct thread_data *td, struct fio_file *f)
338{
339 int ret = put_file(td, f);
340
341 if (ret)
342 td_verror(td, ret, "file close");
343}
344
Jens Axboe10ba5352006-10-20 11:39:27 +0200345void put_io_u(struct thread_data *td, struct io_u *io_u)
346{
Jens Axboe0c6e7512007-02-22 11:19:39 +0100347 assert((io_u->flags & IO_U_F_FREE) == 0);
348 io_u->flags |= IO_U_F_FREE;
349
Jens Axboe60f2c652008-05-16 12:31:36 +0200350 if (io_u->file)
351 put_file_log(td, io_u->file);
Jens Axboe2dbdab72007-04-26 10:21:15 +0200352
Jens Axboe10ba5352006-10-20 11:39:27 +0200353 io_u->file = NULL;
354 list_del(&io_u->list);
355 list_add(&io_u->list, &td->io_u_freelist);
356 td->cur_depth--;
357}
358
Jens Axboe755200a2007-02-19 13:08:12 +0100359void requeue_io_u(struct thread_data *td, struct io_u **io_u)
360{
361 struct io_u *__io_u = *io_u;
362
Jens Axboe465221b2008-05-30 22:07:49 +0200363 dprint(FD_IO, "requeue %p\n", __io_u);
364
Jens Axboe4d2e0f42007-02-24 13:31:57 +0100365 __io_u->flags |= IO_U_F_FREE;
Jens Axboee4f54ad2008-02-04 10:56:07 +0100366 if ((__io_u->flags & IO_U_F_FLIGHT) && (__io_u->ddir != DDIR_SYNC))
367 td->io_issues[__io_u->ddir]--;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100368
Jens Axboe4d2e0f42007-02-24 13:31:57 +0100369 __io_u->flags &= ~IO_U_F_FLIGHT;
370
Jens Axboe755200a2007-02-19 13:08:12 +0100371 list_del(&__io_u->list);
372 list_add_tail(&__io_u->list, &td->io_u_requeues);
373 td->cur_depth--;
374 *io_u = NULL;
375}
376
Jens Axboe9bf20612007-03-01 09:33:57 +0100377static int fill_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200378{
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200379 if (td->io_ops->flags & FIO_NOIO)
380 goto out;
381
Jens Axboe10ba5352006-10-20 11:39:27 +0200382 /*
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200383 * see if it's time to sync
384 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100385 if (td->o.fsync_blocks &&
386 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
387 td->io_issues[DDIR_WRITE] && should_fsync(td)) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200388 io_u->ddir = DDIR_SYNC;
Jens Axboec38e9462007-03-27 08:48:48 +0200389 goto out;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200390 }
391
Jens Axboea00735e2006-11-03 08:58:08 +0100392 io_u->ddir = get_rw_ddir(td);
393
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200394 /*
Jens Axboe48f5abd2007-07-20 13:25:04 +0200395 * See if it's time to switch to a new zone
396 */
397 if (td->zone_bytes >= td->o.zone_size) {
398 td->zone_bytes = 0;
399 io_u->file->last_pos += td->o.zone_skip;
400 td->io_skip_bytes += td->o.zone_skip;
401 }
402
403 /*
Jens Axboec685b5b2007-02-10 20:02:28 +0100404 * No log, let the seq/rand engine retrieve the next buflen and
405 * position.
Jens Axboe10ba5352006-10-20 11:39:27 +0200406 */
Jens Axboe2ba1c292008-02-01 13:16:38 +0100407 if (get_next_offset(td, io_u)) {
408 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100409 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100410 }
Jens Axboec685b5b2007-02-10 20:02:28 +0100411
Jens Axboe9bf20612007-03-01 09:33:57 +0100412 io_u->buflen = get_next_buflen(td, io_u);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100413 if (!io_u->buflen) {
414 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100415 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100416 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200417
Jens Axboe2ba1c292008-02-01 13:16:38 +0100418 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
419 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
Jens Axboe4ba66132008-02-05 10:05:50 +0100420 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
421 io_u->buflen, io_u->file->real_file_size);
Jens Axboe6a5e6882007-07-26 10:47:51 +0200422 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100423 }
Jens Axboe6a5e6882007-07-26 10:47:51 +0200424
Jens Axboebca4ed42007-02-12 05:13:23 +0100425 /*
426 * mark entry before potentially trimming io_u
427 */
Jens Axboe303032a2008-03-26 10:11:10 +0100428 if (td_random(td) && file_randommap(td, io_u->file))
Jens Axboe9bf20612007-03-01 09:33:57 +0100429 mark_random_map(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200430
Jens Axboebca4ed42007-02-12 05:13:23 +0100431 /*
432 * If using a write iolog, store this entry.
433 */
Jens Axboec38e9462007-03-27 08:48:48 +0200434out:
Jens Axboe2ba1c292008-02-01 13:16:38 +0100435 dprint_io_u(io_u, "fill_io_u");
Jens Axboed9d91e32008-01-31 13:25:42 +0100436 td->zone_bytes += io_u->buflen;
Jens Axboef29b25a2007-07-23 08:56:43 +0200437 log_io_u(td, io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100438 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200439}
440
Jens Axboe838bc702008-05-22 13:08:23 +0200441static void __io_u_mark_map(unsigned int *map, unsigned int nr)
442{
443 int index = 0;
444
445 switch (nr) {
446 default:
447 index = 6;
448 break;
449 case 33 ... 64:
450 index = 5;
451 break;
452 case 17 ... 32:
453 index = 4;
454 break;
455 case 9 ... 16:
456 index = 3;
457 break;
458 case 5 ... 8:
459 index = 2;
460 break;
461 case 1 ... 4:
462 index = 1;
463 case 0:
464 break;
465 }
466
467 map[index]++;
468}
469
470void io_u_mark_submit(struct thread_data *td, unsigned int nr)
471{
472 __io_u_mark_map(td->ts.io_u_submit, nr);
473 td->ts.total_submit++;
474}
475
476void io_u_mark_complete(struct thread_data *td, unsigned int nr)
477{
478 __io_u_mark_map(td->ts.io_u_complete, nr);
479 td->ts.total_complete++;
480}
481
Jens Axboed8005752008-05-15 09:49:09 +0200482void io_u_mark_depth(struct thread_data *td, unsigned int nr)
Jens Axboe71619dc2007-01-13 23:56:33 +0100483{
484 int index = 0;
485
486 switch (td->cur_depth) {
487 default:
Jens Axboea783e612007-06-19 09:50:28 +0200488 index = 6;
489 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100490 case 32 ... 63:
Jens Axboea783e612007-06-19 09:50:28 +0200491 index = 5;
492 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100493 case 16 ... 31:
Jens Axboea783e612007-06-19 09:50:28 +0200494 index = 4;
495 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100496 case 8 ... 15:
Jens Axboea783e612007-06-19 09:50:28 +0200497 index = 3;
498 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100499 case 4 ... 7:
Jens Axboea783e612007-06-19 09:50:28 +0200500 index = 2;
501 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100502 case 2 ... 3:
Jens Axboea783e612007-06-19 09:50:28 +0200503 index = 1;
Jens Axboe71619dc2007-01-13 23:56:33 +0100504 case 1:
505 break;
506 }
507
Jens Axboe3bec7ae2008-05-14 21:08:37 +0200508 td->ts.io_u_map[index] += nr;
Jens Axboe71619dc2007-01-13 23:56:33 +0100509}
510
Jens Axboe04a0fea2007-06-19 12:48:41 +0200511static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
512{
513 int index = 0;
514
515 assert(usec < 1000);
516
517 switch (usec) {
518 case 750 ... 999:
519 index = 9;
520 break;
521 case 500 ... 749:
522 index = 8;
523 break;
524 case 250 ... 499:
525 index = 7;
526 break;
527 case 100 ... 249:
528 index = 6;
529 break;
530 case 50 ... 99:
531 index = 5;
532 break;
533 case 20 ... 49:
534 index = 4;
535 break;
536 case 10 ... 19:
537 index = 3;
538 break;
539 case 4 ... 9:
540 index = 2;
541 break;
542 case 2 ... 3:
543 index = 1;
544 case 0 ... 1:
545 break;
546 }
547
548 assert(index < FIO_IO_U_LAT_U_NR);
549 td->ts.io_u_lat_u[index]++;
550}
551
552static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
Jens Axboeec118302007-02-17 04:38:20 +0100553{
554 int index = 0;
555
556 switch (msec) {
557 default:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200558 index = 11;
559 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100560 case 1000 ... 1999:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200561 index = 10;
562 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100563 case 750 ... 999:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200564 index = 9;
565 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100566 case 500 ... 749:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200567 index = 8;
568 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100569 case 250 ... 499:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200570 index = 7;
571 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100572 case 100 ... 249:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200573 index = 6;
574 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100575 case 50 ... 99:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200576 index = 5;
577 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100578 case 20 ... 49:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200579 index = 4;
580 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100581 case 10 ... 19:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200582 index = 3;
583 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100584 case 4 ... 9:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200585 index = 2;
586 break;
Jens Axboeec118302007-02-17 04:38:20 +0100587 case 2 ... 3:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200588 index = 1;
Jens Axboeec118302007-02-17 04:38:20 +0100589 case 0 ... 1:
590 break;
591 }
592
Jens Axboe04a0fea2007-06-19 12:48:41 +0200593 assert(index < FIO_IO_U_LAT_M_NR);
594 td->ts.io_u_lat_m[index]++;
595}
596
597static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
598{
599 if (usec < 1000)
600 io_u_mark_lat_usec(td, usec);
601 else
602 io_u_mark_lat_msec(td, usec / 1000);
Jens Axboeec118302007-02-17 04:38:20 +0100603}
604
Jens Axboe0aabe162007-02-23 08:45:55 +0100605/*
606 * Get next file to service by choosing one at random
607 */
Jens Axboe1c178182007-03-13 13:25:18 +0100608static struct fio_file *get_next_file_rand(struct thread_data *td, int goodf,
609 int badf)
Jens Axboe0aabe162007-02-23 08:45:55 +0100610{
Jens Axboe0aabe162007-02-23 08:45:55 +0100611 struct fio_file *f;
Jens Axboe1c178182007-03-13 13:25:18 +0100612 int fno;
Jens Axboe0aabe162007-02-23 08:45:55 +0100613
614 do {
Jens Axboe7c83c082007-03-01 10:04:15 +0100615 long r = os_random_long(&td->next_file_state);
616
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100617 fno = (unsigned int) ((double) td->o.nr_files
618 * (r / (RAND_MAX + 1.0)));
Jens Axboe126d65c2008-03-01 18:04:31 +0100619 f = td->files[fno];
Jens Axboe059e63c2007-03-27 20:34:47 +0200620 if (f->flags & FIO_FILE_DONE)
621 continue;
Jens Axboe1c178182007-03-13 13:25:18 +0100622
Jens Axboe2ba1c292008-02-01 13:16:38 +0100623 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
624 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
Jens Axboe0aabe162007-02-23 08:45:55 +0100625 return f;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100626 }
Jens Axboe0aabe162007-02-23 08:45:55 +0100627 } while (1);
628}
629
630/*
631 * Get next file to service by doing round robin between all available ones
632 */
Jens Axboe1c178182007-03-13 13:25:18 +0100633static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
634 int badf)
Jens Axboe3d7c3912007-02-19 13:16:12 +0100635{
636 unsigned int old_next_file = td->next_file;
637 struct fio_file *f;
638
639 do {
Jens Axboe126d65c2008-03-01 18:04:31 +0100640 f = td->files[td->next_file];
Jens Axboe3d7c3912007-02-19 13:16:12 +0100641
642 td->next_file++;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100643 if (td->next_file >= td->o.nr_files)
Jens Axboe3d7c3912007-02-19 13:16:12 +0100644 td->next_file = 0;
645
Jens Axboed5ed68e2007-03-28 09:33:43 +0200646 if (f->flags & FIO_FILE_DONE) {
647 f = NULL;
Jens Axboe059e63c2007-03-27 20:34:47 +0200648 continue;
Jens Axboed5ed68e2007-03-28 09:33:43 +0200649 }
Jens Axboe059e63c2007-03-27 20:34:47 +0200650
Jens Axboe1c178182007-03-13 13:25:18 +0100651 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
Jens Axboe3d7c3912007-02-19 13:16:12 +0100652 break;
653
654 f = NULL;
655 } while (td->next_file != old_next_file);
656
Jens Axboe2ba1c292008-02-01 13:16:38 +0100657 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
Jens Axboe3d7c3912007-02-19 13:16:12 +0100658 return f;
659}
660
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100661static struct fio_file *get_next_file(struct thread_data *td)
662{
Jens Axboe1907dbc2007-03-12 11:44:28 +0100663 struct fio_file *f;
664
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100665 assert(td->o.nr_files <= td->files_index);
Jens Axboe1c178182007-03-13 13:25:18 +0100666
Jens Axboe2ba1c292008-02-01 13:16:38 +0100667 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100668 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
669 " nr_files=%d\n", td->nr_open_files,
670 td->nr_done_files,
671 td->o.nr_files);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100672 return NULL;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100673 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100674
Jens Axboe1907dbc2007-03-12 11:44:28 +0100675 f = td->file_service_file;
Jens Axboef11bd942007-03-13 10:16:34 +0100676 if (f && (f->flags & FIO_FILE_OPEN) && td->file_service_left--)
Jens Axboe2ba1c292008-02-01 13:16:38 +0100677 goto out;
Jens Axboe1907dbc2007-03-12 11:44:28 +0100678
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100679 if (td->o.file_service_type == FIO_FSERVICE_RR)
Jens Axboe1c178182007-03-13 13:25:18 +0100680 f = get_next_file_rr(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100681 else
Jens Axboe1c178182007-03-13 13:25:18 +0100682 f = get_next_file_rand(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
Jens Axboe1907dbc2007-03-12 11:44:28 +0100683
684 td->file_service_file = f;
685 td->file_service_left = td->file_service_nr - 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100686out:
687 dprint(FD_FILE, "get_next_file: %p\n", f);
Jens Axboe1907dbc2007-03-12 11:44:28 +0100688 return f;
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100689}
690
Jens Axboe1c178182007-03-13 13:25:18 +0100691static struct fio_file *find_next_new_file(struct thread_data *td)
692{
693 struct fio_file *f;
694
Jens Axboe1020a132007-03-29 11:15:06 +0200695 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files)
696 return NULL;
697
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100698 if (td->o.file_service_type == FIO_FSERVICE_RR)
Jens Axboe1c178182007-03-13 13:25:18 +0100699 f = get_next_file_rr(td, 0, FIO_FILE_OPEN);
700 else
701 f = get_next_file_rand(td, 0, FIO_FILE_OPEN);
702
703 return f;
704}
705
Jens Axboe429f6672007-07-23 10:38:43 +0200706static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
707{
708 struct fio_file *f;
709
710 do {
711 f = get_next_file(td);
712 if (!f)
713 return 1;
714
715set_file:
716 io_u->file = f;
717 get_file(f);
718
719 if (!fill_io_u(td, io_u))
720 break;
721
722 /*
Jens Axboe009bd842008-05-15 10:19:46 +0200723 * optimization to prevent close/open of the same file. This
724 * way we preserve queueing etc.
725 */
726 if (td->o.nr_files == 1 && td->o.time_based) {
Jens Axboe60f2c652008-05-16 12:31:36 +0200727 put_file_log(td, f);
Jens Axboe009bd842008-05-15 10:19:46 +0200728 fio_file_reset(f);
729 goto set_file;
730 }
731
732 /*
Jens Axboe429f6672007-07-23 10:38:43 +0200733 * td_io_close() does a put_file() as well, so no need to
734 * do that here.
735 */
736 io_u->file = NULL;
737 td_io_close_file(td, f);
738 f->flags |= FIO_FILE_DONE;
739 td->nr_done_files++;
740
741 /*
742 * probably not the right place to do this, but see
743 * if we need to open a new file
744 */
745 if (td->nr_open_files < td->o.open_files &&
746 td->o.open_files != td->o.nr_files) {
747 f = find_next_new_file(td);
748
749 if (!f || td_io_open_file(td, f))
750 return 1;
751
752 goto set_file;
753 }
754 } while (1);
755
756 return 0;
757}
758
759
Jens Axboe10ba5352006-10-20 11:39:27 +0200760struct io_u *__get_io_u(struct thread_data *td)
761{
762 struct io_u *io_u = NULL;
763
Jens Axboe755200a2007-02-19 13:08:12 +0100764 if (!list_empty(&td->io_u_requeues))
765 io_u = list_entry(td->io_u_requeues.next, struct io_u, list);
766 else if (!queue_full(td)) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200767 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
768
Jens Axboe6040dab2006-10-24 19:38:15 +0200769 io_u->buflen = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200770 io_u->resid = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100771 io_u->file = NULL;
Jens Axboed7762cf2007-02-23 12:34:57 +0100772 io_u->end_io = NULL;
Jens Axboe755200a2007-02-19 13:08:12 +0100773 }
774
775 if (io_u) {
Jens Axboe0c6e7512007-02-22 11:19:39 +0100776 assert(io_u->flags & IO_U_F_FREE);
777 io_u->flags &= ~IO_U_F_FREE;
778
Jens Axboe755200a2007-02-19 13:08:12 +0100779 io_u->error = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200780 list_del(&io_u->list);
781 list_add(&io_u->list, &td->io_u_busylist);
782 td->cur_depth++;
783 }
784
785 return io_u;
786}
787
788/*
789 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
790 * etc. The returned io_u is fully ready to be prepped and submitted.
791 */
Jens Axboe3d7c3912007-02-19 13:16:12 +0100792struct io_u *get_io_u(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200793{
Jens Axboe3d7c3912007-02-19 13:16:12 +0100794 struct fio_file *f;
Jens Axboe10ba5352006-10-20 11:39:27 +0200795 struct io_u *io_u;
796
797 io_u = __get_io_u(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100798 if (!io_u) {
799 dprint(FD_IO, "__get_io_u failed\n");
Jens Axboe10ba5352006-10-20 11:39:27 +0200800 return NULL;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100801 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200802
Jens Axboe755200a2007-02-19 13:08:12 +0100803 /*
804 * from a requeue, io_u already setup
805 */
806 if (io_u->file)
Jens Axboe77f392b2007-02-19 20:13:09 +0100807 goto out;
Jens Axboe755200a2007-02-19 13:08:12 +0100808
Jens Axboe429f6672007-07-23 10:38:43 +0200809 /*
810 * If using an iolog, grab next piece if any available.
811 */
812 if (td->o.read_iolog_file) {
813 if (read_iolog_get(td, io_u))
814 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100815 } else if (set_io_u_file(td, io_u)) {
816 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
Jens Axboe429f6672007-07-23 10:38:43 +0200817 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100818 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100819
Jens Axboe429f6672007-07-23 10:38:43 +0200820 f = io_u->file;
821 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe97af62c2007-05-22 11:12:13 +0200822
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200823 if (io_u->ddir != DDIR_SYNC) {
Jens Axboed0656a92008-02-01 18:33:23 +0100824 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
Jens Axboe2ba1c292008-02-01 13:16:38 +0100825 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
Jens Axboe429f6672007-07-23 10:38:43 +0200826 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100827 }
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200828
Jens Axboe36167d82007-02-18 05:41:31 +0100829 f->last_pos = io_u->offset + io_u->buflen;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200830
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100831 if (td->o.verify != VERIFY_NONE)
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200832 populate_verify_io_u(td, io_u);
Jens Axboee4dad9c2008-05-28 10:53:44 +0200833 else if (td->o.refill_buffers && io_u->ddir == DDIR_WRITE)
834 io_u_fill_buffer(td, io_u, io_u->xfer_buflen);
Jens Axboe10ba5352006-10-20 11:39:27 +0200835 }
836
Jens Axboe165faf12007-02-07 11:30:37 +0100837 /*
838 * Set io data pointers.
839 */
Jens Axboed460eb32007-04-16 21:39:33 +0200840 io_u->endpos = io_u->offset + io_u->buflen;
Jens Axboecec6b552007-02-06 20:15:38 +0100841 io_u->xfer_buf = io_u->buf;
842 io_u->xfer_buflen = io_u->buflen;
Jens Axboe5973caf2008-05-21 19:52:35 +0200843
Jens Axboe6ac7a332008-03-01 15:22:32 +0100844out:
Jens Axboe429f6672007-07-23 10:38:43 +0200845 if (!td_io_prep(td, io_u)) {
846 fio_gettime(&io_u->start_time, NULL);
847 return io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100848 }
Jens Axboe429f6672007-07-23 10:38:43 +0200849err_put:
Jens Axboe2ba1c292008-02-01 13:16:38 +0100850 dprint(FD_IO, "get_io_u failed\n");
Jens Axboe429f6672007-07-23 10:38:43 +0200851 put_io_u(td, io_u);
852 return NULL;
Jens Axboe10ba5352006-10-20 11:39:27 +0200853}
854
Jens Axboe54517922007-03-05 10:06:06 +0100855void io_u_log_error(struct thread_data *td, struct io_u *io_u)
856{
857 const char *msg[] = { "read", "write", "sync" };
858
859 log_err("fio: io_u error");
860
861 if (io_u->file)
862 log_err(" on file %s", io_u->file->file_name);
863
864 log_err(": %s\n", strerror(io_u->error));
865
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100866 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
867 io_u->offset, io_u->xfer_buflen);
Jens Axboe54517922007-03-05 10:06:06 +0100868
869 if (!td->error)
870 td_verror(td, io_u->error, "io_u error");
871}
872
Jens Axboe97601022007-02-18 12:47:29 +0100873static void io_completed(struct thread_data *td, struct io_u *io_u,
874 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200875{
Jens Axboed85f5112007-06-18 09:41:23 +0200876 unsigned long usec;
Jens Axboe10ba5352006-10-20 11:39:27 +0200877
Jens Axboe2ba1c292008-02-01 13:16:38 +0100878 dprint_io_u(io_u, "io complete");
879
Jens Axboe0c6e7512007-02-22 11:19:39 +0100880 assert(io_u->flags & IO_U_F_FLIGHT);
881 io_u->flags &= ~IO_U_F_FLIGHT;
882
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200883 if (io_u->ddir == DDIR_SYNC) {
884 td->last_was_sync = 1;
885 return;
886 }
887
888 td->last_was_sync = 0;
889
Jens Axboe10ba5352006-10-20 11:39:27 +0200890 if (!io_u->error) {
891 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100892 const enum fio_ddir idx = io_u->ddir;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100893 int ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200894
895 td->io_blocks[idx]++;
896 td->io_bytes[idx] += bytes;
Jens Axboe10ba5352006-10-20 11:39:27 +0200897 td->this_io_bytes[idx] += bytes;
898
Jens Axboed85f5112007-06-18 09:41:23 +0200899 usec = utime_since(&io_u->issue_time, &icd->time);
Jens Axboe10ba5352006-10-20 11:39:27 +0200900
Jens Axboed85f5112007-06-18 09:41:23 +0200901 add_clat_sample(td, idx, usec);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100902 add_bw_sample(td, idx, &icd->time);
Jens Axboe04a0fea2007-06-19 12:48:41 +0200903 io_u_mark_latency(td, usec);
Jens Axboe10ba5352006-10-20 11:39:27 +0200904
Jens Axboe660a1cb2007-04-17 15:37:47 +0200905 if (td_write(td) && idx == DDIR_WRITE &&
Shawn Lewis86705792007-11-21 09:35:41 +0100906 td->o.do_verify &&
Jens Axboe41128402007-03-27 08:32:48 +0200907 td->o.verify != VERIFY_NONE)
Jens Axboe10ba5352006-10-20 11:39:27 +0200908 log_io_piece(td, io_u);
909
910 icd->bytes_done[idx] += bytes;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100911
Jens Axboed7762cf2007-02-23 12:34:57 +0100912 if (io_u->end_io) {
Jens Axboe36690c92007-03-26 10:23:34 +0200913 ret = io_u->end_io(td, io_u);
Jens Axboe3af6ef32007-02-18 06:57:43 +0100914 if (ret && !icd->error)
915 icd->error = ret;
916 }
Jens Axboe54517922007-03-05 10:06:06 +0100917 } else {
Jens Axboe10ba5352006-10-20 11:39:27 +0200918 icd->error = io_u->error;
Jens Axboe54517922007-03-05 10:06:06 +0100919 io_u_log_error(td, io_u);
920 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200921}
922
Jens Axboed7762cf2007-02-23 12:34:57 +0100923static void init_icd(struct io_completion_data *icd, int nr)
Jens Axboe36167d82007-02-18 05:41:31 +0100924{
925 fio_gettime(&icd->time, NULL);
926
Jens Axboe3af6ef32007-02-18 06:57:43 +0100927 icd->nr = nr;
928
Jens Axboe36167d82007-02-18 05:41:31 +0100929 icd->error = 0;
930 icd->bytes_done[0] = icd->bytes_done[1] = 0;
931}
932
Jens Axboe97601022007-02-18 12:47:29 +0100933static void ios_completed(struct thread_data *td,
934 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200935{
936 struct io_u *io_u;
937 int i;
938
Jens Axboe10ba5352006-10-20 11:39:27 +0200939 for (i = 0; i < icd->nr; i++) {
940 io_u = td->io_ops->event(td, i);
941
942 io_completed(td, io_u, icd);
943 put_io_u(td, io_u);
944 }
945}
Jens Axboe97601022007-02-18 12:47:29 +0100946
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100947/*
948 * Complete a single io_u for the sync engines.
949 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100950long io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
Jens Axboe97601022007-02-18 12:47:29 +0100951{
952 struct io_completion_data icd;
953
Jens Axboed7762cf2007-02-23 12:34:57 +0100954 init_icd(&icd, 1);
Jens Axboe97601022007-02-18 12:47:29 +0100955 io_completed(td, io_u, &icd);
956 put_io_u(td, io_u);
957
958 if (!icd.error)
959 return icd.bytes_done[0] + icd.bytes_done[1];
960
Jens Axboe37e974a2007-03-02 15:16:45 +0100961 td_verror(td, icd.error, "io_u_sync_complete");
Jens Axboe97601022007-02-18 12:47:29 +0100962 return -1;
963}
964
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100965/*
966 * Called to complete min_events number of io for the async engines.
967 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100968long io_u_queued_complete(struct thread_data *td, int min_events)
Jens Axboe97601022007-02-18 12:47:29 +0100969{
Jens Axboe97601022007-02-18 12:47:29 +0100970 struct io_completion_data icd;
Jens Axboe00de55e2007-02-20 10:45:57 +0100971 struct timespec *tvp = NULL;
Jens Axboe97601022007-02-18 12:47:29 +0100972 int ret;
Davide Libenzi4d06a332007-03-22 07:43:50 +0100973 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
Jens Axboe97601022007-02-18 12:47:29 +0100974
Jens Axboeb271fe62008-02-04 10:49:41 +0100975 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_events);
976
Davide Libenzi4d06a332007-03-22 07:43:50 +0100977 if (!min_events)
Jens Axboe00de55e2007-02-20 10:45:57 +0100978 tvp = &ts;
Jens Axboe97601022007-02-18 12:47:29 +0100979
Jens Axboe00de55e2007-02-20 10:45:57 +0100980 ret = td_io_getevents(td, min_events, td->cur_depth, tvp);
Jens Axboe97601022007-02-18 12:47:29 +0100981 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100982 td_verror(td, -ret, "td_io_getevents");
Jens Axboe97601022007-02-18 12:47:29 +0100983 return ret;
984 } else if (!ret)
985 return ret;
986
Jens Axboed7762cf2007-02-23 12:34:57 +0100987 init_icd(&icd, ret);
Jens Axboe97601022007-02-18 12:47:29 +0100988 ios_completed(td, &icd);
989 if (!icd.error)
990 return icd.bytes_done[0] + icd.bytes_done[1];
991
Jens Axboe37e974a2007-03-02 15:16:45 +0100992 td_verror(td, icd.error, "io_u_queued_complete");
Jens Axboe97601022007-02-18 12:47:29 +0100993 return -1;
994}
Jens Axboe7e77dd02007-02-20 10:57:34 +0100995
996/*
997 * Call when io_u is really queued, to update the submission latency.
998 */
999void io_u_queued(struct thread_data *td, struct io_u *io_u)
1000{
1001 unsigned long slat_time;
1002
Jens Axboed85f5112007-06-18 09:41:23 +02001003 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
Jens Axboe7e77dd02007-02-20 10:57:34 +01001004 add_slat_sample(td, io_u->ddir, slat_time);
1005}
Jens Axboe433afcb2007-02-22 10:39:01 +01001006
Jens Axboe5973caf2008-05-21 19:52:35 +02001007/*
1008 * "randomly" fill the buffer contents
1009 */
1010void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1011 unsigned int max_bs)
1012{
1013 long *ptr = io_u->buf;
1014
1015 if (!td->o.zero_buffers) {
1016 while ((void *) ptr - io_u->buf < max_bs) {
1017 *ptr = rand() * GOLDEN_RATIO_PRIME;
1018 ptr++;
1019 }
1020 } else
1021 memset(ptr, 0, max_bs);
1022}
1023
Jens Axboe55bc9722007-02-22 11:30:05 +01001024#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +01001025void io_u_set_timeout(struct thread_data *td)
1026{
1027 assert(td->cur_depth);
1028
1029 td->timer.it_interval.tv_sec = 0;
1030 td->timer.it_interval.tv_usec = 0;
1031 td->timer.it_value.tv_sec = IO_U_TIMEOUT + IO_U_TIMEOUT_INC;
1032 td->timer.it_value.tv_usec = 0;
1033 setitimer(ITIMER_REAL, &td->timer, NULL);
1034 fio_gettime(&td->timeout_end, NULL);
1035}
Jens Axboe5945b9b2007-02-22 20:33:01 +01001036
1037static void io_u_dump(struct io_u *io_u)
1038{
1039 unsigned long t_start = mtime_since_now(&io_u->start_time);
1040 unsigned long t_issue = mtime_since_now(&io_u->issue_time);
1041
1042 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 +01001043 log_err(" buf=%p/%p, len=%lu/%lu, offset=%llu\n", io_u->buf,
1044 io_u->xfer_buf, io_u->buflen,
1045 io_u->xfer_buflen,
1046 io_u->offset);
Jens Axboe5945b9b2007-02-22 20:33:01 +01001047 log_err(" ddir=%d, fname=%s\n", io_u->ddir, io_u->file->file_name);
1048}
Jens Axboe55bc9722007-02-22 11:30:05 +01001049#else
1050void io_u_set_timeout(struct thread_data fio_unused *td)
1051{
1052}
1053#endif
Jens Axboe433afcb2007-02-22 10:39:01 +01001054
Jens Axboe55bc9722007-02-22 11:30:05 +01001055#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +01001056static void io_u_timeout_handler(int fio_unused sig)
1057{
1058 struct thread_data *td, *__td;
1059 pid_t pid = getpid();
Jens Axboe5945b9b2007-02-22 20:33:01 +01001060 struct list_head *entry;
1061 struct io_u *io_u;
Jens Axboe433afcb2007-02-22 10:39:01 +01001062 int i;
1063
1064 log_err("fio: io_u timeout\n");
1065
1066 /*
1067 * TLS would be nice...
1068 */
1069 td = NULL;
1070 for_each_td(__td, i) {
1071 if (__td->pid == pid) {
1072 td = __td;
1073 break;
1074 }
1075 }
1076
1077 if (!td) {
1078 log_err("fio: io_u timeout, can't find job\n");
1079 exit(1);
1080 }
1081
1082 if (!td->cur_depth) {
1083 log_err("fio: timeout without pending work?\n");
1084 return;
1085 }
1086
Jens Axboe15506d02007-03-15 15:04:43 +01001087 log_err("fio: io_u timeout: job=%s, pid=%d\n", td->o.name, td->pid);
Jens Axboe5945b9b2007-02-22 20:33:01 +01001088
1089 list_for_each(entry, &td->io_u_busylist) {
1090 io_u = list_entry(entry, struct io_u, list);
1091
1092 io_u_dump(io_u);
1093 }
1094
1095 td_verror(td, ETIMEDOUT, "io_u timeout");
Jens Axboe433afcb2007-02-22 10:39:01 +01001096 exit(1);
1097}
Jens Axboe55bc9722007-02-22 11:30:05 +01001098#endif
Jens Axboe433afcb2007-02-22 10:39:01 +01001099
1100void io_u_init_timeout(void)
1101{
Jens Axboe55bc9722007-02-22 11:30:05 +01001102#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +01001103 signal(SIGALRM, io_u_timeout_handler);
Jens Axboe55bc9722007-02-22 11:30:05 +01001104#endif
Jens Axboe433afcb2007-02-22 10:39:01 +01001105}