blob: 231a581f5478df56054531137d2f1b17ebc3ba57 [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 Axboeebac4652005-12-08 15:25:21 +010052
Jens Axboecdd18ad2008-02-27 18:58:00 +010053static struct fio_mutex *startup_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010054static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010055static int exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +010056
Jens Axboebb3884d2007-01-17 17:23:11 +110057struct io_log *agg_io_log[2];
58
Jens Axboeebac4652005-12-08 15:25:21 +010059#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020060#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010061
Jens Axboe6ce15a32007-01-12 09:04:41 +010062static inline void td_set_runstate(struct thread_data *td, int runstate)
63{
Jens Axboed8fab1b2008-03-06 10:25:17 +010064 if (td->runstate == runstate)
65 return;
66
Jens Axboebd6f78b2008-02-01 20:27:52 +010067 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", td->pid, td->runstate,
68 runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010069 td->runstate = runstate;
70}
71
Jens Axboe390c40e2007-03-05 12:35:29 +010072static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010073{
Jens Axboe34572e22006-10-20 09:33:18 +020074 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010075 int i;
76
Jens Axboe6f88bcb2008-03-19 10:29:07 +010077 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
78
Jens Axboe34572e22006-10-20 09:33:18 +020079 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010080 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +010081 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
82 td->o.name, td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010083 td->terminate = 1;
84 td->o.start_delay = 0;
85
Jens Axboe7a93ab12007-04-18 12:25:41 +020086 /*
87 * if the thread is running, just let it exit
88 */
89 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010090 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010091 else {
92 struct ioengine_ops *ops = td->io_ops;
93
94 if (ops && (ops->flags & FIO_SIGQUIT))
95 kill(td->pid, SIGQUIT);
96 }
Jens Axboeebac4652005-12-08 15:25:21 +010097 }
98 }
99}
100
101static void sig_handler(int sig)
102{
103 switch (sig) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100104 case SIGALRM:
105 update_io_ticks();
106 disk_util_timer_arm();
107 print_thread_status();
108 break;
109 default:
110 printf("\nfio: terminating on signal %d\n", sig);
111 fflush(stdout);
112 terminate_threads(TERMINATE_ALL);
113 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100114 }
115}
116
Jens Axboe906c8d72006-06-13 09:37:56 +0200117/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200118 * Check if we are above the minimum rate given.
119 */
Jens Axboeebac4652005-12-08 15:25:21 +0100120static int check_min_rate(struct thread_data *td, struct timeval *now)
121{
Jens Axboe09042002007-02-23 10:56:22 +0100122 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100123 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100124 unsigned long spent;
125 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100126
127 /*
Jens Axboe780bf1a2007-02-28 11:13:49 +0100128 * No minimum rate set, always ok
129 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100130 if (!td->o.ratemin && !td->o.rate_iops_min)
Jens Axboe780bf1a2007-02-28 11:13:49 +0100131 return 0;
132
133 /*
Jens Axboeebac4652005-12-08 15:25:21 +0100134 * allow a 2 second settle period in the beginning
135 */
136 if (mtime_since(&td->start, now) < 2000)
137 return 0;
138
Jens Axboe4e991c22007-03-15 11:41:11 +0100139 if (td_read(td)) {
140 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100141 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100142 }
143 if (td_write(td)) {
144 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100145 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100146 }
Jens Axboe09042002007-02-23 10:56:22 +0100147
Jens Axboeebac4652005-12-08 15:25:21 +0100148 /*
149 * if rate blocks is set, sample is running
150 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100151 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100152 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100153 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100154 return 0;
155
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100156 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100157 /*
158 * check bandwidth specified rate
159 */
160 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100161 log_err("%s: min rate %u not met\n", td->o.name,
162 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100163 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100164 } else {
165 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100166 if (rate < td->o.ratemin ||
167 bytes < td->rate_bytes) {
168 log_err("%s: min rate %u not met, got"
169 " %luKiB/sec\n", td->o.name,
170 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100171 return 1;
172 }
173 }
174 } else {
175 /*
176 * checks iops specified rate
177 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100178 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100179 log_err("%s: min iops rate %u not met\n",
180 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100181 return 1;
182 } else {
183 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100184 if (rate < td->o.rate_iops_min ||
185 iops < td->rate_blocks) {
186 log_err("%s: min iops rate %u not met,"
187 " got %lu\n", td->o.name,
188 td->o.rate_iops_min,
189 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100190 }
Jens Axboe413dd452007-02-23 09:26:09 +0100191 }
Jens Axboeebac4652005-12-08 15:25:21 +0100192 }
193 }
194
Jens Axboe09042002007-02-23 10:56:22 +0100195 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100196 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100197 memcpy(&td->lastrate, now, sizeof(*now));
198 return 0;
199}
200
201static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
202{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100203 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100204 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100205 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100206 return 1;
207
208 return 0;
209}
210
Jens Axboe906c8d72006-06-13 09:37:56 +0200211/*
212 * When job exits, we can cancel the in-flight IO if we are using async
213 * io. Attempt to do so.
214 */
Jens Axboeebac4652005-12-08 15:25:21 +0100215static void cleanup_pending_aio(struct thread_data *td)
216{
Jens Axboeebac4652005-12-08 15:25:21 +0100217 struct list_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100218 struct io_u *io_u;
219 int r;
220
221 /*
222 * get immediately available events, if any
223 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100224 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100225 if (r < 0)
226 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100227
228 /*
229 * now cancel remaining active events
230 */
Jens Axboe2866c822006-10-09 15:57:48 +0200231 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100232 list_for_each_safe(entry, n, &td->io_u_busylist) {
233 io_u = list_entry(entry, struct io_u, list);
234
Jens Axboe0c6e7512007-02-22 11:19:39 +0100235 /*
236 * if the io_u isn't in flight, then that generally
237 * means someone leaked an io_u. complain but fix
238 * it up, so we don't stall here.
239 */
240 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
241 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100242 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100243 } else {
244 r = td->io_ops->cancel(td, io_u);
245 if (!r)
246 put_io_u(td, io_u);
247 }
Jens Axboeebac4652005-12-08 15:25:21 +0100248 }
249 }
250
Jens Axboe97601022007-02-18 12:47:29 +0100251 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100252 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100253}
254
Jens Axboe906c8d72006-06-13 09:37:56 +0200255/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200256 * Helper to handle the final sync of a file. Works just like the normal
257 * io path, just does everything sync.
258 */
259static int fio_io_sync(struct thread_data *td, struct fio_file *f)
260{
261 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200262 int ret;
263
264 if (!io_u)
265 return 1;
266
267 io_u->ddir = DDIR_SYNC;
268 io_u->file = f;
269
270 if (td_io_prep(td, io_u)) {
271 put_io_u(td, io_u);
272 return 1;
273 }
274
Jens Axboe755200a2007-02-19 13:08:12 +0100275requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200276 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100277 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100278 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200279 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200280 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100281 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100282 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100283 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100284 } else if (ret == FIO_Q_COMPLETED) {
285 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100286 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100287 return 1;
288 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200289
Jens Axboed7762cf2007-02-23 12:34:57 +0100290 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100291 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100292 } else if (ret == FIO_Q_BUSY) {
293 if (td_io_commit(td))
294 return 1;
295 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200296 }
297
298 return 0;
299}
300
301/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100302 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200303 * reads the blocks back in, and checks the crc/md5 of the data.
304 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100305static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100306{
Jens Axboe53cdc682006-10-18 11:50:58 +0200307 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100308 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100309 int ret, min_events;
310 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200311
312 /*
313 * sync io first and invalidate cache, to make sure we really
314 * read from disk.
315 */
316 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100317 if (!(f->flags & FIO_FILE_OPEN))
318 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100319 if (fio_io_sync(td, f))
320 break;
321 if (file_invalidate_cache(td, f))
322 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200323 }
Jens Axboeebac4652005-12-08 15:25:21 +0100324
Jens Axboeb2fdda42007-02-20 20:52:51 +0100325 if (td->error)
326 return;
327
Jens Axboeebac4652005-12-08 15:25:21 +0100328 td_set_runstate(td, TD_VERIFYING);
329
Jens Axboe36167d82007-02-18 05:41:31 +0100330 io_u = NULL;
331 while (!td->terminate) {
Jens Axboe54517922007-03-05 10:06:06 +0100332 int ret2;
333
Jens Axboeebac4652005-12-08 15:25:21 +0100334 io_u = __get_io_u(td);
335 if (!io_u)
336 break;
337
Jens Axboe069c2912007-02-21 23:02:39 +0100338 if (runtime_exceeded(td, &io_u->start_time)) {
339 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200340 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200341 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100342 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200343
Jens Axboe069c2912007-02-21 23:02:39 +0100344 if (get_next_verify(td, io_u)) {
345 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100346 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100347 }
Jens Axboeebac4652005-12-08 15:25:21 +0100348
Jens Axboe069c2912007-02-21 23:02:39 +0100349 if (td_io_prep(td, io_u)) {
350 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100351 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100352 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100353
354 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100355
Jens Axboe11786802007-03-05 18:33:22 +0100356 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100357 switch (ret) {
358 case FIO_Q_COMPLETED:
359 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100360 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100361 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100362 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200363 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100364
Jens Axboe9e9d1642007-03-12 09:37:46 +0100365 /*
366 * zero read, fail
367 */
368 if (!bytes) {
369 td_verror(td, ENODATA, "full resid");
370 put_io_u(td, io_u);
371 break;
372 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200373
Jens Axboe36167d82007-02-18 05:41:31 +0100374 io_u->xfer_buflen = io_u->resid;
375 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200376 io_u->offset += bytes;
377
Jens Axboe30061b92007-04-17 13:31:34 +0200378 td->ts.short_io_u[io_u->ddir]++;
379
Jens Axboed460eb32007-04-16 21:39:33 +0200380 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200381 goto sync_done;
382
Jens Axboe11786802007-03-05 18:33:22 +0100383 requeue_io_u(td, &io_u);
384 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200385sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100386 ret = io_u_sync_complete(td, io_u);
387 if (ret < 0)
388 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100389 }
Jens Axboe36167d82007-02-18 05:41:31 +0100390 continue;
391 case FIO_Q_QUEUED:
392 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100393 case FIO_Q_BUSY:
394 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100395 ret2 = td_io_commit(td);
396 if (ret2 < 0)
397 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100398 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100399 default:
400 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100401 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100402 break;
403 }
404
Jens Axboe99784632007-02-19 10:06:17 +0100405 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100406 break;
407
Jens Axboe3af6ef32007-02-18 06:57:43 +0100408 /*
409 * if we can queue more, do so. but check if there are
410 * completed io_u's first.
411 */
Jens Axboe97601022007-02-18 12:47:29 +0100412 min_events = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100413 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe3af6ef32007-02-18 06:57:43 +0100414 min_events = 1;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100415
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100416 if (td->cur_depth > td->o.iodepth_low)
417 min_events = td->cur_depth - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100418 }
419
Jens Axboe3af6ef32007-02-18 06:57:43 +0100420 /*
421 * Reap required number of io units, if any, and do the
422 * verification on them through the callback handler
423 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100424 if (io_u_queued_complete(td, min_events) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100425 break;
426 }
427
Jens Axboec01c0392007-02-26 12:43:42 +0100428 if (!td->error) {
429 min_events = td->cur_depth;
430
431 if (min_events)
432 ret = io_u_queued_complete(td, min_events);
433 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100434 cleanup_pending_aio(td);
435
436 td_set_runstate(td, TD_RUNNING);
437}
438
Jens Axboe32cd46a2006-06-07 13:40:40 +0200439/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200440 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200441 * and reaps them, checking for rate and errors along the way.
442 */
Jens Axboeebac4652005-12-08 15:25:21 +0100443static void do_io(struct thread_data *td)
444{
Jens Axboe02bcaa82006-11-24 10:42:00 +0100445 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100446 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100447 unsigned int i;
448 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100449
Jens Axboe5853e5a2006-06-08 14:41:05 +0200450 td_set_runstate(td, TD_RUNNING);
451
Jens Axboe7bb48f82007-03-27 15:30:28 +0200452 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100453 struct timeval comp_time;
454 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200455 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100456 struct io_u *io_u;
Jens Axboe54517922007-03-05 10:06:06 +0100457 int ret2;
Jens Axboeebac4652005-12-08 15:25:21 +0100458
459 if (td->terminate)
460 break;
461
Jens Axboe3d7c3912007-02-19 13:16:12 +0100462 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100463 if (!io_u)
464 break;
465
466 memcpy(&s, &io_u->start_time, sizeof(s));
Jens Axboe97601022007-02-18 12:47:29 +0100467
468 if (runtime_exceeded(td, &s)) {
469 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200470 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100471 break;
472 }
Jens Axboe36167d82007-02-18 05:41:31 +0100473
Jens Axboeb67740d2007-07-26 12:22:30 +0200474 /*
475 * Add verification end_io handler, if asked to verify
476 * a previously written file.
477 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100478 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200479 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100480 td_set_runstate(td, TD_VERIFYING);
481 } else
482 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200483
Jens Axboe11786802007-03-05 18:33:22 +0100484 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100485 switch (ret) {
486 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100487 if (io_u->error)
488 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100489 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100490 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200491 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100492
Jens Axboe9e9d1642007-03-12 09:37:46 +0100493 /*
494 * zero read, fail
495 */
496 if (!bytes) {
497 td_verror(td, ENODATA, "full resid");
498 put_io_u(td, io_u);
499 break;
500 }
501
Jens Axboe36167d82007-02-18 05:41:31 +0100502 io_u->xfer_buflen = io_u->resid;
503 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200504 io_u->offset += bytes;
505
Jens Axboe30061b92007-04-17 13:31:34 +0200506 td->ts.short_io_u[io_u->ddir]++;
507
Jens Axboed460eb32007-04-16 21:39:33 +0200508 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200509 goto sync_done;
510
Jens Axboe11786802007-03-05 18:33:22 +0100511 requeue_io_u(td, &io_u);
512 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200513sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100514 fio_gettime(&comp_time, NULL);
515 bytes_done = io_u_sync_complete(td, io_u);
516 if (bytes_done < 0)
517 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100518 }
Jens Axboe36167d82007-02-18 05:41:31 +0100519 break;
520 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100521 /*
522 * if the engine doesn't have a commit hook,
523 * the io_u is really queued. if it does have such
524 * a hook, it has to call io_u_queued() itself.
525 */
526 if (td->io_ops->commit == NULL)
527 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100528 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100529 case FIO_Q_BUSY:
530 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100531 ret2 = td_io_commit(td);
532 if (ret2 < 0)
533 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100534 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100535 default:
536 assert(ret < 0);
537 put_io_u(td, io_u);
538 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100539 }
540
Jens Axboe99784632007-02-19 10:06:17 +0100541 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100542 break;
543
Jens Axboe97601022007-02-18 12:47:29 +0100544 /*
545 * See if we need to complete some commands
546 */
Jens Axboe755200a2007-02-19 13:08:12 +0100547 if (ret == FIO_Q_QUEUED || ret == FIO_Q_BUSY) {
Jens Axboe97601022007-02-18 12:47:29 +0100548 min_evts = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100549 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe36167d82007-02-18 05:41:31 +0100550 min_evts = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100551
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100552 if (td->cur_depth > td->o.iodepth_low)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100553 min_evts = td->cur_depth
554 - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100555 }
556
Jens Axboe97601022007-02-18 12:47:29 +0100557 fio_gettime(&comp_time, NULL);
Jens Axboed7762cf2007-02-23 12:34:57 +0100558 bytes_done = io_u_queued_complete(td, min_evts);
Jens Axboe97601022007-02-18 12:47:29 +0100559 if (bytes_done < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100560 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100561 }
562
Jens Axboe97601022007-02-18 12:47:29 +0100563 if (!bytes_done)
564 continue;
565
Jens Axboeebac4652005-12-08 15:25:21 +0100566 /*
567 * the rate is batched for now, it should work for batches
568 * of completions except the very first one which may look
569 * a little bursty
570 */
Jens Axboe97601022007-02-18 12:47:29 +0100571 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100572
Jens Axboe413dd452007-02-23 09:26:09 +0100573 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100574
Jens Axboe97601022007-02-18 12:47:29 +0100575 if (check_min_rate(td, &comp_time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100576 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100577 terminate_threads(td->groupid);
Jens Axboee1161c32007-02-22 19:36:48 +0100578 td_verror(td, ENODATA, "check_min_rate");
Jens Axboeebac4652005-12-08 15:25:21 +0100579 break;
580 }
581
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100582 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100583 unsigned long long b;
584
585 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100586 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100587 int left;
588
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100589 if (td->o.thinktime_spin)
590 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100591
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100592 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100593 if (left)
594 usec_sleep(td, left);
595 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100596 }
Jens Axboeebac4652005-12-08 15:25:21 +0100597 }
598
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100599 if (td->o.fill_device && td->error == ENOSPC) {
600 td->error = 0;
601 td->terminate = 1;
602 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100603 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100604 struct fio_file *f;
605
Jens Axboec01c0392007-02-26 12:43:42 +0100606 i = td->cur_depth;
607 if (i)
608 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100609
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100610 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200611 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100612
613 for_each_file(td, f, i) {
614 if (!(f->flags & FIO_FILE_OPEN))
615 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200616 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100617 }
Jens Axboe84585002006-10-19 20:26:22 +0200618 }
Jens Axboec01c0392007-02-26 12:43:42 +0100619 } else
620 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100621
622 /*
623 * stop job if we failed doing any IO
624 */
625 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
626 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100627}
628
Jens Axboeebac4652005-12-08 15:25:21 +0100629static void cleanup_io_u(struct thread_data *td)
630{
631 struct list_head *entry, *n;
632 struct io_u *io_u;
633
634 list_for_each_safe(entry, n, &td->io_u_freelist) {
635 io_u = list_entry(entry, struct io_u, list);
636
637 list_del(&io_u->list);
638 free(io_u);
639 }
640
Jens Axboe2f9ade32006-10-20 11:25:52 +0200641 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100642}
643
Jens Axboe6b9cea22006-10-30 17:00:24 +0100644/*
645 * "randomly" fill the buffer contents
646 */
Jens Axboee9459e52007-04-17 15:46:32 +0200647static void fill_io_buf(struct thread_data *td, struct io_u *io_u, int max_bs)
Jens Axboe6b9cea22006-10-30 17:00:24 +0100648{
Jens Axboe210f43b2007-04-17 19:52:18 +0200649 long *ptr = io_u->buf;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100650
Jens Axboee9459e52007-04-17 15:46:32 +0200651 if (!td->o.zero_buffers) {
652 while ((void *) ptr - io_u->buf < max_bs) {
Jens Axboe210f43b2007-04-17 19:52:18 +0200653 *ptr = rand() * GOLDEN_RATIO_PRIME;
Jens Axboee9459e52007-04-17 15:46:32 +0200654 ptr++;
655 }
656 } else
657 memset(ptr, 0, max_bs);
Jens Axboe6b9cea22006-10-30 17:00:24 +0100658}
659
Jens Axboeebac4652005-12-08 15:25:21 +0100660static int init_io_u(struct thread_data *td)
661{
662 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100663 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100664 int i, max_units;
665 char *p;
666
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100667 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100668 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100669 td->orig_buffer_size = (unsigned long long) max_bs
670 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100671
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100672 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
673 unsigned long bs;
674
675 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
676 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
677 }
Jens Axboeebac4652005-12-08 15:25:21 +0100678
Jens Axboec3990642007-07-19 10:17:09 +0200679 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
680 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
681 return 1;
682 }
683
Jens Axboe2f9ade32006-10-20 11:25:52 +0200684 if (allocate_io_mem(td))
685 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100686
Jens Axboeb3f378e2007-07-20 12:39:22 +0200687 if (td->o.odirect)
688 p = ALIGN(td->orig_buffer);
689 else
690 p = td->orig_buffer;
691
Jens Axboeebac4652005-12-08 15:25:21 +0100692 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200693 if (td->terminate)
694 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100695 io_u = malloc(sizeof(*io_u));
696 memset(io_u, 0, sizeof(*io_u));
697 INIT_LIST_HEAD(&io_u->list);
698
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200699 if (!(td->io_ops->flags & FIO_NOIO)) {
700 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200701
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200702 if (td_write(td))
703 fill_io_buf(td, io_u, max_bs);
704 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100705
Jens Axboeb1ff3402006-02-17 11:35:58 +0100706 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100707 io_u->flags = IO_U_F_FREE;
Jens Axboeebac4652005-12-08 15:25:21 +0100708 list_add(&io_u->list, &td->io_u_freelist);
709 }
710
Jens Axboe433afcb2007-02-22 10:39:01 +0100711 io_u_init_timeout();
712
Jens Axboeebac4652005-12-08 15:25:21 +0100713 return 0;
714}
715
Jens Axboeda867742006-06-06 10:39:10 -0700716static int switch_ioscheduler(struct thread_data *td)
717{
718 char tmp[256], tmp2[128];
719 FILE *f;
720 int ret;
721
Jens Axboeba0fbe12007-03-09 14:34:23 +0100722 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200723 return 0;
724
Jens Axboeda867742006-06-06 10:39:10 -0700725 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
726
727 f = fopen(tmp, "r+");
728 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200729 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100730 log_err("fio: os or kernel doesn't support IO scheduler"
731 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200732 return 0;
733 }
734 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700735 return 1;
736 }
737
738 /*
739 * Set io scheduler.
740 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100741 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700742 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100743 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700744 fclose(f);
745 return 1;
746 }
747
748 rewind(f);
749
750 /*
751 * Read back and check that the selected scheduler is now the default.
752 */
753 ret = fread(tmp, 1, sizeof(tmp), f);
754 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100755 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700756 fclose(f);
757 return 1;
758 }
759
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100760 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700761 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100762 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100763 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700764 fclose(f);
765 return 1;
766 }
767
768 fclose(f);
769 return 0;
770}
771
Jens Axboe492158c2007-05-24 10:32:47 +0200772static int keep_running(struct thread_data *td)
773{
774 unsigned long long io_done;
775
Jens Axboe20e354e2007-07-23 14:36:16 +0200776 if (td->done)
777 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200778 if (td->o.time_based)
779 return 1;
780 if (td->o.loops) {
781 td->o.loops--;
782 return 1;
783 }
784
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100785 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
786 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200787 if (io_done < td->o.size)
788 return 1;
789
790 return 0;
791}
792
Jens Axboea978ba62007-03-08 14:29:03 +0100793static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100794{
Jens Axboe53cdc682006-10-18 11:50:58 +0200795 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100796 unsigned int i;
797 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100798
Jens Axboe756867b2007-03-06 15:19:24 +0100799 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100800 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100801 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100802 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100803 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100804 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100805
Jens Axboec1324df2007-02-19 19:06:41 +0100806 td->last_was_sync = 0;
807
Jens Axboe2ba1c292008-02-01 13:16:38 +0100808 /*
809 * reset file done count if we are to start over
810 */
811 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200812 td->nr_done_files = 0;
Jens Axboe281f9b62007-05-23 09:34:42 +0200813
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100814 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100815
816 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200817 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200818 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100819 ret = td_io_open_file(td, f);
820 if (ret)
821 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200822 }
Jens Axboea978ba62007-03-08 14:29:03 +0100823
824 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100825}
826
Jens Axboe906c8d72006-06-13 09:37:56 +0200827/*
828 * Entry point for the thread based jobs. The process based jobs end up
829 * here as well, after a little setup.
830 */
Jens Axboeebac4652005-12-08 15:25:21 +0100831static void *thread_main(void *data)
832{
Shawn Lewis10927312007-11-21 09:38:13 +0100833 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100834 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100835 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100836
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100837 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100838 setsid();
839
840 td->pid = getpid();
841
Jens Axboeee56ad52008-02-01 10:30:20 +0100842 dprint(FD_PROCESS, "jobs pid=%d started\n", td->pid);
843
Jens Axboeaea47d42006-05-26 19:27:29 +0200844 INIT_LIST_HEAD(&td->io_u_freelist);
845 INIT_LIST_HEAD(&td->io_u_busylist);
Jens Axboe755200a2007-02-19 13:08:12 +0100846 INIT_LIST_HEAD(&td->io_u_requeues);
Jens Axboeaea47d42006-05-26 19:27:29 +0200847 INIT_LIST_HEAD(&td->io_log_list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200848 INIT_LIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200849 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200850
Jens Axboe75154842006-06-01 13:56:09 +0200851 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100852 fio_mutex_up(startup_mutex);
853 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100854
Jens Axboe37f56872007-03-09 12:42:35 +0100855 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100856 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100857 * eating a file descriptor
858 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100859 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100860
Jens Axboed84f8d42007-05-16 08:47:46 +0200861 /*
862 * May alter parameters that init_io_u() will use, so we need to
863 * do this first.
864 */
865 if (init_iolog(td))
866 goto err;
867
Jens Axboedb1cd902007-04-26 13:47:07 +0200868 if (init_io_u(td))
869 goto err;
870
Jens Axboe375b2692007-05-22 09:13:02 +0200871 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200872 td_verror(td, errno, "cpu_set_affinity");
873 goto err;
874 }
875
Jens Axboeac684782007-11-08 08:29:07 +0100876 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200877 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
878 td_verror(td, errno, "ioprio_set");
879 goto err;
880 }
881 }
882
883 if (nice(td->o.nice) == -1) {
884 td_verror(td, errno, "nice");
885 goto err;
886 }
887
888 if (td->o.ioscheduler && switch_ioscheduler(td))
889 goto err;
890
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100891 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100892 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100893
894 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100895 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100896
Jens Axboeb5af8292007-03-08 12:43:13 +0100897 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100898 goto err;
899
Jens Axboe68727072007-03-15 20:49:25 +0100900 if (init_random_map(td))
901 goto err;
902
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100903 if (td->o.exec_prerun) {
904 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100905 goto err;
906 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200907
Jens Axboe69008992006-11-24 13:12:56 +0100908 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100909 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100910 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100911
912 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100913 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200914 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100915 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100916 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100917
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100918 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100919 memcpy(&td->lastrate, &td->ts.stat_sample_time,
920 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100921
Jens Axboea978ba62007-03-08 14:29:03 +0100922 if (clear_state && clear_io_state(td))
923 break;
924
Jens Axboeebac4652005-12-08 15:25:21 +0100925 prune_io_piece_log(td);
926
Jens Axboeba0fbe12007-03-09 14:34:23 +0100927 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100928
Jens Axboea978ba62007-03-08 14:29:03 +0100929 clear_state = 1;
930
Jens Axboe38d77ca2007-03-21 14:20:20 +0100931 if (td_read(td) && td->io_bytes[DDIR_READ]) {
932 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100933 elapsed = utime_since(&td->start,
934 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100935 else
936 elapsed = utime_since_now(&td->start);
937
938 runtime[DDIR_READ] += elapsed;
939 }
940 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
941 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100942 elapsed = utime_since(&td->start,
943 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100944 else
945 elapsed = utime_since_now(&td->start);
946
947 runtime[DDIR_WRITE] += elapsed;
948 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100949
Jens Axboeebac4652005-12-08 15:25:21 +0100950 if (td->error || td->terminate)
951 break;
952
Shawn Lewise84c73a2007-08-02 22:19:32 +0200953 if (!td->o.do_verify ||
954 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +0200955 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +0100956 continue;
957
Jens Axboea978ba62007-03-08 14:29:03 +0100958 if (clear_io_state(td))
959 break;
960
Jens Axboe02bcaa82006-11-24 10:42:00 +0100961 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100962
963 do_verify(td);
964
Jens Axboe69008992006-11-24 13:12:56 +0100965 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100966
967 if (td->error || td->terminate)
968 break;
969 }
970
Jens Axboe36dff962006-11-24 13:29:20 +0100971 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +0200972 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
973 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +0100974 td->ts.total_run_time = mtime_since_now(&td->epoch);
975 td->ts.io_bytes[0] = td->io_bytes[0];
976 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +0100977
Jens Axboe756867b2007-03-06 15:19:24 +0100978 if (td->ts.bw_log)
979 finish_log(td, td->ts.bw_log, "bw");
980 if (td->ts.slat_log)
981 finish_log(td, td->ts.slat_log, "slat");
982 if (td->ts.clat_log)
983 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100984 if (td->o.exec_postrun) {
985 if (system(td->o.exec_postrun) < 0)
986 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100987 }
Jens Axboeebac4652005-12-08 15:25:21 +0100988
989 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100990 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100991
992err:
Jens Axboe5bf13a52007-02-17 01:51:47 +0100993 if (td->error)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100994 printf("fio: pid=%d, err=%d/%s\n", td->pid, td->error,
995 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100996 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200997 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100998 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +0200999
1000 /*
1001 * do this very late, it will log file closing as well
1002 */
1003 if (td->o.write_iolog_file)
1004 write_iolog_close(td);
1005
Jens Axboed23bb322007-03-23 15:57:56 +01001006 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001007 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001008 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001009}
1010
Jens Axboe906c8d72006-06-13 09:37:56 +02001011/*
1012 * We cannot pass the td data into a forked process, so attach the td and
1013 * pass it to the thread worker.
1014 */
Jens Axboea6418142007-02-17 04:47:18 +01001015static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001016{
1017 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001018 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001019
1020 data = shmat(shmid, NULL, 0);
1021 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001022 int __err = errno;
1023
Jens Axboeebac4652005-12-08 15:25:21 +01001024 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001025 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001026 }
1027
1028 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001029 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001030 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001031 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001032}
1033
Jens Axboe906c8d72006-06-13 09:37:56 +02001034/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001035 * Run over the job map and reap the threads that have exited, if any.
1036 */
Jens Axboeebac4652005-12-08 15:25:21 +01001037static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1038{
Jens Axboe34572e22006-10-20 09:33:18 +02001039 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001040 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001041
1042 /*
1043 * reap exited threads (TD_EXITED -> TD_REAPED)
1044 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001045 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001046 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001047 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001048
Jens Axboe84585002006-10-19 20:26:22 +02001049 /*
1050 * ->io_ops is NULL for a thread that has closed its
1051 * io engine
1052 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001053 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001054 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001055 else
1056 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001057
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001058 if (!td->pid) {
1059 pending++;
1060 continue;
1061 }
1062 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001063 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001064 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001065 if (td->runstate == TD_EXITED) {
1066 td_set_runstate(td, TD_REAPED);
1067 goto reaped;
1068 }
1069 continue;
1070 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001071
1072 flags = WNOHANG;
1073 if (td->runstate == TD_EXITED)
1074 flags = 0;
1075
1076 /*
1077 * check if someone quit or got killed in an unusual way
1078 */
1079 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001080 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001081 if (errno == ECHILD) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001082 log_err("fio: pid=%d disappeared %d\n", td->pid,
1083 td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001084 td_set_runstate(td, TD_REAPED);
1085 goto reaped;
1086 }
1087 perror("waitpid");
1088 } else if (ret == td->pid) {
1089 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001090 int sig = WTERMSIG(status);
1091
Jens Axboed9244942007-03-05 12:32:32 +01001092 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001093 log_err("fio: pid=%d, got signal=%d\n",
1094 td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001095 td_set_runstate(td, TD_REAPED);
1096 goto reaped;
1097 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001098 if (WIFEXITED(status)) {
1099 if (WEXITSTATUS(status) && !td->error)
1100 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001101
Jens Axboea2f77c92007-02-22 11:53:00 +01001102 td_set_runstate(td, TD_REAPED);
1103 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001104 }
1105 }
Jens Axboeebac4652005-12-08 15:25:21 +01001106
Jens Axboea2f77c92007-02-22 11:53:00 +01001107 /*
1108 * thread is not dead, continue
1109 */
gurudas paie48676b2007-04-11 12:44:27 +02001110 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001111 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001112reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001113 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001114 (*m_rate) -= td->o.ratemin;
1115 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001116 if (!td->pid)
1117 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001118
1119 if (td->error)
1120 exit_value++;
Jens Axboeebac4652005-12-08 15:25:21 +01001121 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001122
Jens Axboe1f809d12007-10-25 18:34:02 +02001123 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001124 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001125}
1126
Jens Axboe906c8d72006-06-13 09:37:56 +02001127/*
1128 * Main function for kicking off and reaping jobs, as needed.
1129 */
Jens Axboeebac4652005-12-08 15:25:21 +01001130static void run_threads(void)
1131{
Jens Axboeebac4652005-12-08 15:25:21 +01001132 struct thread_data *td;
1133 unsigned long spent;
1134 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001135
Jens Axboe2f9ade32006-10-20 11:25:52 +02001136 if (fio_pin_memory())
1137 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001138
Jens Axboec6ae0a52006-06-12 11:04:44 +02001139 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001140 printf("Starting ");
1141 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001142 printf("%d thread%s", nr_thread,
1143 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001144 if (nr_process) {
1145 if (nr_thread)
1146 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001147 printf("%d process%s", nr_process,
1148 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001149 }
1150 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001151 fflush(stdout);
1152 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001153
Jens Axboe4efa9702006-05-31 11:56:49 +02001154 signal(SIGINT, sig_handler);
1155 signal(SIGALRM, sig_handler);
1156
Jens Axboeebac4652005-12-08 15:25:21 +01001157 todo = thread_number;
1158 nr_running = 0;
1159 nr_started = 0;
1160 m_rate = t_rate = 0;
1161
Jens Axboe34572e22006-10-20 09:33:18 +02001162 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001163 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001164
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001165 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001166 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001167 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001168 }
Jens Axboeebac4652005-12-08 15:25:21 +01001169
1170 /*
1171 * do file setup here so it happens sequentially,
1172 * we don't want X number of threads getting their
1173 * client data interspersed on disk
1174 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001175 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001176 exit_value++;
1177 if (td->error)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001178 log_err("fio: pid=%d, err=%d/%s\n", td->pid,
1179 td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001180 td_set_runstate(td, TD_REAPED);
1181 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001182 } else {
1183 struct fio_file *f;
1184 unsigned int i;
1185
1186 /*
1187 * for sharing to work, each job must always open
1188 * its own files. so close them, if we opened them
1189 * for creation
1190 */
1191 for_each_file(td, f, i)
1192 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001193 }
Jens Axboe380cf262007-01-11 14:01:27 +01001194
1195 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001196 }
1197
Jens Axboea2f77c92007-02-22 11:53:00 +01001198 set_genesis_time();
1199
Jens Axboeebac4652005-12-08 15:25:21 +01001200 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001201 struct thread_data *map[MAX_JOBS];
1202 struct timeval this_start;
1203 int this_jobs = 0, left;
1204
Jens Axboeebac4652005-12-08 15:25:21 +01001205 /*
1206 * create threads (TD_NOT_CREATED -> TD_CREATED)
1207 */
Jens Axboe34572e22006-10-20 09:33:18 +02001208 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001209 if (td->runstate != TD_NOT_CREATED)
1210 continue;
1211
1212 /*
1213 * never got a chance to start, killed by other
1214 * thread for some reason
1215 */
1216 if (td->terminate) {
1217 todo--;
1218 continue;
1219 }
1220
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001221 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001222 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001223
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001224 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001225 continue;
1226 }
1227
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001228 if (td->o.stonewall && (nr_started || nr_running)) {
1229 dprint(FD_PROCESS, "%s: stonewall wait\n",
1230 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001231 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001232 }
Jens Axboeebac4652005-12-08 15:25:21 +01001233
Jens Axboe75154842006-06-01 13:56:09 +02001234 /*
1235 * Set state to created. Thread will transition
1236 * to TD_INITIALIZED when it's done setting up.
1237 */
Jens Axboeebac4652005-12-08 15:25:21 +01001238 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001239 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001240 nr_started++;
1241
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001242 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001243 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001244 if (pthread_create(&td->thread, NULL,
1245 thread_main, td)) {
Jens Axboef4705a72008-03-10 10:52:22 +01001246 perror("pthread_create");
Jens Axboeebac4652005-12-08 15:25:21 +01001247 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001248 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001249 }
Jens Axboef4705a72008-03-10 10:52:22 +01001250 if (pthread_detach(td->thread) < 0)
1251 perror("pthread_detach");
Jens Axboeebac4652005-12-08 15:25:21 +01001252 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001253 dprint(FD_PROCESS, "will fork\n");
Jens Axboe07739b52007-03-08 20:25:46 +01001254 if (!fork()) {
Jens Axboea6418142007-02-17 04:47:18 +01001255 int ret = fork_main(shm_id, i);
1256
1257 exit(ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001258 }
1259 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001260 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001261 }
1262
1263 /*
Jens Axboe75154842006-06-01 13:56:09 +02001264 * Wait for the started threads to transition to
1265 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001266 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001267 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001268 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001269 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001270 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1271 break;
1272
1273 usleep(100000);
1274
1275 for (i = 0; i < this_jobs; i++) {
1276 td = map[i];
1277 if (!td)
1278 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001279 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001280 map[i] = NULL;
1281 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001282 } else if (td->runstate >= TD_EXITED) {
1283 map[i] = NULL;
1284 left--;
1285 todo--;
1286 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001287 }
1288 }
1289 }
1290
1291 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001292 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001293 for (i = 0; i < this_jobs; i++) {
1294 td = map[i];
1295 if (!td)
1296 continue;
1297 kill(td->pid, SIGTERM);
1298 }
1299 break;
1300 }
1301
1302 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001303 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001304 */
Jens Axboe34572e22006-10-20 09:33:18 +02001305 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001306 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001307 continue;
1308
1309 td_set_runstate(td, TD_RUNNING);
1310 nr_running++;
1311 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001312 m_rate += td->o.ratemin;
1313 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001314 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001315 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001316 }
1317
1318 reap_threads(&nr_running, &t_rate, &m_rate);
1319
1320 if (todo)
1321 usleep(100000);
1322 }
1323
1324 while (nr_running) {
1325 reap_threads(&nr_running, &t_rate, &m_rate);
1326 usleep(10000);
1327 }
1328
1329 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001330 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001331}
1332
Jens Axboeebac4652005-12-08 15:25:21 +01001333int main(int argc, char *argv[])
1334{
Jens Axboe29d610e2007-02-06 13:25:33 +01001335 long ps;
1336
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001337 sinit();
1338
Jens Axboedbe11252007-02-11 00:19:51 +01001339 /*
1340 * We need locale for number printing, if it isn't set then just
1341 * go with the US format.
1342 */
1343 if (!getenv("LC_NUMERIC"))
1344 setlocale(LC_NUMERIC, "en_US");
1345
Jens Axboeebac4652005-12-08 15:25:21 +01001346 if (parse_options(argc, argv))
1347 return 1;
1348
Jens Axboe4b472fa2007-04-04 11:04:34 +02001349 if (!thread_number)
1350 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001351
Jens Axboe29d610e2007-02-06 13:25:33 +01001352 ps = sysconf(_SC_PAGESIZE);
1353 if (ps < 0) {
1354 log_err("Failed to get page size\n");
1355 return 1;
1356 }
1357
Jens Axboecfc99db2007-03-14 10:34:47 +01001358 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001359 page_mask = ps - 1;
1360
Jens Axboebb3884d2007-01-17 17:23:11 +11001361 if (write_bw_log) {
1362 setup_log(&agg_io_log[DDIR_READ]);
1363 setup_log(&agg_io_log[DDIR_WRITE]);
1364 }
1365
Jens Axboecdd18ad2008-02-27 18:58:00 +01001366 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001367
Jens Axboea2f77c92007-02-22 11:53:00 +01001368 set_genesis_time();
1369
Jens Axboeebac4652005-12-08 15:25:21 +01001370 disk_util_timer_arm();
1371
1372 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001373
Jens Axboebb3884d2007-01-17 17:23:11 +11001374 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001375 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001376 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001377 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1378 __finish_log(agg_io_log[DDIR_WRITE],
1379 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001380 }
1381 }
Jens Axboeebac4652005-12-08 15:25:21 +01001382
Jens Axboecdd18ad2008-02-27 18:58:00 +01001383 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001384 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001385}