blob: 6fb754e7f44701cbfddc628abe4f7164eeccd1aa [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"
9#include "os.h"
10
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 */
18 endio_handler *handler; /* input */
19
20 int error; /* output */
21 unsigned long bytes_done[2]; /* output */
22 struct timeval time; /* output */
23};
24
Jens Axboe10ba5352006-10-20 11:39:27 +020025/*
26 * The ->file_map[] contains a map of blocks we have or have not done io
27 * to yet. Used to make sure we cover the entire range in a fair fashion.
28 */
29static int random_map_free(struct thread_data *td, struct fio_file *f,
30 unsigned long long block)
31{
32 unsigned int idx = RAND_MAP_IDX(td, f, block);
33 unsigned int bit = RAND_MAP_BIT(td, f, block);
34
35 return (f->file_map[idx] & (1UL << bit)) == 0;
36}
37
38/*
Jens Axboedf415582006-10-20 11:41:03 +020039 * Mark a given offset as used in the map.
40 */
41static void mark_random_map(struct thread_data *td, struct fio_file *f,
42 struct io_u *io_u)
43{
Jens Axboeafdbe582007-02-10 19:01:57 +010044 unsigned int min_bs = td->rw_min_bs;
Jens Axboea00735e2006-11-03 08:58:08 +010045 unsigned long long block;
46 unsigned int blocks;
Jens Axboec685b5b2007-02-10 20:02:28 +010047 unsigned int nr_blocks;
Jens Axboedf415582006-10-20 11:41:03 +020048
Jens Axboea00735e2006-11-03 08:58:08 +010049 block = io_u->offset / (unsigned long long) min_bs;
50 blocks = 0;
Jens Axboec685b5b2007-02-10 20:02:28 +010051 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
52
53 while (blocks < nr_blocks) {
Jens Axboedf415582006-10-20 11:41:03 +020054 unsigned int idx, bit;
55
56 if (!random_map_free(td, f, block))
57 break;
58
59 idx = RAND_MAP_IDX(td, f, block);
60 bit = RAND_MAP_BIT(td, f, block);
61
Jens Axboe0032bf92007-02-13 17:39:56 +010062 fio_assert(td, idx < f->num_maps);
Jens Axboedf415582006-10-20 11:41:03 +020063
64 f->file_map[idx] |= (1UL << bit);
65 block++;
66 blocks++;
67 }
68
Jens Axboea00735e2006-11-03 08:58:08 +010069 if ((blocks * min_bs) < io_u->buflen)
70 io_u->buflen = blocks * min_bs;
Jens Axboedf415582006-10-20 11:41:03 +020071}
72
73/*
Jens Axboe10ba5352006-10-20 11:39:27 +020074 * Return the next free block in the map.
75 */
76static int get_next_free_block(struct thread_data *td, struct fio_file *f,
77 unsigned long long *b)
78{
79 int i;
80
Jens Axboec685b5b2007-02-10 20:02:28 +010081 i = f->last_free_lookup;
82 *b = (i * BLOCKS_PER_MAP);
Jens Axboeee884702006-12-20 10:58:07 +010083 while ((*b) * td->rw_min_bs < f->real_file_size) {
Jens Axboe10ba5352006-10-20 11:39:27 +020084 if (f->file_map[i] != -1UL) {
85 *b += ffz(f->file_map[i]);
Jens Axboec685b5b2007-02-10 20:02:28 +010086 f->last_free_lookup = i;
Jens Axboe10ba5352006-10-20 11:39:27 +020087 return 0;
88 }
89
90 *b += BLOCKS_PER_MAP;
91 i++;
92 }
93
94 return 1;
95}
96
97/*
98 * For random io, generate a random new block and see if it's used. Repeat
99 * until we find a free one. For sequential io, just return the end of
100 * the last io issued.
101 */
102static int get_next_offset(struct thread_data *td, struct fio_file *f,
Jens Axboec685b5b2007-02-10 20:02:28 +0100103 struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200104{
Jens Axboec685b5b2007-02-10 20:02:28 +0100105 const int ddir = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200106 unsigned long long b, rb;
107 long r;
108
109 if (!td->sequential) {
Jens Axboef6971252006-11-15 10:00:31 +0100110 unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
Jens Axboec685b5b2007-02-10 20:02:28 +0100111 int loops = 5;
Jens Axboe10ba5352006-10-20 11:39:27 +0200112
113 do {
114 r = os_random_long(&td->random_state);
115 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
Jens Axboebb8895e2006-10-30 15:14:48 +0100116 if (td->norandommap)
117 break;
Jens Axboea00735e2006-11-03 08:58:08 +0100118 rb = b + (f->file_offset / td->min_bs[ddir]);
Jens Axboe10ba5352006-10-20 11:39:27 +0200119 loops--;
120 } while (!random_map_free(td, f, rb) && loops);
121
Jens Axboebca4ed42007-02-12 05:13:23 +0100122 /*
123 * if we failed to retrieve a truly random offset within
124 * the loops assigned, see if there are free ones left at all
125 */
126 if (!loops && get_next_free_block(td, f, &b))
127 return 1;
Jens Axboe10ba5352006-10-20 11:39:27 +0200128 } else
Jens Axboea00735e2006-11-03 08:58:08 +0100129 b = f->last_pos / td->min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200130
Jens Axboec685b5b2007-02-10 20:02:28 +0100131 io_u->offset = (b * td->min_bs[ddir]) + f->file_offset;
Jens Axboebca4ed42007-02-12 05:13:23 +0100132 if (io_u->offset >= f->real_file_size)
Jens Axboe10ba5352006-10-20 11:39:27 +0200133 return 1;
134
135 return 0;
136}
137
Jens Axboebca4ed42007-02-12 05:13:23 +0100138static unsigned int get_next_buflen(struct thread_data *td, struct fio_file *f,
139 struct io_u *io_u)
Jens Axboe10ba5352006-10-20 11:39:27 +0200140{
Jens Axboebca4ed42007-02-12 05:13:23 +0100141 const int ddir = io_u->ddir;
Jens Axboe10ba5352006-10-20 11:39:27 +0200142 unsigned int buflen;
143 long r;
144
Jens Axboea00735e2006-11-03 08:58:08 +0100145 if (td->min_bs[ddir] == td->max_bs[ddir])
146 buflen = td->min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200147 else {
148 r = os_random_long(&td->bsrange_state);
Jens Axboe1e97cce2006-12-05 11:44:16 +0100149 buflen = (unsigned int) (1 + (double) (td->max_bs[ddir] - 1) * r / (RAND_MAX + 1.0));
Jens Axboe690adba2006-10-30 15:25:09 +0100150 if (!td->bs_unaligned)
Jens Axboea00735e2006-11-03 08:58:08 +0100151 buflen = (buflen + td->min_bs[ddir] - 1) & ~(td->min_bs[ddir] - 1);
Jens Axboe10ba5352006-10-20 11:39:27 +0200152 }
153
Jens Axboebca4ed42007-02-12 05:13:23 +0100154 while (buflen + io_u->offset > f->real_file_size) {
Jens Axboebca4ed42007-02-12 05:13:23 +0100155 if (buflen == td->min_bs[ddir])
156 return 0;
157
158 buflen = td->min_bs[ddir];
Jens Axboe10ba5352006-10-20 11:39:27 +0200159 }
160
161 return buflen;
162}
163
164/*
165 * Return the data direction for the next io_u. If the job is a
166 * mixed read/write workload, check the rwmix cycle and switch if
167 * necessary.
168 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100169static enum fio_ddir get_rw_ddir(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200170{
171 if (td_rw(td)) {
172 struct timeval now;
173 unsigned long elapsed;
174
Jens Axboe02bcaa82006-11-24 10:42:00 +0100175 fio_gettime(&now, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200176 elapsed = mtime_since_now(&td->rwmix_switch);
177
178 /*
179 * Check if it's time to seed a new data direction.
180 */
181 if (elapsed >= td->rwmixcycle) {
182 unsigned int v;
183 long r;
184
185 r = os_random_long(&td->rwmix_state);
186 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
187 if (v < td->rwmixread)
188 td->rwmix_ddir = DDIR_READ;
189 else
190 td->rwmix_ddir = DDIR_WRITE;
191 memcpy(&td->rwmix_switch, &now, sizeof(now));
192 }
193 return td->rwmix_ddir;
194 } else if (td_read(td))
195 return DDIR_READ;
196 else
197 return DDIR_WRITE;
198}
199
Jens Axboe10ba5352006-10-20 11:39:27 +0200200void put_io_u(struct thread_data *td, struct io_u *io_u)
201{
Jens Axboe0c6e7512007-02-22 11:19:39 +0100202 assert((io_u->flags & IO_U_F_FREE) == 0);
203 io_u->flags |= IO_U_F_FREE;
204
Jens Axboe10ba5352006-10-20 11:39:27 +0200205 io_u->file = NULL;
206 list_del(&io_u->list);
207 list_add(&io_u->list, &td->io_u_freelist);
208 td->cur_depth--;
209}
210
Jens Axboe755200a2007-02-19 13:08:12 +0100211void requeue_io_u(struct thread_data *td, struct io_u **io_u)
212{
213 struct io_u *__io_u = *io_u;
214
215 list_del(&__io_u->list);
216 list_add_tail(&__io_u->list, &td->io_u_requeues);
217 td->cur_depth--;
218 *io_u = NULL;
219}
220
Jens Axboe10ba5352006-10-20 11:39:27 +0200221static int fill_io_u(struct thread_data *td, struct fio_file *f,
222 struct io_u *io_u)
223{
224 /*
225 * If using an iolog, grab next piece if any available.
226 */
227 if (td->read_iolog)
228 return read_iolog_get(td, io_u);
229
230 /*
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200231 * see if it's time to sync
232 */
Jens Axboe755200a2007-02-19 13:08:12 +0100233 if (td->fsync_blocks && !(td->io_issues[DDIR_WRITE] % td->fsync_blocks)
234 && td->io_issues[DDIR_WRITE] && should_fsync(td)) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200235 io_u->ddir = DDIR_SYNC;
236 io_u->file = f;
237 return 0;
238 }
239
Jens Axboea00735e2006-11-03 08:58:08 +0100240 io_u->ddir = get_rw_ddir(td);
241
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200242 /*
Jens Axboec685b5b2007-02-10 20:02:28 +0100243 * No log, let the seq/rand engine retrieve the next buflen and
244 * position.
Jens Axboe10ba5352006-10-20 11:39:27 +0200245 */
Jens Axboebca4ed42007-02-12 05:13:23 +0100246 if (get_next_offset(td, f, io_u))
247 return 1;
Jens Axboec685b5b2007-02-10 20:02:28 +0100248
Jens Axboebca4ed42007-02-12 05:13:23 +0100249 io_u->buflen = get_next_buflen(td, f, io_u);
250 if (!io_u->buflen)
251 return 1;
Jens Axboe10ba5352006-10-20 11:39:27 +0200252
Jens Axboebca4ed42007-02-12 05:13:23 +0100253 /*
254 * mark entry before potentially trimming io_u
255 */
256 if (!td->read_iolog && !td->sequential && !td->norandommap)
257 mark_random_map(td, f, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200258
Jens Axboebca4ed42007-02-12 05:13:23 +0100259 /*
260 * If using a write iolog, store this entry.
261 */
262 if (td->write_iolog_file)
263 write_iolog_put(td, io_u);
264
265 io_u->file = f;
266 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200267}
268
Jens Axboe71619dc2007-01-13 23:56:33 +0100269static void io_u_mark_depth(struct thread_data *td)
270{
271 int index = 0;
272
273 switch (td->cur_depth) {
274 default:
275 index++;
276 case 32 ... 63:
277 index++;
278 case 16 ... 31:
279 index++;
280 case 8 ... 15:
281 index++;
282 case 4 ... 7:
283 index++;
284 case 2 ... 3:
285 index++;
286 case 1:
287 break;
288 }
289
290 td->io_u_map[index]++;
291 td->total_io_u++;
292}
293
Jens Axboeec118302007-02-17 04:38:20 +0100294static void io_u_mark_latency(struct thread_data *td, unsigned long msec)
295{
296 int index = 0;
297
298 switch (msec) {
299 default:
300 index++;
Jens Axboe8abdce62007-02-21 10:22:55 +0100301 case 1000 ... 1999:
Jens Axboeec118302007-02-17 04:38:20 +0100302 index++;
Jens Axboe8abdce62007-02-21 10:22:55 +0100303 case 750 ... 999:
Jens Axboeec118302007-02-17 04:38:20 +0100304 index++;
Jens Axboe8abdce62007-02-21 10:22:55 +0100305 case 500 ... 749:
Jens Axboeec118302007-02-17 04:38:20 +0100306 index++;
Jens Axboe8abdce62007-02-21 10:22:55 +0100307 case 250 ... 499:
Jens Axboeec118302007-02-17 04:38:20 +0100308 index++;
Jens Axboe8abdce62007-02-21 10:22:55 +0100309 case 100 ... 249:
Jens Axboeec118302007-02-17 04:38:20 +0100310 index++;
Jens Axboe8abdce62007-02-21 10:22:55 +0100311 case 50 ... 99:
Jens Axboeec118302007-02-17 04:38:20 +0100312 index++;
Jens Axboe8abdce62007-02-21 10:22:55 +0100313 case 20 ... 49:
Jens Axboeec118302007-02-17 04:38:20 +0100314 index++;
Jens Axboe8abdce62007-02-21 10:22:55 +0100315 case 10 ... 19:
Jens Axboeec118302007-02-17 04:38:20 +0100316 index++;
Jens Axboe8abdce62007-02-21 10:22:55 +0100317 case 4 ... 9:
Jens Axboeec118302007-02-17 04:38:20 +0100318 index++;
319 case 2 ... 3:
320 index++;
321 case 0 ... 1:
322 break;
323 }
324
325 td->io_u_lat[index]++;
326}
327
Jens Axboe0aabe162007-02-23 08:45:55 +0100328/*
329 * Get next file to service by choosing one at random
330 */
331static struct fio_file *get_next_file_rand(struct thread_data *td)
332{
333 long r = os_random_long(&td->next_file_state);
334 unsigned int fileno;
335 struct fio_file *f;
336
337 do {
338 fileno = (unsigned int) ((double) (td->nr_files - 1) * r / (RAND_MAX + 1.0));
339 f = &td->files[fileno];
340 if (f->fd != -1)
341 return f;
342 } while (1);
343}
344
345/*
346 * Get next file to service by doing round robin between all available ones
347 */
348static struct fio_file *get_next_file_rr(struct thread_data *td)
Jens Axboe3d7c3912007-02-19 13:16:12 +0100349{
350 unsigned int old_next_file = td->next_file;
351 struct fio_file *f;
352
353 do {
354 f = &td->files[td->next_file];
355
356 td->next_file++;
357 if (td->next_file >= td->nr_files)
358 td->next_file = 0;
359
360 if (f->fd != -1)
361 break;
362
363 f = NULL;
364 } while (td->next_file != old_next_file);
365
366 return f;
367}
368
Jens Axboe10ba5352006-10-20 11:39:27 +0200369struct io_u *__get_io_u(struct thread_data *td)
370{
371 struct io_u *io_u = NULL;
372
Jens Axboe755200a2007-02-19 13:08:12 +0100373 if (!list_empty(&td->io_u_requeues))
374 io_u = list_entry(td->io_u_requeues.next, struct io_u, list);
375 else if (!queue_full(td)) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200376 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
377
Jens Axboe6040dab2006-10-24 19:38:15 +0200378 io_u->buflen = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200379 io_u->resid = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100380 io_u->file = NULL;
381 }
382
383 if (io_u) {
Jens Axboe0c6e7512007-02-22 11:19:39 +0100384 assert(io_u->flags & IO_U_F_FREE);
385 io_u->flags &= ~IO_U_F_FREE;
386
Jens Axboe755200a2007-02-19 13:08:12 +0100387 io_u->error = 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200388 list_del(&io_u->list);
389 list_add(&io_u->list, &td->io_u_busylist);
390 td->cur_depth++;
Jens Axboe71619dc2007-01-13 23:56:33 +0100391 io_u_mark_depth(td);
Jens Axboe10ba5352006-10-20 11:39:27 +0200392 }
393
394 return io_u;
395}
396
397/*
398 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
399 * etc. The returned io_u is fully ready to be prepped and submitted.
400 */
Jens Axboe3d7c3912007-02-19 13:16:12 +0100401struct io_u *get_io_u(struct thread_data *td)
Jens Axboe10ba5352006-10-20 11:39:27 +0200402{
Jens Axboe3d7c3912007-02-19 13:16:12 +0100403 struct fio_file *f;
Jens Axboe10ba5352006-10-20 11:39:27 +0200404 struct io_u *io_u;
405
406 io_u = __get_io_u(td);
407 if (!io_u)
408 return NULL;
409
Jens Axboe755200a2007-02-19 13:08:12 +0100410 /*
411 * from a requeue, io_u already setup
412 */
413 if (io_u->file)
Jens Axboe77f392b2007-02-19 20:13:09 +0100414 goto out;
Jens Axboe755200a2007-02-19 13:08:12 +0100415
Jens Axboe0aabe162007-02-23 08:45:55 +0100416 if (td->file_service_type == FIO_FSERVICE_RR)
417 f = get_next_file_rr(td);
418 else
419 f = get_next_file_rand(td);
420
Jens Axboe3d7c3912007-02-19 13:16:12 +0100421 if (!f) {
422 put_io_u(td, io_u);
423 return NULL;
424 }
425
426 io_u->file = f;
427
Jens Axboe10ba5352006-10-20 11:39:27 +0200428 if (td->zone_bytes >= td->zone_size) {
429 td->zone_bytes = 0;
430 f->last_pos += td->zone_skip;
431 }
432
433 if (fill_io_u(td, f, io_u)) {
434 put_io_u(td, io_u);
435 return NULL;
436 }
437
Jens Axboeee884702006-12-20 10:58:07 +0100438 if (io_u->buflen + io_u->offset > f->real_file_size) {
Jens Axboe10ba5352006-10-20 11:39:27 +0200439 if (td->io_ops->flags & FIO_RAWIO) {
440 put_io_u(td, io_u);
441 return NULL;
442 }
443
Jens Axboeee884702006-12-20 10:58:07 +0100444 io_u->buflen = f->real_file_size - io_u->offset;
Jens Axboe10ba5352006-10-20 11:39:27 +0200445 }
446
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200447 if (io_u->ddir != DDIR_SYNC) {
448 if (!io_u->buflen) {
449 put_io_u(td, io_u);
450 return NULL;
451 }
452
Jens Axboe36167d82007-02-18 05:41:31 +0100453 f->last_pos = io_u->offset + io_u->buflen;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200454
455 if (td->verify != VERIFY_NONE)
456 populate_verify_io_u(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200457 }
458
Jens Axboe165faf12007-02-07 11:30:37 +0100459 /*
460 * Set io data pointers.
461 */
Jens Axboe77f392b2007-02-19 20:13:09 +0100462out:
Jens Axboecec6b552007-02-06 20:15:38 +0100463 io_u->xfer_buf = io_u->buf;
464 io_u->xfer_buflen = io_u->buflen;
Jens Axboe165faf12007-02-07 11:30:37 +0100465
Jens Axboe36167d82007-02-18 05:41:31 +0100466 if (td_io_prep(td, io_u)) {
467 put_io_u(td, io_u);
468 return NULL;
469 }
470
Jens Axboe02bcaa82006-11-24 10:42:00 +0100471 fio_gettime(&io_u->start_time, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200472 return io_u;
473}
474
Jens Axboe97601022007-02-18 12:47:29 +0100475static void io_completed(struct thread_data *td, struct io_u *io_u,
476 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200477{
Jens Axboe10ba5352006-10-20 11:39:27 +0200478 unsigned long msec;
479
Jens Axboe0c6e7512007-02-22 11:19:39 +0100480 assert(io_u->flags & IO_U_F_FLIGHT);
481 io_u->flags &= ~IO_U_F_FLIGHT;
482
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200483 if (io_u->ddir == DDIR_SYNC) {
484 td->last_was_sync = 1;
485 return;
486 }
487
488 td->last_was_sync = 0;
489
Jens Axboe10ba5352006-10-20 11:39:27 +0200490 if (!io_u->error) {
491 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100492 const enum fio_ddir idx = io_u->ddir;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100493 int ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200494
495 td->io_blocks[idx]++;
496 td->io_bytes[idx] += bytes;
497 td->zone_bytes += bytes;
498 td->this_io_bytes[idx] += bytes;
499
Jens Axboe02bcaa82006-11-24 10:42:00 +0100500 io_u->file->last_completed_pos = io_u->offset + io_u->buflen;
501
502 msec = mtime_since(&io_u->issue_time, &icd->time);
Jens Axboe10ba5352006-10-20 11:39:27 +0200503
504 add_clat_sample(td, idx, msec);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100505 add_bw_sample(td, idx, &icd->time);
Jens Axboeec118302007-02-17 04:38:20 +0100506 io_u_mark_latency(td, msec);
Jens Axboe10ba5352006-10-20 11:39:27 +0200507
508 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
509 log_io_piece(td, io_u);
510
511 icd->bytes_done[idx] += bytes;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100512
513 if (icd->handler) {
514 ret = icd->handler(io_u);
515 if (ret && !icd->error)
516 icd->error = ret;
517 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200518 } else
519 icd->error = io_u->error;
520}
521
Jens Axboe97601022007-02-18 12:47:29 +0100522static void init_icd(struct io_completion_data *icd, endio_handler *handler,
523 int nr)
Jens Axboe36167d82007-02-18 05:41:31 +0100524{
525 fio_gettime(&icd->time, NULL);
526
Jens Axboe3af6ef32007-02-18 06:57:43 +0100527 icd->handler = handler;
528 icd->nr = nr;
529
Jens Axboe36167d82007-02-18 05:41:31 +0100530 icd->error = 0;
531 icd->bytes_done[0] = icd->bytes_done[1] = 0;
532}
533
Jens Axboe97601022007-02-18 12:47:29 +0100534static void ios_completed(struct thread_data *td,
535 struct io_completion_data *icd)
Jens Axboe10ba5352006-10-20 11:39:27 +0200536{
537 struct io_u *io_u;
538 int i;
539
Jens Axboe10ba5352006-10-20 11:39:27 +0200540 for (i = 0; i < icd->nr; i++) {
541 io_u = td->io_ops->event(td, i);
542
543 io_completed(td, io_u, icd);
544 put_io_u(td, io_u);
545 }
546}
Jens Axboe97601022007-02-18 12:47:29 +0100547
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100548/*
549 * Complete a single io_u for the sync engines.
550 */
Jens Axboe97601022007-02-18 12:47:29 +0100551long io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
552 endio_handler *handler)
553{
554 struct io_completion_data icd;
555
556 init_icd(&icd, handler, 1);
557 io_completed(td, io_u, &icd);
558 put_io_u(td, io_u);
559
560 if (!icd.error)
561 return icd.bytes_done[0] + icd.bytes_done[1];
562
Jens Axboe97601022007-02-18 12:47:29 +0100563 return -1;
564}
565
Jens Axboee7e6cfb2007-02-20 10:58:34 +0100566/*
567 * Called to complete min_events number of io for the async engines.
568 */
Jens Axboe97601022007-02-18 12:47:29 +0100569long io_u_queued_complete(struct thread_data *td, int min_events,
570 endio_handler *handler)
571
572{
Jens Axboe97601022007-02-18 12:47:29 +0100573 struct io_completion_data icd;
Jens Axboe00de55e2007-02-20 10:45:57 +0100574 struct timespec *tvp = NULL;
Jens Axboe97601022007-02-18 12:47:29 +0100575 int ret;
576
Jens Axboe755200a2007-02-19 13:08:12 +0100577 if (min_events > 0) {
Jens Axboe755200a2007-02-19 13:08:12 +0100578 ret = td_io_commit(td);
579 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100580 td_verror(td, -ret, "td_io_commit");
Jens Axboe755200a2007-02-19 13:08:12 +0100581 return ret;
582 }
Jens Axboe00de55e2007-02-20 10:45:57 +0100583 } else {
584 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
585
586 tvp = &ts;
Jens Axboe755200a2007-02-19 13:08:12 +0100587 }
Jens Axboe97601022007-02-18 12:47:29 +0100588
Jens Axboe00de55e2007-02-20 10:45:57 +0100589 ret = td_io_getevents(td, min_events, td->cur_depth, tvp);
Jens Axboe97601022007-02-18 12:47:29 +0100590 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100591 td_verror(td, -ret, "td_io_getevents");
Jens Axboe97601022007-02-18 12:47:29 +0100592 return ret;
593 } else if (!ret)
594 return ret;
595
596 init_icd(&icd, handler, ret);
597 ios_completed(td, &icd);
598 if (!icd.error)
599 return icd.bytes_done[0] + icd.bytes_done[1];
600
Jens Axboe97601022007-02-18 12:47:29 +0100601 return -1;
602}
Jens Axboe7e77dd02007-02-20 10:57:34 +0100603
604/*
605 * Call when io_u is really queued, to update the submission latency.
606 */
607void io_u_queued(struct thread_data *td, struct io_u *io_u)
608{
609 unsigned long slat_time;
610
611 slat_time = mtime_since(&io_u->start_time, &io_u->issue_time);
612 add_slat_sample(td, io_u->ddir, slat_time);
613}
Jens Axboe433afcb2007-02-22 10:39:01 +0100614
Jens Axboe55bc9722007-02-22 11:30:05 +0100615#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +0100616void io_u_set_timeout(struct thread_data *td)
617{
618 assert(td->cur_depth);
619
620 td->timer.it_interval.tv_sec = 0;
621 td->timer.it_interval.tv_usec = 0;
622 td->timer.it_value.tv_sec = IO_U_TIMEOUT + IO_U_TIMEOUT_INC;
623 td->timer.it_value.tv_usec = 0;
624 setitimer(ITIMER_REAL, &td->timer, NULL);
625 fio_gettime(&td->timeout_end, NULL);
626}
Jens Axboe5945b9b2007-02-22 20:33:01 +0100627
628static void io_u_dump(struct io_u *io_u)
629{
630 unsigned long t_start = mtime_since_now(&io_u->start_time);
631 unsigned long t_issue = mtime_since_now(&io_u->issue_time);
632
633 log_err("io_u=%p, t_start=%lu, t_issue=%lu\n", io_u, t_start, t_issue);
634 log_err(" buf=%p/%p, len=%lu/%lu, offset=%llu\n", io_u->buf, io_u->xfer_buf, io_u->buflen, io_u->xfer_buflen, io_u->offset);
635 log_err(" ddir=%d, fname=%s\n", io_u->ddir, io_u->file->file_name);
636}
Jens Axboe55bc9722007-02-22 11:30:05 +0100637#else
638void io_u_set_timeout(struct thread_data fio_unused *td)
639{
640}
641#endif
Jens Axboe433afcb2007-02-22 10:39:01 +0100642
Jens Axboe55bc9722007-02-22 11:30:05 +0100643#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +0100644static void io_u_timeout_handler(int fio_unused sig)
645{
646 struct thread_data *td, *__td;
647 pid_t pid = getpid();
Jens Axboe5945b9b2007-02-22 20:33:01 +0100648 struct list_head *entry;
649 struct io_u *io_u;
Jens Axboe433afcb2007-02-22 10:39:01 +0100650 int i;
651
652 log_err("fio: io_u timeout\n");
653
654 /*
655 * TLS would be nice...
656 */
657 td = NULL;
658 for_each_td(__td, i) {
659 if (__td->pid == pid) {
660 td = __td;
661 break;
662 }
663 }
664
665 if (!td) {
666 log_err("fio: io_u timeout, can't find job\n");
667 exit(1);
668 }
669
670 if (!td->cur_depth) {
671 log_err("fio: timeout without pending work?\n");
672 return;
673 }
674
675 log_err("fio: io_u timeout: job=%s, pid=%d\n", td->name, td->pid);
Jens Axboe5945b9b2007-02-22 20:33:01 +0100676
677 list_for_each(entry, &td->io_u_busylist) {
678 io_u = list_entry(entry, struct io_u, list);
679
680 io_u_dump(io_u);
681 }
682
683 td_verror(td, ETIMEDOUT, "io_u timeout");
Jens Axboe433afcb2007-02-22 10:39:01 +0100684 exit(1);
685}
Jens Axboe55bc9722007-02-22 11:30:05 +0100686#endif
Jens Axboe433afcb2007-02-22 10:39:01 +0100687
688void io_u_init_timeout(void)
689{
Jens Axboe55bc9722007-02-22 11:30:05 +0100690#ifdef FIO_USE_TIMEOUT
Jens Axboe433afcb2007-02-22 10:39:01 +0100691 signal(SIGALRM, io_u_timeout_handler);
Jens Axboe55bc9722007-02-22 11:30:05 +0100692#endif
Jens Axboe433afcb2007-02-22 10:39:01 +0100693}