blob: f2fa859c4bcb4556fa58338b3a7cc6d90f6fdbcd [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 Axboedf415582006-10-20 11:41:03 +020081 }
82
Jens Axboea00735e2006-11-03 08:58:08 +010083 if ((blocks * min_bs) < io_u->buflen)
84 io_u->buflen = blocks * min_bs;
Jens Axboedf415582006-10-20 11:41:03 +020085}
86
Jens Axboe3e3357b2008-06-02 09:53:05 +020087static unsigned long long last_block(struct thread_data *td, struct fio_file *f,
88 enum fio_ddir ddir)
Jens Axboe2ba1c292008-02-01 13:16:38 +010089{
90 unsigned long long max_blocks;
Jens Axboed9dd70f2008-05-23 12:37:23 +020091 unsigned long long max_size;
Jens Axboe2ba1c292008-02-01 13:16:38 +010092
Jens Axboed9dd70f2008-05-23 12:37:23 +020093 /*
94 * Hmm, should we make sure that ->io_size <= ->real_file_size?
95 */
96 max_size = f->io_size;
97 if (max_size > f->real_file_size)
98 max_size = f->real_file_size;
99
100 max_blocks = max_size / (unsigned long long) td->o.min_bs[ddir];
Jens Axboe2ba1c292008-02-01 13:16:38 +0100101 if (!max_blocks)
102 return 0;
103
Jens Axboe67778e82008-05-15 09:20:08 +0200104 return max_blocks;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100105}
106
Jens Axboedf415582006-10-20 11:41:03 +0200107/*
Jens Axboe10ba5352006-10-20 11:39:27 +0200108 * Return the next free block in the map.
109 */
110static int get_next_free_block(struct thread_data *td, struct fio_file *f,
Jens Axboe4ba66132008-02-05 10:05:50 +0100111 enum fio_ddir ddir, unsigned long long *b)
Jens Axboe10ba5352006-10-20 11:39:27 +0200112{
Jens Axboe84422ac2008-02-19 20:10:26 +0100113 unsigned long long min_bs = td->o.rw_min_bs;
Jens Axboe10ba5352006-10-20 11:39:27 +0200114 int i;
115
Jens Axboec685b5b2007-02-10 20:02:28 +0100116 i = f->last_free_lookup;
117 *b = (i * BLOCKS_PER_MAP);
Jens Axboe84422ac2008-02-19 20:10:26 +0100118 while ((*b) * min_bs < f->real_file_size) {
Jens Axboede605662008-05-31 00:21:12 +0200119 if (f->file_map[i] != (unsigned int) -1) {
Jens Axboe697a6062008-05-31 00:04:45 +0200120 *b += ffz(f->file_map[i]);
Jens Axboe4ba66132008-02-05 10:05:50 +0100121 if (*b > last_block(td, f, ddir))
Jens Axboe2ba1c292008-02-01 13:16:38 +0100122 break;
Jens Axboec685b5b2007-02-10 20:02:28 +0100123 f->last_free_lookup = i;
Jens Axboe10ba5352006-10-20 11:39:27 +0200124 return 0;
125 }
126
127 *b += BLOCKS_PER_MAP;
128 i++;
129 }
130
Jens Axboe2ba1c292008-02-01 13:16:38 +0100131 dprint(FD_IO, "failed finding a free block\n");
Jens Axboe10ba5352006-10-20 11:39:27 +0200132 return 1;
133}
134
Jens Axboeec4015d2007-03-23 08:04:27 +0100135static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
Jens Axboe4ba66132008-02-05 10:05:50 +0100136 enum fio_ddir ddir, unsigned long long *b)
Jens Axboeec4015d2007-03-23 08:04:27 +0100137{
Jens Axboe84422ac2008-02-19 20:10:26 +0100138 unsigned long long r;
Jens Axboeec4015d2007-03-23 08:04:27 +0100139 int loops = 5;
140
141 do {
142 r = os_random_long(&td->random_state);
Jens Axboe84422ac2008-02-19 20:10:26 +0100143 dprint(FD_RANDOM, "off rand %llu\n", r);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100144 *b = (last_block(td, f, ddir) - 1)
145 * (r / ((unsigned long long) RAND_MAX + 1.0));
Jens Axboe2ba1c292008-02-01 13:16:38 +0100146
Jens Axboe43c63a72007-05-21 13:23:30 +0200147 /*
148 * if we are not maintaining a random map, we are done.
149 */
Jens Axboe303032a2008-03-26 10:11:10 +0100150 if (!file_randommap(td, f))
Jens Axboe43c63a72007-05-21 13:23:30 +0200151 return 0;
152
153 /*
Jens Axboe84422ac2008-02-19 20:10:26 +0100154 * calculate map offset and check if it's free
Jens Axboe43c63a72007-05-21 13:23:30 +0200155 */
Jens Axboeaec2de22008-04-24 12:44:42 +0200156 if (random_map_free(f, *b))
Jens Axboe43c63a72007-05-21 13:23:30 +0200157 return 0;
158
Jens Axboe84422ac2008-02-19 20:10:26 +0100159 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
160 *b);
Jens Axboe43c63a72007-05-21 13:23:30 +0200161 } while (--loops);
Jens Axboeec4015d2007-03-23 08:04:27 +0100162
163 /*
Jens Axboe43c63a72007-05-21 13:23:30 +0200164 * we get here, if we didn't suceed in looking up a block. generate
165 * a random start offset into the filemap, and find the first free
166 * block from there.
Jens Axboeec4015d2007-03-23 08:04:27 +0100167 */
Jens Axboe43c63a72007-05-21 13:23:30 +0200168 loops = 10;
169 do {
170 f->last_free_lookup = (f->num_maps - 1) * (r / (RAND_MAX+1.0));
Jens Axboe4ba66132008-02-05 10:05:50 +0100171 if (!get_next_free_block(td, f, ddir, b))
Jens Axboe43c63a72007-05-21 13:23:30 +0200172 return 0;
Jens Axboeec4015d2007-03-23 08:04:27 +0100173
Jens Axboe43c63a72007-05-21 13:23:30 +0200174 r = os_random_long(&td->random_state);
175 } while (--loops);
176
177 /*
178 * that didn't work either, try exhaustive search from the start
179 */
180 f->last_free_lookup = 0;
Jens Axboe4ba66132008-02-05 10:05:50 +0100181 return get_next_free_block(td, f, ddir, b);
Jens Axboeec4015d2007-03-23 08:04:27 +0100182}
183
Jens Axboe10ba5352006-10-20 11:39:27 +0200184/*
185 * For random io, generate a random new block and see if it's used. Repeat
186 * until we find a free one. For sequential io, just return the end of
187 * the last io issued.
188 */
Jens Axboe9bf20612007-03-01 09:33:57 +0100189static int get_next_offset(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200190{
Jens Axboe9bf20612007-03-01 09:33:57 +0100191 struct fio_file *f = io_u->file;
Jens Axboeec4015d2007-03-23 08:04:27 +0100192 unsigned long long b;
Jens Axboe4ba66132008-02-05 10:05:50 +0100193 enum fio_ddir ddir = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200194
Jens Axboeec4015d2007-03-23 08:04:27 +0100195 if (td_random(td) && (td->o.ddir_nr && !--td->ddir_nr)) {
196 td->ddir_nr = td->o.ddir_nr;
Jens Axboe10ba5352006-10-20 11:39:27 +0200197
Jens Axboe4ba66132008-02-05 10:05:50 +0100198 if (get_next_rand_offset(td, f, ddir, &b))
Jens Axboebca4ed42007-02-12 05:13:23 +0100199 return 1;
Jens Axboe43063a12007-03-27 20:21:24 +0200200 } else {
ljzhang,Yaxin Hu,Jianchao Tang0c3d7682007-07-26 10:59:25 +0200201 if (f->last_pos >= f->real_file_size) {
Jens Axboe4ba66132008-02-05 10:05:50 +0100202 if (!td_random(td) ||
203 get_next_rand_offset(td, f, ddir, &b))
ljzhang,Yaxin Hu,Jianchao Tang0c3d7682007-07-26 10:59:25 +0200204 return 1;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200205 } else
Jens Axboe4ba66132008-02-05 10:05:50 +0100206 b = (f->last_pos - f->file_offset) / td->o.min_bs[ddir];
Jens Axboe43063a12007-03-27 20:21:24 +0200207 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200208
Jens Axboe009bd842008-05-15 10:19:46 +0200209 io_u->offset = b * td->o.min_bs[ddir];
210 if (io_u->offset >= f->io_size) {
211 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
212 io_u->offset, f->io_size);
213 return 1;
214 }
215
216 io_u->offset += f->file_offset;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100217 if (io_u->offset >= f->real_file_size) {
218 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
219 io_u->offset, f->real_file_size);
Jens Axboe10ba5352006-10-20 11:39:27 +0200220 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100221 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200222
223 return 0;
224}
225
Jens Axboe9bf20612007-03-01 09:33:57 +0100226static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200227{
Jens Axboebca4ed42007-02-12 05:13:23 +0100228 const int ddir = io_u->ddir;
Jens Axboefc4398f2008-05-23 13:28:59 +0200229 unsigned int buflen = buflen; /* silence dumb gcc warning */
Jens Axboe10ba5352006-10-20 11:39:27 +0200230 long r;
231
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100232 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
233 buflen = td->o.min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200234 else {
235 r = os_random_long(&td->bsrange_state);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100236 if (!td->o.bssplit_nr) {
237 buflen = (unsigned int)
238 (1 + (double) (td->o.max_bs[ddir] - 1)
239 * r / (RAND_MAX + 1.0));
240 } else {
Jens Axboe564ca972007-12-14 12:21:19 +0100241 long perc = 0;
242 unsigned int i;
243
244 for (i = 0; i < td->o.bssplit_nr; i++) {
245 struct bssplit *bsp = &td->o.bssplit[i];
246
247 buflen = bsp->bs;
248 perc += bsp->perc;
249 if (r <= ((LONG_MAX / 100L) * perc))
250 break;
251 }
252 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100253 if (!td->o.bs_unaligned) {
254 buflen = (buflen + td->o.min_bs[ddir] - 1)
255 & ~(td->o.min_bs[ddir] - 1);
256 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200257 }
258
Jens Axboe4ba66132008-02-05 10:05:50 +0100259 if (io_u->offset + buflen > io_u->file->real_file_size) {
260 dprint(FD_IO, "lower buflen %u -> %u (ddir=%d)\n", buflen,
261 td->o.min_bs[ddir], ddir);
Jens Axboe6a5e6882007-07-26 10:47:51 +0200262 buflen = td->o.min_bs[ddir];
Jens Axboe4ba66132008-02-05 10:05:50 +0100263 }
Jens Axboe6a5e6882007-07-26 10:47:51 +0200264
Jens Axboe10ba5352006-10-20 11:39:27 +0200265 return buflen;
266}
267
Jens Axboeafe24a52007-03-16 20:27:27 +0100268static void set_rwmix_bytes(struct thread_data *td)
269{
Jens Axboeafe24a52007-03-16 20:27:27 +0100270 unsigned int diff;
271
272 /*
273 * we do time or byte based switch. this is needed because
274 * buffered writes may issue a lot quicker than they complete,
275 * whereas reads do not.
276 */
Jens Axboee47f7992007-03-21 14:05:39 +0100277 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
Jens Axboe04c540d2008-05-28 10:35:26 +0200278 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
Jens Axboee47f7992007-03-21 14:05:39 +0100279}
280
281static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
282{
283 unsigned int v;
284 long r;
285
286 r = os_random_long(&td->rwmix_state);
287 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
Jens Axboe04c540d2008-05-28 10:35:26 +0200288 if (v <= td->o.rwmix[DDIR_READ])
Jens Axboee47f7992007-03-21 14:05:39 +0100289 return DDIR_READ;
290
291 return DDIR_WRITE;
Jens Axboeafe24a52007-03-16 20:27:27 +0100292}
293
Jens Axboe10ba5352006-10-20 11:39:27 +0200294/*
295 * Return the data direction for the next io_u. If the job is a
296 * mixed read/write workload, check the rwmix cycle and switch if
297 * necessary.
298 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100299static enum fio_ddir get_rw_ddir(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200300{
301 if (td_rw(td)) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200302 /*
303 * Check if it's time to seed a new data direction.
304 */
Jens Axboee4928662008-04-07 09:19:46 +0200305 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
Jens Axboee47f7992007-03-21 14:05:39 +0100306 unsigned long long max_bytes;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100307 enum fio_ddir ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200308
Jens Axboee47f7992007-03-21 14:05:39 +0100309 /*
310 * Put a top limit on how many bytes we do for
311 * one data direction, to avoid overflowing the
312 * ranges too much
313 */
314 ddir = get_rand_ddir(td);
315 max_bytes = td->this_io_bytes[ddir];
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100316 if (max_bytes >=
317 (td->o.size * td->o.rwmix[ddir] / 100)) {
Jens Axboee4928662008-04-07 09:19:46 +0200318 if (!td->rw_end_set[ddir])
Jens Axboe38d77ca2007-03-21 14:20:20 +0100319 td->rw_end_set[ddir] = 1;
Jens Axboee4928662008-04-07 09:19:46 +0200320
Jens Axboee47f7992007-03-21 14:05:39 +0100321 ddir ^= 1;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100322 }
Jens Axboee47f7992007-03-21 14:05:39 +0100323
324 if (ddir != td->rwmix_ddir)
325 set_rwmix_bytes(td);
326
327 td->rwmix_ddir = ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200328 }
329 return td->rwmix_ddir;
330 } else if (td_read(td))
331 return DDIR_READ;
332 else
333 return DDIR_WRITE;
334}
335
Jens Axboe60f2c652008-05-16 12:31:36 +0200336static void put_file_log(struct thread_data *td, struct fio_file *f)
337{
338 int ret = put_file(td, f);
339
340 if (ret)
341 td_verror(td, ret, "file close");
342}
343
Jens Axboe10ba5352006-10-20 11:39:27 +0200344void put_io_u(struct thread_data *td, struct io_u *io_u)
345{
Jens Axboe0c6e7512007-02-22 11:19:39 +0100346 assert((io_u->flags & IO_U_F_FREE) == 0);
347 io_u->flags |= IO_U_F_FREE;
348
Jens Axboe60f2c652008-05-16 12:31:36 +0200349 if (io_u->file)
350 put_file_log(td, io_u->file);
Jens Axboe2dbdab72007-04-26 10:21:15 +0200351
Jens Axboe10ba5352006-10-20 11:39:27 +0200352 io_u->file = NULL;
353 list_del(&io_u->list);
354 list_add(&io_u->list, &td->io_u_freelist);
355 td->cur_depth--;
356}
357
Jens Axboe755200a2007-02-19 13:08:12 +0100358void requeue_io_u(struct thread_data *td, struct io_u **io_u)
359{
360 struct io_u *__io_u = *io_u;
361
Jens Axboe465221b2008-05-30 22:07:49 +0200362 dprint(FD_IO, "requeue %p\n", __io_u);
363
Jens Axboe4d2e0f42007-02-24 13:31:57 +0100364 __io_u->flags |= IO_U_F_FREE;
Jens Axboee4f54ad2008-02-04 10:56:07 +0100365 if ((__io_u->flags & IO_U_F_FLIGHT) && (__io_u->ddir != DDIR_SYNC))
366 td->io_issues[__io_u->ddir]--;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100367
Jens Axboe4d2e0f42007-02-24 13:31:57 +0100368 __io_u->flags &= ~IO_U_F_FLIGHT;
369
Jens Axboe755200a2007-02-19 13:08:12 +0100370 list_del(&__io_u->list);
371 list_add_tail(&__io_u->list, &td->io_u_requeues);
372 td->cur_depth--;
373 *io_u = NULL;
374}
375
Jens Axboe9bf20612007-03-01 09:33:57 +0100376static int fill_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200377{
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200378 if (td->io_ops->flags & FIO_NOIO)
379 goto out;
380
Jens Axboe10ba5352006-10-20 11:39:27 +0200381 /*
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200382 * see if it's time to sync
383 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100384 if (td->o.fsync_blocks &&
385 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
386 td->io_issues[DDIR_WRITE] && should_fsync(td)) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200387 io_u->ddir = DDIR_SYNC;
Jens Axboec38e9462007-03-27 08:48:48 +0200388 goto out;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200389 }
390
Jens Axboea00735e2006-11-03 08:58:08 +0100391 io_u->ddir = get_rw_ddir(td);
392
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200393 /*
Jens Axboe48f5abd2007-07-20 13:25:04 +0200394 * See if it's time to switch to a new zone
395 */
396 if (td->zone_bytes >= td->o.zone_size) {
397 td->zone_bytes = 0;
398 io_u->file->last_pos += td->o.zone_skip;
399 td->io_skip_bytes += td->o.zone_skip;
400 }
401
402 /*
Jens Axboec685b5b2007-02-10 20:02:28 +0100403 * No log, let the seq/rand engine retrieve the next buflen and
404 * position.
Jens Axboe10ba5352006-10-20 11:39:27 +0200405 */
Jens Axboe2ba1c292008-02-01 13:16:38 +0100406 if (get_next_offset(td, io_u)) {
407 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100408 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100409 }
Jens Axboec685b5b2007-02-10 20:02:28 +0100410
Jens Axboe9bf20612007-03-01 09:33:57 +0100411 io_u->buflen = get_next_buflen(td, io_u);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100412 if (!io_u->buflen) {
413 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100414 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100415 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200416
Jens Axboe2ba1c292008-02-01 13:16:38 +0100417 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
418 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
Jens Axboe4ba66132008-02-05 10:05:50 +0100419 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
420 io_u->buflen, io_u->file->real_file_size);
Jens Axboe6a5e6882007-07-26 10:47:51 +0200421 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100422 }
Jens Axboe6a5e6882007-07-26 10:47:51 +0200423
Jens Axboebca4ed42007-02-12 05:13:23 +0100424 /*
425 * mark entry before potentially trimming io_u
426 */
Jens Axboe303032a2008-03-26 10:11:10 +0100427 if (td_random(td) && file_randommap(td, io_u->file))
Jens Axboe9bf20612007-03-01 09:33:57 +0100428 mark_random_map(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200429
Jens Axboebca4ed42007-02-12 05:13:23 +0100430 /*
431 * If using a write iolog, store this entry.
432 */
Jens Axboec38e9462007-03-27 08:48:48 +0200433out:
Jens Axboe2ba1c292008-02-01 13:16:38 +0100434 dprint_io_u(io_u, "fill_io_u");
Jens Axboed9d91e32008-01-31 13:25:42 +0100435 td->zone_bytes += io_u->buflen;
Jens Axboef29b25a2007-07-23 08:56:43 +0200436 log_io_u(td, io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100437 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200438}
439
Jens Axboe838bc702008-05-22 13:08:23 +0200440static void __io_u_mark_map(unsigned int *map, unsigned int nr)
441{
442 int index = 0;
443
444 switch (nr) {
445 default:
446 index = 6;
447 break;
448 case 33 ... 64:
449 index = 5;
450 break;
451 case 17 ... 32:
452 index = 4;
453 break;
454 case 9 ... 16:
455 index = 3;
456 break;
457 case 5 ... 8:
458 index = 2;
459 break;
460 case 1 ... 4:
461 index = 1;
462 case 0:
463 break;
464 }
465
466 map[index]++;
467}
468
469void io_u_mark_submit(struct thread_data *td, unsigned int nr)
470{
471 __io_u_mark_map(td->ts.io_u_submit, nr);
472 td->ts.total_submit++;
473}
474
475void io_u_mark_complete(struct thread_data *td, unsigned int nr)
476{
477 __io_u_mark_map(td->ts.io_u_complete, nr);
478 td->ts.total_complete++;
479}
480
Jens Axboed8005752008-05-15 09:49:09 +0200481void io_u_mark_depth(struct thread_data *td, unsigned int nr)
Jens Axboe71619dc2007-01-13 23:56:33 +0100482{
483 int index = 0;
484
485 switch (td->cur_depth) {
486 default:
Jens Axboea783e612007-06-19 09:50:28 +0200487 index = 6;
488 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100489 case 32 ... 63:
Jens Axboea783e612007-06-19 09:50:28 +0200490 index = 5;
491 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100492 case 16 ... 31:
Jens Axboea783e612007-06-19 09:50:28 +0200493 index = 4;
494 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100495 case 8 ... 15:
Jens Axboea783e612007-06-19 09:50:28 +0200496 index = 3;
497 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100498 case 4 ... 7:
Jens Axboea783e612007-06-19 09:50:28 +0200499 index = 2;
500 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100501 case 2 ... 3:
Jens Axboea783e612007-06-19 09:50:28 +0200502 index = 1;
Jens Axboe71619dc2007-01-13 23:56:33 +0100503 case 1:
504 break;
505 }
506
Jens Axboe3bec7ae2008-05-14 21:08:37 +0200507 td->ts.io_u_map[index] += nr;
Jens Axboe71619dc2007-01-13 23:56:33 +0100508}
509
Jens Axboe04a0fea2007-06-19 12:48:41 +0200510static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
511{
512 int index = 0;
513
514 assert(usec < 1000);
515
516 switch (usec) {
517 case 750 ... 999:
518 index = 9;
519 break;
520 case 500 ... 749:
521 index = 8;
522 break;
523 case 250 ... 499:
524 index = 7;
525 break;
526 case 100 ... 249:
527 index = 6;
528 break;
529 case 50 ... 99:
530 index = 5;
531 break;
532 case 20 ... 49:
533 index = 4;
534 break;
535 case 10 ... 19:
536 index = 3;
537 break;
538 case 4 ... 9:
539 index = 2;
540 break;
541 case 2 ... 3:
542 index = 1;
543 case 0 ... 1:
544 break;
545 }
546
547 assert(index < FIO_IO_U_LAT_U_NR);
548 td->ts.io_u_lat_u[index]++;
549}
550
551static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
Jens Axboeec118302007-02-17 04:38:20 +0100552{
553 int index = 0;
554
555 switch (msec) {
556 default:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200557 index = 11;
558 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100559 case 1000 ... 1999:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200560 index = 10;
561 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100562 case 750 ... 999:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200563 index = 9;
564 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100565 case 500 ... 749:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200566 index = 8;
567 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100568 case 250 ... 499:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200569 index = 7;
570 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100571 case 100 ... 249:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200572 index = 6;
573 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100574 case 50 ... 99:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200575 index = 5;
576 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100577 case 20 ... 49:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200578 index = 4;
579 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100580 case 10 ... 19:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200581 index = 3;
582 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100583 case 4 ... 9:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200584 index = 2;
585 break;
Jens Axboeec118302007-02-17 04:38:20 +0100586 case 2 ... 3:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200587 index = 1;
Jens Axboeec118302007-02-17 04:38:20 +0100588 case 0 ... 1:
589 break;
590 }
591
Jens Axboe04a0fea2007-06-19 12:48:41 +0200592 assert(index < FIO_IO_U_LAT_M_NR);
593 td->ts.io_u_lat_m[index]++;
594}
595
596static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
597{
598 if (usec < 1000)
599 io_u_mark_lat_usec(td, usec);
600 else
601 io_u_mark_lat_msec(td, usec / 1000);
Jens Axboeec118302007-02-17 04:38:20 +0100602}
603
Jens Axboe0aabe162007-02-23 08:45:55 +0100604/*
605 * Get next file to service by choosing one at random
606 */
Jens Axboe1c178182007-03-13 13:25:18 +0100607static struct fio_file *get_next_file_rand(struct thread_data *td, int goodf,
608 int badf)
Jens Axboe0aabe162007-02-23 08:45:55 +0100609{
Jens Axboe0aabe162007-02-23 08:45:55 +0100610 struct fio_file *f;
Jens Axboe1c178182007-03-13 13:25:18 +0100611 int fno;
Jens Axboe0aabe162007-02-23 08:45:55 +0100612
613 do {
Jens Axboe7c83c082007-03-01 10:04:15 +0100614 long r = os_random_long(&td->next_file_state);
615
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100616 fno = (unsigned int) ((double) td->o.nr_files
617 * (r / (RAND_MAX + 1.0)));
Jens Axboe126d65c2008-03-01 18:04:31 +0100618 f = td->files[fno];
Jens Axboe059e63c2007-03-27 20:34:47 +0200619 if (f->flags & FIO_FILE_DONE)
620 continue;
Jens Axboe1c178182007-03-13 13:25:18 +0100621
Jens Axboe2ba1c292008-02-01 13:16:38 +0100622 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
623 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
Jens Axboe0aabe162007-02-23 08:45:55 +0100624 return f;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100625 }
Jens Axboe0aabe162007-02-23 08:45:55 +0100626 } while (1);
627}
628
629/*
630 * Get next file to service by doing round robin between all available ones
631 */
Jens Axboe1c178182007-03-13 13:25:18 +0100632static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
633 int badf)
Jens Axboe3d7c3912007-02-19 13:16:12 +0100634{
635 unsigned int old_next_file = td->next_file;
636 struct fio_file *f;
637
638 do {
Jens Axboe126d65c2008-03-01 18:04:31 +0100639 f = td->files[td->next_file];
Jens Axboe3d7c3912007-02-19 13:16:12 +0100640
641 td->next_file++;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100642 if (td->next_file >= td->o.nr_files)
Jens Axboe3d7c3912007-02-19 13:16:12 +0100643 td->next_file = 0;
644
Jens Axboed5ed68e2007-03-28 09:33:43 +0200645 if (f->flags & FIO_FILE_DONE) {
646 f = NULL;
Jens Axboe059e63c2007-03-27 20:34:47 +0200647 continue;
Jens Axboed5ed68e2007-03-28 09:33:43 +0200648 }
Jens Axboe059e63c2007-03-27 20:34:47 +0200649
Jens Axboe1c178182007-03-13 13:25:18 +0100650 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
Jens Axboe3d7c3912007-02-19 13:16:12 +0100651 break;
652
653 f = NULL;
654 } while (td->next_file != old_next_file);
655
Jens Axboe2ba1c292008-02-01 13:16:38 +0100656 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
Jens Axboe3d7c3912007-02-19 13:16:12 +0100657 return f;
658}
659
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100660static struct fio_file *get_next_file(struct thread_data *td)
661{
Jens Axboe1907dbc2007-03-12 11:44:28 +0100662 struct fio_file *f;
663
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100664 assert(td->o.nr_files <= td->files_index);
Jens Axboe1c178182007-03-13 13:25:18 +0100665
Jens Axboe2ba1c292008-02-01 13:16:38 +0100666 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100667 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
668 " nr_files=%d\n", td->nr_open_files,
669 td->nr_done_files,
670 td->o.nr_files);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100671 return NULL;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100672 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100673
Jens Axboe1907dbc2007-03-12 11:44:28 +0100674 f = td->file_service_file;
Jens Axboef11bd942007-03-13 10:16:34 +0100675 if (f && (f->flags & FIO_FILE_OPEN) && td->file_service_left--)
Jens Axboe2ba1c292008-02-01 13:16:38 +0100676 goto out;
Jens Axboe1907dbc2007-03-12 11:44:28 +0100677
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100678 if (td->o.file_service_type == FIO_FSERVICE_RR)
Jens Axboe1c178182007-03-13 13:25:18 +0100679 f = get_next_file_rr(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100680 else
Jens Axboe1c178182007-03-13 13:25:18 +0100681 f = get_next_file_rand(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
Jens Axboe1907dbc2007-03-12 11:44:28 +0100682
683 td->file_service_file = f;
684 td->file_service_left = td->file_service_nr - 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100685out:
686 dprint(FD_FILE, "get_next_file: %p\n", f);
Jens Axboe1907dbc2007-03-12 11:44:28 +0100687 return f;
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100688}
689
Jens Axboe1c178182007-03-13 13:25:18 +0100690static struct fio_file *find_next_new_file(struct thread_data *td)
691{
692 struct fio_file *f;
693
Jens Axboe1020a132007-03-29 11:15:06 +0200694 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files)
695 return NULL;
696
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100697 if (td->o.file_service_type == FIO_FSERVICE_RR)
Jens Axboe1c178182007-03-13 13:25:18 +0100698 f = get_next_file_rr(td, 0, FIO_FILE_OPEN);
699 else
700 f = get_next_file_rand(td, 0, FIO_FILE_OPEN);
701
702 return f;
703}
704
Jens Axboe429f6672007-07-23 10:38:43 +0200705static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
706{
707 struct fio_file *f;
708
709 do {
710 f = get_next_file(td);
711 if (!f)
712 return 1;
713
714set_file:
715 io_u->file = f;
716 get_file(f);
717
718 if (!fill_io_u(td, io_u))
719 break;
720
721 /*
Jens Axboe009bd842008-05-15 10:19:46 +0200722 * optimization to prevent close/open of the same file. This
723 * way we preserve queueing etc.
724 */
725 if (td->o.nr_files == 1 && td->o.time_based) {
Jens Axboe60f2c652008-05-16 12:31:36 +0200726 put_file_log(td, f);
Jens Axboe009bd842008-05-15 10:19:46 +0200727 fio_file_reset(f);
728 goto set_file;
729 }
730
731 /*
Jens Axboe429f6672007-07-23 10:38:43 +0200732 * td_io_close() does a put_file() as well, so no need to
733 * do that here.
734 */
735 io_u->file = NULL;
736 td_io_close_file(td, f);
737 f->flags |= FIO_FILE_DONE;
738 td->nr_done_files++;
739
740 /*
741 * probably not the right place to do this, but see
742 * if we need to open a new file
743 */
744 if (td->nr_open_files < td->o.open_files &&
745 td->o.open_files != td->o.nr_files) {
746 f = find_next_new_file(td);
747
748 if (!f || td_io_open_file(td, f))
749 return 1;
750
751 goto set_file;
752 }
753 } while (1);
754
755 return 0;
756}
757
758
Jens Axboe10ba5352006-10-20 11:39:27 +0200759struct io_u *__get_io_u(struct thread_data *td)
760{
761 struct io_u *io_u = NULL;
762
Jens Axboe755200a2007-02-19 13:08:12 +0100763 if (!list_empty(&td->io_u_requeues))
764 io_u = list_entry(td->io_u_requeues.next, struct io_u, list);
765 else if (!queue_full(td)) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200766 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
767
Jens Axboe6040dab2006-10-24 19:38:15 +0200768 io_u->buflen = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200769 io_u->resid = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100770 io_u->file = NULL;
Jens Axboed7762cf2007-02-23 12:34:57 +0100771 io_u->end_io = NULL;
Jens Axboe755200a2007-02-19 13:08:12 +0100772 }
773
774 if (io_u) {
Jens Axboe0c6e7512007-02-22 11:19:39 +0100775 assert(io_u->flags & IO_U_F_FREE);
776 io_u->flags &= ~IO_U_F_FREE;
777
Jens Axboe755200a2007-02-19 13:08:12 +0100778 io_u->error = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200779 list_del(&io_u->list);
780 list_add(&io_u->list, &td->io_u_busylist);
781 td->cur_depth++;
782 }
783
784 return io_u;
785}
786
787/*
788 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
789 * etc. The returned io_u is fully ready to be prepped and submitted.
790 */
Jens Axboe3d7c3912007-02-19 13:16:12 +0100791struct io_u *get_io_u(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200792{
Jens Axboe3d7c3912007-02-19 13:16:12 +0100793 struct fio_file *f;
Jens Axboe10ba5352006-10-20 11:39:27 +0200794 struct io_u *io_u;
795
796 io_u = __get_io_u(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100797 if (!io_u) {
798 dprint(FD_IO, "__get_io_u failed\n");
Jens Axboe10ba5352006-10-20 11:39:27 +0200799 return NULL;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100800 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200801
Jens Axboe755200a2007-02-19 13:08:12 +0100802 /*
803 * from a requeue, io_u already setup
804 */
805 if (io_u->file)
Jens Axboe77f392b2007-02-19 20:13:09 +0100806 goto out;
Jens Axboe755200a2007-02-19 13:08:12 +0100807
Jens Axboe429f6672007-07-23 10:38:43 +0200808 /*
809 * If using an iolog, grab next piece if any available.
810 */
811 if (td->o.read_iolog_file) {
812 if (read_iolog_get(td, io_u))
813 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100814 } else if (set_io_u_file(td, io_u)) {
815 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
Jens Axboe429f6672007-07-23 10:38:43 +0200816 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100817 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100818
Jens Axboe429f6672007-07-23 10:38:43 +0200819 f = io_u->file;
820 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe97af62c2007-05-22 11:12:13 +0200821
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200822 if (io_u->ddir != DDIR_SYNC) {
Jens Axboed0656a92008-02-01 18:33:23 +0100823 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
Jens Axboe2ba1c292008-02-01 13:16:38 +0100824 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
Jens Axboe429f6672007-07-23 10:38:43 +0200825 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100826 }
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200827
Jens Axboe36167d82007-02-18 05:41:31 +0100828 f->last_pos = io_u->offset + io_u->buflen;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200829
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100830 if (td->o.verify != VERIFY_NONE)
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200831 populate_verify_io_u(td, io_u);
Jens Axboee4dad9c2008-05-28 10:53:44 +0200832 else if (td->o.refill_buffers && io_u->ddir == DDIR_WRITE)
833 io_u_fill_buffer(td, io_u, io_u->xfer_buflen);
Jens Axboe10ba5352006-10-20 11:39:27 +0200834 }
835
Jens Axboe165faf12007-02-07 11:30:37 +0100836 /*
837 * Set io data pointers.
838 */
Jens Axboed460eb32007-04-16 21:39:33 +0200839 io_u->endpos = io_u->offset + io_u->buflen;
Jens Axboecec6b552007-02-06 20:15:38 +0100840 io_u->xfer_buf = io_u->buf;
841 io_u->xfer_buflen = io_u->buflen;
Jens Axboe5973caf2008-05-21 19:52:35 +0200842
Jens Axboe6ac7a332008-03-01 15:22:32 +0100843out:
Jens Axboe429f6672007-07-23 10:38:43 +0200844 if (!td_io_prep(td, io_u)) {
845 fio_gettime(&io_u->start_time, NULL);
846 return io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100847 }
Jens Axboe429f6672007-07-23 10:38:43 +0200848err_put:
Jens Axboe2ba1c292008-02-01 13:16:38 +0100849 dprint(FD_IO, "get_io_u failed\n");
Jens Axboe429f6672007-07-23 10:38:43 +0200850 put_io_u(td, io_u);
851 return NULL;
Jens Axboe10ba5352006-10-20 11:39:27 +0200852}
853
Jens Axboe54517922007-03-05 10:06:06 +0100854void io_u_log_error(struct thread_data *td, struct io_u *io_u)
855{
856 const char *msg[] = { "read", "write", "sync" };
857
858 log_err("fio: io_u error");
859
860 if (io_u->file)
861 log_err(" on file %s", io_u->file->file_name);
862
863 log_err(": %s\n", strerror(io_u->error));
864
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100865 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
866 io_u->offset, io_u->xfer_buflen);
Jens Axboe54517922007-03-05 10:06:06 +0100867
868 if (!td->error)
869 td_verror(td, io_u->error, "io_u error");
870}
871
Jens Axboe97601022007-02-18 12:47:29 +0100872static void io_completed(struct thread_data *td, struct io_u *io_u,
873 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200874{
Jens Axboed85f5112007-06-18 09:41:23 +0200875 unsigned long usec;
Jens Axboe10ba5352006-10-20 11:39:27 +0200876
Jens Axboe2ba1c292008-02-01 13:16:38 +0100877 dprint_io_u(io_u, "io complete");
878
Jens Axboe0c6e7512007-02-22 11:19:39 +0100879 assert(io_u->flags & IO_U_F_FLIGHT);
880 io_u->flags &= ~IO_U_F_FLIGHT;
881
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200882 if (io_u->ddir == DDIR_SYNC) {
883 td->last_was_sync = 1;
884 return;
885 }
886
887 td->last_was_sync = 0;
888
Jens Axboe10ba5352006-10-20 11:39:27 +0200889 if (!io_u->error) {
890 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100891 const enum fio_ddir idx = io_u->ddir;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100892 int ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200893
894 td->io_blocks[idx]++;
895 td->io_bytes[idx] += bytes;
Jens Axboe10ba5352006-10-20 11:39:27 +0200896 td->this_io_bytes[idx] += bytes;
897
Jens Axboed85f5112007-06-18 09:41:23 +0200898 usec = utime_since(&io_u->issue_time, &icd->time);
Jens Axboe10ba5352006-10-20 11:39:27 +0200899
Jens Axboed85f5112007-06-18 09:41:23 +0200900 add_clat_sample(td, idx, usec);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100901 add_bw_sample(td, idx, &icd->time);
Jens Axboe04a0fea2007-06-19 12:48:41 +0200902 io_u_mark_latency(td, usec);
Jens Axboe10ba5352006-10-20 11:39:27 +0200903
Jens Axboe660a1cb2007-04-17 15:37:47 +0200904 if (td_write(td) && idx == DDIR_WRITE &&
Shawn Lewis86705792007-11-21 09:35:41 +0100905 td->o.do_verify &&
Jens Axboe41128402007-03-27 08:32:48 +0200906 td->o.verify != VERIFY_NONE)
Jens Axboe10ba5352006-10-20 11:39:27 +0200907 log_io_piece(td, io_u);
908
909 icd->bytes_done[idx] += bytes;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100910
Jens Axboed7762cf2007-02-23 12:34:57 +0100911 if (io_u->end_io) {
Jens Axboe36690c92007-03-26 10:23:34 +0200912 ret = io_u->end_io(td, io_u);
Jens Axboe3af6ef32007-02-18 06:57:43 +0100913 if (ret && !icd->error)
914 icd->error = ret;
915 }
Jens Axboe54517922007-03-05 10:06:06 +0100916 } else {
Jens Axboe10ba5352006-10-20 11:39:27 +0200917 icd->error = io_u->error;
Jens Axboe54517922007-03-05 10:06:06 +0100918 io_u_log_error(td, io_u);
919 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200920}
921
Jens Axboed7762cf2007-02-23 12:34:57 +0100922static void init_icd(struct io_completion_data *icd, int nr)
Jens Axboe36167d82007-02-18 05:41:31 +0100923{
924 fio_gettime(&icd->time, NULL);
925
Jens Axboe3af6ef32007-02-18 06:57:43 +0100926 icd->nr = nr;
927
Jens Axboe36167d82007-02-18 05:41:31 +0100928 icd->error = 0;
929 icd->bytes_done[0] = icd->bytes_done[1] = 0;
930}
931
Jens Axboe97601022007-02-18 12:47:29 +0100932static void ios_completed(struct thread_data *td,
933 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200934{
935 struct io_u *io_u;
936 int i;
937
Jens Axboe10ba5352006-10-20 11:39:27 +0200938 for (i = 0; i < icd->nr; i++) {
939 io_u = td->io_ops->event(td, i);
940
941 io_completed(td, io_u, icd);
942 put_io_u(td, io_u);
943 }
944}
Jens Axboe97601022007-02-18 12:47:29 +0100945
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100946/*
947 * Complete a single io_u for the sync engines.
948 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100949long io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
Jens Axboe97601022007-02-18 12:47:29 +0100950{
951 struct io_completion_data icd;
952
Jens Axboed7762cf2007-02-23 12:34:57 +0100953 init_icd(&icd, 1);
Jens Axboe97601022007-02-18 12:47:29 +0100954 io_completed(td, io_u, &icd);
955 put_io_u(td, io_u);
956
957 if (!icd.error)
958 return icd.bytes_done[0] + icd.bytes_done[1];
959
Jens Axboe37e974a2007-03-02 15:16:45 +0100960 td_verror(td, icd.error, "io_u_sync_complete");
Jens Axboe97601022007-02-18 12:47:29 +0100961 return -1;
962}
963
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100964/*
965 * Called to complete min_events number of io for the async engines.
966 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100967long io_u_queued_complete(struct thread_data *td, int min_events)
Jens Axboe97601022007-02-18 12:47:29 +0100968{
Jens Axboe97601022007-02-18 12:47:29 +0100969 struct io_completion_data icd;
Jens Axboe00de55e2007-02-20 10:45:57 +0100970 struct timespec *tvp = NULL;
Jens Axboe97601022007-02-18 12:47:29 +0100971 int ret;
Davide Libenzi4d06a332007-03-22 07:43:50 +0100972 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
Jens Axboe97601022007-02-18 12:47:29 +0100973
Jens Axboeb271fe62008-02-04 10:49:41 +0100974 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_events);
975
Davide Libenzi4d06a332007-03-22 07:43:50 +0100976 if (!min_events)
Jens Axboe00de55e2007-02-20 10:45:57 +0100977 tvp = &ts;
Jens Axboe97601022007-02-18 12:47:29 +0100978
Jens Axboe00de55e2007-02-20 10:45:57 +0100979 ret = td_io_getevents(td, min_events, td->cur_depth, tvp);
Jens Axboe97601022007-02-18 12:47:29 +0100980 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100981 td_verror(td, -ret, "td_io_getevents");
Jens Axboe97601022007-02-18 12:47:29 +0100982 return ret;
983 } else if (!ret)
984 return ret;
985
Jens Axboed7762cf2007-02-23 12:34:57 +0100986 init_icd(&icd, ret);
Jens Axboe97601022007-02-18 12:47:29 +0100987 ios_completed(td, &icd);
988 if (!icd.error)
989 return icd.bytes_done[0] + icd.bytes_done[1];
990
Jens Axboe37e974a2007-03-02 15:16:45 +0100991 td_verror(td, icd.error, "io_u_queued_complete");
Jens Axboe97601022007-02-18 12:47:29 +0100992 return -1;
993}
Jens Axboe7e77dd02007-02-20 10:57:34 +0100994
995/*
996 * Call when io_u is really queued, to update the submission latency.
997 */
998void io_u_queued(struct thread_data *td, struct io_u *io_u)
999{
1000 unsigned long slat_time;
1001
Jens Axboed85f5112007-06-18 09:41:23 +02001002 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
Jens Axboe7e77dd02007-02-20 10:57:34 +01001003 add_slat_sample(td, io_u->ddir, slat_time);
1004}
Jens Axboe433afcb2007-02-22 10:39:01 +01001005
Jens Axboe5973caf2008-05-21 19:52:35 +02001006/*
1007 * "randomly" fill the buffer contents
1008 */
1009void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1010 unsigned int max_bs)
1011{
1012 long *ptr = io_u->buf;
1013
1014 if (!td->o.zero_buffers) {
1015 while ((void *) ptr - io_u->buf < max_bs) {
1016 *ptr = rand() * GOLDEN_RATIO_PRIME;
1017 ptr++;
1018 }
1019 } else
1020 memset(ptr, 0, max_bs);
1021}
1022
Jens Axboe55bc9722007-02-22 11:30:05 +01001023#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +01001024void io_u_set_timeout(struct thread_data *td)
1025{
1026 assert(td->cur_depth);
1027
1028 td->timer.it_interval.tv_sec = 0;
1029 td->timer.it_interval.tv_usec = 0;
1030 td->timer.it_value.tv_sec = IO_U_TIMEOUT + IO_U_TIMEOUT_INC;
1031 td->timer.it_value.tv_usec = 0;
1032 setitimer(ITIMER_REAL, &td->timer, NULL);
1033 fio_gettime(&td->timeout_end, NULL);
1034}
Jens Axboe5945b9b2007-02-22 20:33:01 +01001035
1036static void io_u_dump(struct io_u *io_u)
1037{
1038 unsigned long t_start = mtime_since_now(&io_u->start_time);
1039 unsigned long t_issue = mtime_since_now(&io_u->issue_time);
1040
1041 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 +01001042 log_err(" buf=%p/%p, len=%lu/%lu, offset=%llu\n", io_u->buf,
1043 io_u->xfer_buf, io_u->buflen,
1044 io_u->xfer_buflen,
1045 io_u->offset);
Jens Axboe5945b9b2007-02-22 20:33:01 +01001046 log_err(" ddir=%d, fname=%s\n", io_u->ddir, io_u->file->file_name);
1047}
Jens Axboe55bc9722007-02-22 11:30:05 +01001048#else
1049void io_u_set_timeout(struct thread_data fio_unused *td)
1050{
1051}
1052#endif
Jens Axboe433afcb2007-02-22 10:39:01 +01001053
Jens Axboe55bc9722007-02-22 11:30:05 +01001054#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +01001055static void io_u_timeout_handler(int fio_unused sig)
1056{
1057 struct thread_data *td, *__td;
1058 pid_t pid = getpid();
Jens Axboe5945b9b2007-02-22 20:33:01 +01001059 struct list_head *entry;
1060 struct io_u *io_u;
Jens Axboe433afcb2007-02-22 10:39:01 +01001061 int i;
1062
1063 log_err("fio: io_u timeout\n");
1064
1065 /*
1066 * TLS would be nice...
1067 */
1068 td = NULL;
1069 for_each_td(__td, i) {
1070 if (__td->pid == pid) {
1071 td = __td;
1072 break;
1073 }
1074 }
1075
1076 if (!td) {
1077 log_err("fio: io_u timeout, can't find job\n");
1078 exit(1);
1079 }
1080
1081 if (!td->cur_depth) {
1082 log_err("fio: timeout without pending work?\n");
1083 return;
1084 }
1085
Jens Axboe15506d02007-03-15 15:04:43 +01001086 log_err("fio: io_u timeout: job=%s, pid=%d\n", td->o.name, td->pid);
Jens Axboe5945b9b2007-02-22 20:33:01 +01001087
1088 list_for_each(entry, &td->io_u_busylist) {
1089 io_u = list_entry(entry, struct io_u, list);
1090
1091 io_u_dump(io_u);
1092 }
1093
1094 td_verror(td, ETIMEDOUT, "io_u timeout");
Jens Axboe433afcb2007-02-22 10:39:01 +01001095 exit(1);
1096}
Jens Axboe55bc9722007-02-22 11:30:05 +01001097#endif
Jens Axboe433afcb2007-02-22 10:39:01 +01001098
1099void io_u_init_timeout(void)
1100{
Jens Axboe55bc9722007-02-22 11:30:05 +01001101#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +01001102 signal(SIGALRM, io_u_timeout_handler);
Jens Axboe55bc9722007-02-22 11:30:05 +01001103#endif
Jens Axboe433afcb2007-02-22 10:39:01 +01001104}