blob: 0ef039ddb03feef70c7976cf4438200a0c5f4ca3 [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 Axboebe4ecfd2008-12-08 14:10:52 +010058static pthread_t gtod_thread;
Jens Axboeebac4652005-12-08 15:25:21 +010059
Jens Axboebb3884d2007-01-17 17:23:11 +110060struct io_log *agg_io_log[2];
61
Jens Axboeebac4652005-12-08 15:25:21 +010062#define TERMINATE_ALL (-1)
Jens Axboe75154842006-06-01 13:56:09 +020063#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010064
Jens Axboeb29ee5b2008-09-11 10:17:26 +020065void td_set_runstate(struct thread_data *td, int runstate)
Jens Axboe6ce15a32007-01-12 09:04:41 +010066{
Jens Axboed8fab1b2008-03-06 10:25:17 +010067 if (td->runstate == runstate)
68 return;
69
Jens Axboe5921e802008-05-30 15:02:38 +020070 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
71 td->runstate, runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +010072 td->runstate = runstate;
73}
74
Jens Axboe390c40e2007-03-05 12:35:29 +010075static void terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +010076{
Jens Axboe34572e22006-10-20 09:33:18 +020077 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +010078 int i;
79
Jens Axboe6f88bcb2008-03-19 10:29:07 +010080 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
81
Jens Axboe34572e22006-10-20 09:33:18 +020082 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +010083 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +010084 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +020085 td->o.name, (int) td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +010086 td->terminate = 1;
87 td->o.start_delay = 0;
88
Jens Axboe7a93ab12007-04-18 12:25:41 +020089 /*
90 * if the thread is running, just let it exit
91 */
92 if (td->runstate < TD_RUNNING)
Jens Axboec1302d42007-03-05 20:18:31 +010093 kill(td->pid, SIGQUIT);
Jens Axboef63f7f32008-03-01 15:25:24 +010094 else {
95 struct ioengine_ops *ops = td->io_ops;
96
97 if (ops && (ops->flags & FIO_SIGQUIT))
98 kill(td->pid, SIGQUIT);
99 }
Jens Axboeebac4652005-12-08 15:25:21 +0100100 }
101 }
102}
103
Jens Axboeccb0fa22008-05-30 23:18:00 +0200104static void status_timer_arm(void)
105{
106 itimer.it_value.tv_sec = 0;
107 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
108 setitimer(ITIMER_REAL, &itimer, NULL);
109}
110
Jens Axboe02444ad2008-10-07 11:33:00 +0200111static void sig_alrm(int fio_unused sig)
Jens Axboeccb0fa22008-05-30 23:18:00 +0200112{
Jens Axboe697a6062008-05-31 00:04:45 +0200113 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100114 update_io_ticks();
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100115 print_thread_status();
Jens Axboeccb0fa22008-05-30 23:18:00 +0200116 status_timer_arm();
Jens Axboe697a6062008-05-31 00:04:45 +0200117 }
118}
119
Jens Axboe5f8f9722008-11-18 19:10:21 +0100120/*
121 * Happens on thread runs with ctrl-c, ignore our own SIGQUIT
122 */
123static void sig_quit(int sig)
124{
125}
126
Jens Axboe697a6062008-05-31 00:04:45 +0200127static void sig_int(int sig)
128{
129 if (threads) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100130 printf("\nfio: terminating on signal %d\n", sig);
131 fflush(stdout);
132 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100133 }
134}
135
Jens Axboe02444ad2008-10-07 11:33:00 +0200136static void sig_ill(int fio_unused sig)
Jens Axboede79c912008-08-04 21:43:55 +0200137{
138 if (!threads)
139 return;
140
141 log_err("fio: illegal instruction. your cpu does not support "
142 "the sse4.2 instruction for crc32c\n");
143 terminate_threads(TERMINATE_ALL);
144 exit(4);
145}
146
Jens Axboe697a6062008-05-31 00:04:45 +0200147static void set_sig_handlers(void)
148{
149 struct sigaction act;
150
151 memset(&act, 0, sizeof(act));
152 act.sa_handler = sig_alrm;
153 act.sa_flags = SA_RESTART;
154 sigaction(SIGALRM, &act, NULL);
155
156 memset(&act, 0, sizeof(act));
157 act.sa_handler = sig_int;
158 act.sa_flags = SA_RESTART;
159 sigaction(SIGINT, &act, NULL);
Jens Axboede79c912008-08-04 21:43:55 +0200160
161 memset(&act, 0, sizeof(act));
162 act.sa_handler = sig_ill;
163 act.sa_flags = SA_RESTART;
164 sigaction(SIGILL, &act, NULL);
Jens Axboe5f8f9722008-11-18 19:10:21 +0100165
166 memset(&act, 0, sizeof(act));
167 act.sa_handler = sig_quit;
168 act.sa_flags = SA_RESTART;
169 sigaction(SIGQUIT, &act, NULL);
Jens Axboe697a6062008-05-31 00:04:45 +0200170}
171
Jens Axboe993bf482008-11-14 13:04:53 +0100172static inline int should_check_rate(struct thread_data *td)
173{
174 /*
175 * No minimum rate set, always ok
176 */
177 if (!td->o.ratemin && !td->o.rate_iops_min)
178 return 0;
179
180 return 1;
181}
182
Jens Axboe697a6062008-05-31 00:04:45 +0200183/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200184 * Check if we are above the minimum rate given.
185 */
Jens Axboeebac4652005-12-08 15:25:21 +0100186static int check_min_rate(struct thread_data *td, struct timeval *now)
187{
Jens Axboe09042002007-02-23 10:56:22 +0100188 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100189 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100190 unsigned long spent;
191 unsigned long rate;
Jens Axboeebac4652005-12-08 15:25:21 +0100192
193 /*
194 * allow a 2 second settle period in the beginning
195 */
196 if (mtime_since(&td->start, now) < 2000)
197 return 0;
198
Jens Axboe4e991c22007-03-15 11:41:11 +0100199 if (td_read(td)) {
200 iops += td->io_blocks[DDIR_READ];
Jens Axboe09042002007-02-23 10:56:22 +0100201 bytes += td->this_io_bytes[DDIR_READ];
Jens Axboe4e991c22007-03-15 11:41:11 +0100202 }
203 if (td_write(td)) {
204 iops += td->io_blocks[DDIR_WRITE];
Jens Axboe09042002007-02-23 10:56:22 +0100205 bytes += td->this_io_bytes[DDIR_WRITE];
Jens Axboe4e991c22007-03-15 11:41:11 +0100206 }
Jens Axboe09042002007-02-23 10:56:22 +0100207
Jens Axboeebac4652005-12-08 15:25:21 +0100208 /*
209 * if rate blocks is set, sample is running
210 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100211 if (td->rate_bytes || td->rate_blocks) {
Jens Axboeebac4652005-12-08 15:25:21 +0100212 spent = mtime_since(&td->lastrate, now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100213 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100214 return 0;
215
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100216 if (td->o.rate) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100217 /*
218 * check bandwidth specified rate
219 */
220 if (bytes < td->rate_bytes) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100221 log_err("%s: min rate %u not met\n", td->o.name,
222 td->o.ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100223 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100224 } else {
225 rate = (bytes - td->rate_bytes) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100226 if (rate < td->o.ratemin ||
227 bytes < td->rate_bytes) {
228 log_err("%s: min rate %u not met, got"
229 " %luKiB/sec\n", td->o.name,
230 td->o.ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100231 return 1;
232 }
233 }
234 } else {
235 /*
236 * checks iops specified rate
237 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100238 if (iops < td->o.rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100239 log_err("%s: min iops rate %u not met\n",
240 td->o.name, td->o.rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100241 return 1;
242 } else {
243 rate = (iops - td->rate_blocks) / spent;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100244 if (rate < td->o.rate_iops_min ||
245 iops < td->rate_blocks) {
246 log_err("%s: min iops rate %u not met,"
247 " got %lu\n", td->o.name,
248 td->o.rate_iops_min,
249 rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100250 }
Jens Axboe413dd452007-02-23 09:26:09 +0100251 }
Jens Axboeebac4652005-12-08 15:25:21 +0100252 }
253 }
254
Jens Axboe09042002007-02-23 10:56:22 +0100255 td->rate_bytes = bytes;
Jens Axboe4e991c22007-03-15 11:41:11 +0100256 td->rate_blocks = iops;
Jens Axboeebac4652005-12-08 15:25:21 +0100257 memcpy(&td->lastrate, now, sizeof(*now));
258 return 0;
259}
260
261static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
262{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100263 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100264 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100265 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100266 return 1;
267
268 return 0;
269}
270
Jens Axboe906c8d72006-06-13 09:37:56 +0200271/*
272 * When job exits, we can cancel the in-flight IO if we are using async
273 * io. Attempt to do so.
274 */
Jens Axboeebac4652005-12-08 15:25:21 +0100275static void cleanup_pending_aio(struct thread_data *td)
276{
Jens Axboe01743ee2008-06-02 12:19:19 +0200277 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100278 struct io_u *io_u;
279 int r;
280
281 /*
282 * get immediately available events, if any
283 */
Jens Axboed7762cf2007-02-23 12:34:57 +0100284 r = io_u_queued_complete(td, 0);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100285 if (r < 0)
286 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100287
288 /*
289 * now cancel remaining active events
290 */
Jens Axboe2866c822006-10-09 15:57:48 +0200291 if (td->io_ops->cancel) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200292 flist_for_each_safe(entry, n, &td->io_u_busylist) {
293 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100294
Jens Axboe0c6e7512007-02-22 11:19:39 +0100295 /*
296 * if the io_u isn't in flight, then that generally
297 * means someone leaked an io_u. complain but fix
298 * it up, so we don't stall here.
299 */
300 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
301 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100302 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100303 } else {
304 r = td->io_ops->cancel(td, io_u);
305 if (!r)
306 put_io_u(td, io_u);
307 }
Jens Axboeebac4652005-12-08 15:25:21 +0100308 }
309 }
310
Jens Axboe97601022007-02-18 12:47:29 +0100311 if (td->cur_depth)
Jens Axboed7762cf2007-02-23 12:34:57 +0100312 r = io_u_queued_complete(td, td->cur_depth);
Jens Axboeebac4652005-12-08 15:25:21 +0100313}
314
Jens Axboe906c8d72006-06-13 09:37:56 +0200315/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200316 * Helper to handle the final sync of a file. Works just like the normal
317 * io path, just does everything sync.
318 */
319static int fio_io_sync(struct thread_data *td, struct fio_file *f)
320{
321 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200322 int ret;
323
324 if (!io_u)
325 return 1;
326
327 io_u->ddir = DDIR_SYNC;
328 io_u->file = f;
329
330 if (td_io_prep(td, io_u)) {
331 put_io_u(td, io_u);
332 return 1;
333 }
334
Jens Axboe755200a2007-02-19 13:08:12 +0100335requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200336 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100337 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100338 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200339 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200340 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100341 } else if (ret == FIO_Q_QUEUED) {
Jens Axboed7762cf2007-02-23 12:34:57 +0100342 if (io_u_queued_complete(td, 1) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100343 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100344 } else if (ret == FIO_Q_COMPLETED) {
345 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100346 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100347 return 1;
348 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200349
Jens Axboed7762cf2007-02-23 12:34:57 +0100350 if (io_u_sync_complete(td, io_u) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100351 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100352 } else if (ret == FIO_Q_BUSY) {
353 if (td_io_commit(td))
354 return 1;
355 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200356 }
357
358 return 0;
359}
360
Jens Axboe993bf482008-11-14 13:04:53 +0100361static inline void update_tv_cache(struct thread_data *td)
362{
363 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
364 fio_gettime(&td->tv_cache, NULL);
365}
366
Jens Axboe858a3d42006-10-24 14:54:44 +0200367/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100368 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200369 * reads the blocks back in, and checks the crc/md5 of the data.
370 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100371static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100372{
Jens Axboe53cdc682006-10-18 11:50:58 +0200373 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100374 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100375 int ret, min_events;
376 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200377
378 /*
379 * sync io first and invalidate cache, to make sure we really
380 * read from disk.
381 */
382 for_each_file(td, f, i) {
Jens Axboe3d7b4852007-03-13 14:50:28 +0100383 if (!(f->flags & FIO_FILE_OPEN))
384 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100385 if (fio_io_sync(td, f))
386 break;
387 if (file_invalidate_cache(td, f))
388 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200389 }
Jens Axboeebac4652005-12-08 15:25:21 +0100390
Jens Axboeb2fdda42007-02-20 20:52:51 +0100391 if (td->error)
392 return;
393
Jens Axboeebac4652005-12-08 15:25:21 +0100394 td_set_runstate(td, TD_VERIFYING);
395
Jens Axboe36167d82007-02-18 05:41:31 +0100396 io_u = NULL;
397 while (!td->terminate) {
Jens Axboe49504212008-06-05 09:03:30 +0200398 int ret2, full;
Jens Axboe54517922007-03-05 10:06:06 +0100399
Jens Axboeebac4652005-12-08 15:25:21 +0100400 io_u = __get_io_u(td);
401 if (!io_u)
402 break;
403
Jens Axboe993bf482008-11-14 13:04:53 +0100404 update_tv_cache(td);
405
406 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe069c2912007-02-21 23:02:39 +0100407 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200408 td->terminate = 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200409 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100410 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200411
Jens Axboe069c2912007-02-21 23:02:39 +0100412 if (get_next_verify(td, io_u)) {
413 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100414 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100415 }
Jens Axboeebac4652005-12-08 15:25:21 +0100416
Jens Axboe069c2912007-02-21 23:02:39 +0100417 if (td_io_prep(td, io_u)) {
418 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100419 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100420 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100421
422 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100423
Jens Axboe11786802007-03-05 18:33:22 +0100424 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100425 switch (ret) {
426 case FIO_Q_COMPLETED:
427 if (io_u->error)
Jens Axboe22819ec2007-02-18 07:47:14 +0100428 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100429 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100430 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200431 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100432
Jens Axboe9e9d1642007-03-12 09:37:46 +0100433 /*
434 * zero read, fail
435 */
436 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200437 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100438 put_io_u(td, io_u);
439 break;
440 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200441
Jens Axboe36167d82007-02-18 05:41:31 +0100442 io_u->xfer_buflen = io_u->resid;
443 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200444 io_u->offset += bytes;
445
Jens Axboe30061b92007-04-17 13:31:34 +0200446 td->ts.short_io_u[io_u->ddir]++;
447
Jens Axboed460eb32007-04-16 21:39:33 +0200448 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200449 goto sync_done;
450
Jens Axboe11786802007-03-05 18:33:22 +0100451 requeue_io_u(td, &io_u);
452 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200453sync_done:
Jens Axboe11786802007-03-05 18:33:22 +0100454 ret = io_u_sync_complete(td, io_u);
455 if (ret < 0)
456 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100457 }
Jens Axboe36167d82007-02-18 05:41:31 +0100458 continue;
459 case FIO_Q_QUEUED:
460 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100461 case FIO_Q_BUSY:
462 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100463 ret2 = td_io_commit(td);
464 if (ret2 < 0)
465 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100466 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100467 default:
468 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100469 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100470 break;
471 }
472
Jens Axboe99784632007-02-19 10:06:17 +0100473 if (ret < 0 || td->error)
Jens Axboeebac4652005-12-08 15:25:21 +0100474 break;
475
Jens Axboe3af6ef32007-02-18 06:57:43 +0100476 /*
477 * if we can queue more, do so. but check if there are
478 * completed io_u's first.
479 */
Jens Axboe49504212008-06-05 09:03:30 +0200480 full = queue_full(td) || ret == FIO_Q_BUSY;
481 if (full || !td->o.iodepth_batch_complete) {
482 min_events = td->o.iodepth_batch_complete;
483 if (full && !min_events)
Jens Axboe838bc702008-05-22 13:08:23 +0200484 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100485
Jens Axboe49504212008-06-05 09:03:30 +0200486 do {
487 /*
488 * Reap required number of io units, if any,
489 * and do the verification on them through
490 * the callback handler
491 */
492 if (io_u_queued_complete(td, min_events) < 0) {
493 ret = -1;
494 break;
495 }
496 } while (full && (td->cur_depth > td->o.iodepth_low));
497 }
498 if (ret < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100499 break;
500 }
501
Jens Axboec01c0392007-02-26 12:43:42 +0100502 if (!td->error) {
503 min_events = td->cur_depth;
504
505 if (min_events)
506 ret = io_u_queued_complete(td, min_events);
507 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100508 cleanup_pending_aio(td);
509
510 td_set_runstate(td, TD_RUNNING);
511}
512
Jens Axboe32cd46a2006-06-07 13:40:40 +0200513/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200514 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200515 * and reaps them, checking for rate and errors along the way.
516 */
Jens Axboeebac4652005-12-08 15:25:21 +0100517static void do_io(struct thread_data *td)
518{
Jens Axboeebac4652005-12-08 15:25:21 +0100519 unsigned long usec;
Jens Axboeaf52b342007-03-13 10:07:47 +0100520 unsigned int i;
521 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100522
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200523 if (in_ramp_time(td))
524 td_set_runstate(td, TD_RAMP);
525 else
526 td_set_runstate(td, TD_RUNNING);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200527
Jens Axboe7bb48f82007-03-27 15:30:28 +0200528 while ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) {
Jens Axboe97601022007-02-18 12:47:29 +0100529 struct timeval comp_time;
530 long bytes_done = 0;
Jens Axboe84585002006-10-19 20:26:22 +0200531 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100532 struct io_u *io_u;
Jens Axboe49504212008-06-05 09:03:30 +0200533 int ret2, full;
Jens Axboeebac4652005-12-08 15:25:21 +0100534
535 if (td->terminate)
536 break;
537
Jens Axboe3d7c3912007-02-19 13:16:12 +0100538 io_u = get_io_u(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100539 if (!io_u)
540 break;
541
Jens Axboe993bf482008-11-14 13:04:53 +0100542 update_tv_cache(td);
Jens Axboe97601022007-02-18 12:47:29 +0100543
Jens Axboe993bf482008-11-14 13:04:53 +0100544 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe97601022007-02-18 12:47:29 +0100545 put_io_u(td, io_u);
Jens Axboe90a87d42007-04-18 09:02:32 +0200546 td->terminate = 1;
Jens Axboe97601022007-02-18 12:47:29 +0100547 break;
548 }
Jens Axboe36167d82007-02-18 05:41:31 +0100549
Jens Axboeb67740d2007-07-26 12:22:30 +0200550 /*
551 * Add verification end_io handler, if asked to verify
552 * a previously written file.
553 */
Jens Axboecc3fc852008-03-06 11:47:53 +0100554 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ) {
Jens Axboeb67740d2007-07-26 12:22:30 +0200555 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100556 td_set_runstate(td, TD_VERIFYING);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200557 } else if (in_ramp_time(td))
558 td_set_runstate(td, TD_RAMP);
559 else
Jens Axboed8fab1b2008-03-06 10:25:17 +0100560 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200561
Jens Axboe11786802007-03-05 18:33:22 +0100562 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100563 switch (ret) {
564 case FIO_Q_COMPLETED:
Jens Axboe54517922007-03-05 10:06:06 +0100565 if (io_u->error)
566 ret = -io_u->error;
Jens Axboe9e9d1642007-03-12 09:37:46 +0100567 else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100568 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200569 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100570
Jens Axboe9e9d1642007-03-12 09:37:46 +0100571 /*
572 * zero read, fail
573 */
574 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200575 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100576 put_io_u(td, io_u);
577 break;
578 }
579
Jens Axboe36167d82007-02-18 05:41:31 +0100580 io_u->xfer_buflen = io_u->resid;
581 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200582 io_u->offset += bytes;
583
Jens Axboe30061b92007-04-17 13:31:34 +0200584 td->ts.short_io_u[io_u->ddir]++;
585
Jens Axboed460eb32007-04-16 21:39:33 +0200586 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200587 goto sync_done;
588
Jens Axboe11786802007-03-05 18:33:22 +0100589 requeue_io_u(td, &io_u);
590 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200591sync_done:
Jens Axboe993bf482008-11-14 13:04:53 +0100592 if (should_check_rate(td))
593 fio_gettime(&comp_time, NULL);
594
Jens Axboe11786802007-03-05 18:33:22 +0100595 bytes_done = io_u_sync_complete(td, io_u);
596 if (bytes_done < 0)
597 ret = bytes_done;
Jens Axboe36167d82007-02-18 05:41:31 +0100598 }
Jens Axboe36167d82007-02-18 05:41:31 +0100599 break;
600 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100601 /*
602 * if the engine doesn't have a commit hook,
603 * the io_u is really queued. if it does have such
604 * a hook, it has to call io_u_queued() itself.
605 */
606 if (td->io_ops->commit == NULL)
607 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100608 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100609 case FIO_Q_BUSY:
610 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100611 ret2 = td_io_commit(td);
612 if (ret2 < 0)
613 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100614 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100615 default:
616 assert(ret < 0);
617 put_io_u(td, io_u);
618 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100619 }
620
Jens Axboe99784632007-02-19 10:06:17 +0100621 if (ret < 0 || td->error)
Jens Axboe36167d82007-02-18 05:41:31 +0100622 break;
623
Jens Axboe97601022007-02-18 12:47:29 +0100624 /*
625 * See if we need to complete some commands
626 */
Jens Axboe49504212008-06-05 09:03:30 +0200627 full = queue_full(td) || ret == FIO_Q_BUSY;
628 if (full || !td->o.iodepth_batch_complete) {
629 min_evts = td->o.iodepth_batch_complete;
630 if (full && !min_evts)
Jens Axboe36167d82007-02-18 05:41:31 +0100631 min_evts = 1;
Jens Axboe49504212008-06-05 09:03:30 +0200632
Jens Axboe993bf482008-11-14 13:04:53 +0100633 if (should_check_rate(td))
634 fio_gettime(&comp_time, NULL);
Jens Axboe49504212008-06-05 09:03:30 +0200635
636 do {
637 ret = io_u_queued_complete(td, min_evts);
638 if (ret <= 0)
639 break;
640
641 bytes_done += ret;
642 } while (full && (td->cur_depth > td->o.iodepth_low));
Jens Axboeebac4652005-12-08 15:25:21 +0100643 }
644
Jens Axboe49504212008-06-05 09:03:30 +0200645 if (ret < 0)
646 break;
Jens Axboe97601022007-02-18 12:47:29 +0100647 if (!bytes_done)
648 continue;
649
Jens Axboeebac4652005-12-08 15:25:21 +0100650 /*
651 * the rate is batched for now, it should work for batches
652 * of completions except the very first one which may look
653 * a little bursty
654 */
Jens Axboe993bf482008-11-14 13:04:53 +0100655 if (!in_ramp_time(td) && should_check_rate(td)) {
656 usec = utime_since(&td->tv_cache, &comp_time);
Jens Axboeebac4652005-12-08 15:25:21 +0100657
Jens Axboe721938a2008-09-10 09:46:16 +0200658 rate_throttle(td, usec, bytes_done);
Jens Axboeebac4652005-12-08 15:25:21 +0100659
Jens Axboe721938a2008-09-10 09:46:16 +0200660 if (check_min_rate(td, &comp_time)) {
661 if (exitall_on_terminate)
662 terminate_threads(td->groupid);
663 td_verror(td, EIO, "check_min_rate");
664 break;
665 }
Jens Axboeebac4652005-12-08 15:25:21 +0100666 }
667
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100668 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100669 unsigned long long b;
670
671 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100672 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100673 int left;
674
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100675 if (td->o.thinktime_spin)
Jens Axboe6dd6f2c2008-12-10 13:24:12 +0100676 usec_spin(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100677
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100678 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100679 if (left)
680 usec_sleep(td, left);
681 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100682 }
Jens Axboeebac4652005-12-08 15:25:21 +0100683 }
684
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100685 if (td->o.fill_device && td->error == ENOSPC) {
686 td->error = 0;
687 td->terminate = 1;
688 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100689 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100690 struct fio_file *f;
691
Jens Axboec01c0392007-02-26 12:43:42 +0100692 i = td->cur_depth;
693 if (i)
694 ret = io_u_queued_complete(td, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100695
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100696 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200697 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100698
699 for_each_file(td, f, i) {
700 if (!(f->flags & FIO_FILE_OPEN))
701 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200702 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100703 }
Jens Axboe84585002006-10-19 20:26:22 +0200704 }
Jens Axboec01c0392007-02-26 12:43:42 +0100705 } else
706 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100707
708 /*
709 * stop job if we failed doing any IO
710 */
711 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
712 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100713}
714
Jens Axboeebac4652005-12-08 15:25:21 +0100715static void cleanup_io_u(struct thread_data *td)
716{
Jens Axboe01743ee2008-06-02 12:19:19 +0200717 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100718 struct io_u *io_u;
719
Jens Axboe01743ee2008-06-02 12:19:19 +0200720 flist_for_each_safe(entry, n, &td->io_u_freelist) {
721 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100722
Jens Axboe01743ee2008-06-02 12:19:19 +0200723 flist_del(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100724 free(io_u);
725 }
726
Jens Axboe2f9ade32006-10-20 11:25:52 +0200727 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100728}
729
730static int init_io_u(struct thread_data *td)
731{
732 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100733 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100734 int i, max_units;
735 char *p;
736
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100737 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100738 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100739 td->orig_buffer_size = (unsigned long long) max_bs
740 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100741
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100742 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
743 unsigned long bs;
744
745 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
746 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
747 }
Jens Axboeebac4652005-12-08 15:25:21 +0100748
Jens Axboec3990642007-07-19 10:17:09 +0200749 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
750 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
751 return 1;
752 }
753
Jens Axboe2f9ade32006-10-20 11:25:52 +0200754 if (allocate_io_mem(td))
755 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100756
Jens Axboeb3f378e2007-07-20 12:39:22 +0200757 if (td->o.odirect)
758 p = ALIGN(td->orig_buffer);
759 else
760 p = td->orig_buffer;
761
Jens Axboeebac4652005-12-08 15:25:21 +0100762 for (i = 0; i < max_units; i++) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200763 if (td->terminate)
764 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100765 io_u = malloc(sizeof(*io_u));
766 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200767 INIT_FLIST_HEAD(&io_u->list);
Jens Axboeebac4652005-12-08 15:25:21 +0100768
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200769 if (!(td->io_ops->flags & FIO_NOIO)) {
770 io_u->buf = p + max_bs * i;
Jens Axboee9459e52007-04-17 15:46:32 +0200771
Jens Axboe5973caf2008-05-21 19:52:35 +0200772 if (td_write(td) && !td->o.refill_buffers)
773 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200774 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100775
Jens Axboeb1ff3402006-02-17 11:35:58 +0100776 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100777 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200778 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboeebac4652005-12-08 15:25:21 +0100779 }
780
781 return 0;
782}
783
Jens Axboeda867742006-06-06 10:39:10 -0700784static int switch_ioscheduler(struct thread_data *td)
785{
786 char tmp[256], tmp2[128];
787 FILE *f;
788 int ret;
789
Jens Axboeba0fbe12007-03-09 14:34:23 +0100790 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200791 return 0;
792
Jens Axboeda867742006-06-06 10:39:10 -0700793 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
794
795 f = fopen(tmp, "r+");
796 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200797 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100798 log_err("fio: os or kernel doesn't support IO scheduler"
799 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200800 return 0;
801 }
802 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700803 return 1;
804 }
805
806 /*
807 * Set io scheduler.
808 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100809 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700810 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100811 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700812 fclose(f);
813 return 1;
814 }
815
816 rewind(f);
817
818 /*
819 * Read back and check that the selected scheduler is now the default.
820 */
821 ret = fread(tmp, 1, sizeof(tmp), f);
822 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100823 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -0700824 fclose(f);
825 return 1;
826 }
827
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100828 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -0700829 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100830 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +0100831 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -0700832 fclose(f);
833 return 1;
834 }
835
836 fclose(f);
837 return 0;
838}
839
Jens Axboe492158c2007-05-24 10:32:47 +0200840static int keep_running(struct thread_data *td)
841{
842 unsigned long long io_done;
843
Jens Axboe20e354e2007-07-23 14:36:16 +0200844 if (td->done)
845 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +0200846 if (td->o.time_based)
847 return 1;
848 if (td->o.loops) {
849 td->o.loops--;
850 return 1;
851 }
852
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100853 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
854 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +0200855 if (io_done < td->o.size)
856 return 1;
857
858 return 0;
859}
860
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200861static void reset_io_counters(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100862{
Jens Axboe756867b2007-03-06 15:19:24 +0100863 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100864 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100865 td->zone_bytes = 0;
Jens Axboed7d3b492007-03-12 08:02:10 +0100866 td->rate_bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100867 td->rate_blocks = 0;
Jens Axboe38d77ca2007-03-21 14:20:20 +0100868 td->rw_end_set[0] = td->rw_end_set[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100869
Jens Axboec1324df2007-02-19 19:06:41 +0100870 td->last_was_sync = 0;
871
Jens Axboe2ba1c292008-02-01 13:16:38 +0100872 /*
873 * reset file done count if we are to start over
874 */
875 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +0200876 td->nr_done_files = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200877}
878
879void reset_all_stats(struct thread_data *td)
880{
881 struct timeval tv;
882 int i;
883
884 reset_io_counters(td);
885
886 for (i = 0; i < 2; i++) {
887 td->io_bytes[i] = 0;
888 td->io_blocks[i] = 0;
889 td->io_issues[i] = 0;
890 td->ts.total_io_u[i] = 0;
891 }
892
893 fio_gettime(&tv, NULL);
894 memcpy(&td->epoch, &tv, sizeof(tv));
895 memcpy(&td->start, &tv, sizeof(tv));
896}
897
898static int clear_io_state(struct thread_data *td)
899{
900 struct fio_file *f;
901 unsigned int i;
902 int ret;
903
904 reset_io_counters(td);
Jens Axboe281f9b62007-05-23 09:34:42 +0200905
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100906 close_files(td);
Jens Axboea978ba62007-03-08 14:29:03 +0100907
908 ret = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200909 for_each_file(td, f, i) {
Jens Axboe281f9b62007-05-23 09:34:42 +0200910 f->flags &= ~FIO_FILE_DONE;
Jens Axboea978ba62007-03-08 14:29:03 +0100911 ret = td_io_open_file(td, f);
912 if (ret)
913 break;
Jens Axboe53cdc682006-10-18 11:50:58 +0200914 }
Jens Axboea978ba62007-03-08 14:29:03 +0100915
916 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100917}
918
Jens Axboe906c8d72006-06-13 09:37:56 +0200919/*
920 * Entry point for the thread based jobs. The process based jobs end up
921 * here as well, after a little setup.
922 */
Jens Axboeebac4652005-12-08 15:25:21 +0100923static void *thread_main(void *data)
924{
Shawn Lewis10927312007-11-21 09:38:13 +0100925 unsigned long long runtime[2], elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +0100926 struct thread_data *td = data;
Jens Axboea978ba62007-03-08 14:29:03 +0100927 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100928
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100929 if (!td->o.use_thread)
Jens Axboeebac4652005-12-08 15:25:21 +0100930 setsid();
931
932 td->pid = getpid();
933
Jens Axboe5921e802008-05-30 15:02:38 +0200934 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +0100935
Jens Axboe01743ee2008-06-02 12:19:19 +0200936 INIT_FLIST_HEAD(&td->io_u_freelist);
937 INIT_FLIST_HEAD(&td->io_u_busylist);
938 INIT_FLIST_HEAD(&td->io_u_requeues);
939 INIT_FLIST_HEAD(&td->io_log_list);
940 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboebb5d7d02007-03-27 10:21:25 +0200941 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +0200942
Jens Axboe75154842006-06-01 13:56:09 +0200943 td_set_runstate(td, TD_INITIALIZED);
Jens Axboe29adda32009-01-05 19:06:39 +0100944 dprint(FD_MUTEX, "up startup_mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +0100945 fio_mutex_up(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +0100946 dprint(FD_MUTEX, "wait on td->mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +0100947 fio_mutex_down(td->mutex);
Jens Axboe29adda32009-01-05 19:06:39 +0100948 dprint(FD_MUTEX, "done waiting on td->mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100949
Jens Axboe37f56872007-03-09 12:42:35 +0100950 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +0100951 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +0100952 * eating a file descriptor
953 */
Jens Axboecdd18ad2008-02-27 18:58:00 +0100954 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +0100955
Jens Axboed84f8d42007-05-16 08:47:46 +0200956 /*
957 * May alter parameters that init_io_u() will use, so we need to
958 * do this first.
959 */
960 if (init_iolog(td))
961 goto err;
962
Jens Axboedb1cd902007-04-26 13:47:07 +0200963 if (init_io_u(td))
964 goto err;
965
Jens Axboe375b2692007-05-22 09:13:02 +0200966 if (td->o.cpumask_set && fio_setaffinity(td) == -1) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200967 td_verror(td, errno, "cpu_set_affinity");
968 goto err;
969 }
970
Jens Axboef4844702008-12-09 08:04:29 +0100971 /*
972 * If we have a gettimeofday() thread, make sure we exclude that
973 * thread from this job
974 */
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100975 if (td->o.gtod_cpu) {
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100976 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
977 if (fio_setaffinity(td) == -1) {
978 td_verror(td, errno, "cpu_set_affinity");
979 goto err;
980 }
981 }
982
Jens Axboeac684782007-11-08 08:29:07 +0100983 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +0200984 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
985 td_verror(td, errno, "ioprio_set");
986 goto err;
987 }
988 }
989
990 if (nice(td->o.nice) == -1) {
991 td_verror(td, errno, "nice");
992 goto err;
993 }
994
995 if (td->o.ioscheduler && switch_ioscheduler(td))
996 goto err;
997
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100998 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100999 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +01001000
1001 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +01001002 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +01001003
Jens Axboeb5af8292007-03-08 12:43:13 +01001004 if (open_files(td))
Jens Axboe7d6c5282007-02-05 10:52:10 +01001005 goto err;
1006
Jens Axboe68727072007-03-15 20:49:25 +01001007 if (init_random_map(td))
1008 goto err;
1009
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001010 if (td->o.exec_prerun) {
1011 if (system(td->o.exec_prerun) < 0)
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001012 goto err;
1013 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001014
Jens Axboe69008992006-11-24 13:12:56 +01001015 fio_gettime(&td->epoch, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +01001016 getrusage(RUSAGE_SELF, &td->ts.ru_start);
Jens Axboe69008992006-11-24 13:12:56 +01001017
1018 runtime[0] = runtime[1] = 0;
Jens Axboea978ba62007-03-08 14:29:03 +01001019 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +02001020 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +01001021 fio_gettime(&td->start, NULL);
Jens Axboe756867b2007-03-06 15:19:24 +01001022 memcpy(&td->ts.stat_sample_time, &td->start, sizeof(td->start));
Jens Axboe993bf482008-11-14 13:04:53 +01001023 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +01001024
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001025 if (td->o.ratemin)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001026 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1027 sizeof(td->lastrate));
Jens Axboeebac4652005-12-08 15:25:21 +01001028
Jens Axboea978ba62007-03-08 14:29:03 +01001029 if (clear_state && clear_io_state(td))
1030 break;
1031
Jens Axboeebac4652005-12-08 15:25:21 +01001032 prune_io_piece_log(td);
1033
Jens Axboeba0fbe12007-03-09 14:34:23 +01001034 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001035
Jens Axboea978ba62007-03-08 14:29:03 +01001036 clear_state = 1;
1037
Jens Axboe38d77ca2007-03-21 14:20:20 +01001038 if (td_read(td) && td->io_bytes[DDIR_READ]) {
1039 if (td->rw_end_set[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001040 elapsed = utime_since(&td->start,
1041 &td->rw_end[DDIR_READ]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001042 else
1043 elapsed = utime_since_now(&td->start);
1044
1045 runtime[DDIR_READ] += elapsed;
1046 }
1047 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
1048 if (td->rw_end_set[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001049 elapsed = utime_since(&td->start,
1050 &td->rw_end[DDIR_WRITE]);
Jens Axboe38d77ca2007-03-21 14:20:20 +01001051 else
1052 elapsed = utime_since_now(&td->start);
1053
1054 runtime[DDIR_WRITE] += elapsed;
1055 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001056
Jens Axboeebac4652005-12-08 15:25:21 +01001057 if (td->error || td->terminate)
1058 break;
1059
Shawn Lewise84c73a2007-08-02 22:19:32 +02001060 if (!td->o.do_verify ||
1061 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +02001062 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +01001063 continue;
1064
Jens Axboea978ba62007-03-08 14:29:03 +01001065 if (clear_io_state(td))
1066 break;
1067
Jens Axboe02bcaa82006-11-24 10:42:00 +01001068 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +01001069
1070 do_verify(td);
1071
Jens Axboe69008992006-11-24 13:12:56 +01001072 runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +01001073
1074 if (td->error || td->terminate)
1075 break;
1076 }
1077
Jens Axboe36dff962006-11-24 13:29:20 +01001078 update_rusage_stat(td);
ljzhang,Yaxin Hu,Jianchao Tang47f0cc42007-07-26 11:00:29 +02001079 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1080 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +01001081 td->ts.total_run_time = mtime_since_now(&td->epoch);
1082 td->ts.io_bytes[0] = td->io_bytes[0];
1083 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +01001084
Jens Axboee3cedca2008-11-19 19:57:52 +01001085 if (td->ts.bw_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001086 if (td->o.bw_log_file) {
1087 finish_log_named(td, td->ts.bw_log,
1088 td->o.bw_log_file, "bw");
1089 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001090 finish_log(td, td->ts.bw_log, "bw");
1091 }
1092 if (td->ts.slat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001093 if (td->o.lat_log_file) {
1094 finish_log_named(td, td->ts.slat_log,
1095 td->o.lat_log_file, "clat");
1096 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001097 finish_log(td, td->ts.slat_log, "slat");
1098 }
1099 if (td->ts.clat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001100 if (td->o.lat_log_file) {
1101 finish_log_named(td, td->ts.clat_log,
1102 td->o.lat_log_file, "clat");
1103 } else
Jens Axboee3cedca2008-11-19 19:57:52 +01001104 finish_log(td, td->ts.clat_log, "clat");
1105 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001106 if (td->o.exec_postrun) {
1107 if (system(td->o.exec_postrun) < 0)
1108 log_err("fio: postrun %s failed\n", td->o.exec_postrun);
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001109 }
Jens Axboeebac4652005-12-08 15:25:21 +01001110
1111 if (exitall_on_terminate)
Jens Axboe390c40e2007-03-05 12:35:29 +01001112 terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001113
1114err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001115 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001116 printf("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001117 td->verror);
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001118 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001119 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001120 cleanup_io_u(td);
Jens Axboef29b25a2007-07-23 08:56:43 +02001121
Jens Axboed2ce18b2008-12-12 20:51:40 +01001122 if (td->o.cpumask_set) {
1123 int ret = fio_cpuset_exit(&td->o.cpumask);
1124
1125 td_verror(td, ret, "fio_cpuset_exit");
1126 }
Jens Axboe4e78e402008-12-12 20:40:27 +01001127
Jens Axboef29b25a2007-07-23 08:56:43 +02001128 /*
1129 * do this very late, it will log file closing as well
1130 */
1131 if (td->o.write_iolog_file)
1132 write_iolog_close(td);
1133
Jens Axboed23bb322007-03-23 15:57:56 +01001134 options_mem_free(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001135 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001136 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001137}
1138
Jens Axboe906c8d72006-06-13 09:37:56 +02001139/*
1140 * We cannot pass the td data into a forked process, so attach the td and
1141 * pass it to the thread worker.
1142 */
Jens Axboea6418142007-02-17 04:47:18 +01001143static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001144{
1145 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001146 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001147
1148 data = shmat(shmid, NULL, 0);
1149 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001150 int __err = errno;
1151
Jens Axboeebac4652005-12-08 15:25:21 +01001152 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001153 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001154 }
1155
1156 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001157 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001158 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001159 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001160}
1161
Jens Axboe906c8d72006-06-13 09:37:56 +02001162/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001163 * Run over the job map and reap the threads that have exited, if any.
1164 */
Jens Axboeebac4652005-12-08 15:25:21 +01001165static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1166{
Jens Axboe34572e22006-10-20 09:33:18 +02001167 struct thread_data *td;
Jens Axboe1f809d12007-10-25 18:34:02 +02001168 int i, cputhreads, realthreads, pending, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001169
1170 /*
1171 * reap exited threads (TD_EXITED -> TD_REAPED)
1172 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001173 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001174 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001175 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001176
Jens Axboe84585002006-10-19 20:26:22 +02001177 /*
1178 * ->io_ops is NULL for a thread that has closed its
1179 * io engine
1180 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001181 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001182 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001183 else
1184 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001185
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001186 if (!td->pid) {
1187 pending++;
1188 continue;
1189 }
1190 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001191 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001192 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001193 if (td->runstate == TD_EXITED) {
1194 td_set_runstate(td, TD_REAPED);
1195 goto reaped;
1196 }
1197 continue;
1198 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001199
1200 flags = WNOHANG;
1201 if (td->runstate == TD_EXITED)
1202 flags = 0;
1203
1204 /*
1205 * check if someone quit or got killed in an unusual way
1206 */
1207 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001208 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001209 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001210 log_err("fio: pid=%d disappeared %d\n",
1211 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001212 td_set_runstate(td, TD_REAPED);
1213 goto reaped;
1214 }
1215 perror("waitpid");
1216 } else if (ret == td->pid) {
1217 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001218 int sig = WTERMSIG(status);
1219
Jens Axboed9244942007-03-05 12:32:32 +01001220 if (sig != SIGQUIT)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001221 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001222 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001223 td_set_runstate(td, TD_REAPED);
1224 goto reaped;
1225 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001226 if (WIFEXITED(status)) {
1227 if (WEXITSTATUS(status) && !td->error)
1228 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001229
Jens Axboea2f77c92007-02-22 11:53:00 +01001230 td_set_runstate(td, TD_REAPED);
1231 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001232 }
1233 }
Jens Axboeebac4652005-12-08 15:25:21 +01001234
Jens Axboea2f77c92007-02-22 11:53:00 +01001235 /*
1236 * thread is not dead, continue
1237 */
gurudas paie48676b2007-04-11 12:44:27 +02001238 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001239 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001240reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001241 (*nr_running)--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001242 (*m_rate) -= td->o.ratemin;
1243 (*t_rate) -= td->o.rate;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001244 if (!td->pid)
1245 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001246
1247 if (td->error)
1248 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001249
1250 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001251 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001252
Jens Axboe1f809d12007-10-25 18:34:02 +02001253 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboe390c40e2007-03-05 12:35:29 +01001254 terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001255}
1256
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001257static void *gtod_thread_main(void *data)
1258{
1259 fio_mutex_up(startup_mutex);
1260
1261 /*
1262 * As long as we have jobs around, update the clock. It would be nice
1263 * to have some way of NOT hammering that CPU with gettimeofday(),
1264 * but I'm not sure what to use outside of a simple CPU nop to relax
1265 * it - we don't want to lose precision.
1266 */
1267 while (threads) {
1268 fio_gtod_update();
1269 nop;
1270 }
1271
1272 return NULL;
1273}
1274
1275static int fio_start_gtod_thread(void)
1276{
Jens Axboef42c1532009-01-05 19:07:13 +01001277 int ret;
1278
1279 ret = pthread_create(&gtod_thread, NULL, gtod_thread_main, NULL);
1280 if (ret) {
1281 log_err("Can't create gtod thread: %s\n", strerror(ret));
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001282 return 1;
1283 }
Jens Axboef42c1532009-01-05 19:07:13 +01001284
1285 ret = pthread_detach(gtod_thread);
1286 if (ret) {
1287 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001288 return 1;
1289 }
1290
Jens Axboe29adda32009-01-05 19:06:39 +01001291 dprint(FD_MUTEX, "wait on startup_mutex\n");
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001292 fio_mutex_down(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001293 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001294 return 0;
1295}
1296
Jens Axboe906c8d72006-06-13 09:37:56 +02001297/*
1298 * Main function for kicking off and reaping jobs, as needed.
1299 */
Jens Axboeebac4652005-12-08 15:25:21 +01001300static void run_threads(void)
1301{
Jens Axboeebac4652005-12-08 15:25:21 +01001302 struct thread_data *td;
1303 unsigned long spent;
1304 int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001305
Jens Axboe2f9ade32006-10-20 11:25:52 +02001306 if (fio_pin_memory())
1307 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001308
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001309 if (fio_gtod_offload && fio_start_gtod_thread())
1310 return;
1311
Jens Axboec6ae0a52006-06-12 11:04:44 +02001312 if (!terse_output) {
Jens Axboe9cedf162007-03-12 11:29:30 +01001313 printf("Starting ");
1314 if (nr_thread)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001315 printf("%d thread%s", nr_thread,
1316 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001317 if (nr_process) {
1318 if (nr_thread)
1319 printf(" and ");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001320 printf("%d process%s", nr_process,
1321 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001322 }
1323 printf("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001324 fflush(stdout);
1325 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001326
Jens Axboe697a6062008-05-31 00:04:45 +02001327 set_sig_handlers();
Jens Axboe4efa9702006-05-31 11:56:49 +02001328
Jens Axboeebac4652005-12-08 15:25:21 +01001329 todo = thread_number;
1330 nr_running = 0;
1331 nr_started = 0;
1332 m_rate = t_rate = 0;
1333
Jens Axboe34572e22006-10-20 09:33:18 +02001334 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001335 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001336
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001337 if (!td->o.create_serialize) {
Jens Axboe380cf262007-01-11 14:01:27 +01001338 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001339 continue;
Jens Axboe380cf262007-01-11 14:01:27 +01001340 }
Jens Axboeebac4652005-12-08 15:25:21 +01001341
1342 /*
1343 * do file setup here so it happens sequentially,
1344 * we don't want X number of threads getting their
1345 * client data interspersed on disk
1346 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001347 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001348 exit_value++;
1349 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001350 log_err("fio: pid=%d, err=%d/%s\n",
1351 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001352 td_set_runstate(td, TD_REAPED);
1353 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001354 } else {
1355 struct fio_file *f;
1356 unsigned int i;
1357
1358 /*
1359 * for sharing to work, each job must always open
1360 * its own files. so close them, if we opened them
1361 * for creation
1362 */
1363 for_each_file(td, f, i)
1364 td_io_close_file(td, f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001365 }
Jens Axboe380cf262007-01-11 14:01:27 +01001366
1367 init_disk_util(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001368 }
1369
Jens Axboea2f77c92007-02-22 11:53:00 +01001370 set_genesis_time();
1371
Jens Axboeebac4652005-12-08 15:25:21 +01001372 while (todo) {
Jens Axboe75154842006-06-01 13:56:09 +02001373 struct thread_data *map[MAX_JOBS];
1374 struct timeval this_start;
1375 int this_jobs = 0, left;
1376
Jens Axboeebac4652005-12-08 15:25:21 +01001377 /*
1378 * create threads (TD_NOT_CREATED -> TD_CREATED)
1379 */
Jens Axboe34572e22006-10-20 09:33:18 +02001380 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001381 if (td->runstate != TD_NOT_CREATED)
1382 continue;
1383
1384 /*
1385 * never got a chance to start, killed by other
1386 * thread for some reason
1387 */
1388 if (td->terminate) {
1389 todo--;
1390 continue;
1391 }
1392
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001393 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001394 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001395
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001396 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001397 continue;
1398 }
1399
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001400 if (td->o.stonewall && (nr_started || nr_running)) {
1401 dprint(FD_PROCESS, "%s: stonewall wait\n",
1402 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001403 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001404 }
Jens Axboeebac4652005-12-08 15:25:21 +01001405
Jens Axboe75154842006-06-01 13:56:09 +02001406 /*
1407 * Set state to created. Thread will transition
1408 * to TD_INITIALIZED when it's done setting up.
1409 */
Jens Axboeebac4652005-12-08 15:25:21 +01001410 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001411 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001412 nr_started++;
1413
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001414 if (td->o.use_thread) {
Jens Axboef42c1532009-01-05 19:07:13 +01001415 int ret;
1416
Jens Axboeee56ad52008-02-01 10:30:20 +01001417 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboef42c1532009-01-05 19:07:13 +01001418 ret = pthread_create(&td->thread, NULL,
1419 thread_main, td);
1420 if (ret) {
1421 log_err("pthread_create: %s\n",
1422 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001423 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001424 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001425 }
Jens Axboef42c1532009-01-05 19:07:13 +01001426 ret = pthread_detach(td->thread);
1427 if (ret)
1428 log_err("pthread_detach: %s",
1429 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001430 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001431 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001432 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001433 pid = fork();
1434 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001435 int ret = fork_main(shm_id, i);
1436
Jens Axboe5e1d3062008-05-23 11:55:53 +02001437 _exit(ret);
1438 } else if (i == fio_debug_jobno)
1439 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001440 }
Jens Axboe29adda32009-01-05 19:06:39 +01001441 dprint(FD_MUTEX, "wait on startup_mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +01001442 fio_mutex_down(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001443 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001444 }
1445
1446 /*
Jens Axboe75154842006-06-01 13:56:09 +02001447 * Wait for the started threads to transition to
1448 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001449 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001450 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001451 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001452 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001453 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1454 break;
1455
1456 usleep(100000);
1457
1458 for (i = 0; i < this_jobs; i++) {
1459 td = map[i];
1460 if (!td)
1461 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001462 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001463 map[i] = NULL;
1464 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001465 } else if (td->runstate >= TD_EXITED) {
1466 map[i] = NULL;
1467 left--;
1468 todo--;
1469 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001470 }
1471 }
1472 }
1473
1474 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001475 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001476 for (i = 0; i < this_jobs; i++) {
1477 td = map[i];
1478 if (!td)
1479 continue;
1480 kill(td->pid, SIGTERM);
1481 }
1482 break;
1483 }
1484
1485 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001486 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001487 */
Jens Axboe34572e22006-10-20 09:33:18 +02001488 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001489 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001490 continue;
1491
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001492 if (in_ramp_time(td))
1493 td_set_runstate(td, TD_RAMP);
1494 else
1495 td_set_runstate(td, TD_RUNNING);
Jens Axboeebac4652005-12-08 15:25:21 +01001496 nr_running++;
1497 nr_started--;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001498 m_rate += td->o.ratemin;
1499 t_rate += td->o.rate;
Jens Axboe75154842006-06-01 13:56:09 +02001500 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001501 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001502 }
1503
1504 reap_threads(&nr_running, &t_rate, &m_rate);
1505
1506 if (todo)
1507 usleep(100000);
1508 }
1509
1510 while (nr_running) {
1511 reap_threads(&nr_running, &t_rate, &m_rate);
1512 usleep(10000);
1513 }
1514
1515 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001516 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001517}
1518
Jens Axboeebac4652005-12-08 15:25:21 +01001519int main(int argc, char *argv[])
1520{
Jens Axboe29d610e2007-02-06 13:25:33 +01001521 long ps;
1522
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001523 sinit();
1524
Jens Axboedbe11252007-02-11 00:19:51 +01001525 /*
1526 * We need locale for number printing, if it isn't set then just
1527 * go with the US format.
1528 */
1529 if (!getenv("LC_NUMERIC"))
1530 setlocale(LC_NUMERIC, "en_US");
1531
Jens Axboeebac4652005-12-08 15:25:21 +01001532 if (parse_options(argc, argv))
1533 return 1;
1534
Jens Axboe4b472fa2007-04-04 11:04:34 +02001535 if (!thread_number)
1536 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001537
Jens Axboe29d610e2007-02-06 13:25:33 +01001538 ps = sysconf(_SC_PAGESIZE);
1539 if (ps < 0) {
1540 log_err("Failed to get page size\n");
1541 return 1;
1542 }
1543
Jens Axboecfc99db2007-03-14 10:34:47 +01001544 page_size = ps;
Jens Axboe29d610e2007-02-06 13:25:33 +01001545 page_mask = ps - 1;
1546
Jens Axboebb3884d2007-01-17 17:23:11 +11001547 if (write_bw_log) {
1548 setup_log(&agg_io_log[DDIR_READ]);
1549 setup_log(&agg_io_log[DDIR_WRITE]);
1550 }
1551
Jens Axboecdd18ad2008-02-27 18:58:00 +01001552 startup_mutex = fio_mutex_init(0);
Jens Axboe07739b52007-03-08 20:25:46 +01001553
Jens Axboea2f77c92007-02-22 11:53:00 +01001554 set_genesis_time();
1555
Jens Axboeccb0fa22008-05-30 23:18:00 +02001556 status_timer_arm();
Jens Axboeebac4652005-12-08 15:25:21 +01001557
1558 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001559
Jens Axboebb3884d2007-01-17 17:23:11 +11001560 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001561 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001562 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001563 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1564 __finish_log(agg_io_log[DDIR_WRITE],
1565 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001566 }
1567 }
Jens Axboeebac4652005-12-08 15:25:21 +01001568
Jens Axboecdd18ad2008-02-27 18:58:00 +01001569 fio_mutex_remove(startup_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001570 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001571}