blob: c1894f5f771fdeb92f1f05cc18ad55d645782a56 [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
Jens Axboeaae22ca2006-09-05 10:46:22 +02005 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboeebac4652005-12-08 15:25:21 +01006 *
Jens Axboe8e9fe632006-10-24 15:11:09 +02007 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
9 *
Jens Axboeebac4652005-12-08 15:25:21 +010010 * This program is free software; you can redistribute it and/or modify
Jens Axboe8e9fe632006-10-24 15:11:09 +020011 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Jens Axboeebac4652005-12-08 15:25:21 +010013 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
Jens Axboeebac4652005-12-08 15:25:21 +010024#include <unistd.h>
25#include <fcntl.h>
26#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <signal.h>
28#include <time.h>
Jens Axboedbe11252007-02-11 00:19:51 +010029#include <locale.h>
Jens Axboe36167d82007-02-18 05:41:31 +010030#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010031#include <sys/stat.h>
32#include <sys/wait.h>
33#include <sys/ipc.h>
34#include <sys/shm.h>
Jens Axboeebac4652005-12-08 15:25:21 +010035#include <sys/mman.h>
36
37#include "fio.h"
Jens Axboe210f43b2007-04-17 19:52:18 +020038#include "hash.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010039#include "smalloc.h"
Jens Axboeebac4652005-12-08 15:25:21 +010040
Jens Axboecfc99db2007-03-14 10:34:47 +010041unsigned long page_mask;
42unsigned long page_size;
Jens Axboe29d610e2007-02-06 13:25:33 +010043#define ALIGN(buf) \
44 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010045
46int groupid = 0;
47int thread_number = 0;
Jens Axboe9cedf162007-03-12 11:29:30 +010048int nr_process = 0;
49int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010050int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020051int temp_stall_ts;
Jens Axboeebac4652005-12-08 15:25:21 +010052
Jens Axboecdd18ad2008-02-27 18:58:00 +010053static struct fio_mutex *startup_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010054static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010055static int exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +010056
Jens Axboebb3884d2007-01-17 17:23:11 +110057struct io_log *agg_io_log[2];
58
Jens Axboeebac4652005-12-08 15:25:21 +010059#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020060#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010061
Jens Axboe6ce15a32007-01-12 09:04:41 +010062static inline void td_set_runstate(struct thread_data *td, int runstate)
63{
Jens Axboed8fab1b2008-03-06 10:25:17 +010064 if (td->runstate == runstate)
65 return;
66
Jens Axboebd6f78b2008-02-01 20:27:52 +010067 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", td->pid, td->runstate,
68 runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010069 td->runstate = runstate;
70}
71
Jens Axboe390c40e2007-03-05 12:35:29 +010072static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010073{
Jens Axboe34572e22006-10-20 09:33:18 +020074 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010075 int i;
76
Jens Axboe34572e22006-10-20 09:33:18 +020077 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010078 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010079 dprint(FD_PROCESS, "setting terminate on %d\n",
80 td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010081 td->terminate = 1;
82 td->o.start_delay = 0;
83
Jens Axboe7a93ab12007-04-18 12:25:41 +020084 /*
85 * if the thread is running, just let it exit
86 */
87 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010088 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010089 else {
90 struct ioengine_ops *ops = td->io_ops;
91
92 if (ops && (ops->flags & FIO_SIGQUIT))
93 kill(td->pid, SIGQUIT);
94 }
Jens Axboeebac4652005-12-08 15:25:21 +010095 }
96 }
97}
98
99static void sig_handler(int sig)
100{
101 switch (sig) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100102 case SIGALRM:
103 update_io_ticks();
104 disk_util_timer_arm();
105 print_thread_status();
106 break;
107 default:
108 printf("\nfio: terminating on signal %d\n", sig);
109 fflush(stdout);
110 terminate_threads(TERMINATE_ALL);
111 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100112 }
113}
114
Jens Axboe906c8d72006-06-13 09:37:56 +0200115/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200116 * Check if we are above the minimum rate given.
117 */
Jens Axboeebac4652005-12-08 15:25:21 +0100118static int check_min_rate(struct thread_data *td, struct timeval *now)
119{
Jens Axboe09042002007-02-23 10:56:22 +0100120 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100121 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100122 unsigned long spent;
123 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100124
125 /*
Jens Axboe780bf1a2007-02-28 11:13:49 +0100126 * No minimum rate set, always ok
127 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100128 if (!td->o.ratemin && !td->o.rate_iops_min)
Jens Axboe780bf1a2007-02-28 11:13:49 +0100129 return 0;
130
131 /*
Jens Axboeebac4652005-12-08 15:25:21 +0100132 * allow a 2 second settle period in the beginning
133 */
134 if (mtime_since(&td->start, now) < 2000)
135 return 0;
136
Jens Axboe4e991c22007-03-15 11:41:11 +0100137 if (td_read(td)) {
138 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100139 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100140 }
141 if (td_write(td)) {
142 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100143 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100144 }
Jens Axboe09042002007-02-23 10:56:22 +0100145
Jens Axboeebac4652005-12-08 15:25:21 +0100146 /*
147 * if rate blocks is set, sample is running
148 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100149 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100150 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100151 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100152 return 0;
153
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100154 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100155 /*
156 * check bandwidth specified rate
157 */
158 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100159 log_err("%s: min rate %u not met\n", td->o.name,
160 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100161 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100162 } else {
163 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100164 if (rate < td->o.ratemin ||
165 bytes < td->rate_bytes) {
166 log_err("%s: min rate %u not met, got"
167 " %luKiB/sec\n", td->o.name,
168 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100169 return 1;
170 }
171 }
172 } else {
173 /*
174 * checks iops specified rate
175 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100176 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100177 log_err("%s: min iops rate %u not met\n",
178 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100179 return 1;
180 } else {
181 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100182 if (rate < td->o.rate_iops_min ||
183 iops < td->rate_blocks) {
184 log_err("%s: min iops rate %u not met,"
185 " got %lu\n", td->o.name,
186 td->o.rate_iops_min,
187 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100188 }
Jens Axboe413dd452007-02-23 09:26:09 +0100189 }
Jens Axboeebac4652005-12-08 15:25:21 +0100190 }
191 }
192
Jens Axboe09042002007-02-23 10:56:22 +0100193 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100194 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100195 memcpy(&td->lastrate, now, sizeof(*now));
196 return 0;
197}
198
199static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
200{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100201 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100202 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100203 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100204 return 1;
205
206 return 0;
207}
208
Jens Axboe906c8d72006-06-13 09:37:56 +0200209/*
210 * When job exits, we can cancel the in-flight IO if we are using async
211 * io. Attempt to do so.
212 */
Jens Axboeebac4652005-12-08 15:25:21 +0100213static void cleanup_pending_aio(struct thread_data *td)
214{
Jens Axboeebac4652005-12-08 15:25:21 +0100215 struct list_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100216 struct io_u *io_u;
217 int r;
218
219 /*
220 * get immediately available events, if any
221 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100222 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100223 if (r < 0)
224 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100225
226 /*
227 * now cancel remaining active events
228 */
Jens Axboe2866c822006-10-09 15:57:48 +0200229 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100230 list_for_each_safe(entry, n, &td->io_u_busylist) {
231 io_u = list_entry(entry, struct io_u, list);
232
Jens Axboe0c6e7512007-02-22 11:19:39 +0100233 /*
234 * if the io_u isn't in flight, then that generally
235 * means someone leaked an io_u. complain but fix
236 * it up, so we don't stall here.
237 */
238 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
239 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100240 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100241 } else {
242 r = td->io_ops->cancel(td, io_u);
243 if (!r)
244 put_io_u(td, io_u);
245 }
Jens Axboeebac4652005-12-08 15:25:21 +0100246 }
247 }
248
Jens Axboe97601022007-02-18 12:47:29 +0100249 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100250 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100251}
252
Jens Axboe906c8d72006-06-13 09:37:56 +0200253/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200254 * Helper to handle the final sync of a file. Works just like the normal
255 * io path, just does everything sync.
256 */
257static int fio_io_sync(struct thread_data *td, struct fio_file *f)
258{
259 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200260 int ret;
261
262 if (!io_u)
263 return 1;
264
265 io_u->ddir = DDIR_SYNC;
266 io_u->file = f;
267
268 if (td_io_prep(td, io_u)) {
269 put_io_u(td, io_u);
270 return 1;
271 }
272
Jens Axboe755200a2007-02-19 13:08:12 +0100273requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200274 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100275 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100276 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200277 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200278 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100279 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100280 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100281 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100282 } else if (ret == FIO_Q_COMPLETED) {
283 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100284 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100285 return 1;
286 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200287
Jens Axboed7762cf2007-02-23 12:34:57 +0100288 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100289 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100290 } else if (ret == FIO_Q_BUSY) {
291 if (td_io_commit(td))
292 return 1;
293 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200294 }
295
296 return 0;
297}
298
299/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100300 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200301 * reads the blocks back in, and checks the crc/md5 of the data.
302 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100303static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100304{
Jens Axboe53cdc682006-10-18 11:50:58 +0200305 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100306 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100307 int ret, min_events;
308 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200309
310 /*
311 * sync io first and invalidate cache, to make sure we really
312 * read from disk.
313 */
314 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100315 if (!(f->flags & FIO_FILE_OPEN))
316 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100317 if (fio_io_sync(td, f))
318 break;
319 if (file_invalidate_cache(td, f))
320 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200321 }
Jens Axboeebac4652005-12-08 15:25:21 +0100322
Jens Axboeb2fdda42007-02-20 20:52:51 +0100323 if (td->error)
324 return;
325
Jens Axboeebac4652005-12-08 15:25:21 +0100326 td_set_runstate(td, TD_VERIFYING);
327
Jens Axboe36167d82007-02-18 05:41:31 +0100328 io_u = NULL;
329 while (!td->terminate) {
Jens Axboe54517922007-03-05 10:06:06 +0100330 int ret2;
331
Jens Axboeebac4652005-12-08 15:25:21 +0100332 io_u = __get_io_u(td);
333 if (!io_u)
334 break;
335
Jens Axboe069c2912007-02-21 23:02:39 +0100336 if (runtime_exceeded(td, &io_u->start_time)) {
337 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200338 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200339 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100340 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200341
Jens Axboe069c2912007-02-21 23:02:39 +0100342 if (get_next_verify(td, io_u)) {
343 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100344 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100345 }
Jens Axboeebac4652005-12-08 15:25:21 +0100346
Jens Axboe069c2912007-02-21 23:02:39 +0100347 if (td_io_prep(td, io_u)) {
348 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100349 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100350 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100351
352 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100353
Jens Axboe11786802007-03-05 18:33:22 +0100354 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100355 switch (ret) {
356 case FIO_Q_COMPLETED:
357 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100358 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100359 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100360 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200361 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100362
Jens Axboe9e9d1642007-03-12 09:37:46 +0100363 /*
364 * zero read, fail
365 */
366 if (!bytes) {
367 td_verror(td, ENODATA, "full resid");
368 put_io_u(td, io_u);
369 break;
370 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200371
Jens Axboe36167d82007-02-18 05:41:31 +0100372 io_u->xfer_buflen = io_u->resid;
373 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200374 io_u->offset += bytes;
375
Jens Axboe30061b92007-04-17 13:31:34 +0200376 td->ts.short_io_u[io_u->ddir]++;
377
Jens Axboed460eb32007-04-16 21:39:33 +0200378 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200379 goto sync_done;
380
Jens Axboe11786802007-03-05 18:33:22 +0100381 requeue_io_u(td, &io_u);
382 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200383sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100384 ret = io_u_sync_complete(td, io_u);
385 if (ret < 0)
386 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100387 }
Jens Axboe36167d82007-02-18 05:41:31 +0100388 continue;
389 case FIO_Q_QUEUED:
390 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100391 case FIO_Q_BUSY:
392 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100393 ret2 = td_io_commit(td);
394 if (ret2 < 0)
395 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100396 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100397 default:
398 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100399 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100400 break;
401 }
402
Jens Axboe99784632007-02-19 10:06:17 +0100403 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100404 break;
405
Jens Axboe3af6ef32007-02-18 06:57:43 +0100406 /*
407 * if we can queue more, do so. but check if there are
408 * completed io_u's first.
409 */
Jens Axboe97601022007-02-18 12:47:29 +0100410 min_events = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100411 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe3af6ef32007-02-18 06:57:43 +0100412 min_events = 1;
Jens Axboe3af6ef32007-02-18 06:57:43 +0100413
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100414 if (td->cur_depth > td->o.iodepth_low)
415 min_events = td->cur_depth - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100416 }
417
Jens Axboe3af6ef32007-02-18 06:57:43 +0100418 /*
419 * Reap required number of io units, if any, and do the
420 * verification on them through the callback handler
421 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100422 if (io_u_queued_complete(td, min_events) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100423 break;
424 }
425
Jens Axboec01c0392007-02-26 12:43:42 +0100426 if (!td->error) {
427 min_events = td->cur_depth;
428
429 if (min_events)
430 ret = io_u_queued_complete(td, min_events);
431 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100432 cleanup_pending_aio(td);
433
434 td_set_runstate(td, TD_RUNNING);
435}
436
Jens Axboe32cd46a2006-06-07 13:40:40 +0200437/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200438 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200439 * and reaps them, checking for rate and errors along the way.
440 */
Jens Axboeebac4652005-12-08 15:25:21 +0100441static void do_io(struct thread_data *td)
442{
Jens Axboe02bcaa82006-11-24 10:42:00 +0100443 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100444 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100445 unsigned int i;
446 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100447
Jens Axboe5853e5a2006-06-08 14:41:05 +0200448 td_set_runstate(td, TD_RUNNING);
449
Jens Axboe7bb48f82007-03-27 15:30:28 +0200450 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100451 struct timeval comp_time;
452 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200453 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100454 struct io_u *io_u;
Jens Axboe54517922007-03-05 10:06:06 +0100455 int ret2;
Jens Axboeebac4652005-12-08 15:25:21 +0100456
457 if (td->terminate)
458 break;
459
Jens Axboe3d7c3912007-02-19 13:16:12 +0100460 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100461 if (!io_u)
462 break;
463
464 memcpy(&s, &io_u->start_time, sizeof(s));
Jens Axboe97601022007-02-18 12:47:29 +0100465
466 if (runtime_exceeded(td, &s)) {
467 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200468 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100469 break;
470 }
Jens Axboe36167d82007-02-18 05:41:31 +0100471
Jens Axboeb67740d2007-07-26 12:22:30 +0200472 /*
473 * Add verification end_io handler, if asked to verify
474 * a previously written file.
475 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100476 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200477 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100478 td_set_runstate(td, TD_VERIFYING);
479 } else
480 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200481
Jens Axboe11786802007-03-05 18:33:22 +0100482 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100483 switch (ret) {
484 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100485 if (io_u->error)
486 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100487 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100488 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200489 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100490
Jens Axboe9e9d1642007-03-12 09:37:46 +0100491 /*
492 * zero read, fail
493 */
494 if (!bytes) {
495 td_verror(td, ENODATA, "full resid");
496 put_io_u(td, io_u);
497 break;
498 }
499
Jens Axboe36167d82007-02-18 05:41:31 +0100500 io_u->xfer_buflen = io_u->resid;
501 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200502 io_u->offset += bytes;
503
Jens Axboe30061b92007-04-17 13:31:34 +0200504 td->ts.short_io_u[io_u->ddir]++;
505
Jens Axboed460eb32007-04-16 21:39:33 +0200506 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200507 goto sync_done;
508
Jens Axboe11786802007-03-05 18:33:22 +0100509 requeue_io_u(td, &io_u);
510 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200511sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100512 fio_gettime(&comp_time, NULL);
513 bytes_done = io_u_sync_complete(td, io_u);
514 if (bytes_done < 0)
515 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100516 }
Jens Axboe36167d82007-02-18 05:41:31 +0100517 break;
518 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100519 /*
520 * if the engine doesn't have a commit hook,
521 * the io_u is really queued. if it does have such
522 * a hook, it has to call io_u_queued() itself.
523 */
524 if (td->io_ops->commit == NULL)
525 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100526 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100527 case FIO_Q_BUSY:
528 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100529 ret2 = td_io_commit(td);
530 if (ret2 < 0)
531 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100532 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100533 default:
534 assert(ret < 0);
535 put_io_u(td, io_u);
536 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100537 }
538
Jens Axboe99784632007-02-19 10:06:17 +0100539 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100540 break;
541
Jens Axboe97601022007-02-18 12:47:29 +0100542 /*
543 * See if we need to complete some commands
544 */
Jens Axboe755200a2007-02-19 13:08:12 +0100545 if (ret == FIO_Q_QUEUED || ret == FIO_Q_BUSY) {
Jens Axboe97601022007-02-18 12:47:29 +0100546 min_evts = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100547 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe36167d82007-02-18 05:41:31 +0100548 min_evts = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100549
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100550 if (td->cur_depth > td->o.iodepth_low)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100551 min_evts = td->cur_depth
552 - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100553 }
554
Jens Axboe97601022007-02-18 12:47:29 +0100555 fio_gettime(&comp_time, NULL);
Jens Axboed7762cf2007-02-23 12:34:57 +0100556 bytes_done = io_u_queued_complete(td, min_evts);
Jens Axboe97601022007-02-18 12:47:29 +0100557 if (bytes_done < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100558 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100559 }
560
Jens Axboe97601022007-02-18 12:47:29 +0100561 if (!bytes_done)
562 continue;
563
Jens Axboeebac4652005-12-08 15:25:21 +0100564 /*
565 * the rate is batched for now, it should work for batches
566 * of completions except the very first one which may look
567 * a little bursty
568 */
Jens Axboe97601022007-02-18 12:47:29 +0100569 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100570
Jens Axboe413dd452007-02-23 09:26:09 +0100571 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100572
Jens Axboe97601022007-02-18 12:47:29 +0100573 if (check_min_rate(td, &comp_time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100574 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100575 terminate_threads(td->groupid);
Jens Axboee1161c32007-02-22 19:36:48 +0100576 td_verror(td, ENODATA, "check_min_rate");
Jens Axboeebac4652005-12-08 15:25:21 +0100577 break;
578 }
579
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100580 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100581 unsigned long long b;
582
583 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100584 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100585 int left;
586
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100587 if (td->o.thinktime_spin)
588 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100589
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100590 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100591 if (left)
592 usec_sleep(td, left);
593 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100594 }
Jens Axboeebac4652005-12-08 15:25:21 +0100595 }
596
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100597 if (td->o.fill_device && td->error == ENOSPC) {
598 td->error = 0;
599 td->terminate = 1;
600 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100601 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100602 struct fio_file *f;
603
Jens Axboec01c0392007-02-26 12:43:42 +0100604 i = td->cur_depth;
605 if (i)
606 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100607
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100608 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200609 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100610
611 for_each_file(td, f, i) {
612 if (!(f->flags & FIO_FILE_OPEN))
613 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200614 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100615 }
Jens Axboe84585002006-10-19 20:26:22 +0200616 }
Jens Axboec01c0392007-02-26 12:43:42 +0100617 } else
618 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100619
620 /*
621 * stop job if we failed doing any IO
622 */
623 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
624 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100625}
626
Jens Axboeebac4652005-12-08 15:25:21 +0100627static void cleanup_io_u(struct thread_data *td)
628{
629 struct list_head *entry, *n;
630 struct io_u *io_u;
631
632 list_for_each_safe(entry, n, &td->io_u_freelist) {
633 io_u = list_entry(entry, struct io_u, list);
634
635 list_del(&io_u->list);
636 free(io_u);
637 }
638
Jens Axboe2f9ade32006-10-20 11:25:52 +0200639 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100640}
641
Jens Axboe6b9cea22006-10-30 17:00:24 +0100642/*
643 * "randomly" fill the buffer contents
644 */
Jens Axboee9459e52007-04-17 15:46:32 +0200645static void fill_io_buf(struct thread_data *td, struct io_u *io_u, int max_bs)
Jens Axboe6b9cea22006-10-30 17:00:24 +0100646{
Jens Axboe210f43b2007-04-17 19:52:18 +0200647 long *ptr = io_u->buf;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100648
Jens Axboee9459e52007-04-17 15:46:32 +0200649 if (!td->o.zero_buffers) {
650 while ((void *) ptr - io_u->buf < max_bs) {
Jens Axboe210f43b2007-04-17 19:52:18 +0200651 *ptr = rand() * GOLDEN_RATIO_PRIME;
Jens Axboee9459e52007-04-17 15:46:32 +0200652 ptr++;
653 }
654 } else
655 memset(ptr, 0, max_bs);
Jens Axboe6b9cea22006-10-30 17:00:24 +0100656}
657
Jens Axboeebac4652005-12-08 15:25:21 +0100658static int init_io_u(struct thread_data *td)
659{
660 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100661 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100662 int i, max_units;
663 char *p;
664
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100665 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100666 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100667 td->orig_buffer_size = (unsigned long long) max_bs
668 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100669
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100670 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
671 unsigned long bs;
672
673 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
674 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
675 }
Jens Axboeebac4652005-12-08 15:25:21 +0100676
Jens Axboec3990642007-07-19 10:17:09 +0200677 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
678 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
679 return 1;
680 }
681
Jens Axboe2f9ade32006-10-20 11:25:52 +0200682 if (allocate_io_mem(td))
683 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100684
Jens Axboeb3f378e2007-07-20 12:39:22 +0200685 if (td->o.odirect)
686 p = ALIGN(td->orig_buffer);
687 else
688 p = td->orig_buffer;
689
Jens Axboeebac4652005-12-08 15:25:21 +0100690 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200691 if (td->terminate)
692 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100693 io_u = malloc(sizeof(*io_u));
694 memset(io_u, 0, sizeof(*io_u));
695 INIT_LIST_HEAD(&io_u->list);
696
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200697 if (!(td->io_ops->flags & FIO_NOIO)) {
698 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200699
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200700 if (td_write(td))
701 fill_io_buf(td, io_u, max_bs);
702 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100703
Jens Axboeb1ff3402006-02-17 11:35:58 +0100704 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100705 io_u->flags = IO_U_F_FREE;
Jens Axboeebac4652005-12-08 15:25:21 +0100706 list_add(&io_u->list, &td->io_u_freelist);
707 }
708
Jens Axboe433afcb2007-02-22 10:39:01 +0100709 io_u_init_timeout();
710
Jens Axboeebac4652005-12-08 15:25:21 +0100711 return 0;
712}
713
Jens Axboeda867742006-06-06 10:39:10 -0700714static int switch_ioscheduler(struct thread_data *td)
715{
716 char tmp[256], tmp2[128];
717 FILE *f;
718 int ret;
719
Jens Axboeba0fbe12007-03-09 14:34:23 +0100720 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200721 return 0;
722
Jens Axboeda867742006-06-06 10:39:10 -0700723 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
724
725 f = fopen(tmp, "r+");
726 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200727 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100728 log_err("fio: os or kernel doesn't support IO scheduler"
729 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200730 return 0;
731 }
732 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700733 return 1;
734 }
735
736 /*
737 * Set io scheduler.
738 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100739 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700740 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100741 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700742 fclose(f);
743 return 1;
744 }
745
746 rewind(f);
747
748 /*
749 * Read back and check that the selected scheduler is now the default.
750 */
751 ret = fread(tmp, 1, sizeof(tmp), f);
752 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100753 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700754 fclose(f);
755 return 1;
756 }
757
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100758 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700759 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100760 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100761 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700762 fclose(f);
763 return 1;
764 }
765
766 fclose(f);
767 return 0;
768}
769
Jens Axboe492158c2007-05-24 10:32:47 +0200770static int keep_running(struct thread_data *td)
771{
772 unsigned long long io_done;
773
Jens Axboe20e354e2007-07-23 14:36:16 +0200774 if (td->done)
775 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200776 if (td->o.time_based)
777 return 1;
778 if (td->o.loops) {
779 td->o.loops--;
780 return 1;
781 }
782
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100783 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
784 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200785 if (io_done < td->o.size)
786 return 1;
787
788 return 0;
789}
790
Jens Axboea978ba62007-03-08 14:29:03 +0100791static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100792{
Jens Axboe53cdc682006-10-18 11:50:58 +0200793 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100794 unsigned int i;
795 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100796
Jens Axboe756867b2007-03-06 15:19:24 +0100797 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100798 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100799 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100800 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100801 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100802 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100803
Jens Axboec1324df2007-02-19 19:06:41 +0100804 td->last_was_sync = 0;
805
Jens Axboe2ba1c292008-02-01 13:16:38 +0100806 /*
807 * reset file done count if we are to start over
808 */
809 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200810 td->nr_done_files = 0;
Jens Axboe281f9b62007-05-23 09:34:42 +0200811
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100812 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100813
814 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200815 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200816 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100817 ret = td_io_open_file(td, f);
818 if (ret)
819 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200820 }
Jens Axboea978ba62007-03-08 14:29:03 +0100821
822 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100823}
824
Jens Axboe906c8d72006-06-13 09:37:56 +0200825/*
826 * Entry point for the thread based jobs. The process based jobs end up
827 * here as well, after a little setup.
828 */
Jens Axboeebac4652005-12-08 15:25:21 +0100829static void *thread_main(void *data)
830{
Shawn Lewis10927312007-11-21 09:38:13 +0100831 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100832 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100833 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100834
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100835 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100836 setsid();
837
838 td->pid = getpid();
839
Jens Axboeee56ad52008-02-01 10:30:20 +0100840 dprint(FD_PROCESS, "jobs pid=%d started\n", td->pid);
841
Jens Axboeaea47d42006-05-26 19:27:29 +0200842 INIT_LIST_HEAD(&td->io_u_freelist);
843 INIT_LIST_HEAD(&td->io_u_busylist);
Jens Axboe755200a2007-02-19 13:08:12 +0100844 INIT_LIST_HEAD(&td->io_u_requeues);
Jens Axboeaea47d42006-05-26 19:27:29 +0200845 INIT_LIST_HEAD(&td->io_log_list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200846 INIT_LIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200847 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200848
Jens Axboe75154842006-06-01 13:56:09 +0200849 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100850 fio_mutex_up(startup_mutex);
851 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100852
Jens Axboe37f56872007-03-09 12:42:35 +0100853 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100854 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100855 * eating a file descriptor
856 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100857 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100858
Jens Axboed84f8d42007-05-16 08:47:46 +0200859 /*
860 * May alter parameters that init_io_u() will use, so we need to
861 * do this first.
862 */
863 if (init_iolog(td))
864 goto err;
865
Jens Axboedb1cd902007-04-26 13:47:07 +0200866 if (init_io_u(td))
867 goto err;
868
Jens Axboe375b2692007-05-22 09:13:02 +0200869 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200870 td_verror(td, errno, "cpu_set_affinity");
871 goto err;
872 }
873
Jens Axboeac684782007-11-08 08:29:07 +0100874 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200875 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
876 td_verror(td, errno, "ioprio_set");
877 goto err;
878 }
879 }
880
881 if (nice(td->o.nice) == -1) {
882 td_verror(td, errno, "nice");
883 goto err;
884 }
885
886 if (td->o.ioscheduler && switch_ioscheduler(td))
887 goto err;
888
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100889 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100890 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100891
892 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100893 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100894
Jens Axboeb5af8292007-03-08 12:43:13 +0100895 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100896 goto err;
897
Jens Axboe68727072007-03-15 20:49:25 +0100898 if (init_random_map(td))
899 goto err;
900
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100901 if (td->o.exec_prerun) {
902 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100903 goto err;
904 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200905
Jens Axboe69008992006-11-24 13:12:56 +0100906 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100907 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100908 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100909
910 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100911 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200912 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100913 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100914 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100915
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100916 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100917 memcpy(&td->lastrate, &td->ts.stat_sample_time,
918 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100919
Jens Axboea978ba62007-03-08 14:29:03 +0100920 if (clear_state && clear_io_state(td))
921 break;
922
Jens Axboeebac4652005-12-08 15:25:21 +0100923 prune_io_piece_log(td);
924
Jens Axboeba0fbe12007-03-09 14:34:23 +0100925 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100926
Jens Axboea978ba62007-03-08 14:29:03 +0100927 clear_state = 1;
928
Jens Axboe38d77ca2007-03-21 14:20:20 +0100929 if (td_read(td) && td->io_bytes[DDIR_READ]) {
930 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100931 elapsed = utime_since(&td->start,
932 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100933 else
934 elapsed = utime_since_now(&td->start);
935
936 runtime[DDIR_READ] += elapsed;
937 }
938 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
939 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100940 elapsed = utime_since(&td->start,
941 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100942 else
943 elapsed = utime_since_now(&td->start);
944
945 runtime[DDIR_WRITE] += elapsed;
946 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100947
Jens Axboeebac4652005-12-08 15:25:21 +0100948 if (td->error || td->terminate)
949 break;
950
Shawn Lewise84c73a2007-08-02 22:19:32 +0200951 if (!td->o.do_verify ||
952 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +0200953 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +0100954 continue;
955
Jens Axboea978ba62007-03-08 14:29:03 +0100956 if (clear_io_state(td))
957 break;
958
Jens Axboe02bcaa82006-11-24 10:42:00 +0100959 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100960
961 do_verify(td);
962
Jens Axboe69008992006-11-24 13:12:56 +0100963 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100964
965 if (td->error || td->terminate)
966 break;
967 }
968
Jens Axboe36dff962006-11-24 13:29:20 +0100969 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +0200970 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
971 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +0100972 td->ts.total_run_time = mtime_since_now(&td->epoch);
973 td->ts.io_bytes[0] = td->io_bytes[0];
974 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +0100975
Jens Axboe756867b2007-03-06 15:19:24 +0100976 if (td->ts.bw_log)
977 finish_log(td, td->ts.bw_log, "bw");
978 if (td->ts.slat_log)
979 finish_log(td, td->ts.slat_log, "slat");
980 if (td->ts.clat_log)
981 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100982 if (td->o.exec_postrun) {
983 if (system(td->o.exec_postrun) < 0)
984 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100985 }
Jens Axboeebac4652005-12-08 15:25:21 +0100986
987 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100988 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100989
990err:
Jens Axboe5bf13a52007-02-17 01:51:47 +0100991 if (td->error)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100992 printf("fio: pid=%d, err=%d/%s\n", td->pid, td->error,
993 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100994 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200995 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100996 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +0200997
998 /*
999 * do this very late, it will log file closing as well
1000 */
1001 if (td->o.write_iolog_file)
1002 write_iolog_close(td);
1003
Jens Axboed23bb322007-03-23 15:57:56 +01001004 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001005 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001006 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001007}
1008
Jens Axboe906c8d72006-06-13 09:37:56 +02001009/*
1010 * We cannot pass the td data into a forked process, so attach the td and
1011 * pass it to the thread worker.
1012 */
Jens Axboea6418142007-02-17 04:47:18 +01001013static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001014{
1015 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001016 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001017
1018 data = shmat(shmid, NULL, 0);
1019 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001020 int __err = errno;
1021
Jens Axboeebac4652005-12-08 15:25:21 +01001022 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001023 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001024 }
1025
1026 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001027 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001028 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001029 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001030}
1031
Jens Axboe906c8d72006-06-13 09:37:56 +02001032/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001033 * Run over the job map and reap the threads that have exited, if any.
1034 */
Jens Axboeebac4652005-12-08 15:25:21 +01001035static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1036{
Jens Axboe34572e22006-10-20 09:33:18 +02001037 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001038 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001039
1040 /*
1041 * reap exited threads (TD_EXITED -> TD_REAPED)
1042 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001043 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001044 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001045 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001046
Jens Axboe84585002006-10-19 20:26:22 +02001047 /*
1048 * ->io_ops is NULL for a thread that has closed its
1049 * io engine
1050 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001051 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001052 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001053 else
1054 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001055
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001056 if (!td->pid) {
1057 pending++;
1058 continue;
1059 }
1060 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001061 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001062 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001063 if (td->runstate == TD_EXITED) {
1064 td_set_runstate(td, TD_REAPED);
1065 goto reaped;
1066 }
1067 continue;
1068 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001069
1070 flags = WNOHANG;
1071 if (td->runstate == TD_EXITED)
1072 flags = 0;
1073
1074 /*
1075 * check if someone quit or got killed in an unusual way
1076 */
1077 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001078 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001079 if (errno == ECHILD) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001080 log_err("fio: pid=%d disappeared %d\n", td->pid,
1081 td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001082 td_set_runstate(td, TD_REAPED);
1083 goto reaped;
1084 }
1085 perror("waitpid");
1086 } else if (ret == td->pid) {
1087 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001088 int sig = WTERMSIG(status);
1089
Jens Axboed9244942007-03-05 12:32:32 +01001090 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001091 log_err("fio: pid=%d, got signal=%d\n",
1092 td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001093 td_set_runstate(td, TD_REAPED);
1094 goto reaped;
1095 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001096 if (WIFEXITED(status)) {
1097 if (WEXITSTATUS(status) && !td->error)
1098 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001099
Jens Axboea2f77c92007-02-22 11:53:00 +01001100 td_set_runstate(td, TD_REAPED);
1101 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001102 }
1103 }
Jens Axboeebac4652005-12-08 15:25:21 +01001104
Jens Axboea2f77c92007-02-22 11:53:00 +01001105 /*
1106 * thread is not dead, continue
1107 */
gurudas paie48676b2007-04-11 12:44:27 +02001108 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001109 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001110reaped:
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001111 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001112 long ret;
1113
Jens Axboeee56ad52008-02-01 10:30:20 +01001114 dprint(FD_PROCESS, "joining tread %d\n", td->pid);
1115 if (pthread_join(td->thread, (void *) &ret)) {
1116 dprint(FD_PROCESS, "join failed %ld\n", ret);
Jens Axboe3707f452007-02-22 12:26:57 +01001117 perror("pthread_join");
Jens Axboeee56ad52008-02-01 10:30:20 +01001118 }
Jens Axboe3707f452007-02-22 12:26:57 +01001119 }
1120
Jens Axboeebac4652005-12-08 15:25:21 +01001121 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001122 (*m_rate) -= td->o.ratemin;
1123 (*t_rate) -= td->o.rate;
gurudas paie48676b2007-04-11 12:44:27 +02001124 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001125
1126 if (td->error)
1127 exit_value++;
Jens Axboeebac4652005-12-08 15:25:21 +01001128 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001129
Jens Axboe1f809d12007-10-25 18:34:02 +02001130 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001131 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001132}
1133
Jens Axboe906c8d72006-06-13 09:37:56 +02001134/*
1135 * Main function for kicking off and reaping jobs, as needed.
1136 */
Jens Axboeebac4652005-12-08 15:25:21 +01001137static void run_threads(void)
1138{
Jens Axboeebac4652005-12-08 15:25:21 +01001139 struct thread_data *td;
1140 unsigned long spent;
1141 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001142
Jens Axboe2f9ade32006-10-20 11:25:52 +02001143 if (fio_pin_memory())
1144 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001145
Jens Axboec6ae0a52006-06-12 11:04:44 +02001146 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001147 printf("Starting ");
1148 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001149 printf("%d thread%s", nr_thread,
1150 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001151 if (nr_process) {
1152 if (nr_thread)
1153 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001154 printf("%d process%s", nr_process,
1155 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001156 }
1157 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001158 fflush(stdout);
1159 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001160
Jens Axboe4efa9702006-05-31 11:56:49 +02001161 signal(SIGINT, sig_handler);
1162 signal(SIGALRM, sig_handler);
1163
Jens Axboeebac4652005-12-08 15:25:21 +01001164 todo = thread_number;
1165 nr_running = 0;
1166 nr_started = 0;
1167 m_rate = t_rate = 0;
1168
Jens Axboe34572e22006-10-20 09:33:18 +02001169 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001170 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001171
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001172 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001173 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001174 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001175 }
Jens Axboeebac4652005-12-08 15:25:21 +01001176
1177 /*
1178 * do file setup here so it happens sequentially,
1179 * we don't want X number of threads getting their
1180 * client data interspersed on disk
1181 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001182 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001183 exit_value++;
1184 if (td->error)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001185 log_err("fio: pid=%d, err=%d/%s\n", td->pid,
1186 td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001187 td_set_runstate(td, TD_REAPED);
1188 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001189 } else {
1190 struct fio_file *f;
1191 unsigned int i;
1192
1193 /*
1194 * for sharing to work, each job must always open
1195 * its own files. so close them, if we opened them
1196 * for creation
1197 */
1198 for_each_file(td, f, i)
1199 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001200 }
Jens Axboe380cf262007-01-11 14:01:27 +01001201
1202 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001203 }
1204
Jens Axboea2f77c92007-02-22 11:53:00 +01001205 set_genesis_time();
1206
Jens Axboeebac4652005-12-08 15:25:21 +01001207 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001208 struct thread_data *map[MAX_JOBS];
1209 struct timeval this_start;
1210 int this_jobs = 0, left;
1211
Jens Axboeebac4652005-12-08 15:25:21 +01001212 /*
1213 * create threads (TD_NOT_CREATED -> TD_CREATED)
1214 */
Jens Axboe34572e22006-10-20 09:33:18 +02001215 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001216 if (td->runstate != TD_NOT_CREATED)
1217 continue;
1218
1219 /*
1220 * never got a chance to start, killed by other
1221 * thread for some reason
1222 */
1223 if (td->terminate) {
1224 todo--;
1225 continue;
1226 }
1227
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001228 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001229 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001230
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001231 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001232 continue;
1233 }
1234
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001235 if (td->o.stonewall && (nr_started || nr_running))
Jens Axboeebac4652005-12-08 15:25:21 +01001236 break;
1237
Jens Axboe75154842006-06-01 13:56:09 +02001238 /*
1239 * Set state to created. Thread will transition
1240 * to TD_INITIALIZED when it's done setting up.
1241 */
Jens Axboeebac4652005-12-08 15:25:21 +01001242 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001243 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001244 nr_started++;
1245
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001246 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001247 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001248 if (pthread_create(&td->thread, NULL,
1249 thread_main, td)) {
Jens Axboeebac4652005-12-08 15:25:21 +01001250 perror("thread_create");
1251 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001252 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001253 }
1254 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001255 dprint(FD_PROCESS, "will fork\n");
Jens Axboe07739b52007-03-08 20:25:46 +01001256 if (!fork()) {
Jens Axboea6418142007-02-17 04:47:18 +01001257 int ret = fork_main(shm_id, i);
1258
1259 exit(ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001260 }
1261 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001262 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001263 }
1264
1265 /*
Jens Axboe75154842006-06-01 13:56:09 +02001266 * Wait for the started threads to transition to
1267 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001268 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001269 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001270 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001271 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001272 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1273 break;
1274
1275 usleep(100000);
1276
1277 for (i = 0; i < this_jobs; i++) {
1278 td = map[i];
1279 if (!td)
1280 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001281 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001282 map[i] = NULL;
1283 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001284 } else if (td->runstate >= TD_EXITED) {
1285 map[i] = NULL;
1286 left--;
1287 todo--;
1288 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001289 }
1290 }
1291 }
1292
1293 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001294 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001295 for (i = 0; i < this_jobs; i++) {
1296 td = map[i];
1297 if (!td)
1298 continue;
1299 kill(td->pid, SIGTERM);
1300 }
1301 break;
1302 }
1303
1304 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001305 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001306 */
Jens Axboe34572e22006-10-20 09:33:18 +02001307 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001308 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001309 continue;
1310
1311 td_set_runstate(td, TD_RUNNING);
1312 nr_running++;
1313 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001314 m_rate += td->o.ratemin;
1315 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001316 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001317 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001318 }
1319
1320 reap_threads(&nr_running, &t_rate, &m_rate);
1321
1322 if (todo)
1323 usleep(100000);
1324 }
1325
1326 while (nr_running) {
1327 reap_threads(&nr_running, &t_rate, &m_rate);
1328 usleep(10000);
1329 }
1330
1331 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001332 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001333}
1334
Jens Axboeebac4652005-12-08 15:25:21 +01001335int main(int argc, char *argv[])
1336{
Jens Axboe29d610e2007-02-06 13:25:33 +01001337 long ps;
1338
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001339 sinit();
1340
Jens Axboedbe11252007-02-11 00:19:51 +01001341 /*
1342 * We need locale for number printing, if it isn't set then just
1343 * go with the US format.
1344 */
1345 if (!getenv("LC_NUMERIC"))
1346 setlocale(LC_NUMERIC, "en_US");
1347
Jens Axboeebac4652005-12-08 15:25:21 +01001348 if (parse_options(argc, argv))
1349 return 1;
1350
Jens Axboe4b472fa2007-04-04 11:04:34 +02001351 if (!thread_number)
1352 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001353
Jens Axboe29d610e2007-02-06 13:25:33 +01001354 ps = sysconf(_SC_PAGESIZE);
1355 if (ps < 0) {
1356 log_err("Failed to get page size\n");
1357 return 1;
1358 }
1359
Jens Axboecfc99db2007-03-14 10:34:47 +01001360 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001361 page_mask = ps - 1;
1362
Jens Axboebb3884d2007-01-17 17:23:11 +11001363 if (write_bw_log) {
1364 setup_log(&agg_io_log[DDIR_READ]);
1365 setup_log(&agg_io_log[DDIR_WRITE]);
1366 }
1367
Jens Axboecdd18ad2008-02-27 18:58:00 +01001368 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001369
Jens Axboea2f77c92007-02-22 11:53:00 +01001370 set_genesis_time();
1371
Jens Axboeebac4652005-12-08 15:25:21 +01001372 disk_util_timer_arm();
1373
1374 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001375
Jens Axboebb3884d2007-01-17 17:23:11 +11001376 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001377 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001378 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001379 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1380 __finish_log(agg_io_log[DDIR_WRITE],
1381 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001382 }
1383 }
Jens Axboeebac4652005-12-08 15:25:21 +01001384
Jens Axboecdd18ad2008-02-27 18:58:00 +01001385 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001386 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001387}