blob: 0d55c09ae59dd2c70ea60313dff4bc1ddfef29a6 [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 Axboe97601022007-02-18 12:47:29 +010011struct io_completion_data {
12 int nr; /* input */
Jens Axboe97601022007-02-18 12:47:29 +010013
14 int error; /* output */
15 unsigned long bytes_done[2]; /* output */
16 struct timeval time; /* output */
17};
18
Jens Axboe10ba5352006-10-20 11:39:27 +020019/*
20 * The ->file_map[] contains a map of blocks we have or have not done io
21 * to yet. Used to make sure we cover the entire range in a fair fashion.
22 */
Jens Axboeaec2de22008-04-24 12:44:42 +020023static int random_map_free(struct fio_file *f, const unsigned long long block)
Jens Axboe10ba5352006-10-20 11:39:27 +020024{
Jens Axboeaec2de22008-04-24 12:44:42 +020025 unsigned int idx = RAND_MAP_IDX(f, block);
26 unsigned int bit = RAND_MAP_BIT(f, block);
Jens Axboe10ba5352006-10-20 11:39:27 +020027
Jens Axboe84422ac2008-02-19 20:10:26 +010028 dprint(FD_RANDOM, "free: b=%llu, idx=%u, bit=%u\n", block, idx, bit);
29
Jens Axboede605662008-05-31 00:21:12 +020030 return (f->file_map[idx] & (1 << bit)) == 0;
Jens Axboe10ba5352006-10-20 11:39:27 +020031}
32
33/*
Jens Axboedf415582006-10-20 11:41:03 +020034 * Mark a given offset as used in the map.
35 */
Jens Axboe9bf20612007-03-01 09:33:57 +010036static void mark_random_map(struct thread_data *td, struct io_u *io_u)
Jens Axboedf415582006-10-20 11:41:03 +020037{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010038 unsigned int min_bs = td->o.rw_min_bs;
Jens Axboe9bf20612007-03-01 09:33:57 +010039 struct fio_file *f = io_u->file;
Jens Axboea00735e2006-11-03 08:58:08 +010040 unsigned long long block;
Jens Axboe3e3357b2008-06-02 09:53:05 +020041 unsigned int blocks, nr_blocks;
Jens Axboedf415582006-10-20 11:41:03 +020042
Jens Axboeb9c5b642008-02-18 14:13:08 +010043 block = (io_u->offset - f->file_offset) / (unsigned long long) min_bs;
Jens Axboec685b5b2007-02-10 20:02:28 +010044 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
Jens Axboe3e3357b2008-06-02 09:53:05 +020045 blocks = 0;
Jens Axboec685b5b2007-02-10 20:02:28 +010046
Jens Axboe3e3357b2008-06-02 09:53:05 +020047 while (nr_blocks) {
48 unsigned int this_blocks, mask;
Jens Axboedf415582006-10-20 11:41:03 +020049 unsigned int idx, bit;
50
Jens Axboe1e3d53a2007-03-22 19:01:48 +010051 /*
52 * If we have a mixed random workload, we may
53 * encounter blocks we already did IO to.
54 */
Jens Axboe8b113b42008-06-02 10:15:48 +020055 if ((td->o.ddir_nr == 1) && !random_map_free(f, block)) {
56 if (!blocks)
57 blocks = 1;
Jens Axboedf415582006-10-20 11:41:03 +020058 break;
Jens Axboe8b113b42008-06-02 10:15:48 +020059 }
Jens Axboedf415582006-10-20 11:41:03 +020060
Jens Axboeaec2de22008-04-24 12:44:42 +020061 idx = RAND_MAP_IDX(f, block);
62 bit = RAND_MAP_BIT(f, block);
Jens Axboedf415582006-10-20 11:41:03 +020063
Jens Axboe0032bf92007-02-13 17:39:56 +010064 fio_assert(td, idx < f->num_maps);
Jens Axboedf415582006-10-20 11:41:03 +020065
Jens Axboe3e3357b2008-06-02 09:53:05 +020066 this_blocks = nr_blocks;
67 if (this_blocks + bit > BLOCKS_PER_MAP)
68 this_blocks = BLOCKS_PER_MAP - bit;
69
70 if (this_blocks == BLOCKS_PER_MAP)
71 mask = -1U;
72 else
73 mask = ((1U << this_blocks) - 1) << bit;
74
Jens Axboe3e3357b2008-06-02 09:53:05 +020075 f->file_map[idx] |= mask;
76 nr_blocks -= this_blocks;
77 blocks += this_blocks;
Jens Axboec9dd34b2008-06-02 10:09:43 +020078 block += this_blocks;
Jens Axboedf415582006-10-20 11:41:03 +020079 }
80
Jens Axboea00735e2006-11-03 08:58:08 +010081 if ((blocks * min_bs) < io_u->buflen)
82 io_u->buflen = blocks * min_bs;
Jens Axboedf415582006-10-20 11:41:03 +020083}
84
Jens Axboe3e3357b2008-06-02 09:53:05 +020085static unsigned long long last_block(struct thread_data *td, struct fio_file *f,
86 enum fio_ddir ddir)
Jens Axboe2ba1c292008-02-01 13:16:38 +010087{
88 unsigned long long max_blocks;
Jens Axboed9dd70f2008-05-23 12:37:23 +020089 unsigned long long max_size;
Jens Axboe2ba1c292008-02-01 13:16:38 +010090
Jens Axboed9dd70f2008-05-23 12:37:23 +020091 /*
92 * Hmm, should we make sure that ->io_size <= ->real_file_size?
93 */
94 max_size = f->io_size;
95 if (max_size > f->real_file_size)
96 max_size = f->real_file_size;
97
98 max_blocks = max_size / (unsigned long long) td->o.min_bs[ddir];
Jens Axboe2ba1c292008-02-01 13:16:38 +010099 if (!max_blocks)
100 return 0;
101
Jens Axboe67778e82008-05-15 09:20:08 +0200102 return max_blocks;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100103}
104
Jens Axboedf415582006-10-20 11:41:03 +0200105/*
Jens Axboe10ba5352006-10-20 11:39:27 +0200106 * Return the next free block in the map.
107 */
108static int get_next_free_block(struct thread_data *td, struct fio_file *f,
Jens Axboe4ba66132008-02-05 10:05:50 +0100109 enum fio_ddir ddir, unsigned long long *b)
Jens Axboe10ba5352006-10-20 11:39:27 +0200110{
Jens Axboe84422ac2008-02-19 20:10:26 +0100111 unsigned long long min_bs = td->o.rw_min_bs;
Jens Axboe10ba5352006-10-20 11:39:27 +0200112 int i;
113
Jens Axboec685b5b2007-02-10 20:02:28 +0100114 i = f->last_free_lookup;
115 *b = (i * BLOCKS_PER_MAP);
Jens Axboe84422ac2008-02-19 20:10:26 +0100116 while ((*b) * min_bs < f->real_file_size) {
Jens Axboede605662008-05-31 00:21:12 +0200117 if (f->file_map[i] != (unsigned int) -1) {
Jens Axboe697a6062008-05-31 00:04:45 +0200118 *b += ffz(f->file_map[i]);
Jens Axboe4ba66132008-02-05 10:05:50 +0100119 if (*b > last_block(td, f, ddir))
Jens Axboe2ba1c292008-02-01 13:16:38 +0100120 break;
Jens Axboec685b5b2007-02-10 20:02:28 +0100121 f->last_free_lookup = i;
Jens Axboe10ba5352006-10-20 11:39:27 +0200122 return 0;
123 }
124
125 *b += BLOCKS_PER_MAP;
126 i++;
127 }
128
Jens Axboe2ba1c292008-02-01 13:16:38 +0100129 dprint(FD_IO, "failed finding a free block\n");
Jens Axboe10ba5352006-10-20 11:39:27 +0200130 return 1;
131}
132
Jens Axboeec4015d2007-03-23 08:04:27 +0100133static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
Jens Axboe4ba66132008-02-05 10:05:50 +0100134 enum fio_ddir ddir, unsigned long long *b)
Jens Axboeec4015d2007-03-23 08:04:27 +0100135{
Jens Axboe84422ac2008-02-19 20:10:26 +0100136 unsigned long long r;
Jens Axboeec4015d2007-03-23 08:04:27 +0100137 int loops = 5;
138
139 do {
140 r = os_random_long(&td->random_state);
Jens Axboe84422ac2008-02-19 20:10:26 +0100141 dprint(FD_RANDOM, "off rand %llu\n", r);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100142 *b = (last_block(td, f, ddir) - 1)
Jens Axboedc873b62008-06-04 20:13:04 +0200143 * (r / ((unsigned long long) OS_RAND_MAX + 1.0));
Jens Axboe2ba1c292008-02-01 13:16:38 +0100144
Jens Axboe43c63a72007-05-21 13:23:30 +0200145 /*
146 * if we are not maintaining a random map, we are done.
147 */
Jens Axboe303032a2008-03-26 10:11:10 +0100148 if (!file_randommap(td, f))
Jens Axboe43c63a72007-05-21 13:23:30 +0200149 return 0;
150
151 /*
Jens Axboe84422ac2008-02-19 20:10:26 +0100152 * calculate map offset and check if it's free
Jens Axboe43c63a72007-05-21 13:23:30 +0200153 */
Jens Axboeaec2de22008-04-24 12:44:42 +0200154 if (random_map_free(f, *b))
Jens Axboe43c63a72007-05-21 13:23:30 +0200155 return 0;
156
Jens Axboe84422ac2008-02-19 20:10:26 +0100157 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
158 *b);
Jens Axboe43c63a72007-05-21 13:23:30 +0200159 } while (--loops);
Jens Axboeec4015d2007-03-23 08:04:27 +0100160
161 /*
Jens Axboe43c63a72007-05-21 13:23:30 +0200162 * we get here, if we didn't suceed in looking up a block. generate
163 * a random start offset into the filemap, and find the first free
164 * block from there.
Jens Axboeec4015d2007-03-23 08:04:27 +0100165 */
Jens Axboe43c63a72007-05-21 13:23:30 +0200166 loops = 10;
167 do {
Jens Axboedc873b62008-06-04 20:13:04 +0200168 f->last_free_lookup = (f->num_maps - 1) *
169 (r / (OS_RAND_MAX + 1.0));
Jens Axboe4ba66132008-02-05 10:05:50 +0100170 if (!get_next_free_block(td, f, ddir, b))
Jens Axboe43c63a72007-05-21 13:23:30 +0200171 return 0;
Jens Axboeec4015d2007-03-23 08:04:27 +0100172
Jens Axboe43c63a72007-05-21 13:23:30 +0200173 r = os_random_long(&td->random_state);
174 } while (--loops);
175
176 /*
177 * that didn't work either, try exhaustive search from the start
178 */
179 f->last_free_lookup = 0;
Jens Axboe4ba66132008-02-05 10:05:50 +0100180 return get_next_free_block(td, f, ddir, b);
Jens Axboeec4015d2007-03-23 08:04:27 +0100181}
182
Jens Axboe10ba5352006-10-20 11:39:27 +0200183/*
184 * For random io, generate a random new block and see if it's used. Repeat
185 * until we find a free one. For sequential io, just return the end of
186 * the last io issued.
187 */
Jens Axboe9bf20612007-03-01 09:33:57 +0100188static int get_next_offset(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200189{
Jens Axboe9bf20612007-03-01 09:33:57 +0100190 struct fio_file *f = io_u->file;
Jens Axboeec4015d2007-03-23 08:04:27 +0100191 unsigned long long b;
Jens Axboe4ba66132008-02-05 10:05:50 +0100192 enum fio_ddir ddir = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200193
Jens Axboeec4015d2007-03-23 08:04:27 +0100194 if (td_random(td) && (td->o.ddir_nr && !--td->ddir_nr)) {
195 td->ddir_nr = td->o.ddir_nr;
Jens Axboe10ba5352006-10-20 11:39:27 +0200196
Jens Axboe4ba66132008-02-05 10:05:50 +0100197 if (get_next_rand_offset(td, f, ddir, &b))
Jens Axboebca4ed42007-02-12 05:13:23 +0100198 return 1;
Jens Axboe43063a12007-03-27 20:21:24 +0200199 } else {
ljzhang,Yaxin Hu,Jianchao Tang0c3d7682007-07-26 10:59:25 +0200200 if (f->last_pos >= f->real_file_size) {
Jens Axboe4ba66132008-02-05 10:05:50 +0100201 if (!td_random(td) ||
202 get_next_rand_offset(td, f, ddir, &b))
ljzhang,Yaxin Hu,Jianchao Tang0c3d7682007-07-26 10:59:25 +0200203 return 1;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200204 } else
Jens Axboe4ba66132008-02-05 10:05:50 +0100205 b = (f->last_pos - f->file_offset) / td->o.min_bs[ddir];
Jens Axboe43063a12007-03-27 20:21:24 +0200206 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200207
Jens Axboe009bd842008-05-15 10:19:46 +0200208 io_u->offset = b * td->o.min_bs[ddir];
209 if (io_u->offset >= f->io_size) {
210 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
211 io_u->offset, f->io_size);
212 return 1;
213 }
214
215 io_u->offset += f->file_offset;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100216 if (io_u->offset >= f->real_file_size) {
217 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
218 io_u->offset, f->real_file_size);
Jens Axboe10ba5352006-10-20 11:39:27 +0200219 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100220 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200221
222 return 0;
223}
224
Jens Axboef3059de2008-06-11 15:37:32 +0200225static inline int is_power_of_2(unsigned int val)
226{
227 return (val != 0 && ((val & (val - 1)) == 0));
228}
229
Jens Axboe9bf20612007-03-01 09:33:57 +0100230static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200231{
Jens Axboebca4ed42007-02-12 05:13:23 +0100232 const int ddir = io_u->ddir;
Jens Axboef3f552b2008-06-13 10:27:05 +0200233 unsigned int uninitialized_var(buflen);
Jens Axboef3059de2008-06-11 15:37:32 +0200234 unsigned int minbs, maxbs;
Jens Axboe10ba5352006-10-20 11:39:27 +0200235 long r;
236
Jens Axboef3059de2008-06-11 15:37:32 +0200237 minbs = td->o.min_bs[ddir];
238 maxbs = td->o.max_bs[ddir];
239
240 if (minbs == maxbs)
241 buflen = minbs;
Jens Axboe10ba5352006-10-20 11:39:27 +0200242 else {
243 r = os_random_long(&td->bsrange_state);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100244 if (!td->o.bssplit_nr) {
Jens Axboef3059de2008-06-11 15:37:32 +0200245 buflen = 1 + (unsigned int) ((double) maxbs *
246 (r / (OS_RAND_MAX + 1.0)));
247 if (buflen < minbs)
248 buflen = minbs;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100249 } else {
Jens Axboe564ca972007-12-14 12:21:19 +0100250 long perc = 0;
251 unsigned int i;
252
253 for (i = 0; i < td->o.bssplit_nr; i++) {
254 struct bssplit *bsp = &td->o.bssplit[i];
255
256 buflen = bsp->bs;
257 perc += bsp->perc;
Jens Axboef3059de2008-06-11 15:37:32 +0200258 if (r <= ((OS_RAND_MAX / 100L) * perc))
Jens Axboe564ca972007-12-14 12:21:19 +0100259 break;
260 }
261 }
Jens Axboef3059de2008-06-11 15:37:32 +0200262 if (!td->o.bs_unaligned && is_power_of_2(minbs))
263 buflen = (buflen + minbs - 1) & ~(minbs - 1);
Jens Axboe10ba5352006-10-20 11:39:27 +0200264 }
265
Jens Axboe4ba66132008-02-05 10:05:50 +0100266 if (io_u->offset + buflen > io_u->file->real_file_size) {
267 dprint(FD_IO, "lower buflen %u -> %u (ddir=%d)\n", buflen,
Jens Axboef3059de2008-06-11 15:37:32 +0200268 minbs, ddir);
269 buflen = minbs;
Jens Axboe4ba66132008-02-05 10:05:50 +0100270 }
Jens Axboe6a5e6882007-07-26 10:47:51 +0200271
Jens Axboe10ba5352006-10-20 11:39:27 +0200272 return buflen;
273}
274
Jens Axboeafe24a52007-03-16 20:27:27 +0100275static void set_rwmix_bytes(struct thread_data *td)
276{
Jens Axboeafe24a52007-03-16 20:27:27 +0100277 unsigned int diff;
278
279 /*
280 * we do time or byte based switch. this is needed because
281 * buffered writes may issue a lot quicker than they complete,
282 * whereas reads do not.
283 */
Jens Axboee47f7992007-03-21 14:05:39 +0100284 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
Jens Axboe04c540d2008-05-28 10:35:26 +0200285 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
Jens Axboee47f7992007-03-21 14:05:39 +0100286}
287
288static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
289{
290 unsigned int v;
291 long r;
292
293 r = os_random_long(&td->rwmix_state);
Jens Axboedc873b62008-06-04 20:13:04 +0200294 v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
Jens Axboe04c540d2008-05-28 10:35:26 +0200295 if (v <= td->o.rwmix[DDIR_READ])
Jens Axboee47f7992007-03-21 14:05:39 +0100296 return DDIR_READ;
297
298 return DDIR_WRITE;
Jens Axboeafe24a52007-03-16 20:27:27 +0100299}
300
Jens Axboe10ba5352006-10-20 11:39:27 +0200301/*
302 * Return the data direction for the next io_u. If the job is a
303 * mixed read/write workload, check the rwmix cycle and switch if
304 * necessary.
305 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100306static enum fio_ddir get_rw_ddir(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200307{
308 if (td_rw(td)) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200309 /*
310 * Check if it's time to seed a new data direction.
311 */
Jens Axboee4928662008-04-07 09:19:46 +0200312 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
Jens Axboee47f7992007-03-21 14:05:39 +0100313 unsigned long long max_bytes;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100314 enum fio_ddir ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200315
Jens Axboee47f7992007-03-21 14:05:39 +0100316 /*
317 * Put a top limit on how many bytes we do for
318 * one data direction, to avoid overflowing the
319 * ranges too much
320 */
321 ddir = get_rand_ddir(td);
322 max_bytes = td->this_io_bytes[ddir];
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100323 if (max_bytes >=
324 (td->o.size * td->o.rwmix[ddir] / 100)) {
Jens Axboe6162f522008-06-04 10:35:25 +0200325 if (!td->rw_end_set[ddir]) {
Jens Axboe38d77ca2007-03-21 14:20:20 +0100326 td->rw_end_set[ddir] = 1;
Jens Axboe6162f522008-06-04 10:35:25 +0200327 fio_gettime(&td->rw_end[ddir], NULL);
328 }
Jens Axboee4928662008-04-07 09:19:46 +0200329
Jens Axboee47f7992007-03-21 14:05:39 +0100330 ddir ^= 1;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100331 }
Jens Axboee47f7992007-03-21 14:05:39 +0100332
333 if (ddir != td->rwmix_ddir)
334 set_rwmix_bytes(td);
335
336 td->rwmix_ddir = ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200337 }
338 return td->rwmix_ddir;
339 } else if (td_read(td))
340 return DDIR_READ;
341 else
342 return DDIR_WRITE;
343}
344
Jens Axboe60f2c652008-05-16 12:31:36 +0200345static void put_file_log(struct thread_data *td, struct fio_file *f)
346{
347 int ret = put_file(td, f);
348
349 if (ret)
350 td_verror(td, ret, "file close");
351}
352
Jens Axboe10ba5352006-10-20 11:39:27 +0200353void put_io_u(struct thread_data *td, struct io_u *io_u)
354{
Jens Axboe0c6e7512007-02-22 11:19:39 +0100355 assert((io_u->flags & IO_U_F_FREE) == 0);
356 io_u->flags |= IO_U_F_FREE;
357
Jens Axboe60f2c652008-05-16 12:31:36 +0200358 if (io_u->file)
359 put_file_log(td, io_u->file);
Jens Axboe2dbdab72007-04-26 10:21:15 +0200360
Jens Axboe10ba5352006-10-20 11:39:27 +0200361 io_u->file = NULL;
Jens Axboe01743ee2008-06-02 12:19:19 +0200362 flist_del(&io_u->list);
363 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboe10ba5352006-10-20 11:39:27 +0200364 td->cur_depth--;
365}
366
Jens Axboe755200a2007-02-19 13:08:12 +0100367void requeue_io_u(struct thread_data *td, struct io_u **io_u)
368{
369 struct io_u *__io_u = *io_u;
370
Jens Axboe465221b2008-05-30 22:07:49 +0200371 dprint(FD_IO, "requeue %p\n", __io_u);
372
Jens Axboe4d2e0f42007-02-24 13:31:57 +0100373 __io_u->flags |= IO_U_F_FREE;
Jens Axboee4f54ad2008-02-04 10:56:07 +0100374 if ((__io_u->flags & IO_U_F_FLIGHT) && (__io_u->ddir != DDIR_SYNC))
375 td->io_issues[__io_u->ddir]--;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100376
Jens Axboe4d2e0f42007-02-24 13:31:57 +0100377 __io_u->flags &= ~IO_U_F_FLIGHT;
378
Jens Axboe01743ee2008-06-02 12:19:19 +0200379 flist_del(&__io_u->list);
380 flist_add_tail(&__io_u->list, &td->io_u_requeues);
Jens Axboe755200a2007-02-19 13:08:12 +0100381 td->cur_depth--;
382 *io_u = NULL;
383}
384
Jens Axboe9bf20612007-03-01 09:33:57 +0100385static int fill_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200386{
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200387 if (td->io_ops->flags & FIO_NOIO)
388 goto out;
389
Jens Axboe10ba5352006-10-20 11:39:27 +0200390 /*
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200391 * see if it's time to sync
392 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100393 if (td->o.fsync_blocks &&
394 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
395 td->io_issues[DDIR_WRITE] && should_fsync(td)) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200396 io_u->ddir = DDIR_SYNC;
Jens Axboec38e9462007-03-27 08:48:48 +0200397 goto out;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200398 }
399
Jens Axboea00735e2006-11-03 08:58:08 +0100400 io_u->ddir = get_rw_ddir(td);
401
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200402 /*
Jens Axboe48f5abd2007-07-20 13:25:04 +0200403 * See if it's time to switch to a new zone
404 */
405 if (td->zone_bytes >= td->o.zone_size) {
406 td->zone_bytes = 0;
407 io_u->file->last_pos += td->o.zone_skip;
408 td->io_skip_bytes += td->o.zone_skip;
409 }
410
411 /*
Jens Axboec685b5b2007-02-10 20:02:28 +0100412 * No log, let the seq/rand engine retrieve the next buflen and
413 * position.
Jens Axboe10ba5352006-10-20 11:39:27 +0200414 */
Jens Axboe2ba1c292008-02-01 13:16:38 +0100415 if (get_next_offset(td, io_u)) {
416 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100417 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100418 }
Jens Axboec685b5b2007-02-10 20:02:28 +0100419
Jens Axboe9bf20612007-03-01 09:33:57 +0100420 io_u->buflen = get_next_buflen(td, io_u);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100421 if (!io_u->buflen) {
422 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100423 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100424 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200425
Jens Axboe2ba1c292008-02-01 13:16:38 +0100426 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
427 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
Jens Axboe4ba66132008-02-05 10:05:50 +0100428 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
429 io_u->buflen, io_u->file->real_file_size);
Jens Axboe6a5e6882007-07-26 10:47:51 +0200430 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100431 }
Jens Axboe6a5e6882007-07-26 10:47:51 +0200432
Jens Axboebca4ed42007-02-12 05:13:23 +0100433 /*
434 * mark entry before potentially trimming io_u
435 */
Jens Axboe303032a2008-03-26 10:11:10 +0100436 if (td_random(td) && file_randommap(td, io_u->file))
Jens Axboe9bf20612007-03-01 09:33:57 +0100437 mark_random_map(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200438
Jens Axboebca4ed42007-02-12 05:13:23 +0100439 /*
440 * If using a write iolog, store this entry.
441 */
Jens Axboec38e9462007-03-27 08:48:48 +0200442out:
Jens Axboe2ba1c292008-02-01 13:16:38 +0100443 dprint_io_u(io_u, "fill_io_u");
Jens Axboed9d91e32008-01-31 13:25:42 +0100444 td->zone_bytes += io_u->buflen;
Jens Axboef29b25a2007-07-23 08:56:43 +0200445 log_io_u(td, io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100446 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200447}
448
Jens Axboe838bc702008-05-22 13:08:23 +0200449static void __io_u_mark_map(unsigned int *map, unsigned int nr)
450{
451 int index = 0;
452
453 switch (nr) {
454 default:
455 index = 6;
456 break;
457 case 33 ... 64:
458 index = 5;
459 break;
460 case 17 ... 32:
461 index = 4;
462 break;
463 case 9 ... 16:
464 index = 3;
465 break;
466 case 5 ... 8:
467 index = 2;
468 break;
469 case 1 ... 4:
470 index = 1;
471 case 0:
472 break;
473 }
474
475 map[index]++;
476}
477
478void io_u_mark_submit(struct thread_data *td, unsigned int nr)
479{
480 __io_u_mark_map(td->ts.io_u_submit, nr);
481 td->ts.total_submit++;
482}
483
484void io_u_mark_complete(struct thread_data *td, unsigned int nr)
485{
486 __io_u_mark_map(td->ts.io_u_complete, nr);
487 td->ts.total_complete++;
488}
489
Jens Axboed8005752008-05-15 09:49:09 +0200490void io_u_mark_depth(struct thread_data *td, unsigned int nr)
Jens Axboe71619dc2007-01-13 23:56:33 +0100491{
492 int index = 0;
493
494 switch (td->cur_depth) {
495 default:
Jens Axboea783e612007-06-19 09:50:28 +0200496 index = 6;
497 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100498 case 32 ... 63:
Jens Axboea783e612007-06-19 09:50:28 +0200499 index = 5;
500 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100501 case 16 ... 31:
Jens Axboea783e612007-06-19 09:50:28 +0200502 index = 4;
503 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100504 case 8 ... 15:
Jens Axboea783e612007-06-19 09:50:28 +0200505 index = 3;
506 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100507 case 4 ... 7:
Jens Axboea783e612007-06-19 09:50:28 +0200508 index = 2;
509 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100510 case 2 ... 3:
Jens Axboea783e612007-06-19 09:50:28 +0200511 index = 1;
Jens Axboe71619dc2007-01-13 23:56:33 +0100512 case 1:
513 break;
514 }
515
Jens Axboe3bec7ae2008-05-14 21:08:37 +0200516 td->ts.io_u_map[index] += nr;
Jens Axboe71619dc2007-01-13 23:56:33 +0100517}
518
Jens Axboe04a0fea2007-06-19 12:48:41 +0200519static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
520{
521 int index = 0;
522
523 assert(usec < 1000);
524
525 switch (usec) {
526 case 750 ... 999:
527 index = 9;
528 break;
529 case 500 ... 749:
530 index = 8;
531 break;
532 case 250 ... 499:
533 index = 7;
534 break;
535 case 100 ... 249:
536 index = 6;
537 break;
538 case 50 ... 99:
539 index = 5;
540 break;
541 case 20 ... 49:
542 index = 4;
543 break;
544 case 10 ... 19:
545 index = 3;
546 break;
547 case 4 ... 9:
548 index = 2;
549 break;
550 case 2 ... 3:
551 index = 1;
552 case 0 ... 1:
553 break;
554 }
555
556 assert(index < FIO_IO_U_LAT_U_NR);
557 td->ts.io_u_lat_u[index]++;
558}
559
560static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
Jens Axboeec118302007-02-17 04:38:20 +0100561{
562 int index = 0;
563
564 switch (msec) {
565 default:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200566 index = 11;
567 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100568 case 1000 ... 1999:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200569 index = 10;
570 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100571 case 750 ... 999:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200572 index = 9;
573 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100574 case 500 ... 749:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200575 index = 8;
576 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100577 case 250 ... 499:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200578 index = 7;
579 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100580 case 100 ... 249:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200581 index = 6;
582 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100583 case 50 ... 99:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200584 index = 5;
585 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100586 case 20 ... 49:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200587 index = 4;
588 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100589 case 10 ... 19:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200590 index = 3;
591 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100592 case 4 ... 9:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200593 index = 2;
594 break;
Jens Axboeec118302007-02-17 04:38:20 +0100595 case 2 ... 3:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200596 index = 1;
Jens Axboeec118302007-02-17 04:38:20 +0100597 case 0 ... 1:
598 break;
599 }
600
Jens Axboe04a0fea2007-06-19 12:48:41 +0200601 assert(index < FIO_IO_U_LAT_M_NR);
602 td->ts.io_u_lat_m[index]++;
603}
604
605static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
606{
607 if (usec < 1000)
608 io_u_mark_lat_usec(td, usec);
609 else
610 io_u_mark_lat_msec(td, usec / 1000);
Jens Axboeec118302007-02-17 04:38:20 +0100611}
612
Jens Axboe0aabe162007-02-23 08:45:55 +0100613/*
614 * Get next file to service by choosing one at random
615 */
Jens Axboe1c178182007-03-13 13:25:18 +0100616static struct fio_file *get_next_file_rand(struct thread_data *td, int goodf,
617 int badf)
Jens Axboe0aabe162007-02-23 08:45:55 +0100618{
Jens Axboe0aabe162007-02-23 08:45:55 +0100619 struct fio_file *f;
Jens Axboe1c178182007-03-13 13:25:18 +0100620 int fno;
Jens Axboe0aabe162007-02-23 08:45:55 +0100621
622 do {
Jens Axboe7c83c082007-03-01 10:04:15 +0100623 long r = os_random_long(&td->next_file_state);
624
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100625 fno = (unsigned int) ((double) td->o.nr_files
Jens Axboedc873b62008-06-04 20:13:04 +0200626 * (r / (OS_RAND_MAX + 1.0)));
Jens Axboe126d65c2008-03-01 18:04:31 +0100627 f = td->files[fno];
Jens Axboe059e63c2007-03-27 20:34:47 +0200628 if (f->flags & FIO_FILE_DONE)
629 continue;
Jens Axboe1c178182007-03-13 13:25:18 +0100630
Jens Axboe2ba1c292008-02-01 13:16:38 +0100631 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
632 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
Jens Axboe0aabe162007-02-23 08:45:55 +0100633 return f;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100634 }
Jens Axboe0aabe162007-02-23 08:45:55 +0100635 } while (1);
636}
637
638/*
639 * Get next file to service by doing round robin between all available ones
640 */
Jens Axboe1c178182007-03-13 13:25:18 +0100641static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
642 int badf)
Jens Axboe3d7c3912007-02-19 13:16:12 +0100643{
644 unsigned int old_next_file = td->next_file;
645 struct fio_file *f;
646
647 do {
Jens Axboe126d65c2008-03-01 18:04:31 +0100648 f = td->files[td->next_file];
Jens Axboe3d7c3912007-02-19 13:16:12 +0100649
650 td->next_file++;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100651 if (td->next_file >= td->o.nr_files)
Jens Axboe3d7c3912007-02-19 13:16:12 +0100652 td->next_file = 0;
653
Jens Axboed5ed68e2007-03-28 09:33:43 +0200654 if (f->flags & FIO_FILE_DONE) {
655 f = NULL;
Jens Axboe059e63c2007-03-27 20:34:47 +0200656 continue;
Jens Axboed5ed68e2007-03-28 09:33:43 +0200657 }
Jens Axboe059e63c2007-03-27 20:34:47 +0200658
Jens Axboe1c178182007-03-13 13:25:18 +0100659 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
Jens Axboe3d7c3912007-02-19 13:16:12 +0100660 break;
661
662 f = NULL;
663 } while (td->next_file != old_next_file);
664
Jens Axboe2ba1c292008-02-01 13:16:38 +0100665 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
Jens Axboe3d7c3912007-02-19 13:16:12 +0100666 return f;
667}
668
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100669static struct fio_file *get_next_file(struct thread_data *td)
670{
Jens Axboe1907dbc2007-03-12 11:44:28 +0100671 struct fio_file *f;
672
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100673 assert(td->o.nr_files <= td->files_index);
Jens Axboe1c178182007-03-13 13:25:18 +0100674
Jens Axboe2ba1c292008-02-01 13:16:38 +0100675 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100676 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
677 " nr_files=%d\n", td->nr_open_files,
678 td->nr_done_files,
679 td->o.nr_files);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100680 return NULL;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100681 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100682
Jens Axboe1907dbc2007-03-12 11:44:28 +0100683 f = td->file_service_file;
Jens Axboea086c252009-03-04 08:27:37 +0100684 if (f && (f->flags & FIO_FILE_OPEN)) {
685 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
686 goto out;
687 if (td->file_service_left--)
688 goto out;
689 }
Jens Axboe1907dbc2007-03-12 11:44:28 +0100690
Jens Axboea086c252009-03-04 08:27:37 +0100691 if (td->o.file_service_type == FIO_FSERVICE_RR ||
692 td->o.file_service_type == FIO_FSERVICE_SEQ)
Jens Axboe1c178182007-03-13 13:25:18 +0100693 f = get_next_file_rr(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100694 else
Jens Axboe1c178182007-03-13 13:25:18 +0100695 f = get_next_file_rand(td, FIO_FILE_OPEN, FIO_FILE_CLOSING);
Jens Axboe1907dbc2007-03-12 11:44:28 +0100696
697 td->file_service_file = f;
698 td->file_service_left = td->file_service_nr - 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100699out:
700 dprint(FD_FILE, "get_next_file: %p\n", f);
Jens Axboe1907dbc2007-03-12 11:44:28 +0100701 return f;
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100702}
703
Jens Axboe1c178182007-03-13 13:25:18 +0100704static struct fio_file *find_next_new_file(struct thread_data *td)
705{
706 struct fio_file *f;
707
Jens Axboe1020a132007-03-29 11:15:06 +0200708 if (!td->nr_open_files || td->nr_done_files >= td->o.nr_files)
709 return NULL;
710
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100711 if (td->o.file_service_type == FIO_FSERVICE_RR)
Jens Axboe1c178182007-03-13 13:25:18 +0100712 f = get_next_file_rr(td, 0, FIO_FILE_OPEN);
713 else
714 f = get_next_file_rand(td, 0, FIO_FILE_OPEN);
715
716 return f;
717}
718
Jens Axboe429f6672007-07-23 10:38:43 +0200719static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
720{
721 struct fio_file *f;
722
723 do {
724 f = get_next_file(td);
725 if (!f)
726 return 1;
727
728set_file:
729 io_u->file = f;
730 get_file(f);
731
732 if (!fill_io_u(td, io_u))
733 break;
734
735 /*
Jens Axboe009bd842008-05-15 10:19:46 +0200736 * optimization to prevent close/open of the same file. This
737 * way we preserve queueing etc.
738 */
739 if (td->o.nr_files == 1 && td->o.time_based) {
Jens Axboe60f2c652008-05-16 12:31:36 +0200740 put_file_log(td, f);
Jens Axboe009bd842008-05-15 10:19:46 +0200741 fio_file_reset(f);
742 goto set_file;
743 }
744
745 /*
Jens Axboe429f6672007-07-23 10:38:43 +0200746 * td_io_close() does a put_file() as well, so no need to
747 * do that here.
748 */
749 io_u->file = NULL;
750 td_io_close_file(td, f);
751 f->flags |= FIO_FILE_DONE;
752 td->nr_done_files++;
753
754 /*
755 * probably not the right place to do this, but see
756 * if we need to open a new file
757 */
758 if (td->nr_open_files < td->o.open_files &&
759 td->o.open_files != td->o.nr_files) {
760 f = find_next_new_file(td);
761
762 if (!f || td_io_open_file(td, f))
763 return 1;
764
765 goto set_file;
766 }
767 } while (1);
768
769 return 0;
770}
771
772
Jens Axboe10ba5352006-10-20 11:39:27 +0200773struct io_u *__get_io_u(struct thread_data *td)
774{
775 struct io_u *io_u = NULL;
776
Jens Axboe01743ee2008-06-02 12:19:19 +0200777 if (!flist_empty(&td->io_u_requeues))
778 io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
Jens Axboe755200a2007-02-19 13:08:12 +0100779 else if (!queue_full(td)) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200780 io_u = flist_entry(td->io_u_freelist.next, struct io_u, list);
Jens Axboe10ba5352006-10-20 11:39:27 +0200781
Jens Axboe6040dab2006-10-24 19:38:15 +0200782 io_u->buflen = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200783 io_u->resid = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100784 io_u->file = NULL;
Jens Axboed7762cf2007-02-23 12:34:57 +0100785 io_u->end_io = NULL;
Jens Axboe755200a2007-02-19 13:08:12 +0100786 }
787
788 if (io_u) {
Jens Axboe0c6e7512007-02-22 11:19:39 +0100789 assert(io_u->flags & IO_U_F_FREE);
790 io_u->flags &= ~IO_U_F_FREE;
791
Jens Axboe755200a2007-02-19 13:08:12 +0100792 io_u->error = 0;
Jens Axboe01743ee2008-06-02 12:19:19 +0200793 flist_del(&io_u->list);
794 flist_add(&io_u->list, &td->io_u_busylist);
Jens Axboe10ba5352006-10-20 11:39:27 +0200795 td->cur_depth++;
796 }
797
798 return io_u;
799}
800
801/*
802 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
803 * etc. The returned io_u is fully ready to be prepped and submitted.
804 */
Jens Axboe3d7c3912007-02-19 13:16:12 +0100805struct io_u *get_io_u(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200806{
Jens Axboe3d7c3912007-02-19 13:16:12 +0100807 struct fio_file *f;
Jens Axboe10ba5352006-10-20 11:39:27 +0200808 struct io_u *io_u;
809
810 io_u = __get_io_u(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100811 if (!io_u) {
812 dprint(FD_IO, "__get_io_u failed\n");
Jens Axboe10ba5352006-10-20 11:39:27 +0200813 return NULL;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100814 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200815
Jens Axboe755200a2007-02-19 13:08:12 +0100816 /*
817 * from a requeue, io_u already setup
818 */
819 if (io_u->file)
Jens Axboe77f392b2007-02-19 20:13:09 +0100820 goto out;
Jens Axboe755200a2007-02-19 13:08:12 +0100821
Jens Axboe429f6672007-07-23 10:38:43 +0200822 /*
823 * If using an iolog, grab next piece if any available.
824 */
825 if (td->o.read_iolog_file) {
826 if (read_iolog_get(td, io_u))
827 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100828 } else if (set_io_u_file(td, io_u)) {
829 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
Jens Axboe429f6672007-07-23 10:38:43 +0200830 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100831 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100832
Jens Axboe429f6672007-07-23 10:38:43 +0200833 f = io_u->file;
834 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe97af62c2007-05-22 11:12:13 +0200835
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200836 if (io_u->ddir != DDIR_SYNC) {
Jens Axboed0656a92008-02-01 18:33:23 +0100837 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
Jens Axboe2ba1c292008-02-01 13:16:38 +0100838 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
Jens Axboe429f6672007-07-23 10:38:43 +0200839 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100840 }
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200841
Jens Axboe36167d82007-02-18 05:41:31 +0100842 f->last_pos = io_u->offset + io_u->buflen;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200843
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100844 if (td->o.verify != VERIFY_NONE)
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200845 populate_verify_io_u(td, io_u);
Jens Axboee4dad9c2008-05-28 10:53:44 +0200846 else if (td->o.refill_buffers && io_u->ddir == DDIR_WRITE)
847 io_u_fill_buffer(td, io_u, io_u->xfer_buflen);
Jens Axboe10ba5352006-10-20 11:39:27 +0200848 }
849
Jens Axboe165faf12007-02-07 11:30:37 +0100850 /*
851 * Set io data pointers.
852 */
Jens Axboed460eb32007-04-16 21:39:33 +0200853 io_u->endpos = io_u->offset + io_u->buflen;
Jens Axboecec6b552007-02-06 20:15:38 +0100854 io_u->xfer_buf = io_u->buf;
855 io_u->xfer_buflen = io_u->buflen;
Jens Axboe5973caf2008-05-21 19:52:35 +0200856
Jens Axboe6ac7a332008-03-01 15:22:32 +0100857out:
Jens Axboe429f6672007-07-23 10:38:43 +0200858 if (!td_io_prep(td, io_u)) {
Jens Axboe993bf482008-11-14 13:04:53 +0100859 if (!td->o.disable_slat)
860 fio_gettime(&io_u->start_time, NULL);
Jens Axboe429f6672007-07-23 10:38:43 +0200861 return io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100862 }
Jens Axboe429f6672007-07-23 10:38:43 +0200863err_put:
Jens Axboe2ba1c292008-02-01 13:16:38 +0100864 dprint(FD_IO, "get_io_u failed\n");
Jens Axboe429f6672007-07-23 10:38:43 +0200865 put_io_u(td, io_u);
866 return NULL;
Jens Axboe10ba5352006-10-20 11:39:27 +0200867}
868
Jens Axboe54517922007-03-05 10:06:06 +0100869void io_u_log_error(struct thread_data *td, struct io_u *io_u)
870{
871 const char *msg[] = { "read", "write", "sync" };
872
873 log_err("fio: io_u error");
874
875 if (io_u->file)
876 log_err(" on file %s", io_u->file->file_name);
877
878 log_err(": %s\n", strerror(io_u->error));
879
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100880 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
881 io_u->offset, io_u->xfer_buflen);
Jens Axboe54517922007-03-05 10:06:06 +0100882
883 if (!td->error)
884 td_verror(td, io_u->error, "io_u error");
885}
886
Jens Axboe97601022007-02-18 12:47:29 +0100887static void io_completed(struct thread_data *td, struct io_u *io_u,
888 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200889{
Jens Axboedbad30b2008-12-05 13:17:16 +0100890 /*
891 * Older gcc's are too dumb to realize that usec is always used
892 * initialized, silence that warning.
893 */
894 unsigned long uninitialized_var(usec);
Jens Axboe10ba5352006-10-20 11:39:27 +0200895
Jens Axboe2ba1c292008-02-01 13:16:38 +0100896 dprint_io_u(io_u, "io complete");
897
Jens Axboe0c6e7512007-02-22 11:19:39 +0100898 assert(io_u->flags & IO_U_F_FLIGHT);
899 io_u->flags &= ~IO_U_F_FLIGHT;
900
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200901 if (io_u->ddir == DDIR_SYNC) {
902 td->last_was_sync = 1;
903 return;
904 }
905
906 td->last_was_sync = 0;
907
Jens Axboe10ba5352006-10-20 11:39:27 +0200908 if (!io_u->error) {
909 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100910 const enum fio_ddir idx = io_u->ddir;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200911 int ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200912
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200913 td->io_blocks[idx]++;
914 td->io_bytes[idx] += bytes;
915 td->this_io_bytes[idx] += bytes;
Jens Axboe10ba5352006-10-20 11:39:27 +0200916
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200917 if (ramp_time_over(td)) {
Jens Axboe9520ebb2008-10-16 21:03:27 +0200918 if (!td->o.disable_clat || !td->o.disable_bw)
919 usec = utime_since(&io_u->issue_time,
920 &icd->time);
Jens Axboe721938a2008-09-10 09:46:16 +0200921
Jens Axboe9520ebb2008-10-16 21:03:27 +0200922 if (!td->o.disable_clat) {
923 add_clat_sample(td, idx, usec);
924 io_u_mark_latency(td, usec);
925 }
926 if (!td->o.disable_bw)
927 add_bw_sample(td, idx, &icd->time);
Jens Axboe721938a2008-09-10 09:46:16 +0200928 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200929
Jens Axboe660a1cb2007-04-17 15:37:47 +0200930 if (td_write(td) && idx == DDIR_WRITE &&
Shawn Lewis86705792007-11-21 09:35:41 +0100931 td->o.do_verify &&
Jens Axboe41128402007-03-27 08:32:48 +0200932 td->o.verify != VERIFY_NONE)
Jens Axboe10ba5352006-10-20 11:39:27 +0200933 log_io_piece(td, io_u);
934
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200935 icd->bytes_done[idx] += bytes;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100936
Jens Axboed7762cf2007-02-23 12:34:57 +0100937 if (io_u->end_io) {
Jens Axboe36690c92007-03-26 10:23:34 +0200938 ret = io_u->end_io(td, io_u);
Jens Axboe3af6ef32007-02-18 06:57:43 +0100939 if (ret && !icd->error)
940 icd->error = ret;
941 }
Jens Axboe54517922007-03-05 10:06:06 +0100942 } else {
Jens Axboe10ba5352006-10-20 11:39:27 +0200943 icd->error = io_u->error;
Jens Axboe54517922007-03-05 10:06:06 +0100944 io_u_log_error(td, io_u);
945 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200946}
947
Jens Axboe9520ebb2008-10-16 21:03:27 +0200948static void init_icd(struct thread_data *td, struct io_completion_data *icd,
949 int nr)
Jens Axboe36167d82007-02-18 05:41:31 +0100950{
Jens Axboe9520ebb2008-10-16 21:03:27 +0200951 if (!td->o.disable_clat || !td->o.disable_bw)
952 fio_gettime(&icd->time, NULL);
Jens Axboe36167d82007-02-18 05:41:31 +0100953
Jens Axboe3af6ef32007-02-18 06:57:43 +0100954 icd->nr = nr;
955
Jens Axboe36167d82007-02-18 05:41:31 +0100956 icd->error = 0;
957 icd->bytes_done[0] = icd->bytes_done[1] = 0;
958}
959
Jens Axboe97601022007-02-18 12:47:29 +0100960static void ios_completed(struct thread_data *td,
961 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200962{
963 struct io_u *io_u;
964 int i;
965
Jens Axboe10ba5352006-10-20 11:39:27 +0200966 for (i = 0; i < icd->nr; i++) {
967 io_u = td->io_ops->event(td, i);
968
969 io_completed(td, io_u, icd);
970 put_io_u(td, io_u);
971 }
972}
Jens Axboe97601022007-02-18 12:47:29 +0100973
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100974/*
975 * Complete a single io_u for the sync engines.
976 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100977long io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
Jens Axboe97601022007-02-18 12:47:29 +0100978{
979 struct io_completion_data icd;
980
Jens Axboe9520ebb2008-10-16 21:03:27 +0200981 init_icd(td, &icd, 1);
Jens Axboe97601022007-02-18 12:47:29 +0100982 io_completed(td, io_u, &icd);
983 put_io_u(td, io_u);
984
985 if (!icd.error)
986 return icd.bytes_done[0] + icd.bytes_done[1];
987
Jens Axboe37e974a2007-03-02 15:16:45 +0100988 td_verror(td, icd.error, "io_u_sync_complete");
Jens Axboe97601022007-02-18 12:47:29 +0100989 return -1;
990}
991
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100992/*
993 * Called to complete min_events number of io for the async engines.
994 */
Jens Axboe49504212008-06-05 09:03:30 +0200995long io_u_queued_complete(struct thread_data *td, int min_evts)
Jens Axboe97601022007-02-18 12:47:29 +0100996{
Jens Axboe97601022007-02-18 12:47:29 +0100997 struct io_completion_data icd;
Jens Axboe00de55e2007-02-20 10:45:57 +0100998 struct timespec *tvp = NULL;
Jens Axboe97601022007-02-18 12:47:29 +0100999 int ret;
Davide Libenzi4d06a332007-03-22 07:43:50 +01001000 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
Jens Axboe97601022007-02-18 12:47:29 +01001001
Jens Axboe49504212008-06-05 09:03:30 +02001002 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
Jens Axboeb271fe62008-02-04 10:49:41 +01001003
Jens Axboe49504212008-06-05 09:03:30 +02001004 if (!min_evts)
Jens Axboe00de55e2007-02-20 10:45:57 +01001005 tvp = &ts;
Jens Axboe97601022007-02-18 12:47:29 +01001006
Jens Axboe49504212008-06-05 09:03:30 +02001007 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
Jens Axboe97601022007-02-18 12:47:29 +01001008 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001009 td_verror(td, -ret, "td_io_getevents");
Jens Axboe97601022007-02-18 12:47:29 +01001010 return ret;
1011 } else if (!ret)
1012 return ret;
1013
Jens Axboe9520ebb2008-10-16 21:03:27 +02001014 init_icd(td, &icd, ret);
Jens Axboe97601022007-02-18 12:47:29 +01001015 ios_completed(td, &icd);
1016 if (!icd.error)
1017 return icd.bytes_done[0] + icd.bytes_done[1];
1018
Jens Axboe37e974a2007-03-02 15:16:45 +01001019 td_verror(td, icd.error, "io_u_queued_complete");
Jens Axboe97601022007-02-18 12:47:29 +01001020 return -1;
1021}
Jens Axboe7e77dd02007-02-20 10:57:34 +01001022
1023/*
1024 * Call when io_u is really queued, to update the submission latency.
1025 */
1026void io_u_queued(struct thread_data *td, struct io_u *io_u)
1027{
Jens Axboe9520ebb2008-10-16 21:03:27 +02001028 if (!td->o.disable_slat) {
1029 unsigned long slat_time;
Jens Axboe7e77dd02007-02-20 10:57:34 +01001030
Jens Axboe9520ebb2008-10-16 21:03:27 +02001031 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
1032 add_slat_sample(td, io_u->ddir, slat_time);
1033 }
Jens Axboe7e77dd02007-02-20 10:57:34 +01001034}
Jens Axboe433afcb2007-02-22 10:39:01 +01001035
Jens Axboe5973caf2008-05-21 19:52:35 +02001036/*
1037 * "randomly" fill the buffer contents
1038 */
1039void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1040 unsigned int max_bs)
1041{
1042 long *ptr = io_u->buf;
1043
1044 if (!td->o.zero_buffers) {
1045 while ((void *) ptr - io_u->buf < max_bs) {
1046 *ptr = rand() * GOLDEN_RATIO_PRIME;
1047 ptr++;
1048 }
1049 } else
1050 memset(ptr, 0, max_bs);
1051}