blob: a2651f0325490d780ee7a9352fa01d53b81350b5 [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
Jens Axboeaae22ca2006-09-05 10:46:22 +02005 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboeebac4652005-12-08 15:25:21 +01006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
Jens Axboeebac4652005-12-08 15:25:21 +010022#include <unistd.h>
23#include <fcntl.h>
24#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010025#include <signal.h>
26#include <time.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010028#include <sys/stat.h>
29#include <sys/wait.h>
30#include <sys/ipc.h>
31#include <sys/shm.h>
32#include <sys/ioctl.h>
33#include <sys/mman.h>
34
35#include "fio.h"
36#include "os.h"
37
38#define MASK (4095)
39
40#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
41
42int groupid = 0;
43int thread_number = 0;
44static char run_str[MAX_JOBS + 1];
45int shm_id = 0;
Jens Axboe5289b842005-12-08 20:47:28 +010046static struct timeval genesis;
Jens Axboe88c6ed82006-06-09 11:28:10 +020047static int temp_stall_ts;
Jens Axboeebac4652005-12-08 15:25:21 +010048
Jens Axboeebac4652005-12-08 15:25:21 +010049static void print_thread_status(void);
50
Jens Axboec04f7ec2006-05-31 10:13:16 +020051extern unsigned long long mlock_size;
52
Jens Axboeebac4652005-12-08 15:25:21 +010053/*
Jens Axboe79809112006-06-09 10:14:54 +020054 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
55 * will never back again. It may cycle between running/verififying/fsyncing.
56 * Once the thread reaches TD_EXITED, it is just waiting for the core to
57 * reap it.
Jens Axboeebac4652005-12-08 15:25:21 +010058 */
59enum {
60 TD_NOT_CREATED = 0,
61 TD_CREATED,
Jens Axboe75154842006-06-01 13:56:09 +020062 TD_INITIALIZED,
Jens Axboeebac4652005-12-08 15:25:21 +010063 TD_RUNNING,
64 TD_VERIFYING,
Jens Axboe5853e5a2006-06-08 14:41:05 +020065 TD_FSYNCING,
Jens Axboeebac4652005-12-08 15:25:21 +010066 TD_EXITED,
67 TD_REAPED,
68};
69
Jens Axboe3d60d1e2006-05-25 06:31:06 +020070#define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
Jens Axboeebac4652005-12-08 15:25:21 +010071
Jens Axboebbfd6b02006-06-07 19:42:54 +020072static volatile int startup_sem;
Jens Axboeebac4652005-12-08 15:25:21 +010073
74#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020075#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010076
77static void terminate_threads(int group_id)
78{
79 int i;
80
81 for (i = 0; i < thread_number; i++) {
82 struct thread_data *td = &threads[i];
83
84 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
85 td->terminate = 1;
86 td->start_delay = 0;
87 }
88 }
89}
90
91static void sig_handler(int sig)
92{
93 switch (sig) {
94 case SIGALRM:
95 update_io_ticks();
96 disk_util_timer_arm();
97 print_thread_status();
98 break;
99 default:
100 printf("\nfio: terminating on signal\n");
101 fflush(stdout);
102 terminate_threads(TERMINATE_ALL);
103 break;
104 }
105}
106
Jens Axboe906c8d72006-06-13 09:37:56 +0200107/*
108 * The ->file_map[] contains a map of blocks we have or have not done io
109 * to yet. Used to make sure we cover the entire range in a fair fashion.
110 */
Jens Axboeebac4652005-12-08 15:25:21 +0100111static int random_map_free(struct thread_data *td, unsigned long long block)
112{
113 unsigned int idx = RAND_MAP_IDX(td, block);
114 unsigned int bit = RAND_MAP_BIT(td, block);
115
116 return (td->file_map[idx] & (1UL << bit)) == 0;
117}
118
Jens Axboe906c8d72006-06-13 09:37:56 +0200119/*
120 * Return the next free block in the map.
121 */
Jens Axboeebac4652005-12-08 15:25:21 +0100122static int get_next_free_block(struct thread_data *td, unsigned long long *b)
123{
124 int i;
125
126 *b = 0;
127 i = 0;
128 while ((*b) * td->min_bs < td->io_size) {
129 if (td->file_map[i] != -1UL) {
130 *b += ffz(td->file_map[i]);
131 return 0;
132 }
133
134 *b += BLOCKS_PER_MAP;
135 i++;
136 }
137
138 return 1;
139}
140
Jens Axboe906c8d72006-06-13 09:37:56 +0200141/*
142 * Mark a given offset as used in the map.
143 */
Jens Axboeebac4652005-12-08 15:25:21 +0100144static void mark_random_map(struct thread_data *td, struct io_u *io_u)
145{
Jens Axboe200bc852006-06-01 16:10:12 +0200146 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100147 unsigned int blocks = 0;
148
149 while (blocks < (io_u->buflen / td->min_bs)) {
150 unsigned int idx, bit;
151
152 if (!random_map_free(td, block))
153 break;
154
155 idx = RAND_MAP_IDX(td, block);
156 bit = RAND_MAP_BIT(td, block);
157
158 assert(idx < td->num_maps);
159
160 td->file_map[idx] |= (1UL << bit);
161 block++;
162 blocks++;
163 }
164
165 if ((blocks * td->min_bs) < io_u->buflen)
166 io_u->buflen = blocks * td->min_bs;
167}
168
Jens Axboe906c8d72006-06-13 09:37:56 +0200169/*
170 * For random io, generate a random new block and see if it's used. Repeat
171 * until we find a free one. For sequential io, just return the end of
172 * the last io issued.
173 */
Jens Axboe20dc95c2005-12-09 10:29:35 +0100174static int get_next_offset(struct thread_data *td, unsigned long long *offset)
175{
176 unsigned long long b, rb;
177 long r;
178
179 if (!td->sequential) {
Jens Axboe085227a2006-06-01 15:54:09 -0700180 unsigned long long max_blocks = td->io_size / td->min_bs;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100181 int loops = 50;
182
183 do {
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200184 r = os_random_long(&td->random_state);
Jens Axboe085227a2006-06-01 15:54:09 -0700185 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
Jens Axboe20dc95c2005-12-09 10:29:35 +0100186 rb = b + (td->file_offset / td->min_bs);
187 loops--;
188 } while (!random_map_free(td, rb) && loops);
189
190 if (!loops) {
191 if (get_next_free_block(td, &b))
192 return 1;
193 }
194 } else
195 b = td->last_pos / td->min_bs;
196
197 *offset = (b * td->min_bs) + td->file_offset;
Jens Axboe838a3cd2006-01-20 12:38:29 +0100198 if (*offset > td->real_file_size)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100199 return 1;
200
201 return 0;
202}
203
204static unsigned int get_next_buflen(struct thread_data *td)
205{
206 unsigned int buflen;
207 long r;
208
209 if (td->min_bs == td->max_bs)
210 buflen = td->min_bs;
211 else {
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200212 r = os_random_long(&td->bsrange_state);
Jens Axboe20dc95c2005-12-09 10:29:35 +0100213 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
214 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
215 }
216
217 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
218 buflen = td->io_size - td->this_io_bytes[td->ddir];
219
220 return buflen;
221}
222
Jens Axboe906c8d72006-06-13 09:37:56 +0200223/*
224 * Check if we are above the minimum rate given.
225 */
Jens Axboeebac4652005-12-08 15:25:21 +0100226static int check_min_rate(struct thread_data *td, struct timeval *now)
227{
228 unsigned long spent;
229 unsigned long rate;
230 int ddir = td->ddir;
231
232 /*
233 * allow a 2 second settle period in the beginning
234 */
235 if (mtime_since(&td->start, now) < 2000)
236 return 0;
237
238 /*
239 * if rate blocks is set, sample is running
240 */
241 if (td->rate_bytes) {
242 spent = mtime_since(&td->lastrate, now);
243 if (spent < td->ratecycle)
244 return 0;
245
246 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
247 if (rate < td->ratemin) {
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200248 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
Jens Axboeebac4652005-12-08 15:25:21 +0100249 if (rate_quit)
250 terminate_threads(td->groupid);
251 return 1;
252 }
253 }
254
255 td->rate_bytes = td->this_io_bytes[ddir];
256 memcpy(&td->lastrate, now, sizeof(*now));
257 return 0;
258}
259
260static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
261{
262 if (!td->timeout)
263 return 0;
264 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
265 return 1;
266
267 return 0;
268}
269
270static void fill_random_bytes(struct thread_data *td,
271 unsigned char *p, unsigned int len)
272{
273 unsigned int todo;
274 double r;
275
276 while (len) {
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200277 r = os_random_double(&td->verify_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100278
279 /*
280 * lrand48_r seems to be broken and only fill the bottom
281 * 32-bits, even on 64-bit archs with 64-bit longs
282 */
283 todo = sizeof(r);
284 if (todo > len)
285 todo = len;
286
287 memcpy(p, &r, todo);
288
289 len -= todo;
290 p += todo;
291 }
292}
293
294static void hexdump(void *buffer, int len)
295{
296 unsigned char *p = buffer;
297 int i;
298
299 for (i = 0; i < len; i++)
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200300 fprintf(f_out, "%02x", p[i]);
301 fprintf(f_out, "\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100302}
303
304static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
305{
306 unsigned char *p = (unsigned char *) io_u->buf;
307 unsigned long c;
Jens Axboeebac4652005-12-08 15:25:21 +0100308
309 p += sizeof(*hdr);
310 c = crc32(p, hdr->len - sizeof(*hdr));
Jens Axboeebac4652005-12-08 15:25:21 +0100311
Jens Axboe22f78b32006-06-07 11:30:07 +0200312 if (c != hdr->crc32) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200313 log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
314 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
Jens Axboe22f78b32006-06-07 11:30:07 +0200315 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100316 }
317
Jens Axboe22f78b32006-06-07 11:30:07 +0200318 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100319}
320
321static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
322{
323 unsigned char *p = (unsigned char *) io_u->buf;
324 struct md5_ctx md5_ctx;
Jens Axboeebac4652005-12-08 15:25:21 +0100325
326 memset(&md5_ctx, 0, sizeof(md5_ctx));
327 p += sizeof(*hdr);
328 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
329
Jens Axboe22f78b32006-06-07 11:30:07 +0200330 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200331 log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
Jens Axboeebac4652005-12-08 15:25:21 +0100332 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
333 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
Jens Axboe22f78b32006-06-07 11:30:07 +0200334 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100335 }
336
Jens Axboe22f78b32006-06-07 11:30:07 +0200337 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100338}
339
340static int verify_io_u(struct io_u *io_u)
341{
342 struct verify_header *hdr = (struct verify_header *) io_u->buf;
343 int ret;
344
345 if (hdr->fio_magic != FIO_HDR_MAGIC)
346 return 1;
347
348 if (hdr->verify_type == VERIFY_MD5)
349 ret = verify_io_u_md5(hdr, io_u);
350 else if (hdr->verify_type == VERIFY_CRC32)
351 ret = verify_io_u_crc32(hdr, io_u);
352 else {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200353 log_err("Bad verify type %d\n", hdr->verify_type);
Jens Axboeebac4652005-12-08 15:25:21 +0100354 ret = 1;
355 }
356
357 return ret;
358}
359
360static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
361{
362 hdr->crc32 = crc32(p, len);
363}
364
365static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
366{
367 struct md5_ctx md5_ctx;
368
369 memset(&md5_ctx, 0, sizeof(md5_ctx));
370 md5_update(&md5_ctx, p, len);
371 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
372}
373
Jens Axboe906c8d72006-06-13 09:37:56 +0200374/*
375 * Return the data direction for the next io_u. If the job is a
376 * mixed read/write workload, check the rwmix cycle and switch if
377 * necessary.
378 */
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200379static int get_rw_ddir(struct thread_data *td)
380{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200381 if (td_rw(td)) {
382 struct timeval now;
383 unsigned long elapsed;
384
385 gettimeofday(&now, NULL);
386 elapsed = mtime_since_now(&td->rwmix_switch);
387
388 /*
389 * Check if it's time to seed a new data direction.
390 */
391 if (elapsed >= td->rwmixcycle) {
Jens Axboec1ee2ca2006-06-07 22:42:37 +0200392 int v;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200393 long r;
394
Jens Axboec1ee2ca2006-06-07 22:42:37 +0200395 r = os_random_long(&td->rwmix_state);
396 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200397 if (v < td->rwmixread)
398 td->rwmix_ddir = DDIR_READ;
399 else
400 td->rwmix_ddir = DDIR_WRITE;
401 memcpy(&td->rwmix_switch, &now, sizeof(now));
402 }
403 return td->rwmix_ddir;
404 } else if (td_read(td))
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200405 return DDIR_READ;
406 else
407 return DDIR_WRITE;
408}
409
Jens Axboeebac4652005-12-08 15:25:21 +0100410/*
411 * fill body of io_u->buf with random data and add a header with the
Jens Axboe22f78b32006-06-07 11:30:07 +0200412 * crc32 or md5 sum of that data.
Jens Axboeebac4652005-12-08 15:25:21 +0100413 */
414static void populate_io_u(struct thread_data *td, struct io_u *io_u)
415{
416 unsigned char *p = (unsigned char *) io_u->buf;
417 struct verify_header hdr;
418
419 hdr.fio_magic = FIO_HDR_MAGIC;
420 hdr.len = io_u->buflen;
421 p += sizeof(hdr);
422 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
423
424 if (td->verify == VERIFY_MD5) {
425 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
426 hdr.verify_type = VERIFY_MD5;
427 } else {
428 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
429 hdr.verify_type = VERIFY_CRC32;
430 }
431
432 memcpy(io_u->buf, &hdr, sizeof(hdr));
433}
434
Jens Axboeaea47d42006-05-26 19:27:29 +0200435static int td_io_prep(struct thread_data *td, struct io_u *io_u)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100436{
Jens Axboe2866c822006-10-09 15:57:48 +0200437 if (td->io_ops->prep && td->io_ops->prep(td, io_u))
Jens Axboe20dc95c2005-12-09 10:29:35 +0100438 return 1;
439
440 return 0;
441}
442
Jens Axboeb1ff3402006-02-17 11:35:58 +0100443void put_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboeebac4652005-12-08 15:25:21 +0100444{
445 list_del(&io_u->list);
446 list_add(&io_u->list, &td->io_u_freelist);
447 td->cur_depth--;
448}
449
Jens Axboe843a7412006-06-01 21:14:21 -0700450static int fill_io_u(struct thread_data *td, struct io_u *io_u)
451{
452 /*
453 * If using an iolog, grab next piece if any available.
454 */
455 if (td->read_iolog)
456 return read_iolog_get(td, io_u);
457
Jens Axboeaea47d42006-05-26 19:27:29 +0200458 /*
459 * No log, let the seq/rand engine retrieve the next position.
460 */
461 if (!get_next_offset(td, &io_u->offset)) {
462 io_u->buflen = get_next_buflen(td);
463
464 if (io_u->buflen) {
465 io_u->ddir = get_rw_ddir(td);
Jens Axboe843a7412006-06-01 21:14:21 -0700466
467 /*
468 * If using a write iolog, store this entry.
469 */
470 if (td->write_iolog)
471 write_iolog_put(td, io_u);
472
Jens Axboeaea47d42006-05-26 19:27:29 +0200473 return 0;
474 }
475 }
476
477 return 1;
478}
479
Jens Axboe22f78b32006-06-07 11:30:07 +0200480#define queue_full(td) list_empty(&(td)->io_u_freelist)
Jens Axboeebac4652005-12-08 15:25:21 +0100481
Jens Axboeb1ff3402006-02-17 11:35:58 +0100482struct io_u *__get_io_u(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100483{
Jens Axboe22f78b32006-06-07 11:30:07 +0200484 struct io_u *io_u = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100485
Jens Axboe22f78b32006-06-07 11:30:07 +0200486 if (!queue_full(td)) {
487 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100488
Jens Axboe22f78b32006-06-07 11:30:07 +0200489 io_u->error = 0;
490 io_u->resid = 0;
491 list_del(&io_u->list);
492 list_add(&io_u->list, &td->io_u_busylist);
493 td->cur_depth++;
494 }
495
Jens Axboeebac4652005-12-08 15:25:21 +0100496 return io_u;
497}
498
Jens Axboe906c8d72006-06-13 09:37:56 +0200499/*
500 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
501 * etc. The returned io_u is fully ready to be prepped and submitted.
502 */
Jens Axboeebac4652005-12-08 15:25:21 +0100503static struct io_u *get_io_u(struct thread_data *td)
504{
505 struct io_u *io_u;
506
507 io_u = __get_io_u(td);
508 if (!io_u)
509 return NULL;
510
Jens Axboe20dc95c2005-12-09 10:29:35 +0100511 if (td->zone_bytes >= td->zone_size) {
512 td->zone_bytes = 0;
513 td->last_pos += td->zone_skip;
514 }
515
Jens Axboeaea47d42006-05-26 19:27:29 +0200516 if (fill_io_u(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100517 put_io_u(td, io_u);
518 return NULL;
519 }
520
Jens Axboe838a3cd2006-01-20 12:38:29 +0100521 if (io_u->buflen + io_u->offset > td->real_file_size)
522 io_u->buflen = td->real_file_size - io_u->offset;
Jens Axboeebac4652005-12-08 15:25:21 +0100523
524 if (!io_u->buflen) {
525 put_io_u(td, io_u);
526 return NULL;
527 }
528
Jens Axboe843a7412006-06-01 21:14:21 -0700529 if (!td->read_iolog && !td->sequential)
Jens Axboeebac4652005-12-08 15:25:21 +0100530 mark_random_map(td, io_u);
531
Jens Axboe20dc95c2005-12-09 10:29:35 +0100532 td->last_pos += io_u->buflen;
Jens Axboeebac4652005-12-08 15:25:21 +0100533
534 if (td->verify != VERIFY_NONE)
535 populate_io_u(td, io_u);
536
Jens Axboeaea47d42006-05-26 19:27:29 +0200537 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100538 put_io_u(td, io_u);
539 return NULL;
540 }
541
542 gettimeofday(&io_u->start_time, NULL);
543 return io_u;
544}
545
546static inline void td_set_runstate(struct thread_data *td, int runstate)
547{
Jens Axboeebac4652005-12-08 15:25:21 +0100548 td->runstate = runstate;
549}
550
Jens Axboeaea47d42006-05-26 19:27:29 +0200551static int get_next_verify(struct thread_data *td, struct io_u *io_u)
Jens Axboeebac4652005-12-08 15:25:21 +0100552{
553 struct io_piece *ipo;
554
Jens Axboe22f78b32006-06-07 11:30:07 +0200555 if (!list_empty(&td->io_hist_list)) {
556 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100557
Jens Axboe22f78b32006-06-07 11:30:07 +0200558 list_del(&ipo->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100559
Jens Axboe22f78b32006-06-07 11:30:07 +0200560 io_u->offset = ipo->offset;
561 io_u->buflen = ipo->len;
562 io_u->ddir = DDIR_READ;
563 free(ipo);
564 return 0;
565 }
566
567 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100568}
569
Jens Axboeebac4652005-12-08 15:25:21 +0100570static int sync_td(struct thread_data *td)
571{
Jens Axboe2866c822006-10-09 15:57:48 +0200572 if (td->io_ops->sync)
573 return td->io_ops->sync(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100574
575 return 0;
576}
577
578static int io_u_getevents(struct thread_data *td, int min, int max,
579 struct timespec *t)
580{
Jens Axboe2866c822006-10-09 15:57:48 +0200581 return td->io_ops->getevents(td, min, max, t);
Jens Axboeebac4652005-12-08 15:25:21 +0100582}
583
584static int io_u_queue(struct thread_data *td, struct io_u *io_u)
585{
586 gettimeofday(&io_u->issue_time, NULL);
587
Jens Axboe2866c822006-10-09 15:57:48 +0200588 return td->io_ops->queue(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100589}
590
591#define iocb_time(iocb) ((unsigned long) (iocb)->data)
592
593static void io_completed(struct thread_data *td, struct io_u *io_u,
594 struct io_completion_data *icd)
595{
596 struct timeval e;
597 unsigned long msec;
598
599 gettimeofday(&e, NULL);
600
601 if (!io_u->error) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100602 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200603 const int idx = io_u->ddir;
Jens Axboeebac4652005-12-08 15:25:21 +0100604
605 td->io_blocks[idx]++;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100606 td->io_bytes[idx] += bytes;
607 td->zone_bytes += bytes;
608 td->this_io_bytes[idx] += bytes;
Jens Axboeebac4652005-12-08 15:25:21 +0100609
610 msec = mtime_since(&io_u->issue_time, &e);
611
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200612 add_clat_sample(td, idx, msec);
613 add_bw_sample(td, idx);
Jens Axboeebac4652005-12-08 15:25:21 +0100614
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200615 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
Jens Axboeebac4652005-12-08 15:25:21 +0100616 log_io_piece(td, io_u);
617
Jens Axboe20dc95c2005-12-09 10:29:35 +0100618 icd->bytes_done[idx] += bytes;
Jens Axboeebac4652005-12-08 15:25:21 +0100619 } else
620 icd->error = io_u->error;
621}
622
623static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
624{
625 struct io_u *io_u;
626 int i;
627
628 icd->error = 0;
629 icd->bytes_done[0] = icd->bytes_done[1] = 0;
630
631 for (i = 0; i < icd->nr; i++) {
Jens Axboe2866c822006-10-09 15:57:48 +0200632 io_u = td->io_ops->event(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100633
634 io_completed(td, io_u, icd);
635 put_io_u(td, io_u);
636 }
637}
638
Jens Axboe906c8d72006-06-13 09:37:56 +0200639/*
640 * When job exits, we can cancel the in-flight IO if we are using async
641 * io. Attempt to do so.
642 */
Jens Axboeebac4652005-12-08 15:25:21 +0100643static void cleanup_pending_aio(struct thread_data *td)
644{
645 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
646 struct list_head *entry, *n;
647 struct io_completion_data icd;
648 struct io_u *io_u;
649 int r;
650
651 /*
652 * get immediately available events, if any
653 */
654 r = io_u_getevents(td, 0, td->cur_depth, &ts);
655 if (r > 0) {
656 icd.nr = r;
657 ios_completed(td, &icd);
658 }
659
660 /*
661 * now cancel remaining active events
662 */
Jens Axboe2866c822006-10-09 15:57:48 +0200663 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100664 list_for_each_safe(entry, n, &td->io_u_busylist) {
665 io_u = list_entry(entry, struct io_u, list);
666
Jens Axboe2866c822006-10-09 15:57:48 +0200667 r = td->io_ops->cancel(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100668 if (!r)
669 put_io_u(td, io_u);
670 }
671 }
672
673 if (td->cur_depth) {
674 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
675 if (r > 0) {
676 icd.nr = r;
677 ios_completed(td, &icd);
678 }
679 }
680}
681
682static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
683{
684 struct io_u *v_io_u = *io_u;
685 int ret = 0;
686
687 if (v_io_u) {
688 ret = verify_io_u(v_io_u);
689 put_io_u(td, v_io_u);
690 *io_u = NULL;
691 }
692
693 return ret;
694}
695
Jens Axboe906c8d72006-06-13 09:37:56 +0200696/*
697 * The main verify engine. Runs over the writes we previusly submitted,
698 * reads the blocks back in, and checks the crc/md5 of the data.
699 */
Jens Axboeebac4652005-12-08 15:25:21 +0100700static void do_verify(struct thread_data *td)
701{
702 struct timeval t;
703 struct io_u *io_u, *v_io_u = NULL;
704 struct io_completion_data icd;
705 int ret;
706
707 td_set_runstate(td, TD_VERIFYING);
708
709 do {
710 if (td->terminate)
711 break;
712
713 gettimeofday(&t, NULL);
714 if (runtime_exceeded(td, &t))
715 break;
716
717 io_u = __get_io_u(td);
718 if (!io_u)
719 break;
720
Jens Axboeaea47d42006-05-26 19:27:29 +0200721 if (get_next_verify(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100722 put_io_u(td, io_u);
723 break;
724 }
725
Jens Axboeaea47d42006-05-26 19:27:29 +0200726 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100727 put_io_u(td, io_u);
728 break;
729 }
730
731 ret = io_u_queue(td, io_u);
732 if (ret) {
733 put_io_u(td, io_u);
734 td_verror(td, ret);
735 break;
736 }
737
738 /*
739 * we have one pending to verify, do that while
740 * we are doing io on the next one
741 */
742 if (do_io_u_verify(td, &v_io_u))
743 break;
744
745 ret = io_u_getevents(td, 1, 1, NULL);
746 if (ret != 1) {
747 if (ret < 0)
748 td_verror(td, ret);
749 break;
750 }
751
Jens Axboe2866c822006-10-09 15:57:48 +0200752 v_io_u = td->io_ops->event(td, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100753 icd.nr = 1;
754 icd.error = 0;
755 io_completed(td, v_io_u, &icd);
756
757 if (icd.error) {
758 td_verror(td, icd.error);
759 put_io_u(td, v_io_u);
760 v_io_u = NULL;
761 break;
762 }
763
Jens Axboeebac4652005-12-08 15:25:21 +0100764 /*
765 * if we can't submit more io, we need to verify now
766 */
767 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
768 break;
769
770 } while (1);
771
772 do_io_u_verify(td, &v_io_u);
773
774 if (td->cur_depth)
775 cleanup_pending_aio(td);
776
777 td_set_runstate(td, TD_RUNNING);
778}
779
Jens Axboe32cd46a2006-06-07 13:40:40 +0200780/*
Jens Axboeb990b5c2006-09-14 09:48:22 +0200781 * Not really an io thread, all it does is burn CPU cycles in the specified
782 * manner.
783 */
784static void do_cpuio(struct thread_data *td)
785{
786 struct timeval e;
787 int split = 100 / td->cpuload;
788 int i = 0;
789
790 while (!td->terminate) {
791 gettimeofday(&e, NULL);
792
793 if (runtime_exceeded(td, &e))
794 break;
795
796 if (!(i % split))
797 __usec_sleep(10000);
798 else
799 usec_sleep(td, 10000);
800
801 i++;
802 }
803}
804
805/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200806 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200807 * and reaps them, checking for rate and errors along the way.
808 */
Jens Axboeebac4652005-12-08 15:25:21 +0100809static void do_io(struct thread_data *td)
810{
811 struct io_completion_data icd;
812 struct timeval s, e;
813 unsigned long usec;
814
Jens Axboe5853e5a2006-06-08 14:41:05 +0200815 td_set_runstate(td, TD_RUNNING);
816
Jens Axboeebac4652005-12-08 15:25:21 +0100817 while (td->this_io_bytes[td->ddir] < td->io_size) {
818 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
819 struct timespec *timeout;
820 int ret, min_evts = 0;
821 struct io_u *io_u;
822
823 if (td->terminate)
824 break;
825
826 io_u = get_io_u(td);
827 if (!io_u)
828 break;
829
830 memcpy(&s, &io_u->start_time, sizeof(s));
831
832 ret = io_u_queue(td, io_u);
833 if (ret) {
834 put_io_u(td, io_u);
835 td_verror(td, ret);
836 break;
837 }
838
839 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
840
841 if (td->cur_depth < td->iodepth) {
842 timeout = &ts;
843 min_evts = 0;
844 } else {
845 timeout = NULL;
846 min_evts = 1;
847 }
848
849 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
850 if (ret < 0) {
851 td_verror(td, ret);
852 break;
853 } else if (!ret)
854 continue;
855
856 icd.nr = ret;
857 ios_completed(td, &icd);
858 if (icd.error) {
859 td_verror(td, icd.error);
860 break;
861 }
862
863 /*
864 * the rate is batched for now, it should work for batches
865 * of completions except the very first one which may look
866 * a little bursty
867 */
868 gettimeofday(&e, NULL);
869 usec = utime_since(&s, &e);
870
871 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
872
873 if (check_min_rate(td, &e)) {
874 td_verror(td, ENOMEM);
875 break;
876 }
877
878 if (runtime_exceeded(td, &e))
879 break;
880
881 if (td->thinktime)
882 usec_sleep(td, td->thinktime);
883
884 if (should_fsync(td) && td->fsync_blocks &&
885 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
886 sync_td(td);
887 }
888
889 if (td->cur_depth)
890 cleanup_pending_aio(td);
891
Jens Axboe5853e5a2006-06-08 14:41:05 +0200892 if (should_fsync(td) && td->end_fsync) {
893 td_set_runstate(td, TD_FSYNCING);
Jens Axboeebac4652005-12-08 15:25:21 +0100894 sync_td(td);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200895 }
Jens Axboeebac4652005-12-08 15:25:21 +0100896}
897
Jens Axboeebac4652005-12-08 15:25:21 +0100898static int init_io(struct thread_data *td)
899{
Jens Axboe2866c822006-10-09 15:57:48 +0200900 if (td->io_ops->init)
901 return td->io_ops->init(td);
902
903 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100904}
905
906static void cleanup_io_u(struct thread_data *td)
907{
908 struct list_head *entry, *n;
909 struct io_u *io_u;
910
911 list_for_each_safe(entry, n, &td->io_u_freelist) {
912 io_u = list_entry(entry, struct io_u, list);
913
914 list_del(&io_u->list);
915 free(io_u);
916 }
917
918 if (td->mem_type == MEM_MALLOC)
919 free(td->orig_buffer);
920 else if (td->mem_type == MEM_SHM) {
921 struct shmid_ds sbuf;
922
923 shmdt(td->orig_buffer);
924 shmctl(td->shm_id, IPC_RMID, &sbuf);
925 } else if (td->mem_type == MEM_MMAP)
926 munmap(td->orig_buffer, td->orig_buffer_size);
927 else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200928 log_err("Bad memory type %d\n", td->mem_type);
Jens Axboeebac4652005-12-08 15:25:21 +0100929
930 td->orig_buffer = NULL;
931}
932
933static int init_io_u(struct thread_data *td)
934{
935 struct io_u *io_u;
936 int i, max_units;
937 char *p;
938
Jens Axboe2866c822006-10-09 15:57:48 +0200939 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200940 return 0;
941
Jens Axboe2866c822006-10-09 15:57:48 +0200942 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +0100943 max_units = 1;
944 else
945 max_units = td->iodepth;
946
947 td->orig_buffer_size = td->max_bs * max_units + MASK;
948
949 if (td->mem_type == MEM_MALLOC)
950 td->orig_buffer = malloc(td->orig_buffer_size);
951 else if (td->mem_type == MEM_SHM) {
952 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
953 if (td->shm_id < 0) {
954 td_verror(td, errno);
955 perror("shmget");
956 return 1;
957 }
958
959 td->orig_buffer = shmat(td->shm_id, NULL, 0);
960 if (td->orig_buffer == (void *) -1) {
961 td_verror(td, errno);
962 perror("shmat");
963 td->orig_buffer = NULL;
964 return 1;
965 }
966 } else if (td->mem_type == MEM_MMAP) {
967 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
968 if (td->orig_buffer == MAP_FAILED) {
969 td_verror(td, errno);
970 perror("mmap");
971 td->orig_buffer = NULL;
972 return 1;
973 }
974 }
975
Jens Axboeebac4652005-12-08 15:25:21 +0100976 p = ALIGN(td->orig_buffer);
977 for (i = 0; i < max_units; i++) {
978 io_u = malloc(sizeof(*io_u));
979 memset(io_u, 0, sizeof(*io_u));
980 INIT_LIST_HEAD(&io_u->list);
981
982 io_u->buf = p + td->max_bs * i;
Jens Axboeb1ff3402006-02-17 11:35:58 +0100983 io_u->index = i;
Jens Axboeebac4652005-12-08 15:25:21 +0100984 list_add(&io_u->list, &td->io_u_freelist);
985 }
986
987 return 0;
988}
989
Jens Axboe9cc935a2006-06-13 09:11:18 +0200990static int create_file(struct thread_data *td, unsigned long long size)
Jens Axboeebac4652005-12-08 15:25:21 +0100991{
992 unsigned long long left;
993 unsigned int bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100994 char *b;
Jens Axboe9cc935a2006-06-13 09:11:18 +0200995 int r;
Jens Axboeebac4652005-12-08 15:25:21 +0100996
997 /*
998 * unless specifically asked for overwrite, let normal io extend it
999 */
Jens Axboe9cc935a2006-06-13 09:11:18 +02001000 if (!td->overwrite) {
Jens Axboec20d25b2006-06-12 14:56:43 +02001001 td->real_file_size = size;
Jens Axboeebac4652005-12-08 15:25:21 +01001002 return 0;
Jens Axboec20d25b2006-06-12 14:56:43 +02001003 }
Jens Axboeebac4652005-12-08 15:25:21 +01001004
1005 if (!size) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001006 log_err("Need size for create\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001007 td_verror(td, EINVAL);
1008 return 1;
1009 }
1010
Jens Axboe88c6ed82006-06-09 11:28:10 +02001011 temp_stall_ts = 1;
Jens Axboe9cc935a2006-06-13 09:11:18 +02001012 fprintf(f_out, "%s: Laying out IO file (%LuMiB)\n",td->name,size >> 20);
Jens Axboe88c6ed82006-06-09 11:28:10 +02001013
Jens Axboe9cc935a2006-06-13 09:11:18 +02001014 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
Jens Axboeebac4652005-12-08 15:25:21 +01001015 if (td->fd < 0) {
1016 td_verror(td, errno);
Jens Axboec20d25b2006-06-12 14:56:43 +02001017 goto done_noclose;
Jens Axboeebac4652005-12-08 15:25:21 +01001018 }
1019
Jens Axboe9cc935a2006-06-13 09:11:18 +02001020 if (ftruncate(td->fd, td->file_size) == -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001021 td_verror(td, errno);
Jens Axboec20d25b2006-06-12 14:56:43 +02001022 goto done;
Jens Axboeebac4652005-12-08 15:25:21 +01001023 }
1024
1025 td->io_size = td->file_size;
1026 b = malloc(td->max_bs);
1027 memset(b, 0, td->max_bs);
1028
1029 left = size;
1030 while (left && !td->terminate) {
1031 bs = td->max_bs;
1032 if (bs > left)
1033 bs = left;
1034
1035 r = write(td->fd, b, bs);
1036
1037 if (r == (int) bs) {
1038 left -= bs;
1039 continue;
1040 } else {
1041 if (r < 0)
1042 td_verror(td, errno);
1043 else
1044 td_verror(td, EIO);
1045
1046 break;
1047 }
1048 }
1049
1050 if (td->terminate)
1051 unlink(td->file_name);
1052 else if (td->create_fsync)
1053 fsync(td->fd);
1054
Jens Axboec20d25b2006-06-12 14:56:43 +02001055 free(b);
1056done:
Jens Axboeebac4652005-12-08 15:25:21 +01001057 close(td->fd);
1058 td->fd = -1;
Jens Axboec20d25b2006-06-12 14:56:43 +02001059done_noclose:
1060 temp_stall_ts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001061 return 0;
1062}
1063
1064static int file_size(struct thread_data *td)
1065{
1066 struct stat st;
1067
Jens Axboec20d25b2006-06-12 14:56:43 +02001068 if (td->overwrite) {
1069 if (fstat(td->fd, &st) == -1) {
1070 td_verror(td, errno);
1071 return 1;
1072 }
1073
1074 td->real_file_size = st.st_size;
1075
1076 if (!td->file_size || td->file_size > td->real_file_size)
1077 td->file_size = td->real_file_size;
Jens Axboeebac4652005-12-08 15:25:21 +01001078 }
1079
Jens Axboee8e387c2006-05-05 11:11:22 +02001080 td->file_size -= td->file_offset;
Jens Axboeebac4652005-12-08 15:25:21 +01001081 return 0;
1082}
1083
1084static int bdev_size(struct thread_data *td)
1085{
Jens Axboe9104f872005-12-28 23:50:45 +01001086 unsigned long long bytes;
Jens Axboeebac4652005-12-08 15:25:21 +01001087 int r;
1088
1089 r = blockdev_size(td->fd, &bytes);
1090 if (r) {
1091 td_verror(td, r);
1092 return 1;
1093 }
1094
Jens Axboe838a3cd2006-01-20 12:38:29 +01001095 td->real_file_size = bytes;
1096
Jens Axboeebac4652005-12-08 15:25:21 +01001097 /*
1098 * no extend possibilities, so limit size to device size if too large
1099 */
Jens Axboe838a3cd2006-01-20 12:38:29 +01001100 if (!td->file_size || td->file_size > td->real_file_size)
1101 td->file_size = td->real_file_size;
Jens Axboeebac4652005-12-08 15:25:21 +01001102
Jens Axboee8e387c2006-05-05 11:11:22 +02001103 td->file_size -= td->file_offset;
Jens Axboeebac4652005-12-08 15:25:21 +01001104 return 0;
1105}
1106
1107static int get_file_size(struct thread_data *td)
1108{
Jens Axboe0af7b542006-02-17 10:10:12 +01001109 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001110
1111 if (td->filetype == FIO_TYPE_FILE)
1112 ret = file_size(td);
Jens Axboe0af7b542006-02-17 10:10:12 +01001113 else if (td->filetype == FIO_TYPE_BD)
Jens Axboeebac4652005-12-08 15:25:21 +01001114 ret = bdev_size(td);
Jens Axboe0af7b542006-02-17 10:10:12 +01001115 else
1116 td->real_file_size = -1;
Jens Axboeebac4652005-12-08 15:25:21 +01001117
1118 if (ret)
1119 return ret;
1120
Jens Axboee8e387c2006-05-05 11:11:22 +02001121 if (td->file_offset > td->real_file_size) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001122 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size);
Jens Axboeebac4652005-12-08 15:25:21 +01001123 return 1;
1124 }
1125
Jens Axboe838a3cd2006-01-20 12:38:29 +01001126 td->io_size = td->file_size;
Jens Axboeebac4652005-12-08 15:25:21 +01001127 if (td->io_size == 0) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001128 log_err("%s: no io blocks\n", td->name);
Jens Axboeebac4652005-12-08 15:25:21 +01001129 td_verror(td, EINVAL);
1130 return 1;
1131 }
1132
Jens Axboe20dc95c2005-12-09 10:29:35 +01001133 if (!td->zone_size)
1134 td->zone_size = td->io_size;
1135
Jens Axboeebac4652005-12-08 15:25:21 +01001136 td->total_io_size = td->io_size * td->loops;
1137 return 0;
1138}
1139
1140static int setup_file_mmap(struct thread_data *td)
1141{
1142 int flags;
1143
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001144 if (td_rw(td))
1145 flags = PROT_READ | PROT_WRITE;
1146 else if (td_write(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +01001147 flags = PROT_WRITE;
1148
1149 if (td->verify != VERIFY_NONE)
1150 flags |= PROT_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001151 } else
1152 flags = PROT_READ;
Jens Axboeebac4652005-12-08 15:25:21 +01001153
1154 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1155 if (td->mmap == MAP_FAILED) {
1156 td->mmap = NULL;
1157 td_verror(td, errno);
1158 return 1;
1159 }
1160
1161 if (td->invalidate_cache) {
1162 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1163 td_verror(td, errno);
1164 return 1;
1165 }
1166 }
1167
1168 if (td->sequential) {
1169 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1170 td_verror(td, errno);
1171 return 1;
1172 }
1173 } else {
1174 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1175 td_verror(td, errno);
1176 return 1;
1177 }
1178 }
1179
1180 return 0;
1181}
1182
1183static int setup_file_plain(struct thread_data *td)
1184{
1185 if (td->invalidate_cache) {
1186 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1187 td_verror(td, errno);
1188 return 1;
1189 }
1190 }
1191
1192 if (td->sequential) {
1193 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1194 td_verror(td, errno);
1195 return 1;
1196 }
1197 } else {
1198 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1199 td_verror(td, errno);
1200 return 1;
1201 }
1202 }
1203
1204 return 0;
1205}
1206
1207static int setup_file(struct thread_data *td)
1208{
1209 struct stat st;
1210 int flags = 0;
1211
Jens Axboe2866c822006-10-09 15:57:48 +02001212 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +02001213 return 0;
1214
Jens Axboeebac4652005-12-08 15:25:21 +01001215 if (stat(td->file_name, &st) == -1) {
1216 if (errno != ENOENT) {
1217 td_verror(td, errno);
1218 return 1;
1219 }
1220 if (!td->create_file) {
1221 td_verror(td, ENOENT);
1222 return 1;
1223 }
Jens Axboe9cc935a2006-06-13 09:11:18 +02001224 if (create_file(td, td->file_size))
Jens Axboeebac4652005-12-08 15:25:21 +01001225 return 1;
Jens Axboe9cc935a2006-06-13 09:11:18 +02001226 } else if (td->filetype == FIO_TYPE_FILE &&
1227 st.st_size < (off_t) td->file_size) {
1228 if (create_file(td, td->file_size))
1229 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001230 }
1231
1232 if (td->odirect)
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001233 flags |= OS_O_DIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +01001234
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001235 if (td_write(td) || td_rw(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +01001236 if (td->filetype == FIO_TYPE_FILE) {
1237 if (!td->overwrite)
1238 flags |= O_TRUNC;
1239
1240 flags |= O_CREAT;
1241 }
1242 if (td->sync_io)
1243 flags |= O_SYNC;
1244
1245 flags |= O_RDWR;
1246
1247 td->fd = open(td->file_name, flags, 0600);
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001248 } else {
1249 if (td->filetype == FIO_TYPE_CHAR)
1250 flags |= O_RDWR;
1251 else
1252 flags |= O_RDONLY;
1253
1254 td->fd = open(td->file_name, flags);
Jens Axboeebac4652005-12-08 15:25:21 +01001255 }
1256
1257 if (td->fd == -1) {
1258 td_verror(td, errno);
1259 return 1;
1260 }
1261
1262 if (get_file_size(td))
1263 return 1;
1264
Jens Axboe2866c822006-10-09 15:57:48 +02001265 if (td->io_ops->flags & FIO_MMAPIO)
Jens Axboeebac4652005-12-08 15:25:21 +01001266 return setup_file_mmap(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001267 else
1268 return setup_file_plain(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001269}
1270
Jens Axboeda867742006-06-06 10:39:10 -07001271static int switch_ioscheduler(struct thread_data *td)
1272{
1273 char tmp[256], tmp2[128];
1274 FILE *f;
1275 int ret;
1276
1277 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1278
1279 f = fopen(tmp, "r+");
1280 if (!f) {
1281 td_verror(td, errno);
1282 return 1;
1283 }
1284
1285 /*
1286 * Set io scheduler.
1287 */
1288 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1289 if (ferror(f) || ret != 1) {
1290 td_verror(td, errno);
1291 fclose(f);
1292 return 1;
1293 }
1294
1295 rewind(f);
1296
1297 /*
1298 * Read back and check that the selected scheduler is now the default.
1299 */
1300 ret = fread(tmp, 1, sizeof(tmp), f);
1301 if (ferror(f) || ret < 0) {
1302 td_verror(td, errno);
1303 fclose(f);
1304 return 1;
1305 }
1306
1307 sprintf(tmp2, "[%s]", td->ioscheduler);
1308 if (!strstr(tmp, tmp2)) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001309 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -07001310 td_verror(td, EINVAL);
1311 fclose(f);
1312 return 1;
1313 }
1314
1315 fclose(f);
1316 return 0;
1317}
1318
Jens Axboeebac4652005-12-08 15:25:21 +01001319static void clear_io_state(struct thread_data *td)
1320{
Jens Axboe2866c822006-10-09 15:57:48 +02001321 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +01001322 lseek(td->fd, SEEK_SET, 0);
1323
Jens Axboe20dc95c2005-12-09 10:29:35 +01001324 td->last_pos = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001325 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1326 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +01001327 td->zone_bytes = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001328
1329 if (td->file_map)
1330 memset(td->file_map, 0, td->num_maps * sizeof(long));
1331}
1332
Jens Axboe906c8d72006-06-13 09:37:56 +02001333/*
1334 * Entry point for the thread based jobs. The process based jobs end up
1335 * here as well, after a little setup.
1336 */
Jens Axboeebac4652005-12-08 15:25:21 +01001337static void *thread_main(void *data)
1338{
1339 struct thread_data *td = data;
Jens Axboeebac4652005-12-08 15:25:21 +01001340
1341 if (!td->use_thread)
1342 setsid();
1343
1344 td->pid = getpid();
1345
Jens Axboeaea47d42006-05-26 19:27:29 +02001346 INIT_LIST_HEAD(&td->io_u_freelist);
1347 INIT_LIST_HEAD(&td->io_u_busylist);
1348 INIT_LIST_HEAD(&td->io_hist_list);
1349 INIT_LIST_HEAD(&td->io_log_list);
1350
Jens Axboeebac4652005-12-08 15:25:21 +01001351 if (init_io_u(td))
1352 goto err;
1353
1354 if (fio_setaffinity(td) == -1) {
1355 td_verror(td, errno);
1356 goto err;
1357 }
1358
1359 if (init_io(td))
1360 goto err;
1361
Jens Axboeaea47d42006-05-26 19:27:29 +02001362 if (init_iolog(td))
1363 goto err;
1364
Jens Axboeebac4652005-12-08 15:25:21 +01001365 if (td->ioprio) {
1366 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1367 td_verror(td, errno);
1368 goto err;
1369 }
1370 }
1371
Jens Axboe1056eaa2006-07-13 10:33:45 -07001372 if (nice(td->nice) == -1) {
Jens Axboeb6f4d882006-06-02 10:32:51 +02001373 td_verror(td, errno);
1374 goto err;
1375 }
1376
Jens Axboe75154842006-06-01 13:56:09 +02001377 if (init_random_state(td))
1378 goto err;
1379
Jens Axboeda867742006-06-06 10:39:10 -07001380 if (td->ioscheduler && switch_ioscheduler(td))
1381 goto err;
1382
Jens Axboe75154842006-06-01 13:56:09 +02001383 td_set_runstate(td, TD_INITIALIZED);
Jens Axboebbfd6b02006-06-07 19:42:54 +02001384 fio_sem_up(&startup_sem);
1385 fio_sem_down(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001386
1387 if (!td->create_serialize && setup_file(td))
1388 goto err;
1389
Jens Axboeebac4652005-12-08 15:25:21 +01001390 gettimeofday(&td->epoch, NULL);
1391
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001392 if (td->exec_prerun)
1393 system(td->exec_prerun);
1394
Jens Axboeebac4652005-12-08 15:25:21 +01001395 while (td->loops--) {
1396 getrusage(RUSAGE_SELF, &td->ru_start);
1397 gettimeofday(&td->start, NULL);
1398 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1399
1400 if (td->ratemin)
1401 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1402
1403 clear_io_state(td);
1404 prune_io_piece_log(td);
1405
Jens Axboe2866c822006-10-09 15:57:48 +02001406 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +02001407 do_cpuio(td);
1408 else
1409 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001410
1411 td->runtime[td->ddir] += mtime_since_now(&td->start);
Jens Axboeaea47d42006-05-26 19:27:29 +02001412 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001413 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1414
Jens Axboeebac4652005-12-08 15:25:21 +01001415 update_rusage_stat(td);
1416
1417 if (td->error || td->terminate)
1418 break;
1419
1420 if (td->verify == VERIFY_NONE)
1421 continue;
1422
1423 clear_io_state(td);
1424 gettimeofday(&td->start, NULL);
1425
1426 do_verify(td);
1427
1428 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1429
1430 if (td->error || td->terminate)
1431 break;
1432 }
1433
Jens Axboeebac4652005-12-08 15:25:21 +01001434 if (td->bw_log)
1435 finish_log(td, td->bw_log, "bw");
1436 if (td->slat_log)
1437 finish_log(td, td->slat_log, "slat");
1438 if (td->clat_log)
1439 finish_log(td, td->clat_log, "clat");
Jens Axboe843a7412006-06-01 21:14:21 -07001440 if (td->write_iolog)
1441 write_iolog_close(td);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001442 if (td->exec_postrun)
1443 system(td->exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +01001444
1445 if (exitall_on_terminate)
1446 terminate_threads(td->groupid);
1447
1448err:
1449 if (td->fd != -1) {
1450 close(td->fd);
1451 td->fd = -1;
1452 }
1453 if (td->mmap)
1454 munmap(td->mmap, td->file_size);
Jens Axboe2866c822006-10-09 15:57:48 +02001455 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001456 cleanup_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001457 td_set_runstate(td, TD_EXITED);
1458 return NULL;
1459
1460}
1461
Jens Axboe906c8d72006-06-13 09:37:56 +02001462/*
1463 * We cannot pass the td data into a forked process, so attach the td and
1464 * pass it to the thread worker.
1465 */
Jens Axboeebac4652005-12-08 15:25:21 +01001466static void *fork_main(int shmid, int offset)
1467{
1468 struct thread_data *td;
1469 void *data;
1470
1471 data = shmat(shmid, NULL, 0);
1472 if (data == (void *) -1) {
1473 perror("shmat");
1474 return NULL;
1475 }
1476
1477 td = data + offset * sizeof(struct thread_data);
1478 thread_main(td);
1479 shmdt(data);
1480 return NULL;
1481}
1482
Jens Axboe906c8d72006-06-13 09:37:56 +02001483/*
1484 * Sets the status of the 'td' in the printed status map.
1485 */
Jens Axboeebac4652005-12-08 15:25:21 +01001486static void check_str_update(struct thread_data *td)
1487{
1488 char c = run_str[td->thread_number - 1];
1489
Jens Axboeebac4652005-12-08 15:25:21 +01001490 switch (td->runstate) {
1491 case TD_REAPED:
1492 c = '_';
1493 break;
1494 case TD_EXITED:
1495 c = 'E';
1496 break;
1497 case TD_RUNNING:
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001498 if (td_rw(td)) {
1499 if (td->sequential)
1500 c = 'M';
1501 else
1502 c = 'm';
1503 } else if (td_read(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +01001504 if (td->sequential)
1505 c = 'R';
1506 else
1507 c = 'r';
1508 } else {
1509 if (td->sequential)
1510 c = 'W';
1511 else
1512 c = 'w';
1513 }
1514 break;
1515 case TD_VERIFYING:
1516 c = 'V';
1517 break;
Jens Axboe5853e5a2006-06-08 14:41:05 +02001518 case TD_FSYNCING:
1519 c = 'F';
1520 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001521 case TD_CREATED:
1522 c = 'C';
1523 break;
Jens Axboe75154842006-06-01 13:56:09 +02001524 case TD_INITIALIZED:
1525 c = 'I';
1526 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001527 case TD_NOT_CREATED:
1528 c = 'P';
1529 break;
1530 default:
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001531 log_err("state %d\n", td->runstate);
Jens Axboeebac4652005-12-08 15:25:21 +01001532 }
1533
1534 run_str[td->thread_number - 1] = c;
Jens Axboeebac4652005-12-08 15:25:21 +01001535}
1536
Jens Axboe906c8d72006-06-13 09:37:56 +02001537/*
1538 * Convert seconds to a printable string.
1539 */
Jens Axboe5289b842005-12-08 20:47:28 +01001540static void eta_to_str(char *str, int eta_sec)
1541{
1542 unsigned int d, h, m, s;
1543 static int always_d, always_h;
1544
1545 d = h = m = s = 0;
1546
1547 s = eta_sec % 60;
1548 eta_sec /= 60;
1549 m = eta_sec % 60;
1550 eta_sec /= 60;
1551 h = eta_sec % 24;
1552 eta_sec /= 24;
1553 d = eta_sec;
1554
1555 if (d || always_d) {
1556 always_d = 1;
1557 str += sprintf(str, "%02dd:", d);
1558 }
1559 if (h || always_h) {
1560 always_h = 1;
1561 str += sprintf(str, "%02dh:", h);
1562 }
1563
1564 str += sprintf(str, "%02dm:", m);
1565 str += sprintf(str, "%02ds", s);
1566}
1567
Jens Axboe906c8d72006-06-13 09:37:56 +02001568/*
1569 * Best effort calculation of the estimated pending runtime of a job.
1570 */
Jens Axboe6a0106a2006-05-05 10:34:43 +02001571static int thread_eta(struct thread_data *td, unsigned long elapsed)
1572{
1573 unsigned long long bytes_total, bytes_done;
1574 unsigned int eta_sec = 0;
1575
1576 bytes_total = td->total_io_size;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001577
1578 /*
1579 * if writing, bytes_total will be twice the size. If mixing,
1580 * assume a 50/50 split and thus bytes_total will be 50% larger.
1581 */
1582 if (td->verify) {
1583 if (td_rw(td))
1584 bytes_total = bytes_total * 3 / 2;
1585 else
1586 bytes_total <<= 1;
1587 }
Jens Axboe6a0106a2006-05-05 10:34:43 +02001588 if (td->zone_size && td->zone_skip)
1589 bytes_total /= (td->zone_skip / td->zone_size);
1590
1591 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1592 double perc;
Jens Axboe8b611c32006-05-25 01:46:59 +02001593
Jens Axboe6a0106a2006-05-05 10:34:43 +02001594 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1595 perc = (double) bytes_done / (double) bytes_total;
1596 if (perc > 1.0)
1597 perc = 1.0;
1598
1599 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1600
Jens Axboe8b611c32006-05-25 01:46:59 +02001601 if (td->timeout && eta_sec > (td->timeout - elapsed))
Jens Axboe6a0106a2006-05-05 10:34:43 +02001602 eta_sec = td->timeout - elapsed;
Jens Axboe75154842006-06-01 13:56:09 +02001603 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1604 || td->runstate == TD_INITIALIZED) {
Jens Axboe6a0106a2006-05-05 10:34:43 +02001605 int t_eta = 0, r_eta = 0;
1606
1607 /*
1608 * We can only guess - assume it'll run the full timeout
1609 * if given, otherwise assume it'll run at the specified rate.
1610 */
1611 if (td->timeout)
1612 t_eta = td->timeout + td->start_delay - elapsed;
1613 if (td->rate) {
1614 r_eta = (bytes_total / 1024) / td->rate;
1615 r_eta += td->start_delay - elapsed;
1616 }
1617
1618 if (r_eta && t_eta)
1619 eta_sec = min(r_eta, t_eta);
1620 else if (r_eta)
1621 eta_sec = r_eta;
1622 else if (t_eta)
1623 eta_sec = t_eta;
1624 else
Jens Axboe972cfd22006-06-09 11:08:56 +02001625 eta_sec = 0;
Jens Axboe6a0106a2006-05-05 10:34:43 +02001626 } else {
1627 /*
Jens Axboe5853e5a2006-06-08 14:41:05 +02001628 * thread is already done or waiting for fsync
Jens Axboe6a0106a2006-05-05 10:34:43 +02001629 */
1630 eta_sec = 0;
1631 }
1632
1633 return eta_sec;
1634}
1635
Jens Axboe906c8d72006-06-13 09:37:56 +02001636/*
1637 * Print status of the jobs we know about. This includes rate estimates,
1638 * ETA, thread state, etc.
1639 */
Jens Axboeebac4652005-12-08 15:25:21 +01001640static void print_thread_status(void)
1641{
Jens Axboe6a0106a2006-05-05 10:34:43 +02001642 unsigned long elapsed = time_since_now(&genesis);
Jens Axboe71a751c2006-06-08 14:48:47 +02001643 int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec;
Jens Axboe5289b842005-12-08 20:47:28 +01001644 char eta_str[32];
Jens Axboe6a0106a2006-05-05 10:34:43 +02001645 double perc = 0.0;
Jens Axboeebac4652005-12-08 15:25:21 +01001646
Jens Axboec6ae0a52006-06-12 11:04:44 +02001647 if (temp_stall_ts || terse_output)
Jens Axboe88c6ed82006-06-09 11:28:10 +02001648 return;
1649
Jens Axboe6a0106a2006-05-05 10:34:43 +02001650 eta_secs = malloc(thread_number * sizeof(int));
1651 memset(eta_secs, 0, thread_number * sizeof(int));
1652
Jens Axboe71a751c2006-06-08 14:48:47 +02001653 nr_pending = nr_running = t_rate = m_rate = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001654 for (i = 0; i < thread_number; i++) {
1655 struct thread_data *td = &threads[i];
1656
Jens Axboe5853e5a2006-06-08 14:41:05 +02001657 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING||
1658 td->runstate == TD_FSYNCING) {
Jens Axboeebac4652005-12-08 15:25:21 +01001659 nr_running++;
1660 t_rate += td->rate;
1661 m_rate += td->ratemin;
Jens Axboe71a751c2006-06-08 14:48:47 +02001662 } else if (td->runstate < TD_RUNNING)
1663 nr_pending++;
Jens Axboeebac4652005-12-08 15:25:21 +01001664
Jens Axboe6a0106a2006-05-05 10:34:43 +02001665 if (elapsed >= 3)
1666 eta_secs[i] = thread_eta(td, elapsed);
Jens Axboe8b611c32006-05-25 01:46:59 +02001667 else
1668 eta_secs[i] = INT_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +01001669
1670 check_str_update(td);
1671 }
1672
Jens Axboe6a0106a2006-05-05 10:34:43 +02001673 if (exitall_on_terminate)
1674 eta_sec = INT_MAX;
1675 else
1676 eta_sec = 0;
Jens Axboe5289b842005-12-08 20:47:28 +01001677
Jens Axboe6a0106a2006-05-05 10:34:43 +02001678 for (i = 0; i < thread_number; i++) {
1679 if (exitall_on_terminate) {
1680 if (eta_secs[i] < eta_sec)
1681 eta_sec = eta_secs[i];
1682 } else {
1683 if (eta_secs[i] > eta_sec)
1684 eta_sec = eta_secs[i];
Jens Axboe5289b842005-12-08 20:47:28 +01001685 }
Jens Axboe6a0106a2006-05-05 10:34:43 +02001686 }
Jens Axboe5289b842005-12-08 20:47:28 +01001687
Jens Axboe8b611c32006-05-25 01:46:59 +02001688 if (eta_sec != INT_MAX && elapsed) {
Jens Axboe6a0106a2006-05-05 10:34:43 +02001689 perc = (double) elapsed / (double) (elapsed + eta_sec);
1690 eta_to_str(eta_str, eta_sec);
Jens Axboeebac4652005-12-08 15:25:21 +01001691 }
1692
Jens Axboe71a751c2006-06-08 14:48:47 +02001693 if (!nr_running && !nr_pending)
Jens Axboef44c1d42006-06-08 14:46:24 +02001694 return;
1695
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001696 printf("Threads running: %d", nr_running);
Jens Axboeebac4652005-12-08 15:25:21 +01001697 if (m_rate || t_rate)
1698 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
Jens Axboe30521452006-07-28 15:05:34 +02001699 if (eta_sec != INT_MAX && nr_running) {
Jens Axboe6a0106a2006-05-05 10:34:43 +02001700 perc *= 100.0;
1701 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1702 }
Jens Axboe5289b842005-12-08 20:47:28 +01001703 printf("\r");
Jens Axboeebac4652005-12-08 15:25:21 +01001704 fflush(stdout);
Jens Axboe6a0106a2006-05-05 10:34:43 +02001705 free(eta_secs);
Jens Axboeebac4652005-12-08 15:25:21 +01001706}
1707
Jens Axboe906c8d72006-06-13 09:37:56 +02001708/*
1709 * Run over the job map and reap the threads that have exited, if any.
1710 */
Jens Axboeebac4652005-12-08 15:25:21 +01001711static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1712{
Jens Axboeb990b5c2006-09-14 09:48:22 +02001713 int i, cputhreads;
Jens Axboeebac4652005-12-08 15:25:21 +01001714
1715 /*
1716 * reap exited threads (TD_EXITED -> TD_REAPED)
1717 */
Jens Axboeb990b5c2006-09-14 09:48:22 +02001718 for (i = 0, cputhreads = 0; i < thread_number; i++) {
Jens Axboeebac4652005-12-08 15:25:21 +01001719 struct thread_data *td = &threads[i];
1720
Jens Axboe2866c822006-10-09 15:57:48 +02001721 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +02001722 cputhreads++;
1723
Jens Axboeebac4652005-12-08 15:25:21 +01001724 if (td->runstate != TD_EXITED)
1725 continue;
1726
1727 td_set_runstate(td, TD_REAPED);
1728
1729 if (td->use_thread) {
1730 long ret;
1731
1732 if (pthread_join(td->thread, (void *) &ret))
1733 perror("thread_join");
1734 } else
1735 waitpid(td->pid, NULL, 0);
1736
1737 (*nr_running)--;
1738 (*m_rate) -= td->ratemin;
1739 (*t_rate) -= td->rate;
1740 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001741
1742 if (*nr_running == cputhreads)
1743 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001744}
1745
Jens Axboefcb6ade2006-05-31 12:14:35 +02001746static void fio_unpin_memory(void *pinned)
1747{
1748 if (pinned) {
1749 if (munlock(pinned, mlock_size) < 0)
1750 perror("munlock");
1751 munmap(pinned, mlock_size);
1752 }
1753}
1754
1755static void *fio_pin_memory(void)
1756{
Jens Axboe32cd46a2006-06-07 13:40:40 +02001757 unsigned long long phys_mem;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001758 void *ptr;
1759
1760 if (!mlock_size)
1761 return NULL;
1762
1763 /*
1764 * Don't allow mlock of more than real_mem-128MB
1765 */
Jens Axboe32cd46a2006-06-07 13:40:40 +02001766 phys_mem = os_phys_mem();
1767 if (phys_mem) {
1768 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1769 mlock_size = phys_mem - 128 * 1024 * 1024;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001770 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
Jens Axboefcb6ade2006-05-31 12:14:35 +02001771 }
1772 }
1773
1774 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1775 if (!ptr) {
1776 perror("malloc locked mem");
1777 return NULL;
1778 }
1779 if (mlock(ptr, mlock_size) < 0) {
1780 munmap(ptr, mlock_size);
1781 perror("mlock");
1782 return NULL;
1783 }
1784
1785 return ptr;
1786}
1787
Jens Axboe906c8d72006-06-13 09:37:56 +02001788/*
1789 * Main function for kicking off and reaping jobs, as needed.
1790 */
Jens Axboeebac4652005-12-08 15:25:21 +01001791static void run_threads(void)
1792{
Jens Axboeebac4652005-12-08 15:25:21 +01001793 struct thread_data *td;
1794 unsigned long spent;
1795 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001796 void *mlocked_mem;
1797
1798 mlocked_mem = fio_pin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001799
Jens Axboec6ae0a52006-06-12 11:04:44 +02001800 if (!terse_output) {
1801 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1802 fflush(stdout);
1803 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001804
Jens Axboe4efa9702006-05-31 11:56:49 +02001805 signal(SIGINT, sig_handler);
1806 signal(SIGALRM, sig_handler);
1807
Jens Axboeebac4652005-12-08 15:25:21 +01001808 todo = thread_number;
1809 nr_running = 0;
1810 nr_started = 0;
1811 m_rate = t_rate = 0;
1812
1813 for (i = 0; i < thread_number; i++) {
1814 td = &threads[i];
1815
1816 run_str[td->thread_number - 1] = 'P';
1817
1818 init_disk_util(td);
1819
1820 if (!td->create_serialize)
1821 continue;
1822
1823 /*
1824 * do file setup here so it happens sequentially,
1825 * we don't want X number of threads getting their
1826 * client data interspersed on disk
1827 */
1828 if (setup_file(td)) {
1829 td_set_runstate(td, TD_REAPED);
1830 todo--;
1831 }
1832 }
1833
1834 gettimeofday(&genesis, NULL);
1835
1836 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001837 struct thread_data *map[MAX_JOBS];
1838 struct timeval this_start;
1839 int this_jobs = 0, left;
1840
Jens Axboeebac4652005-12-08 15:25:21 +01001841 /*
1842 * create threads (TD_NOT_CREATED -> TD_CREATED)
1843 */
1844 for (i = 0; i < thread_number; i++) {
1845 td = &threads[i];
1846
1847 if (td->runstate != TD_NOT_CREATED)
1848 continue;
1849
1850 /*
1851 * never got a chance to start, killed by other
1852 * thread for some reason
1853 */
1854 if (td->terminate) {
1855 todo--;
1856 continue;
1857 }
1858
1859 if (td->start_delay) {
1860 spent = mtime_since_now(&genesis);
1861
1862 if (td->start_delay * 1000 > spent)
1863 continue;
1864 }
1865
1866 if (td->stonewall && (nr_started || nr_running))
1867 break;
1868
Jens Axboe75154842006-06-01 13:56:09 +02001869 /*
1870 * Set state to created. Thread will transition
1871 * to TD_INITIALIZED when it's done setting up.
1872 */
Jens Axboeebac4652005-12-08 15:25:21 +01001873 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001874 map[this_jobs++] = td;
Jens Axboebbfd6b02006-06-07 19:42:54 +02001875 fio_sem_init(&startup_sem, 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001876 nr_started++;
1877
1878 if (td->use_thread) {
1879 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1880 perror("thread_create");
1881 nr_started--;
1882 }
1883 } else {
1884 if (fork())
Jens Axboebbfd6b02006-06-07 19:42:54 +02001885 fio_sem_down(&startup_sem);
Jens Axboeebac4652005-12-08 15:25:21 +01001886 else {
1887 fork_main(shm_id, i);
1888 exit(0);
1889 }
1890 }
1891 }
1892
1893 /*
Jens Axboe75154842006-06-01 13:56:09 +02001894 * Wait for the started threads to transition to
1895 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001896 */
Jens Axboe75154842006-06-01 13:56:09 +02001897 gettimeofday(&this_start, NULL);
1898 left = this_jobs;
1899 while (left) {
1900 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1901 break;
1902
1903 usleep(100000);
1904
1905 for (i = 0; i < this_jobs; i++) {
1906 td = map[i];
1907 if (!td)
1908 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001909 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001910 map[i] = NULL;
1911 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001912 } else if (td->runstate >= TD_EXITED) {
1913 map[i] = NULL;
1914 left--;
1915 todo--;
1916 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001917 }
1918 }
1919 }
1920
1921 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001922 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001923 for (i = 0; i < this_jobs; i++) {
1924 td = map[i];
1925 if (!td)
1926 continue;
1927 kill(td->pid, SIGTERM);
1928 }
1929 break;
1930 }
1931
1932 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001933 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001934 */
Jens Axboeebac4652005-12-08 15:25:21 +01001935 for (i = 0; i < thread_number; i++) {
1936 td = &threads[i];
1937
Jens Axboe75154842006-06-01 13:56:09 +02001938 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001939 continue;
1940
1941 td_set_runstate(td, TD_RUNNING);
1942 nr_running++;
1943 nr_started--;
1944 m_rate += td->ratemin;
1945 t_rate += td->rate;
Jens Axboe75154842006-06-01 13:56:09 +02001946 todo--;
Jens Axboebbfd6b02006-06-07 19:42:54 +02001947 fio_sem_up(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001948 }
1949
1950 reap_threads(&nr_running, &t_rate, &m_rate);
1951
1952 if (todo)
1953 usleep(100000);
1954 }
1955
1956 while (nr_running) {
1957 reap_threads(&nr_running, &t_rate, &m_rate);
1958 usleep(10000);
1959 }
1960
1961 update_io_ticks();
Jens Axboefcb6ade2006-05-31 12:14:35 +02001962 fio_unpin_memory(mlocked_mem);
Jens Axboeebac4652005-12-08 15:25:21 +01001963}
1964
Jens Axboeebac4652005-12-08 15:25:21 +01001965int main(int argc, char *argv[])
1966{
1967 if (parse_options(argc, argv))
1968 return 1;
1969
1970 if (!thread_number) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001971 log_err("Nothing to do\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001972 return 1;
1973 }
1974
1975 disk_util_timer_arm();
1976
1977 run_threads();
1978 show_run_stats();
1979
1980 return 0;
1981}