blob: 6a72ce773820d0a19cad1497dd41e0a64ae31128 [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 Axboe34572e22006-10-20 09:33:18 +020077 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010078 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboeee56ad52008-02-01 10:30:20 +010079 dprint(FD_PROCESS, "setting terminate on %d\n",td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010080
81 td->terminate = 1;
82 td->o.start_delay = 0;
83
Jens Axboe7a93ab12007-04-18 12:25:41 +020084 /*
85 * if the thread is running, just let it exit
86 */
87 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010088 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010089 else {
90 struct ioengine_ops *ops = td->io_ops;
91
92 if (ops && (ops->flags & FIO_SIGQUIT))
93 kill(td->pid, SIGQUIT);
94 }
Jens Axboeebac4652005-12-08 15:25:21 +010095 }
96 }
97}
98
99static void sig_handler(int sig)
100{
101 switch (sig) {
102 case SIGALRM:
103 update_io_ticks();
104 disk_util_timer_arm();
105 print_thread_status();
106 break;
107 default:
Jens Axboe6ce15a32007-01-12 09:04:41 +0100108 printf("\nfio: terminating on signal %d\n", sig);
Jens Axboeebac4652005-12-08 15:25:21 +0100109 fflush(stdout);
Jens Axboe390c40e2007-03-05 12:35:29 +0100110 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100111 break;
112 }
113}
114
Jens Axboe906c8d72006-06-13 09:37:56 +0200115/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200116 * Check if we are above the minimum rate given.
117 */
Jens Axboeebac4652005-12-08 15:25:21 +0100118static int check_min_rate(struct thread_data *td, struct timeval *now)
119{
Jens Axboe09042002007-02-23 10:56:22 +0100120 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100121 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100122 unsigned long spent;
123 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100124
125 /*
Jens Axboe780bf1a2007-02-28 11:13:49 +0100126 * No minimum rate set, always ok
127 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100128 if (!td->o.ratemin && !td->o.rate_iops_min)
Jens Axboe780bf1a2007-02-28 11:13:49 +0100129 return 0;
130
131 /*
Jens Axboeebac4652005-12-08 15:25:21 +0100132 * allow a 2 second settle period in the beginning
133 */
134 if (mtime_since(&td->start, now) < 2000)
135 return 0;
136
Jens Axboe4e991c22007-03-15 11:41:11 +0100137 if (td_read(td)) {
138 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100139 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100140 }
141 if (td_write(td)) {
142 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100143 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100144 }
Jens Axboe09042002007-02-23 10:56:22 +0100145
Jens Axboeebac4652005-12-08 15:25:21 +0100146 /*
147 * if rate blocks is set, sample is running
148 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100149 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100150 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100151 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100152 return 0;
153
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100154 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100155 /*
156 * check bandwidth specified rate
157 */
158 if (bytes < td->rate_bytes) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100159 log_err("%s: min rate %u not met\n", td->o.name, td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100160 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100161 } else {
162 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100163 if (rate < td->o.ratemin || bytes < td->rate_bytes) {
164 log_err("%s: min rate %u not met, got %luKiB/sec\n", td->o.name, td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100165 return 1;
166 }
167 }
168 } else {
169 /*
170 * checks iops specified rate
171 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100172 if (iops < td->o.rate_iops) {
173 log_err("%s: min iops rate %u not met\n", td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100174 return 1;
175 } else {
176 rate = (iops - td->rate_blocks) / spent;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100177 if (rate < td->o.rate_iops_min || iops < td->rate_blocks) {
178 log_err("%s: min iops rate %u not met, got %lu\n", td->o.name, td->o.rate_iops_min, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100179 }
Jens Axboe413dd452007-02-23 09:26:09 +0100180 }
Jens Axboeebac4652005-12-08 15:25:21 +0100181 }
182 }
183
Jens Axboe09042002007-02-23 10:56:22 +0100184 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100185 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100186 memcpy(&td->lastrate, now, sizeof(*now));
187 return 0;
188}
189
190static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
191{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100192 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100193 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100194 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100195 return 1;
196
197 return 0;
198}
199
Jens Axboe906c8d72006-06-13 09:37:56 +0200200/*
201 * When job exits, we can cancel the in-flight IO if we are using async
202 * io. Attempt to do so.
203 */
Jens Axboeebac4652005-12-08 15:25:21 +0100204static void cleanup_pending_aio(struct thread_data *td)
205{
Jens Axboeebac4652005-12-08 15:25:21 +0100206 struct list_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100207 struct io_u *io_u;
208 int r;
209
210 /*
211 * get immediately available events, if any
212 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100213 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100214 if (r < 0)
215 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100216
217 /*
218 * now cancel remaining active events
219 */
Jens Axboe2866c822006-10-09 15:57:48 +0200220 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100221 list_for_each_safe(entry, n, &td->io_u_busylist) {
222 io_u = list_entry(entry, struct io_u, list);
223
Jens Axboe0c6e7512007-02-22 11:19:39 +0100224 /*
225 * if the io_u isn't in flight, then that generally
226 * means someone leaked an io_u. complain but fix
227 * it up, so we don't stall here.
228 */
229 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
230 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100231 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100232 } else {
233 r = td->io_ops->cancel(td, io_u);
234 if (!r)
235 put_io_u(td, io_u);
236 }
Jens Axboeebac4652005-12-08 15:25:21 +0100237 }
238 }
239
Jens Axboe97601022007-02-18 12:47:29 +0100240 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100241 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100242}
243
Jens Axboe906c8d72006-06-13 09:37:56 +0200244/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200245 * Helper to handle the final sync of a file. Works just like the normal
246 * io path, just does everything sync.
247 */
248static int fio_io_sync(struct thread_data *td, struct fio_file *f)
249{
250 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200251 int ret;
252
253 if (!io_u)
254 return 1;
255
256 io_u->ddir = DDIR_SYNC;
257 io_u->file = f;
258
259 if (td_io_prep(td, io_u)) {
260 put_io_u(td, io_u);
261 return 1;
262 }
263
Jens Axboe755200a2007-02-19 13:08:12 +0100264requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200265 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100266 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100267 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200268 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200269 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100270 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100271 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100272 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100273 } else if (ret == FIO_Q_COMPLETED) {
274 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100275 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100276 return 1;
277 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200278
Jens Axboed7762cf2007-02-23 12:34:57 +0100279 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100280 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100281 } else if (ret == FIO_Q_BUSY) {
282 if (td_io_commit(td))
283 return 1;
284 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200285 }
286
287 return 0;
288}
289
290/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100291 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200292 * reads the blocks back in, and checks the crc/md5 of the data.
293 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100294static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100295{
Jens Axboe53cdc682006-10-18 11:50:58 +0200296 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100297 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100298 int ret, min_events;
299 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200300
301 /*
302 * sync io first and invalidate cache, to make sure we really
303 * read from disk.
304 */
305 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100306 if (!(f->flags & FIO_FILE_OPEN))
307 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100308 if (fio_io_sync(td, f))
309 break;
310 if (file_invalidate_cache(td, f))
311 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200312 }
Jens Axboeebac4652005-12-08 15:25:21 +0100313
Jens Axboeb2fdda42007-02-20 20:52:51 +0100314 if (td->error)
315 return;
316
Jens Axboeebac4652005-12-08 15:25:21 +0100317 td_set_runstate(td, TD_VERIFYING);
318
Jens Axboe36167d82007-02-18 05:41:31 +0100319 io_u = NULL;
320 while (!td->terminate) {
Jens Axboe54517922007-03-05 10:06:06 +0100321 int ret2;
322
Jens Axboeebac4652005-12-08 15:25:21 +0100323 io_u = __get_io_u(td);
324 if (!io_u)
325 break;
326
Jens Axboe069c2912007-02-21 23:02:39 +0100327 if (runtime_exceeded(td, &io_u->start_time)) {
328 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200329 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200330 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100331 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200332
Jens Axboe069c2912007-02-21 23:02:39 +0100333 if (get_next_verify(td, io_u)) {
334 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100335 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100336 }
Jens Axboeebac4652005-12-08 15:25:21 +0100337
Jens Axboe069c2912007-02-21 23:02:39 +0100338 if (td_io_prep(td, io_u)) {
339 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100340 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100341 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100342
343 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100344
Jens Axboe11786802007-03-05 18:33:22 +0100345 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100346 switch (ret) {
347 case FIO_Q_COMPLETED:
348 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100349 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100350 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100351 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200352 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100353
Jens Axboe9e9d1642007-03-12 09:37:46 +0100354 /*
355 * zero read, fail
356 */
357 if (!bytes) {
358 td_verror(td, ENODATA, "full resid");
359 put_io_u(td, io_u);
360 break;
361 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200362
Jens Axboe36167d82007-02-18 05:41:31 +0100363 io_u->xfer_buflen = io_u->resid;
364 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200365 io_u->offset += bytes;
366
Jens Axboe30061b92007-04-17 13:31:34 +0200367 td->ts.short_io_u[io_u->ddir]++;
368
Jens Axboed460eb32007-04-16 21:39:33 +0200369 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200370 goto sync_done;
371
Jens Axboe11786802007-03-05 18:33:22 +0100372 requeue_io_u(td, &io_u);
373 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200374sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100375 ret = io_u_sync_complete(td, io_u);
376 if (ret < 0)
377 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100378 }
Jens Axboe36167d82007-02-18 05:41:31 +0100379 continue;
380 case FIO_Q_QUEUED:
381 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100382 case FIO_Q_BUSY:
383 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100384 ret2 = td_io_commit(td);
385 if (ret2 < 0)
386 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100387 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100388 default:
389 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100390 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100391 break;
392 }
393
Jens Axboe99784632007-02-19 10:06:17 +0100394 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100395 break;
396
Jens Axboe3af6ef32007-02-18 06:57:43 +0100397 /*
398 * if we can queue more, do so. but check if there are
399 * completed io_u's first.
400 */
Jens Axboe97601022007-02-18 12:47:29 +0100401 min_events = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100402 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe3af6ef32007-02-18 06:57:43 +0100403 min_events = 1;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100404
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100405 if (td->cur_depth > td->o.iodepth_low)
406 min_events = td->cur_depth - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100407 }
408
Jens Axboe3af6ef32007-02-18 06:57:43 +0100409 /*
410 * Reap required number of io units, if any, and do the
411 * verification on them through the callback handler
412 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100413 if (io_u_queued_complete(td, min_events) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100414 break;
415 }
416
Jens Axboec01c0392007-02-26 12:43:42 +0100417 if (!td->error) {
418 min_events = td->cur_depth;
419
420 if (min_events)
421 ret = io_u_queued_complete(td, min_events);
422 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100423 cleanup_pending_aio(td);
424
425 td_set_runstate(td, TD_RUNNING);
426}
427
Jens Axboe32cd46a2006-06-07 13:40:40 +0200428/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200429 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200430 * and reaps them, checking for rate and errors along the way.
431 */
Jens Axboeebac4652005-12-08 15:25:21 +0100432static void do_io(struct thread_data *td)
433{
Jens Axboe02bcaa82006-11-24 10:42:00 +0100434 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100435 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100436 unsigned int i;
437 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100438
Jens Axboe5853e5a2006-06-08 14:41:05 +0200439 td_set_runstate(td, TD_RUNNING);
440
Jens Axboe7bb48f82007-03-27 15:30:28 +0200441 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100442 struct timeval comp_time;
443 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200444 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100445 struct io_u *io_u;
Jens Axboe54517922007-03-05 10:06:06 +0100446 int ret2;
Jens Axboeebac4652005-12-08 15:25:21 +0100447
448 if (td->terminate)
449 break;
450
Jens Axboe3d7c3912007-02-19 13:16:12 +0100451 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100452 if (!io_u)
453 break;
454
455 memcpy(&s, &io_u->start_time, sizeof(s));
Jens Axboe97601022007-02-18 12:47:29 +0100456
457 if (runtime_exceeded(td, &s)) {
458 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200459 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100460 break;
461 }
Jens Axboe36167d82007-02-18 05:41:31 +0100462
Jens Axboeb67740d2007-07-26 12:22:30 +0200463 /*
464 * Add verification end_io handler, if asked to verify
465 * a previously written file.
466 */
Jens Axboed8fab1b2008-03-06 10:25:17 +0100467 if (td->o.verify != VERIFY_NONE) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200468 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100469 td_set_runstate(td, TD_VERIFYING);
470 } else
471 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200472
Jens Axboe11786802007-03-05 18:33:22 +0100473 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100474 switch (ret) {
475 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100476 if (io_u->error)
477 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100478 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100479 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200480 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100481
Jens Axboe9e9d1642007-03-12 09:37:46 +0100482 /*
483 * zero read, fail
484 */
485 if (!bytes) {
486 td_verror(td, ENODATA, "full resid");
487 put_io_u(td, io_u);
488 break;
489 }
490
Jens Axboe36167d82007-02-18 05:41:31 +0100491 io_u->xfer_buflen = io_u->resid;
492 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200493 io_u->offset += bytes;
494
Jens Axboe30061b92007-04-17 13:31:34 +0200495 td->ts.short_io_u[io_u->ddir]++;
496
Jens Axboed460eb32007-04-16 21:39:33 +0200497 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200498 goto sync_done;
499
Jens Axboe11786802007-03-05 18:33:22 +0100500 requeue_io_u(td, &io_u);
501 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200502sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100503 fio_gettime(&comp_time, NULL);
504 bytes_done = io_u_sync_complete(td, io_u);
505 if (bytes_done < 0)
506 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100507 }
Jens Axboe36167d82007-02-18 05:41:31 +0100508 break;
509 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100510 /*
511 * if the engine doesn't have a commit hook,
512 * the io_u is really queued. if it does have such
513 * a hook, it has to call io_u_queued() itself.
514 */
515 if (td->io_ops->commit == NULL)
516 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100517 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100518 case FIO_Q_BUSY:
519 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100520 ret2 = td_io_commit(td);
521 if (ret2 < 0)
522 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100523 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100524 default:
525 assert(ret < 0);
526 put_io_u(td, io_u);
527 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100528 }
529
Jens Axboe99784632007-02-19 10:06:17 +0100530 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100531 break;
532
Jens Axboe97601022007-02-18 12:47:29 +0100533 /*
534 * See if we need to complete some commands
535 */
Jens Axboe755200a2007-02-19 13:08:12 +0100536 if (ret == FIO_Q_QUEUED || ret == FIO_Q_BUSY) {
Jens Axboe97601022007-02-18 12:47:29 +0100537 min_evts = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100538 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe36167d82007-02-18 05:41:31 +0100539 min_evts = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100540
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100541 if (td->cur_depth > td->o.iodepth_low)
542 min_evts = td->cur_depth - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100543 }
544
Jens Axboe97601022007-02-18 12:47:29 +0100545 fio_gettime(&comp_time, NULL);
Jens Axboed7762cf2007-02-23 12:34:57 +0100546 bytes_done = io_u_queued_complete(td, min_evts);
Jens Axboe97601022007-02-18 12:47:29 +0100547 if (bytes_done < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100548 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100549 }
550
Jens Axboe97601022007-02-18 12:47:29 +0100551 if (!bytes_done)
552 continue;
553
Jens Axboeebac4652005-12-08 15:25:21 +0100554 /*
555 * the rate is batched for now, it should work for batches
556 * of completions except the very first one which may look
557 * a little bursty
558 */
Jens Axboe97601022007-02-18 12:47:29 +0100559 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100560
Jens Axboe413dd452007-02-23 09:26:09 +0100561 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100562
Jens Axboe97601022007-02-18 12:47:29 +0100563 if (check_min_rate(td, &comp_time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100564 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100565 terminate_threads(td->groupid);
Jens Axboee1161c32007-02-22 19:36:48 +0100566 td_verror(td, ENODATA, "check_min_rate");
Jens Axboeebac4652005-12-08 15:25:21 +0100567 break;
568 }
569
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100570 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100571 unsigned long long b;
572
573 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100574 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100575 int left;
576
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100577 if (td->o.thinktime_spin)
578 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100579
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100580 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100581 if (left)
582 usec_sleep(td, left);
583 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100584 }
Jens Axboeebac4652005-12-08 15:25:21 +0100585 }
586
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100587 if (td->o.fill_device && td->error == ENOSPC) {
588 td->error = 0;
589 td->terminate = 1;
590 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100591 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100592 struct fio_file *f;
593
Jens Axboec01c0392007-02-26 12:43:42 +0100594 i = td->cur_depth;
595 if (i)
596 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100597
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100598 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200599 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100600
601 for_each_file(td, f, i) {
602 if (!(f->flags & FIO_FILE_OPEN))
603 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200604 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100605 }
Jens Axboe84585002006-10-19 20:26:22 +0200606 }
Jens Axboec01c0392007-02-26 12:43:42 +0100607 } else
608 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100609
610 /*
611 * stop job if we failed doing any IO
612 */
613 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
614 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100615}
616
Jens Axboeebac4652005-12-08 15:25:21 +0100617static void cleanup_io_u(struct thread_data *td)
618{
619 struct list_head *entry, *n;
620 struct io_u *io_u;
621
622 list_for_each_safe(entry, n, &td->io_u_freelist) {
623 io_u = list_entry(entry, struct io_u, list);
624
625 list_del(&io_u->list);
626 free(io_u);
627 }
628
Jens Axboe2f9ade32006-10-20 11:25:52 +0200629 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100630}
631
Jens Axboe6b9cea22006-10-30 17:00:24 +0100632/*
633 * "randomly" fill the buffer contents
634 */
Jens Axboee9459e52007-04-17 15:46:32 +0200635static void fill_io_buf(struct thread_data *td, struct io_u *io_u, int max_bs)
Jens Axboe6b9cea22006-10-30 17:00:24 +0100636{
Jens Axboe210f43b2007-04-17 19:52:18 +0200637 long *ptr = io_u->buf;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100638
Jens Axboee9459e52007-04-17 15:46:32 +0200639 if (!td->o.zero_buffers) {
640 while ((void *) ptr - io_u->buf < max_bs) {
Jens Axboe210f43b2007-04-17 19:52:18 +0200641 *ptr = rand() * GOLDEN_RATIO_PRIME;
Jens Axboee9459e52007-04-17 15:46:32 +0200642 ptr++;
643 }
644 } else
645 memset(ptr, 0, max_bs);
Jens Axboe6b9cea22006-10-30 17:00:24 +0100646}
647
Jens Axboeebac4652005-12-08 15:25:21 +0100648static int init_io_u(struct thread_data *td)
649{
650 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100651 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100652 int i, max_units;
653 char *p;
654
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100655 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100656 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboec3990642007-07-19 10:17:09 +0200657 td->orig_buffer_size = (unsigned long long) max_bs * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100658
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100659 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE)
660 td->orig_buffer_size = (td->orig_buffer_size + td->o.hugepage_size - 1) & ~(td->o.hugepage_size - 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100661
Jens Axboec3990642007-07-19 10:17:09 +0200662 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
663 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
664 return 1;
665 }
666
Jens Axboe2f9ade32006-10-20 11:25:52 +0200667 if (allocate_io_mem(td))
668 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100669
Jens Axboeb3f378e2007-07-20 12:39:22 +0200670 if (td->o.odirect)
671 p = ALIGN(td->orig_buffer);
672 else
673 p = td->orig_buffer;
674
Jens Axboeebac4652005-12-08 15:25:21 +0100675 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200676 if (td->terminate)
677 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100678 io_u = malloc(sizeof(*io_u));
679 memset(io_u, 0, sizeof(*io_u));
680 INIT_LIST_HEAD(&io_u->list);
681
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200682 if (!(td->io_ops->flags & FIO_NOIO)) {
683 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200684
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200685 if (td_write(td))
686 fill_io_buf(td, io_u, max_bs);
687 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100688
Jens Axboeb1ff3402006-02-17 11:35:58 +0100689 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100690 io_u->flags = IO_U_F_FREE;
Jens Axboeebac4652005-12-08 15:25:21 +0100691 list_add(&io_u->list, &td->io_u_freelist);
692 }
693
Jens Axboe433afcb2007-02-22 10:39:01 +0100694 io_u_init_timeout();
695
Jens Axboeebac4652005-12-08 15:25:21 +0100696 return 0;
697}
698
Jens Axboeda867742006-06-06 10:39:10 -0700699static int switch_ioscheduler(struct thread_data *td)
700{
701 char tmp[256], tmp2[128];
702 FILE *f;
703 int ret;
704
Jens Axboeba0fbe12007-03-09 14:34:23 +0100705 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200706 return 0;
707
Jens Axboeda867742006-06-06 10:39:10 -0700708 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
709
710 f = fopen(tmp, "r+");
711 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200712 if (errno == ENOENT) {
713 log_err("fio: os or kernel doesn't support IO scheduler switching\n");
714 return 0;
715 }
716 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700717 return 1;
718 }
719
720 /*
721 * Set io scheduler.
722 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100723 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700724 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100725 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700726 fclose(f);
727 return 1;
728 }
729
730 rewind(f);
731
732 /*
733 * Read back and check that the selected scheduler is now the default.
734 */
735 ret = fread(tmp, 1, sizeof(tmp), f);
736 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100737 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700738 fclose(f);
739 return 1;
740 }
741
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100742 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700743 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100744 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100745 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700746 fclose(f);
747 return 1;
748 }
749
750 fclose(f);
751 return 0;
752}
753
Jens Axboe492158c2007-05-24 10:32:47 +0200754static int keep_running(struct thread_data *td)
755{
756 unsigned long long io_done;
757
Jens Axboe20e354e2007-07-23 14:36:16 +0200758 if (td->done)
759 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200760 if (td->o.time_based)
761 return 1;
762 if (td->o.loops) {
763 td->o.loops--;
764 return 1;
765 }
766
Jens Axboe48f5abd2007-07-20 13:25:04 +0200767 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE] + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200768 if (io_done < td->o.size)
769 return 1;
770
771 return 0;
772}
773
Jens Axboea978ba62007-03-08 14:29:03 +0100774static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100775{
Jens Axboe53cdc682006-10-18 11:50:58 +0200776 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100777 unsigned int i;
778 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100779
Jens Axboe756867b2007-03-06 15:19:24 +0100780 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100781 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100782 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100783 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100784 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100785 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100786
Jens Axboec1324df2007-02-19 19:06:41 +0100787 td->last_was_sync = 0;
788
Jens Axboe2ba1c292008-02-01 13:16:38 +0100789 /*
790 * reset file done count if we are to start over
791 */
792 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200793 td->nr_done_files = 0;
Jens Axboe281f9b62007-05-23 09:34:42 +0200794
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100795 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100796
797 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200798 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200799 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100800 ret = td_io_open_file(td, f);
801 if (ret)
802 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200803 }
Jens Axboea978ba62007-03-08 14:29:03 +0100804
805 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100806}
807
Jens Axboe906c8d72006-06-13 09:37:56 +0200808/*
809 * Entry point for the thread based jobs. The process based jobs end up
810 * here as well, after a little setup.
811 */
Jens Axboeebac4652005-12-08 15:25:21 +0100812static void *thread_main(void *data)
813{
Shawn Lewis10927312007-11-21 09:38:13 +0100814 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100815 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100816 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100817
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100818 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100819 setsid();
820
821 td->pid = getpid();
822
Jens Axboeee56ad52008-02-01 10:30:20 +0100823 dprint(FD_PROCESS, "jobs pid=%d started\n", td->pid);
824
Jens Axboeaea47d42006-05-26 19:27:29 +0200825 INIT_LIST_HEAD(&td->io_u_freelist);
826 INIT_LIST_HEAD(&td->io_u_busylist);
Jens Axboe755200a2007-02-19 13:08:12 +0100827 INIT_LIST_HEAD(&td->io_u_requeues);
Jens Axboeaea47d42006-05-26 19:27:29 +0200828 INIT_LIST_HEAD(&td->io_log_list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200829 INIT_LIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200830 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200831
Jens Axboe75154842006-06-01 13:56:09 +0200832 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100833 fio_mutex_up(startup_mutex);
834 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100835
Jens Axboe37f56872007-03-09 12:42:35 +0100836 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100837 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100838 * eating a file descriptor
839 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100840 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100841
Jens Axboed84f8d42007-05-16 08:47:46 +0200842 /*
843 * May alter parameters that init_io_u() will use, so we need to
844 * do this first.
845 */
846 if (init_iolog(td))
847 goto err;
848
Jens Axboedb1cd902007-04-26 13:47:07 +0200849 if (init_io_u(td))
850 goto err;
851
Jens Axboe375b2692007-05-22 09:13:02 +0200852 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200853 td_verror(td, errno, "cpu_set_affinity");
854 goto err;
855 }
856
Jens Axboeac684782007-11-08 08:29:07 +0100857 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200858 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
859 td_verror(td, errno, "ioprio_set");
860 goto err;
861 }
862 }
863
864 if (nice(td->o.nice) == -1) {
865 td_verror(td, errno, "nice");
866 goto err;
867 }
868
869 if (td->o.ioscheduler && switch_ioscheduler(td))
870 goto err;
871
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100872 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100873 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100874
875 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100876 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100877
Jens Axboeb5af8292007-03-08 12:43:13 +0100878 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100879 goto err;
880
Jens Axboe68727072007-03-15 20:49:25 +0100881 if (init_random_map(td))
882 goto err;
883
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100884 if (td->o.exec_prerun) {
885 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100886 goto err;
887 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200888
Jens Axboe69008992006-11-24 13:12:56 +0100889 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100890 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100891 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100892
893 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100894 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200895 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100896 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100897 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100898
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100899 if (td->o.ratemin)
Jens Axboe756867b2007-03-06 15:19:24 +0100900 memcpy(&td->lastrate, &td->ts.stat_sample_time, sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100901
Jens Axboea978ba62007-03-08 14:29:03 +0100902 if (clear_state && clear_io_state(td))
903 break;
904
Jens Axboeebac4652005-12-08 15:25:21 +0100905 prune_io_piece_log(td);
906
Jens Axboeba0fbe12007-03-09 14:34:23 +0100907 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100908
Jens Axboea978ba62007-03-08 14:29:03 +0100909 clear_state = 1;
910
Jens Axboe38d77ca2007-03-21 14:20:20 +0100911 if (td_read(td) && td->io_bytes[DDIR_READ]) {
912 if (td->rw_end_set[DDIR_READ])
913 elapsed = utime_since(&td->start, &td->rw_end[DDIR_READ]);
914 else
915 elapsed = utime_since_now(&td->start);
916
917 runtime[DDIR_READ] += elapsed;
918 }
919 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
920 if (td->rw_end_set[DDIR_WRITE])
921 elapsed = utime_since(&td->start, &td->rw_end[DDIR_WRITE]);
922 else
923 elapsed = utime_since_now(&td->start);
924
925 runtime[DDIR_WRITE] += elapsed;
926 }
Jens Axboe413dd452007-02-23 09:26:09 +0100927
Jens Axboeebac4652005-12-08 15:25:21 +0100928 if (td->error || td->terminate)
929 break;
930
Shawn Lewise84c73a2007-08-02 22:19:32 +0200931 if (!td->o.do_verify ||
932 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +0200933 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +0100934 continue;
935
Jens Axboea978ba62007-03-08 14:29:03 +0100936 if (clear_io_state(td))
937 break;
938
Jens Axboe02bcaa82006-11-24 10:42:00 +0100939 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100940
941 do_verify(td);
942
Jens Axboe69008992006-11-24 13:12:56 +0100943 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100944
945 if (td->error || td->terminate)
946 break;
947 }
948
Jens Axboe36dff962006-11-24 13:29:20 +0100949 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +0200950 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
951 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +0100952 td->ts.total_run_time = mtime_since_now(&td->epoch);
953 td->ts.io_bytes[0] = td->io_bytes[0];
954 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +0100955
Jens Axboe756867b2007-03-06 15:19:24 +0100956 if (td->ts.bw_log)
957 finish_log(td, td->ts.bw_log, "bw");
958 if (td->ts.slat_log)
959 finish_log(td, td->ts.slat_log, "slat");
960 if (td->ts.clat_log)
961 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100962 if (td->o.exec_postrun) {
963 if (system(td->o.exec_postrun) < 0)
964 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100965 }
Jens Axboeebac4652005-12-08 15:25:21 +0100966
967 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100968 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100969
970err:
Jens Axboe5bf13a52007-02-17 01:51:47 +0100971 if (td->error)
972 printf("fio: pid=%d, err=%d/%s\n", td->pid, td->error, td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100973 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200974 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100975 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +0200976
977 /*
978 * do this very late, it will log file closing as well
979 */
980 if (td->o.write_iolog_file)
981 write_iolog_close(td);
982
Jens Axboed23bb322007-03-23 15:57:56 +0100983 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100984 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +0100985 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +0100986}
987
Jens Axboe906c8d72006-06-13 09:37:56 +0200988/*
989 * We cannot pass the td data into a forked process, so attach the td and
990 * pass it to the thread worker.
991 */
Jens Axboea6418142007-02-17 04:47:18 +0100992static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +0100993{
994 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +0100995 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100996
997 data = shmat(shmid, NULL, 0);
998 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +0100999 int __err = errno;
1000
Jens Axboeebac4652005-12-08 15:25:21 +01001001 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001002 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001003 }
1004
1005 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001006 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001007 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001008 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001009}
1010
Jens Axboe906c8d72006-06-13 09:37:56 +02001011/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001012 * Run over the job map and reap the threads that have exited, if any.
1013 */
Jens Axboeebac4652005-12-08 15:25:21 +01001014static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1015{
Jens Axboe34572e22006-10-20 09:33:18 +02001016 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001017 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001018
1019 /*
1020 * reap exited threads (TD_EXITED -> TD_REAPED)
1021 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001022 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001023 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001024 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001025
Jens Axboe84585002006-10-19 20:26:22 +02001026 /*
1027 * ->io_ops is NULL for a thread that has closed its
1028 * io engine
1029 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001030 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001031 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001032 else
1033 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001034
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001035 if (!td->pid) {
1036 pending++;
1037 continue;
1038 }
1039 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001040 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001041 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001042 if (td->runstate == TD_EXITED) {
1043 td_set_runstate(td, TD_REAPED);
1044 goto reaped;
1045 }
1046 continue;
1047 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001048
1049 flags = WNOHANG;
1050 if (td->runstate == TD_EXITED)
1051 flags = 0;
1052
1053 /*
1054 * check if someone quit or got killed in an unusual way
1055 */
1056 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001057 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001058 if (errno == ECHILD) {
1059 log_err("fio: pid=%d disappeared %d\n", td->pid, td->runstate);
1060 td_set_runstate(td, TD_REAPED);
1061 goto reaped;
1062 }
1063 perror("waitpid");
1064 } else if (ret == td->pid) {
1065 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001066 int sig = WTERMSIG(status);
1067
Jens Axboed9244942007-03-05 12:32:32 +01001068 if (sig != SIGQUIT)
1069 log_err("fio: pid=%d, got signal=%d\n", td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001070 td_set_runstate(td, TD_REAPED);
1071 goto reaped;
1072 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001073 if (WIFEXITED(status)) {
1074 if (WEXITSTATUS(status) && !td->error)
1075 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001076
Jens Axboea2f77c92007-02-22 11:53:00 +01001077 td_set_runstate(td, TD_REAPED);
1078 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001079 }
1080 }
Jens Axboeebac4652005-12-08 15:25:21 +01001081
Jens Axboea2f77c92007-02-22 11:53:00 +01001082 /*
1083 * thread is not dead, continue
1084 */
gurudas paie48676b2007-04-11 12:44:27 +02001085 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001086 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001087reaped:
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001088 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001089 long ret;
1090
Jens Axboeee56ad52008-02-01 10:30:20 +01001091 dprint(FD_PROCESS, "joining tread %d\n", td->pid);
1092 if (pthread_join(td->thread, (void *) &ret)) {
1093 dprint(FD_PROCESS, "join failed %ld\n", ret);
Jens Axboe3707f452007-02-22 12:26:57 +01001094 perror("pthread_join");
Jens Axboeee56ad52008-02-01 10:30:20 +01001095 }
Jens Axboe3707f452007-02-22 12:26:57 +01001096 }
1097
Jens Axboeebac4652005-12-08 15:25:21 +01001098 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001099 (*m_rate) -= td->o.ratemin;
1100 (*t_rate) -= td->o.rate;
gurudas paie48676b2007-04-11 12:44:27 +02001101 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001102
1103 if (td->error)
1104 exit_value++;
Jens Axboeebac4652005-12-08 15:25:21 +01001105 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001106
Jens Axboe1f809d12007-10-25 18:34:02 +02001107 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001108 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001109}
1110
Jens Axboe906c8d72006-06-13 09:37:56 +02001111/*
1112 * Main function for kicking off and reaping jobs, as needed.
1113 */
Jens Axboeebac4652005-12-08 15:25:21 +01001114static void run_threads(void)
1115{
Jens Axboeebac4652005-12-08 15:25:21 +01001116 struct thread_data *td;
1117 unsigned long spent;
1118 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001119
Jens Axboe2f9ade32006-10-20 11:25:52 +02001120 if (fio_pin_memory())
1121 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001122
Jens Axboec6ae0a52006-06-12 11:04:44 +02001123 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001124 printf("Starting ");
1125 if (nr_thread)
1126 printf("%d thread%s", nr_thread, nr_thread > 1 ? "s" : "");
1127 if (nr_process) {
1128 if (nr_thread)
1129 printf(" and ");
1130 printf("%d process%s", nr_process, nr_process > 1 ? "es" : "");
1131 }
1132 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001133 fflush(stdout);
1134 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001135
Jens Axboe4efa9702006-05-31 11:56:49 +02001136 signal(SIGINT, sig_handler);
1137 signal(SIGALRM, sig_handler);
1138
Jens Axboeebac4652005-12-08 15:25:21 +01001139 todo = thread_number;
1140 nr_running = 0;
1141 nr_started = 0;
1142 m_rate = t_rate = 0;
1143
Jens Axboe34572e22006-10-20 09:33:18 +02001144 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001145 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001146
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001147 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001148 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001149 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001150 }
Jens Axboeebac4652005-12-08 15:25:21 +01001151
1152 /*
1153 * do file setup here so it happens sequentially,
1154 * we don't want X number of threads getting their
1155 * client data interspersed on disk
1156 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001157 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001158 exit_value++;
1159 if (td->error)
1160 log_err("fio: pid=%d, err=%d/%s\n", td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001161 td_set_runstate(td, TD_REAPED);
1162 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001163 } else {
1164 struct fio_file *f;
1165 unsigned int i;
1166
1167 /*
1168 * for sharing to work, each job must always open
1169 * its own files. so close them, if we opened them
1170 * for creation
1171 */
1172 for_each_file(td, f, i)
1173 td_io_close_file(td, f);
1174 }
Jens Axboe380cf262007-01-11 14:01:27 +01001175
1176 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001177 }
1178
Jens Axboea2f77c92007-02-22 11:53:00 +01001179 set_genesis_time();
1180
Jens Axboeebac4652005-12-08 15:25:21 +01001181 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001182 struct thread_data *map[MAX_JOBS];
1183 struct timeval this_start;
1184 int this_jobs = 0, left;
1185
Jens Axboeebac4652005-12-08 15:25:21 +01001186 /*
1187 * create threads (TD_NOT_CREATED -> TD_CREATED)
1188 */
Jens Axboe34572e22006-10-20 09:33:18 +02001189 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001190 if (td->runstate != TD_NOT_CREATED)
1191 continue;
1192
1193 /*
1194 * never got a chance to start, killed by other
1195 * thread for some reason
1196 */
1197 if (td->terminate) {
1198 todo--;
1199 continue;
1200 }
1201
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001202 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001203 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001204
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001205 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001206 continue;
1207 }
1208
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001209 if (td->o.stonewall && (nr_started || nr_running))
Jens Axboeebac4652005-12-08 15:25:21 +01001210 break;
1211
Jens Axboe75154842006-06-01 13:56:09 +02001212 /*
1213 * Set state to created. Thread will transition
1214 * to TD_INITIALIZED when it's done setting up.
1215 */
Jens Axboeebac4652005-12-08 15:25:21 +01001216 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001217 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001218 nr_started++;
1219
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001220 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001221 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001222 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1223 perror("thread_create");
1224 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001225 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001226 }
1227 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001228 dprint(FD_PROCESS, "will fork\n");
Jens Axboe07739b52007-03-08 20:25:46 +01001229 if (!fork()) {
Jens Axboea6418142007-02-17 04:47:18 +01001230 int ret = fork_main(shm_id, i);
1231
1232 exit(ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001233 }
1234 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001235 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001236 }
1237
1238 /*
Jens Axboe75154842006-06-01 13:56:09 +02001239 * Wait for the started threads to transition to
1240 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001241 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001242 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001243 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001244 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001245 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1246 break;
1247
1248 usleep(100000);
1249
1250 for (i = 0; i < this_jobs; i++) {
1251 td = map[i];
1252 if (!td)
1253 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001254 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001255 map[i] = NULL;
1256 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001257 } else if (td->runstate >= TD_EXITED) {
1258 map[i] = NULL;
1259 left--;
1260 todo--;
1261 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001262 }
1263 }
1264 }
1265
1266 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001267 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001268 for (i = 0; i < this_jobs; i++) {
1269 td = map[i];
1270 if (!td)
1271 continue;
1272 kill(td->pid, SIGTERM);
1273 }
1274 break;
1275 }
1276
1277 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001278 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001279 */
Jens Axboe34572e22006-10-20 09:33:18 +02001280 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001281 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001282 continue;
1283
1284 td_set_runstate(td, TD_RUNNING);
1285 nr_running++;
1286 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001287 m_rate += td->o.ratemin;
1288 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001289 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001290 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001291 }
1292
1293 reap_threads(&nr_running, &t_rate, &m_rate);
1294
1295 if (todo)
1296 usleep(100000);
1297 }
1298
1299 while (nr_running) {
1300 reap_threads(&nr_running, &t_rate, &m_rate);
1301 usleep(10000);
1302 }
1303
1304 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001305 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001306}
1307
Jens Axboeebac4652005-12-08 15:25:21 +01001308int main(int argc, char *argv[])
1309{
Jens Axboe29d610e2007-02-06 13:25:33 +01001310 long ps;
1311
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001312 sinit();
1313
Jens Axboedbe11252007-02-11 00:19:51 +01001314 /*
1315 * We need locale for number printing, if it isn't set then just
1316 * go with the US format.
1317 */
1318 if (!getenv("LC_NUMERIC"))
1319 setlocale(LC_NUMERIC, "en_US");
1320
Jens Axboeebac4652005-12-08 15:25:21 +01001321 if (parse_options(argc, argv))
1322 return 1;
1323
Jens Axboe4b472fa2007-04-04 11:04:34 +02001324 if (!thread_number)
1325 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001326
Jens Axboe29d610e2007-02-06 13:25:33 +01001327 ps = sysconf(_SC_PAGESIZE);
1328 if (ps < 0) {
1329 log_err("Failed to get page size\n");
1330 return 1;
1331 }
1332
Jens Axboecfc99db2007-03-14 10:34:47 +01001333 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001334 page_mask = ps - 1;
1335
Jens Axboebb3884d2007-01-17 17:23:11 +11001336 if (write_bw_log) {
1337 setup_log(&agg_io_log[DDIR_READ]);
1338 setup_log(&agg_io_log[DDIR_WRITE]);
1339 }
1340
Jens Axboecdd18ad2008-02-27 18:58:00 +01001341 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001342
Jens Axboea2f77c92007-02-22 11:53:00 +01001343 set_genesis_time();
1344
Jens Axboeebac4652005-12-08 15:25:21 +01001345 disk_util_timer_arm();
1346
1347 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001348
Jens Axboebb3884d2007-01-17 17:23:11 +11001349 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001350 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001351 if (write_bw_log) {
1352 __finish_log(agg_io_log[DDIR_READ],"agg-read_bw.log");
1353 __finish_log(agg_io_log[DDIR_WRITE],"agg-write_bw.log");
1354 }
1355 }
Jens Axboeebac4652005-12-08 15:25:21 +01001356
Jens Axboecdd18ad2008-02-27 18:58:00 +01001357 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001358 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001359}