blob: c5926289b34475bd511e8b8b5fadb0ce44b50c06 [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
Jens Axboeaae22ca2006-09-05 10:46:22 +02005 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboeebac4652005-12-08 15:25:21 +01006 *
Jens Axboe8e9fe632006-10-24 15:11:09 +02007 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
9 *
Jens Axboeebac4652005-12-08 15:25:21 +010010 * This program is free software; you can redistribute it and/or modify
Jens Axboe8e9fe632006-10-24 15:11:09 +020011 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Jens Axboeebac4652005-12-08 15:25:21 +010013 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
Jens Axboeebac4652005-12-08 15:25:21 +010024#include <unistd.h>
25#include <fcntl.h>
26#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <signal.h>
28#include <time.h>
Jens Axboedbe11252007-02-11 00:19:51 +010029#include <locale.h>
Jens Axboe36167d82007-02-18 05:41:31 +010030#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010031#include <sys/stat.h>
32#include <sys/wait.h>
33#include <sys/ipc.h>
34#include <sys/shm.h>
Jens Axboeebac4652005-12-08 15:25:21 +010035#include <sys/mman.h>
36
37#include "fio.h"
Jens Axboe210f43b2007-04-17 19:52:18 +020038#include "hash.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010039#include "smalloc.h"
Jens Axboeebac4652005-12-08 15:25:21 +010040
Jens Axboecfc99db2007-03-14 10:34:47 +010041unsigned long page_mask;
42unsigned long page_size;
Jens Axboe29d610e2007-02-06 13:25:33 +010043#define ALIGN(buf) \
44 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010045
46int groupid = 0;
47int thread_number = 0;
Jens Axboe9cedf162007-03-12 11:29:30 +010048int nr_process = 0;
49int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010050int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020051int temp_stall_ts;
Jens Axboed3eeeab2008-04-06 12:19:05 +020052unsigned long done_secs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010053
Jens Axboecdd18ad2008-02-27 18:58:00 +010054static struct fio_mutex *startup_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010055static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010056static int exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +010057
Jens Axboebb3884d2007-01-17 17:23:11 +110058struct io_log *agg_io_log[2];
59
Jens Axboeebac4652005-12-08 15:25:21 +010060#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020061#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010062
Jens Axboe6ce15a32007-01-12 09:04:41 +010063static inline void td_set_runstate(struct thread_data *td, int runstate)
64{
Jens Axboed8fab1b2008-03-06 10:25:17 +010065 if (td->runstate == runstate)
66 return;
67
Jens Axboe5921e802008-05-30 15:02:38 +020068 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
69 td->runstate, runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010070 td->runstate = runstate;
71}
72
Jens Axboe390c40e2007-03-05 12:35:29 +010073static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010074{
Jens Axboe34572e22006-10-20 09:33:18 +020075 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010076 int i;
77
Jens Axboe6f88bcb2008-03-19 10:29:07 +010078 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
79
Jens Axboe34572e22006-10-20 09:33:18 +020080 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010081 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +010082 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +020083 td->o.name, (int) td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010084 td->terminate = 1;
85 td->o.start_delay = 0;
86
Jens Axboe7a93ab12007-04-18 12:25:41 +020087 /*
88 * if the thread is running, just let it exit
89 */
90 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010091 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010092 else {
93 struct ioengine_ops *ops = td->io_ops;
94
95 if (ops && (ops->flags & FIO_SIGQUIT))
96 kill(td->pid, SIGQUIT);
97 }
Jens Axboeebac4652005-12-08 15:25:21 +010098 }
99 }
100}
101
102static void sig_handler(int sig)
103{
Jens Axboe6c28f0b2008-05-26 16:38:28 +0200104 if (!threads)
105 return;
106
Jens Axboeebac4652005-12-08 15:25:21 +0100107 switch (sig) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100108 case SIGALRM:
109 update_io_ticks();
110 disk_util_timer_arm();
111 print_thread_status();
112 break;
113 default:
114 printf("\nfio: terminating on signal %d\n", sig);
115 fflush(stdout);
116 terminate_threads(TERMINATE_ALL);
117 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100118 }
119}
120
Jens Axboe906c8d72006-06-13 09:37:56 +0200121/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200122 * Check if we are above the minimum rate given.
123 */
Jens Axboeebac4652005-12-08 15:25:21 +0100124static int check_min_rate(struct thread_data *td, struct timeval *now)
125{
Jens Axboe09042002007-02-23 10:56:22 +0100126 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100127 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100128 unsigned long spent;
129 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100130
131 /*
Jens Axboe780bf1a2007-02-28 11:13:49 +0100132 * No minimum rate set, always ok
133 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100134 if (!td->o.ratemin && !td->o.rate_iops_min)
Jens Axboe780bf1a2007-02-28 11:13:49 +0100135 return 0;
136
137 /*
Jens Axboeebac4652005-12-08 15:25:21 +0100138 * allow a 2 second settle period in the beginning
139 */
140 if (mtime_since(&td->start, now) < 2000)
141 return 0;
142
Jens Axboe4e991c22007-03-15 11:41:11 +0100143 if (td_read(td)) {
144 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100145 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100146 }
147 if (td_write(td)) {
148 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100149 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100150 }
Jens Axboe09042002007-02-23 10:56:22 +0100151
Jens Axboeebac4652005-12-08 15:25:21 +0100152 /*
153 * if rate blocks is set, sample is running
154 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100155 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100156 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100157 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100158 return 0;
159
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100160 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100161 /*
162 * check bandwidth specified rate
163 */
164 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100165 log_err("%s: min rate %u not met\n", td->o.name,
166 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100167 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100168 } else {
169 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100170 if (rate < td->o.ratemin ||
171 bytes < td->rate_bytes) {
172 log_err("%s: min rate %u not met, got"
173 " %luKiB/sec\n", td->o.name,
174 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100175 return 1;
176 }
177 }
178 } else {
179 /*
180 * checks iops specified rate
181 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100182 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100183 log_err("%s: min iops rate %u not met\n",
184 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100185 return 1;
186 } else {
187 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100188 if (rate < td->o.rate_iops_min ||
189 iops < td->rate_blocks) {
190 log_err("%s: min iops rate %u not met,"
191 " got %lu\n", td->o.name,
192 td->o.rate_iops_min,
193 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100194 }
Jens Axboe413dd452007-02-23 09:26:09 +0100195 }
Jens Axboeebac4652005-12-08 15:25:21 +0100196 }
197 }
198
Jens Axboe09042002007-02-23 10:56:22 +0100199 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100200 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100201 memcpy(&td->lastrate, now, sizeof(*now));
202 return 0;
203}
204
205static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
206{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100207 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100208 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100209 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100210 return 1;
211
212 return 0;
213}
214
Jens Axboe906c8d72006-06-13 09:37:56 +0200215/*
216 * When job exits, we can cancel the in-flight IO if we are using async
217 * io. Attempt to do so.
218 */
Jens Axboeebac4652005-12-08 15:25:21 +0100219static void cleanup_pending_aio(struct thread_data *td)
220{
Jens Axboeebac4652005-12-08 15:25:21 +0100221 struct list_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100222 struct io_u *io_u;
223 int r;
224
225 /*
226 * get immediately available events, if any
227 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100228 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100229 if (r < 0)
230 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100231
232 /*
233 * now cancel remaining active events
234 */
Jens Axboe2866c822006-10-09 15:57:48 +0200235 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100236 list_for_each_safe(entry, n, &td->io_u_busylist) {
237 io_u = list_entry(entry, struct io_u, list);
238
Jens Axboe0c6e7512007-02-22 11:19:39 +0100239 /*
240 * if the io_u isn't in flight, then that generally
241 * means someone leaked an io_u. complain but fix
242 * it up, so we don't stall here.
243 */
244 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
245 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100246 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100247 } else {
248 r = td->io_ops->cancel(td, io_u);
249 if (!r)
250 put_io_u(td, io_u);
251 }
Jens Axboeebac4652005-12-08 15:25:21 +0100252 }
253 }
254
Jens Axboe97601022007-02-18 12:47:29 +0100255 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100256 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100257}
258
Jens Axboe906c8d72006-06-13 09:37:56 +0200259/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200260 * Helper to handle the final sync of a file. Works just like the normal
261 * io path, just does everything sync.
262 */
263static int fio_io_sync(struct thread_data *td, struct fio_file *f)
264{
265 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200266 int ret;
267
268 if (!io_u)
269 return 1;
270
271 io_u->ddir = DDIR_SYNC;
272 io_u->file = f;
273
274 if (td_io_prep(td, io_u)) {
275 put_io_u(td, io_u);
276 return 1;
277 }
278
Jens Axboe755200a2007-02-19 13:08:12 +0100279requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200280 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100281 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100282 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200283 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200284 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100285 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100286 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100287 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100288 } else if (ret == FIO_Q_COMPLETED) {
289 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100290 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100291 return 1;
292 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200293
Jens Axboed7762cf2007-02-23 12:34:57 +0100294 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100295 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100296 } else if (ret == FIO_Q_BUSY) {
297 if (td_io_commit(td))
298 return 1;
299 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200300 }
301
302 return 0;
303}
304
305/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100306 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200307 * reads the blocks back in, and checks the crc/md5 of the data.
308 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100309static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100310{
Jens Axboe53cdc682006-10-18 11:50:58 +0200311 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100312 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100313 int ret, min_events;
314 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200315
316 /*
317 * sync io first and invalidate cache, to make sure we really
318 * read from disk.
319 */
320 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100321 if (!(f->flags & FIO_FILE_OPEN))
322 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100323 if (fio_io_sync(td, f))
324 break;
325 if (file_invalidate_cache(td, f))
326 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200327 }
Jens Axboeebac4652005-12-08 15:25:21 +0100328
Jens Axboeb2fdda42007-02-20 20:52:51 +0100329 if (td->error)
330 return;
331
Jens Axboeebac4652005-12-08 15:25:21 +0100332 td_set_runstate(td, TD_VERIFYING);
333
Jens Axboe36167d82007-02-18 05:41:31 +0100334 io_u = NULL;
335 while (!td->terminate) {
Jens Axboe54517922007-03-05 10:06:06 +0100336 int ret2;
337
Jens Axboeebac4652005-12-08 15:25:21 +0100338 io_u = __get_io_u(td);
339 if (!io_u)
340 break;
341
Jens Axboe069c2912007-02-21 23:02:39 +0100342 if (runtime_exceeded(td, &io_u->start_time)) {
343 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200344 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200345 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100346 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200347
Jens Axboe069c2912007-02-21 23:02:39 +0100348 if (get_next_verify(td, io_u)) {
349 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100350 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100351 }
Jens Axboeebac4652005-12-08 15:25:21 +0100352
Jens Axboe069c2912007-02-21 23:02:39 +0100353 if (td_io_prep(td, io_u)) {
354 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100355 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100356 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100357
358 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100359
Jens Axboe11786802007-03-05 18:33:22 +0100360 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100361 switch (ret) {
362 case FIO_Q_COMPLETED:
363 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100364 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100365 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100366 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200367 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100368
Jens Axboe9e9d1642007-03-12 09:37:46 +0100369 /*
370 * zero read, fail
371 */
372 if (!bytes) {
373 td_verror(td, ENODATA, "full resid");
374 put_io_u(td, io_u);
375 break;
376 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200377
Jens Axboe36167d82007-02-18 05:41:31 +0100378 io_u->xfer_buflen = io_u->resid;
379 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200380 io_u->offset += bytes;
381
Jens Axboe30061b92007-04-17 13:31:34 +0200382 td->ts.short_io_u[io_u->ddir]++;
383
Jens Axboed460eb32007-04-16 21:39:33 +0200384 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200385 goto sync_done;
386
Jens Axboe11786802007-03-05 18:33:22 +0100387 requeue_io_u(td, &io_u);
388 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200389sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100390 ret = io_u_sync_complete(td, io_u);
391 if (ret < 0)
392 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100393 }
Jens Axboe36167d82007-02-18 05:41:31 +0100394 continue;
395 case FIO_Q_QUEUED:
396 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100397 case FIO_Q_BUSY:
398 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100399 ret2 = td_io_commit(td);
400 if (ret2 < 0)
401 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100402 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100403 default:
404 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100405 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100406 break;
407 }
408
Jens Axboe99784632007-02-19 10:06:17 +0100409 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100410 break;
411
Jens Axboe3af6ef32007-02-18 06:57:43 +0100412 /*
413 * if we can queue more, do so. but check if there are
414 * completed io_u's first.
415 */
Jens Axboe97601022007-02-18 12:47:29 +0100416 min_events = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100417 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe838bc702008-05-22 13:08:23 +0200418 if (td->cur_depth >= td->o.iodepth_low)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100419 min_events = td->cur_depth - td->o.iodepth_low;
Jens Axboe838bc702008-05-22 13:08:23 +0200420 if (!min_events)
421 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100422 }
423
Jens Axboe3af6ef32007-02-18 06:57:43 +0100424 /*
425 * Reap required number of io units, if any, and do the
426 * verification on them through the callback handler
427 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100428 if (io_u_queued_complete(td, min_events) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100429 break;
430 }
431
Jens Axboec01c0392007-02-26 12:43:42 +0100432 if (!td->error) {
433 min_events = td->cur_depth;
434
435 if (min_events)
436 ret = io_u_queued_complete(td, min_events);
437 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100438 cleanup_pending_aio(td);
439
440 td_set_runstate(td, TD_RUNNING);
441}
442
Jens Axboe32cd46a2006-06-07 13:40:40 +0200443/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200444 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200445 * and reaps them, checking for rate and errors along the way.
446 */
Jens Axboeebac4652005-12-08 15:25:21 +0100447static void do_io(struct thread_data *td)
448{
Jens Axboe02bcaa82006-11-24 10:42:00 +0100449 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100450 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100451 unsigned int i;
452 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100453
Jens Axboe5853e5a2006-06-08 14:41:05 +0200454 td_set_runstate(td, TD_RUNNING);
455
Jens Axboe7bb48f82007-03-27 15:30:28 +0200456 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100457 struct timeval comp_time;
458 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200459 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100460 struct io_u *io_u;
Jens Axboe54517922007-03-05 10:06:06 +0100461 int ret2;
Jens Axboeebac4652005-12-08 15:25:21 +0100462
463 if (td->terminate)
464 break;
465
Jens Axboe3d7c3912007-02-19 13:16:12 +0100466 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100467 if (!io_u)
468 break;
469
470 memcpy(&s, &io_u->start_time, sizeof(s));
Jens Axboe97601022007-02-18 12:47:29 +0100471
472 if (runtime_exceeded(td, &s)) {
473 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200474 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100475 break;
476 }
Jens Axboe36167d82007-02-18 05:41:31 +0100477
Jens Axboeb67740d2007-07-26 12:22:30 +0200478 /*
479 * Add verification end_io handler, if asked to verify
480 * a previously written file.
481 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100482 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200483 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100484 td_set_runstate(td, TD_VERIFYING);
485 } else
486 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200487
Jens Axboe11786802007-03-05 18:33:22 +0100488 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100489 switch (ret) {
490 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100491 if (io_u->error)
492 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100493 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100494 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200495 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100496
Jens Axboe9e9d1642007-03-12 09:37:46 +0100497 /*
498 * zero read, fail
499 */
500 if (!bytes) {
501 td_verror(td, ENODATA, "full resid");
502 put_io_u(td, io_u);
503 break;
504 }
505
Jens Axboe36167d82007-02-18 05:41:31 +0100506 io_u->xfer_buflen = io_u->resid;
507 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200508 io_u->offset += bytes;
509
Jens Axboe30061b92007-04-17 13:31:34 +0200510 td->ts.short_io_u[io_u->ddir]++;
511
Jens Axboed460eb32007-04-16 21:39:33 +0200512 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200513 goto sync_done;
514
Jens Axboe11786802007-03-05 18:33:22 +0100515 requeue_io_u(td, &io_u);
516 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200517sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100518 fio_gettime(&comp_time, NULL);
519 bytes_done = io_u_sync_complete(td, io_u);
520 if (bytes_done < 0)
521 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100522 }
Jens Axboe36167d82007-02-18 05:41:31 +0100523 break;
524 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100525 /*
526 * if the engine doesn't have a commit hook,
527 * the io_u is really queued. if it does have such
528 * a hook, it has to call io_u_queued() itself.
529 */
530 if (td->io_ops->commit == NULL)
531 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100532 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100533 case FIO_Q_BUSY:
534 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100535 ret2 = td_io_commit(td);
536 if (ret2 < 0)
537 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100538 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100539 default:
540 assert(ret < 0);
541 put_io_u(td, io_u);
542 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100543 }
544
Jens Axboe99784632007-02-19 10:06:17 +0100545 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100546 break;
547
Jens Axboe97601022007-02-18 12:47:29 +0100548 /*
549 * See if we need to complete some commands
550 */
Jens Axboe838bc702008-05-22 13:08:23 +0200551 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe97601022007-02-18 12:47:29 +0100552 min_evts = 0;
Jens Axboe838bc702008-05-22 13:08:23 +0200553 if (td->cur_depth >= td->o.iodepth_low)
554 min_evts = td->cur_depth - td->o.iodepth_low;
555 if (!min_evts)
Jens Axboe36167d82007-02-18 05:41:31 +0100556 min_evts = 1;
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
644static int init_io_u(struct thread_data *td)
645{
646 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100647 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100648 int i, max_units;
649 char *p;
650
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100651 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100652 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100653 td->orig_buffer_size = (unsigned long long) max_bs
654 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100655
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100656 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
657 unsigned long bs;
658
659 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
660 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
661 }
Jens Axboeebac4652005-12-08 15:25:21 +0100662
Jens Axboec3990642007-07-19 10:17:09 +0200663 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
664 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
665 return 1;
666 }
667
Jens Axboe2f9ade32006-10-20 11:25:52 +0200668 if (allocate_io_mem(td))
669 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100670
Jens Axboeb3f378e2007-07-20 12:39:22 +0200671 if (td->o.odirect)
672 p = ALIGN(td->orig_buffer);
673 else
674 p = td->orig_buffer;
675
Jens Axboeebac4652005-12-08 15:25:21 +0100676 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200677 if (td->terminate)
678 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100679 io_u = malloc(sizeof(*io_u));
680 memset(io_u, 0, sizeof(*io_u));
681 INIT_LIST_HEAD(&io_u->list);
682
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200683 if (!(td->io_ops->flags & FIO_NOIO)) {
684 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200685
Jens Axboe5973caf2008-05-21 19:52:35 +0200686 if (td_write(td) && !td->o.refill_buffers)
687 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200688 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100689
Jens Axboeb1ff3402006-02-17 11:35:58 +0100690 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100691 io_u->flags = IO_U_F_FREE;
Jens Axboeebac4652005-12-08 15:25:21 +0100692 list_add(&io_u->list, &td->io_u_freelist);
693 }
694
Jens Axboe433afcb2007-02-22 10:39:01 +0100695 io_u_init_timeout();
696
Jens Axboeebac4652005-12-08 15:25:21 +0100697 return 0;
698}
699
Jens Axboeda867742006-06-06 10:39:10 -0700700static int switch_ioscheduler(struct thread_data *td)
701{
702 char tmp[256], tmp2[128];
703 FILE *f;
704 int ret;
705
Jens Axboeba0fbe12007-03-09 14:34:23 +0100706 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200707 return 0;
708
Jens Axboeda867742006-06-06 10:39:10 -0700709 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
710
711 f = fopen(tmp, "r+");
712 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200713 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100714 log_err("fio: os or kernel doesn't support IO scheduler"
715 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200716 return 0;
717 }
718 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700719 return 1;
720 }
721
722 /*
723 * Set io scheduler.
724 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100725 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700726 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100727 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700728 fclose(f);
729 return 1;
730 }
731
732 rewind(f);
733
734 /*
735 * Read back and check that the selected scheduler is now the default.
736 */
737 ret = fread(tmp, 1, sizeof(tmp), f);
738 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100739 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700740 fclose(f);
741 return 1;
742 }
743
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100744 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700745 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100746 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100747 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700748 fclose(f);
749 return 1;
750 }
751
752 fclose(f);
753 return 0;
754}
755
Jens Axboe492158c2007-05-24 10:32:47 +0200756static int keep_running(struct thread_data *td)
757{
758 unsigned long long io_done;
759
Jens Axboe20e354e2007-07-23 14:36:16 +0200760 if (td->done)
761 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200762 if (td->o.time_based)
763 return 1;
764 if (td->o.loops) {
765 td->o.loops--;
766 return 1;
767 }
768
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100769 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
770 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200771 if (io_done < td->o.size)
772 return 1;
773
774 return 0;
775}
776
Jens Axboea978ba62007-03-08 14:29:03 +0100777static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100778{
Jens Axboe53cdc682006-10-18 11:50:58 +0200779 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100780 unsigned int i;
781 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100782
Jens Axboe756867b2007-03-06 15:19:24 +0100783 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100784 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100785 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100786 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100787 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100788 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100789
Jens Axboec1324df2007-02-19 19:06:41 +0100790 td->last_was_sync = 0;
791
Jens Axboe2ba1c292008-02-01 13:16:38 +0100792 /*
793 * reset file done count if we are to start over
794 */
795 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200796 td->nr_done_files = 0;
Jens Axboe281f9b62007-05-23 09:34:42 +0200797
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100798 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100799
800 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200801 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200802 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100803 ret = td_io_open_file(td, f);
804 if (ret)
805 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200806 }
Jens Axboea978ba62007-03-08 14:29:03 +0100807
808 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100809}
810
Jens Axboe906c8d72006-06-13 09:37:56 +0200811/*
812 * Entry point for the thread based jobs. The process based jobs end up
813 * here as well, after a little setup.
814 */
Jens Axboeebac4652005-12-08 15:25:21 +0100815static void *thread_main(void *data)
816{
Shawn Lewis10927312007-11-21 09:38:13 +0100817 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100818 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100819 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100820
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100821 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100822 setsid();
823
824 td->pid = getpid();
825
Jens Axboe5921e802008-05-30 15:02:38 +0200826 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100827
Jens Axboeaea47d42006-05-26 19:27:29 +0200828 INIT_LIST_HEAD(&td->io_u_freelist);
829 INIT_LIST_HEAD(&td->io_u_busylist);
Jens Axboe755200a2007-02-19 13:08:12 +0100830 INIT_LIST_HEAD(&td->io_u_requeues);
Jens Axboeaea47d42006-05-26 19:27:29 +0200831 INIT_LIST_HEAD(&td->io_log_list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200832 INIT_LIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200833 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200834
Jens Axboe75154842006-06-01 13:56:09 +0200835 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100836 fio_mutex_up(startup_mutex);
837 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100838
Jens Axboe37f56872007-03-09 12:42:35 +0100839 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100840 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100841 * eating a file descriptor
842 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100843 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100844
Jens Axboed84f8d42007-05-16 08:47:46 +0200845 /*
846 * May alter parameters that init_io_u() will use, so we need to
847 * do this first.
848 */
849 if (init_iolog(td))
850 goto err;
851
Jens Axboedb1cd902007-04-26 13:47:07 +0200852 if (init_io_u(td))
853 goto err;
854
Jens Axboe375b2692007-05-22 09:13:02 +0200855 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200856 td_verror(td, errno, "cpu_set_affinity");
857 goto err;
858 }
859
Jens Axboeac684782007-11-08 08:29:07 +0100860 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200861 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
862 td_verror(td, errno, "ioprio_set");
863 goto err;
864 }
865 }
866
867 if (nice(td->o.nice) == -1) {
868 td_verror(td, errno, "nice");
869 goto err;
870 }
871
872 if (td->o.ioscheduler && switch_ioscheduler(td))
873 goto err;
874
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100875 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100876 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100877
878 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100879 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100880
Jens Axboeb5af8292007-03-08 12:43:13 +0100881 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100882 goto err;
883
Jens Axboe68727072007-03-15 20:49:25 +0100884 if (init_random_map(td))
885 goto err;
886
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100887 if (td->o.exec_prerun) {
888 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100889 goto err;
890 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200891
Jens Axboe69008992006-11-24 13:12:56 +0100892 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100893 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100894 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100895
896 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100897 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200898 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100899 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100900 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100901
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100902 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100903 memcpy(&td->lastrate, &td->ts.stat_sample_time,
904 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100905
Jens Axboea978ba62007-03-08 14:29:03 +0100906 if (clear_state && clear_io_state(td))
907 break;
908
Jens Axboeebac4652005-12-08 15:25:21 +0100909 prune_io_piece_log(td);
910
Jens Axboeba0fbe12007-03-09 14:34:23 +0100911 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100912
Jens Axboea978ba62007-03-08 14:29:03 +0100913 clear_state = 1;
914
Jens Axboe38d77ca2007-03-21 14:20:20 +0100915 if (td_read(td) && td->io_bytes[DDIR_READ]) {
916 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100917 elapsed = utime_since(&td->start,
918 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100919 else
920 elapsed = utime_since_now(&td->start);
921
922 runtime[DDIR_READ] += elapsed;
923 }
924 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
925 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100926 elapsed = utime_since(&td->start,
927 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100928 else
929 elapsed = utime_since_now(&td->start);
930
931 runtime[DDIR_WRITE] += elapsed;
932 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100933
Jens Axboeebac4652005-12-08 15:25:21 +0100934 if (td->error || td->terminate)
935 break;
936
Shawn Lewise84c73a2007-08-02 22:19:32 +0200937 if (!td->o.do_verify ||
938 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +0200939 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +0100940 continue;
941
Jens Axboea978ba62007-03-08 14:29:03 +0100942 if (clear_io_state(td))
943 break;
944
Jens Axboe02bcaa82006-11-24 10:42:00 +0100945 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100946
947 do_verify(td);
948
Jens Axboe69008992006-11-24 13:12:56 +0100949 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100950
951 if (td->error || td->terminate)
952 break;
953 }
954
Jens Axboe36dff962006-11-24 13:29:20 +0100955 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +0200956 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
957 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +0100958 td->ts.total_run_time = mtime_since_now(&td->epoch);
959 td->ts.io_bytes[0] = td->io_bytes[0];
960 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +0100961
Jens Axboe756867b2007-03-06 15:19:24 +0100962 if (td->ts.bw_log)
963 finish_log(td, td->ts.bw_log, "bw");
964 if (td->ts.slat_log)
965 finish_log(td, td->ts.slat_log, "slat");
966 if (td->ts.clat_log)
967 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100968 if (td->o.exec_postrun) {
969 if (system(td->o.exec_postrun) < 0)
970 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100971 }
Jens Axboeebac4652005-12-08 15:25:21 +0100972
973 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100974 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100975
976err:
Jens Axboe5bf13a52007-02-17 01:51:47 +0100977 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +0200978 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100979 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100980 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200981 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100982 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +0200983
984 /*
985 * do this very late, it will log file closing as well
986 */
987 if (td->o.write_iolog_file)
988 write_iolog_close(td);
989
Jens Axboed23bb322007-03-23 15:57:56 +0100990 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100991 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +0100992 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +0100993}
994
Jens Axboe906c8d72006-06-13 09:37:56 +0200995/*
996 * We cannot pass the td data into a forked process, so attach the td and
997 * pass it to the thread worker.
998 */
Jens Axboea6418142007-02-17 04:47:18 +0100999static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001000{
1001 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001002 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001003
1004 data = shmat(shmid, NULL, 0);
1005 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001006 int __err = errno;
1007
Jens Axboeebac4652005-12-08 15:25:21 +01001008 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001009 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001010 }
1011
1012 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001013 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001014 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001015 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001016}
1017
Jens Axboe906c8d72006-06-13 09:37:56 +02001018/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001019 * Run over the job map and reap the threads that have exited, if any.
1020 */
Jens Axboeebac4652005-12-08 15:25:21 +01001021static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1022{
Jens Axboe34572e22006-10-20 09:33:18 +02001023 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001024 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001025
1026 /*
1027 * reap exited threads (TD_EXITED -> TD_REAPED)
1028 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001029 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001030 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001031 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001032
Jens Axboe84585002006-10-19 20:26:22 +02001033 /*
1034 * ->io_ops is NULL for a thread that has closed its
1035 * io engine
1036 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001037 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001038 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001039 else
1040 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001041
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001042 if (!td->pid) {
1043 pending++;
1044 continue;
1045 }
1046 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001047 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001048 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001049 if (td->runstate == TD_EXITED) {
1050 td_set_runstate(td, TD_REAPED);
1051 goto reaped;
1052 }
1053 continue;
1054 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001055
1056 flags = WNOHANG;
1057 if (td->runstate == TD_EXITED)
1058 flags = 0;
1059
1060 /*
1061 * check if someone quit or got killed in an unusual way
1062 */
1063 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001064 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001065 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001066 log_err("fio: pid=%d disappeared %d\n",
1067 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001068 td_set_runstate(td, TD_REAPED);
1069 goto reaped;
1070 }
1071 perror("waitpid");
1072 } else if (ret == td->pid) {
1073 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001074 int sig = WTERMSIG(status);
1075
Jens Axboed9244942007-03-05 12:32:32 +01001076 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001077 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001078 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001079 td_set_runstate(td, TD_REAPED);
1080 goto reaped;
1081 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001082 if (WIFEXITED(status)) {
1083 if (WEXITSTATUS(status) && !td->error)
1084 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001085
Jens Axboea2f77c92007-02-22 11:53:00 +01001086 td_set_runstate(td, TD_REAPED);
1087 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001088 }
1089 }
Jens Axboeebac4652005-12-08 15:25:21 +01001090
Jens Axboea2f77c92007-02-22 11:53:00 +01001091 /*
1092 * thread is not dead, continue
1093 */
gurudas paie48676b2007-04-11 12:44:27 +02001094 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001095 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001096reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001097 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001098 (*m_rate) -= td->o.ratemin;
1099 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001100 if (!td->pid)
1101 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001102
1103 if (td->error)
1104 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001105
1106 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001107 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001108
Jens Axboe1f809d12007-10-25 18:34:02 +02001109 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001110 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001111}
1112
Jens Axboe906c8d72006-06-13 09:37:56 +02001113/*
1114 * Main function for kicking off and reaping jobs, as needed.
1115 */
Jens Axboeebac4652005-12-08 15:25:21 +01001116static void run_threads(void)
1117{
Jens Axboeebac4652005-12-08 15:25:21 +01001118 struct thread_data *td;
1119 unsigned long spent;
1120 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001121
Jens Axboe2f9ade32006-10-20 11:25:52 +02001122 if (fio_pin_memory())
1123 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001124
Jens Axboec6ae0a52006-06-12 11:04:44 +02001125 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001126 printf("Starting ");
1127 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001128 printf("%d thread%s", nr_thread,
1129 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001130 if (nr_process) {
1131 if (nr_thread)
1132 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001133 printf("%d process%s", nr_process,
1134 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001135 }
1136 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001137 fflush(stdout);
1138 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001139
Jens Axboe4efa9702006-05-31 11:56:49 +02001140 signal(SIGINT, sig_handler);
1141 signal(SIGALRM, sig_handler);
1142
Jens Axboeebac4652005-12-08 15:25:21 +01001143 todo = thread_number;
1144 nr_running = 0;
1145 nr_started = 0;
1146 m_rate = t_rate = 0;
1147
Jens Axboe34572e22006-10-20 09:33:18 +02001148 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001149 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001150
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001151 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001152 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001153 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001154 }
Jens Axboeebac4652005-12-08 15:25:21 +01001155
1156 /*
1157 * do file setup here so it happens sequentially,
1158 * we don't want X number of threads getting their
1159 * client data interspersed on disk
1160 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001161 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001162 exit_value++;
1163 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001164 log_err("fio: pid=%d, err=%d/%s\n",
1165 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001166 td_set_runstate(td, TD_REAPED);
1167 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001168 } else {
1169 struct fio_file *f;
1170 unsigned int i;
1171
1172 /*
1173 * for sharing to work, each job must always open
1174 * its own files. so close them, if we opened them
1175 * for creation
1176 */
1177 for_each_file(td, f, i)
1178 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001179 }
Jens Axboe380cf262007-01-11 14:01:27 +01001180
1181 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001182 }
1183
Jens Axboea2f77c92007-02-22 11:53:00 +01001184 set_genesis_time();
1185
Jens Axboeebac4652005-12-08 15:25:21 +01001186 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001187 struct thread_data *map[MAX_JOBS];
1188 struct timeval this_start;
1189 int this_jobs = 0, left;
1190
Jens Axboeebac4652005-12-08 15:25:21 +01001191 /*
1192 * create threads (TD_NOT_CREATED -> TD_CREATED)
1193 */
Jens Axboe34572e22006-10-20 09:33:18 +02001194 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001195 if (td->runstate != TD_NOT_CREATED)
1196 continue;
1197
1198 /*
1199 * never got a chance to start, killed by other
1200 * thread for some reason
1201 */
1202 if (td->terminate) {
1203 todo--;
1204 continue;
1205 }
1206
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001207 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001208 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001209
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001210 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001211 continue;
1212 }
1213
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001214 if (td->o.stonewall && (nr_started || nr_running)) {
1215 dprint(FD_PROCESS, "%s: stonewall wait\n",
1216 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001217 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001218 }
Jens Axboeebac4652005-12-08 15:25:21 +01001219
Jens Axboe75154842006-06-01 13:56:09 +02001220 /*
1221 * Set state to created. Thread will transition
1222 * to TD_INITIALIZED when it's done setting up.
1223 */
Jens Axboeebac4652005-12-08 15:25:21 +01001224 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001225 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001226 nr_started++;
1227
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001228 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001229 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001230 if (pthread_create(&td->thread, NULL,
1231 thread_main, td)) {
Jens Axboef4705a72008-03-10 10:52:22 +01001232 perror("pthread_create");
Jens Axboeebac4652005-12-08 15:25:21 +01001233 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001234 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001235 }
Jens Axboef4705a72008-03-10 10:52:22 +01001236 if (pthread_detach(td->thread) < 0)
1237 perror("pthread_detach");
Jens Axboeebac4652005-12-08 15:25:21 +01001238 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001239 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001240 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001241 pid = fork();
1242 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001243 int ret = fork_main(shm_id, i);
1244
Jens Axboe5e1d3062008-05-23 11:55:53 +02001245 _exit(ret);
1246 } else if (i == fio_debug_jobno)
1247 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001248 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001249 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001250 }
1251
1252 /*
Jens Axboe75154842006-06-01 13:56:09 +02001253 * Wait for the started threads to transition to
1254 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001255 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001256 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001257 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001258 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001259 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1260 break;
1261
1262 usleep(100000);
1263
1264 for (i = 0; i < this_jobs; i++) {
1265 td = map[i];
1266 if (!td)
1267 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001268 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001269 map[i] = NULL;
1270 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001271 } else if (td->runstate >= TD_EXITED) {
1272 map[i] = NULL;
1273 left--;
1274 todo--;
1275 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001276 }
1277 }
1278 }
1279
1280 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001281 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001282 for (i = 0; i < this_jobs; i++) {
1283 td = map[i];
1284 if (!td)
1285 continue;
1286 kill(td->pid, SIGTERM);
1287 }
1288 break;
1289 }
1290
1291 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001292 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001293 */
Jens Axboe34572e22006-10-20 09:33:18 +02001294 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001295 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001296 continue;
1297
1298 td_set_runstate(td, TD_RUNNING);
1299 nr_running++;
1300 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001301 m_rate += td->o.ratemin;
1302 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001303 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001304 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001305 }
1306
1307 reap_threads(&nr_running, &t_rate, &m_rate);
1308
1309 if (todo)
1310 usleep(100000);
1311 }
1312
1313 while (nr_running) {
1314 reap_threads(&nr_running, &t_rate, &m_rate);
1315 usleep(10000);
1316 }
1317
1318 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001319 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001320}
1321
Jens Axboeebac4652005-12-08 15:25:21 +01001322int main(int argc, char *argv[])
1323{
Jens Axboe29d610e2007-02-06 13:25:33 +01001324 long ps;
1325
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001326 sinit();
1327
Jens Axboedbe11252007-02-11 00:19:51 +01001328 /*
1329 * We need locale for number printing, if it isn't set then just
1330 * go with the US format.
1331 */
1332 if (!getenv("LC_NUMERIC"))
1333 setlocale(LC_NUMERIC, "en_US");
1334
Jens Axboeebac4652005-12-08 15:25:21 +01001335 if (parse_options(argc, argv))
1336 return 1;
1337
Jens Axboe4b472fa2007-04-04 11:04:34 +02001338 if (!thread_number)
1339 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001340
Jens Axboe29d610e2007-02-06 13:25:33 +01001341 ps = sysconf(_SC_PAGESIZE);
1342 if (ps < 0) {
1343 log_err("Failed to get page size\n");
1344 return 1;
1345 }
1346
Jens Axboecfc99db2007-03-14 10:34:47 +01001347 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001348 page_mask = ps - 1;
1349
Jens Axboebb3884d2007-01-17 17:23:11 +11001350 if (write_bw_log) {
1351 setup_log(&agg_io_log[DDIR_READ]);
1352 setup_log(&agg_io_log[DDIR_WRITE]);
1353 }
1354
Jens Axboecdd18ad2008-02-27 18:58:00 +01001355 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001356
Jens Axboea2f77c92007-02-22 11:53:00 +01001357 set_genesis_time();
1358
Jens Axboeebac4652005-12-08 15:25:21 +01001359 disk_util_timer_arm();
1360
1361 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001362
Jens Axboebb3884d2007-01-17 17:23:11 +11001363 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001364 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001365 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001366 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1367 __finish_log(agg_io_log[DDIR_WRITE],
1368 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001369 }
1370 }
Jens Axboeebac4652005-12-08 15:25:21 +01001371
Jens Axboecdd18ad2008-02-27 18:58:00 +01001372 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001373 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001374}