blob: b79aa93e644296be9bdf0439863760d8dd4a6cfd [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 *
Jens Axboe8e9fe632006-10-24 15:11:09 +02007 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
9 *
Jens Axboeebac4652005-12-08 15:25:21 +010010 * This program is free software; you can redistribute it and/or modify
Jens Axboe8e9fe632006-10-24 15:11:09 +020011 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Jens Axboeebac4652005-12-08 15:25:21 +010013 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
Jens Axboeebac4652005-12-08 15:25:21 +010024#include <unistd.h>
25#include <fcntl.h>
26#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <signal.h>
28#include <time.h>
Jens Axboedbe11252007-02-11 00:19:51 +010029#include <locale.h>
Jens Axboe36167d82007-02-18 05:41:31 +010030#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010031#include <sys/stat.h>
32#include <sys/wait.h>
33#include <sys/ipc.h>
34#include <sys/shm.h>
Jens Axboeebac4652005-12-08 15:25:21 +010035#include <sys/mman.h>
36
37#include "fio.h"
Jens Axboe210f43b2007-04-17 19:52:18 +020038#include "hash.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010039#include "smalloc.h"
Jens Axboeebac4652005-12-08 15:25:21 +010040
Jens Axboecfc99db2007-03-14 10:34:47 +010041unsigned long page_mask;
42unsigned long page_size;
Jens Axboe29d610e2007-02-06 13:25:33 +010043#define ALIGN(buf) \
44 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010045
46int groupid = 0;
47int thread_number = 0;
Jens Axboe9cedf162007-03-12 11:29:30 +010048int nr_process = 0;
49int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010050int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020051int temp_stall_ts;
Jens Axboed3eeeab2008-04-06 12:19:05 +020052unsigned long done_secs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010053
Jens Axboecdd18ad2008-02-27 18:58:00 +010054static struct fio_mutex *startup_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010055static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010056static int exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +010057
Jens Axboebb3884d2007-01-17 17:23:11 +110058struct io_log *agg_io_log[2];
59
Jens Axboeebac4652005-12-08 15:25:21 +010060#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020061#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010062
Jens Axboe6ce15a32007-01-12 09:04:41 +010063static inline void td_set_runstate(struct thread_data *td, int runstate)
64{
Jens Axboed8fab1b2008-03-06 10:25:17 +010065 if (td->runstate == runstate)
66 return;
67
Jens Axboebd6f78b2008-02-01 20:27:52 +010068 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", td->pid, td->runstate,
69 runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010070 td->runstate = runstate;
71}
72
Jens Axboe390c40e2007-03-05 12:35:29 +010073static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010074{
Jens Axboe34572e22006-10-20 09:33:18 +020075 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010076 int i;
77
Jens Axboe6f88bcb2008-03-19 10:29:07 +010078 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
79
Jens Axboe34572e22006-10-20 09:33:18 +020080 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010081 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +010082 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
83 td->o.name, td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010084 td->terminate = 1;
85 td->o.start_delay = 0;
86
Jens Axboe7a93ab12007-04-18 12:25:41 +020087 /*
88 * if the thread is running, just let it exit
89 */
90 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010091 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010092 else {
93 struct ioengine_ops *ops = td->io_ops;
94
95 if (ops && (ops->flags & FIO_SIGQUIT))
96 kill(td->pid, SIGQUIT);
97 }
Jens Axboeebac4652005-12-08 15:25:21 +010098 }
99 }
100}
101
102static void sig_handler(int sig)
103{
104 switch (sig) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100105 case SIGALRM:
106 update_io_ticks();
107 disk_util_timer_arm();
108 print_thread_status();
109 break;
110 default:
111 printf("\nfio: terminating on signal %d\n", sig);
112 fflush(stdout);
113 terminate_threads(TERMINATE_ALL);
114 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100115 }
116}
117
Jens Axboe906c8d72006-06-13 09:37:56 +0200118/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200119 * Check if we are above the minimum rate given.
120 */
Jens Axboeebac4652005-12-08 15:25:21 +0100121static int check_min_rate(struct thread_data *td, struct timeval *now)
122{
Jens Axboe09042002007-02-23 10:56:22 +0100123 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100124 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100125 unsigned long spent;
126 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100127
128 /*
Jens Axboe780bf1a2007-02-28 11:13:49 +0100129 * No minimum rate set, always ok
130 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100131 if (!td->o.ratemin && !td->o.rate_iops_min)
Jens Axboe780bf1a2007-02-28 11:13:49 +0100132 return 0;
133
134 /*
Jens Axboeebac4652005-12-08 15:25:21 +0100135 * allow a 2 second settle period in the beginning
136 */
137 if (mtime_since(&td->start, now) < 2000)
138 return 0;
139
Jens Axboe4e991c22007-03-15 11:41:11 +0100140 if (td_read(td)) {
141 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100142 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100143 }
144 if (td_write(td)) {
145 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100146 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100147 }
Jens Axboe09042002007-02-23 10:56:22 +0100148
Jens Axboeebac4652005-12-08 15:25:21 +0100149 /*
150 * if rate blocks is set, sample is running
151 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100152 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100153 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100154 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100155 return 0;
156
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100157 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100158 /*
159 * check bandwidth specified rate
160 */
161 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100162 log_err("%s: min rate %u not met\n", td->o.name,
163 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100164 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100165 } else {
166 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100167 if (rate < td->o.ratemin ||
168 bytes < td->rate_bytes) {
169 log_err("%s: min rate %u not met, got"
170 " %luKiB/sec\n", td->o.name,
171 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100172 return 1;
173 }
174 }
175 } else {
176 /*
177 * checks iops specified rate
178 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100179 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100180 log_err("%s: min iops rate %u not met\n",
181 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100182 return 1;
183 } else {
184 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100185 if (rate < td->o.rate_iops_min ||
186 iops < td->rate_blocks) {
187 log_err("%s: min iops rate %u not met,"
188 " got %lu\n", td->o.name,
189 td->o.rate_iops_min,
190 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100191 }
Jens Axboe413dd452007-02-23 09:26:09 +0100192 }
Jens Axboeebac4652005-12-08 15:25:21 +0100193 }
194 }
195
Jens Axboe09042002007-02-23 10:56:22 +0100196 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100197 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100198 memcpy(&td->lastrate, now, sizeof(*now));
199 return 0;
200}
201
202static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
203{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100204 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100205 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100206 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100207 return 1;
208
209 return 0;
210}
211
Jens Axboe906c8d72006-06-13 09:37:56 +0200212/*
213 * When job exits, we can cancel the in-flight IO if we are using async
214 * io. Attempt to do so.
215 */
Jens Axboeebac4652005-12-08 15:25:21 +0100216static void cleanup_pending_aio(struct thread_data *td)
217{
Jens Axboeebac4652005-12-08 15:25:21 +0100218 struct list_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100219 struct io_u *io_u;
220 int r;
221
222 /*
223 * get immediately available events, if any
224 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100225 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100226 if (r < 0)
227 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100228
229 /*
230 * now cancel remaining active events
231 */
Jens Axboe2866c822006-10-09 15:57:48 +0200232 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100233 list_for_each_safe(entry, n, &td->io_u_busylist) {
234 io_u = list_entry(entry, struct io_u, list);
235
Jens Axboe0c6e7512007-02-22 11:19:39 +0100236 /*
237 * if the io_u isn't in flight, then that generally
238 * means someone leaked an io_u. complain but fix
239 * it up, so we don't stall here.
240 */
241 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
242 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100243 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100244 } else {
245 r = td->io_ops->cancel(td, io_u);
246 if (!r)
247 put_io_u(td, io_u);
248 }
Jens Axboeebac4652005-12-08 15:25:21 +0100249 }
250 }
251
Jens Axboe97601022007-02-18 12:47:29 +0100252 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100253 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100254}
255
Jens Axboe906c8d72006-06-13 09:37:56 +0200256/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200257 * Helper to handle the final sync of a file. Works just like the normal
258 * io path, just does everything sync.
259 */
260static int fio_io_sync(struct thread_data *td, struct fio_file *f)
261{
262 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200263 int ret;
264
265 if (!io_u)
266 return 1;
267
268 io_u->ddir = DDIR_SYNC;
269 io_u->file = f;
270
271 if (td_io_prep(td, io_u)) {
272 put_io_u(td, io_u);
273 return 1;
274 }
275
Jens Axboe755200a2007-02-19 13:08:12 +0100276requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200277 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100278 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100279 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200280 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200281 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100282 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100283 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100284 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100285 } else if (ret == FIO_Q_COMPLETED) {
286 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100287 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100288 return 1;
289 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200290
Jens Axboed7762cf2007-02-23 12:34:57 +0100291 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100292 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100293 } else if (ret == FIO_Q_BUSY) {
294 if (td_io_commit(td))
295 return 1;
296 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200297 }
298
299 return 0;
300}
301
302/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100303 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200304 * reads the blocks back in, and checks the crc/md5 of the data.
305 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100306static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100307{
Jens Axboe53cdc682006-10-18 11:50:58 +0200308 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100309 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100310 int ret, min_events;
311 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200312
313 /*
314 * sync io first and invalidate cache, to make sure we really
315 * read from disk.
316 */
317 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100318 if (!(f->flags & FIO_FILE_OPEN))
319 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100320 if (fio_io_sync(td, f))
321 break;
322 if (file_invalidate_cache(td, f))
323 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200324 }
Jens Axboeebac4652005-12-08 15:25:21 +0100325
Jens Axboeb2fdda42007-02-20 20:52:51 +0100326 if (td->error)
327 return;
328
Jens Axboeebac4652005-12-08 15:25:21 +0100329 td_set_runstate(td, TD_VERIFYING);
330
Jens Axboe36167d82007-02-18 05:41:31 +0100331 io_u = NULL;
332 while (!td->terminate) {
Jens Axboe54517922007-03-05 10:06:06 +0100333 int ret2;
334
Jens Axboeebac4652005-12-08 15:25:21 +0100335 io_u = __get_io_u(td);
336 if (!io_u)
337 break;
338
Jens Axboe069c2912007-02-21 23:02:39 +0100339 if (runtime_exceeded(td, &io_u->start_time)) {
340 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200341 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200342 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100343 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200344
Jens Axboe069c2912007-02-21 23:02:39 +0100345 if (get_next_verify(td, io_u)) {
346 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100347 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100348 }
Jens Axboeebac4652005-12-08 15:25:21 +0100349
Jens Axboe069c2912007-02-21 23:02:39 +0100350 if (td_io_prep(td, io_u)) {
351 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100352 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100353 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100354
355 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100356
Jens Axboe11786802007-03-05 18:33:22 +0100357 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100358 switch (ret) {
359 case FIO_Q_COMPLETED:
360 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100361 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100362 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100363 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200364 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100365
Jens Axboe9e9d1642007-03-12 09:37:46 +0100366 /*
367 * zero read, fail
368 */
369 if (!bytes) {
370 td_verror(td, ENODATA, "full resid");
371 put_io_u(td, io_u);
372 break;
373 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200374
Jens Axboe36167d82007-02-18 05:41:31 +0100375 io_u->xfer_buflen = io_u->resid;
376 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200377 io_u->offset += bytes;
378
Jens Axboe30061b92007-04-17 13:31:34 +0200379 td->ts.short_io_u[io_u->ddir]++;
380
Jens Axboed460eb32007-04-16 21:39:33 +0200381 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200382 goto sync_done;
383
Jens Axboe11786802007-03-05 18:33:22 +0100384 requeue_io_u(td, &io_u);
385 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200386sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100387 ret = io_u_sync_complete(td, io_u);
388 if (ret < 0)
389 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100390 }
Jens Axboe36167d82007-02-18 05:41:31 +0100391 continue;
392 case FIO_Q_QUEUED:
393 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100394 case FIO_Q_BUSY:
395 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100396 ret2 = td_io_commit(td);
397 if (ret2 < 0)
398 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100399 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100400 default:
401 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100402 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100403 break;
404 }
405
Jens Axboe99784632007-02-19 10:06:17 +0100406 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100407 break;
408
Jens Axboe3af6ef32007-02-18 06:57:43 +0100409 /*
410 * if we can queue more, do so. but check if there are
411 * completed io_u's first.
412 */
Jens Axboe97601022007-02-18 12:47:29 +0100413 min_events = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100414 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe3af6ef32007-02-18 06:57:43 +0100415 min_events = 1;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100416
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100417 if (td->cur_depth > td->o.iodepth_low)
418 min_events = td->cur_depth - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100419 }
420
Jens Axboe3af6ef32007-02-18 06:57:43 +0100421 /*
422 * Reap required number of io units, if any, and do the
423 * verification on them through the callback handler
424 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100425 if (io_u_queued_complete(td, min_events) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100426 break;
427 }
428
Jens Axboec01c0392007-02-26 12:43:42 +0100429 if (!td->error) {
430 min_events = td->cur_depth;
431
432 if (min_events)
433 ret = io_u_queued_complete(td, min_events);
434 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100435 cleanup_pending_aio(td);
436
437 td_set_runstate(td, TD_RUNNING);
438}
439
Jens Axboe32cd46a2006-06-07 13:40:40 +0200440/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200441 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200442 * and reaps them, checking for rate and errors along the way.
443 */
Jens Axboeebac4652005-12-08 15:25:21 +0100444static void do_io(struct thread_data *td)
445{
Jens Axboe02bcaa82006-11-24 10:42:00 +0100446 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100447 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100448 unsigned int i;
449 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100450
Jens Axboe5853e5a2006-06-08 14:41:05 +0200451 td_set_runstate(td, TD_RUNNING);
452
Jens Axboe7bb48f82007-03-27 15:30:28 +0200453 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100454 struct timeval comp_time;
455 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200456 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100457 struct io_u *io_u;
Jens Axboe54517922007-03-05 10:06:06 +0100458 int ret2;
Jens Axboeebac4652005-12-08 15:25:21 +0100459
460 if (td->terminate)
461 break;
462
Jens Axboe3d7c3912007-02-19 13:16:12 +0100463 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100464 if (!io_u)
465 break;
466
467 memcpy(&s, &io_u->start_time, sizeof(s));
Jens Axboe97601022007-02-18 12:47:29 +0100468
469 if (runtime_exceeded(td, &s)) {
470 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200471 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100472 break;
473 }
Jens Axboe36167d82007-02-18 05:41:31 +0100474
Jens Axboeb67740d2007-07-26 12:22:30 +0200475 /*
476 * Add verification end_io handler, if asked to verify
477 * a previously written file.
478 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100479 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200480 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100481 td_set_runstate(td, TD_VERIFYING);
482 } else
483 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200484
Jens Axboe11786802007-03-05 18:33:22 +0100485 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100486 switch (ret) {
487 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100488 if (io_u->error)
489 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100490 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100491 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200492 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100493
Jens Axboe9e9d1642007-03-12 09:37:46 +0100494 /*
495 * zero read, fail
496 */
497 if (!bytes) {
498 td_verror(td, ENODATA, "full resid");
499 put_io_u(td, io_u);
500 break;
501 }
502
Jens Axboe36167d82007-02-18 05:41:31 +0100503 io_u->xfer_buflen = io_u->resid;
504 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200505 io_u->offset += bytes;
506
Jens Axboe30061b92007-04-17 13:31:34 +0200507 td->ts.short_io_u[io_u->ddir]++;
508
Jens Axboed460eb32007-04-16 21:39:33 +0200509 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200510 goto sync_done;
511
Jens Axboe11786802007-03-05 18:33:22 +0100512 requeue_io_u(td, &io_u);
513 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200514sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100515 fio_gettime(&comp_time, NULL);
516 bytes_done = io_u_sync_complete(td, io_u);
517 if (bytes_done < 0)
518 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100519 }
Jens Axboe36167d82007-02-18 05:41:31 +0100520 break;
521 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100522 /*
523 * if the engine doesn't have a commit hook,
524 * the io_u is really queued. if it does have such
525 * a hook, it has to call io_u_queued() itself.
526 */
527 if (td->io_ops->commit == NULL)
528 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100529 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100530 case FIO_Q_BUSY:
531 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100532 ret2 = td_io_commit(td);
533 if (ret2 < 0)
534 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100535 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100536 default:
537 assert(ret < 0);
538 put_io_u(td, io_u);
539 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100540 }
541
Jens Axboe99784632007-02-19 10:06:17 +0100542 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100543 break;
544
Jens Axboe97601022007-02-18 12:47:29 +0100545 /*
546 * See if we need to complete some commands
547 */
Jens Axboe755200a2007-02-19 13:08:12 +0100548 if (ret == FIO_Q_QUEUED || ret == FIO_Q_BUSY) {
Jens Axboe97601022007-02-18 12:47:29 +0100549 min_evts = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100550 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe36167d82007-02-18 05:41:31 +0100551 min_evts = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100552
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100553 if (td->cur_depth > td->o.iodepth_low)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100554 min_evts = td->cur_depth
555 - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100556 }
557
Jens Axboe97601022007-02-18 12:47:29 +0100558 fio_gettime(&comp_time, NULL);
Jens Axboed7762cf2007-02-23 12:34:57 +0100559 bytes_done = io_u_queued_complete(td, min_evts);
Jens Axboe97601022007-02-18 12:47:29 +0100560 if (bytes_done < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100561 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100562 }
563
Jens Axboe97601022007-02-18 12:47:29 +0100564 if (!bytes_done)
565 continue;
566
Jens Axboeebac4652005-12-08 15:25:21 +0100567 /*
568 * the rate is batched for now, it should work for batches
569 * of completions except the very first one which may look
570 * a little bursty
571 */
Jens Axboe97601022007-02-18 12:47:29 +0100572 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100573
Jens Axboe413dd452007-02-23 09:26:09 +0100574 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100575
Jens Axboe97601022007-02-18 12:47:29 +0100576 if (check_min_rate(td, &comp_time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100577 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100578 terminate_threads(td->groupid);
Jens Axboee1161c32007-02-22 19:36:48 +0100579 td_verror(td, ENODATA, "check_min_rate");
Jens Axboeebac4652005-12-08 15:25:21 +0100580 break;
581 }
582
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100583 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100584 unsigned long long b;
585
586 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100587 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100588 int left;
589
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100590 if (td->o.thinktime_spin)
591 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100592
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100593 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100594 if (left)
595 usec_sleep(td, left);
596 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100597 }
Jens Axboeebac4652005-12-08 15:25:21 +0100598 }
599
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100600 if (td->o.fill_device && td->error == ENOSPC) {
601 td->error = 0;
602 td->terminate = 1;
603 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100604 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100605 struct fio_file *f;
606
Jens Axboec01c0392007-02-26 12:43:42 +0100607 i = td->cur_depth;
608 if (i)
609 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100610
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100611 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200612 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100613
614 for_each_file(td, f, i) {
615 if (!(f->flags & FIO_FILE_OPEN))
616 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200617 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100618 }
Jens Axboe84585002006-10-19 20:26:22 +0200619 }
Jens Axboec01c0392007-02-26 12:43:42 +0100620 } else
621 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100622
623 /*
624 * stop job if we failed doing any IO
625 */
626 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
627 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100628}
629
Jens Axboeebac4652005-12-08 15:25:21 +0100630static void cleanup_io_u(struct thread_data *td)
631{
632 struct list_head *entry, *n;
633 struct io_u *io_u;
634
635 list_for_each_safe(entry, n, &td->io_u_freelist) {
636 io_u = list_entry(entry, struct io_u, list);
637
638 list_del(&io_u->list);
639 free(io_u);
640 }
641
Jens Axboe2f9ade32006-10-20 11:25:52 +0200642 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100643}
644
Jens Axboe6b9cea22006-10-30 17:00:24 +0100645/*
646 * "randomly" fill the buffer contents
647 */
Jens Axboee9459e52007-04-17 15:46:32 +0200648static void fill_io_buf(struct thread_data *td, struct io_u *io_u, int max_bs)
Jens Axboe6b9cea22006-10-30 17:00:24 +0100649{
Jens Axboe210f43b2007-04-17 19:52:18 +0200650 long *ptr = io_u->buf;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100651
Jens Axboee9459e52007-04-17 15:46:32 +0200652 if (!td->o.zero_buffers) {
653 while ((void *) ptr - io_u->buf < max_bs) {
Jens Axboe210f43b2007-04-17 19:52:18 +0200654 *ptr = rand() * GOLDEN_RATIO_PRIME;
Jens Axboee9459e52007-04-17 15:46:32 +0200655 ptr++;
656 }
657 } else
658 memset(ptr, 0, max_bs);
Jens Axboe6b9cea22006-10-30 17:00:24 +0100659}
660
Jens Axboeebac4652005-12-08 15:25:21 +0100661static int init_io_u(struct thread_data *td)
662{
663 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100664 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100665 int i, max_units;
666 char *p;
667
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100668 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100669 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100670 td->orig_buffer_size = (unsigned long long) max_bs
671 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100672
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100673 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
674 unsigned long bs;
675
676 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
677 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
678 }
Jens Axboeebac4652005-12-08 15:25:21 +0100679
Jens Axboec3990642007-07-19 10:17:09 +0200680 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
681 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
682 return 1;
683 }
684
Jens Axboe2f9ade32006-10-20 11:25:52 +0200685 if (allocate_io_mem(td))
686 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100687
Jens Axboeb3f378e2007-07-20 12:39:22 +0200688 if (td->o.odirect)
689 p = ALIGN(td->orig_buffer);
690 else
691 p = td->orig_buffer;
692
Jens Axboeebac4652005-12-08 15:25:21 +0100693 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200694 if (td->terminate)
695 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100696 io_u = malloc(sizeof(*io_u));
697 memset(io_u, 0, sizeof(*io_u));
698 INIT_LIST_HEAD(&io_u->list);
699
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200700 if (!(td->io_ops->flags & FIO_NOIO)) {
701 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200702
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200703 if (td_write(td))
704 fill_io_buf(td, io_u, max_bs);
705 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100706
Jens Axboeb1ff3402006-02-17 11:35:58 +0100707 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100708 io_u->flags = IO_U_F_FREE;
Jens Axboeebac4652005-12-08 15:25:21 +0100709 list_add(&io_u->list, &td->io_u_freelist);
710 }
711
Jens Axboe433afcb2007-02-22 10:39:01 +0100712 io_u_init_timeout();
713
Jens Axboeebac4652005-12-08 15:25:21 +0100714 return 0;
715}
716
Jens Axboeda867742006-06-06 10:39:10 -0700717static int switch_ioscheduler(struct thread_data *td)
718{
719 char tmp[256], tmp2[128];
720 FILE *f;
721 int ret;
722
Jens Axboeba0fbe12007-03-09 14:34:23 +0100723 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200724 return 0;
725
Jens Axboeda867742006-06-06 10:39:10 -0700726 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
727
728 f = fopen(tmp, "r+");
729 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200730 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100731 log_err("fio: os or kernel doesn't support IO scheduler"
732 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200733 return 0;
734 }
735 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700736 return 1;
737 }
738
739 /*
740 * Set io scheduler.
741 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100742 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700743 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100744 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700745 fclose(f);
746 return 1;
747 }
748
749 rewind(f);
750
751 /*
752 * Read back and check that the selected scheduler is now the default.
753 */
754 ret = fread(tmp, 1, sizeof(tmp), f);
755 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100756 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700757 fclose(f);
758 return 1;
759 }
760
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100761 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700762 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100763 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100764 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700765 fclose(f);
766 return 1;
767 }
768
769 fclose(f);
770 return 0;
771}
772
Jens Axboe492158c2007-05-24 10:32:47 +0200773static int keep_running(struct thread_data *td)
774{
775 unsigned long long io_done;
776
Jens Axboe20e354e2007-07-23 14:36:16 +0200777 if (td->done)
778 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200779 if (td->o.time_based)
780 return 1;
781 if (td->o.loops) {
782 td->o.loops--;
783 return 1;
784 }
785
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100786 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
787 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200788 if (io_done < td->o.size)
789 return 1;
790
791 return 0;
792}
793
Jens Axboea978ba62007-03-08 14:29:03 +0100794static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100795{
Jens Axboe53cdc682006-10-18 11:50:58 +0200796 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100797 unsigned int i;
798 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100799
Jens Axboe756867b2007-03-06 15:19:24 +0100800 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100801 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100802 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100803 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100804 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100805 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100806
Jens Axboec1324df2007-02-19 19:06:41 +0100807 td->last_was_sync = 0;
808
Jens Axboe2ba1c292008-02-01 13:16:38 +0100809 /*
810 * reset file done count if we are to start over
811 */
812 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200813 td->nr_done_files = 0;
Jens Axboe281f9b62007-05-23 09:34:42 +0200814
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100815 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100816
817 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200818 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200819 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100820 ret = td_io_open_file(td, f);
821 if (ret)
822 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200823 }
Jens Axboea978ba62007-03-08 14:29:03 +0100824
825 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100826}
827
Jens Axboe906c8d72006-06-13 09:37:56 +0200828/*
829 * Entry point for the thread based jobs. The process based jobs end up
830 * here as well, after a little setup.
831 */
Jens Axboeebac4652005-12-08 15:25:21 +0100832static void *thread_main(void *data)
833{
Shawn Lewis10927312007-11-21 09:38:13 +0100834 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100835 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100836 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100837
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100838 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100839 setsid();
840
841 td->pid = getpid();
842
Jens Axboeee56ad52008-02-01 10:30:20 +0100843 dprint(FD_PROCESS, "jobs pid=%d started\n", td->pid);
844
Jens Axboeaea47d42006-05-26 19:27:29 +0200845 INIT_LIST_HEAD(&td->io_u_freelist);
846 INIT_LIST_HEAD(&td->io_u_busylist);
Jens Axboe755200a2007-02-19 13:08:12 +0100847 INIT_LIST_HEAD(&td->io_u_requeues);
Jens Axboeaea47d42006-05-26 19:27:29 +0200848 INIT_LIST_HEAD(&td->io_log_list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200849 INIT_LIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200850 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200851
Jens Axboe75154842006-06-01 13:56:09 +0200852 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100853 fio_mutex_up(startup_mutex);
854 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100855
Jens Axboe37f56872007-03-09 12:42:35 +0100856 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100857 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100858 * eating a file descriptor
859 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100860 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100861
Jens Axboed84f8d42007-05-16 08:47:46 +0200862 /*
863 * May alter parameters that init_io_u() will use, so we need to
864 * do this first.
865 */
866 if (init_iolog(td))
867 goto err;
868
Jens Axboedb1cd902007-04-26 13:47:07 +0200869 if (init_io_u(td))
870 goto err;
871
Jens Axboe375b2692007-05-22 09:13:02 +0200872 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200873 td_verror(td, errno, "cpu_set_affinity");
874 goto err;
875 }
876
Jens Axboeac684782007-11-08 08:29:07 +0100877 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200878 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
879 td_verror(td, errno, "ioprio_set");
880 goto err;
881 }
882 }
883
884 if (nice(td->o.nice) == -1) {
885 td_verror(td, errno, "nice");
886 goto err;
887 }
888
889 if (td->o.ioscheduler && switch_ioscheduler(td))
890 goto err;
891
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100892 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100893 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100894
895 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100896 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100897
Jens Axboeb5af8292007-03-08 12:43:13 +0100898 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100899 goto err;
900
Jens Axboe68727072007-03-15 20:49:25 +0100901 if (init_random_map(td))
902 goto err;
903
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100904 if (td->o.exec_prerun) {
905 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100906 goto err;
907 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200908
Jens Axboe69008992006-11-24 13:12:56 +0100909 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100910 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100911 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100912
913 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100914 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200915 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100916 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100917 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100918
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100919 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100920 memcpy(&td->lastrate, &td->ts.stat_sample_time,
921 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100922
Jens Axboea978ba62007-03-08 14:29:03 +0100923 if (clear_state && clear_io_state(td))
924 break;
925
Jens Axboeebac4652005-12-08 15:25:21 +0100926 prune_io_piece_log(td);
927
Jens Axboeba0fbe12007-03-09 14:34:23 +0100928 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100929
Jens Axboea978ba62007-03-08 14:29:03 +0100930 clear_state = 1;
931
Jens Axboe38d77ca2007-03-21 14:20:20 +0100932 if (td_read(td) && td->io_bytes[DDIR_READ]) {
933 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100934 elapsed = utime_since(&td->start,
935 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100936 else
937 elapsed = utime_since_now(&td->start);
938
939 runtime[DDIR_READ] += elapsed;
940 }
941 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
942 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100943 elapsed = utime_since(&td->start,
944 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100945 else
946 elapsed = utime_since_now(&td->start);
947
948 runtime[DDIR_WRITE] += elapsed;
949 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100950
Jens Axboeebac4652005-12-08 15:25:21 +0100951 if (td->error || td->terminate)
952 break;
953
Shawn Lewise84c73a2007-08-02 22:19:32 +0200954 if (!td->o.do_verify ||
955 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +0200956 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +0100957 continue;
958
Jens Axboea978ba62007-03-08 14:29:03 +0100959 if (clear_io_state(td))
960 break;
961
Jens Axboe02bcaa82006-11-24 10:42:00 +0100962 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100963
964 do_verify(td);
965
Jens Axboe69008992006-11-24 13:12:56 +0100966 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100967
968 if (td->error || td->terminate)
969 break;
970 }
971
Jens Axboe36dff962006-11-24 13:29:20 +0100972 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +0200973 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
974 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +0100975 td->ts.total_run_time = mtime_since_now(&td->epoch);
976 td->ts.io_bytes[0] = td->io_bytes[0];
977 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +0100978
Jens Axboe756867b2007-03-06 15:19:24 +0100979 if (td->ts.bw_log)
980 finish_log(td, td->ts.bw_log, "bw");
981 if (td->ts.slat_log)
982 finish_log(td, td->ts.slat_log, "slat");
983 if (td->ts.clat_log)
984 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100985 if (td->o.exec_postrun) {
986 if (system(td->o.exec_postrun) < 0)
987 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100988 }
Jens Axboeebac4652005-12-08 15:25:21 +0100989
990 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100991 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100992
993err:
Jens Axboe5bf13a52007-02-17 01:51:47 +0100994 if (td->error)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100995 printf("fio: pid=%d, err=%d/%s\n", td->pid, td->error,
996 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100997 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200998 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100999 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001000
1001 /*
1002 * do this very late, it will log file closing as well
1003 */
1004 if (td->o.write_iolog_file)
1005 write_iolog_close(td);
1006
Jens Axboed23bb322007-03-23 15:57:56 +01001007 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001008 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001009 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001010}
1011
Jens Axboe906c8d72006-06-13 09:37:56 +02001012/*
1013 * We cannot pass the td data into a forked process, so attach the td and
1014 * pass it to the thread worker.
1015 */
Jens Axboea6418142007-02-17 04:47:18 +01001016static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001017{
1018 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001019 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001020
1021 data = shmat(shmid, NULL, 0);
1022 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001023 int __err = errno;
1024
Jens Axboeebac4652005-12-08 15:25:21 +01001025 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001026 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001027 }
1028
1029 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001030 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001031 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001032 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001033}
1034
Jens Axboe906c8d72006-06-13 09:37:56 +02001035/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001036 * Run over the job map and reap the threads that have exited, if any.
1037 */
Jens Axboeebac4652005-12-08 15:25:21 +01001038static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1039{
Jens Axboe34572e22006-10-20 09:33:18 +02001040 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001041 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001042
1043 /*
1044 * reap exited threads (TD_EXITED -> TD_REAPED)
1045 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001046 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001047 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001048 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001049
Jens Axboe84585002006-10-19 20:26:22 +02001050 /*
1051 * ->io_ops is NULL for a thread that has closed its
1052 * io engine
1053 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001054 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001055 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001056 else
1057 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001058
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001059 if (!td->pid) {
1060 pending++;
1061 continue;
1062 }
1063 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001064 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001065 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001066 if (td->runstate == TD_EXITED) {
1067 td_set_runstate(td, TD_REAPED);
1068 goto reaped;
1069 }
1070 continue;
1071 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001072
1073 flags = WNOHANG;
1074 if (td->runstate == TD_EXITED)
1075 flags = 0;
1076
1077 /*
1078 * check if someone quit or got killed in an unusual way
1079 */
1080 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001081 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001082 if (errno == ECHILD) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001083 log_err("fio: pid=%d disappeared %d\n", td->pid,
1084 td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001085 td_set_runstate(td, TD_REAPED);
1086 goto reaped;
1087 }
1088 perror("waitpid");
1089 } else if (ret == td->pid) {
1090 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001091 int sig = WTERMSIG(status);
1092
Jens Axboed9244942007-03-05 12:32:32 +01001093 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001094 log_err("fio: pid=%d, got signal=%d\n",
1095 td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001096 td_set_runstate(td, TD_REAPED);
1097 goto reaped;
1098 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001099 if (WIFEXITED(status)) {
1100 if (WEXITSTATUS(status) && !td->error)
1101 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001102
Jens Axboea2f77c92007-02-22 11:53:00 +01001103 td_set_runstate(td, TD_REAPED);
1104 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001105 }
1106 }
Jens Axboeebac4652005-12-08 15:25:21 +01001107
Jens Axboea2f77c92007-02-22 11:53:00 +01001108 /*
1109 * thread is not dead, continue
1110 */
gurudas paie48676b2007-04-11 12:44:27 +02001111 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001112 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001113reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001114 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001115 (*m_rate) -= td->o.ratemin;
1116 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001117 if (!td->pid)
1118 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001119
1120 if (td->error)
1121 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001122
1123 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001124 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001125
Jens Axboe1f809d12007-10-25 18:34:02 +02001126 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001127 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001128}
1129
Jens Axboe906c8d72006-06-13 09:37:56 +02001130/*
1131 * Main function for kicking off and reaping jobs, as needed.
1132 */
Jens Axboeebac4652005-12-08 15:25:21 +01001133static void run_threads(void)
1134{
Jens Axboeebac4652005-12-08 15:25:21 +01001135 struct thread_data *td;
1136 unsigned long spent;
1137 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001138
Jens Axboe2f9ade32006-10-20 11:25:52 +02001139 if (fio_pin_memory())
1140 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001141
Jens Axboec6ae0a52006-06-12 11:04:44 +02001142 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001143 printf("Starting ");
1144 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001145 printf("%d thread%s", nr_thread,
1146 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001147 if (nr_process) {
1148 if (nr_thread)
1149 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001150 printf("%d process%s", nr_process,
1151 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001152 }
1153 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001154 fflush(stdout);
1155 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001156
Jens Axboe4efa9702006-05-31 11:56:49 +02001157 signal(SIGINT, sig_handler);
1158 signal(SIGALRM, sig_handler);
1159
Jens Axboeebac4652005-12-08 15:25:21 +01001160 todo = thread_number;
1161 nr_running = 0;
1162 nr_started = 0;
1163 m_rate = t_rate = 0;
1164
Jens Axboe34572e22006-10-20 09:33:18 +02001165 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001166 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001167
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001168 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001169 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001170 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001171 }
Jens Axboeebac4652005-12-08 15:25:21 +01001172
1173 /*
1174 * do file setup here so it happens sequentially,
1175 * we don't want X number of threads getting their
1176 * client data interspersed on disk
1177 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001178 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001179 exit_value++;
1180 if (td->error)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001181 log_err("fio: pid=%d, err=%d/%s\n", td->pid,
1182 td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001183 td_set_runstate(td, TD_REAPED);
1184 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001185 } else {
1186 struct fio_file *f;
1187 unsigned int i;
1188
1189 /*
1190 * for sharing to work, each job must always open
1191 * its own files. so close them, if we opened them
1192 * for creation
1193 */
1194 for_each_file(td, f, i)
1195 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001196 }
Jens Axboe380cf262007-01-11 14:01:27 +01001197
1198 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001199 }
1200
Jens Axboea2f77c92007-02-22 11:53:00 +01001201 set_genesis_time();
1202
Jens Axboeebac4652005-12-08 15:25:21 +01001203 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001204 struct thread_data *map[MAX_JOBS];
1205 struct timeval this_start;
1206 int this_jobs = 0, left;
1207
Jens Axboeebac4652005-12-08 15:25:21 +01001208 /*
1209 * create threads (TD_NOT_CREATED -> TD_CREATED)
1210 */
Jens Axboe34572e22006-10-20 09:33:18 +02001211 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001212 if (td->runstate != TD_NOT_CREATED)
1213 continue;
1214
1215 /*
1216 * never got a chance to start, killed by other
1217 * thread for some reason
1218 */
1219 if (td->terminate) {
1220 todo--;
1221 continue;
1222 }
1223
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001224 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001225 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001226
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001227 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001228 continue;
1229 }
1230
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001231 if (td->o.stonewall && (nr_started || nr_running)) {
1232 dprint(FD_PROCESS, "%s: stonewall wait\n",
1233 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001234 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001235 }
Jens Axboeebac4652005-12-08 15:25:21 +01001236
Jens Axboe75154842006-06-01 13:56:09 +02001237 /*
1238 * Set state to created. Thread will transition
1239 * to TD_INITIALIZED when it's done setting up.
1240 */
Jens Axboeebac4652005-12-08 15:25:21 +01001241 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001242 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001243 nr_started++;
1244
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001245 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001246 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001247 if (pthread_create(&td->thread, NULL,
1248 thread_main, td)) {
Jens Axboef4705a72008-03-10 10:52:22 +01001249 perror("pthread_create");
Jens Axboeebac4652005-12-08 15:25:21 +01001250 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001251 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001252 }
Jens Axboef4705a72008-03-10 10:52:22 +01001253 if (pthread_detach(td->thread) < 0)
1254 perror("pthread_detach");
Jens Axboeebac4652005-12-08 15:25:21 +01001255 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001256 dprint(FD_PROCESS, "will fork\n");
Jens Axboe07739b52007-03-08 20:25:46 +01001257 if (!fork()) {
Jens Axboea6418142007-02-17 04:47:18 +01001258 int ret = fork_main(shm_id, i);
1259
1260 exit(ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001261 }
1262 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001263 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001264 }
1265
1266 /*
Jens Axboe75154842006-06-01 13:56:09 +02001267 * Wait for the started threads to transition to
1268 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001269 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001270 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001271 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001272 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001273 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1274 break;
1275
1276 usleep(100000);
1277
1278 for (i = 0; i < this_jobs; i++) {
1279 td = map[i];
1280 if (!td)
1281 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001282 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001283 map[i] = NULL;
1284 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001285 } else if (td->runstate >= TD_EXITED) {
1286 map[i] = NULL;
1287 left--;
1288 todo--;
1289 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001290 }
1291 }
1292 }
1293
1294 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001295 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001296 for (i = 0; i < this_jobs; i++) {
1297 td = map[i];
1298 if (!td)
1299 continue;
1300 kill(td->pid, SIGTERM);
1301 }
1302 break;
1303 }
1304
1305 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001306 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001307 */
Jens Axboe34572e22006-10-20 09:33:18 +02001308 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001309 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001310 continue;
1311
1312 td_set_runstate(td, TD_RUNNING);
1313 nr_running++;
1314 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001315 m_rate += td->o.ratemin;
1316 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001317 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001318 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001319 }
1320
1321 reap_threads(&nr_running, &t_rate, &m_rate);
1322
1323 if (todo)
1324 usleep(100000);
1325 }
1326
1327 while (nr_running) {
1328 reap_threads(&nr_running, &t_rate, &m_rate);
1329 usleep(10000);
1330 }
1331
1332 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001333 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001334}
1335
Jens Axboeebac4652005-12-08 15:25:21 +01001336int main(int argc, char *argv[])
1337{
Jens Axboe29d610e2007-02-06 13:25:33 +01001338 long ps;
1339
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001340 sinit();
1341
Jens Axboedbe11252007-02-11 00:19:51 +01001342 /*
1343 * We need locale for number printing, if it isn't set then just
1344 * go with the US format.
1345 */
1346 if (!getenv("LC_NUMERIC"))
1347 setlocale(LC_NUMERIC, "en_US");
1348
Jens Axboeebac4652005-12-08 15:25:21 +01001349 if (parse_options(argc, argv))
1350 return 1;
1351
Jens Axboe4b472fa2007-04-04 11:04:34 +02001352 if (!thread_number)
1353 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001354
Jens Axboe29d610e2007-02-06 13:25:33 +01001355 ps = sysconf(_SC_PAGESIZE);
1356 if (ps < 0) {
1357 log_err("Failed to get page size\n");
1358 return 1;
1359 }
1360
Jens Axboecfc99db2007-03-14 10:34:47 +01001361 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001362 page_mask = ps - 1;
1363
Jens Axboebb3884d2007-01-17 17:23:11 +11001364 if (write_bw_log) {
1365 setup_log(&agg_io_log[DDIR_READ]);
1366 setup_log(&agg_io_log[DDIR_WRITE]);
1367 }
1368
Jens Axboecdd18ad2008-02-27 18:58:00 +01001369 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001370
Jens Axboea2f77c92007-02-22 11:53:00 +01001371 set_genesis_time();
1372
Jens Axboeebac4652005-12-08 15:25:21 +01001373 disk_util_timer_arm();
1374
1375 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001376
Jens Axboebb3884d2007-01-17 17:23:11 +11001377 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001378 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001379 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001380 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1381 __finish_log(agg_io_log[DDIR_WRITE],
1382 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001383 }
1384 }
Jens Axboeebac4652005-12-08 15:25:21 +01001385
Jens Axboecdd18ad2008-02-27 18:58:00 +01001386 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001387 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001388}