blob: 82f321bfee82ccb9ff46f48f7bd0aeed07f217d2 [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>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
Jens Axboeebac4652005-12-08 15:25:21 +010021#include <unistd.h>
22#include <fcntl.h>
23#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010024#include <signal.h>
25#include <time.h>
Jens Axboeebac4652005-12-08 15:25:21 +010026#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <sys/stat.h>
28#include <sys/wait.h>
29#include <sys/ipc.h>
30#include <sys/shm.h>
31#include <sys/ioctl.h>
32#include <sys/mman.h>
33
34#include "fio.h"
35#include "os.h"
36
37#define MASK (4095)
38
39#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
40
41int groupid = 0;
42int thread_number = 0;
43static char run_str[MAX_JOBS + 1];
44int shm_id = 0;
Jens Axboe5289b842005-12-08 20:47:28 +010045static struct timeval genesis;
Jens Axboe88c6ed82006-06-09 11:28:10 +020046static int temp_stall_ts;
Jens Axboeebac4652005-12-08 15:25:21 +010047
Jens Axboeebac4652005-12-08 15:25:21 +010048static void print_thread_status(void);
49
Jens Axboec04f7ec2006-05-31 10:13:16 +020050extern unsigned long long mlock_size;
51
Jens Axboeebac4652005-12-08 15:25:21 +010052/*
Jens Axboe79809112006-06-09 10:14:54 +020053 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
54 * will never back again. It may cycle between running/verififying/fsyncing.
55 * Once the thread reaches TD_EXITED, it is just waiting for the core to
56 * reap it.
Jens Axboeebac4652005-12-08 15:25:21 +010057 */
58enum {
59 TD_NOT_CREATED = 0,
60 TD_CREATED,
Jens Axboe75154842006-06-01 13:56:09 +020061 TD_INITIALIZED,
Jens Axboeebac4652005-12-08 15:25:21 +010062 TD_RUNNING,
63 TD_VERIFYING,
Jens Axboe5853e5a2006-06-08 14:41:05 +020064 TD_FSYNCING,
Jens Axboeebac4652005-12-08 15:25:21 +010065 TD_EXITED,
66 TD_REAPED,
67};
68
Jens Axboe3d60d1e2006-05-25 06:31:06 +020069#define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
Jens Axboeebac4652005-12-08 15:25:21 +010070
Jens Axboebbfd6b02006-06-07 19:42:54 +020071static volatile int startup_sem;
Jens Axboeebac4652005-12-08 15:25:21 +010072
73#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020074#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010075
76static void terminate_threads(int group_id)
77{
78 int i;
79
80 for (i = 0; i < thread_number; i++) {
81 struct thread_data *td = &threads[i];
82
83 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
84 td->terminate = 1;
85 td->start_delay = 0;
86 }
87 }
88}
89
90static void sig_handler(int sig)
91{
92 switch (sig) {
93 case SIGALRM:
94 update_io_ticks();
95 disk_util_timer_arm();
96 print_thread_status();
97 break;
98 default:
99 printf("\nfio: terminating on signal\n");
100 fflush(stdout);
101 terminate_threads(TERMINATE_ALL);
102 break;
103 }
104}
105
Jens Axboeebac4652005-12-08 15:25:21 +0100106static int random_map_free(struct thread_data *td, unsigned long long block)
107{
108 unsigned int idx = RAND_MAP_IDX(td, block);
109 unsigned int bit = RAND_MAP_BIT(td, block);
110
111 return (td->file_map[idx] & (1UL << bit)) == 0;
112}
113
114static int get_next_free_block(struct thread_data *td, unsigned long long *b)
115{
116 int i;
117
118 *b = 0;
119 i = 0;
120 while ((*b) * td->min_bs < td->io_size) {
121 if (td->file_map[i] != -1UL) {
122 *b += ffz(td->file_map[i]);
123 return 0;
124 }
125
126 *b += BLOCKS_PER_MAP;
127 i++;
128 }
129
130 return 1;
131}
132
133static void mark_random_map(struct thread_data *td, struct io_u *io_u)
134{
Jens Axboe200bc852006-06-01 16:10:12 +0200135 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100136 unsigned int blocks = 0;
137
138 while (blocks < (io_u->buflen / td->min_bs)) {
139 unsigned int idx, bit;
140
141 if (!random_map_free(td, block))
142 break;
143
144 idx = RAND_MAP_IDX(td, block);
145 bit = RAND_MAP_BIT(td, block);
146
147 assert(idx < td->num_maps);
148
149 td->file_map[idx] |= (1UL << bit);
150 block++;
151 blocks++;
152 }
153
154 if ((blocks * td->min_bs) < io_u->buflen)
155 io_u->buflen = blocks * td->min_bs;
156}
157
Jens Axboe20dc95c2005-12-09 10:29:35 +0100158static int get_next_offset(struct thread_data *td, unsigned long long *offset)
159{
160 unsigned long long b, rb;
161 long r;
162
163 if (!td->sequential) {
Jens Axboe085227a2006-06-01 15:54:09 -0700164 unsigned long long max_blocks = td->io_size / td->min_bs;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100165 int loops = 50;
166
167 do {
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200168 r = os_random_long(&td->random_state);
Jens Axboe085227a2006-06-01 15:54:09 -0700169 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
Jens Axboe20dc95c2005-12-09 10:29:35 +0100170 rb = b + (td->file_offset / td->min_bs);
171 loops--;
172 } while (!random_map_free(td, rb) && loops);
173
174 if (!loops) {
175 if (get_next_free_block(td, &b))
176 return 1;
177 }
178 } else
179 b = td->last_pos / td->min_bs;
180
181 *offset = (b * td->min_bs) + td->file_offset;
Jens Axboe838a3cd2006-01-20 12:38:29 +0100182 if (*offset > td->real_file_size)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100183 return 1;
184
185 return 0;
186}
187
188static unsigned int get_next_buflen(struct thread_data *td)
189{
190 unsigned int buflen;
191 long r;
192
193 if (td->min_bs == td->max_bs)
194 buflen = td->min_bs;
195 else {
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200196 r = os_random_long(&td->bsrange_state);
Jens Axboe20dc95c2005-12-09 10:29:35 +0100197 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
198 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
199 }
200
201 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
202 buflen = td->io_size - td->this_io_bytes[td->ddir];
203
204 return buflen;
205}
206
Jens Axboeebac4652005-12-08 15:25:21 +0100207static int check_min_rate(struct thread_data *td, struct timeval *now)
208{
209 unsigned long spent;
210 unsigned long rate;
211 int ddir = td->ddir;
212
213 /*
214 * allow a 2 second settle period in the beginning
215 */
216 if (mtime_since(&td->start, now) < 2000)
217 return 0;
218
219 /*
220 * if rate blocks is set, sample is running
221 */
222 if (td->rate_bytes) {
223 spent = mtime_since(&td->lastrate, now);
224 if (spent < td->ratecycle)
225 return 0;
226
227 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
228 if (rate < td->ratemin) {
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200229 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 +0100230 if (rate_quit)
231 terminate_threads(td->groupid);
232 return 1;
233 }
234 }
235
236 td->rate_bytes = td->this_io_bytes[ddir];
237 memcpy(&td->lastrate, now, sizeof(*now));
238 return 0;
239}
240
241static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
242{
243 if (!td->timeout)
244 return 0;
245 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
246 return 1;
247
248 return 0;
249}
250
251static void fill_random_bytes(struct thread_data *td,
252 unsigned char *p, unsigned int len)
253{
254 unsigned int todo;
255 double r;
256
257 while (len) {
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200258 r = os_random_double(&td->verify_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100259
260 /*
261 * lrand48_r seems to be broken and only fill the bottom
262 * 32-bits, even on 64-bit archs with 64-bit longs
263 */
264 todo = sizeof(r);
265 if (todo > len)
266 todo = len;
267
268 memcpy(p, &r, todo);
269
270 len -= todo;
271 p += todo;
272 }
273}
274
275static void hexdump(void *buffer, int len)
276{
277 unsigned char *p = buffer;
278 int i;
279
280 for (i = 0; i < len; i++)
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200281 fprintf(f_out, "%02x", p[i]);
282 fprintf(f_out, "\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100283}
284
285static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
286{
287 unsigned char *p = (unsigned char *) io_u->buf;
288 unsigned long c;
Jens Axboeebac4652005-12-08 15:25:21 +0100289
290 p += sizeof(*hdr);
291 c = crc32(p, hdr->len - sizeof(*hdr));
Jens Axboeebac4652005-12-08 15:25:21 +0100292
Jens Axboe22f78b32006-06-07 11:30:07 +0200293 if (c != hdr->crc32) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200294 log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
295 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
Jens Axboe22f78b32006-06-07 11:30:07 +0200296 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100297 }
298
Jens Axboe22f78b32006-06-07 11:30:07 +0200299 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100300}
301
302static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
303{
304 unsigned char *p = (unsigned char *) io_u->buf;
305 struct md5_ctx md5_ctx;
Jens Axboeebac4652005-12-08 15:25:21 +0100306
307 memset(&md5_ctx, 0, sizeof(md5_ctx));
308 p += sizeof(*hdr);
309 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
310
Jens Axboe22f78b32006-06-07 11:30:07 +0200311 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200312 log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
Jens Axboeebac4652005-12-08 15:25:21 +0100313 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
314 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
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(struct io_u *io_u)
322{
323 struct verify_header *hdr = (struct verify_header *) io_u->buf;
324 int ret;
325
326 if (hdr->fio_magic != FIO_HDR_MAGIC)
327 return 1;
328
329 if (hdr->verify_type == VERIFY_MD5)
330 ret = verify_io_u_md5(hdr, io_u);
331 else if (hdr->verify_type == VERIFY_CRC32)
332 ret = verify_io_u_crc32(hdr, io_u);
333 else {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200334 log_err("Bad verify type %d\n", hdr->verify_type);
Jens Axboeebac4652005-12-08 15:25:21 +0100335 ret = 1;
336 }
337
338 return ret;
339}
340
341static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
342{
343 hdr->crc32 = crc32(p, len);
344}
345
346static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
347{
348 struct md5_ctx md5_ctx;
349
350 memset(&md5_ctx, 0, sizeof(md5_ctx));
351 md5_update(&md5_ctx, p, len);
352 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
353}
354
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200355static int get_rw_ddir(struct thread_data *td)
356{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200357 if (td_rw(td)) {
358 struct timeval now;
359 unsigned long elapsed;
360
361 gettimeofday(&now, NULL);
362 elapsed = mtime_since_now(&td->rwmix_switch);
363
364 /*
365 * Check if it's time to seed a new data direction.
366 */
367 if (elapsed >= td->rwmixcycle) {
Jens Axboec1ee2ca2006-06-07 22:42:37 +0200368 int v;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200369 long r;
370
Jens Axboec1ee2ca2006-06-07 22:42:37 +0200371 r = os_random_long(&td->rwmix_state);
372 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200373 if (v < td->rwmixread)
374 td->rwmix_ddir = DDIR_READ;
375 else
376 td->rwmix_ddir = DDIR_WRITE;
377 memcpy(&td->rwmix_switch, &now, sizeof(now));
378 }
379 return td->rwmix_ddir;
380 } else if (td_read(td))
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200381 return DDIR_READ;
382 else
383 return DDIR_WRITE;
384}
385
Jens Axboeebac4652005-12-08 15:25:21 +0100386/*
387 * fill body of io_u->buf with random data and add a header with the
Jens Axboe22f78b32006-06-07 11:30:07 +0200388 * crc32 or md5 sum of that data.
Jens Axboeebac4652005-12-08 15:25:21 +0100389 */
390static void populate_io_u(struct thread_data *td, struct io_u *io_u)
391{
392 unsigned char *p = (unsigned char *) io_u->buf;
393 struct verify_header hdr;
394
395 hdr.fio_magic = FIO_HDR_MAGIC;
396 hdr.len = io_u->buflen;
397 p += sizeof(hdr);
398 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
399
400 if (td->verify == VERIFY_MD5) {
401 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
402 hdr.verify_type = VERIFY_MD5;
403 } else {
404 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
405 hdr.verify_type = VERIFY_CRC32;
406 }
407
408 memcpy(io_u->buf, &hdr, sizeof(hdr));
409}
410
Jens Axboeaea47d42006-05-26 19:27:29 +0200411static int td_io_prep(struct thread_data *td, struct io_u *io_u)
Jens Axboe20dc95c2005-12-09 10:29:35 +0100412{
Jens Axboe20dc95c2005-12-09 10:29:35 +0100413 if (td->io_prep && td->io_prep(td, io_u))
414 return 1;
415
416 return 0;
417}
418
Jens Axboeb1ff3402006-02-17 11:35:58 +0100419void put_io_u(struct thread_data *td, struct io_u *io_u)
Jens Axboeebac4652005-12-08 15:25:21 +0100420{
421 list_del(&io_u->list);
422 list_add(&io_u->list, &td->io_u_freelist);
423 td->cur_depth--;
424}
425
Jens Axboe843a7412006-06-01 21:14:21 -0700426static int fill_io_u(struct thread_data *td, struct io_u *io_u)
427{
428 /*
429 * If using an iolog, grab next piece if any available.
430 */
431 if (td->read_iolog)
432 return read_iolog_get(td, io_u);
433
Jens Axboeaea47d42006-05-26 19:27:29 +0200434 /*
435 * No log, let the seq/rand engine retrieve the next position.
436 */
437 if (!get_next_offset(td, &io_u->offset)) {
438 io_u->buflen = get_next_buflen(td);
439
440 if (io_u->buflen) {
441 io_u->ddir = get_rw_ddir(td);
Jens Axboe843a7412006-06-01 21:14:21 -0700442
443 /*
444 * If using a write iolog, store this entry.
445 */
446 if (td->write_iolog)
447 write_iolog_put(td, io_u);
448
Jens Axboeaea47d42006-05-26 19:27:29 +0200449 return 0;
450 }
451 }
452
453 return 1;
454}
455
Jens Axboe22f78b32006-06-07 11:30:07 +0200456#define queue_full(td) list_empty(&(td)->io_u_freelist)
Jens Axboeebac4652005-12-08 15:25:21 +0100457
Jens Axboeb1ff3402006-02-17 11:35:58 +0100458struct io_u *__get_io_u(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100459{
Jens Axboe22f78b32006-06-07 11:30:07 +0200460 struct io_u *io_u = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100461
Jens Axboe22f78b32006-06-07 11:30:07 +0200462 if (!queue_full(td)) {
463 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100464
Jens Axboe22f78b32006-06-07 11:30:07 +0200465 io_u->error = 0;
466 io_u->resid = 0;
467 list_del(&io_u->list);
468 list_add(&io_u->list, &td->io_u_busylist);
469 td->cur_depth++;
470 }
471
Jens Axboeebac4652005-12-08 15:25:21 +0100472 return io_u;
473}
474
Jens Axboeebac4652005-12-08 15:25:21 +0100475static struct io_u *get_io_u(struct thread_data *td)
476{
477 struct io_u *io_u;
478
479 io_u = __get_io_u(td);
480 if (!io_u)
481 return NULL;
482
Jens Axboe20dc95c2005-12-09 10:29:35 +0100483 if (td->zone_bytes >= td->zone_size) {
484 td->zone_bytes = 0;
485 td->last_pos += td->zone_skip;
486 }
487
Jens Axboeaea47d42006-05-26 19:27:29 +0200488 if (fill_io_u(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100489 put_io_u(td, io_u);
490 return NULL;
491 }
492
Jens Axboe838a3cd2006-01-20 12:38:29 +0100493 if (io_u->buflen + io_u->offset > td->real_file_size)
494 io_u->buflen = td->real_file_size - io_u->offset;
Jens Axboeebac4652005-12-08 15:25:21 +0100495
496 if (!io_u->buflen) {
497 put_io_u(td, io_u);
498 return NULL;
499 }
500
Jens Axboe843a7412006-06-01 21:14:21 -0700501 if (!td->read_iolog && !td->sequential)
Jens Axboeebac4652005-12-08 15:25:21 +0100502 mark_random_map(td, io_u);
503
Jens Axboe20dc95c2005-12-09 10:29:35 +0100504 td->last_pos += io_u->buflen;
Jens Axboeebac4652005-12-08 15:25:21 +0100505
506 if (td->verify != VERIFY_NONE)
507 populate_io_u(td, io_u);
508
Jens Axboeaea47d42006-05-26 19:27:29 +0200509 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100510 put_io_u(td, io_u);
511 return NULL;
512 }
513
514 gettimeofday(&io_u->start_time, NULL);
515 return io_u;
516}
517
518static inline void td_set_runstate(struct thread_data *td, int runstate)
519{
Jens Axboeebac4652005-12-08 15:25:21 +0100520 td->runstate = runstate;
521}
522
Jens Axboeaea47d42006-05-26 19:27:29 +0200523static int get_next_verify(struct thread_data *td, struct io_u *io_u)
Jens Axboeebac4652005-12-08 15:25:21 +0100524{
525 struct io_piece *ipo;
526
Jens Axboe22f78b32006-06-07 11:30:07 +0200527 if (!list_empty(&td->io_hist_list)) {
528 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100529
Jens Axboe22f78b32006-06-07 11:30:07 +0200530 list_del(&ipo->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100531
Jens Axboe22f78b32006-06-07 11:30:07 +0200532 io_u->offset = ipo->offset;
533 io_u->buflen = ipo->len;
534 io_u->ddir = DDIR_READ;
535 free(ipo);
536 return 0;
537 }
538
539 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100540}
541
Jens Axboeebac4652005-12-08 15:25:21 +0100542static int sync_td(struct thread_data *td)
543{
544 if (td->io_sync)
545 return td->io_sync(td);
546
547 return 0;
548}
549
550static int io_u_getevents(struct thread_data *td, int min, int max,
551 struct timespec *t)
552{
553 return td->io_getevents(td, min, max, t);
554}
555
556static int io_u_queue(struct thread_data *td, struct io_u *io_u)
557{
558 gettimeofday(&io_u->issue_time, NULL);
559
560 return td->io_queue(td, io_u);
561}
562
563#define iocb_time(iocb) ((unsigned long) (iocb)->data)
564
565static void io_completed(struct thread_data *td, struct io_u *io_u,
566 struct io_completion_data *icd)
567{
568 struct timeval e;
569 unsigned long msec;
570
571 gettimeofday(&e, NULL);
572
573 if (!io_u->error) {
Jens Axboe20dc95c2005-12-09 10:29:35 +0100574 unsigned int bytes = io_u->buflen - io_u->resid;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200575 const int idx = io_u->ddir;
Jens Axboeebac4652005-12-08 15:25:21 +0100576
577 td->io_blocks[idx]++;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100578 td->io_bytes[idx] += bytes;
579 td->zone_bytes += bytes;
580 td->this_io_bytes[idx] += bytes;
Jens Axboeebac4652005-12-08 15:25:21 +0100581
582 msec = mtime_since(&io_u->issue_time, &e);
583
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200584 add_clat_sample(td, idx, msec);
585 add_bw_sample(td, idx);
Jens Axboeebac4652005-12-08 15:25:21 +0100586
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200587 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
Jens Axboeebac4652005-12-08 15:25:21 +0100588 log_io_piece(td, io_u);
589
Jens Axboe20dc95c2005-12-09 10:29:35 +0100590 icd->bytes_done[idx] += bytes;
Jens Axboeebac4652005-12-08 15:25:21 +0100591 } else
592 icd->error = io_u->error;
593}
594
595static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
596{
597 struct io_u *io_u;
598 int i;
599
600 icd->error = 0;
601 icd->bytes_done[0] = icd->bytes_done[1] = 0;
602
603 for (i = 0; i < icd->nr; i++) {
604 io_u = td->io_event(td, i);
605
606 io_completed(td, io_u, icd);
607 put_io_u(td, io_u);
608 }
609}
610
611static void cleanup_pending_aio(struct thread_data *td)
612{
613 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
614 struct list_head *entry, *n;
615 struct io_completion_data icd;
616 struct io_u *io_u;
617 int r;
618
619 /*
620 * get immediately available events, if any
621 */
622 r = io_u_getevents(td, 0, td->cur_depth, &ts);
623 if (r > 0) {
624 icd.nr = r;
625 ios_completed(td, &icd);
626 }
627
628 /*
629 * now cancel remaining active events
630 */
631 if (td->io_cancel) {
632 list_for_each_safe(entry, n, &td->io_u_busylist) {
633 io_u = list_entry(entry, struct io_u, list);
634
635 r = td->io_cancel(td, io_u);
636 if (!r)
637 put_io_u(td, io_u);
638 }
639 }
640
641 if (td->cur_depth) {
642 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
643 if (r > 0) {
644 icd.nr = r;
645 ios_completed(td, &icd);
646 }
647 }
648}
649
650static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
651{
652 struct io_u *v_io_u = *io_u;
653 int ret = 0;
654
655 if (v_io_u) {
656 ret = verify_io_u(v_io_u);
657 put_io_u(td, v_io_u);
658 *io_u = NULL;
659 }
660
661 return ret;
662}
663
664static void do_verify(struct thread_data *td)
665{
666 struct timeval t;
667 struct io_u *io_u, *v_io_u = NULL;
668 struct io_completion_data icd;
669 int ret;
670
671 td_set_runstate(td, TD_VERIFYING);
672
673 do {
674 if (td->terminate)
675 break;
676
677 gettimeofday(&t, NULL);
678 if (runtime_exceeded(td, &t))
679 break;
680
681 io_u = __get_io_u(td);
682 if (!io_u)
683 break;
684
Jens Axboeaea47d42006-05-26 19:27:29 +0200685 if (get_next_verify(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100686 put_io_u(td, io_u);
687 break;
688 }
689
Jens Axboeaea47d42006-05-26 19:27:29 +0200690 if (td_io_prep(td, io_u)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100691 put_io_u(td, io_u);
692 break;
693 }
694
695 ret = io_u_queue(td, io_u);
696 if (ret) {
697 put_io_u(td, io_u);
698 td_verror(td, ret);
699 break;
700 }
701
702 /*
703 * we have one pending to verify, do that while
704 * we are doing io on the next one
705 */
706 if (do_io_u_verify(td, &v_io_u))
707 break;
708
709 ret = io_u_getevents(td, 1, 1, NULL);
710 if (ret != 1) {
711 if (ret < 0)
712 td_verror(td, ret);
713 break;
714 }
715
716 v_io_u = td->io_event(td, 0);
717 icd.nr = 1;
718 icd.error = 0;
719 io_completed(td, v_io_u, &icd);
720
721 if (icd.error) {
722 td_verror(td, icd.error);
723 put_io_u(td, v_io_u);
724 v_io_u = NULL;
725 break;
726 }
727
Jens Axboeebac4652005-12-08 15:25:21 +0100728 /*
729 * if we can't submit more io, we need to verify now
730 */
731 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
732 break;
733
734 } while (1);
735
736 do_io_u_verify(td, &v_io_u);
737
738 if (td->cur_depth)
739 cleanup_pending_aio(td);
740
741 td_set_runstate(td, TD_RUNNING);
742}
743
Jens Axboe32cd46a2006-06-07 13:40:40 +0200744/*
745 * Main IO worker functions. It retrieves io_u's to process and queues
746 * and reaps them, checking for rate and errors along the way.
747 */
Jens Axboeebac4652005-12-08 15:25:21 +0100748static void do_io(struct thread_data *td)
749{
750 struct io_completion_data icd;
751 struct timeval s, e;
752 unsigned long usec;
753
Jens Axboe5853e5a2006-06-08 14:41:05 +0200754 td_set_runstate(td, TD_RUNNING);
755
Jens Axboeebac4652005-12-08 15:25:21 +0100756 while (td->this_io_bytes[td->ddir] < td->io_size) {
757 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
758 struct timespec *timeout;
759 int ret, min_evts = 0;
760 struct io_u *io_u;
761
762 if (td->terminate)
763 break;
764
765 io_u = get_io_u(td);
766 if (!io_u)
767 break;
768
769 memcpy(&s, &io_u->start_time, sizeof(s));
770
771 ret = io_u_queue(td, io_u);
772 if (ret) {
773 put_io_u(td, io_u);
774 td_verror(td, ret);
775 break;
776 }
777
778 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
779
780 if (td->cur_depth < td->iodepth) {
781 timeout = &ts;
782 min_evts = 0;
783 } else {
784 timeout = NULL;
785 min_evts = 1;
786 }
787
788 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
789 if (ret < 0) {
790 td_verror(td, ret);
791 break;
792 } else if (!ret)
793 continue;
794
795 icd.nr = ret;
796 ios_completed(td, &icd);
797 if (icd.error) {
798 td_verror(td, icd.error);
799 break;
800 }
801
802 /*
803 * the rate is batched for now, it should work for batches
804 * of completions except the very first one which may look
805 * a little bursty
806 */
807 gettimeofday(&e, NULL);
808 usec = utime_since(&s, &e);
809
810 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
811
812 if (check_min_rate(td, &e)) {
813 td_verror(td, ENOMEM);
814 break;
815 }
816
817 if (runtime_exceeded(td, &e))
818 break;
819
820 if (td->thinktime)
821 usec_sleep(td, td->thinktime);
822
823 if (should_fsync(td) && td->fsync_blocks &&
824 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
825 sync_td(td);
826 }
827
828 if (td->cur_depth)
829 cleanup_pending_aio(td);
830
Jens Axboe5853e5a2006-06-08 14:41:05 +0200831 if (should_fsync(td) && td->end_fsync) {
832 td_set_runstate(td, TD_FSYNCING);
Jens Axboeebac4652005-12-08 15:25:21 +0100833 sync_td(td);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200834 }
Jens Axboeebac4652005-12-08 15:25:21 +0100835}
836
837static void cleanup_io(struct thread_data *td)
838{
839 if (td->io_cleanup)
840 td->io_cleanup(td);
841}
842
843static int init_io(struct thread_data *td)
844{
845 if (td->io_engine == FIO_SYNCIO)
846 return fio_syncio_init(td);
847 else if (td->io_engine == FIO_MMAPIO)
848 return fio_mmapio_init(td);
849 else if (td->io_engine == FIO_LIBAIO)
850 return fio_libaio_init(td);
851 else if (td->io_engine == FIO_POSIXAIO)
852 return fio_posixaio_init(td);
853 else if (td->io_engine == FIO_SGIO)
854 return fio_sgio_init(td);
Jens Axboe8756e4d2006-05-27 20:24:53 +0200855 else if (td->io_engine == FIO_SPLICEIO)
856 return fio_spliceio_init(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100857 else {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200858 log_err("bad io_engine %d\n", td->io_engine);
Jens Axboeebac4652005-12-08 15:25:21 +0100859 return 1;
860 }
861}
862
863static void cleanup_io_u(struct thread_data *td)
864{
865 struct list_head *entry, *n;
866 struct io_u *io_u;
867
868 list_for_each_safe(entry, n, &td->io_u_freelist) {
869 io_u = list_entry(entry, struct io_u, list);
870
871 list_del(&io_u->list);
872 free(io_u);
873 }
874
875 if (td->mem_type == MEM_MALLOC)
876 free(td->orig_buffer);
877 else if (td->mem_type == MEM_SHM) {
878 struct shmid_ds sbuf;
879
880 shmdt(td->orig_buffer);
881 shmctl(td->shm_id, IPC_RMID, &sbuf);
882 } else if (td->mem_type == MEM_MMAP)
883 munmap(td->orig_buffer, td->orig_buffer_size);
884 else
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200885 log_err("Bad memory type %d\n", td->mem_type);
Jens Axboeebac4652005-12-08 15:25:21 +0100886
887 td->orig_buffer = NULL;
888}
889
890static int init_io_u(struct thread_data *td)
891{
892 struct io_u *io_u;
893 int i, max_units;
894 char *p;
895
896 if (td->io_engine & FIO_SYNCIO)
897 max_units = 1;
898 else
899 max_units = td->iodepth;
900
901 td->orig_buffer_size = td->max_bs * max_units + MASK;
902
903 if (td->mem_type == MEM_MALLOC)
904 td->orig_buffer = malloc(td->orig_buffer_size);
905 else if (td->mem_type == MEM_SHM) {
906 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
907 if (td->shm_id < 0) {
908 td_verror(td, errno);
909 perror("shmget");
910 return 1;
911 }
912
913 td->orig_buffer = shmat(td->shm_id, NULL, 0);
914 if (td->orig_buffer == (void *) -1) {
915 td_verror(td, errno);
916 perror("shmat");
917 td->orig_buffer = NULL;
918 return 1;
919 }
920 } else if (td->mem_type == MEM_MMAP) {
921 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
922 if (td->orig_buffer == MAP_FAILED) {
923 td_verror(td, errno);
924 perror("mmap");
925 td->orig_buffer = NULL;
926 return 1;
927 }
928 }
929
Jens Axboeebac4652005-12-08 15:25:21 +0100930 p = ALIGN(td->orig_buffer);
931 for (i = 0; i < max_units; i++) {
932 io_u = malloc(sizeof(*io_u));
933 memset(io_u, 0, sizeof(*io_u));
934 INIT_LIST_HEAD(&io_u->list);
935
936 io_u->buf = p + td->max_bs * i;
Jens Axboeb1ff3402006-02-17 11:35:58 +0100937 io_u->index = i;
Jens Axboeebac4652005-12-08 15:25:21 +0100938 list_add(&io_u->list, &td->io_u_freelist);
939 }
940
941 return 0;
942}
943
944static int create_file(struct thread_data *td, unsigned long long size,
945 int extend)
946{
947 unsigned long long left;
948 unsigned int bs;
949 int r, oflags;
950 char *b;
951
952 /*
953 * unless specifically asked for overwrite, let normal io extend it
954 */
Jens Axboec20d25b2006-06-12 14:56:43 +0200955 if (td_write(td) && !td->overwrite) {
956 td->real_file_size = size;
Jens Axboeebac4652005-12-08 15:25:21 +0100957 return 0;
Jens Axboec20d25b2006-06-12 14:56:43 +0200958 }
Jens Axboeebac4652005-12-08 15:25:21 +0100959
960 if (!size) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200961 log_err("Need size for create\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100962 td_verror(td, EINVAL);
963 return 1;
964 }
965
Jens Axboe88c6ed82006-06-09 11:28:10 +0200966 temp_stall_ts = 1;
967
Jens Axboeebac4652005-12-08 15:25:21 +0100968 if (!extend) {
969 oflags = O_CREAT | O_TRUNC;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200970 fprintf(f_out, "%s: Laying out IO file (%LuMiB)\n", td->name, size >> 20);
Jens Axboeebac4652005-12-08 15:25:21 +0100971 } else {
972 oflags = O_APPEND;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200973 fprintf(f_out, "%s: Extending IO file (%Lu -> %LuMiB)\n", td->name, (td->file_size - size) >> 20, td->file_size >> 20);
Jens Axboeebac4652005-12-08 15:25:21 +0100974 }
975
976 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
977 if (td->fd < 0) {
978 td_verror(td, errno);
Jens Axboec20d25b2006-06-12 14:56:43 +0200979 goto done_noclose;
Jens Axboeebac4652005-12-08 15:25:21 +0100980 }
981
982 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
983 td_verror(td, errno);
Jens Axboec20d25b2006-06-12 14:56:43 +0200984 goto done;
Jens Axboeebac4652005-12-08 15:25:21 +0100985 }
986
987 td->io_size = td->file_size;
988 b = malloc(td->max_bs);
989 memset(b, 0, td->max_bs);
990
991 left = size;
992 while (left && !td->terminate) {
993 bs = td->max_bs;
994 if (bs > left)
995 bs = left;
996
997 r = write(td->fd, b, bs);
998
999 if (r == (int) bs) {
1000 left -= bs;
1001 continue;
1002 } else {
1003 if (r < 0)
1004 td_verror(td, errno);
1005 else
1006 td_verror(td, EIO);
1007
1008 break;
1009 }
1010 }
1011
1012 if (td->terminate)
1013 unlink(td->file_name);
1014 else if (td->create_fsync)
1015 fsync(td->fd);
1016
Jens Axboec20d25b2006-06-12 14:56:43 +02001017 free(b);
1018done:
Jens Axboeebac4652005-12-08 15:25:21 +01001019 close(td->fd);
1020 td->fd = -1;
Jens Axboec20d25b2006-06-12 14:56:43 +02001021done_noclose:
1022 temp_stall_ts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001023 return 0;
1024}
1025
1026static int file_size(struct thread_data *td)
1027{
1028 struct stat st;
1029
Jens Axboec20d25b2006-06-12 14:56:43 +02001030 if (td->overwrite) {
1031 if (fstat(td->fd, &st) == -1) {
1032 td_verror(td, errno);
1033 return 1;
1034 }
1035
1036 td->real_file_size = st.st_size;
1037
1038 if (!td->file_size || td->file_size > td->real_file_size)
1039 td->file_size = td->real_file_size;
Jens Axboeebac4652005-12-08 15:25:21 +01001040 }
1041
Jens Axboee8e387c2006-05-05 11:11:22 +02001042 td->file_size -= td->file_offset;
Jens Axboeebac4652005-12-08 15:25:21 +01001043 return 0;
1044}
1045
1046static int bdev_size(struct thread_data *td)
1047{
Jens Axboe9104f872005-12-28 23:50:45 +01001048 unsigned long long bytes;
Jens Axboeebac4652005-12-08 15:25:21 +01001049 int r;
1050
1051 r = blockdev_size(td->fd, &bytes);
1052 if (r) {
1053 td_verror(td, r);
1054 return 1;
1055 }
1056
Jens Axboe838a3cd2006-01-20 12:38:29 +01001057 td->real_file_size = bytes;
1058
Jens Axboeebac4652005-12-08 15:25:21 +01001059 /*
1060 * no extend possibilities, so limit size to device size if too large
1061 */
Jens Axboe838a3cd2006-01-20 12:38:29 +01001062 if (!td->file_size || td->file_size > td->real_file_size)
1063 td->file_size = td->real_file_size;
Jens Axboeebac4652005-12-08 15:25:21 +01001064
Jens Axboee8e387c2006-05-05 11:11:22 +02001065 td->file_size -= td->file_offset;
Jens Axboeebac4652005-12-08 15:25:21 +01001066 return 0;
1067}
1068
1069static int get_file_size(struct thread_data *td)
1070{
Jens Axboe0af7b542006-02-17 10:10:12 +01001071 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001072
1073 if (td->filetype == FIO_TYPE_FILE)
1074 ret = file_size(td);
Jens Axboe0af7b542006-02-17 10:10:12 +01001075 else if (td->filetype == FIO_TYPE_BD)
Jens Axboeebac4652005-12-08 15:25:21 +01001076 ret = bdev_size(td);
Jens Axboe0af7b542006-02-17 10:10:12 +01001077 else
1078 td->real_file_size = -1;
Jens Axboeebac4652005-12-08 15:25:21 +01001079
1080 if (ret)
1081 return ret;
1082
Jens Axboee8e387c2006-05-05 11:11:22 +02001083 if (td->file_offset > td->real_file_size) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001084 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 +01001085 return 1;
1086 }
1087
Jens Axboe838a3cd2006-01-20 12:38:29 +01001088 td->io_size = td->file_size;
Jens Axboeebac4652005-12-08 15:25:21 +01001089 if (td->io_size == 0) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001090 log_err("%s: no io blocks\n", td->name);
Jens Axboeebac4652005-12-08 15:25:21 +01001091 td_verror(td, EINVAL);
1092 return 1;
1093 }
1094
Jens Axboe20dc95c2005-12-09 10:29:35 +01001095 if (!td->zone_size)
1096 td->zone_size = td->io_size;
1097
Jens Axboeebac4652005-12-08 15:25:21 +01001098 td->total_io_size = td->io_size * td->loops;
1099 return 0;
1100}
1101
1102static int setup_file_mmap(struct thread_data *td)
1103{
1104 int flags;
1105
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001106 if (td_rw(td))
1107 flags = PROT_READ | PROT_WRITE;
1108 else if (td_write(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +01001109 flags = PROT_WRITE;
1110
1111 if (td->verify != VERIFY_NONE)
1112 flags |= PROT_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001113 } else
1114 flags = PROT_READ;
Jens Axboeebac4652005-12-08 15:25:21 +01001115
1116 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1117 if (td->mmap == MAP_FAILED) {
1118 td->mmap = NULL;
1119 td_verror(td, errno);
1120 return 1;
1121 }
1122
1123 if (td->invalidate_cache) {
1124 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1125 td_verror(td, errno);
1126 return 1;
1127 }
1128 }
1129
1130 if (td->sequential) {
1131 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1132 td_verror(td, errno);
1133 return 1;
1134 }
1135 } else {
1136 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1137 td_verror(td, errno);
1138 return 1;
1139 }
1140 }
1141
1142 return 0;
1143}
1144
1145static int setup_file_plain(struct thread_data *td)
1146{
1147 if (td->invalidate_cache) {
1148 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1149 td_verror(td, errno);
1150 return 1;
1151 }
1152 }
1153
1154 if (td->sequential) {
1155 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1156 td_verror(td, errno);
1157 return 1;
1158 }
1159 } else {
1160 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1161 td_verror(td, errno);
1162 return 1;
1163 }
1164 }
1165
1166 return 0;
1167}
1168
1169static int setup_file(struct thread_data *td)
1170{
1171 struct stat st;
1172 int flags = 0;
1173
1174 if (stat(td->file_name, &st) == -1) {
1175 if (errno != ENOENT) {
1176 td_verror(td, errno);
1177 return 1;
1178 }
1179 if (!td->create_file) {
1180 td_verror(td, ENOENT);
1181 return 1;
1182 }
1183 if (create_file(td, td->file_size, 0))
1184 return 1;
1185 } else if (td->filetype == FIO_TYPE_FILE) {
Jens Axboe6a0106a2006-05-05 10:34:43 +02001186 if (st.st_size < (off_t) td->file_size) {
Jens Axboea71e13b2006-06-12 15:17:03 +02001187 if (create_file(td, td->file_size, 1))
Jens Axboeebac4652005-12-08 15:25:21 +01001188 return 1;
1189 }
1190 }
1191
1192 if (td->odirect)
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001193 flags |= OS_O_DIRECT;
Jens Axboeebac4652005-12-08 15:25:21 +01001194
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001195 if (td_write(td) || td_rw(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +01001196 if (td->filetype == FIO_TYPE_FILE) {
1197 if (!td->overwrite)
1198 flags |= O_TRUNC;
1199
1200 flags |= O_CREAT;
1201 }
1202 if (td->sync_io)
1203 flags |= O_SYNC;
1204
1205 flags |= O_RDWR;
1206
1207 td->fd = open(td->file_name, flags, 0600);
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001208 } else {
1209 if (td->filetype == FIO_TYPE_CHAR)
1210 flags |= O_RDWR;
1211 else
1212 flags |= O_RDONLY;
1213
1214 td->fd = open(td->file_name, flags);
Jens Axboeebac4652005-12-08 15:25:21 +01001215 }
1216
1217 if (td->fd == -1) {
1218 td_verror(td, errno);
1219 return 1;
1220 }
1221
1222 if (get_file_size(td))
1223 return 1;
1224
1225 if (td->io_engine != FIO_MMAPIO)
1226 return setup_file_plain(td);
1227 else
1228 return setup_file_mmap(td);
1229}
1230
Jens Axboeda867742006-06-06 10:39:10 -07001231static int switch_ioscheduler(struct thread_data *td)
1232{
1233 char tmp[256], tmp2[128];
1234 FILE *f;
1235 int ret;
1236
1237 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1238
1239 f = fopen(tmp, "r+");
1240 if (!f) {
1241 td_verror(td, errno);
1242 return 1;
1243 }
1244
1245 /*
1246 * Set io scheduler.
1247 */
1248 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1249 if (ferror(f) || ret != 1) {
1250 td_verror(td, errno);
1251 fclose(f);
1252 return 1;
1253 }
1254
1255 rewind(f);
1256
1257 /*
1258 * Read back and check that the selected scheduler is now the default.
1259 */
1260 ret = fread(tmp, 1, sizeof(tmp), f);
1261 if (ferror(f) || ret < 0) {
1262 td_verror(td, errno);
1263 fclose(f);
1264 return 1;
1265 }
1266
1267 sprintf(tmp2, "[%s]", td->ioscheduler);
1268 if (!strstr(tmp, tmp2)) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001269 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -07001270 td_verror(td, EINVAL);
1271 fclose(f);
1272 return 1;
1273 }
1274
1275 fclose(f);
1276 return 0;
1277}
1278
Jens Axboeebac4652005-12-08 15:25:21 +01001279static void clear_io_state(struct thread_data *td)
1280{
1281 if (td->io_engine == FIO_SYNCIO)
1282 lseek(td->fd, SEEK_SET, 0);
1283
Jens Axboe20dc95c2005-12-09 10:29:35 +01001284 td->last_pos = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001285 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1286 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +01001287 td->zone_bytes = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001288
1289 if (td->file_map)
1290 memset(td->file_map, 0, td->num_maps * sizeof(long));
1291}
1292
Jens Axboeebac4652005-12-08 15:25:21 +01001293static void *thread_main(void *data)
1294{
1295 struct thread_data *td = data;
Jens Axboeebac4652005-12-08 15:25:21 +01001296
1297 if (!td->use_thread)
1298 setsid();
1299
1300 td->pid = getpid();
1301
Jens Axboeaea47d42006-05-26 19:27:29 +02001302 INIT_LIST_HEAD(&td->io_u_freelist);
1303 INIT_LIST_HEAD(&td->io_u_busylist);
1304 INIT_LIST_HEAD(&td->io_hist_list);
1305 INIT_LIST_HEAD(&td->io_log_list);
1306
Jens Axboeebac4652005-12-08 15:25:21 +01001307 if (init_io_u(td))
1308 goto err;
1309
1310 if (fio_setaffinity(td) == -1) {
1311 td_verror(td, errno);
1312 goto err;
1313 }
1314
1315 if (init_io(td))
1316 goto err;
1317
Jens Axboeaea47d42006-05-26 19:27:29 +02001318 if (init_iolog(td))
1319 goto err;
1320
Jens Axboeebac4652005-12-08 15:25:21 +01001321 if (td->ioprio) {
1322 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1323 td_verror(td, errno);
1324 goto err;
1325 }
1326 }
1327
Jens Axboeb6f4d882006-06-02 10:32:51 +02001328 if (nice(td->nice) < 0) {
1329 td_verror(td, errno);
1330 goto err;
1331 }
1332
Jens Axboe75154842006-06-01 13:56:09 +02001333 if (init_random_state(td))
1334 goto err;
1335
Jens Axboeda867742006-06-06 10:39:10 -07001336 if (td->ioscheduler && switch_ioscheduler(td))
1337 goto err;
1338
Jens Axboe75154842006-06-01 13:56:09 +02001339 td_set_runstate(td, TD_INITIALIZED);
Jens Axboebbfd6b02006-06-07 19:42:54 +02001340 fio_sem_up(&startup_sem);
1341 fio_sem_down(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001342
1343 if (!td->create_serialize && setup_file(td))
1344 goto err;
1345
Jens Axboeebac4652005-12-08 15:25:21 +01001346 gettimeofday(&td->epoch, NULL);
1347
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001348 if (td->exec_prerun)
1349 system(td->exec_prerun);
1350
Jens Axboeebac4652005-12-08 15:25:21 +01001351 while (td->loops--) {
1352 getrusage(RUSAGE_SELF, &td->ru_start);
1353 gettimeofday(&td->start, NULL);
1354 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1355
1356 if (td->ratemin)
1357 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1358
1359 clear_io_state(td);
1360 prune_io_piece_log(td);
1361
1362 do_io(td);
1363
1364 td->runtime[td->ddir] += mtime_since_now(&td->start);
Jens Axboeaea47d42006-05-26 19:27:29 +02001365 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001366 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1367
Jens Axboeebac4652005-12-08 15:25:21 +01001368 update_rusage_stat(td);
1369
1370 if (td->error || td->terminate)
1371 break;
1372
1373 if (td->verify == VERIFY_NONE)
1374 continue;
1375
1376 clear_io_state(td);
1377 gettimeofday(&td->start, NULL);
1378
1379 do_verify(td);
1380
1381 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1382
1383 if (td->error || td->terminate)
1384 break;
1385 }
1386
Jens Axboeebac4652005-12-08 15:25:21 +01001387 if (td->bw_log)
1388 finish_log(td, td->bw_log, "bw");
1389 if (td->slat_log)
1390 finish_log(td, td->slat_log, "slat");
1391 if (td->clat_log)
1392 finish_log(td, td->clat_log, "clat");
Jens Axboe843a7412006-06-01 21:14:21 -07001393 if (td->write_iolog)
1394 write_iolog_close(td);
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001395 if (td->exec_postrun)
1396 system(td->exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +01001397
1398 if (exitall_on_terminate)
1399 terminate_threads(td->groupid);
1400
1401err:
1402 if (td->fd != -1) {
1403 close(td->fd);
1404 td->fd = -1;
1405 }
1406 if (td->mmap)
1407 munmap(td->mmap, td->file_size);
1408 cleanup_io(td);
1409 cleanup_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001410 td_set_runstate(td, TD_EXITED);
1411 return NULL;
1412
1413}
1414
1415static void *fork_main(int shmid, int offset)
1416{
1417 struct thread_data *td;
1418 void *data;
1419
1420 data = shmat(shmid, NULL, 0);
1421 if (data == (void *) -1) {
1422 perror("shmat");
1423 return NULL;
1424 }
1425
1426 td = data + offset * sizeof(struct thread_data);
1427 thread_main(td);
1428 shmdt(data);
1429 return NULL;
1430}
1431
Jens Axboeebac4652005-12-08 15:25:21 +01001432static void check_str_update(struct thread_data *td)
1433{
1434 char c = run_str[td->thread_number - 1];
1435
Jens Axboeebac4652005-12-08 15:25:21 +01001436 switch (td->runstate) {
1437 case TD_REAPED:
1438 c = '_';
1439 break;
1440 case TD_EXITED:
1441 c = 'E';
1442 break;
1443 case TD_RUNNING:
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001444 if (td_rw(td)) {
1445 if (td->sequential)
1446 c = 'M';
1447 else
1448 c = 'm';
1449 } else if (td_read(td)) {
Jens Axboeebac4652005-12-08 15:25:21 +01001450 if (td->sequential)
1451 c = 'R';
1452 else
1453 c = 'r';
1454 } else {
1455 if (td->sequential)
1456 c = 'W';
1457 else
1458 c = 'w';
1459 }
1460 break;
1461 case TD_VERIFYING:
1462 c = 'V';
1463 break;
Jens Axboe5853e5a2006-06-08 14:41:05 +02001464 case TD_FSYNCING:
1465 c = 'F';
1466 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001467 case TD_CREATED:
1468 c = 'C';
1469 break;
Jens Axboe75154842006-06-01 13:56:09 +02001470 case TD_INITIALIZED:
1471 c = 'I';
1472 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001473 case TD_NOT_CREATED:
1474 c = 'P';
1475 break;
1476 default:
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001477 log_err("state %d\n", td->runstate);
Jens Axboeebac4652005-12-08 15:25:21 +01001478 }
1479
1480 run_str[td->thread_number - 1] = c;
Jens Axboeebac4652005-12-08 15:25:21 +01001481}
1482
Jens Axboe5289b842005-12-08 20:47:28 +01001483static void eta_to_str(char *str, int eta_sec)
1484{
1485 unsigned int d, h, m, s;
1486 static int always_d, always_h;
1487
1488 d = h = m = s = 0;
1489
1490 s = eta_sec % 60;
1491 eta_sec /= 60;
1492 m = eta_sec % 60;
1493 eta_sec /= 60;
1494 h = eta_sec % 24;
1495 eta_sec /= 24;
1496 d = eta_sec;
1497
1498 if (d || always_d) {
1499 always_d = 1;
1500 str += sprintf(str, "%02dd:", d);
1501 }
1502 if (h || always_h) {
1503 always_h = 1;
1504 str += sprintf(str, "%02dh:", h);
1505 }
1506
1507 str += sprintf(str, "%02dm:", m);
1508 str += sprintf(str, "%02ds", s);
1509}
1510
Jens Axboe6a0106a2006-05-05 10:34:43 +02001511static int thread_eta(struct thread_data *td, unsigned long elapsed)
1512{
1513 unsigned long long bytes_total, bytes_done;
1514 unsigned int eta_sec = 0;
1515
1516 bytes_total = td->total_io_size;
Jens Axboe3d60d1e2006-05-25 06:31:06 +02001517
1518 /*
1519 * if writing, bytes_total will be twice the size. If mixing,
1520 * assume a 50/50 split and thus bytes_total will be 50% larger.
1521 */
1522 if (td->verify) {
1523 if (td_rw(td))
1524 bytes_total = bytes_total * 3 / 2;
1525 else
1526 bytes_total <<= 1;
1527 }
Jens Axboe6a0106a2006-05-05 10:34:43 +02001528 if (td->zone_size && td->zone_skip)
1529 bytes_total /= (td->zone_skip / td->zone_size);
1530
1531 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1532 double perc;
Jens Axboe8b611c32006-05-25 01:46:59 +02001533
Jens Axboe6a0106a2006-05-05 10:34:43 +02001534 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1535 perc = (double) bytes_done / (double) bytes_total;
1536 if (perc > 1.0)
1537 perc = 1.0;
1538
1539 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1540
Jens Axboe8b611c32006-05-25 01:46:59 +02001541 if (td->timeout && eta_sec > (td->timeout - elapsed))
Jens Axboe6a0106a2006-05-05 10:34:43 +02001542 eta_sec = td->timeout - elapsed;
Jens Axboe75154842006-06-01 13:56:09 +02001543 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1544 || td->runstate == TD_INITIALIZED) {
Jens Axboe6a0106a2006-05-05 10:34:43 +02001545 int t_eta = 0, r_eta = 0;
1546
1547 /*
1548 * We can only guess - assume it'll run the full timeout
1549 * if given, otherwise assume it'll run at the specified rate.
1550 */
1551 if (td->timeout)
1552 t_eta = td->timeout + td->start_delay - elapsed;
1553 if (td->rate) {
1554 r_eta = (bytes_total / 1024) / td->rate;
1555 r_eta += td->start_delay - elapsed;
1556 }
1557
1558 if (r_eta && t_eta)
1559 eta_sec = min(r_eta, t_eta);
1560 else if (r_eta)
1561 eta_sec = r_eta;
1562 else if (t_eta)
1563 eta_sec = t_eta;
1564 else
Jens Axboe972cfd22006-06-09 11:08:56 +02001565 eta_sec = 0;
Jens Axboe6a0106a2006-05-05 10:34:43 +02001566 } else {
1567 /*
Jens Axboe5853e5a2006-06-08 14:41:05 +02001568 * thread is already done or waiting for fsync
Jens Axboe6a0106a2006-05-05 10:34:43 +02001569 */
1570 eta_sec = 0;
1571 }
1572
1573 return eta_sec;
1574}
1575
Jens Axboeebac4652005-12-08 15:25:21 +01001576static void print_thread_status(void)
1577{
Jens Axboe6a0106a2006-05-05 10:34:43 +02001578 unsigned long elapsed = time_since_now(&genesis);
Jens Axboe71a751c2006-06-08 14:48:47 +02001579 int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec;
Jens Axboe5289b842005-12-08 20:47:28 +01001580 char eta_str[32];
Jens Axboe6a0106a2006-05-05 10:34:43 +02001581 double perc = 0.0;
Jens Axboeebac4652005-12-08 15:25:21 +01001582
Jens Axboec6ae0a52006-06-12 11:04:44 +02001583 if (temp_stall_ts || terse_output)
Jens Axboe88c6ed82006-06-09 11:28:10 +02001584 return;
1585
Jens Axboe6a0106a2006-05-05 10:34:43 +02001586 eta_secs = malloc(thread_number * sizeof(int));
1587 memset(eta_secs, 0, thread_number * sizeof(int));
1588
Jens Axboe71a751c2006-06-08 14:48:47 +02001589 nr_pending = nr_running = t_rate = m_rate = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001590 for (i = 0; i < thread_number; i++) {
1591 struct thread_data *td = &threads[i];
1592
Jens Axboe5853e5a2006-06-08 14:41:05 +02001593 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING||
1594 td->runstate == TD_FSYNCING) {
Jens Axboeebac4652005-12-08 15:25:21 +01001595 nr_running++;
1596 t_rate += td->rate;
1597 m_rate += td->ratemin;
Jens Axboe71a751c2006-06-08 14:48:47 +02001598 } else if (td->runstate < TD_RUNNING)
1599 nr_pending++;
Jens Axboeebac4652005-12-08 15:25:21 +01001600
Jens Axboe6a0106a2006-05-05 10:34:43 +02001601 if (elapsed >= 3)
1602 eta_secs[i] = thread_eta(td, elapsed);
Jens Axboe8b611c32006-05-25 01:46:59 +02001603 else
1604 eta_secs[i] = INT_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +01001605
1606 check_str_update(td);
1607 }
1608
Jens Axboe6a0106a2006-05-05 10:34:43 +02001609 if (exitall_on_terminate)
1610 eta_sec = INT_MAX;
1611 else
1612 eta_sec = 0;
Jens Axboe5289b842005-12-08 20:47:28 +01001613
Jens Axboe6a0106a2006-05-05 10:34:43 +02001614 for (i = 0; i < thread_number; i++) {
1615 if (exitall_on_terminate) {
1616 if (eta_secs[i] < eta_sec)
1617 eta_sec = eta_secs[i];
1618 } else {
1619 if (eta_secs[i] > eta_sec)
1620 eta_sec = eta_secs[i];
Jens Axboe5289b842005-12-08 20:47:28 +01001621 }
Jens Axboe6a0106a2006-05-05 10:34:43 +02001622 }
Jens Axboe5289b842005-12-08 20:47:28 +01001623
Jens Axboe8b611c32006-05-25 01:46:59 +02001624 if (eta_sec != INT_MAX && elapsed) {
Jens Axboe6a0106a2006-05-05 10:34:43 +02001625 perc = (double) elapsed / (double) (elapsed + eta_sec);
1626 eta_to_str(eta_str, eta_sec);
Jens Axboeebac4652005-12-08 15:25:21 +01001627 }
1628
Jens Axboe71a751c2006-06-08 14:48:47 +02001629 if (!nr_running && !nr_pending)
Jens Axboef44c1d42006-06-08 14:46:24 +02001630 return;
1631
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001632 printf("Threads running: %d", nr_running);
Jens Axboeebac4652005-12-08 15:25:21 +01001633 if (m_rate || t_rate)
1634 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
Jens Axboe8b611c32006-05-25 01:46:59 +02001635 if (eta_sec != INT_MAX) {
Jens Axboe6a0106a2006-05-05 10:34:43 +02001636 perc *= 100.0;
1637 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1638 }
Jens Axboe5289b842005-12-08 20:47:28 +01001639 printf("\r");
Jens Axboeebac4652005-12-08 15:25:21 +01001640 fflush(stdout);
Jens Axboe6a0106a2006-05-05 10:34:43 +02001641 free(eta_secs);
Jens Axboeebac4652005-12-08 15:25:21 +01001642}
1643
1644static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1645{
1646 int i;
1647
1648 /*
1649 * reap exited threads (TD_EXITED -> TD_REAPED)
1650 */
1651 for (i = 0; i < thread_number; i++) {
1652 struct thread_data *td = &threads[i];
1653
1654 if (td->runstate != TD_EXITED)
1655 continue;
1656
1657 td_set_runstate(td, TD_REAPED);
1658
1659 if (td->use_thread) {
1660 long ret;
1661
1662 if (pthread_join(td->thread, (void *) &ret))
1663 perror("thread_join");
1664 } else
1665 waitpid(td->pid, NULL, 0);
1666
1667 (*nr_running)--;
1668 (*m_rate) -= td->ratemin;
1669 (*t_rate) -= td->rate;
1670 }
1671}
1672
Jens Axboefcb6ade2006-05-31 12:14:35 +02001673static void fio_unpin_memory(void *pinned)
1674{
1675 if (pinned) {
1676 if (munlock(pinned, mlock_size) < 0)
1677 perror("munlock");
1678 munmap(pinned, mlock_size);
1679 }
1680}
1681
1682static void *fio_pin_memory(void)
1683{
Jens Axboe32cd46a2006-06-07 13:40:40 +02001684 unsigned long long phys_mem;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001685 void *ptr;
1686
1687 if (!mlock_size)
1688 return NULL;
1689
1690 /*
1691 * Don't allow mlock of more than real_mem-128MB
1692 */
Jens Axboe32cd46a2006-06-07 13:40:40 +02001693 phys_mem = os_phys_mem();
1694 if (phys_mem) {
1695 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1696 mlock_size = phys_mem - 128 * 1024 * 1024;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001697 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
Jens Axboefcb6ade2006-05-31 12:14:35 +02001698 }
1699 }
1700
1701 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1702 if (!ptr) {
1703 perror("malloc locked mem");
1704 return NULL;
1705 }
1706 if (mlock(ptr, mlock_size) < 0) {
1707 munmap(ptr, mlock_size);
1708 perror("mlock");
1709 return NULL;
1710 }
1711
1712 return ptr;
1713}
1714
Jens Axboeebac4652005-12-08 15:25:21 +01001715static void run_threads(void)
1716{
Jens Axboeebac4652005-12-08 15:25:21 +01001717 struct thread_data *td;
1718 unsigned long spent;
1719 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001720 void *mlocked_mem;
1721
1722 mlocked_mem = fio_pin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001723
Jens Axboec6ae0a52006-06-12 11:04:44 +02001724 if (!terse_output) {
1725 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1726 fflush(stdout);
1727 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001728
Jens Axboe4efa9702006-05-31 11:56:49 +02001729 signal(SIGINT, sig_handler);
1730 signal(SIGALRM, sig_handler);
1731
Jens Axboeebac4652005-12-08 15:25:21 +01001732 todo = thread_number;
1733 nr_running = 0;
1734 nr_started = 0;
1735 m_rate = t_rate = 0;
1736
1737 for (i = 0; i < thread_number; i++) {
1738 td = &threads[i];
1739
1740 run_str[td->thread_number - 1] = 'P';
1741
1742 init_disk_util(td);
1743
1744 if (!td->create_serialize)
1745 continue;
1746
1747 /*
1748 * do file setup here so it happens sequentially,
1749 * we don't want X number of threads getting their
1750 * client data interspersed on disk
1751 */
1752 if (setup_file(td)) {
1753 td_set_runstate(td, TD_REAPED);
1754 todo--;
1755 }
1756 }
1757
1758 gettimeofday(&genesis, NULL);
1759
1760 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001761 struct thread_data *map[MAX_JOBS];
1762 struct timeval this_start;
1763 int this_jobs = 0, left;
1764
Jens Axboeebac4652005-12-08 15:25:21 +01001765 /*
1766 * create threads (TD_NOT_CREATED -> TD_CREATED)
1767 */
1768 for (i = 0; i < thread_number; i++) {
1769 td = &threads[i];
1770
1771 if (td->runstate != TD_NOT_CREATED)
1772 continue;
1773
1774 /*
1775 * never got a chance to start, killed by other
1776 * thread for some reason
1777 */
1778 if (td->terminate) {
1779 todo--;
1780 continue;
1781 }
1782
1783 if (td->start_delay) {
1784 spent = mtime_since_now(&genesis);
1785
1786 if (td->start_delay * 1000 > spent)
1787 continue;
1788 }
1789
1790 if (td->stonewall && (nr_started || nr_running))
1791 break;
1792
Jens Axboe75154842006-06-01 13:56:09 +02001793 /*
1794 * Set state to created. Thread will transition
1795 * to TD_INITIALIZED when it's done setting up.
1796 */
Jens Axboeebac4652005-12-08 15:25:21 +01001797 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001798 map[this_jobs++] = td;
Jens Axboebbfd6b02006-06-07 19:42:54 +02001799 fio_sem_init(&startup_sem, 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001800 nr_started++;
1801
1802 if (td->use_thread) {
1803 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1804 perror("thread_create");
1805 nr_started--;
1806 }
1807 } else {
1808 if (fork())
Jens Axboebbfd6b02006-06-07 19:42:54 +02001809 fio_sem_down(&startup_sem);
Jens Axboeebac4652005-12-08 15:25:21 +01001810 else {
1811 fork_main(shm_id, i);
1812 exit(0);
1813 }
1814 }
1815 }
1816
1817 /*
Jens Axboe75154842006-06-01 13:56:09 +02001818 * Wait for the started threads to transition to
1819 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001820 */
Jens Axboe75154842006-06-01 13:56:09 +02001821 gettimeofday(&this_start, NULL);
1822 left = this_jobs;
1823 while (left) {
1824 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1825 break;
1826
1827 usleep(100000);
1828
1829 for (i = 0; i < this_jobs; i++) {
1830 td = map[i];
1831 if (!td)
1832 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001833 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001834 map[i] = NULL;
1835 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001836 } else if (td->runstate >= TD_EXITED) {
1837 map[i] = NULL;
1838 left--;
1839 todo--;
1840 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001841 }
1842 }
1843 }
1844
1845 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001846 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001847 for (i = 0; i < this_jobs; i++) {
1848 td = map[i];
1849 if (!td)
1850 continue;
1851 kill(td->pid, SIGTERM);
1852 }
1853 break;
1854 }
1855
1856 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001857 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001858 */
Jens Axboeebac4652005-12-08 15:25:21 +01001859 for (i = 0; i < thread_number; i++) {
1860 td = &threads[i];
1861
Jens Axboe75154842006-06-01 13:56:09 +02001862 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001863 continue;
1864
1865 td_set_runstate(td, TD_RUNNING);
1866 nr_running++;
1867 nr_started--;
1868 m_rate += td->ratemin;
1869 t_rate += td->rate;
Jens Axboe75154842006-06-01 13:56:09 +02001870 todo--;
Jens Axboebbfd6b02006-06-07 19:42:54 +02001871 fio_sem_up(&td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001872 }
1873
1874 reap_threads(&nr_running, &t_rate, &m_rate);
1875
1876 if (todo)
1877 usleep(100000);
1878 }
1879
1880 while (nr_running) {
1881 reap_threads(&nr_running, &t_rate, &m_rate);
1882 usleep(10000);
1883 }
1884
1885 update_io_ticks();
Jens Axboefcb6ade2006-05-31 12:14:35 +02001886 fio_unpin_memory(mlocked_mem);
Jens Axboeebac4652005-12-08 15:25:21 +01001887}
1888
Jens Axboeebac4652005-12-08 15:25:21 +01001889int main(int argc, char *argv[])
1890{
1891 if (parse_options(argc, argv))
1892 return 1;
1893
1894 if (!thread_number) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001895 log_err("Nothing to do\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001896 return 1;
1897 }
1898
1899 disk_util_timer_arm();
1900
1901 run_threads();
1902 show_run_stats();
1903
1904 return 0;
1905}