blob: 89cc5996ca8854c9b9c655a232a366e8d278c9d7 [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 Axboeebac4652005-12-08 15:25:21 +010038
Jens Axboecfc99db2007-03-14 10:34:47 +010039unsigned long page_mask;
40unsigned long page_size;
Jens Axboe29d610e2007-02-06 13:25:33 +010041#define ALIGN(buf) \
42 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010043
44int groupid = 0;
45int thread_number = 0;
Jens Axboe9cedf162007-03-12 11:29:30 +010046int nr_process = 0;
47int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010048int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020049int temp_stall_ts;
Jens Axboeebac4652005-12-08 15:25:21 +010050
Jens Axboe07739b52007-03-08 20:25:46 +010051static struct fio_sem *startup_sem;
Jens Axboe6ce15a32007-01-12 09:04:41 +010052static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010053static int exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +010054
Jens Axboebb3884d2007-01-17 17:23:11 +110055struct io_log *agg_io_log[2];
56
Jens Axboeebac4652005-12-08 15:25:21 +010057#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020058#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010059
Jens Axboe6ce15a32007-01-12 09:04:41 +010060static inline void td_set_runstate(struct thread_data *td, int runstate)
61{
62 td->runstate = runstate;
63}
64
Jens Axboe390c40e2007-03-05 12:35:29 +010065static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010066{
Jens Axboe34572e22006-10-20 09:33:18 +020067 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010068 int i;
69
Jens Axboe34572e22006-10-20 09:33:18 +020070 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010071 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboec1302d42007-03-05 20:18:31 +010072 /*
73 * if the thread is running, just let it exit
74 */
75 if (td->runstate < TD_RUNNING)
76 kill(td->pid, SIGQUIT);
Jens Axboeebac4652005-12-08 15:25:21 +010077 td->terminate = 1;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010078 td->o.start_delay = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010079 }
80 }
81}
82
83static void sig_handler(int sig)
84{
85 switch (sig) {
86 case SIGALRM:
87 update_io_ticks();
88 disk_util_timer_arm();
89 print_thread_status();
90 break;
91 default:
Jens Axboe6ce15a32007-01-12 09:04:41 +010092 printf("\nfio: terminating on signal %d\n", sig);
Jens Axboeebac4652005-12-08 15:25:21 +010093 fflush(stdout);
Jens Axboe390c40e2007-03-05 12:35:29 +010094 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +010095 break;
96 }
97}
98
Jens Axboe906c8d72006-06-13 09:37:56 +020099/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200100 * Check if we are above the minimum rate given.
101 */
Jens Axboeebac4652005-12-08 15:25:21 +0100102static int check_min_rate(struct thread_data *td, struct timeval *now)
103{
Jens Axboe09042002007-02-23 10:56:22 +0100104 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100105 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100106 unsigned long spent;
107 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100108
109 /*
Jens Axboe780bf1a2007-02-28 11:13:49 +0100110 * No minimum rate set, always ok
111 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100112 if (!td->o.ratemin && !td->o.rate_iops_min)
Jens Axboe780bf1a2007-02-28 11:13:49 +0100113 return 0;
114
115 /*
Jens Axboeebac4652005-12-08 15:25:21 +0100116 * allow a 2 second settle period in the beginning
117 */
118 if (mtime_since(&td->start, now) < 2000)
119 return 0;
120
Jens Axboe4e991c22007-03-15 11:41:11 +0100121 if (td_read(td)) {
122 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100123 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100124 }
125 if (td_write(td)) {
126 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100127 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100128 }
Jens Axboe09042002007-02-23 10:56:22 +0100129
Jens Axboeebac4652005-12-08 15:25:21 +0100130 /*
131 * if rate blocks is set, sample is running
132 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100133 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100134 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100135 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100136 return 0;
137
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100138 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100139 /*
140 * check bandwidth specified rate
141 */
142 if (bytes < td->rate_bytes) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100143 log_err("%s: min rate %u not met\n", td->o.name, td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100144 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100145 } else {
146 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100147 if (rate < td->o.ratemin || bytes < td->rate_bytes) {
148 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 +0100149 return 1;
150 }
151 }
152 } else {
153 /*
154 * checks iops specified rate
155 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100156 if (iops < td->o.rate_iops) {
157 log_err("%s: min iops rate %u not met\n", td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100158 return 1;
159 } else {
160 rate = (iops - td->rate_blocks) / spent;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100161 if (rate < td->o.rate_iops_min || iops < td->rate_blocks) {
162 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 +0100163 }
Jens Axboe413dd452007-02-23 09:26:09 +0100164 }
Jens Axboeebac4652005-12-08 15:25:21 +0100165 }
166 }
167
Jens Axboe09042002007-02-23 10:56:22 +0100168 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100169 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100170 memcpy(&td->lastrate, now, sizeof(*now));
171 return 0;
172}
173
174static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
175{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100176 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100177 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100178 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100179 return 1;
180
181 return 0;
182}
183
Jens Axboe906c8d72006-06-13 09:37:56 +0200184/*
185 * When job exits, we can cancel the in-flight IO if we are using async
186 * io. Attempt to do so.
187 */
Jens Axboeebac4652005-12-08 15:25:21 +0100188static void cleanup_pending_aio(struct thread_data *td)
189{
Jens Axboeebac4652005-12-08 15:25:21 +0100190 struct list_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100191 struct io_u *io_u;
192 int r;
193
194 /*
195 * get immediately available events, if any
196 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100197 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100198 if (r < 0)
199 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100200
201 /*
202 * now cancel remaining active events
203 */
Jens Axboe2866c822006-10-09 15:57:48 +0200204 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100205 list_for_each_safe(entry, n, &td->io_u_busylist) {
206 io_u = list_entry(entry, struct io_u, list);
207
Jens Axboe0c6e7512007-02-22 11:19:39 +0100208 /*
209 * if the io_u isn't in flight, then that generally
210 * means someone leaked an io_u. complain but fix
211 * it up, so we don't stall here.
212 */
213 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
214 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100215 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100216 } else {
217 r = td->io_ops->cancel(td, io_u);
218 if (!r)
219 put_io_u(td, io_u);
220 }
Jens Axboeebac4652005-12-08 15:25:21 +0100221 }
222 }
223
Jens Axboe97601022007-02-18 12:47:29 +0100224 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100225 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100226}
227
Jens Axboe906c8d72006-06-13 09:37:56 +0200228/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200229 * Helper to handle the final sync of a file. Works just like the normal
230 * io path, just does everything sync.
231 */
232static int fio_io_sync(struct thread_data *td, struct fio_file *f)
233{
234 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200235 int ret;
236
237 if (!io_u)
238 return 1;
239
240 io_u->ddir = DDIR_SYNC;
241 io_u->file = f;
242
243 if (td_io_prep(td, io_u)) {
244 put_io_u(td, io_u);
245 return 1;
246 }
247
Jens Axboe755200a2007-02-19 13:08:12 +0100248requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200249 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100250 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100251 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200252 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200253 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100254 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100255 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100256 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100257 } else if (ret == FIO_Q_COMPLETED) {
258 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100259 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100260 return 1;
261 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200262
Jens Axboed7762cf2007-02-23 12:34:57 +0100263 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100264 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100265 } else if (ret == FIO_Q_BUSY) {
266 if (td_io_commit(td))
267 return 1;
268 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200269 }
270
271 return 0;
272}
273
274/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100275 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200276 * reads the blocks back in, and checks the crc/md5 of the data.
277 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100278static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100279{
Jens Axboe53cdc682006-10-18 11:50:58 +0200280 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100281 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100282 int ret, min_events;
283 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200284
285 /*
286 * sync io first and invalidate cache, to make sure we really
287 * read from disk.
288 */
289 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100290 if (!(f->flags & FIO_FILE_OPEN))
291 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100292 if (fio_io_sync(td, f))
293 break;
294 if (file_invalidate_cache(td, f))
295 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200296 }
Jens Axboeebac4652005-12-08 15:25:21 +0100297
Jens Axboeb2fdda42007-02-20 20:52:51 +0100298 if (td->error)
299 return;
300
Jens Axboeebac4652005-12-08 15:25:21 +0100301 td_set_runstate(td, TD_VERIFYING);
302
Jens Axboe36167d82007-02-18 05:41:31 +0100303 io_u = NULL;
304 while (!td->terminate) {
Jens Axboe54517922007-03-05 10:06:06 +0100305 int ret2;
306
Jens Axboeebac4652005-12-08 15:25:21 +0100307 io_u = __get_io_u(td);
308 if (!io_u)
309 break;
310
Jens Axboe069c2912007-02-21 23:02:39 +0100311 if (runtime_exceeded(td, &io_u->start_time)) {
312 put_io_u(td, io_u);
Jens Axboe53cdc682006-10-18 11:50:58 +0200313 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100314 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200315
Jens Axboe069c2912007-02-21 23:02:39 +0100316 if (get_next_verify(td, io_u)) {
317 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100318 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100319 }
Jens Axboeebac4652005-12-08 15:25:21 +0100320
Jens Axboe069c2912007-02-21 23:02:39 +0100321 if (td_io_prep(td, io_u)) {
322 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100323 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100324 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100325
326 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100327
Jens Axboe11786802007-03-05 18:33:22 +0100328 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100329 switch (ret) {
330 case FIO_Q_COMPLETED:
331 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100332 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100333 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100334 int bytes = io_u->xfer_buflen - io_u->resid;
335
Jens Axboe9e9d1642007-03-12 09:37:46 +0100336 /*
337 * zero read, fail
338 */
339 if (!bytes) {
340 td_verror(td, ENODATA, "full resid");
341 put_io_u(td, io_u);
342 break;
343 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200344
Jens Axboe36167d82007-02-18 05:41:31 +0100345 io_u->xfer_buflen = io_u->resid;
346 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200347 io_u->offset += bytes;
348
349 if (io_u->offset == io_u->file->real_file_size)
350 goto sync_done;
351
Jens Axboe11786802007-03-05 18:33:22 +0100352 requeue_io_u(td, &io_u);
353 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200354sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100355 ret = io_u_sync_complete(td, io_u);
356 if (ret < 0)
357 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100358 }
Jens Axboe36167d82007-02-18 05:41:31 +0100359 continue;
360 case FIO_Q_QUEUED:
361 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100362 case FIO_Q_BUSY:
363 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100364 ret2 = td_io_commit(td);
365 if (ret2 < 0)
366 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100367 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100368 default:
369 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100370 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100371 break;
372 }
373
Jens Axboe99784632007-02-19 10:06:17 +0100374 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100375 break;
376
Jens Axboe3af6ef32007-02-18 06:57:43 +0100377 /*
378 * if we can queue more, do so. but check if there are
379 * completed io_u's first.
380 */
Jens Axboe97601022007-02-18 12:47:29 +0100381 min_events = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100382 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe3af6ef32007-02-18 06:57:43 +0100383 min_events = 1;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100384
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100385 if (td->cur_depth > td->o.iodepth_low)
386 min_events = td->cur_depth - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100387 }
388
Jens Axboe3af6ef32007-02-18 06:57:43 +0100389 /*
390 * Reap required number of io units, if any, and do the
391 * verification on them through the callback handler
392 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100393 if (io_u_queued_complete(td, min_events) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100394 break;
395 }
396
Jens Axboec01c0392007-02-26 12:43:42 +0100397 if (!td->error) {
398 min_events = td->cur_depth;
399
400 if (min_events)
401 ret = io_u_queued_complete(td, min_events);
402 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100403 cleanup_pending_aio(td);
404
405 td_set_runstate(td, TD_RUNNING);
406}
407
Jens Axboe32cd46a2006-06-07 13:40:40 +0200408/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200409 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200410 * and reaps them, checking for rate and errors along the way.
411 */
Jens Axboeebac4652005-12-08 15:25:21 +0100412static void do_io(struct thread_data *td)
413{
Jens Axboe02bcaa82006-11-24 10:42:00 +0100414 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100415 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100416 unsigned int i;
417 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100418
Jens Axboe5853e5a2006-06-08 14:41:05 +0200419 td_set_runstate(td, TD_RUNNING);
420
Jens Axboe7bb48f82007-03-27 15:30:28 +0200421 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100422 struct timeval comp_time;
423 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200424 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100425 struct io_u *io_u;
Jens Axboe54517922007-03-05 10:06:06 +0100426 int ret2;
Jens Axboeebac4652005-12-08 15:25:21 +0100427
428 if (td->terminate)
429 break;
430
Jens Axboe3d7c3912007-02-19 13:16:12 +0100431 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100432 if (!io_u)
433 break;
434
435 memcpy(&s, &io_u->start_time, sizeof(s));
Jens Axboe97601022007-02-18 12:47:29 +0100436
437 if (runtime_exceeded(td, &s)) {
438 put_io_u(td, io_u);
439 break;
440 }
Jens Axboe36167d82007-02-18 05:41:31 +0100441
Jens Axboe11786802007-03-05 18:33:22 +0100442 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100443 switch (ret) {
444 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100445 if (io_u->error)
446 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100447 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100448 int bytes = io_u->xfer_buflen - io_u->resid;
449
Jens Axboe9e9d1642007-03-12 09:37:46 +0100450 /*
451 * zero read, fail
452 */
453 if (!bytes) {
454 td_verror(td, ENODATA, "full resid");
455 put_io_u(td, io_u);
456 break;
457 }
458
Jens Axboe36167d82007-02-18 05:41:31 +0100459 io_u->xfer_buflen = io_u->resid;
460 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200461 io_u->offset += bytes;
462
463 if (io_u->offset == io_u->file->real_file_size)
464 goto sync_done;
465
Jens Axboe11786802007-03-05 18:33:22 +0100466 requeue_io_u(td, &io_u);
467 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200468sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100469 fio_gettime(&comp_time, NULL);
470 bytes_done = io_u_sync_complete(td, io_u);
471 if (bytes_done < 0)
472 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100473 }
Jens Axboe36167d82007-02-18 05:41:31 +0100474 break;
475 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100476 /*
477 * if the engine doesn't have a commit hook,
478 * the io_u is really queued. if it does have such
479 * a hook, it has to call io_u_queued() itself.
480 */
481 if (td->io_ops->commit == NULL)
482 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100483 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100484 case FIO_Q_BUSY:
485 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100486 ret2 = td_io_commit(td);
487 if (ret2 < 0)
488 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100489 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100490 default:
491 assert(ret < 0);
492 put_io_u(td, io_u);
493 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100494 }
495
Jens Axboe99784632007-02-19 10:06:17 +0100496 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100497 break;
498
Jens Axboe97601022007-02-18 12:47:29 +0100499 /*
500 * See if we need to complete some commands
501 */
Jens Axboe755200a2007-02-19 13:08:12 +0100502 if (ret == FIO_Q_QUEUED || ret == FIO_Q_BUSY) {
Jens Axboe97601022007-02-18 12:47:29 +0100503 min_evts = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100504 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe36167d82007-02-18 05:41:31 +0100505 min_evts = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100506
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100507 if (td->cur_depth > td->o.iodepth_low)
508 min_evts = td->cur_depth - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100509 }
510
Jens Axboe97601022007-02-18 12:47:29 +0100511 fio_gettime(&comp_time, NULL);
Jens Axboed7762cf2007-02-23 12:34:57 +0100512 bytes_done = io_u_queued_complete(td, min_evts);
Jens Axboe97601022007-02-18 12:47:29 +0100513 if (bytes_done < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100514 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100515 }
516
Jens Axboe97601022007-02-18 12:47:29 +0100517 if (!bytes_done)
518 continue;
519
Jens Axboeebac4652005-12-08 15:25:21 +0100520 /*
521 * the rate is batched for now, it should work for batches
522 * of completions except the very first one which may look
523 * a little bursty
524 */
Jens Axboe97601022007-02-18 12:47:29 +0100525 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100526
Jens Axboe413dd452007-02-23 09:26:09 +0100527 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100528
Jens Axboe97601022007-02-18 12:47:29 +0100529 if (check_min_rate(td, &comp_time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100530 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100531 terminate_threads(td->groupid);
Jens Axboee1161c32007-02-22 19:36:48 +0100532 td_verror(td, ENODATA, "check_min_rate");
Jens Axboeebac4652005-12-08 15:25:21 +0100533 break;
534 }
535
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100536 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100537 unsigned long long b;
538
539 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100540 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100541 int left;
542
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100543 if (td->o.thinktime_spin)
544 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100545
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100546 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100547 if (left)
548 usec_sleep(td, left);
549 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100550 }
Jens Axboeebac4652005-12-08 15:25:21 +0100551 }
552
Jens Axboe4d2413c2006-11-23 11:58:59 +0100553 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100554 struct fio_file *f;
555
Jens Axboec01c0392007-02-26 12:43:42 +0100556 i = td->cur_depth;
557 if (i)
558 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100559
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100560 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200561 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100562
563 for_each_file(td, f, i) {
564 if (!(f->flags & FIO_FILE_OPEN))
565 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200566 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100567 }
Jens Axboe84585002006-10-19 20:26:22 +0200568 }
Jens Axboec01c0392007-02-26 12:43:42 +0100569 } else
570 cleanup_pending_aio(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100571}
572
Jens Axboeebac4652005-12-08 15:25:21 +0100573static void cleanup_io_u(struct thread_data *td)
574{
575 struct list_head *entry, *n;
576 struct io_u *io_u;
577
578 list_for_each_safe(entry, n, &td->io_u_freelist) {
579 io_u = list_entry(entry, struct io_u, list);
580
581 list_del(&io_u->list);
582 free(io_u);
583 }
584
Jens Axboe2f9ade32006-10-20 11:25:52 +0200585 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100586}
587
Jens Axboe6b9cea22006-10-30 17:00:24 +0100588/*
589 * "randomly" fill the buffer contents
590 */
Jens Axboe66eeb292006-11-01 08:35:44 +0100591static void fill_rand_buf(struct io_u *io_u, int max_bs)
Jens Axboe6b9cea22006-10-30 17:00:24 +0100592{
Jens Axboe66eeb292006-11-01 08:35:44 +0100593 int *ptr = io_u->buf;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100594
595 while ((void *) ptr - io_u->buf < max_bs) {
596 *ptr = rand() * 0x9e370001;
597 ptr++;
598 }
599}
600
Jens Axboeebac4652005-12-08 15:25:21 +0100601static int init_io_u(struct thread_data *td)
602{
Jens Axboe2b4ce342007-03-21 10:32:54 +0100603 unsigned long long buf_size;
Jens Axboeebac4652005-12-08 15:25:21 +0100604 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100605 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100606 int i, max_units;
607 char *p;
608
Jens Axboe2866c822006-10-09 15:57:48 +0200609 if (td->io_ops->flags & FIO_SYNCIO)
Jens Axboeebac4652005-12-08 15:25:21 +0100610 max_units = 1;
611 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100612 max_units = td->o.iodepth;
Jens Axboeebac4652005-12-08 15:25:21 +0100613
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100614 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe2b4ce342007-03-21 10:32:54 +0100615 buf_size = (unsigned long long) max_bs * (unsigned long long) max_units;
616 buf_size += page_mask;
617 if (buf_size != (size_t) buf_size) {
618 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
619 return 1;
620 }
621
622 td->orig_buffer_size = buf_size;
Jens Axboe74b025b2006-12-19 15:18:14 +0100623
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100624 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE)
625 td->orig_buffer_size = (td->orig_buffer_size + td->o.hugepage_size - 1) & ~(td->o.hugepage_size - 1);
Jens Axboe4afbf662007-03-20 10:33:11 +0100626 else if (td->orig_buffer_size & page_mask)
627 td->orig_buffer_size = (td->orig_buffer_size + page_mask) & ~page_mask;
Jens Axboeebac4652005-12-08 15:25:21 +0100628
Jens Axboe2f9ade32006-10-20 11:25:52 +0200629 if (allocate_io_mem(td))
630 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100631
Jens Axboeebac4652005-12-08 15:25:21 +0100632 p = ALIGN(td->orig_buffer);
633 for (i = 0; i < max_units; i++) {
634 io_u = malloc(sizeof(*io_u));
635 memset(io_u, 0, sizeof(*io_u));
636 INIT_LIST_HEAD(&io_u->list);
637
Jens Axboea00735e2006-11-03 08:58:08 +0100638 io_u->buf = p + max_bs * i;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100639 if (td_write(td) || td_rw(td))
Jens Axboea00735e2006-11-03 08:58:08 +0100640 fill_rand_buf(io_u, max_bs);
Jens Axboe6b9cea22006-10-30 17:00:24 +0100641
Jens Axboeb1ff3402006-02-17 11:35:58 +0100642 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100643 io_u->flags = IO_U_F_FREE;
Jens Axboeebac4652005-12-08 15:25:21 +0100644 list_add(&io_u->list, &td->io_u_freelist);
645 }
646
Jens Axboe433afcb2007-02-22 10:39:01 +0100647 io_u_init_timeout();
648
Jens Axboeebac4652005-12-08 15:25:21 +0100649 return 0;
650}
651
Jens Axboeda867742006-06-06 10:39:10 -0700652static int switch_ioscheduler(struct thread_data *td)
653{
654 char tmp[256], tmp2[128];
655 FILE *f;
656 int ret;
657
Jens Axboeba0fbe12007-03-09 14:34:23 +0100658 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200659 return 0;
660
Jens Axboeda867742006-06-06 10:39:10 -0700661 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
662
663 f = fopen(tmp, "r+");
664 if (!f) {
Jens Axboee1161c32007-02-22 19:36:48 +0100665 td_verror(td, errno, "fopen");
Jens Axboeda867742006-06-06 10:39:10 -0700666 return 1;
667 }
668
669 /*
670 * Set io scheduler.
671 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100672 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700673 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100674 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700675 fclose(f);
676 return 1;
677 }
678
679 rewind(f);
680
681 /*
682 * Read back and check that the selected scheduler is now the default.
683 */
684 ret = fread(tmp, 1, sizeof(tmp), f);
685 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100686 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700687 fclose(f);
688 return 1;
689 }
690
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100691 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700692 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100693 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100694 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700695 fclose(f);
696 return 1;
697 }
698
699 fclose(f);
700 return 0;
701}
702
Jens Axboea978ba62007-03-08 14:29:03 +0100703static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100704{
Jens Axboe53cdc682006-10-18 11:50:58 +0200705 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100706 unsigned int i;
707 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100708
Jens Axboe756867b2007-03-06 15:19:24 +0100709 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100710 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100711 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100712 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100713 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100714 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100715
Jens Axboec1324df2007-02-19 19:06:41 +0100716 td->last_was_sync = 0;
717
Jens Axboea978ba62007-03-08 14:29:03 +0100718 for_each_file(td, f, i)
719 td_io_close_file(td, f);
720
721 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200722 for_each_file(td, f, i) {
Jens Axboea978ba62007-03-08 14:29:03 +0100723 ret = td_io_open_file(td, f);
724 if (ret)
725 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200726 }
Jens Axboea978ba62007-03-08 14:29:03 +0100727
728 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100729}
730
Jens Axboe906c8d72006-06-13 09:37:56 +0200731/*
732 * Entry point for the thread based jobs. The process based jobs end up
733 * here as well, after a little setup.
734 */
Jens Axboeebac4652005-12-08 15:25:21 +0100735static void *thread_main(void *data)
736{
Jens Axboe69008992006-11-24 13:12:56 +0100737 unsigned long long runtime[2];
Jens Axboeebac4652005-12-08 15:25:21 +0100738 struct thread_data *td = data;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100739 unsigned long elapsed;
Jens Axboea978ba62007-03-08 14:29:03 +0100740 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100741
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100742 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100743 setsid();
744
745 td->pid = getpid();
746
Jens Axboeaea47d42006-05-26 19:27:29 +0200747 INIT_LIST_HEAD(&td->io_u_freelist);
748 INIT_LIST_HEAD(&td->io_u_busylist);
Jens Axboe755200a2007-02-19 13:08:12 +0100749 INIT_LIST_HEAD(&td->io_u_requeues);
Jens Axboeaea47d42006-05-26 19:27:29 +0200750 INIT_LIST_HEAD(&td->io_log_list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200751 INIT_LIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200752 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200753
Jens Axboeebac4652005-12-08 15:25:21 +0100754 if (init_io_u(td))
Jens Axboebda4fd92007-03-12 11:21:48 +0100755 goto err_sem;
Jens Axboeebac4652005-12-08 15:25:21 +0100756
757 if (fio_setaffinity(td) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100758 td_verror(td, errno, "cpu_set_affinity");
Jens Axboebda4fd92007-03-12 11:21:48 +0100759 goto err_sem;
Jens Axboeebac4652005-12-08 15:25:21 +0100760 }
761
Jens Axboeaea47d42006-05-26 19:27:29 +0200762 if (init_iolog(td))
Jens Axboebda4fd92007-03-12 11:21:48 +0100763 goto err_sem;
Jens Axboeaea47d42006-05-26 19:27:29 +0200764
Jens Axboeebac4652005-12-08 15:25:21 +0100765 if (td->ioprio) {
766 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100767 td_verror(td, errno, "ioprio_set");
Jens Axboebda4fd92007-03-12 11:21:48 +0100768 goto err_sem;
Jens Axboeebac4652005-12-08 15:25:21 +0100769 }
770 }
771
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100772 if (nice(td->o.nice) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100773 td_verror(td, errno, "nice");
Jens Axboebda4fd92007-03-12 11:21:48 +0100774 goto err_sem;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200775 }
776
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100777 if (td->o.ioscheduler && switch_ioscheduler(td))
Jens Axboebda4fd92007-03-12 11:21:48 +0100778 goto err_sem;
Jens Axboeda867742006-06-06 10:39:10 -0700779
Jens Axboe75154842006-06-01 13:56:09 +0200780 td_set_runstate(td, TD_INITIALIZED);
Jens Axboe07739b52007-03-08 20:25:46 +0100781 fio_sem_up(startup_sem);
782 fio_sem_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100783
Jens Axboe37f56872007-03-09 12:42:35 +0100784 /*
785 * the ->mutex semaphore is now no longer used, close it to avoid
786 * eating a file descriptor
787 */
788 fio_sem_remove(td->mutex);
789
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100790 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100791 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100792
793 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100794 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100795
Jens Axboeb5af8292007-03-08 12:43:13 +0100796 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100797 goto err;
798
Jens Axboe68727072007-03-15 20:49:25 +0100799 if (init_random_map(td))
800 goto err;
801
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100802 if (td->o.exec_prerun) {
803 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100804 goto err;
805 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200806
Jens Axboe69008992006-11-24 13:12:56 +0100807 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100808 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100809 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100810
811 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100812 clear_state = 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100813 while (td->o.loops--) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100814 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100815 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100816
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100817 if (td->o.ratemin)
Jens Axboe756867b2007-03-06 15:19:24 +0100818 memcpy(&td->lastrate, &td->ts.stat_sample_time, sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100819
Jens Axboea978ba62007-03-08 14:29:03 +0100820 if (clear_state && clear_io_state(td))
821 break;
822
Jens Axboeebac4652005-12-08 15:25:21 +0100823 prune_io_piece_log(td);
824
Jens Axboeba0fbe12007-03-09 14:34:23 +0100825 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100826
Jens Axboea978ba62007-03-08 14:29:03 +0100827 clear_state = 1;
828
Jens Axboe38d77ca2007-03-21 14:20:20 +0100829 if (td_read(td) && td->io_bytes[DDIR_READ]) {
830 if (td->rw_end_set[DDIR_READ])
831 elapsed = utime_since(&td->start, &td->rw_end[DDIR_READ]);
832 else
833 elapsed = utime_since_now(&td->start);
834
835 runtime[DDIR_READ] += elapsed;
836 }
837 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
838 if (td->rw_end_set[DDIR_WRITE])
839 elapsed = utime_since(&td->start, &td->rw_end[DDIR_WRITE]);
840 else
841 elapsed = utime_since_now(&td->start);
842
843 runtime[DDIR_WRITE] += elapsed;
844 }
Jens Axboe413dd452007-02-23 09:26:09 +0100845
Jens Axboeebac4652005-12-08 15:25:21 +0100846 if (td->error || td->terminate)
847 break;
848
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100849 if (td->o.verify == VERIFY_NONE)
Jens Axboeebac4652005-12-08 15:25:21 +0100850 continue;
851
Jens Axboea978ba62007-03-08 14:29:03 +0100852 if (clear_io_state(td))
853 break;
854
Jens Axboe02bcaa82006-11-24 10:42:00 +0100855 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100856
857 do_verify(td);
858
Jens Axboe69008992006-11-24 13:12:56 +0100859 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100860
861 if (td->error || td->terminate)
862 break;
863 }
864
Jens Axboe36dff962006-11-24 13:29:20 +0100865 update_rusage_stat(td);
Jens Axboe756867b2007-03-06 15:19:24 +0100866 td->ts.runtime[0] = runtime[0] / 1000;
867 td->ts.runtime[1] = runtime[1] / 1000;
868 td->ts.total_run_time = mtime_since_now(&td->epoch);
869 td->ts.io_bytes[0] = td->io_bytes[0];
870 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +0100871
Jens Axboe756867b2007-03-06 15:19:24 +0100872 if (td->ts.bw_log)
873 finish_log(td, td->ts.bw_log, "bw");
874 if (td->ts.slat_log)
875 finish_log(td, td->ts.slat_log, "slat");
876 if (td->ts.clat_log)
877 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100878 if (td->o.write_iolog_file)
Jens Axboe843a7412006-06-01 21:14:21 -0700879 write_iolog_close(td);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100880 if (td->o.exec_postrun) {
881 if (system(td->o.exec_postrun) < 0)
882 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100883 }
Jens Axboeebac4652005-12-08 15:25:21 +0100884
885 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100886 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100887
888err:
Jens Axboe5bf13a52007-02-17 01:51:47 +0100889 if (td->error)
890 printf("fio: pid=%d, err=%d/%s\n", td->pid, td->error, td->verror);
Jens Axboe53cdc682006-10-18 11:50:58 +0200891 close_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200892 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100893 cleanup_io_u(td);
Jens Axboed23bb322007-03-23 15:57:56 +0100894 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100895 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +0100896 return (void *) (unsigned long) td->error;
Jens Axboebda4fd92007-03-12 11:21:48 +0100897err_sem:
898 fio_sem_up(startup_sem);
899 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100900}
901
Jens Axboe906c8d72006-06-13 09:37:56 +0200902/*
903 * We cannot pass the td data into a forked process, so attach the td and
904 * pass it to the thread worker.
905 */
Jens Axboea6418142007-02-17 04:47:18 +0100906static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +0100907{
908 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +0100909 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100910
911 data = shmat(shmid, NULL, 0);
912 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +0100913 int __err = errno;
914
Jens Axboeebac4652005-12-08 15:25:21 +0100915 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +0100916 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +0100917 }
918
919 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +0100920 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100921 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +0100922 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100923}
924
Jens Axboe906c8d72006-06-13 09:37:56 +0200925/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200926 * Run over the job map and reap the threads that have exited, if any.
927 */
Jens Axboeebac4652005-12-08 15:25:21 +0100928static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
929{
Jens Axboe34572e22006-10-20 09:33:18 +0200930 struct thread_data *td;
Jens Axboefab6aa72007-02-17 06:19:24 +0100931 int i, cputhreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100932
933 /*
934 * reap exited threads (TD_EXITED -> TD_REAPED)
935 */
Jens Axboe4d2413c2006-11-23 11:58:59 +0100936 pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +0200937 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +0100938 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +0100939
Jens Axboe84585002006-10-19 20:26:22 +0200940 /*
941 * ->io_ops is NULL for a thread that has closed its
942 * io engine
943 */
Jens Axboeba0fbe12007-03-09 14:34:23 +0100944 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +0200945 cputhreads++;
946
Jens Axboea2f77c92007-02-22 11:53:00 +0100947 if (!td->pid || td->runstate == TD_REAPED)
948 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100949 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +0100950 if (td->runstate == TD_EXITED) {
951 td_set_runstate(td, TD_REAPED);
952 goto reaped;
953 }
954 continue;
955 }
Jens Axboea2f77c92007-02-22 11:53:00 +0100956
957 flags = WNOHANG;
958 if (td->runstate == TD_EXITED)
959 flags = 0;
960
961 /*
962 * check if someone quit or got killed in an unusual way
963 */
964 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +0100965 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +0100966 if (errno == ECHILD) {
967 log_err("fio: pid=%d disappeared %d\n", td->pid, td->runstate);
968 td_set_runstate(td, TD_REAPED);
969 goto reaped;
970 }
971 perror("waitpid");
972 } else if (ret == td->pid) {
973 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +0100974 int sig = WTERMSIG(status);
975
Jens Axboed9244942007-03-05 12:32:32 +0100976 if (sig != SIGQUIT)
977 log_err("fio: pid=%d, got signal=%d\n", td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +0100978 td_set_runstate(td, TD_REAPED);
979 goto reaped;
980 }
Jens Axboea2f77c92007-02-22 11:53:00 +0100981 if (WIFEXITED(status)) {
982 if (WEXITSTATUS(status) && !td->error)
983 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +0100984
Jens Axboea2f77c92007-02-22 11:53:00 +0100985 td_set_runstate(td, TD_REAPED);
986 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +0100987 }
988 }
Jens Axboeebac4652005-12-08 15:25:21 +0100989
Jens Axboea2f77c92007-02-22 11:53:00 +0100990 /*
991 * thread is not dead, continue
992 */
993 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +0100994reaped:
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100995 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +0100996 long ret;
997
998 if (pthread_join(td->thread, (void *) &ret))
999 perror("pthread_join");
1000 }
1001
Jens Axboeebac4652005-12-08 15:25:21 +01001002 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001003 (*m_rate) -= td->o.ratemin;
1004 (*t_rate) -= td->o.rate;
Jens Axboea2f77c92007-02-22 11:53:00 +01001005
1006 if (td->error)
1007 exit_value++;
Jens Axboeebac4652005-12-08 15:25:21 +01001008 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001009
Jens Axboe4d2413c2006-11-23 11:58:59 +01001010 if (*nr_running == cputhreads && !pending)
Jens Axboe390c40e2007-03-05 12:35:29 +01001011 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001012}
1013
Jens Axboe906c8d72006-06-13 09:37:56 +02001014/*
1015 * Main function for kicking off and reaping jobs, as needed.
1016 */
Jens Axboeebac4652005-12-08 15:25:21 +01001017static void run_threads(void)
1018{
Jens Axboeebac4652005-12-08 15:25:21 +01001019 struct thread_data *td;
1020 unsigned long spent;
1021 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001022
Jens Axboe2f9ade32006-10-20 11:25:52 +02001023 if (fio_pin_memory())
1024 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001025
Jens Axboec6ae0a52006-06-12 11:04:44 +02001026 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001027 printf("Starting ");
1028 if (nr_thread)
1029 printf("%d thread%s", nr_thread, nr_thread > 1 ? "s" : "");
1030 if (nr_process) {
1031 if (nr_thread)
1032 printf(" and ");
1033 printf("%d process%s", nr_process, nr_process > 1 ? "es" : "");
1034 }
1035 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001036 fflush(stdout);
1037 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001038
Jens Axboe4efa9702006-05-31 11:56:49 +02001039 signal(SIGINT, sig_handler);
1040 signal(SIGALRM, sig_handler);
1041
Jens Axboeebac4652005-12-08 15:25:21 +01001042 todo = thread_number;
1043 nr_running = 0;
1044 nr_started = 0;
1045 m_rate = t_rate = 0;
1046
Jens Axboe34572e22006-10-20 09:33:18 +02001047 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001048 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001049
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001050 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001051 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001052 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001053 }
Jens Axboeebac4652005-12-08 15:25:21 +01001054
1055 /*
1056 * do file setup here so it happens sequentially,
1057 * we don't want X number of threads getting their
1058 * client data interspersed on disk
1059 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001060 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001061 exit_value++;
1062 if (td->error)
1063 log_err("fio: pid=%d, err=%d/%s\n", td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001064 td_set_runstate(td, TD_REAPED);
1065 todo--;
1066 }
Jens Axboe380cf262007-01-11 14:01:27 +01001067
1068 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001069 }
1070
Jens Axboea2f77c92007-02-22 11:53:00 +01001071 set_genesis_time();
1072
Jens Axboeebac4652005-12-08 15:25:21 +01001073 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001074 struct thread_data *map[MAX_JOBS];
1075 struct timeval this_start;
1076 int this_jobs = 0, left;
1077
Jens Axboeebac4652005-12-08 15:25:21 +01001078 /*
1079 * create threads (TD_NOT_CREATED -> TD_CREATED)
1080 */
Jens Axboe34572e22006-10-20 09:33:18 +02001081 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001082 if (td->runstate != TD_NOT_CREATED)
1083 continue;
1084
1085 /*
1086 * never got a chance to start, killed by other
1087 * thread for some reason
1088 */
1089 if (td->terminate) {
1090 todo--;
1091 continue;
1092 }
1093
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001094 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001095 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001096
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001097 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001098 continue;
1099 }
1100
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001101 if (td->o.stonewall && (nr_started || nr_running))
Jens Axboeebac4652005-12-08 15:25:21 +01001102 break;
1103
Jens Axboe75154842006-06-01 13:56:09 +02001104 /*
1105 * Set state to created. Thread will transition
1106 * to TD_INITIALIZED when it's done setting up.
1107 */
Jens Axboeebac4652005-12-08 15:25:21 +01001108 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001109 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001110 nr_started++;
1111
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001112 if (td->o.use_thread) {
Jens Axboeebac4652005-12-08 15:25:21 +01001113 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1114 perror("thread_create");
1115 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001116 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001117 }
1118 } else {
Jens Axboe07739b52007-03-08 20:25:46 +01001119 if (!fork()) {
Jens Axboea6418142007-02-17 04:47:18 +01001120 int ret = fork_main(shm_id, i);
1121
1122 exit(ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001123 }
1124 }
Jens Axboe07739b52007-03-08 20:25:46 +01001125 fio_sem_down(startup_sem);
Jens Axboeebac4652005-12-08 15:25:21 +01001126 }
1127
1128 /*
Jens Axboe75154842006-06-01 13:56:09 +02001129 * Wait for the started threads to transition to
1130 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001131 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001132 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001133 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001134 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001135 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1136 break;
1137
1138 usleep(100000);
1139
1140 for (i = 0; i < this_jobs; i++) {
1141 td = map[i];
1142 if (!td)
1143 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001144 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001145 map[i] = NULL;
1146 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001147 } else if (td->runstate >= TD_EXITED) {
1148 map[i] = NULL;
1149 left--;
1150 todo--;
1151 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001152 }
1153 }
1154 }
1155
1156 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001157 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001158 for (i = 0; i < this_jobs; i++) {
1159 td = map[i];
1160 if (!td)
1161 continue;
1162 kill(td->pid, SIGTERM);
1163 }
1164 break;
1165 }
1166
1167 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001168 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001169 */
Jens Axboe34572e22006-10-20 09:33:18 +02001170 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001171 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001172 continue;
1173
1174 td_set_runstate(td, TD_RUNNING);
1175 nr_running++;
1176 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001177 m_rate += td->o.ratemin;
1178 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001179 todo--;
Jens Axboe07739b52007-03-08 20:25:46 +01001180 fio_sem_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001181 }
1182
1183 reap_threads(&nr_running, &t_rate, &m_rate);
1184
1185 if (todo)
1186 usleep(100000);
1187 }
1188
1189 while (nr_running) {
1190 reap_threads(&nr_running, &t_rate, &m_rate);
1191 usleep(10000);
1192 }
1193
1194 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001195 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001196}
1197
Jens Axboeebac4652005-12-08 15:25:21 +01001198int main(int argc, char *argv[])
1199{
Jens Axboe29d610e2007-02-06 13:25:33 +01001200 long ps;
1201
Jens Axboedbe11252007-02-11 00:19:51 +01001202 /*
1203 * We need locale for number printing, if it isn't set then just
1204 * go with the US format.
1205 */
1206 if (!getenv("LC_NUMERIC"))
1207 setlocale(LC_NUMERIC, "en_US");
1208
Jens Axboeebac4652005-12-08 15:25:21 +01001209 if (parse_options(argc, argv))
1210 return 1;
1211
Jens Axboe4b472fa2007-04-04 11:04:34 +02001212 if (!thread_number)
1213 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001214
Jens Axboe29d610e2007-02-06 13:25:33 +01001215 ps = sysconf(_SC_PAGESIZE);
1216 if (ps < 0) {
1217 log_err("Failed to get page size\n");
1218 return 1;
1219 }
1220
Jens Axboecfc99db2007-03-14 10:34:47 +01001221 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001222 page_mask = ps - 1;
1223
Jens Axboebb3884d2007-01-17 17:23:11 +11001224 if (write_bw_log) {
1225 setup_log(&agg_io_log[DDIR_READ]);
1226 setup_log(&agg_io_log[DDIR_WRITE]);
1227 }
1228
Jens Axboe07739b52007-03-08 20:25:46 +01001229 startup_sem = fio_sem_init(0);
1230
Jens Axboea2f77c92007-02-22 11:53:00 +01001231 set_genesis_time();
1232
Jens Axboeebac4652005-12-08 15:25:21 +01001233 disk_util_timer_arm();
1234
1235 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001236
Jens Axboebb3884d2007-01-17 17:23:11 +11001237 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001238 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001239 if (write_bw_log) {
1240 __finish_log(agg_io_log[DDIR_READ],"agg-read_bw.log");
1241 __finish_log(agg_io_log[DDIR_WRITE],"agg-write_bw.log");
1242 }
1243 }
Jens Axboeebac4652005-12-08 15:25:21 +01001244
Jens Axboe07739b52007-03-08 20:25:46 +01001245 fio_sem_remove(startup_sem);
Jens Axboe437c9b72007-02-13 18:20:37 +01001246 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001247}