blob: d12f370b2ff30c001c1978da232a60edd2a7d4d6 [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
Jens Axboe2b7a01d2009-03-11 11:00:13 +010098 max_blocks = max_size / (unsigned long long) td->o.ba[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);
Radha Ramachandran899c29f2009-04-22 08:17:42 +0200116 while ((*b) * min_bs < f->real_file_size &&
117 (*b) * min_bs < f->io_size) {
Jens Axboede605662008-05-31 00:21:12 +0200118 if (f->file_map[i] != (unsigned int) -1) {
Jens Axboe697a6062008-05-31 00:04:45 +0200119 *b += ffz(f->file_map[i]);
Jens Axboe4ba66132008-02-05 10:05:50 +0100120 if (*b > last_block(td, f, ddir))
Jens Axboe2ba1c292008-02-01 13:16:38 +0100121 break;
Jens Axboec685b5b2007-02-10 20:02:28 +0100122 f->last_free_lookup = i;
Jens Axboe10ba5352006-10-20 11:39:27 +0200123 return 0;
124 }
125
126 *b += BLOCKS_PER_MAP;
127 i++;
128 }
129
Jens Axboe2ba1c292008-02-01 13:16:38 +0100130 dprint(FD_IO, "failed finding a free block\n");
Jens Axboe10ba5352006-10-20 11:39:27 +0200131 return 1;
132}
133
Jens Axboeec4015d2007-03-23 08:04:27 +0100134static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
Jens Axboe4ba66132008-02-05 10:05:50 +0100135 enum fio_ddir ddir, unsigned long long *b)
Jens Axboeec4015d2007-03-23 08:04:27 +0100136{
Jens Axboe84422ac2008-02-19 20:10:26 +0100137 unsigned long long r;
Jens Axboeec4015d2007-03-23 08:04:27 +0100138 int loops = 5;
139
140 do {
141 r = os_random_long(&td->random_state);
Jens Axboe84422ac2008-02-19 20:10:26 +0100142 dprint(FD_RANDOM, "off rand %llu\n", r);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100143 *b = (last_block(td, f, ddir) - 1)
Jens Axboedc873b62008-06-04 20:13:04 +0200144 * (r / ((unsigned long long) OS_RAND_MAX + 1.0));
Jens Axboe2ba1c292008-02-01 13:16:38 +0100145
Jens Axboe43c63a72007-05-21 13:23:30 +0200146 /*
147 * if we are not maintaining a random map, we are done.
148 */
Jens Axboe303032a2008-03-26 10:11:10 +0100149 if (!file_randommap(td, f))
Jens Axboe43c63a72007-05-21 13:23:30 +0200150 return 0;
151
152 /*
Jens Axboe84422ac2008-02-19 20:10:26 +0100153 * calculate map offset and check if it's free
Jens Axboe43c63a72007-05-21 13:23:30 +0200154 */
Jens Axboeaec2de22008-04-24 12:44:42 +0200155 if (random_map_free(f, *b))
Jens Axboe43c63a72007-05-21 13:23:30 +0200156 return 0;
157
Jens Axboe84422ac2008-02-19 20:10:26 +0100158 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
159 *b);
Jens Axboe43c63a72007-05-21 13:23:30 +0200160 } while (--loops);
Jens Axboeec4015d2007-03-23 08:04:27 +0100161
162 /*
Jens Axboe43c63a72007-05-21 13:23:30 +0200163 * we get here, if we didn't suceed in looking up a block. generate
164 * a random start offset into the filemap, and find the first free
165 * block from there.
Jens Axboeec4015d2007-03-23 08:04:27 +0100166 */
Jens Axboe43c63a72007-05-21 13:23:30 +0200167 loops = 10;
168 do {
Jens Axboedc873b62008-06-04 20:13:04 +0200169 f->last_free_lookup = (f->num_maps - 1) *
170 (r / (OS_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 Axboe683023e2009-03-04 08:41:31 +0100198 if (get_next_rand_offset(td, f, ddir, &b)) {
199 dprint(FD_IO, "%s: getting rand offset failed\n",
200 f->file_name);
Jens Axboebca4ed42007-02-12 05:13:23 +0100201 return 1;
Jens Axboe683023e2009-03-04 08:41:31 +0100202 }
Jens Axboe43063a12007-03-27 20:21:24 +0200203 } else {
ljzhang,Yaxin Hu,Jianchao Tang0c3d7682007-07-26 10:59:25 +0200204 if (f->last_pos >= f->real_file_size) {
Jens Axboe4ba66132008-02-05 10:05:50 +0100205 if (!td_random(td) ||
Jens Axboe683023e2009-03-04 08:41:31 +0100206 get_next_rand_offset(td, f, ddir, &b)) {
207 dprint(FD_IO, "%s: pos %llu > size %llu\n",
208 f->file_name, f->last_pos,
209 f->real_file_size);
ljzhang,Yaxin Hu,Jianchao Tang0c3d7682007-07-26 10:59:25 +0200210 return 1;
Jens Axboe683023e2009-03-04 08:41:31 +0100211 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200212 } else
Jens Axboe4ba66132008-02-05 10:05:50 +0100213 b = (f->last_pos - f->file_offset) / td->o.min_bs[ddir];
Jens Axboe43063a12007-03-27 20:21:24 +0200214 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200215
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100216 io_u->offset = b * td->o.ba[ddir];
Jens Axboe009bd842008-05-15 10:19:46 +0200217 if (io_u->offset >= f->io_size) {
218 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
219 io_u->offset, f->io_size);
220 return 1;
221 }
222
223 io_u->offset += f->file_offset;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100224 if (io_u->offset >= f->real_file_size) {
225 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
226 io_u->offset, f->real_file_size);
Jens Axboe10ba5352006-10-20 11:39:27 +0200227 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100228 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200229
230 return 0;
231}
232
Jens Axboef3059de2008-06-11 15:37:32 +0200233static inline int is_power_of_2(unsigned int val)
234{
235 return (val != 0 && ((val & (val - 1)) == 0));
236}
237
Jens Axboe9bf20612007-03-01 09:33:57 +0100238static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200239{
Jens Axboebca4ed42007-02-12 05:13:23 +0100240 const int ddir = io_u->ddir;
Jens Axboef3f552b2008-06-13 10:27:05 +0200241 unsigned int uninitialized_var(buflen);
Jens Axboef3059de2008-06-11 15:37:32 +0200242 unsigned int minbs, maxbs;
Jens Axboe10ba5352006-10-20 11:39:27 +0200243 long r;
244
Jens Axboef3059de2008-06-11 15:37:32 +0200245 minbs = td->o.min_bs[ddir];
246 maxbs = td->o.max_bs[ddir];
247
248 if (minbs == maxbs)
249 buflen = minbs;
Jens Axboe10ba5352006-10-20 11:39:27 +0200250 else {
251 r = os_random_long(&td->bsrange_state);
Jens Axboe720e84a2009-04-21 08:29:55 +0200252 if (!td->o.bssplit_nr[ddir]) {
Jens Axboef3059de2008-06-11 15:37:32 +0200253 buflen = 1 + (unsigned int) ((double) maxbs *
254 (r / (OS_RAND_MAX + 1.0)));
255 if (buflen < minbs)
256 buflen = minbs;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100257 } else {
Jens Axboe564ca972007-12-14 12:21:19 +0100258 long perc = 0;
259 unsigned int i;
260
Jens Axboe720e84a2009-04-21 08:29:55 +0200261 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
262 struct bssplit *bsp = &td->o.bssplit[ddir][i];
Jens Axboe564ca972007-12-14 12:21:19 +0100263
264 buflen = bsp->bs;
265 perc += bsp->perc;
Jens Axboef3059de2008-06-11 15:37:32 +0200266 if (r <= ((OS_RAND_MAX / 100L) * perc))
Jens Axboe564ca972007-12-14 12:21:19 +0100267 break;
268 }
269 }
Jens Axboef3059de2008-06-11 15:37:32 +0200270 if (!td->o.bs_unaligned && is_power_of_2(minbs))
271 buflen = (buflen + minbs - 1) & ~(minbs - 1);
Jens Axboe10ba5352006-10-20 11:39:27 +0200272 }
273
Jens Axboe4ba66132008-02-05 10:05:50 +0100274 if (io_u->offset + buflen > io_u->file->real_file_size) {
275 dprint(FD_IO, "lower buflen %u -> %u (ddir=%d)\n", buflen,
Jens Axboef3059de2008-06-11 15:37:32 +0200276 minbs, ddir);
277 buflen = minbs;
Jens Axboe4ba66132008-02-05 10:05:50 +0100278 }
Jens Axboe6a5e6882007-07-26 10:47:51 +0200279
Jens Axboe10ba5352006-10-20 11:39:27 +0200280 return buflen;
281}
282
Jens Axboeafe24a52007-03-16 20:27:27 +0100283static void set_rwmix_bytes(struct thread_data *td)
284{
Jens Axboeafe24a52007-03-16 20:27:27 +0100285 unsigned int diff;
286
287 /*
288 * we do time or byte based switch. this is needed because
289 * buffered writes may issue a lot quicker than they complete,
290 * whereas reads do not.
291 */
Jens Axboee47f7992007-03-21 14:05:39 +0100292 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
Jens Axboe04c540d2008-05-28 10:35:26 +0200293 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
Jens Axboee47f7992007-03-21 14:05:39 +0100294}
295
296static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
297{
298 unsigned int v;
299 long r;
300
301 r = os_random_long(&td->rwmix_state);
Jens Axboedc873b62008-06-04 20:13:04 +0200302 v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
Jens Axboe04c540d2008-05-28 10:35:26 +0200303 if (v <= td->o.rwmix[DDIR_READ])
Jens Axboee47f7992007-03-21 14:05:39 +0100304 return DDIR_READ;
305
306 return DDIR_WRITE;
Jens Axboeafe24a52007-03-16 20:27:27 +0100307}
308
Jens Axboe10ba5352006-10-20 11:39:27 +0200309/*
310 * Return the data direction for the next io_u. If the job is a
311 * mixed read/write workload, check the rwmix cycle and switch if
312 * necessary.
313 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100314static enum fio_ddir get_rw_ddir(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200315{
316 if (td_rw(td)) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200317 /*
318 * Check if it's time to seed a new data direction.
319 */
Jens Axboee4928662008-04-07 09:19:46 +0200320 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
Jens Axboee47f7992007-03-21 14:05:39 +0100321 unsigned long long max_bytes;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100322 enum fio_ddir ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200323
Jens Axboee47f7992007-03-21 14:05:39 +0100324 /*
325 * Put a top limit on how many bytes we do for
326 * one data direction, to avoid overflowing the
327 * ranges too much
328 */
329 ddir = get_rand_ddir(td);
330 max_bytes = td->this_io_bytes[ddir];
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100331 if (max_bytes >=
332 (td->o.size * td->o.rwmix[ddir] / 100)) {
Jens Axboe6162f522008-06-04 10:35:25 +0200333 if (!td->rw_end_set[ddir]) {
Jens Axboe38d77ca2007-03-21 14:20:20 +0100334 td->rw_end_set[ddir] = 1;
Jens Axboe6162f522008-06-04 10:35:25 +0200335 fio_gettime(&td->rw_end[ddir], NULL);
336 }
Jens Axboee4928662008-04-07 09:19:46 +0200337
Jens Axboee47f7992007-03-21 14:05:39 +0100338 ddir ^= 1;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100339 }
Jens Axboee47f7992007-03-21 14:05:39 +0100340
341 if (ddir != td->rwmix_ddir)
342 set_rwmix_bytes(td);
343
344 td->rwmix_ddir = ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200345 }
346 return td->rwmix_ddir;
347 } else if (td_read(td))
348 return DDIR_READ;
349 else
350 return DDIR_WRITE;
351}
352
Jens Axboe60f2c652008-05-16 12:31:36 +0200353static void put_file_log(struct thread_data *td, struct fio_file *f)
354{
355 int ret = put_file(td, f);
356
357 if (ret)
358 td_verror(td, ret, "file close");
359}
360
Jens Axboe10ba5352006-10-20 11:39:27 +0200361void put_io_u(struct thread_data *td, struct io_u *io_u)
362{
Jens Axboe0c6e7512007-02-22 11:19:39 +0100363 assert((io_u->flags & IO_U_F_FREE) == 0);
364 io_u->flags |= IO_U_F_FREE;
365
Jens Axboe60f2c652008-05-16 12:31:36 +0200366 if (io_u->file)
367 put_file_log(td, io_u->file);
Jens Axboe2dbdab72007-04-26 10:21:15 +0200368
Jens Axboe10ba5352006-10-20 11:39:27 +0200369 io_u->file = NULL;
Jens Axboe01743ee2008-06-02 12:19:19 +0200370 flist_del(&io_u->list);
371 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboe10ba5352006-10-20 11:39:27 +0200372 td->cur_depth--;
373}
374
Jens Axboe755200a2007-02-19 13:08:12 +0100375void requeue_io_u(struct thread_data *td, struct io_u **io_u)
376{
377 struct io_u *__io_u = *io_u;
378
Jens Axboe465221b2008-05-30 22:07:49 +0200379 dprint(FD_IO, "requeue %p\n", __io_u);
380
Jens Axboe4d2e0f42007-02-24 13:31:57 +0100381 __io_u->flags |= IO_U_F_FREE;
Jens Axboee4f54ad2008-02-04 10:56:07 +0100382 if ((__io_u->flags & IO_U_F_FLIGHT) && (__io_u->ddir != DDIR_SYNC))
383 td->io_issues[__io_u->ddir]--;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100384
Jens Axboe4d2e0f42007-02-24 13:31:57 +0100385 __io_u->flags &= ~IO_U_F_FLIGHT;
386
Jens Axboe01743ee2008-06-02 12:19:19 +0200387 flist_del(&__io_u->list);
388 flist_add_tail(&__io_u->list, &td->io_u_requeues);
Jens Axboe755200a2007-02-19 13:08:12 +0100389 td->cur_depth--;
390 *io_u = NULL;
391}
392
Jens Axboe9bf20612007-03-01 09:33:57 +0100393static int fill_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200394{
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200395 if (td->io_ops->flags & FIO_NOIO)
396 goto out;
397
Jens Axboe10ba5352006-10-20 11:39:27 +0200398 /*
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200399 * see if it's time to sync
400 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100401 if (td->o.fsync_blocks &&
402 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
403 td->io_issues[DDIR_WRITE] && should_fsync(td)) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200404 io_u->ddir = DDIR_SYNC;
Jens Axboec38e9462007-03-27 08:48:48 +0200405 goto out;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200406 }
407
Jens Axboea00735e2006-11-03 08:58:08 +0100408 io_u->ddir = get_rw_ddir(td);
409
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200410 /*
Jens Axboe48f5abd2007-07-20 13:25:04 +0200411 * See if it's time to switch to a new zone
412 */
413 if (td->zone_bytes >= td->o.zone_size) {
414 td->zone_bytes = 0;
415 io_u->file->last_pos += td->o.zone_skip;
416 td->io_skip_bytes += td->o.zone_skip;
417 }
418
419 /*
Jens Axboec685b5b2007-02-10 20:02:28 +0100420 * No log, let the seq/rand engine retrieve the next buflen and
421 * position.
Jens Axboe10ba5352006-10-20 11:39:27 +0200422 */
Jens Axboe2ba1c292008-02-01 13:16:38 +0100423 if (get_next_offset(td, io_u)) {
424 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100425 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100426 }
Jens Axboec685b5b2007-02-10 20:02:28 +0100427
Jens Axboe9bf20612007-03-01 09:33:57 +0100428 io_u->buflen = get_next_buflen(td, io_u);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100429 if (!io_u->buflen) {
430 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100431 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100432 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200433
Jens Axboe2ba1c292008-02-01 13:16:38 +0100434 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
435 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
Jens Axboe4ba66132008-02-05 10:05:50 +0100436 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
437 io_u->buflen, io_u->file->real_file_size);
Jens Axboe6a5e6882007-07-26 10:47:51 +0200438 return 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100439 }
Jens Axboe6a5e6882007-07-26 10:47:51 +0200440
Jens Axboebca4ed42007-02-12 05:13:23 +0100441 /*
442 * mark entry before potentially trimming io_u
443 */
Jens Axboe303032a2008-03-26 10:11:10 +0100444 if (td_random(td) && file_randommap(td, io_u->file))
Jens Axboe9bf20612007-03-01 09:33:57 +0100445 mark_random_map(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200446
Jens Axboebca4ed42007-02-12 05:13:23 +0100447 /*
448 * If using a write iolog, store this entry.
449 */
Jens Axboec38e9462007-03-27 08:48:48 +0200450out:
Jens Axboe2ba1c292008-02-01 13:16:38 +0100451 dprint_io_u(io_u, "fill_io_u");
Jens Axboed9d91e32008-01-31 13:25:42 +0100452 td->zone_bytes += io_u->buflen;
Jens Axboef29b25a2007-07-23 08:56:43 +0200453 log_io_u(td, io_u);
Jens Axboebca4ed42007-02-12 05:13:23 +0100454 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200455}
456
Jens Axboe838bc702008-05-22 13:08:23 +0200457static void __io_u_mark_map(unsigned int *map, unsigned int nr)
458{
459 int index = 0;
460
461 switch (nr) {
462 default:
463 index = 6;
464 break;
465 case 33 ... 64:
466 index = 5;
467 break;
468 case 17 ... 32:
469 index = 4;
470 break;
471 case 9 ... 16:
472 index = 3;
473 break;
474 case 5 ... 8:
475 index = 2;
476 break;
477 case 1 ... 4:
478 index = 1;
479 case 0:
480 break;
481 }
482
483 map[index]++;
484}
485
486void io_u_mark_submit(struct thread_data *td, unsigned int nr)
487{
488 __io_u_mark_map(td->ts.io_u_submit, nr);
489 td->ts.total_submit++;
490}
491
492void io_u_mark_complete(struct thread_data *td, unsigned int nr)
493{
494 __io_u_mark_map(td->ts.io_u_complete, nr);
495 td->ts.total_complete++;
496}
497
Jens Axboed8005752008-05-15 09:49:09 +0200498void io_u_mark_depth(struct thread_data *td, unsigned int nr)
Jens Axboe71619dc2007-01-13 23:56:33 +0100499{
500 int index = 0;
501
502 switch (td->cur_depth) {
503 default:
Jens Axboea783e612007-06-19 09:50:28 +0200504 index = 6;
505 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100506 case 32 ... 63:
Jens Axboea783e612007-06-19 09:50:28 +0200507 index = 5;
508 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100509 case 16 ... 31:
Jens Axboea783e612007-06-19 09:50:28 +0200510 index = 4;
511 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100512 case 8 ... 15:
Jens Axboea783e612007-06-19 09:50:28 +0200513 index = 3;
514 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100515 case 4 ... 7:
Jens Axboea783e612007-06-19 09:50:28 +0200516 index = 2;
517 break;
Jens Axboe71619dc2007-01-13 23:56:33 +0100518 case 2 ... 3:
Jens Axboea783e612007-06-19 09:50:28 +0200519 index = 1;
Jens Axboe71619dc2007-01-13 23:56:33 +0100520 case 1:
521 break;
522 }
523
Jens Axboe3bec7ae2008-05-14 21:08:37 +0200524 td->ts.io_u_map[index] += nr;
Jens Axboe71619dc2007-01-13 23:56:33 +0100525}
526
Jens Axboe04a0fea2007-06-19 12:48:41 +0200527static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
528{
529 int index = 0;
530
531 assert(usec < 1000);
532
533 switch (usec) {
534 case 750 ... 999:
535 index = 9;
536 break;
537 case 500 ... 749:
538 index = 8;
539 break;
540 case 250 ... 499:
541 index = 7;
542 break;
543 case 100 ... 249:
544 index = 6;
545 break;
546 case 50 ... 99:
547 index = 5;
548 break;
549 case 20 ... 49:
550 index = 4;
551 break;
552 case 10 ... 19:
553 index = 3;
554 break;
555 case 4 ... 9:
556 index = 2;
557 break;
558 case 2 ... 3:
559 index = 1;
560 case 0 ... 1:
561 break;
562 }
563
564 assert(index < FIO_IO_U_LAT_U_NR);
565 td->ts.io_u_lat_u[index]++;
566}
567
568static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
Jens Axboeec118302007-02-17 04:38:20 +0100569{
570 int index = 0;
571
572 switch (msec) {
573 default:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200574 index = 11;
575 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100576 case 1000 ... 1999:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200577 index = 10;
578 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100579 case 750 ... 999:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200580 index = 9;
581 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100582 case 500 ... 749:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200583 index = 8;
584 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100585 case 250 ... 499:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200586 index = 7;
587 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100588 case 100 ... 249:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200589 index = 6;
590 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100591 case 50 ... 99:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200592 index = 5;
593 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100594 case 20 ... 49:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200595 index = 4;
596 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100597 case 10 ... 19:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200598 index = 3;
599 break;
Jens Axboe8abdce62007-02-21 10:22:55 +0100600 case 4 ... 9:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200601 index = 2;
602 break;
Jens Axboeec118302007-02-17 04:38:20 +0100603 case 2 ... 3:
Jens Axboe04a0fea2007-06-19 12:48:41 +0200604 index = 1;
Jens Axboeec118302007-02-17 04:38:20 +0100605 case 0 ... 1:
606 break;
607 }
608
Jens Axboe04a0fea2007-06-19 12:48:41 +0200609 assert(index < FIO_IO_U_LAT_M_NR);
610 td->ts.io_u_lat_m[index]++;
611}
612
613static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
614{
615 if (usec < 1000)
616 io_u_mark_lat_usec(td, usec);
617 else
618 io_u_mark_lat_msec(td, usec / 1000);
Jens Axboeec118302007-02-17 04:38:20 +0100619}
620
Jens Axboe0aabe162007-02-23 08:45:55 +0100621/*
622 * Get next file to service by choosing one at random
623 */
Jens Axboed6aed792009-06-03 08:41:15 +0200624static struct fio_file *get_next_file_rand(struct thread_data *td, enum fio_file_flags goodf,
625 enum fio_file_flags badf)
Jens Axboe0aabe162007-02-23 08:45:55 +0100626{
Jens Axboe0aabe162007-02-23 08:45:55 +0100627 struct fio_file *f;
Jens Axboe1c178182007-03-13 13:25:18 +0100628 int fno;
Jens Axboe0aabe162007-02-23 08:45:55 +0100629
630 do {
Jens Axboe7c83c082007-03-01 10:04:15 +0100631 long r = os_random_long(&td->next_file_state);
Jens Axboe87b10672009-03-04 09:39:47 +0100632 int opened = 0;
Jens Axboe7c83c082007-03-01 10:04:15 +0100633
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100634 fno = (unsigned int) ((double) td->o.nr_files
Jens Axboedc873b62008-06-04 20:13:04 +0200635 * (r / (OS_RAND_MAX + 1.0)));
Jens Axboe126d65c2008-03-01 18:04:31 +0100636 f = td->files[fno];
Jens Axboed6aed792009-06-03 08:41:15 +0200637 if (fio_file_done(f))
Jens Axboe059e63c2007-03-27 20:34:47 +0200638 continue;
Jens Axboe1c178182007-03-13 13:25:18 +0100639
Jens Axboed6aed792009-06-03 08:41:15 +0200640 if (!fio_file_open(f)) {
Jens Axboe87b10672009-03-04 09:39:47 +0100641 int err;
642
643 err = td_io_open_file(td, f);
644 if (err)
645 continue;
646 opened = 1;
647 }
648
Jens Axboe2ba1c292008-02-01 13:16:38 +0100649 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
650 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
Jens Axboe0aabe162007-02-23 08:45:55 +0100651 return f;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100652 }
Jens Axboe87b10672009-03-04 09:39:47 +0100653 if (opened)
654 td_io_close_file(td, f);
Jens Axboe0aabe162007-02-23 08:45:55 +0100655 } while (1);
656}
657
658/*
659 * Get next file to service by doing round robin between all available ones
660 */
Jens Axboe1c178182007-03-13 13:25:18 +0100661static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
662 int badf)
Jens Axboe3d7c3912007-02-19 13:16:12 +0100663{
664 unsigned int old_next_file = td->next_file;
665 struct fio_file *f;
666
667 do {
Jens Axboe87b10672009-03-04 09:39:47 +0100668 int opened = 0;
669
Jens Axboe126d65c2008-03-01 18:04:31 +0100670 f = td->files[td->next_file];
Jens Axboe3d7c3912007-02-19 13:16:12 +0100671
672 td->next_file++;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100673 if (td->next_file >= td->o.nr_files)
Jens Axboe3d7c3912007-02-19 13:16:12 +0100674 td->next_file = 0;
675
Jens Axboe87b10672009-03-04 09:39:47 +0100676 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
Jens Axboed6aed792009-06-03 08:41:15 +0200677 if (fio_file_done(f)) {
Jens Axboed5ed68e2007-03-28 09:33:43 +0200678 f = NULL;
Jens Axboe059e63c2007-03-27 20:34:47 +0200679 continue;
Jens Axboed5ed68e2007-03-28 09:33:43 +0200680 }
Jens Axboe059e63c2007-03-27 20:34:47 +0200681
Jens Axboed6aed792009-06-03 08:41:15 +0200682 if (!fio_file_open(f)) {
Jens Axboe87b10672009-03-04 09:39:47 +0100683 int err;
684
685 err = td_io_open_file(td, f);
Jens Axboeb5696bf2009-03-04 16:03:49 +0100686 if (err) {
687 dprint(FD_FILE, "error %d on open of %s\n",
688 err, f->file_name);
Jens Axboe87c27b42009-05-20 10:45:12 +0200689 f = NULL;
Jens Axboe87b10672009-03-04 09:39:47 +0100690 continue;
Jens Axboeb5696bf2009-03-04 16:03:49 +0100691 }
Jens Axboe87b10672009-03-04 09:39:47 +0100692 opened = 1;
693 }
694
Jens Axboeb5696bf2009-03-04 16:03:49 +0100695 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf, f->flags);
Jens Axboe1c178182007-03-13 13:25:18 +0100696 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
Jens Axboe3d7c3912007-02-19 13:16:12 +0100697 break;
698
Jens Axboe87b10672009-03-04 09:39:47 +0100699 if (opened)
700 td_io_close_file(td, f);
701
Jens Axboe3d7c3912007-02-19 13:16:12 +0100702 f = NULL;
703 } while (td->next_file != old_next_file);
704
Jens Axboe2ba1c292008-02-01 13:16:38 +0100705 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
Jens Axboe3d7c3912007-02-19 13:16:12 +0100706 return f;
707}
708
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100709static struct fio_file *get_next_file(struct thread_data *td)
710{
Jens Axboe1907dbc2007-03-12 11:44:28 +0100711 struct fio_file *f;
712
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100713 assert(td->o.nr_files <= td->files_index);
Jens Axboe1c178182007-03-13 13:25:18 +0100714
Jens Axboeb5696bf2009-03-04 16:03:49 +0100715 if (td->nr_done_files >= td->o.nr_files) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100716 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
717 " nr_files=%d\n", td->nr_open_files,
718 td->nr_done_files,
719 td->o.nr_files);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100720 return NULL;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100721 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100722
Jens Axboe1907dbc2007-03-12 11:44:28 +0100723 f = td->file_service_file;
Jens Axboed6aed792009-06-03 08:41:15 +0200724 if (f && fio_file_open(f) && !fio_file_closing(f)) {
Jens Axboea086c252009-03-04 08:27:37 +0100725 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
726 goto out;
727 if (td->file_service_left--)
728 goto out;
729 }
Jens Axboe1907dbc2007-03-12 11:44:28 +0100730
Jens Axboea086c252009-03-04 08:27:37 +0100731 if (td->o.file_service_type == FIO_FSERVICE_RR ||
732 td->o.file_service_type == FIO_FSERVICE_SEQ)
Jens Axboed6aed792009-06-03 08:41:15 +0200733 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100734 else
Jens Axboed6aed792009-06-03 08:41:15 +0200735 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
Jens Axboe1907dbc2007-03-12 11:44:28 +0100736
737 td->file_service_file = f;
738 td->file_service_left = td->file_service_nr - 1;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100739out:
Jens Axboe683023e2009-03-04 08:41:31 +0100740 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
Jens Axboe1907dbc2007-03-12 11:44:28 +0100741 return f;
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100742}
743
Jens Axboe429f6672007-07-23 10:38:43 +0200744static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
745{
746 struct fio_file *f;
747
748 do {
749 f = get_next_file(td);
750 if (!f)
751 return 1;
752
Jens Axboe429f6672007-07-23 10:38:43 +0200753 io_u->file = f;
754 get_file(f);
755
756 if (!fill_io_u(td, io_u))
757 break;
758
Jens Axboeb5696bf2009-03-04 16:03:49 +0100759 put_file_log(td, f);
Jens Axboe429f6672007-07-23 10:38:43 +0200760 td_io_close_file(td, f);
Jens Axboeb5696bf2009-03-04 16:03:49 +0100761 io_u->file = NULL;
Jens Axboed6aed792009-06-03 08:41:15 +0200762 fio_file_set_done(f);
Jens Axboe429f6672007-07-23 10:38:43 +0200763 td->nr_done_files++;
Jens Axboe87b10672009-03-04 09:39:47 +0100764 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name, td->nr_done_files, td->o.nr_files);
Jens Axboe429f6672007-07-23 10:38:43 +0200765 } while (1);
766
767 return 0;
768}
769
770
Jens Axboe10ba5352006-10-20 11:39:27 +0200771struct io_u *__get_io_u(struct thread_data *td)
772{
773 struct io_u *io_u = NULL;
774
Jens Axboe01743ee2008-06-02 12:19:19 +0200775 if (!flist_empty(&td->io_u_requeues))
776 io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
Jens Axboe755200a2007-02-19 13:08:12 +0100777 else if (!queue_full(td)) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200778 io_u = flist_entry(td->io_u_freelist.next, struct io_u, list);
Jens Axboe10ba5352006-10-20 11:39:27 +0200779
Jens Axboe6040dab2006-10-24 19:38:15 +0200780 io_u->buflen = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200781 io_u->resid = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100782 io_u->file = NULL;
Jens Axboed7762cf2007-02-23 12:34:57 +0100783 io_u->end_io = NULL;
Jens Axboe755200a2007-02-19 13:08:12 +0100784 }
785
786 if (io_u) {
Jens Axboe0c6e7512007-02-22 11:19:39 +0100787 assert(io_u->flags & IO_U_F_FREE);
788 io_u->flags &= ~IO_U_F_FREE;
789
Jens Axboe755200a2007-02-19 13:08:12 +0100790 io_u->error = 0;
Jens Axboe01743ee2008-06-02 12:19:19 +0200791 flist_del(&io_u->list);
792 flist_add(&io_u->list, &td->io_u_busylist);
Jens Axboe10ba5352006-10-20 11:39:27 +0200793 td->cur_depth++;
794 }
795
796 return io_u;
797}
798
799/*
800 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
801 * etc. The returned io_u is fully ready to be prepped and submitted.
802 */
Jens Axboe3d7c3912007-02-19 13:16:12 +0100803struct io_u *get_io_u(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200804{
Jens Axboe3d7c3912007-02-19 13:16:12 +0100805 struct fio_file *f;
Jens Axboe10ba5352006-10-20 11:39:27 +0200806 struct io_u *io_u;
807
808 io_u = __get_io_u(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100809 if (!io_u) {
810 dprint(FD_IO, "__get_io_u failed\n");
Jens Axboe10ba5352006-10-20 11:39:27 +0200811 return NULL;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100812 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200813
Jens Axboe755200a2007-02-19 13:08:12 +0100814 /*
815 * from a requeue, io_u already setup
816 */
817 if (io_u->file)
Jens Axboe77f392b2007-02-19 20:13:09 +0100818 goto out;
Jens Axboe755200a2007-02-19 13:08:12 +0100819
Jens Axboe429f6672007-07-23 10:38:43 +0200820 /*
821 * If using an iolog, grab next piece if any available.
822 */
823 if (td->o.read_iolog_file) {
824 if (read_iolog_get(td, io_u))
825 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100826 } else if (set_io_u_file(td, io_u)) {
827 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
Jens Axboe429f6672007-07-23 10:38:43 +0200828 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100829 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100830
Jens Axboe429f6672007-07-23 10:38:43 +0200831 f = io_u->file;
Jens Axboed6aed792009-06-03 08:41:15 +0200832 assert(fio_file_open(f));
Jens Axboe97af62c2007-05-22 11:12:13 +0200833
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200834 if (io_u->ddir != DDIR_SYNC) {
Jens Axboed0656a92008-02-01 18:33:23 +0100835 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
Jens Axboe2ba1c292008-02-01 13:16:38 +0100836 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
Jens Axboe429f6672007-07-23 10:38:43 +0200837 goto err_put;
Jens Axboe2ba1c292008-02-01 13:16:38 +0100838 }
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200839
Jens Axboe36167d82007-02-18 05:41:31 +0100840 f->last_pos = io_u->offset + io_u->buflen;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200841
Radha Ramachandranc311cd22009-04-28 08:12:56 +0200842 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_WRITE)
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200843 populate_verify_io_u(td, io_u);
Jens Axboee4dad9c2008-05-28 10:53:44 +0200844 else if (td->o.refill_buffers && io_u->ddir == DDIR_WRITE)
845 io_u_fill_buffer(td, io_u, io_u->xfer_buflen);
Jens Axboe10ba5352006-10-20 11:39:27 +0200846 }
847
Jens Axboe165faf12007-02-07 11:30:37 +0100848 /*
849 * Set io data pointers.
850 */
Jens Axboecec6b552007-02-06 20:15:38 +0100851 io_u->xfer_buf = io_u->buf;
852 io_u->xfer_buflen = io_u->buflen;
Jens Axboe5973caf2008-05-21 19:52:35 +0200853
Jens Axboe6ac7a332008-03-01 15:22:32 +0100854out:
Jens Axboe429f6672007-07-23 10:38:43 +0200855 if (!td_io_prep(td, io_u)) {
Jens Axboe993bf482008-11-14 13:04:53 +0100856 if (!td->o.disable_slat)
857 fio_gettime(&io_u->start_time, NULL);
Jens Axboe429f6672007-07-23 10:38:43 +0200858 return io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100859 }
Jens Axboe429f6672007-07-23 10:38:43 +0200860err_put:
Jens Axboe2ba1c292008-02-01 13:16:38 +0100861 dprint(FD_IO, "get_io_u failed\n");
Jens Axboe429f6672007-07-23 10:38:43 +0200862 put_io_u(td, io_u);
863 return NULL;
Jens Axboe10ba5352006-10-20 11:39:27 +0200864}
865
Jens Axboe54517922007-03-05 10:06:06 +0100866void io_u_log_error(struct thread_data *td, struct io_u *io_u)
867{
868 const char *msg[] = { "read", "write", "sync" };
869
870 log_err("fio: io_u error");
871
872 if (io_u->file)
873 log_err(" on file %s", io_u->file->file_name);
874
875 log_err(": %s\n", strerror(io_u->error));
876
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100877 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
878 io_u->offset, io_u->xfer_buflen);
Jens Axboe54517922007-03-05 10:06:06 +0100879
880 if (!td->error)
881 td_verror(td, io_u->error, "io_u error");
882}
883
Jens Axboe97601022007-02-18 12:47:29 +0100884static void io_completed(struct thread_data *td, struct io_u *io_u,
885 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200886{
Jens Axboedbad30b2008-12-05 13:17:16 +0100887 /*
888 * Older gcc's are too dumb to realize that usec is always used
889 * initialized, silence that warning.
890 */
891 unsigned long uninitialized_var(usec);
Jens Axboe10ba5352006-10-20 11:39:27 +0200892
Jens Axboe2ba1c292008-02-01 13:16:38 +0100893 dprint_io_u(io_u, "io complete");
894
Jens Axboe0c6e7512007-02-22 11:19:39 +0100895 assert(io_u->flags & IO_U_F_FLIGHT);
896 io_u->flags &= ~IO_U_F_FLIGHT;
897
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200898 if (io_u->ddir == DDIR_SYNC) {
899 td->last_was_sync = 1;
900 return;
901 }
902
903 td->last_was_sync = 0;
904
Jens Axboe10ba5352006-10-20 11:39:27 +0200905 if (!io_u->error) {
906 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100907 const enum fio_ddir idx = io_u->ddir;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200908 int ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200909
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200910 td->io_blocks[idx]++;
911 td->io_bytes[idx] += bytes;
912 td->this_io_bytes[idx] += bytes;
Jens Axboe10ba5352006-10-20 11:39:27 +0200913
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200914 if (ramp_time_over(td)) {
Jens Axboe9520ebb2008-10-16 21:03:27 +0200915 if (!td->o.disable_clat || !td->o.disable_bw)
916 usec = utime_since(&io_u->issue_time,
917 &icd->time);
Jens Axboe721938a2008-09-10 09:46:16 +0200918
Jens Axboe9520ebb2008-10-16 21:03:27 +0200919 if (!td->o.disable_clat) {
Jens Axboe306ddc92009-05-18 13:08:12 +0200920 add_clat_sample(td, idx, usec, bytes);
Jens Axboe9520ebb2008-10-16 21:03:27 +0200921 io_u_mark_latency(td, usec);
922 }
923 if (!td->o.disable_bw)
Jens Axboe306ddc92009-05-18 13:08:12 +0200924 add_bw_sample(td, idx, bytes, &icd->time);
Jens Axboe721938a2008-09-10 09:46:16 +0200925 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200926
Jens Axboe660a1cb2007-04-17 15:37:47 +0200927 if (td_write(td) && idx == DDIR_WRITE &&
Shawn Lewis86705792007-11-21 09:35:41 +0100928 td->o.do_verify &&
Jens Axboe41128402007-03-27 08:32:48 +0200929 td->o.verify != VERIFY_NONE)
Jens Axboe10ba5352006-10-20 11:39:27 +0200930 log_io_piece(td, io_u);
931
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200932 icd->bytes_done[idx] += bytes;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100933
Jens Axboed7762cf2007-02-23 12:34:57 +0100934 if (io_u->end_io) {
Jens Axboe36690c92007-03-26 10:23:34 +0200935 ret = io_u->end_io(td, io_u);
Jens Axboe3af6ef32007-02-18 06:57:43 +0100936 if (ret && !icd->error)
937 icd->error = ret;
938 }
Jens Axboe54517922007-03-05 10:06:06 +0100939 } else {
Jens Axboe10ba5352006-10-20 11:39:27 +0200940 icd->error = io_u->error;
Jens Axboe54517922007-03-05 10:06:06 +0100941 io_u_log_error(td, io_u);
942 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200943}
944
Jens Axboe9520ebb2008-10-16 21:03:27 +0200945static void init_icd(struct thread_data *td, struct io_completion_data *icd,
946 int nr)
Jens Axboe36167d82007-02-18 05:41:31 +0100947{
Jens Axboe9520ebb2008-10-16 21:03:27 +0200948 if (!td->o.disable_clat || !td->o.disable_bw)
949 fio_gettime(&icd->time, NULL);
Jens Axboe36167d82007-02-18 05:41:31 +0100950
Jens Axboe3af6ef32007-02-18 06:57:43 +0100951 icd->nr = nr;
952
Jens Axboe36167d82007-02-18 05:41:31 +0100953 icd->error = 0;
954 icd->bytes_done[0] = icd->bytes_done[1] = 0;
955}
956
Jens Axboe97601022007-02-18 12:47:29 +0100957static void ios_completed(struct thread_data *td,
958 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200959{
960 struct io_u *io_u;
961 int i;
962
Jens Axboe10ba5352006-10-20 11:39:27 +0200963 for (i = 0; i < icd->nr; i++) {
964 io_u = td->io_ops->event(td, i);
965
966 io_completed(td, io_u, icd);
967 put_io_u(td, io_u);
968 }
969}
Jens Axboe97601022007-02-18 12:47:29 +0100970
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100971/*
972 * Complete a single io_u for the sync engines.
973 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100974long io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
Jens Axboe97601022007-02-18 12:47:29 +0100975{
976 struct io_completion_data icd;
977
Jens Axboe9520ebb2008-10-16 21:03:27 +0200978 init_icd(td, &icd, 1);
Jens Axboe97601022007-02-18 12:47:29 +0100979 io_completed(td, io_u, &icd);
980 put_io_u(td, io_u);
981
982 if (!icd.error)
983 return icd.bytes_done[0] + icd.bytes_done[1];
984
Jens Axboe37e974a2007-03-02 15:16:45 +0100985 td_verror(td, icd.error, "io_u_sync_complete");
Jens Axboe97601022007-02-18 12:47:29 +0100986 return -1;
987}
988
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100989/*
990 * Called to complete min_events number of io for the async engines.
991 */
Jens Axboe49504212008-06-05 09:03:30 +0200992long io_u_queued_complete(struct thread_data *td, int min_evts)
Jens Axboe97601022007-02-18 12:47:29 +0100993{
Jens Axboe97601022007-02-18 12:47:29 +0100994 struct io_completion_data icd;
Jens Axboe00de55e2007-02-20 10:45:57 +0100995 struct timespec *tvp = NULL;
Jens Axboe97601022007-02-18 12:47:29 +0100996 int ret;
Davide Libenzi4d06a332007-03-22 07:43:50 +0100997 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
Jens Axboe97601022007-02-18 12:47:29 +0100998
Jens Axboe49504212008-06-05 09:03:30 +0200999 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
Jens Axboeb271fe62008-02-04 10:49:41 +01001000
Jens Axboe49504212008-06-05 09:03:30 +02001001 if (!min_evts)
Jens Axboe00de55e2007-02-20 10:45:57 +01001002 tvp = &ts;
Jens Axboe97601022007-02-18 12:47:29 +01001003
Jens Axboe49504212008-06-05 09:03:30 +02001004 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
Jens Axboe97601022007-02-18 12:47:29 +01001005 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001006 td_verror(td, -ret, "td_io_getevents");
Jens Axboe97601022007-02-18 12:47:29 +01001007 return ret;
1008 } else if (!ret)
1009 return ret;
1010
Jens Axboe9520ebb2008-10-16 21:03:27 +02001011 init_icd(td, &icd, ret);
Jens Axboe97601022007-02-18 12:47:29 +01001012 ios_completed(td, &icd);
1013 if (!icd.error)
1014 return icd.bytes_done[0] + icd.bytes_done[1];
1015
Jens Axboe37e974a2007-03-02 15:16:45 +01001016 td_verror(td, icd.error, "io_u_queued_complete");
Jens Axboe97601022007-02-18 12:47:29 +01001017 return -1;
1018}
Jens Axboe7e77dd02007-02-20 10:57:34 +01001019
1020/*
1021 * Call when io_u is really queued, to update the submission latency.
1022 */
1023void io_u_queued(struct thread_data *td, struct io_u *io_u)
1024{
Jens Axboe9520ebb2008-10-16 21:03:27 +02001025 if (!td->o.disable_slat) {
1026 unsigned long slat_time;
Jens Axboe7e77dd02007-02-20 10:57:34 +01001027
Jens Axboe9520ebb2008-10-16 21:03:27 +02001028 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
Jens Axboe306ddc92009-05-18 13:08:12 +02001029 add_slat_sample(td, io_u->ddir, io_u->xfer_buflen, slat_time);
Jens Axboe9520ebb2008-10-16 21:03:27 +02001030 }
Jens Axboe7e77dd02007-02-20 10:57:34 +01001031}
Jens Axboe433afcb2007-02-22 10:39:01 +01001032
Jens Axboe5973caf2008-05-21 19:52:35 +02001033/*
1034 * "randomly" fill the buffer contents
1035 */
1036void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1037 unsigned int max_bs)
1038{
1039 long *ptr = io_u->buf;
1040
1041 if (!td->o.zero_buffers) {
1042 while ((void *) ptr - io_u->buf < max_bs) {
1043 *ptr = rand() * GOLDEN_RATIO_PRIME;
1044 ptr++;
1045 }
1046 } else
1047 memset(ptr, 0, max_bs);
1048}