blob: c609f55d8e9520672e8c1e67e8c7b811dbceb002 [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 Axboeebac4652005-12-08 15:25:21 +010039
Jens Axboecfc99db2007-03-14 10:34:47 +010040unsigned long page_mask;
41unsigned long page_size;
Jens Axboe29d610e2007-02-06 13:25:33 +010042#define ALIGN(buf) \
43 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010044
45int groupid = 0;
46int thread_number = 0;
Jens Axboe9cedf162007-03-12 11:29:30 +010047int nr_process = 0;
48int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010049int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020050int temp_stall_ts;
Jens Axboeebac4652005-12-08 15:25:21 +010051
Jens Axboecdd18ad2008-02-27 18:58:00 +010052static struct fio_mutex *startup_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010053static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010054static int exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +010055
Jens Axboebb3884d2007-01-17 17:23:11 +110056struct io_log *agg_io_log[2];
57
Jens Axboeebac4652005-12-08 15:25:21 +010058#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020059#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010060
Jens Axboe6ce15a32007-01-12 09:04:41 +010061static inline void td_set_runstate(struct thread_data *td, int runstate)
62{
Jens Axboebd6f78b2008-02-01 20:27:52 +010063 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", td->pid, td->runstate,
64 runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010065 td->runstate = runstate;
66}
67
Jens Axboe390c40e2007-03-05 12:35:29 +010068static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010069{
Jens Axboe34572e22006-10-20 09:33:18 +020070 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010071 int i;
72
Jens Axboe34572e22006-10-20 09:33:18 +020073 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010074 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboeee56ad52008-02-01 10:30:20 +010075 dprint(FD_PROCESS, "setting terminate on %d\n",td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010076
77 td->terminate = 1;
78 td->o.start_delay = 0;
79
Jens Axboe7a93ab12007-04-18 12:25:41 +020080 /*
81 * if the thread is running, just let it exit
82 */
83 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010084 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010085 else {
86 struct ioengine_ops *ops = td->io_ops;
87
88 if (ops && (ops->flags & FIO_SIGQUIT))
89 kill(td->pid, SIGQUIT);
90 }
Jens Axboeebac4652005-12-08 15:25:21 +010091 }
92 }
93}
94
95static void sig_handler(int sig)
96{
97 switch (sig) {
98 case SIGALRM:
99 update_io_ticks();
100 disk_util_timer_arm();
101 print_thread_status();
102 break;
103 default:
Jens Axboe6ce15a32007-01-12 09:04:41 +0100104 printf("\nfio: terminating on signal %d\n", sig);
Jens Axboeebac4652005-12-08 15:25:21 +0100105 fflush(stdout);
Jens Axboe390c40e2007-03-05 12:35:29 +0100106 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100107 break;
108 }
109}
110
Jens Axboe906c8d72006-06-13 09:37:56 +0200111/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200112 * Check if we are above the minimum rate given.
113 */
Jens Axboeebac4652005-12-08 15:25:21 +0100114static int check_min_rate(struct thread_data *td, struct timeval *now)
115{
Jens Axboe09042002007-02-23 10:56:22 +0100116 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100117 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100118 unsigned long spent;
119 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100120
121 /*
Jens Axboe780bf1a2007-02-28 11:13:49 +0100122 * No minimum rate set, always ok
123 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100124 if (!td->o.ratemin && !td->o.rate_iops_min)
Jens Axboe780bf1a2007-02-28 11:13:49 +0100125 return 0;
126
127 /*
Jens Axboeebac4652005-12-08 15:25:21 +0100128 * allow a 2 second settle period in the beginning
129 */
130 if (mtime_since(&td->start, now) < 2000)
131 return 0;
132
Jens Axboe4e991c22007-03-15 11:41:11 +0100133 if (td_read(td)) {
134 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100135 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100136 }
137 if (td_write(td)) {
138 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100139 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100140 }
Jens Axboe09042002007-02-23 10:56:22 +0100141
Jens Axboeebac4652005-12-08 15:25:21 +0100142 /*
143 * if rate blocks is set, sample is running
144 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100145 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100146 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100147 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100148 return 0;
149
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100150 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100151 /*
152 * check bandwidth specified rate
153 */
154 if (bytes < td->rate_bytes) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100155 log_err("%s: min rate %u not met\n", td->o.name, td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100156 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100157 } else {
158 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100159 if (rate < td->o.ratemin || bytes < td->rate_bytes) {
160 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 +0100161 return 1;
162 }
163 }
164 } else {
165 /*
166 * checks iops specified rate
167 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100168 if (iops < td->o.rate_iops) {
169 log_err("%s: min iops rate %u not met\n", td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100170 return 1;
171 } else {
172 rate = (iops - td->rate_blocks) / spent;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100173 if (rate < td->o.rate_iops_min || iops < td->rate_blocks) {
174 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 +0100175 }
Jens Axboe413dd452007-02-23 09:26:09 +0100176 }
Jens Axboeebac4652005-12-08 15:25:21 +0100177 }
178 }
179
Jens Axboe09042002007-02-23 10:56:22 +0100180 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100181 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100182 memcpy(&td->lastrate, now, sizeof(*now));
183 return 0;
184}
185
186static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
187{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100188 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100189 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100190 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100191 return 1;
192
193 return 0;
194}
195
Jens Axboe906c8d72006-06-13 09:37:56 +0200196/*
197 * When job exits, we can cancel the in-flight IO if we are using async
198 * io. Attempt to do so.
199 */
Jens Axboeebac4652005-12-08 15:25:21 +0100200static void cleanup_pending_aio(struct thread_data *td)
201{
Jens Axboeebac4652005-12-08 15:25:21 +0100202 struct list_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100203 struct io_u *io_u;
204 int r;
205
206 /*
207 * get immediately available events, if any
208 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100209 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100210 if (r < 0)
211 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100212
213 /*
214 * now cancel remaining active events
215 */
Jens Axboe2866c822006-10-09 15:57:48 +0200216 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100217 list_for_each_safe(entry, n, &td->io_u_busylist) {
218 io_u = list_entry(entry, struct io_u, list);
219
Jens Axboe0c6e7512007-02-22 11:19:39 +0100220 /*
221 * if the io_u isn't in flight, then that generally
222 * means someone leaked an io_u. complain but fix
223 * it up, so we don't stall here.
224 */
225 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
226 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100227 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100228 } else {
229 r = td->io_ops->cancel(td, io_u);
230 if (!r)
231 put_io_u(td, io_u);
232 }
Jens Axboeebac4652005-12-08 15:25:21 +0100233 }
234 }
235
Jens Axboe97601022007-02-18 12:47:29 +0100236 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100237 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100238}
239
Jens Axboe906c8d72006-06-13 09:37:56 +0200240/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200241 * Helper to handle the final sync of a file. Works just like the normal
242 * io path, just does everything sync.
243 */
244static int fio_io_sync(struct thread_data *td, struct fio_file *f)
245{
246 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200247 int ret;
248
249 if (!io_u)
250 return 1;
251
252 io_u->ddir = DDIR_SYNC;
253 io_u->file = f;
254
255 if (td_io_prep(td, io_u)) {
256 put_io_u(td, io_u);
257 return 1;
258 }
259
Jens Axboe755200a2007-02-19 13:08:12 +0100260requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200261 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100262 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100263 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200264 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200265 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100266 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100267 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100268 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100269 } else if (ret == FIO_Q_COMPLETED) {
270 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100271 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100272 return 1;
273 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200274
Jens Axboed7762cf2007-02-23 12:34:57 +0100275 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100276 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100277 } else if (ret == FIO_Q_BUSY) {
278 if (td_io_commit(td))
279 return 1;
280 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200281 }
282
283 return 0;
284}
285
286/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100287 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200288 * reads the blocks back in, and checks the crc/md5 of the data.
289 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100290static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100291{
Jens Axboe53cdc682006-10-18 11:50:58 +0200292 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100293 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100294 int ret, min_events;
295 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200296
297 /*
298 * sync io first and invalidate cache, to make sure we really
299 * read from disk.
300 */
301 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100302 if (!(f->flags & FIO_FILE_OPEN))
303 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100304 if (fio_io_sync(td, f))
305 break;
306 if (file_invalidate_cache(td, f))
307 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200308 }
Jens Axboeebac4652005-12-08 15:25:21 +0100309
Jens Axboeb2fdda42007-02-20 20:52:51 +0100310 if (td->error)
311 return;
312
Jens Axboeebac4652005-12-08 15:25:21 +0100313 td_set_runstate(td, TD_VERIFYING);
314
Jens Axboe36167d82007-02-18 05:41:31 +0100315 io_u = NULL;
316 while (!td->terminate) {
Jens Axboe54517922007-03-05 10:06:06 +0100317 int ret2;
318
Jens Axboeebac4652005-12-08 15:25:21 +0100319 io_u = __get_io_u(td);
320 if (!io_u)
321 break;
322
Jens Axboe069c2912007-02-21 23:02:39 +0100323 if (runtime_exceeded(td, &io_u->start_time)) {
324 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200325 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200326 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100327 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200328
Jens Axboe069c2912007-02-21 23:02:39 +0100329 if (get_next_verify(td, io_u)) {
330 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100331 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100332 }
Jens Axboeebac4652005-12-08 15:25:21 +0100333
Jens Axboe069c2912007-02-21 23:02:39 +0100334 if (td_io_prep(td, io_u)) {
335 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100336 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100337 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100338
339 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100340
Jens Axboe11786802007-03-05 18:33:22 +0100341 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100342 switch (ret) {
343 case FIO_Q_COMPLETED:
344 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100345 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100346 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100347 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200348 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100349
Jens Axboe9e9d1642007-03-12 09:37:46 +0100350 /*
351 * zero read, fail
352 */
353 if (!bytes) {
354 td_verror(td, ENODATA, "full resid");
355 put_io_u(td, io_u);
356 break;
357 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200358
Jens Axboe36167d82007-02-18 05:41:31 +0100359 io_u->xfer_buflen = io_u->resid;
360 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200361 io_u->offset += bytes;
Jens Axboed460eb32007-04-16 21:39:33 +0200362 f->last_completed_pos = io_u->offset;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200363
Jens Axboe30061b92007-04-17 13:31:34 +0200364 td->ts.short_io_u[io_u->ddir]++;
365
Jens Axboed460eb32007-04-16 21:39:33 +0200366 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200367 goto sync_done;
368
Jens Axboe11786802007-03-05 18:33:22 +0100369 requeue_io_u(td, &io_u);
370 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200371sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100372 ret = io_u_sync_complete(td, io_u);
373 if (ret < 0)
374 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100375 }
Jens Axboe36167d82007-02-18 05:41:31 +0100376 continue;
377 case FIO_Q_QUEUED:
378 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100379 case FIO_Q_BUSY:
380 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100381 ret2 = td_io_commit(td);
382 if (ret2 < 0)
383 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100384 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100385 default:
386 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100387 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100388 break;
389 }
390
Jens Axboe99784632007-02-19 10:06:17 +0100391 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100392 break;
393
Jens Axboe3af6ef32007-02-18 06:57:43 +0100394 /*
395 * if we can queue more, do so. but check if there are
396 * completed io_u's first.
397 */
Jens Axboe97601022007-02-18 12:47:29 +0100398 min_events = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100399 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe3af6ef32007-02-18 06:57:43 +0100400 min_events = 1;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100401
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100402 if (td->cur_depth > td->o.iodepth_low)
403 min_events = td->cur_depth - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100404 }
405
Jens Axboe3af6ef32007-02-18 06:57:43 +0100406 /*
407 * Reap required number of io units, if any, and do the
408 * verification on them through the callback handler
409 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100410 if (io_u_queued_complete(td, min_events) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100411 break;
412 }
413
Jens Axboec01c0392007-02-26 12:43:42 +0100414 if (!td->error) {
415 min_events = td->cur_depth;
416
417 if (min_events)
418 ret = io_u_queued_complete(td, min_events);
419 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100420 cleanup_pending_aio(td);
421
422 td_set_runstate(td, TD_RUNNING);
423}
424
Jens Axboe32cd46a2006-06-07 13:40:40 +0200425/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200426 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200427 * and reaps them, checking for rate and errors along the way.
428 */
Jens Axboeebac4652005-12-08 15:25:21 +0100429static void do_io(struct thread_data *td)
430{
Jens Axboe02bcaa82006-11-24 10:42:00 +0100431 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100432 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100433 unsigned int i;
434 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100435
Jens Axboe5853e5a2006-06-08 14:41:05 +0200436 td_set_runstate(td, TD_RUNNING);
437
Jens Axboe7bb48f82007-03-27 15:30:28 +0200438 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100439 struct timeval comp_time;
440 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200441 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100442 struct io_u *io_u;
Jens Axboe54517922007-03-05 10:06:06 +0100443 int ret2;
Jens Axboeebac4652005-12-08 15:25:21 +0100444
445 if (td->terminate)
446 break;
447
Jens Axboe3d7c3912007-02-19 13:16:12 +0100448 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100449 if (!io_u)
450 break;
451
452 memcpy(&s, &io_u->start_time, sizeof(s));
Jens Axboe97601022007-02-18 12:47:29 +0100453
454 if (runtime_exceeded(td, &s)) {
455 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200456 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100457 break;
458 }
Jens Axboe36167d82007-02-18 05:41:31 +0100459
Jens Axboeb67740d2007-07-26 12:22:30 +0200460 /*
461 * Add verification end_io handler, if asked to verify
462 * a previously written file.
463 */
464 if (td->o.verify != VERIFY_NONE)
465 io_u->end_io = verify_io_u;
466
Jens Axboe11786802007-03-05 18:33:22 +0100467 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100468 switch (ret) {
469 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100470 if (io_u->error)
471 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100472 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100473 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200474 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100475
Jens Axboe9e9d1642007-03-12 09:37:46 +0100476 /*
477 * zero read, fail
478 */
479 if (!bytes) {
480 td_verror(td, ENODATA, "full resid");
481 put_io_u(td, io_u);
482 break;
483 }
484
Jens Axboe36167d82007-02-18 05:41:31 +0100485 io_u->xfer_buflen = io_u->resid;
486 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200487 io_u->offset += bytes;
Jens Axboed460eb32007-04-16 21:39:33 +0200488 f->last_completed_pos = io_u->offset;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200489
Jens Axboe30061b92007-04-17 13:31:34 +0200490 td->ts.short_io_u[io_u->ddir]++;
491
Jens Axboed460eb32007-04-16 21:39:33 +0200492 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200493 goto sync_done;
494
Jens Axboe11786802007-03-05 18:33:22 +0100495 requeue_io_u(td, &io_u);
496 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200497sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100498 fio_gettime(&comp_time, NULL);
499 bytes_done = io_u_sync_complete(td, io_u);
500 if (bytes_done < 0)
501 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100502 }
Jens Axboe36167d82007-02-18 05:41:31 +0100503 break;
504 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100505 /*
506 * if the engine doesn't have a commit hook,
507 * the io_u is really queued. if it does have such
508 * a hook, it has to call io_u_queued() itself.
509 */
510 if (td->io_ops->commit == NULL)
511 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100512 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100513 case FIO_Q_BUSY:
514 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100515 ret2 = td_io_commit(td);
516 if (ret2 < 0)
517 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100518 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100519 default:
520 assert(ret < 0);
521 put_io_u(td, io_u);
522 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100523 }
524
Jens Axboe99784632007-02-19 10:06:17 +0100525 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100526 break;
527
Jens Axboe97601022007-02-18 12:47:29 +0100528 /*
529 * See if we need to complete some commands
530 */
Jens Axboe755200a2007-02-19 13:08:12 +0100531 if (ret == FIO_Q_QUEUED || ret == FIO_Q_BUSY) {
Jens Axboe97601022007-02-18 12:47:29 +0100532 min_evts = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100533 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe36167d82007-02-18 05:41:31 +0100534 min_evts = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100535
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100536 if (td->cur_depth > td->o.iodepth_low)
537 min_evts = td->cur_depth - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100538 }
539
Jens Axboe97601022007-02-18 12:47:29 +0100540 fio_gettime(&comp_time, NULL);
Jens Axboed7762cf2007-02-23 12:34:57 +0100541 bytes_done = io_u_queued_complete(td, min_evts);
Jens Axboe97601022007-02-18 12:47:29 +0100542 if (bytes_done < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100543 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100544 }
545
Jens Axboe97601022007-02-18 12:47:29 +0100546 if (!bytes_done)
547 continue;
548
Jens Axboeebac4652005-12-08 15:25:21 +0100549 /*
550 * the rate is batched for now, it should work for batches
551 * of completions except the very first one which may look
552 * a little bursty
553 */
Jens Axboe97601022007-02-18 12:47:29 +0100554 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100555
Jens Axboe413dd452007-02-23 09:26:09 +0100556 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100557
Jens Axboe97601022007-02-18 12:47:29 +0100558 if (check_min_rate(td, &comp_time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100559 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100560 terminate_threads(td->groupid);
Jens Axboee1161c32007-02-22 19:36:48 +0100561 td_verror(td, ENODATA, "check_min_rate");
Jens Axboeebac4652005-12-08 15:25:21 +0100562 break;
563 }
564
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100565 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100566 unsigned long long b;
567
568 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100569 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100570 int left;
571
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100572 if (td->o.thinktime_spin)
573 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100574
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100575 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100576 if (left)
577 usec_sleep(td, left);
578 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100579 }
Jens Axboeebac4652005-12-08 15:25:21 +0100580 }
581
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100582 if (td->o.fill_device && td->error == ENOSPC) {
583 td->error = 0;
584 td->terminate = 1;
585 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100586 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100587 struct fio_file *f;
588
Jens Axboec01c0392007-02-26 12:43:42 +0100589 i = td->cur_depth;
590 if (i)
591 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100592
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100593 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200594 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100595
596 for_each_file(td, f, i) {
597 if (!(f->flags & FIO_FILE_OPEN))
598 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200599 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100600 }
Jens Axboe84585002006-10-19 20:26:22 +0200601 }
Jens Axboec01c0392007-02-26 12:43:42 +0100602 } else
603 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100604
605 /*
606 * stop job if we failed doing any IO
607 */
608 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
609 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100610}
611
Jens Axboeebac4652005-12-08 15:25:21 +0100612static void cleanup_io_u(struct thread_data *td)
613{
614 struct list_head *entry, *n;
615 struct io_u *io_u;
616
617 list_for_each_safe(entry, n, &td->io_u_freelist) {
618 io_u = list_entry(entry, struct io_u, list);
619
620 list_del(&io_u->list);
621 free(io_u);
622 }
623
Jens Axboe2f9ade32006-10-20 11:25:52 +0200624 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100625}
626
Jens Axboe6b9cea22006-10-30 17:00:24 +0100627/*
628 * "randomly" fill the buffer contents
629 */
Jens Axboee9459e52007-04-17 15:46:32 +0200630static void fill_io_buf(struct thread_data *td, struct io_u *io_u, int max_bs)
Jens Axboe6b9cea22006-10-30 17:00:24 +0100631{
Jens Axboe210f43b2007-04-17 19:52:18 +0200632 long *ptr = io_u->buf;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100633
Jens Axboee9459e52007-04-17 15:46:32 +0200634 if (!td->o.zero_buffers) {
635 while ((void *) ptr - io_u->buf < max_bs) {
Jens Axboe210f43b2007-04-17 19:52:18 +0200636 *ptr = rand() * GOLDEN_RATIO_PRIME;
Jens Axboee9459e52007-04-17 15:46:32 +0200637 ptr++;
638 }
639 } else
640 memset(ptr, 0, max_bs);
Jens Axboe6b9cea22006-10-30 17:00:24 +0100641}
642
Jens Axboeebac4652005-12-08 15:25:21 +0100643static int init_io_u(struct thread_data *td)
644{
645 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100646 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100647 int i, max_units;
648 char *p;
649
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100650 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100651 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboec3990642007-07-19 10:17:09 +0200652 td->orig_buffer_size = (unsigned long long) max_bs * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100653
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100654 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE)
655 td->orig_buffer_size = (td->orig_buffer_size + td->o.hugepage_size - 1) & ~(td->o.hugepage_size - 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100656
Jens Axboec3990642007-07-19 10:17:09 +0200657 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
658 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
659 return 1;
660 }
661
Jens Axboe2f9ade32006-10-20 11:25:52 +0200662 if (allocate_io_mem(td))
663 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100664
Jens Axboeb3f378e2007-07-20 12:39:22 +0200665 if (td->o.odirect)
666 p = ALIGN(td->orig_buffer);
667 else
668 p = td->orig_buffer;
669
Jens Axboeebac4652005-12-08 15:25:21 +0100670 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200671 if (td->terminate)
672 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100673 io_u = malloc(sizeof(*io_u));
674 memset(io_u, 0, sizeof(*io_u));
675 INIT_LIST_HEAD(&io_u->list);
676
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200677 if (!(td->io_ops->flags & FIO_NOIO)) {
678 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200679
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200680 if (td_write(td))
681 fill_io_buf(td, io_u, max_bs);
682 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100683
Jens Axboeb1ff3402006-02-17 11:35:58 +0100684 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100685 io_u->flags = IO_U_F_FREE;
Jens Axboeebac4652005-12-08 15:25:21 +0100686 list_add(&io_u->list, &td->io_u_freelist);
687 }
688
Jens Axboe433afcb2007-02-22 10:39:01 +0100689 io_u_init_timeout();
690
Jens Axboeebac4652005-12-08 15:25:21 +0100691 return 0;
692}
693
Jens Axboeda867742006-06-06 10:39:10 -0700694static int switch_ioscheduler(struct thread_data *td)
695{
696 char tmp[256], tmp2[128];
697 FILE *f;
698 int ret;
699
Jens Axboeba0fbe12007-03-09 14:34:23 +0100700 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200701 return 0;
702
Jens Axboeda867742006-06-06 10:39:10 -0700703 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
704
705 f = fopen(tmp, "r+");
706 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200707 if (errno == ENOENT) {
708 log_err("fio: os or kernel doesn't support IO scheduler switching\n");
709 return 0;
710 }
711 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700712 return 1;
713 }
714
715 /*
716 * Set io scheduler.
717 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100718 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700719 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100720 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700721 fclose(f);
722 return 1;
723 }
724
725 rewind(f);
726
727 /*
728 * Read back and check that the selected scheduler is now the default.
729 */
730 ret = fread(tmp, 1, sizeof(tmp), f);
731 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100732 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700733 fclose(f);
734 return 1;
735 }
736
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100737 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700738 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100739 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100740 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700741 fclose(f);
742 return 1;
743 }
744
745 fclose(f);
746 return 0;
747}
748
Jens Axboe492158c2007-05-24 10:32:47 +0200749static int keep_running(struct thread_data *td)
750{
751 unsigned long long io_done;
752
Jens Axboe20e354e2007-07-23 14:36:16 +0200753 if (td->done)
754 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200755 if (td->o.time_based)
756 return 1;
757 if (td->o.loops) {
758 td->o.loops--;
759 return 1;
760 }
761
Jens Axboe48f5abd2007-07-20 13:25:04 +0200762 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE] + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200763 if (io_done < td->o.size)
764 return 1;
765
766 return 0;
767}
768
Jens Axboea978ba62007-03-08 14:29:03 +0100769static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100770{
Jens Axboe53cdc682006-10-18 11:50:58 +0200771 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100772 unsigned int i;
773 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100774
Jens Axboe756867b2007-03-06 15:19:24 +0100775 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100776 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100777 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100778 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100779 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100780 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100781
Jens Axboec1324df2007-02-19 19:06:41 +0100782 td->last_was_sync = 0;
783
Jens Axboe2ba1c292008-02-01 13:16:38 +0100784 /*
785 * reset file done count if we are to start over
786 */
787 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200788 td->nr_done_files = 0;
Jens Axboe281f9b62007-05-23 09:34:42 +0200789
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100790 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100791
792 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200793 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200794 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100795 ret = td_io_open_file(td, f);
796 if (ret)
797 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200798 }
Jens Axboea978ba62007-03-08 14:29:03 +0100799
800 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100801}
802
Jens Axboe906c8d72006-06-13 09:37:56 +0200803/*
804 * Entry point for the thread based jobs. The process based jobs end up
805 * here as well, after a little setup.
806 */
Jens Axboeebac4652005-12-08 15:25:21 +0100807static void *thread_main(void *data)
808{
Shawn Lewis10927312007-11-21 09:38:13 +0100809 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100810 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100811 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100812
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100813 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100814 setsid();
815
816 td->pid = getpid();
817
Jens Axboeee56ad52008-02-01 10:30:20 +0100818 dprint(FD_PROCESS, "jobs pid=%d started\n", td->pid);
819
Jens Axboeaea47d42006-05-26 19:27:29 +0200820 INIT_LIST_HEAD(&td->io_u_freelist);
821 INIT_LIST_HEAD(&td->io_u_busylist);
Jens Axboe755200a2007-02-19 13:08:12 +0100822 INIT_LIST_HEAD(&td->io_u_requeues);
Jens Axboeaea47d42006-05-26 19:27:29 +0200823 INIT_LIST_HEAD(&td->io_log_list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200824 INIT_LIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200825 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200826
Jens Axboe75154842006-06-01 13:56:09 +0200827 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100828 fio_mutex_up(startup_mutex);
829 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100830
Jens Axboe37f56872007-03-09 12:42:35 +0100831 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100832 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100833 * eating a file descriptor
834 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100835 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100836
Jens Axboed84f8d42007-05-16 08:47:46 +0200837 /*
838 * May alter parameters that init_io_u() will use, so we need to
839 * do this first.
840 */
841 if (init_iolog(td))
842 goto err;
843
Jens Axboedb1cd902007-04-26 13:47:07 +0200844 if (init_io_u(td))
845 goto err;
846
Jens Axboe375b2692007-05-22 09:13:02 +0200847 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200848 td_verror(td, errno, "cpu_set_affinity");
849 goto err;
850 }
851
Jens Axboeac684782007-11-08 08:29:07 +0100852 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200853 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
854 td_verror(td, errno, "ioprio_set");
855 goto err;
856 }
857 }
858
859 if (nice(td->o.nice) == -1) {
860 td_verror(td, errno, "nice");
861 goto err;
862 }
863
864 if (td->o.ioscheduler && switch_ioscheduler(td))
865 goto err;
866
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100867 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100868 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100869
870 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100871 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100872
Jens Axboeb5af8292007-03-08 12:43:13 +0100873 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100874 goto err;
875
Jens Axboe68727072007-03-15 20:49:25 +0100876 if (init_random_map(td))
877 goto err;
878
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100879 if (td->o.exec_prerun) {
880 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100881 goto err;
882 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200883
Jens Axboe69008992006-11-24 13:12:56 +0100884 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100885 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100886 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100887
888 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100889 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200890 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100891 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100892 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100893
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100894 if (td->o.ratemin)
Jens Axboe756867b2007-03-06 15:19:24 +0100895 memcpy(&td->lastrate, &td->ts.stat_sample_time, sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100896
Jens Axboea978ba62007-03-08 14:29:03 +0100897 if (clear_state && clear_io_state(td))
898 break;
899
Jens Axboeebac4652005-12-08 15:25:21 +0100900 prune_io_piece_log(td);
901
Jens Axboeba0fbe12007-03-09 14:34:23 +0100902 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100903
Jens Axboea978ba62007-03-08 14:29:03 +0100904 clear_state = 1;
905
Jens Axboe38d77ca2007-03-21 14:20:20 +0100906 if (td_read(td) && td->io_bytes[DDIR_READ]) {
907 if (td->rw_end_set[DDIR_READ])
908 elapsed = utime_since(&td->start, &td->rw_end[DDIR_READ]);
909 else
910 elapsed = utime_since_now(&td->start);
911
912 runtime[DDIR_READ] += elapsed;
913 }
914 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
915 if (td->rw_end_set[DDIR_WRITE])
916 elapsed = utime_since(&td->start, &td->rw_end[DDIR_WRITE]);
917 else
918 elapsed = utime_since_now(&td->start);
919
920 runtime[DDIR_WRITE] += elapsed;
921 }
Jens Axboe413dd452007-02-23 09:26:09 +0100922
Jens Axboeebac4652005-12-08 15:25:21 +0100923 if (td->error || td->terminate)
924 break;
925
Shawn Lewise84c73a2007-08-02 22:19:32 +0200926 if (!td->o.do_verify ||
927 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +0200928 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +0100929 continue;
930
Jens Axboea978ba62007-03-08 14:29:03 +0100931 if (clear_io_state(td))
932 break;
933
Jens Axboe02bcaa82006-11-24 10:42:00 +0100934 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100935
936 do_verify(td);
937
Jens Axboe69008992006-11-24 13:12:56 +0100938 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100939
940 if (td->error || td->terminate)
941 break;
942 }
943
Jens Axboe36dff962006-11-24 13:29:20 +0100944 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +0200945 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
946 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +0100947 td->ts.total_run_time = mtime_since_now(&td->epoch);
948 td->ts.io_bytes[0] = td->io_bytes[0];
949 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +0100950
Jens Axboe756867b2007-03-06 15:19:24 +0100951 if (td->ts.bw_log)
952 finish_log(td, td->ts.bw_log, "bw");
953 if (td->ts.slat_log)
954 finish_log(td, td->ts.slat_log, "slat");
955 if (td->ts.clat_log)
956 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100957 if (td->o.exec_postrun) {
958 if (system(td->o.exec_postrun) < 0)
959 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100960 }
Jens Axboeebac4652005-12-08 15:25:21 +0100961
962 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100963 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100964
965err:
Jens Axboe5bf13a52007-02-17 01:51:47 +0100966 if (td->error)
967 printf("fio: pid=%d, err=%d/%s\n", td->pid, td->error, td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100968 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200969 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100970 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +0200971
972 /*
973 * do this very late, it will log file closing as well
974 */
975 if (td->o.write_iolog_file)
976 write_iolog_close(td);
977
Jens Axboed23bb322007-03-23 15:57:56 +0100978 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100979 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +0100980 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +0100981}
982
Jens Axboe906c8d72006-06-13 09:37:56 +0200983/*
984 * We cannot pass the td data into a forked process, so attach the td and
985 * pass it to the thread worker.
986 */
Jens Axboea6418142007-02-17 04:47:18 +0100987static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +0100988{
989 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +0100990 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100991
992 data = shmat(shmid, NULL, 0);
993 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +0100994 int __err = errno;
995
Jens Axboeebac4652005-12-08 15:25:21 +0100996 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +0100997 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +0100998 }
999
1000 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001001 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001002 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001003 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001004}
1005
Jens Axboe906c8d72006-06-13 09:37:56 +02001006/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001007 * Run over the job map and reap the threads that have exited, if any.
1008 */
Jens Axboeebac4652005-12-08 15:25:21 +01001009static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1010{
Jens Axboe34572e22006-10-20 09:33:18 +02001011 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001012 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001013
1014 /*
1015 * reap exited threads (TD_EXITED -> TD_REAPED)
1016 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001017 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001018 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001019 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001020
Jens Axboe84585002006-10-19 20:26:22 +02001021 /*
1022 * ->io_ops is NULL for a thread that has closed its
1023 * io engine
1024 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001025 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001026 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001027 else
1028 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001029
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001030 if (!td->pid) {
1031 pending++;
1032 continue;
1033 }
1034 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001035 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001036 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001037 if (td->runstate == TD_EXITED) {
1038 td_set_runstate(td, TD_REAPED);
1039 goto reaped;
1040 }
1041 continue;
1042 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001043
1044 flags = WNOHANG;
1045 if (td->runstate == TD_EXITED)
1046 flags = 0;
1047
1048 /*
1049 * check if someone quit or got killed in an unusual way
1050 */
1051 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001052 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001053 if (errno == ECHILD) {
1054 log_err("fio: pid=%d disappeared %d\n", td->pid, td->runstate);
1055 td_set_runstate(td, TD_REAPED);
1056 goto reaped;
1057 }
1058 perror("waitpid");
1059 } else if (ret == td->pid) {
1060 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001061 int sig = WTERMSIG(status);
1062
Jens Axboed9244942007-03-05 12:32:32 +01001063 if (sig != SIGQUIT)
1064 log_err("fio: pid=%d, got signal=%d\n", td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001065 td_set_runstate(td, TD_REAPED);
1066 goto reaped;
1067 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001068 if (WIFEXITED(status)) {
1069 if (WEXITSTATUS(status) && !td->error)
1070 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001071
Jens Axboea2f77c92007-02-22 11:53:00 +01001072 td_set_runstate(td, TD_REAPED);
1073 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001074 }
1075 }
Jens Axboeebac4652005-12-08 15:25:21 +01001076
Jens Axboea2f77c92007-02-22 11:53:00 +01001077 /*
1078 * thread is not dead, continue
1079 */
gurudas paie48676b2007-04-11 12:44:27 +02001080 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001081 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001082reaped:
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001083 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001084 long ret;
1085
Jens Axboeee56ad52008-02-01 10:30:20 +01001086 dprint(FD_PROCESS, "joining tread %d\n", td->pid);
1087 if (pthread_join(td->thread, (void *) &ret)) {
1088 dprint(FD_PROCESS, "join failed %ld\n", ret);
Jens Axboe3707f452007-02-22 12:26:57 +01001089 perror("pthread_join");
Jens Axboeee56ad52008-02-01 10:30:20 +01001090 }
Jens Axboe3707f452007-02-22 12:26:57 +01001091 }
1092
Jens Axboeebac4652005-12-08 15:25:21 +01001093 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001094 (*m_rate) -= td->o.ratemin;
1095 (*t_rate) -= td->o.rate;
gurudas paie48676b2007-04-11 12:44:27 +02001096 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001097
1098 if (td->error)
1099 exit_value++;
Jens Axboeebac4652005-12-08 15:25:21 +01001100 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001101
Jens Axboe1f809d12007-10-25 18:34:02 +02001102 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001103 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001104}
1105
Jens Axboe906c8d72006-06-13 09:37:56 +02001106/*
1107 * Main function for kicking off and reaping jobs, as needed.
1108 */
Jens Axboeebac4652005-12-08 15:25:21 +01001109static void run_threads(void)
1110{
Jens Axboeebac4652005-12-08 15:25:21 +01001111 struct thread_data *td;
1112 unsigned long spent;
1113 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001114
Jens Axboe2f9ade32006-10-20 11:25:52 +02001115 if (fio_pin_memory())
1116 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001117
Jens Axboec6ae0a52006-06-12 11:04:44 +02001118 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001119 printf("Starting ");
1120 if (nr_thread)
1121 printf("%d thread%s", nr_thread, nr_thread > 1 ? "s" : "");
1122 if (nr_process) {
1123 if (nr_thread)
1124 printf(" and ");
1125 printf("%d process%s", nr_process, nr_process > 1 ? "es" : "");
1126 }
1127 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001128 fflush(stdout);
1129 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001130
Jens Axboe4efa9702006-05-31 11:56:49 +02001131 signal(SIGINT, sig_handler);
1132 signal(SIGALRM, sig_handler);
1133
Jens Axboeebac4652005-12-08 15:25:21 +01001134 todo = thread_number;
1135 nr_running = 0;
1136 nr_started = 0;
1137 m_rate = t_rate = 0;
1138
Jens Axboe34572e22006-10-20 09:33:18 +02001139 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001140 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001141
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001142 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001143 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001144 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001145 }
Jens Axboeebac4652005-12-08 15:25:21 +01001146
1147 /*
1148 * do file setup here so it happens sequentially,
1149 * we don't want X number of threads getting their
1150 * client data interspersed on disk
1151 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001152 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001153 exit_value++;
1154 if (td->error)
1155 log_err("fio: pid=%d, err=%d/%s\n", td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001156 td_set_runstate(td, TD_REAPED);
1157 todo--;
1158 }
Jens Axboe380cf262007-01-11 14:01:27 +01001159
1160 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001161 }
1162
Jens Axboea2f77c92007-02-22 11:53:00 +01001163 set_genesis_time();
1164
Jens Axboeebac4652005-12-08 15:25:21 +01001165 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001166 struct thread_data *map[MAX_JOBS];
1167 struct timeval this_start;
1168 int this_jobs = 0, left;
1169
Jens Axboeebac4652005-12-08 15:25:21 +01001170 /*
1171 * create threads (TD_NOT_CREATED -> TD_CREATED)
1172 */
Jens Axboe34572e22006-10-20 09:33:18 +02001173 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001174 if (td->runstate != TD_NOT_CREATED)
1175 continue;
1176
1177 /*
1178 * never got a chance to start, killed by other
1179 * thread for some reason
1180 */
1181 if (td->terminate) {
1182 todo--;
1183 continue;
1184 }
1185
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001186 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001187 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001188
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001189 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001190 continue;
1191 }
1192
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001193 if (td->o.stonewall && (nr_started || nr_running))
Jens Axboeebac4652005-12-08 15:25:21 +01001194 break;
1195
Jens Axboe75154842006-06-01 13:56:09 +02001196 /*
1197 * Set state to created. Thread will transition
1198 * to TD_INITIALIZED when it's done setting up.
1199 */
Jens Axboeebac4652005-12-08 15:25:21 +01001200 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001201 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001202 nr_started++;
1203
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001204 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001205 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001206 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1207 perror("thread_create");
1208 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001209 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001210 }
1211 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001212 dprint(FD_PROCESS, "will fork\n");
Jens Axboe07739b52007-03-08 20:25:46 +01001213 if (!fork()) {
Jens Axboea6418142007-02-17 04:47:18 +01001214 int ret = fork_main(shm_id, i);
1215
1216 exit(ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001217 }
1218 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001219 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001220 }
1221
1222 /*
Jens Axboe75154842006-06-01 13:56:09 +02001223 * Wait for the started threads to transition to
1224 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001225 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001226 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001227 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001228 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001229 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1230 break;
1231
1232 usleep(100000);
1233
1234 for (i = 0; i < this_jobs; i++) {
1235 td = map[i];
1236 if (!td)
1237 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001238 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001239 map[i] = NULL;
1240 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001241 } else if (td->runstate >= TD_EXITED) {
1242 map[i] = NULL;
1243 left--;
1244 todo--;
1245 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001246 }
1247 }
1248 }
1249
1250 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001251 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001252 for (i = 0; i < this_jobs; i++) {
1253 td = map[i];
1254 if (!td)
1255 continue;
1256 kill(td->pid, SIGTERM);
1257 }
1258 break;
1259 }
1260
1261 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001262 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001263 */
Jens Axboe34572e22006-10-20 09:33:18 +02001264 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001265 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001266 continue;
1267
1268 td_set_runstate(td, TD_RUNNING);
1269 nr_running++;
1270 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001271 m_rate += td->o.ratemin;
1272 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001273 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001274 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001275 }
1276
1277 reap_threads(&nr_running, &t_rate, &m_rate);
1278
1279 if (todo)
1280 usleep(100000);
1281 }
1282
1283 while (nr_running) {
1284 reap_threads(&nr_running, &t_rate, &m_rate);
1285 usleep(10000);
1286 }
1287
1288 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001289 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001290}
1291
Jens Axboeebac4652005-12-08 15:25:21 +01001292int main(int argc, char *argv[])
1293{
Jens Axboe29d610e2007-02-06 13:25:33 +01001294 long ps;
1295
Jens Axboedbe11252007-02-11 00:19:51 +01001296 /*
1297 * We need locale for number printing, if it isn't set then just
1298 * go with the US format.
1299 */
1300 if (!getenv("LC_NUMERIC"))
1301 setlocale(LC_NUMERIC, "en_US");
1302
Jens Axboeebac4652005-12-08 15:25:21 +01001303 if (parse_options(argc, argv))
1304 return 1;
1305
Jens Axboe4b472fa2007-04-04 11:04:34 +02001306 if (!thread_number)
1307 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001308
Jens Axboe29d610e2007-02-06 13:25:33 +01001309 ps = sysconf(_SC_PAGESIZE);
1310 if (ps < 0) {
1311 log_err("Failed to get page size\n");
1312 return 1;
1313 }
1314
Jens Axboecfc99db2007-03-14 10:34:47 +01001315 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001316 page_mask = ps - 1;
1317
Jens Axboebb3884d2007-01-17 17:23:11 +11001318 if (write_bw_log) {
1319 setup_log(&agg_io_log[DDIR_READ]);
1320 setup_log(&agg_io_log[DDIR_WRITE]);
1321 }
1322
Jens Axboecdd18ad2008-02-27 18:58:00 +01001323 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001324
Jens Axboea2f77c92007-02-22 11:53:00 +01001325 set_genesis_time();
1326
Jens Axboeebac4652005-12-08 15:25:21 +01001327 disk_util_timer_arm();
1328
1329 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001330
Jens Axboebb3884d2007-01-17 17:23:11 +11001331 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001332 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001333 if (write_bw_log) {
1334 __finish_log(agg_io_log[DDIR_READ],"agg-read_bw.log");
1335 __finish_log(agg_io_log[DDIR_WRITE],"agg-write_bw.log");
1336 }
1337 }
Jens Axboeebac4652005-12-08 15:25:21 +01001338
Jens Axboecdd18ad2008-02-27 18:58:00 +01001339 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001340 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001341}