blob: de6132c034a3daa04a64c0b4e60967ccbf99d331 [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 Axboebd6f78b2008-02-01 20:27:52 +010064 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", td->pid, td->runstate,
65 runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010066 td->runstate = runstate;
67}
68
Jens Axboe390c40e2007-03-05 12:35:29 +010069static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010070{
Jens Axboe34572e22006-10-20 09:33:18 +020071 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010072 int i;
73
Jens Axboe34572e22006-10-20 09:33:18 +020074 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010075 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboeee56ad52008-02-01 10:30:20 +010076 dprint(FD_PROCESS, "setting terminate on %d\n",td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010077
78 td->terminate = 1;
79 td->o.start_delay = 0;
80
Jens Axboe7a93ab12007-04-18 12:25:41 +020081 /*
82 * if the thread is running, just let it exit
83 */
84 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010085 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010086 else {
87 struct ioengine_ops *ops = td->io_ops;
88
89 if (ops && (ops->flags & FIO_SIGQUIT))
90 kill(td->pid, SIGQUIT);
91 }
Jens Axboeebac4652005-12-08 15:25:21 +010092 }
93 }
94}
95
96static void sig_handler(int sig)
97{
98 switch (sig) {
99 case SIGALRM:
100 update_io_ticks();
101 disk_util_timer_arm();
102 print_thread_status();
103 break;
104 default:
Jens Axboe6ce15a32007-01-12 09:04:41 +0100105 printf("\nfio: terminating on signal %d\n", sig);
Jens Axboeebac4652005-12-08 15:25:21 +0100106 fflush(stdout);
Jens Axboe390c40e2007-03-05 12:35:29 +0100107 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100108 break;
109 }
110}
111
Jens Axboe906c8d72006-06-13 09:37:56 +0200112/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200113 * Check if we are above the minimum rate given.
114 */
Jens Axboeebac4652005-12-08 15:25:21 +0100115static int check_min_rate(struct thread_data *td, struct timeval *now)
116{
Jens Axboe09042002007-02-23 10:56:22 +0100117 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100118 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100119 unsigned long spent;
120 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100121
122 /*
Jens Axboe780bf1a2007-02-28 11:13:49 +0100123 * No minimum rate set, always ok
124 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100125 if (!td->o.ratemin && !td->o.rate_iops_min)
Jens Axboe780bf1a2007-02-28 11:13:49 +0100126 return 0;
127
128 /*
Jens Axboeebac4652005-12-08 15:25:21 +0100129 * allow a 2 second settle period in the beginning
130 */
131 if (mtime_since(&td->start, now) < 2000)
132 return 0;
133
Jens Axboe4e991c22007-03-15 11:41:11 +0100134 if (td_read(td)) {
135 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100136 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100137 }
138 if (td_write(td)) {
139 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100140 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100141 }
Jens Axboe09042002007-02-23 10:56:22 +0100142
Jens Axboeebac4652005-12-08 15:25:21 +0100143 /*
144 * if rate blocks is set, sample is running
145 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100146 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100147 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100148 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100149 return 0;
150
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100151 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100152 /*
153 * check bandwidth specified rate
154 */
155 if (bytes < td->rate_bytes) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100156 log_err("%s: min rate %u not met\n", td->o.name, td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100157 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100158 } else {
159 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100160 if (rate < td->o.ratemin || bytes < td->rate_bytes) {
161 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 +0100162 return 1;
163 }
164 }
165 } else {
166 /*
167 * checks iops specified rate
168 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100169 if (iops < td->o.rate_iops) {
170 log_err("%s: min iops rate %u not met\n", td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100171 return 1;
172 } else {
173 rate = (iops - td->rate_blocks) / spent;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100174 if (rate < td->o.rate_iops_min || iops < td->rate_blocks) {
175 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 +0100176 }
Jens Axboe413dd452007-02-23 09:26:09 +0100177 }
Jens Axboeebac4652005-12-08 15:25:21 +0100178 }
179 }
180
Jens Axboe09042002007-02-23 10:56:22 +0100181 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100182 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100183 memcpy(&td->lastrate, now, sizeof(*now));
184 return 0;
185}
186
187static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
188{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100189 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100190 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100191 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100192 return 1;
193
194 return 0;
195}
196
Jens Axboe906c8d72006-06-13 09:37:56 +0200197/*
198 * When job exits, we can cancel the in-flight IO if we are using async
199 * io. Attempt to do so.
200 */
Jens Axboeebac4652005-12-08 15:25:21 +0100201static void cleanup_pending_aio(struct thread_data *td)
202{
Jens Axboeebac4652005-12-08 15:25:21 +0100203 struct list_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100204 struct io_u *io_u;
205 int r;
206
207 /*
208 * get immediately available events, if any
209 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100210 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100211 if (r < 0)
212 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100213
214 /*
215 * now cancel remaining active events
216 */
Jens Axboe2866c822006-10-09 15:57:48 +0200217 if (td->io_ops->cancel) {
Jens Axboeebac4652005-12-08 15:25:21 +0100218 list_for_each_safe(entry, n, &td->io_u_busylist) {
219 io_u = list_entry(entry, struct io_u, list);
220
Jens Axboe0c6e7512007-02-22 11:19:39 +0100221 /*
222 * if the io_u isn't in flight, then that generally
223 * means someone leaked an io_u. complain but fix
224 * it up, so we don't stall here.
225 */
226 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
227 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100228 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100229 } else {
230 r = td->io_ops->cancel(td, io_u);
231 if (!r)
232 put_io_u(td, io_u);
233 }
Jens Axboeebac4652005-12-08 15:25:21 +0100234 }
235 }
236
Jens Axboe97601022007-02-18 12:47:29 +0100237 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100238 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100239}
240
Jens Axboe906c8d72006-06-13 09:37:56 +0200241/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200242 * Helper to handle the final sync of a file. Works just like the normal
243 * io path, just does everything sync.
244 */
245static int fio_io_sync(struct thread_data *td, struct fio_file *f)
246{
247 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200248 int ret;
249
250 if (!io_u)
251 return 1;
252
253 io_u->ddir = DDIR_SYNC;
254 io_u->file = f;
255
256 if (td_io_prep(td, io_u)) {
257 put_io_u(td, io_u);
258 return 1;
259 }
260
Jens Axboe755200a2007-02-19 13:08:12 +0100261requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200262 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100263 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100264 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200265 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200266 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100267 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100268 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100269 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100270 } else if (ret == FIO_Q_COMPLETED) {
271 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100272 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100273 return 1;
274 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200275
Jens Axboed7762cf2007-02-23 12:34:57 +0100276 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100277 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100278 } else if (ret == FIO_Q_BUSY) {
279 if (td_io_commit(td))
280 return 1;
281 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200282 }
283
284 return 0;
285}
286
287/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100288 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200289 * reads the blocks back in, and checks the crc/md5 of the data.
290 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100291static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100292{
Jens Axboe53cdc682006-10-18 11:50:58 +0200293 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100294 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100295 int ret, min_events;
296 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200297
298 /*
299 * sync io first and invalidate cache, to make sure we really
300 * read from disk.
301 */
302 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100303 if (!(f->flags & FIO_FILE_OPEN))
304 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100305 if (fio_io_sync(td, f))
306 break;
307 if (file_invalidate_cache(td, f))
308 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200309 }
Jens Axboeebac4652005-12-08 15:25:21 +0100310
Jens Axboeb2fdda42007-02-20 20:52:51 +0100311 if (td->error)
312 return;
313
Jens Axboeebac4652005-12-08 15:25:21 +0100314 td_set_runstate(td, TD_VERIFYING);
315
Jens Axboe36167d82007-02-18 05:41:31 +0100316 io_u = NULL;
317 while (!td->terminate) {
Jens Axboe54517922007-03-05 10:06:06 +0100318 int ret2;
319
Jens Axboeebac4652005-12-08 15:25:21 +0100320 io_u = __get_io_u(td);
321 if (!io_u)
322 break;
323
Jens Axboe069c2912007-02-21 23:02:39 +0100324 if (runtime_exceeded(td, &io_u->start_time)) {
325 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200326 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200327 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100328 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200329
Jens Axboe069c2912007-02-21 23:02:39 +0100330 if (get_next_verify(td, io_u)) {
331 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100332 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100333 }
Jens Axboeebac4652005-12-08 15:25:21 +0100334
Jens Axboe069c2912007-02-21 23:02:39 +0100335 if (td_io_prep(td, io_u)) {
336 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100337 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100338 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100339
340 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100341
Jens Axboe11786802007-03-05 18:33:22 +0100342 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100343 switch (ret) {
344 case FIO_Q_COMPLETED:
345 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100346 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100347 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100348 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200349 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100350
Jens Axboe9e9d1642007-03-12 09:37:46 +0100351 /*
352 * zero read, fail
353 */
354 if (!bytes) {
355 td_verror(td, ENODATA, "full resid");
356 put_io_u(td, io_u);
357 break;
358 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200359
Jens Axboe36167d82007-02-18 05:41:31 +0100360 io_u->xfer_buflen = io_u->resid;
361 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200362 io_u->offset += bytes;
363
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;
488
Jens Axboe30061b92007-04-17 13:31:34 +0200489 td->ts.short_io_u[io_u->ddir]++;
490
Jens Axboed460eb32007-04-16 21:39:33 +0200491 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200492 goto sync_done;
493
Jens Axboe11786802007-03-05 18:33:22 +0100494 requeue_io_u(td, &io_u);
495 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200496sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100497 fio_gettime(&comp_time, NULL);
498 bytes_done = io_u_sync_complete(td, io_u);
499 if (bytes_done < 0)
500 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100501 }
Jens Axboe36167d82007-02-18 05:41:31 +0100502 break;
503 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100504 /*
505 * if the engine doesn't have a commit hook,
506 * the io_u is really queued. if it does have such
507 * a hook, it has to call io_u_queued() itself.
508 */
509 if (td->io_ops->commit == NULL)
510 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100511 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100512 case FIO_Q_BUSY:
513 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100514 ret2 = td_io_commit(td);
515 if (ret2 < 0)
516 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100517 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100518 default:
519 assert(ret < 0);
520 put_io_u(td, io_u);
521 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100522 }
523
Jens Axboe99784632007-02-19 10:06:17 +0100524 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100525 break;
526
Jens Axboe97601022007-02-18 12:47:29 +0100527 /*
528 * See if we need to complete some commands
529 */
Jens Axboe755200a2007-02-19 13:08:12 +0100530 if (ret == FIO_Q_QUEUED || ret == FIO_Q_BUSY) {
Jens Axboe97601022007-02-18 12:47:29 +0100531 min_evts = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100532 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe36167d82007-02-18 05:41:31 +0100533 min_evts = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100534
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100535 if (td->cur_depth > td->o.iodepth_low)
536 min_evts = td->cur_depth - td->o.iodepth_low;
Jens Axboee916b392007-02-20 14:37:26 +0100537 }
538
Jens Axboe97601022007-02-18 12:47:29 +0100539 fio_gettime(&comp_time, NULL);
Jens Axboed7762cf2007-02-23 12:34:57 +0100540 bytes_done = io_u_queued_complete(td, min_evts);
Jens Axboe97601022007-02-18 12:47:29 +0100541 if (bytes_done < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100542 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100543 }
544
Jens Axboe97601022007-02-18 12:47:29 +0100545 if (!bytes_done)
546 continue;
547
Jens Axboeebac4652005-12-08 15:25:21 +0100548 /*
549 * the rate is batched for now, it should work for batches
550 * of completions except the very first one which may look
551 * a little bursty
552 */
Jens Axboe97601022007-02-18 12:47:29 +0100553 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100554
Jens Axboe413dd452007-02-23 09:26:09 +0100555 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100556
Jens Axboe97601022007-02-18 12:47:29 +0100557 if (check_min_rate(td, &comp_time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100558 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100559 terminate_threads(td->groupid);
Jens Axboee1161c32007-02-22 19:36:48 +0100560 td_verror(td, ENODATA, "check_min_rate");
Jens Axboeebac4652005-12-08 15:25:21 +0100561 break;
562 }
563
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100564 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100565 unsigned long long b;
566
567 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100568 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100569 int left;
570
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100571 if (td->o.thinktime_spin)
572 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100573
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100574 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100575 if (left)
576 usec_sleep(td, left);
577 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100578 }
Jens Axboeebac4652005-12-08 15:25:21 +0100579 }
580
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100581 if (td->o.fill_device && td->error == ENOSPC) {
582 td->error = 0;
583 td->terminate = 1;
584 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100585 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100586 struct fio_file *f;
587
Jens Axboec01c0392007-02-26 12:43:42 +0100588 i = td->cur_depth;
589 if (i)
590 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100591
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100592 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200593 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100594
595 for_each_file(td, f, i) {
596 if (!(f->flags & FIO_FILE_OPEN))
597 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200598 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100599 }
Jens Axboe84585002006-10-19 20:26:22 +0200600 }
Jens Axboec01c0392007-02-26 12:43:42 +0100601 } else
602 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100603
604 /*
605 * stop job if we failed doing any IO
606 */
607 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
608 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100609}
610
Jens Axboeebac4652005-12-08 15:25:21 +0100611static void cleanup_io_u(struct thread_data *td)
612{
613 struct list_head *entry, *n;
614 struct io_u *io_u;
615
616 list_for_each_safe(entry, n, &td->io_u_freelist) {
617 io_u = list_entry(entry, struct io_u, list);
618
619 list_del(&io_u->list);
620 free(io_u);
621 }
622
Jens Axboe2f9ade32006-10-20 11:25:52 +0200623 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100624}
625
Jens Axboe6b9cea22006-10-30 17:00:24 +0100626/*
627 * "randomly" fill the buffer contents
628 */
Jens Axboee9459e52007-04-17 15:46:32 +0200629static void fill_io_buf(struct thread_data *td, struct io_u *io_u, int max_bs)
Jens Axboe6b9cea22006-10-30 17:00:24 +0100630{
Jens Axboe210f43b2007-04-17 19:52:18 +0200631 long *ptr = io_u->buf;
Jens Axboe6b9cea22006-10-30 17:00:24 +0100632
Jens Axboee9459e52007-04-17 15:46:32 +0200633 if (!td->o.zero_buffers) {
634 while ((void *) ptr - io_u->buf < max_bs) {
Jens Axboe210f43b2007-04-17 19:52:18 +0200635 *ptr = rand() * GOLDEN_RATIO_PRIME;
Jens Axboee9459e52007-04-17 15:46:32 +0200636 ptr++;
637 }
638 } else
639 memset(ptr, 0, max_bs);
Jens Axboe6b9cea22006-10-30 17:00:24 +0100640}
641
Jens Axboeebac4652005-12-08 15:25:21 +0100642static int init_io_u(struct thread_data *td)
643{
644 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100645 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100646 int i, max_units;
647 char *p;
648
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100649 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100650 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboec3990642007-07-19 10:17:09 +0200651 td->orig_buffer_size = (unsigned long long) max_bs * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100652
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100653 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE)
654 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 +0100655
Jens Axboec3990642007-07-19 10:17:09 +0200656 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
657 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
658 return 1;
659 }
660
Jens Axboe2f9ade32006-10-20 11:25:52 +0200661 if (allocate_io_mem(td))
662 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100663
Jens Axboeb3f378e2007-07-20 12:39:22 +0200664 if (td->o.odirect)
665 p = ALIGN(td->orig_buffer);
666 else
667 p = td->orig_buffer;
668
Jens Axboeebac4652005-12-08 15:25:21 +0100669 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200670 if (td->terminate)
671 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100672 io_u = malloc(sizeof(*io_u));
673 memset(io_u, 0, sizeof(*io_u));
674 INIT_LIST_HEAD(&io_u->list);
675
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200676 if (!(td->io_ops->flags & FIO_NOIO)) {
677 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200678
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200679 if (td_write(td))
680 fill_io_buf(td, io_u, max_bs);
681 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100682
Jens Axboeb1ff3402006-02-17 11:35:58 +0100683 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100684 io_u->flags = IO_U_F_FREE;
Jens Axboeebac4652005-12-08 15:25:21 +0100685 list_add(&io_u->list, &td->io_u_freelist);
686 }
687
Jens Axboe433afcb2007-02-22 10:39:01 +0100688 io_u_init_timeout();
689
Jens Axboeebac4652005-12-08 15:25:21 +0100690 return 0;
691}
692
Jens Axboeda867742006-06-06 10:39:10 -0700693static int switch_ioscheduler(struct thread_data *td)
694{
695 char tmp[256], tmp2[128];
696 FILE *f;
697 int ret;
698
Jens Axboeba0fbe12007-03-09 14:34:23 +0100699 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200700 return 0;
701
Jens Axboeda867742006-06-06 10:39:10 -0700702 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
703
704 f = fopen(tmp, "r+");
705 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200706 if (errno == ENOENT) {
707 log_err("fio: os or kernel doesn't support IO scheduler switching\n");
708 return 0;
709 }
710 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700711 return 1;
712 }
713
714 /*
715 * Set io scheduler.
716 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100717 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700718 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100719 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700720 fclose(f);
721 return 1;
722 }
723
724 rewind(f);
725
726 /*
727 * Read back and check that the selected scheduler is now the default.
728 */
729 ret = fread(tmp, 1, sizeof(tmp), f);
730 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100731 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700732 fclose(f);
733 return 1;
734 }
735
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100736 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700737 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100738 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100739 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700740 fclose(f);
741 return 1;
742 }
743
744 fclose(f);
745 return 0;
746}
747
Jens Axboe492158c2007-05-24 10:32:47 +0200748static int keep_running(struct thread_data *td)
749{
750 unsigned long long io_done;
751
Jens Axboe20e354e2007-07-23 14:36:16 +0200752 if (td->done)
753 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200754 if (td->o.time_based)
755 return 1;
756 if (td->o.loops) {
757 td->o.loops--;
758 return 1;
759 }
760
Jens Axboe48f5abd2007-07-20 13:25:04 +0200761 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE] + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200762 if (io_done < td->o.size)
763 return 1;
764
765 return 0;
766}
767
Jens Axboea978ba62007-03-08 14:29:03 +0100768static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100769{
Jens Axboe53cdc682006-10-18 11:50:58 +0200770 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100771 unsigned int i;
772 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100773
Jens Axboe756867b2007-03-06 15:19:24 +0100774 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100775 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100776 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100777 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100778 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100779 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100780
Jens Axboec1324df2007-02-19 19:06:41 +0100781 td->last_was_sync = 0;
782
Jens Axboe2ba1c292008-02-01 13:16:38 +0100783 /*
784 * reset file done count if we are to start over
785 */
786 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200787 td->nr_done_files = 0;
Jens Axboe281f9b62007-05-23 09:34:42 +0200788
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100789 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100790
791 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200792 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200793 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100794 ret = td_io_open_file(td, f);
795 if (ret)
796 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200797 }
Jens Axboea978ba62007-03-08 14:29:03 +0100798
799 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100800}
801
Jens Axboe906c8d72006-06-13 09:37:56 +0200802/*
803 * Entry point for the thread based jobs. The process based jobs end up
804 * here as well, after a little setup.
805 */
Jens Axboeebac4652005-12-08 15:25:21 +0100806static void *thread_main(void *data)
807{
Shawn Lewis10927312007-11-21 09:38:13 +0100808 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100809 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100810 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100811
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100812 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100813 setsid();
814
815 td->pid = getpid();
816
Jens Axboeee56ad52008-02-01 10:30:20 +0100817 dprint(FD_PROCESS, "jobs pid=%d started\n", td->pid);
818
Jens Axboeaea47d42006-05-26 19:27:29 +0200819 INIT_LIST_HEAD(&td->io_u_freelist);
820 INIT_LIST_HEAD(&td->io_u_busylist);
Jens Axboe755200a2007-02-19 13:08:12 +0100821 INIT_LIST_HEAD(&td->io_u_requeues);
Jens Axboeaea47d42006-05-26 19:27:29 +0200822 INIT_LIST_HEAD(&td->io_log_list);
Jens Axboe8de8f042007-03-27 10:36:12 +0200823 INIT_LIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200824 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200825
Jens Axboe75154842006-06-01 13:56:09 +0200826 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100827 fio_mutex_up(startup_mutex);
828 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100829
Jens Axboe37f56872007-03-09 12:42:35 +0100830 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100831 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100832 * eating a file descriptor
833 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100834 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100835
Jens Axboed84f8d42007-05-16 08:47:46 +0200836 /*
837 * May alter parameters that init_io_u() will use, so we need to
838 * do this first.
839 */
840 if (init_iolog(td))
841 goto err;
842
Jens Axboedb1cd902007-04-26 13:47:07 +0200843 if (init_io_u(td))
844 goto err;
845
Jens Axboe375b2692007-05-22 09:13:02 +0200846 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200847 td_verror(td, errno, "cpu_set_affinity");
848 goto err;
849 }
850
Jens Axboeac684782007-11-08 08:29:07 +0100851 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200852 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
853 td_verror(td, errno, "ioprio_set");
854 goto err;
855 }
856 }
857
858 if (nice(td->o.nice) == -1) {
859 td_verror(td, errno, "nice");
860 goto err;
861 }
862
863 if (td->o.ioscheduler && switch_ioscheduler(td))
864 goto err;
865
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100866 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100867 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100868
869 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100870 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100871
Jens Axboeb5af8292007-03-08 12:43:13 +0100872 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100873 goto err;
874
Jens Axboe68727072007-03-15 20:49:25 +0100875 if (init_random_map(td))
876 goto err;
877
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100878 if (td->o.exec_prerun) {
879 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100880 goto err;
881 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200882
Jens Axboe69008992006-11-24 13:12:56 +0100883 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100884 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100885 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100886
887 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100888 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200889 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100890 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100891 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100892
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100893 if (td->o.ratemin)
Jens Axboe756867b2007-03-06 15:19:24 +0100894 memcpy(&td->lastrate, &td->ts.stat_sample_time, sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100895
Jens Axboea978ba62007-03-08 14:29:03 +0100896 if (clear_state && clear_io_state(td))
897 break;
898
Jens Axboeebac4652005-12-08 15:25:21 +0100899 prune_io_piece_log(td);
900
Jens Axboeba0fbe12007-03-09 14:34:23 +0100901 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100902
Jens Axboea978ba62007-03-08 14:29:03 +0100903 clear_state = 1;
904
Jens Axboe38d77ca2007-03-21 14:20:20 +0100905 if (td_read(td) && td->io_bytes[DDIR_READ]) {
906 if (td->rw_end_set[DDIR_READ])
907 elapsed = utime_since(&td->start, &td->rw_end[DDIR_READ]);
908 else
909 elapsed = utime_since_now(&td->start);
910
911 runtime[DDIR_READ] += elapsed;
912 }
913 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
914 if (td->rw_end_set[DDIR_WRITE])
915 elapsed = utime_since(&td->start, &td->rw_end[DDIR_WRITE]);
916 else
917 elapsed = utime_since_now(&td->start);
918
919 runtime[DDIR_WRITE] += elapsed;
920 }
Jens Axboe413dd452007-02-23 09:26:09 +0100921
Jens Axboeebac4652005-12-08 15:25:21 +0100922 if (td->error || td->terminate)
923 break;
924
Shawn Lewise84c73a2007-08-02 22:19:32 +0200925 if (!td->o.do_verify ||
926 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +0200927 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +0100928 continue;
929
Jens Axboea978ba62007-03-08 14:29:03 +0100930 if (clear_io_state(td))
931 break;
932
Jens Axboe02bcaa82006-11-24 10:42:00 +0100933 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100934
935 do_verify(td);
936
Jens Axboe69008992006-11-24 13:12:56 +0100937 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100938
939 if (td->error || td->terminate)
940 break;
941 }
942
Jens Axboe36dff962006-11-24 13:29:20 +0100943 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +0200944 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
945 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +0100946 td->ts.total_run_time = mtime_since_now(&td->epoch);
947 td->ts.io_bytes[0] = td->io_bytes[0];
948 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +0100949
Jens Axboe756867b2007-03-06 15:19:24 +0100950 if (td->ts.bw_log)
951 finish_log(td, td->ts.bw_log, "bw");
952 if (td->ts.slat_log)
953 finish_log(td, td->ts.slat_log, "slat");
954 if (td->ts.clat_log)
955 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100956 if (td->o.exec_postrun) {
957 if (system(td->o.exec_postrun) < 0)
958 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100959 }
Jens Axboeebac4652005-12-08 15:25:21 +0100960
961 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100962 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100963
964err:
Jens Axboe5bf13a52007-02-17 01:51:47 +0100965 if (td->error)
966 printf("fio: pid=%d, err=%d/%s\n", td->pid, td->error, td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100967 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200968 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100969 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +0200970
971 /*
972 * do this very late, it will log file closing as well
973 */
974 if (td->o.write_iolog_file)
975 write_iolog_close(td);
976
Jens Axboed23bb322007-03-23 15:57:56 +0100977 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100978 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +0100979 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +0100980}
981
Jens Axboe906c8d72006-06-13 09:37:56 +0200982/*
983 * We cannot pass the td data into a forked process, so attach the td and
984 * pass it to the thread worker.
985 */
Jens Axboea6418142007-02-17 04:47:18 +0100986static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +0100987{
988 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +0100989 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100990
991 data = shmat(shmid, NULL, 0);
992 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +0100993 int __err = errno;
994
Jens Axboeebac4652005-12-08 15:25:21 +0100995 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +0100996 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +0100997 }
998
999 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001000 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001001 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001002 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001003}
1004
Jens Axboe906c8d72006-06-13 09:37:56 +02001005/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001006 * Run over the job map and reap the threads that have exited, if any.
1007 */
Jens Axboeebac4652005-12-08 15:25:21 +01001008static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1009{
Jens Axboe34572e22006-10-20 09:33:18 +02001010 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001011 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001012
1013 /*
1014 * reap exited threads (TD_EXITED -> TD_REAPED)
1015 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001016 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001017 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001018 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001019
Jens Axboe84585002006-10-19 20:26:22 +02001020 /*
1021 * ->io_ops is NULL for a thread that has closed its
1022 * io engine
1023 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001024 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001025 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001026 else
1027 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001028
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001029 if (!td->pid) {
1030 pending++;
1031 continue;
1032 }
1033 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001034 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001035 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001036 if (td->runstate == TD_EXITED) {
1037 td_set_runstate(td, TD_REAPED);
1038 goto reaped;
1039 }
1040 continue;
1041 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001042
1043 flags = WNOHANG;
1044 if (td->runstate == TD_EXITED)
1045 flags = 0;
1046
1047 /*
1048 * check if someone quit or got killed in an unusual way
1049 */
1050 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001051 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001052 if (errno == ECHILD) {
1053 log_err("fio: pid=%d disappeared %d\n", td->pid, td->runstate);
1054 td_set_runstate(td, TD_REAPED);
1055 goto reaped;
1056 }
1057 perror("waitpid");
1058 } else if (ret == td->pid) {
1059 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001060 int sig = WTERMSIG(status);
1061
Jens Axboed9244942007-03-05 12:32:32 +01001062 if (sig != SIGQUIT)
1063 log_err("fio: pid=%d, got signal=%d\n", td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001064 td_set_runstate(td, TD_REAPED);
1065 goto reaped;
1066 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001067 if (WIFEXITED(status)) {
1068 if (WEXITSTATUS(status) && !td->error)
1069 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001070
Jens Axboea2f77c92007-02-22 11:53:00 +01001071 td_set_runstate(td, TD_REAPED);
1072 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001073 }
1074 }
Jens Axboeebac4652005-12-08 15:25:21 +01001075
Jens Axboea2f77c92007-02-22 11:53:00 +01001076 /*
1077 * thread is not dead, continue
1078 */
gurudas paie48676b2007-04-11 12:44:27 +02001079 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001080 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001081reaped:
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001082 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001083 long ret;
1084
Jens Axboeee56ad52008-02-01 10:30:20 +01001085 dprint(FD_PROCESS, "joining tread %d\n", td->pid);
1086 if (pthread_join(td->thread, (void *) &ret)) {
1087 dprint(FD_PROCESS, "join failed %ld\n", ret);
Jens Axboe3707f452007-02-22 12:26:57 +01001088 perror("pthread_join");
Jens Axboeee56ad52008-02-01 10:30:20 +01001089 }
Jens Axboe3707f452007-02-22 12:26:57 +01001090 }
1091
Jens Axboeebac4652005-12-08 15:25:21 +01001092 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001093 (*m_rate) -= td->o.ratemin;
1094 (*t_rate) -= td->o.rate;
gurudas paie48676b2007-04-11 12:44:27 +02001095 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001096
1097 if (td->error)
1098 exit_value++;
Jens Axboeebac4652005-12-08 15:25:21 +01001099 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001100
Jens Axboe1f809d12007-10-25 18:34:02 +02001101 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001102 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001103}
1104
Jens Axboe906c8d72006-06-13 09:37:56 +02001105/*
1106 * Main function for kicking off and reaping jobs, as needed.
1107 */
Jens Axboeebac4652005-12-08 15:25:21 +01001108static void run_threads(void)
1109{
Jens Axboeebac4652005-12-08 15:25:21 +01001110 struct thread_data *td;
1111 unsigned long spent;
1112 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001113
Jens Axboe2f9ade32006-10-20 11:25:52 +02001114 if (fio_pin_memory())
1115 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001116
Jens Axboec6ae0a52006-06-12 11:04:44 +02001117 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001118 printf("Starting ");
1119 if (nr_thread)
1120 printf("%d thread%s", nr_thread, nr_thread > 1 ? "s" : "");
1121 if (nr_process) {
1122 if (nr_thread)
1123 printf(" and ");
1124 printf("%d process%s", nr_process, nr_process > 1 ? "es" : "");
1125 }
1126 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001127 fflush(stdout);
1128 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001129
Jens Axboe4efa9702006-05-31 11:56:49 +02001130 signal(SIGINT, sig_handler);
1131 signal(SIGALRM, sig_handler);
1132
Jens Axboeebac4652005-12-08 15:25:21 +01001133 todo = thread_number;
1134 nr_running = 0;
1135 nr_started = 0;
1136 m_rate = t_rate = 0;
1137
Jens Axboe34572e22006-10-20 09:33:18 +02001138 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001139 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001140
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001141 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001142 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001143 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001144 }
Jens Axboeebac4652005-12-08 15:25:21 +01001145
1146 /*
1147 * do file setup here so it happens sequentially,
1148 * we don't want X number of threads getting their
1149 * client data interspersed on disk
1150 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001151 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001152 exit_value++;
1153 if (td->error)
1154 log_err("fio: pid=%d, err=%d/%s\n", td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001155 td_set_runstate(td, TD_REAPED);
1156 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001157 } else {
1158 struct fio_file *f;
1159 unsigned int i;
1160
1161 /*
1162 * for sharing to work, each job must always open
1163 * its own files. so close them, if we opened them
1164 * for creation
1165 */
1166 for_each_file(td, f, i)
1167 td_io_close_file(td, f);
1168 }
Jens Axboe380cf262007-01-11 14:01:27 +01001169
1170 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001171 }
1172
Jens Axboea2f77c92007-02-22 11:53:00 +01001173 set_genesis_time();
1174
Jens Axboeebac4652005-12-08 15:25:21 +01001175 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001176 struct thread_data *map[MAX_JOBS];
1177 struct timeval this_start;
1178 int this_jobs = 0, left;
1179
Jens Axboeebac4652005-12-08 15:25:21 +01001180 /*
1181 * create threads (TD_NOT_CREATED -> TD_CREATED)
1182 */
Jens Axboe34572e22006-10-20 09:33:18 +02001183 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001184 if (td->runstate != TD_NOT_CREATED)
1185 continue;
1186
1187 /*
1188 * never got a chance to start, killed by other
1189 * thread for some reason
1190 */
1191 if (td->terminate) {
1192 todo--;
1193 continue;
1194 }
1195
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001196 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001197 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001198
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001199 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001200 continue;
1201 }
1202
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001203 if (td->o.stonewall && (nr_started || nr_running))
Jens Axboeebac4652005-12-08 15:25:21 +01001204 break;
1205
Jens Axboe75154842006-06-01 13:56:09 +02001206 /*
1207 * Set state to created. Thread will transition
1208 * to TD_INITIALIZED when it's done setting up.
1209 */
Jens Axboeebac4652005-12-08 15:25:21 +01001210 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001211 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001212 nr_started++;
1213
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001214 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001215 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001216 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1217 perror("thread_create");
1218 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001219 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001220 }
1221 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001222 dprint(FD_PROCESS, "will fork\n");
Jens Axboe07739b52007-03-08 20:25:46 +01001223 if (!fork()) {
Jens Axboea6418142007-02-17 04:47:18 +01001224 int ret = fork_main(shm_id, i);
1225
1226 exit(ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001227 }
1228 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001229 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001230 }
1231
1232 /*
Jens Axboe75154842006-06-01 13:56:09 +02001233 * Wait for the started threads to transition to
1234 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001235 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001236 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001237 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001238 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001239 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1240 break;
1241
1242 usleep(100000);
1243
1244 for (i = 0; i < this_jobs; i++) {
1245 td = map[i];
1246 if (!td)
1247 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001248 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001249 map[i] = NULL;
1250 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001251 } else if (td->runstate >= TD_EXITED) {
1252 map[i] = NULL;
1253 left--;
1254 todo--;
1255 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001256 }
1257 }
1258 }
1259
1260 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001261 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001262 for (i = 0; i < this_jobs; i++) {
1263 td = map[i];
1264 if (!td)
1265 continue;
1266 kill(td->pid, SIGTERM);
1267 }
1268 break;
1269 }
1270
1271 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001272 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001273 */
Jens Axboe34572e22006-10-20 09:33:18 +02001274 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001275 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001276 continue;
1277
1278 td_set_runstate(td, TD_RUNNING);
1279 nr_running++;
1280 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001281 m_rate += td->o.ratemin;
1282 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001283 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001284 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001285 }
1286
1287 reap_threads(&nr_running, &t_rate, &m_rate);
1288
1289 if (todo)
1290 usleep(100000);
1291 }
1292
1293 while (nr_running) {
1294 reap_threads(&nr_running, &t_rate, &m_rate);
1295 usleep(10000);
1296 }
1297
1298 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001299 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001300}
1301
Jens Axboeebac4652005-12-08 15:25:21 +01001302int main(int argc, char *argv[])
1303{
Jens Axboe29d610e2007-02-06 13:25:33 +01001304 long ps;
1305
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001306 sinit();
1307
Jens Axboedbe11252007-02-11 00:19:51 +01001308 /*
1309 * We need locale for number printing, if it isn't set then just
1310 * go with the US format.
1311 */
1312 if (!getenv("LC_NUMERIC"))
1313 setlocale(LC_NUMERIC, "en_US");
1314
Jens Axboeebac4652005-12-08 15:25:21 +01001315 if (parse_options(argc, argv))
1316 return 1;
1317
Jens Axboe4b472fa2007-04-04 11:04:34 +02001318 if (!thread_number)
1319 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001320
Jens Axboe29d610e2007-02-06 13:25:33 +01001321 ps = sysconf(_SC_PAGESIZE);
1322 if (ps < 0) {
1323 log_err("Failed to get page size\n");
1324 return 1;
1325 }
1326
Jens Axboecfc99db2007-03-14 10:34:47 +01001327 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001328 page_mask = ps - 1;
1329
Jens Axboebb3884d2007-01-17 17:23:11 +11001330 if (write_bw_log) {
1331 setup_log(&agg_io_log[DDIR_READ]);
1332 setup_log(&agg_io_log[DDIR_WRITE]);
1333 }
1334
Jens Axboecdd18ad2008-02-27 18:58:00 +01001335 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001336
Jens Axboea2f77c92007-02-22 11:53:00 +01001337 set_genesis_time();
1338
Jens Axboeebac4652005-12-08 15:25:21 +01001339 disk_util_timer_arm();
1340
1341 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001342
Jens Axboebb3884d2007-01-17 17:23:11 +11001343 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001344 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001345 if (write_bw_log) {
1346 __finish_log(agg_io_log[DDIR_READ],"agg-read_bw.log");
1347 __finish_log(agg_io_log[DDIR_WRITE],"agg-write_bw.log");
1348 }
1349 }
Jens Axboeebac4652005-12-08 15:25:21 +01001350
Jens Axboecdd18ad2008-02-27 18:58:00 +01001351 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001352 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001353}