blob: f0566ed77f75f71b33634c3ba2cac7dbe4aa23eb [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
Jens Axboeaae22ca2006-09-05 10:46:22 +02005 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboeebac4652005-12-08 15:25:21 +01006 *
Jens Axboe8e9fe632006-10-24 15:11:09 +02007 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
9 *
Jens Axboeebac4652005-12-08 15:25:21 +010010 * This program is free software; you can redistribute it and/or modify
Jens Axboe8e9fe632006-10-24 15:11:09 +020011 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Jens Axboeebac4652005-12-08 15:25:21 +010013 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
Jens Axboeebac4652005-12-08 15:25:21 +010024#include <unistd.h>
25#include <fcntl.h>
26#include <string.h>
Jens Axboeebac4652005-12-08 15:25:21 +010027#include <signal.h>
28#include <time.h>
Jens Axboedbe11252007-02-11 00:19:51 +010029#include <locale.h>
Jens Axboe36167d82007-02-18 05:41:31 +010030#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010031#include <sys/stat.h>
32#include <sys/wait.h>
33#include <sys/ipc.h>
34#include <sys/shm.h>
Jens Axboeebac4652005-12-08 15:25:21 +010035#include <sys/mman.h>
36
37#include "fio.h"
Jens Axboe210f43b2007-04-17 19:52:18 +020038#include "hash.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010039#include "smalloc.h"
Jens Axboeebac4652005-12-08 15:25:21 +010040
Jens Axboecfc99db2007-03-14 10:34:47 +010041unsigned long page_mask;
42unsigned long page_size;
Jens Axboe29d610e2007-02-06 13:25:33 +010043#define ALIGN(buf) \
44 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010045
46int groupid = 0;
47int thread_number = 0;
Jens Axboe9cedf162007-03-12 11:29:30 +010048int nr_process = 0;
49int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010050int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020051int temp_stall_ts;
Jens Axboed3eeeab2008-04-06 12:19:05 +020052unsigned long done_secs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010053
Jens Axboecdd18ad2008-02-27 18:58:00 +010054static struct fio_mutex *startup_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010055static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010056static int exit_value;
Jens Axboeccb0fa22008-05-30 23:18:00 +020057static struct itimerval itimer;
Jens Axboeebac4652005-12-08 15:25:21 +010058
Jens Axboebb3884d2007-01-17 17:23:11 +110059struct io_log *agg_io_log[2];
60
Jens Axboeebac4652005-12-08 15:25:21 +010061#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020062#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010063
Jens Axboe6ce15a32007-01-12 09:04:41 +010064static inline void td_set_runstate(struct thread_data *td, int runstate)
65{
Jens Axboed8fab1b2008-03-06 10:25:17 +010066 if (td->runstate == runstate)
67 return;
68
Jens Axboe5921e802008-05-30 15:02:38 +020069 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
70 td->runstate, runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010071 td->runstate = runstate;
72}
73
Jens Axboe390c40e2007-03-05 12:35:29 +010074static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010075{
Jens Axboe34572e22006-10-20 09:33:18 +020076 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010077 int i;
78
Jens Axboe6f88bcb2008-03-19 10:29:07 +010079 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
80
Jens Axboe34572e22006-10-20 09:33:18 +020081 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010082 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +010083 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +020084 td->o.name, (int) td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010085 td->terminate = 1;
86 td->o.start_delay = 0;
87
Jens Axboe7a93ab12007-04-18 12:25:41 +020088 /*
89 * if the thread is running, just let it exit
90 */
91 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010092 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010093 else {
94 struct ioengine_ops *ops = td->io_ops;
95
96 if (ops && (ops->flags & FIO_SIGQUIT))
97 kill(td->pid, SIGQUIT);
98 }
Jens Axboeebac4652005-12-08 15:25:21 +010099 }
100 }
101}
102
Jens Axboeccb0fa22008-05-30 23:18:00 +0200103static void status_timer_arm(void)
104{
105 itimer.it_value.tv_sec = 0;
106 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
107 setitimer(ITIMER_REAL, &itimer, NULL);
108}
109
Jens Axboe697a6062008-05-31 00:04:45 +0200110static void sig_alrm(int sig)
Jens Axboeccb0fa22008-05-30 23:18:00 +0200111{
Jens Axboe697a6062008-05-31 00:04:45 +0200112 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100113 update_io_ticks();
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100114 print_thread_status();
Jens Axboeccb0fa22008-05-30 23:18:00 +0200115 status_timer_arm();
Jens Axboe697a6062008-05-31 00:04:45 +0200116 }
117}
118
119static void sig_int(int sig)
120{
121 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100122 printf("\nfio: terminating on signal %d\n", sig);
123 fflush(stdout);
124 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100125 }
126}
127
Jens Axboe697a6062008-05-31 00:04:45 +0200128static void set_sig_handlers(void)
129{
130 struct sigaction act;
131
132 memset(&act, 0, sizeof(act));
133 act.sa_handler = sig_alrm;
134 act.sa_flags = SA_RESTART;
135 sigaction(SIGALRM, &act, NULL);
136
137 memset(&act, 0, sizeof(act));
138 act.sa_handler = sig_int;
139 act.sa_flags = SA_RESTART;
140 sigaction(SIGINT, &act, NULL);
141}
142
143/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200144 * Check if we are above the minimum rate given.
145 */
Jens Axboeebac4652005-12-08 15:25:21 +0100146static int check_min_rate(struct thread_data *td, struct timeval *now)
147{
Jens Axboe09042002007-02-23 10:56:22 +0100148 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100149 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100150 unsigned long spent;
151 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100152
153 /*
Jens Axboe780bf1a2007-02-28 11:13:49 +0100154 * No minimum rate set, always ok
155 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100156 if (!td->o.ratemin && !td->o.rate_iops_min)
Jens Axboe780bf1a2007-02-28 11:13:49 +0100157 return 0;
158
159 /*
Jens Axboeebac4652005-12-08 15:25:21 +0100160 * allow a 2 second settle period in the beginning
161 */
162 if (mtime_since(&td->start, now) < 2000)
163 return 0;
164
Jens Axboe4e991c22007-03-15 11:41:11 +0100165 if (td_read(td)) {
166 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100167 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100168 }
169 if (td_write(td)) {
170 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100171 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100172 }
Jens Axboe09042002007-02-23 10:56:22 +0100173
Jens Axboeebac4652005-12-08 15:25:21 +0100174 /*
175 * if rate blocks is set, sample is running
176 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100177 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100178 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100179 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100180 return 0;
181
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100182 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100183 /*
184 * check bandwidth specified rate
185 */
186 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100187 log_err("%s: min rate %u not met\n", td->o.name,
188 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100189 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100190 } else {
191 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100192 if (rate < td->o.ratemin ||
193 bytes < td->rate_bytes) {
194 log_err("%s: min rate %u not met, got"
195 " %luKiB/sec\n", td->o.name,
196 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100197 return 1;
198 }
199 }
200 } else {
201 /*
202 * checks iops specified rate
203 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100204 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100205 log_err("%s: min iops rate %u not met\n",
206 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100207 return 1;
208 } else {
209 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100210 if (rate < td->o.rate_iops_min ||
211 iops < td->rate_blocks) {
212 log_err("%s: min iops rate %u not met,"
213 " got %lu\n", td->o.name,
214 td->o.rate_iops_min,
215 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100216 }
Jens Axboe413dd452007-02-23 09:26:09 +0100217 }
Jens Axboeebac4652005-12-08 15:25:21 +0100218 }
219 }
220
Jens Axboe09042002007-02-23 10:56:22 +0100221 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100222 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100223 memcpy(&td->lastrate, now, sizeof(*now));
224 return 0;
225}
226
227static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
228{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100229 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100230 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100231 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100232 return 1;
233
234 return 0;
235}
236
Jens Axboe906c8d72006-06-13 09:37:56 +0200237/*
238 * When job exits, we can cancel the in-flight IO if we are using async
239 * io. Attempt to do so.
240 */
Jens Axboeebac4652005-12-08 15:25:21 +0100241static void cleanup_pending_aio(struct thread_data *td)
242{
Jens Axboe01743ee2008-06-02 12:19:19 +0200243 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100244 struct io_u *io_u;
245 int r;
246
247 /*
248 * get immediately available events, if any
249 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100250 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100251 if (r < 0)
252 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100253
254 /*
255 * now cancel remaining active events
256 */
Jens Axboe2866c822006-10-09 15:57:48 +0200257 if (td->io_ops->cancel) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200258 flist_for_each_safe(entry, n, &td->io_u_busylist) {
259 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100260
Jens Axboe0c6e7512007-02-22 11:19:39 +0100261 /*
262 * if the io_u isn't in flight, then that generally
263 * means someone leaked an io_u. complain but fix
264 * it up, so we don't stall here.
265 */
266 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
267 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100268 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100269 } else {
270 r = td->io_ops->cancel(td, io_u);
271 if (!r)
272 put_io_u(td, io_u);
273 }
Jens Axboeebac4652005-12-08 15:25:21 +0100274 }
275 }
276
Jens Axboe97601022007-02-18 12:47:29 +0100277 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100278 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100279}
280
Jens Axboe906c8d72006-06-13 09:37:56 +0200281/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200282 * Helper to handle the final sync of a file. Works just like the normal
283 * io path, just does everything sync.
284 */
285static int fio_io_sync(struct thread_data *td, struct fio_file *f)
286{
287 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200288 int ret;
289
290 if (!io_u)
291 return 1;
292
293 io_u->ddir = DDIR_SYNC;
294 io_u->file = f;
295
296 if (td_io_prep(td, io_u)) {
297 put_io_u(td, io_u);
298 return 1;
299 }
300
Jens Axboe755200a2007-02-19 13:08:12 +0100301requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200302 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100303 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100304 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200305 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200306 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100307 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100308 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100309 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100310 } else if (ret == FIO_Q_COMPLETED) {
311 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100312 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100313 return 1;
314 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200315
Jens Axboed7762cf2007-02-23 12:34:57 +0100316 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100317 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100318 } else if (ret == FIO_Q_BUSY) {
319 if (td_io_commit(td))
320 return 1;
321 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200322 }
323
324 return 0;
325}
326
327/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100328 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200329 * reads the blocks back in, and checks the crc/md5 of the data.
330 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100331static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100332{
Jens Axboe53cdc682006-10-18 11:50:58 +0200333 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100334 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100335 int ret, min_events;
336 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200337
338 /*
339 * sync io first and invalidate cache, to make sure we really
340 * read from disk.
341 */
342 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100343 if (!(f->flags & FIO_FILE_OPEN))
344 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100345 if (fio_io_sync(td, f))
346 break;
347 if (file_invalidate_cache(td, f))
348 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200349 }
Jens Axboeebac4652005-12-08 15:25:21 +0100350
Jens Axboeb2fdda42007-02-20 20:52:51 +0100351 if (td->error)
352 return;
353
Jens Axboeebac4652005-12-08 15:25:21 +0100354 td_set_runstate(td, TD_VERIFYING);
355
Jens Axboe36167d82007-02-18 05:41:31 +0100356 io_u = NULL;
357 while (!td->terminate) {
Jens Axboe54517922007-03-05 10:06:06 +0100358 int ret2;
359
Jens Axboeebac4652005-12-08 15:25:21 +0100360 io_u = __get_io_u(td);
361 if (!io_u)
362 break;
363
Jens Axboe069c2912007-02-21 23:02:39 +0100364 if (runtime_exceeded(td, &io_u->start_time)) {
365 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200366 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200367 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100368 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200369
Jens Axboe069c2912007-02-21 23:02:39 +0100370 if (get_next_verify(td, io_u)) {
371 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100372 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100373 }
Jens Axboeebac4652005-12-08 15:25:21 +0100374
Jens Axboe069c2912007-02-21 23:02:39 +0100375 if (td_io_prep(td, io_u)) {
376 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100377 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100378 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100379
380 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100381
Jens Axboe11786802007-03-05 18:33:22 +0100382 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100383 switch (ret) {
384 case FIO_Q_COMPLETED:
385 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100386 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100387 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100388 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200389 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100390
Jens Axboe9e9d1642007-03-12 09:37:46 +0100391 /*
392 * zero read, fail
393 */
394 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200395 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100396 put_io_u(td, io_u);
397 break;
398 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200399
Jens Axboe36167d82007-02-18 05:41:31 +0100400 io_u->xfer_buflen = io_u->resid;
401 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200402 io_u->offset += bytes;
403
Jens Axboe30061b92007-04-17 13:31:34 +0200404 td->ts.short_io_u[io_u->ddir]++;
405
Jens Axboed460eb32007-04-16 21:39:33 +0200406 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200407 goto sync_done;
408
Jens Axboe11786802007-03-05 18:33:22 +0100409 requeue_io_u(td, &io_u);
410 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200411sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100412 ret = io_u_sync_complete(td, io_u);
413 if (ret < 0)
414 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100415 }
Jens Axboe36167d82007-02-18 05:41:31 +0100416 continue;
417 case FIO_Q_QUEUED:
418 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100419 case FIO_Q_BUSY:
420 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100421 ret2 = td_io_commit(td);
422 if (ret2 < 0)
423 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100424 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100425 default:
426 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100427 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100428 break;
429 }
430
Jens Axboe99784632007-02-19 10:06:17 +0100431 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100432 break;
433
Jens Axboe3af6ef32007-02-18 06:57:43 +0100434 /*
435 * if we can queue more, do so. but check if there are
436 * completed io_u's first.
437 */
Jens Axboe97601022007-02-18 12:47:29 +0100438 min_events = 0;
Jens Axboee916b392007-02-20 14:37:26 +0100439 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe838bc702008-05-22 13:08:23 +0200440 if (td->cur_depth >= td->o.iodepth_low)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100441 min_events = td->cur_depth - td->o.iodepth_low;
Jens Axboe838bc702008-05-22 13:08:23 +0200442 if (!min_events)
443 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100444 }
445
Jens Axboe3af6ef32007-02-18 06:57:43 +0100446 /*
447 * Reap required number of io units, if any, and do the
448 * verification on them through the callback handler
449 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100450 if (io_u_queued_complete(td, min_events) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100451 break;
452 }
453
Jens Axboec01c0392007-02-26 12:43:42 +0100454 if (!td->error) {
455 min_events = td->cur_depth;
456
457 if (min_events)
458 ret = io_u_queued_complete(td, min_events);
459 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100460 cleanup_pending_aio(td);
461
462 td_set_runstate(td, TD_RUNNING);
463}
464
Jens Axboe32cd46a2006-06-07 13:40:40 +0200465/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200466 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200467 * and reaps them, checking for rate and errors along the way.
468 */
Jens Axboeebac4652005-12-08 15:25:21 +0100469static void do_io(struct thread_data *td)
470{
Jens Axboe02bcaa82006-11-24 10:42:00 +0100471 struct timeval s;
Jens Axboeebac4652005-12-08 15:25:21 +0100472 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100473 unsigned int i;
474 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100475
Jens Axboe5853e5a2006-06-08 14:41:05 +0200476 td_set_runstate(td, TD_RUNNING);
477
Jens Axboe7bb48f82007-03-27 15:30:28 +0200478 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100479 struct timeval comp_time;
480 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200481 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100482 struct io_u *io_u;
Jens Axboe54517922007-03-05 10:06:06 +0100483 int ret2;
Jens Axboeebac4652005-12-08 15:25:21 +0100484
485 if (td->terminate)
486 break;
487
Jens Axboe3d7c3912007-02-19 13:16:12 +0100488 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100489 if (!io_u)
490 break;
491
492 memcpy(&s, &io_u->start_time, sizeof(s));
Jens Axboe97601022007-02-18 12:47:29 +0100493
494 if (runtime_exceeded(td, &s)) {
495 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200496 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100497 break;
498 }
Jens Axboe36167d82007-02-18 05:41:31 +0100499
Jens Axboeb67740d2007-07-26 12:22:30 +0200500 /*
501 * Add verification end_io handler, if asked to verify
502 * a previously written file.
503 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100504 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200505 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100506 td_set_runstate(td, TD_VERIFYING);
507 } else
508 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200509
Jens Axboe11786802007-03-05 18:33:22 +0100510 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100511 switch (ret) {
512 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100513 if (io_u->error)
514 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100515 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100516 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200517 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100518
Jens Axboe9e9d1642007-03-12 09:37:46 +0100519 /*
520 * zero read, fail
521 */
522 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200523 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100524 put_io_u(td, io_u);
525 break;
526 }
527
Jens Axboe36167d82007-02-18 05:41:31 +0100528 io_u->xfer_buflen = io_u->resid;
529 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200530 io_u->offset += bytes;
531
Jens Axboe30061b92007-04-17 13:31:34 +0200532 td->ts.short_io_u[io_u->ddir]++;
533
Jens Axboed460eb32007-04-16 21:39:33 +0200534 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200535 goto sync_done;
536
Jens Axboe11786802007-03-05 18:33:22 +0100537 requeue_io_u(td, &io_u);
538 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200539sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100540 fio_gettime(&comp_time, NULL);
541 bytes_done = io_u_sync_complete(td, io_u);
542 if (bytes_done < 0)
543 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100544 }
Jens Axboe36167d82007-02-18 05:41:31 +0100545 break;
546 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100547 /*
548 * if the engine doesn't have a commit hook,
549 * the io_u is really queued. if it does have such
550 * a hook, it has to call io_u_queued() itself.
551 */
552 if (td->io_ops->commit == NULL)
553 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100554 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100555 case FIO_Q_BUSY:
556 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100557 ret2 = td_io_commit(td);
558 if (ret2 < 0)
559 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100560 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100561 default:
562 assert(ret < 0);
563 put_io_u(td, io_u);
564 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100565 }
566
Jens Axboe99784632007-02-19 10:06:17 +0100567 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100568 break;
569
Jens Axboe97601022007-02-18 12:47:29 +0100570 /*
571 * See if we need to complete some commands
572 */
Jens Axboe838bc702008-05-22 13:08:23 +0200573 if (queue_full(td) || ret == FIO_Q_BUSY) {
Jens Axboe97601022007-02-18 12:47:29 +0100574 min_evts = 0;
Jens Axboe838bc702008-05-22 13:08:23 +0200575 if (td->cur_depth >= td->o.iodepth_low)
576 min_evts = td->cur_depth - td->o.iodepth_low;
577 if (!min_evts)
Jens Axboe36167d82007-02-18 05:41:31 +0100578 min_evts = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100579 fio_gettime(&comp_time, NULL);
Jens Axboed7762cf2007-02-23 12:34:57 +0100580 bytes_done = io_u_queued_complete(td, min_evts);
Jens Axboe97601022007-02-18 12:47:29 +0100581 if (bytes_done < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100582 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100583 }
584
Jens Axboe97601022007-02-18 12:47:29 +0100585 if (!bytes_done)
586 continue;
587
Jens Axboeebac4652005-12-08 15:25:21 +0100588 /*
589 * the rate is batched for now, it should work for batches
590 * of completions except the very first one which may look
591 * a little bursty
592 */
Jens Axboe97601022007-02-18 12:47:29 +0100593 usec = utime_since(&s, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100594
Jens Axboe413dd452007-02-23 09:26:09 +0100595 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100596
Jens Axboe97601022007-02-18 12:47:29 +0100597 if (check_min_rate(td, &comp_time)) {
Jens Axboe98aa62d2006-11-07 14:16:19 +0100598 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100599 terminate_threads(td->groupid);
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200600 td_verror(td, EIO, "check_min_rate");
Jens Axboeebac4652005-12-08 15:25:21 +0100601 break;
602 }
603
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100604 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100605 unsigned long long b;
606
607 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100608 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100609 int left;
610
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100611 if (td->o.thinktime_spin)
612 __usec_sleep(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100613
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100614 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100615 if (left)
616 usec_sleep(td, left);
617 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100618 }
Jens Axboeebac4652005-12-08 15:25:21 +0100619 }
620
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100621 if (td->o.fill_device && td->error == ENOSPC) {
622 td->error = 0;
623 td->terminate = 1;
624 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100625 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100626 struct fio_file *f;
627
Jens Axboec01c0392007-02-26 12:43:42 +0100628 i = td->cur_depth;
629 if (i)
630 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100631
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100632 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200633 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100634
635 for_each_file(td, f, i) {
636 if (!(f->flags & FIO_FILE_OPEN))
637 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200638 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100639 }
Jens Axboe84585002006-10-19 20:26:22 +0200640 }
Jens Axboec01c0392007-02-26 12:43:42 +0100641 } else
642 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100643
644 /*
645 * stop job if we failed doing any IO
646 */
647 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
648 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100649}
650
Jens Axboeebac4652005-12-08 15:25:21 +0100651static void cleanup_io_u(struct thread_data *td)
652{
Jens Axboe01743ee2008-06-02 12:19:19 +0200653 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100654 struct io_u *io_u;
655
Jens Axboe01743ee2008-06-02 12:19:19 +0200656 flist_for_each_safe(entry, n, &td->io_u_freelist) {
657 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100658
Jens Axboe01743ee2008-06-02 12:19:19 +0200659 flist_del(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100660 free(io_u);
661 }
662
Jens Axboe2f9ade32006-10-20 11:25:52 +0200663 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100664}
665
666static int init_io_u(struct thread_data *td)
667{
668 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100669 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100670 int i, max_units;
671 char *p;
672
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100673 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100674 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100675 td->orig_buffer_size = (unsigned long long) max_bs
676 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100677
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100678 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
679 unsigned long bs;
680
681 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
682 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
683 }
Jens Axboeebac4652005-12-08 15:25:21 +0100684
Jens Axboec3990642007-07-19 10:17:09 +0200685 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
686 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
687 return 1;
688 }
689
Jens Axboe2f9ade32006-10-20 11:25:52 +0200690 if (allocate_io_mem(td))
691 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100692
Jens Axboeb3f378e2007-07-20 12:39:22 +0200693 if (td->o.odirect)
694 p = ALIGN(td->orig_buffer);
695 else
696 p = td->orig_buffer;
697
Jens Axboeebac4652005-12-08 15:25:21 +0100698 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200699 if (td->terminate)
700 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100701 io_u = malloc(sizeof(*io_u));
702 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200703 INIT_FLIST_HEAD(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100704
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200705 if (!(td->io_ops->flags & FIO_NOIO)) {
706 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200707
Jens Axboe5973caf2008-05-21 19:52:35 +0200708 if (td_write(td) && !td->o.refill_buffers)
709 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200710 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100711
Jens Axboeb1ff3402006-02-17 11:35:58 +0100712 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100713 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200714 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100715 }
716
Jens Axboe433afcb2007-02-22 10:39:01 +0100717 io_u_init_timeout();
718
Jens Axboeebac4652005-12-08 15:25:21 +0100719 return 0;
720}
721
Jens Axboeda867742006-06-06 10:39:10 -0700722static int switch_ioscheduler(struct thread_data *td)
723{
724 char tmp[256], tmp2[128];
725 FILE *f;
726 int ret;
727
Jens Axboeba0fbe12007-03-09 14:34:23 +0100728 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200729 return 0;
730
Jens Axboeda867742006-06-06 10:39:10 -0700731 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
732
733 f = fopen(tmp, "r+");
734 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200735 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100736 log_err("fio: os or kernel doesn't support IO scheduler"
737 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200738 return 0;
739 }
740 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700741 return 1;
742 }
743
744 /*
745 * Set io scheduler.
746 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100747 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700748 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100749 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700750 fclose(f);
751 return 1;
752 }
753
754 rewind(f);
755
756 /*
757 * Read back and check that the selected scheduler is now the default.
758 */
759 ret = fread(tmp, 1, sizeof(tmp), f);
760 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100761 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700762 fclose(f);
763 return 1;
764 }
765
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100766 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700767 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100768 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100769 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700770 fclose(f);
771 return 1;
772 }
773
774 fclose(f);
775 return 0;
776}
777
Jens Axboe492158c2007-05-24 10:32:47 +0200778static int keep_running(struct thread_data *td)
779{
780 unsigned long long io_done;
781
Jens Axboe20e354e2007-07-23 14:36:16 +0200782 if (td->done)
783 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200784 if (td->o.time_based)
785 return 1;
786 if (td->o.loops) {
787 td->o.loops--;
788 return 1;
789 }
790
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100791 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
792 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200793 if (io_done < td->o.size)
794 return 1;
795
796 return 0;
797}
798
Jens Axboea978ba62007-03-08 14:29:03 +0100799static int clear_io_state(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100800{
Jens Axboe53cdc682006-10-18 11:50:58 +0200801 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100802 unsigned int i;
803 int ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100804
Jens Axboe756867b2007-03-06 15:19:24 +0100805 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100806 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100807 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100808 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100809 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100810 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100811
Jens Axboec1324df2007-02-19 19:06:41 +0100812 td->last_was_sync = 0;
813
Jens Axboe2ba1c292008-02-01 13:16:38 +0100814 /*
815 * reset file done count if we are to start over
816 */
817 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200818 td->nr_done_files = 0;
Jens Axboe281f9b62007-05-23 09:34:42 +0200819
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100820 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100821
822 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200823 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200824 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100825 ret = td_io_open_file(td, f);
826 if (ret)
827 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200828 }
Jens Axboea978ba62007-03-08 14:29:03 +0100829
830 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100831}
832
Jens Axboe906c8d72006-06-13 09:37:56 +0200833/*
834 * Entry point for the thread based jobs. The process based jobs end up
835 * here as well, after a little setup.
836 */
Jens Axboeebac4652005-12-08 15:25:21 +0100837static void *thread_main(void *data)
838{
Shawn Lewis10927312007-11-21 09:38:13 +0100839 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100840 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100841 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100842
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100843 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100844 setsid();
845
846 td->pid = getpid();
847
Jens Axboe5921e802008-05-30 15:02:38 +0200848 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100849
Jens Axboe01743ee2008-06-02 12:19:19 +0200850 INIT_FLIST_HEAD(&td->io_u_freelist);
851 INIT_FLIST_HEAD(&td->io_u_busylist);
852 INIT_FLIST_HEAD(&td->io_u_requeues);
853 INIT_FLIST_HEAD(&td->io_log_list);
854 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200855 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200856
Jens Axboe75154842006-06-01 13:56:09 +0200857 td_set_runstate(td, TD_INITIALIZED);
Jens Axboecdd18ad2008-02-27 18:58:00 +0100858 fio_mutex_up(startup_mutex);
859 fio_mutex_down(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +0100860
Jens Axboe37f56872007-03-09 12:42:35 +0100861 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100862 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100863 * eating a file descriptor
864 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100865 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100866
Jens Axboed84f8d42007-05-16 08:47:46 +0200867 /*
868 * May alter parameters that init_io_u() will use, so we need to
869 * do this first.
870 */
871 if (init_iolog(td))
872 goto err;
873
Jens Axboedb1cd902007-04-26 13:47:07 +0200874 if (init_io_u(td))
875 goto err;
876
Jens Axboe375b2692007-05-22 09:13:02 +0200877 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200878 td_verror(td, errno, "cpu_set_affinity");
879 goto err;
880 }
881
Jens Axboeac684782007-11-08 08:29:07 +0100882 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200883 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
884 td_verror(td, errno, "ioprio_set");
885 goto err;
886 }
887 }
888
889 if (nice(td->o.nice) == -1) {
890 td_verror(td, errno, "nice");
891 goto err;
892 }
893
894 if (td->o.ioscheduler && switch_ioscheduler(td))
895 goto err;
896
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100897 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100898 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +0100899
900 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +0100901 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +0100902
Jens Axboeb5af8292007-03-08 12:43:13 +0100903 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +0100904 goto err;
905
Jens Axboe68727072007-03-15 20:49:25 +0100906 if (init_random_map(td))
907 goto err;
908
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100909 if (td->o.exec_prerun) {
910 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100911 goto err;
912 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200913
Jens Axboe69008992006-11-24 13:12:56 +0100914 fio_gettime(&td->epoch, NULL);
Jens Axboe433afcb2007-02-22 10:39:01 +0100915 memcpy(&td->timeout_end, &td->epoch, sizeof(td->epoch));
Jens Axboe756867b2007-03-06 15:19:24 +0100916 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +0100917
918 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +0100919 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200920 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100921 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +0100922 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +0100923
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100924 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100925 memcpy(&td->lastrate, &td->ts.stat_sample_time,
926 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +0100927
Jens Axboea978ba62007-03-08 14:29:03 +0100928 if (clear_state && clear_io_state(td))
929 break;
930
Jens Axboeebac4652005-12-08 15:25:21 +0100931 prune_io_piece_log(td);
932
Jens Axboeba0fbe12007-03-09 14:34:23 +0100933 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100934
Jens Axboea978ba62007-03-08 14:29:03 +0100935 clear_state = 1;
936
Jens Axboe38d77ca2007-03-21 14:20:20 +0100937 if (td_read(td) && td->io_bytes[DDIR_READ]) {
938 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100939 elapsed = utime_since(&td->start,
940 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100941 else
942 elapsed = utime_since_now(&td->start);
943
944 runtime[DDIR_READ] += elapsed;
945 }
946 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
947 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100948 elapsed = utime_since(&td->start,
949 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +0100950 else
951 elapsed = utime_since_now(&td->start);
952
953 runtime[DDIR_WRITE] += elapsed;
954 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100955
Jens Axboeebac4652005-12-08 15:25:21 +0100956 if (td->error || td->terminate)
957 break;
958
Shawn Lewise84c73a2007-08-02 22:19:32 +0200959 if (!td->o.do_verify ||
960 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +0200961 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +0100962 continue;
963
Jens Axboea978ba62007-03-08 14:29:03 +0100964 if (clear_io_state(td))
965 break;
966
Jens Axboe02bcaa82006-11-24 10:42:00 +0100967 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100968
969 do_verify(td);
970
Jens Axboe69008992006-11-24 13:12:56 +0100971 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +0100972
973 if (td->error || td->terminate)
974 break;
975 }
976
Jens Axboe36dff962006-11-24 13:29:20 +0100977 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +0200978 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
979 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +0100980 td->ts.total_run_time = mtime_since_now(&td->epoch);
981 td->ts.io_bytes[0] = td->io_bytes[0];
982 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +0100983
Jens Axboe756867b2007-03-06 15:19:24 +0100984 if (td->ts.bw_log)
985 finish_log(td, td->ts.bw_log, "bw");
986 if (td->ts.slat_log)
987 finish_log(td, td->ts.slat_log, "slat");
988 if (td->ts.clat_log)
989 finish_log(td, td->ts.clat_log, "clat");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100990 if (td->o.exec_postrun) {
991 if (system(td->o.exec_postrun) < 0)
992 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +0100993 }
Jens Axboeebac4652005-12-08 15:25:21 +0100994
995 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +0100996 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +0100997
998err:
Jens Axboe5bf13a52007-02-17 01:51:47 +0100999 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001000 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001001 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001002 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001003 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001004 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001005
1006 /*
1007 * do this very late, it will log file closing as well
1008 */
1009 if (td->o.write_iolog_file)
1010 write_iolog_close(td);
1011
Jens Axboed23bb322007-03-23 15:57:56 +01001012 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001013 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001014 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001015}
1016
Jens Axboe906c8d72006-06-13 09:37:56 +02001017/*
1018 * We cannot pass the td data into a forked process, so attach the td and
1019 * pass it to the thread worker.
1020 */
Jens Axboea6418142007-02-17 04:47:18 +01001021static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001022{
1023 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001024 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001025
1026 data = shmat(shmid, NULL, 0);
1027 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001028 int __err = errno;
1029
Jens Axboeebac4652005-12-08 15:25:21 +01001030 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001031 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001032 }
1033
1034 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001035 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001036 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001037 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001038}
1039
Jens Axboe906c8d72006-06-13 09:37:56 +02001040/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001041 * Run over the job map and reap the threads that have exited, if any.
1042 */
Jens Axboeebac4652005-12-08 15:25:21 +01001043static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1044{
Jens Axboe34572e22006-10-20 09:33:18 +02001045 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001046 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001047
1048 /*
1049 * reap exited threads (TD_EXITED -> TD_REAPED)
1050 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001051 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001052 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001053 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001054
Jens Axboe84585002006-10-19 20:26:22 +02001055 /*
1056 * ->io_ops is NULL for a thread that has closed its
1057 * io engine
1058 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001059 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001060 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001061 else
1062 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001063
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001064 if (!td->pid) {
1065 pending++;
1066 continue;
1067 }
1068 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001069 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001070 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001071 if (td->runstate == TD_EXITED) {
1072 td_set_runstate(td, TD_REAPED);
1073 goto reaped;
1074 }
1075 continue;
1076 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001077
1078 flags = WNOHANG;
1079 if (td->runstate == TD_EXITED)
1080 flags = 0;
1081
1082 /*
1083 * check if someone quit or got killed in an unusual way
1084 */
1085 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001086 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001087 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001088 log_err("fio: pid=%d disappeared %d\n",
1089 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001090 td_set_runstate(td, TD_REAPED);
1091 goto reaped;
1092 }
1093 perror("waitpid");
1094 } else if (ret == td->pid) {
1095 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001096 int sig = WTERMSIG(status);
1097
Jens Axboed9244942007-03-05 12:32:32 +01001098 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001099 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001100 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001101 td_set_runstate(td, TD_REAPED);
1102 goto reaped;
1103 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001104 if (WIFEXITED(status)) {
1105 if (WEXITSTATUS(status) && !td->error)
1106 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001107
Jens Axboea2f77c92007-02-22 11:53:00 +01001108 td_set_runstate(td, TD_REAPED);
1109 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001110 }
1111 }
Jens Axboeebac4652005-12-08 15:25:21 +01001112
Jens Axboea2f77c92007-02-22 11:53:00 +01001113 /*
1114 * thread is not dead, continue
1115 */
gurudas paie48676b2007-04-11 12:44:27 +02001116 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001117 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001118reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001119 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001120 (*m_rate) -= td->o.ratemin;
1121 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001122 if (!td->pid)
1123 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001124
1125 if (td->error)
1126 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001127
1128 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001129 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001130
Jens Axboe1f809d12007-10-25 18:34:02 +02001131 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001132 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001133}
1134
Jens Axboe906c8d72006-06-13 09:37:56 +02001135/*
1136 * Main function for kicking off and reaping jobs, as needed.
1137 */
Jens Axboeebac4652005-12-08 15:25:21 +01001138static void run_threads(void)
1139{
Jens Axboeebac4652005-12-08 15:25:21 +01001140 struct thread_data *td;
1141 unsigned long spent;
1142 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001143
Jens Axboe2f9ade32006-10-20 11:25:52 +02001144 if (fio_pin_memory())
1145 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001146
Jens Axboec6ae0a52006-06-12 11:04:44 +02001147 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001148 printf("Starting ");
1149 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001150 printf("%d thread%s", nr_thread,
1151 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001152 if (nr_process) {
1153 if (nr_thread)
1154 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001155 printf("%d process%s", nr_process,
1156 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001157 }
1158 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001159 fflush(stdout);
1160 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001161
Jens Axboe697a6062008-05-31 00:04:45 +02001162 set_sig_handlers();
Jens Axboe4efa9702006-05-31 11:56:49 +02001163
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 Axboe5921e802008-05-30 15:02:38 +02001185 log_err("fio: pid=%d, err=%d/%s\n",
1186 (int) td->pid, 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 Axboe6f88bcb2008-03-19 10:29:07 +01001235 if (td->o.stonewall && (nr_started || nr_running)) {
1236 dprint(FD_PROCESS, "%s: stonewall wait\n",
1237 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001238 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001239 }
Jens Axboeebac4652005-12-08 15:25:21 +01001240
Jens Axboe75154842006-06-01 13:56:09 +02001241 /*
1242 * Set state to created. Thread will transition
1243 * to TD_INITIALIZED when it's done setting up.
1244 */
Jens Axboeebac4652005-12-08 15:25:21 +01001245 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001246 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001247 nr_started++;
1248
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001249 if (td->o.use_thread) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001250 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001251 if (pthread_create(&td->thread, NULL,
1252 thread_main, td)) {
Jens Axboef4705a72008-03-10 10:52:22 +01001253 perror("pthread_create");
Jens Axboeebac4652005-12-08 15:25:21 +01001254 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001255 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001256 }
Jens Axboef4705a72008-03-10 10:52:22 +01001257 if (pthread_detach(td->thread) < 0)
1258 perror("pthread_detach");
Jens Axboeebac4652005-12-08 15:25:21 +01001259 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001260 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001261 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001262 pid = fork();
1263 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001264 int ret = fork_main(shm_id, i);
1265
Jens Axboe5e1d3062008-05-23 11:55:53 +02001266 _exit(ret);
1267 } else if (i == fio_debug_jobno)
1268 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001269 }
Jens Axboecdd18ad2008-02-27 18:58:00 +01001270 fio_mutex_down(startup_mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001271 }
1272
1273 /*
Jens Axboe75154842006-06-01 13:56:09 +02001274 * Wait for the started threads to transition to
1275 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001276 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001277 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001278 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001279 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001280 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1281 break;
1282
1283 usleep(100000);
1284
1285 for (i = 0; i < this_jobs; i++) {
1286 td = map[i];
1287 if (!td)
1288 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001289 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001290 map[i] = NULL;
1291 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001292 } else if (td->runstate >= TD_EXITED) {
1293 map[i] = NULL;
1294 left--;
1295 todo--;
1296 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001297 }
1298 }
1299 }
1300
1301 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001302 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001303 for (i = 0; i < this_jobs; i++) {
1304 td = map[i];
1305 if (!td)
1306 continue;
1307 kill(td->pid, SIGTERM);
1308 }
1309 break;
1310 }
1311
1312 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001313 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001314 */
Jens Axboe34572e22006-10-20 09:33:18 +02001315 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001316 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001317 continue;
1318
1319 td_set_runstate(td, TD_RUNNING);
1320 nr_running++;
1321 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001322 m_rate += td->o.ratemin;
1323 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001324 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001325 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001326 }
1327
1328 reap_threads(&nr_running, &t_rate, &m_rate);
1329
1330 if (todo)
1331 usleep(100000);
1332 }
1333
1334 while (nr_running) {
1335 reap_threads(&nr_running, &t_rate, &m_rate);
1336 usleep(10000);
1337 }
1338
1339 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001340 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001341}
1342
Jens Axboeebac4652005-12-08 15:25:21 +01001343int main(int argc, char *argv[])
1344{
Jens Axboe29d610e2007-02-06 13:25:33 +01001345 long ps;
1346
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001347 sinit();
1348
Jens Axboedbe11252007-02-11 00:19:51 +01001349 /*
1350 * We need locale for number printing, if it isn't set then just
1351 * go with the US format.
1352 */
1353 if (!getenv("LC_NUMERIC"))
1354 setlocale(LC_NUMERIC, "en_US");
1355
Jens Axboeebac4652005-12-08 15:25:21 +01001356 if (parse_options(argc, argv))
1357 return 1;
1358
Jens Axboe4b472fa2007-04-04 11:04:34 +02001359 if (!thread_number)
1360 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001361
Jens Axboe29d610e2007-02-06 13:25:33 +01001362 ps = sysconf(_SC_PAGESIZE);
1363 if (ps < 0) {
1364 log_err("Failed to get page size\n");
1365 return 1;
1366 }
1367
Jens Axboecfc99db2007-03-14 10:34:47 +01001368 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001369 page_mask = ps - 1;
1370
Jens Axboebb3884d2007-01-17 17:23:11 +11001371 if (write_bw_log) {
1372 setup_log(&agg_io_log[DDIR_READ]);
1373 setup_log(&agg_io_log[DDIR_WRITE]);
1374 }
1375
Jens Axboecdd18ad2008-02-27 18:58:00 +01001376 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001377
Jens Axboea2f77c92007-02-22 11:53:00 +01001378 set_genesis_time();
1379
Jens Axboeccb0fa22008-05-30 23:18:00 +02001380 status_timer_arm();
Jens Axboeebac4652005-12-08 15:25:21 +01001381
1382 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001383
Jens Axboebb3884d2007-01-17 17:23:11 +11001384 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001385 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001386 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001387 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1388 __finish_log(agg_io_log[DDIR_WRITE],
1389 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001390 }
1391 }
Jens Axboeebac4652005-12-08 15:25:21 +01001392
Jens Axboecdd18ad2008-02-27 18:58:00 +01001393 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001394 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001395}