blob: 4f07214ecb65ddd7a56b77216ebc03bb3479121a [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>
Bruce Cran9b836562011-01-08 19:49:54 +010027#include <limits.h>
Jens Axboeebac4652005-12-08 15:25:21 +010028#include <signal.h>
29#include <time.h>
Jens Axboedbe11252007-02-11 00:19:51 +010030#include <locale.h>
Jens Axboe36167d82007-02-18 05:41:31 +010031#include <assert.h>
Jens Axboea5b01f12010-11-11 09:19:49 +010032#include <time.h>
Jens Axboeebac4652005-12-08 15:25:21 +010033#include <sys/stat.h>
34#include <sys/wait.h>
35#include <sys/ipc.h>
36#include <sys/shm.h>
Jens Axboeebac4652005-12-08 15:25:21 +010037#include <sys/mman.h>
38
39#include "fio.h"
Jens Axboe210f43b2007-04-17 19:52:18 +020040#include "hash.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010041#include "smalloc.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020042#include "verify.h"
Jens Axboea917a8b2010-09-02 13:23:20 +020043#include "trim.h"
Jens Axboe7c9b1bc2009-06-03 09:25:57 +020044#include "diskutil.h"
Jens Axboea696fa22009-12-04 10:05:02 +010045#include "cgroup.h"
Jens Axboe07b32322010-03-05 09:48:44 +010046#include "profile.h"
Jens Axboed54a1442010-03-25 23:07:35 +010047#include "lib/rand.h"
Jens Axboe91aea6e2011-04-25 20:46:58 +020048#include "memalign.h"
Jens Axboe009b1be2011-09-29 18:27:02 -060049#include "server.h"
Jens Axboeebac4652005-12-08 15:25:21 +010050
Jens Axboecfc99db2007-03-14 10:34:47 +010051unsigned long page_mask;
52unsigned long page_size;
Jens Axboed529ee12009-07-01 10:33:03 +020053
54#define PAGE_ALIGN(buf) \
Jens Axboe29d610e2007-02-06 13:25:33 +010055 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
Jens Axboeebac4652005-12-08 15:25:21 +010056
57int groupid = 0;
Jens Axboe11e950b2011-10-16 21:34:14 +020058unsigned int thread_number = 0;
59unsigned int nr_process = 0;
60unsigned int nr_thread = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010061int shm_id = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +020062int temp_stall_ts;
Jens Axboed3eeeab2008-04-06 12:19:05 +020063unsigned long done_secs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +010064
Jens Axboed09a64a2011-10-13 11:38:56 +020065/*
66 * Just expose an empty list, if the OS does not support disk util stats
67 */
68#ifndef FIO_HAVE_DISK_UTIL
69FLIST_HEAD(disk_list);
70#endif
71
Jens Axboecdd18ad2008-02-27 18:58:00 +010072static struct fio_mutex *startup_mutex;
Fabio Checconid2bbb8d2009-05-18 12:54:50 +020073static struct fio_mutex *writeout_mutex;
Jens Axboe6ce15a32007-01-12 09:04:41 +010074static volatile int fio_abort;
Jens Axboe437c9b72007-02-13 18:20:37 +010075static int exit_value;
Jens Axboe79857012011-01-14 14:14:58 +010076static pthread_t disk_util_thread;
Jens Axboedae53412009-12-04 19:54:18 +010077static struct flist_head *cgroup_list;
Jens Axboe6adb38a2009-12-07 08:01:26 +010078static char *cgroup_mnt;
Jens Axboeebac4652005-12-08 15:25:21 +010079
Jens Axboed3cc4eb2011-08-25 14:24:03 +020080unsigned long arch_flags = 0;
81
Jens Axboebb3884d2007-01-17 17:23:11 +110082struct io_log *agg_io_log[2];
83
Jens Axboe75154842006-06-01 13:56:09 +020084#define JOB_START_TIMEOUT (5 * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +010085
Jens Axboecca84642011-10-07 12:47:57 +020086static const char *fio_os_strings[os_nr] = {
87 "Invalid",
88 "Linux",
89 "AIX",
90 "FreeBSD",
91 "HP-UX",
92 "OSX",
93 "NetBSD",
94 "Solaris",
95 "Windows"
96};
97
98static const char *fio_arch_strings[arch_nr] = {
99 "Invalid",
100 "x86-64",
101 "x86",
102 "ppc",
103 "ia64",
104 "s390",
105 "alpha",
106 "sparc",
107 "sparc64",
108 "arm",
109 "sh",
110 "hppa",
111 "generic"
112};
113
114const char *fio_get_os_string(int nr)
115{
116 if (nr < os_nr)
117 return fio_os_strings[nr];
118
119 return NULL;
120}
121
122const char *fio_get_arch_string(int nr)
123{
124 if (nr < arch_nr)
125 return fio_arch_strings[nr];
126
127 return NULL;
128}
129
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200130void td_set_runstate(struct thread_data *td, int runstate)
Jens Axboe6ce15a32007-01-12 09:04:41 +0100131{
Jens Axboed8fab1b2008-03-06 10:25:17 +0100132 if (td->runstate == runstate)
133 return;
134
Jens Axboe5921e802008-05-30 15:02:38 +0200135 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
136 td->runstate, runstate);
Jens Axboe6ce15a32007-01-12 09:04:41 +0100137 td->runstate = runstate;
138}
139
Jens Axboecc0df002011-10-03 20:53:32 +0200140void fio_terminate_threads(int group_id)
Jens Axboeebac4652005-12-08 15:25:21 +0100141{
Jens Axboe34572e22006-10-20 09:33:18 +0200142 struct thread_data *td;
Jens Axboeebac4652005-12-08 15:25:21 +0100143 int i;
144
Jens Axboe6f88bcb2008-03-19 10:29:07 +0100145 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
146
Jens Axboe34572e22006-10-20 09:33:18 +0200147 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +0100148 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
Jens Axboe6f88bcb2008-03-19 10:29:07 +0100149 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +0200150 td->o.name, (int) td->pid);
Jens Axboead830ed2008-02-18 21:11:24 +0100151 td->terminate = 1;
152 td->o.start_delay = 0;
153
Jens Axboe7a93ab12007-04-18 12:25:41 +0200154 /*
155 * if the thread is running, just let it exit
156 */
Michael Callahan6aa27812011-07-14 07:56:37 +0200157 if (!td->pid)
158 continue;
159 else if (td->runstate < TD_RAMP)
Bruce Cran03e20d62011-01-02 20:14:54 +0100160 kill(td->pid, SIGTERM);
Jens Axboef63f7f32008-03-01 15:25:24 +0100161 else {
162 struct ioengine_ops *ops = td->io_ops;
163
Bruce Cran03e20d62011-01-02 20:14:54 +0100164 if (ops && (ops->flags & FIO_SIGTERM))
165 kill(td->pid, SIGTERM);
Jens Axboef63f7f32008-03-01 15:25:24 +0100166 }
Jens Axboeebac4652005-12-08 15:25:21 +0100167 }
168 }
169}
170
Jens Axboe697a6062008-05-31 00:04:45 +0200171static void sig_int(int sig)
172{
173 if (threads) {
Jens Axboe7b821682011-10-11 12:16:32 +0200174 if (is_backend)
175 fio_server_got_signal(sig);
176 else {
177 log_info("\nfio: terminating on signal %d\n", sig);
178 fflush(stdout);
179 exit_value = 128;
180 }
181
Jens Axboecc0df002011-10-03 20:53:32 +0200182 fio_terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +0100183 }
184}
185
Jens Axboe79857012011-01-14 14:14:58 +0100186static void *disk_thread_main(void *data)
Jens Axboea5b01f12010-11-11 09:19:49 +0100187{
Jens Axboe79857012011-01-14 14:14:58 +0100188 fio_mutex_up(startup_mutex);
189
190 while (threads) {
191 usleep(DISK_UTIL_MSEC * 1000);
Jens Axboe9a793c22011-01-14 15:22:14 +0100192 if (!threads)
193 break;
Jens Axboe79857012011-01-14 14:14:58 +0100194 update_io_ticks();
Jens Axboecf451d12011-10-03 16:48:30 +0200195
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200196 if (!is_backend)
Jens Axboecf451d12011-10-03 16:48:30 +0200197 print_thread_status();
Jens Axboe79857012011-01-14 14:14:58 +0100198 }
199
200 return NULL;
Jens Axboea5b01f12010-11-11 09:19:49 +0100201}
202
Jens Axboe79857012011-01-14 14:14:58 +0100203static int create_disk_util_thread(void)
Jens Axboea5b01f12010-11-11 09:19:49 +0100204{
Jens Axboe79857012011-01-14 14:14:58 +0100205 int ret;
Jens Axboea5b01f12010-11-11 09:19:49 +0100206
Jens Axboe69204d62011-01-14 18:29:19 +0100207 ret = pthread_create(&disk_util_thread, NULL, disk_thread_main, NULL);
Jens Axboe79857012011-01-14 14:14:58 +0100208 if (ret) {
209 log_err("Can't create disk util thread: %s\n", strerror(ret));
210 return 1;
211 }
Jens Axboea5b01f12010-11-11 09:19:49 +0100212
Jens Axboe79857012011-01-14 14:14:58 +0100213 ret = pthread_detach(disk_util_thread);
214 if (ret) {
215 log_err("Can't detatch disk util thread: %s\n", strerror(ret));
216 return 1;
217 }
Bruce Cran03e20d62011-01-02 20:14:54 +0100218
Jens Axboe79857012011-01-14 14:14:58 +0100219 dprint(FD_MUTEX, "wait on startup_mutex\n");
220 fio_mutex_down(startup_mutex);
221 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
222 return 0;
Jens Axboea5b01f12010-11-11 09:19:49 +0100223}
224
Jens Axboe697a6062008-05-31 00:04:45 +0200225static void set_sig_handlers(void)
226{
227 struct sigaction act;
228
229 memset(&act, 0, sizeof(act));
Jens Axboe697a6062008-05-31 00:04:45 +0200230 act.sa_handler = sig_int;
231 act.sa_flags = SA_RESTART;
232 sigaction(SIGINT, &act, NULL);
Jens Axboede79c912008-08-04 21:43:55 +0200233
234 memset(&act, 0, sizeof(act));
Jens Axboeb20b6be2011-05-11 13:23:01 +0200235 act.sa_handler = sig_int;
Jens Axboe5f8f9722008-11-18 19:10:21 +0100236 act.sa_flags = SA_RESTART;
Bruce Cran03e20d62011-01-02 20:14:54 +0100237 sigaction(SIGTERM, &act, NULL);
Jens Axboecc0df002011-10-03 20:53:32 +0200238
239 if (is_backend) {
240 memset(&act, 0, sizeof(act));
241 act.sa_handler = sig_int;
242 act.sa_flags = SA_RESTART;
243 sigaction(SIGPIPE, &act, NULL);
244 }
Jens Axboe697a6062008-05-31 00:04:45 +0200245}
246
247/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200248 * Check if we are above the minimum rate given.
249 */
Jens Axboe581e7142009-06-09 12:47:16 +0200250static int __check_min_rate(struct thread_data *td, struct timeval *now,
Steven Langbd93e342011-06-03 21:15:50 +0200251 enum fio_ddir ddir)
Jens Axboeebac4652005-12-08 15:25:21 +0100252{
Jens Axboe09042002007-02-23 10:56:22 +0100253 unsigned long long bytes = 0;
Jens Axboe4e991c22007-03-15 11:41:11 +0100254 unsigned long iops = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100255 unsigned long spent;
256 unsigned long rate;
Jens Axboe581e7142009-06-09 12:47:16 +0200257 unsigned int ratemin = 0;
258 unsigned int rate_iops = 0;
259 unsigned int rate_iops_min = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100260
Jens Axboeff58fce2010-08-25 12:02:08 +0200261 assert(ddir_rw(ddir));
262
Jens Axboed982d872009-06-10 06:35:48 +0200263 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
264 return 0;
265
Jens Axboeebac4652005-12-08 15:25:21 +0100266 /*
267 * allow a 2 second settle period in the beginning
268 */
269 if (mtime_since(&td->start, now) < 2000)
270 return 0;
271
Jens Axboec8eeb9d2011-10-05 14:02:22 +0200272 iops += td->this_io_blocks[ddir];
Jens Axboe581e7142009-06-09 12:47:16 +0200273 bytes += td->this_io_bytes[ddir];
274 ratemin += td->o.ratemin[ddir];
275 rate_iops += td->o.rate_iops[ddir];
276 rate_iops_min += td->o.rate_iops_min[ddir];
Jens Axboe09042002007-02-23 10:56:22 +0100277
Jens Axboeebac4652005-12-08 15:25:21 +0100278 /*
279 * if rate blocks is set, sample is running
280 */
Jens Axboe581e7142009-06-09 12:47:16 +0200281 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
282 spent = mtime_since(&td->lastrate[ddir], now);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100283 if (spent < td->o.ratecycle)
Jens Axboeebac4652005-12-08 15:25:21 +0100284 return 0;
285
Jens Axboe581e7142009-06-09 12:47:16 +0200286 if (td->o.rate[ddir]) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100287 /*
288 * check bandwidth specified rate
289 */
Jens Axboe581e7142009-06-09 12:47:16 +0200290 if (bytes < td->rate_bytes[ddir]) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100291 log_err("%s: min rate %u not met\n", td->o.name,
Jens Axboe581e7142009-06-09 12:47:16 +0200292 ratemin);
Jens Axboe413dd452007-02-23 09:26:09 +0100293 return 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100294 } else {
Jens Axboe581e7142009-06-09 12:47:16 +0200295 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
296 if (rate < ratemin ||
297 bytes < td->rate_bytes[ddir]) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100298 log_err("%s: min rate %u not met, got"
Jens Axboeb22989b2009-07-17 22:29:23 +0200299 " %luKB/sec\n", td->o.name,
Jens Axboe581e7142009-06-09 12:47:16 +0200300 ratemin, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100301 return 1;
302 }
303 }
304 } else {
305 /*
306 * checks iops specified rate
307 */
Jens Axboe581e7142009-06-09 12:47:16 +0200308 if (iops < rate_iops) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100309 log_err("%s: min iops rate %u not met\n",
Jens Axboe581e7142009-06-09 12:47:16 +0200310 td->o.name, rate_iops);
Jens Axboe4e991c22007-03-15 11:41:11 +0100311 return 1;
312 } else {
Jens Axboe581e7142009-06-09 12:47:16 +0200313 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
314 if (rate < rate_iops_min ||
315 iops < td->rate_blocks[ddir]) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100316 log_err("%s: min iops rate %u not met,"
317 " got %lu\n", td->o.name,
Jens Axboe581e7142009-06-09 12:47:16 +0200318 rate_iops_min, rate);
Jens Axboe4e991c22007-03-15 11:41:11 +0100319 }
Jens Axboe413dd452007-02-23 09:26:09 +0100320 }
Jens Axboeebac4652005-12-08 15:25:21 +0100321 }
322 }
323
Jens Axboe581e7142009-06-09 12:47:16 +0200324 td->rate_bytes[ddir] = bytes;
325 td->rate_blocks[ddir] = iops;
326 memcpy(&td->lastrate[ddir], now, sizeof(*now));
Jens Axboeebac4652005-12-08 15:25:21 +0100327 return 0;
328}
329
Jens Axboe581e7142009-06-09 12:47:16 +0200330static int check_min_rate(struct thread_data *td, struct timeval *now,
331 unsigned long *bytes_done)
332{
333 int ret = 0;
334
335 if (bytes_done[0])
336 ret |= __check_min_rate(td, now, 0);
337 if (bytes_done[1])
338 ret |= __check_min_rate(td, now, 1);
339
340 return ret;
341}
342
Jens Axboeebac4652005-12-08 15:25:21 +0100343static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
344{
Steven Noonane4413f02012-01-30 13:34:04 -0800345 if (in_ramp_time(td))
346 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100347 if (!td->o.timeout)
Jens Axboeebac4652005-12-08 15:25:21 +0100348 return 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100349 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
Jens Axboeebac4652005-12-08 15:25:21 +0100350 return 1;
351
352 return 0;
353}
354
Jens Axboe906c8d72006-06-13 09:37:56 +0200355/*
356 * When job exits, we can cancel the in-flight IO if we are using async
357 * io. Attempt to do so.
358 */
Jens Axboeebac4652005-12-08 15:25:21 +0100359static void cleanup_pending_aio(struct thread_data *td)
360{
Jens Axboe01743ee2008-06-02 12:19:19 +0200361 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100362 struct io_u *io_u;
363 int r;
364
365 /*
366 * get immediately available events, if any
367 */
Jens Axboe581e7142009-06-09 12:47:16 +0200368 r = io_u_queued_complete(td, 0, NULL);
Jens Axboeb2fdda42007-02-20 20:52:51 +0100369 if (r < 0)
370 return;
Jens Axboeebac4652005-12-08 15:25:21 +0100371
372 /*
373 * now cancel remaining active events
374 */
Jens Axboe2866c822006-10-09 15:57:48 +0200375 if (td->io_ops->cancel) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200376 flist_for_each_safe(entry, n, &td->io_u_busylist) {
377 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100378
Jens Axboe0c6e7512007-02-22 11:19:39 +0100379 /*
380 * if the io_u isn't in flight, then that generally
381 * means someone leaked an io_u. complain but fix
382 * it up, so we don't stall here.
383 */
384 if ((io_u->flags & IO_U_F_FLIGHT) == 0) {
385 log_err("fio: non-busy IO on busy list\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100386 put_io_u(td, io_u);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100387 } else {
388 r = td->io_ops->cancel(td, io_u);
389 if (!r)
390 put_io_u(td, io_u);
391 }
Jens Axboeebac4652005-12-08 15:25:21 +0100392 }
393 }
394
Jens Axboe97601022007-02-18 12:47:29 +0100395 if (td->cur_depth)
Jens Axboe581e7142009-06-09 12:47:16 +0200396 r = io_u_queued_complete(td, td->cur_depth, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +0100397}
398
Jens Axboe906c8d72006-06-13 09:37:56 +0200399/*
Jens Axboe858a3d42006-10-24 14:54:44 +0200400 * Helper to handle the final sync of a file. Works just like the normal
401 * io path, just does everything sync.
402 */
403static int fio_io_sync(struct thread_data *td, struct fio_file *f)
404{
405 struct io_u *io_u = __get_io_u(td);
Jens Axboe858a3d42006-10-24 14:54:44 +0200406 int ret;
407
408 if (!io_u)
409 return 1;
410
411 io_u->ddir = DDIR_SYNC;
412 io_u->file = f;
413
414 if (td_io_prep(td, io_u)) {
415 put_io_u(td, io_u);
416 return 1;
417 }
418
Jens Axboe755200a2007-02-19 13:08:12 +0100419requeue:
Jens Axboe858a3d42006-10-24 14:54:44 +0200420 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100421 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100422 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe858a3d42006-10-24 14:54:44 +0200423 put_io_u(td, io_u);
Jens Axboe858a3d42006-10-24 14:54:44 +0200424 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100425 } else if (ret == FIO_Q_QUEUED) {
Jens Axboe581e7142009-06-09 12:47:16 +0200426 if (io_u_queued_complete(td, 1, NULL) < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100427 return 1;
Jens Axboe36167d82007-02-18 05:41:31 +0100428 } else if (ret == FIO_Q_COMPLETED) {
429 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100430 td_verror(td, io_u->error, "td_io_queue");
Jens Axboe36167d82007-02-18 05:41:31 +0100431 return 1;
432 }
Jens Axboe858a3d42006-10-24 14:54:44 +0200433
Jens Axboe581e7142009-06-09 12:47:16 +0200434 if (io_u_sync_complete(td, io_u, NULL) < 0)
Jens Axboeb2fdda42007-02-20 20:52:51 +0100435 return 1;
Jens Axboe755200a2007-02-19 13:08:12 +0100436 } else if (ret == FIO_Q_BUSY) {
437 if (td_io_commit(td))
438 return 1;
439 goto requeue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200440 }
441
442 return 0;
443}
444
Jens Axboe687c6a22011-07-07 12:36:59 +0200445static inline void __update_tv_cache(struct thread_data *td)
446{
447 fio_gettime(&td->tv_cache, NULL);
448}
449
Jens Axboe993bf482008-11-14 13:04:53 +0100450static inline void update_tv_cache(struct thread_data *td)
451{
452 if ((++td->tv_cache_nr & td->tv_cache_mask) == td->tv_cache_mask)
Jens Axboe687c6a22011-07-07 12:36:59 +0200453 __update_tv_cache(td);
Jens Axboe993bf482008-11-14 13:04:53 +0100454}
455
Steven Lang06842022011-11-17 09:45:17 +0100456static int break_on_this_error(struct thread_data *td, enum fio_ddir ddir,
457 int *retptr)
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200458{
459 int ret = *retptr;
460
461 if (ret < 0 || td->error) {
462 int err;
463
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200464 if (ret < 0)
465 err = -ret;
466 else
467 err = td->error;
468
Steven Lang06842022011-11-17 09:45:17 +0100469 if (!(td->o.continue_on_error & td_error_type(ddir, err)))
470 return 1;
471
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200472 if (td_non_fatal_error(err)) {
473 /*
474 * Continue with the I/Os in case of
475 * a non fatal error.
476 */
Radha Ramachandran8a4f8a22009-07-02 08:30:49 +0200477 update_error_count(td, err);
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200478 td_clear_error(td);
479 *retptr = 0;
480 return 0;
Radha Ramachandran8a4f8a22009-07-02 08:30:49 +0200481 } else if (td->o.fill_device && err == ENOSPC) {
482 /*
483 * We expect to hit this error if
484 * fill_device option is set.
485 */
486 td_clear_error(td);
487 td->terminate = 1;
488 return 1;
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200489 } else {
490 /*
491 * Stop the I/O in case of a fatal
492 * error.
493 */
Radha Ramachandran8a4f8a22009-07-02 08:30:49 +0200494 update_error_count(td, err);
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200495 return 1;
496 }
497 }
498
499 return 0;
500}
501
Jens Axboe858a3d42006-10-24 14:54:44 +0200502/*
Jens Axboe34403fb2007-03-02 21:43:25 +0100503 * The main verify engine. Runs over the writes we previously submitted,
Jens Axboe906c8d72006-06-13 09:37:56 +0200504 * reads the blocks back in, and checks the crc/md5 of the data.
505 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100506static void do_verify(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100507{
Jens Axboe53cdc682006-10-18 11:50:58 +0200508 struct fio_file *f;
Jens Axboe36167d82007-02-18 05:41:31 +0100509 struct io_u *io_u;
Jens Axboeaf52b342007-03-13 10:07:47 +0100510 int ret, min_events;
511 unsigned int i;
Jens Axboee5b401d2006-10-18 16:03:40 +0200512
Jens Axboe008618a2010-03-18 19:29:45 +0100513 dprint(FD_VERIFY, "starting loop\n");
514
Jens Axboee5b401d2006-10-18 16:03:40 +0200515 /*
516 * sync io first and invalidate cache, to make sure we really
517 * read from disk.
518 */
519 for_each_file(td, f, i) {
Jens Axboed6aed792009-06-03 08:41:15 +0200520 if (!fio_file_open(f))
Jens Axboe3d7b4852007-03-13 14:50:28 +0100521 continue;
Jens Axboeb2fdda42007-02-20 20:52:51 +0100522 if (fio_io_sync(td, f))
523 break;
524 if (file_invalidate_cache(td, f))
525 break;
Jens Axboee5b401d2006-10-18 16:03:40 +0200526 }
Jens Axboeebac4652005-12-08 15:25:21 +0100527
Jens Axboeb2fdda42007-02-20 20:52:51 +0100528 if (td->error)
529 return;
530
Jens Axboeebac4652005-12-08 15:25:21 +0100531 td_set_runstate(td, TD_VERIFYING);
532
Jens Axboe36167d82007-02-18 05:41:31 +0100533 io_u = NULL;
534 while (!td->terminate) {
Jens Axboe49504212008-06-05 09:03:30 +0200535 int ret2, full;
Jens Axboe54517922007-03-05 10:06:06 +0100536
Jens Axboe993bf482008-11-14 13:04:53 +0100537 update_tv_cache(td);
538
539 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe687c6a22011-07-07 12:36:59 +0200540 __update_tv_cache(td);
541 if (runtime_exceeded(td, &td->tv_cache)) {
542 td->terminate = 1;
543 break;
544 }
Jens Axboe069c2912007-02-21 23:02:39 +0100545 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200546
Radha Ramachandrandcb48302009-06-16 08:31:29 +0200547 io_u = __get_io_u(td);
548 if (!io_u)
549 break;
550
Jens Axboe069c2912007-02-21 23:02:39 +0100551 if (get_next_verify(td, io_u)) {
552 put_io_u(td, io_u);
Jens Axboeebac4652005-12-08 15:25:21 +0100553 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100554 }
Jens Axboeebac4652005-12-08 15:25:21 +0100555
Jens Axboe069c2912007-02-21 23:02:39 +0100556 if (td_io_prep(td, io_u)) {
557 put_io_u(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100558 break;
Jens Axboe069c2912007-02-21 23:02:39 +0100559 }
Jens Axboed7762cf2007-02-23 12:34:57 +0100560
Jens Axboee8462bd2009-07-06 12:59:04 +0200561 if (td->o.verify_async)
562 io_u->end_io = verify_io_u_async;
563 else
564 io_u->end_io = verify_io_u;
Jens Axboe36167d82007-02-18 05:41:31 +0100565
Jens Axboe11786802007-03-05 18:33:22 +0100566 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100567 switch (ret) {
568 case FIO_Q_COMPLETED:
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200569 if (io_u->error) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100570 ret = -io_u->error;
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200571 clear_io_u(td, io_u);
572 } else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100573 int bytes = io_u->xfer_buflen - io_u->resid;
574
Jens Axboe9e9d1642007-03-12 09:37:46 +0100575 /*
576 * zero read, fail
577 */
578 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200579 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100580 put_io_u(td, io_u);
581 break;
582 }
Jens Axboe8400d9b2007-03-28 11:10:09 +0200583
Jens Axboe36167d82007-02-18 05:41:31 +0100584 io_u->xfer_buflen = io_u->resid;
585 io_u->xfer_buf += bytes;
Jens Axboe8400d9b2007-03-28 11:10:09 +0200586 io_u->offset += bytes;
587
Jens Axboeff58fce2010-08-25 12:02:08 +0200588 if (ddir_rw(io_u->ddir))
589 td->ts.short_io_u[io_u->ddir]++;
Jens Axboe30061b92007-04-17 13:31:34 +0200590
Jens Axboe2b13e712011-01-19 14:04:16 -0700591 f = io_u->file;
Jens Axboed460eb32007-04-16 21:39:33 +0200592 if (io_u->offset == f->real_file_size)
Jens Axboe8400d9b2007-03-28 11:10:09 +0200593 goto sync_done;
594
Jens Axboe11786802007-03-05 18:33:22 +0100595 requeue_io_u(td, &io_u);
596 } else {
Jens Axboe8400d9b2007-03-28 11:10:09 +0200597sync_done:
Jens Axboe581e7142009-06-09 12:47:16 +0200598 ret = io_u_sync_complete(td, io_u, NULL);
Jens Axboe11786802007-03-05 18:33:22 +0100599 if (ret < 0)
600 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100601 }
Jens Axboe36167d82007-02-18 05:41:31 +0100602 continue;
603 case FIO_Q_QUEUED:
604 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100605 case FIO_Q_BUSY:
606 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100607 ret2 = td_io_commit(td);
608 if (ret2 < 0)
609 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100610 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100611 default:
612 assert(ret < 0);
Jens Axboee1161c32007-02-22 19:36:48 +0100613 td_verror(td, -ret, "td_io_queue");
Jens Axboeebac4652005-12-08 15:25:21 +0100614 break;
615 }
616
Steven Lang06842022011-11-17 09:45:17 +0100617 if (break_on_this_error(td, io_u->ddir, &ret))
Jens Axboeebac4652005-12-08 15:25:21 +0100618 break;
619
Jens Axboe3af6ef32007-02-18 06:57:43 +0100620 /*
621 * if we can queue more, do so. but check if there are
Jens Axboeccc02052011-07-14 12:45:13 +0200622 * completed io_u's first. Note that we can get BUSY even
623 * without IO queued, if the system is resource starved.
Jens Axboe3af6ef32007-02-18 06:57:43 +0100624 */
Jens Axboeccc02052011-07-14 12:45:13 +0200625 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
Jens Axboe49504212008-06-05 09:03:30 +0200626 if (full || !td->o.iodepth_batch_complete) {
Radha Ramachandran6a962762009-11-05 08:33:49 +0100627 min_events = min(td->o.iodepth_batch_complete,
628 td->cur_depth);
Dan Ehrenberg1f84f1d2011-07-23 13:36:44 +0200629 if (full && !min_events && td->o.iodepth_batch_complete != 0)
Jens Axboe838bc702008-05-22 13:08:23 +0200630 min_events = 1;
Jens Axboee916b392007-02-20 14:37:26 +0100631
Jens Axboe49504212008-06-05 09:03:30 +0200632 do {
633 /*
634 * Reap required number of io units, if any,
635 * and do the verification on them through
636 * the callback handler
637 */
Jens Axboe581e7142009-06-09 12:47:16 +0200638 if (io_u_queued_complete(td, min_events, NULL) < 0) {
Jens Axboe49504212008-06-05 09:03:30 +0200639 ret = -1;
640 break;
641 }
642 } while (full && (td->cur_depth > td->o.iodepth_low));
643 }
644 if (ret < 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100645 break;
646 }
647
Jens Axboec01c0392007-02-26 12:43:42 +0100648 if (!td->error) {
649 min_events = td->cur_depth;
650
651 if (min_events)
Jens Axboe581e7142009-06-09 12:47:16 +0200652 ret = io_u_queued_complete(td, min_events, NULL);
Jens Axboec01c0392007-02-26 12:43:42 +0100653 } else
Jens Axboeebac4652005-12-08 15:25:21 +0100654 cleanup_pending_aio(td);
655
656 td_set_runstate(td, TD_RUNNING);
Jens Axboe008618a2010-03-18 19:29:45 +0100657
658 dprint(FD_VERIFY, "exiting loop\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100659}
660
Jens Axboe32cd46a2006-06-07 13:40:40 +0200661/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200662 * Main IO worker function. It retrieves io_u's to process and queues
Jens Axboe32cd46a2006-06-07 13:40:40 +0200663 * and reaps them, checking for rate and errors along the way.
664 */
Jens Axboeebac4652005-12-08 15:25:21 +0100665static void do_io(struct thread_data *td)
666{
Jens Axboeaf52b342007-03-13 10:07:47 +0100667 unsigned int i;
668 int ret = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100669
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200670 if (in_ramp_time(td))
671 td_set_runstate(td, TD_RAMP);
672 else
673 td_set_runstate(td, TD_RUNNING);
Jens Axboe5853e5a2006-06-08 14:41:05 +0200674
Glen Ogilvie457bf392009-11-03 21:52:36 +0100675 while ( (td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
Jens Axboea917a8b2010-09-02 13:23:20 +0200676 (!flist_empty(&td->trim_list)) ||
Glen Ogilvie457bf392009-11-03 21:52:36 +0100677 ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) ) {
Jens Axboe97601022007-02-18 12:47:29 +0100678 struct timeval comp_time;
Jens Axboe581e7142009-06-09 12:47:16 +0200679 unsigned long bytes_done[2] = { 0, 0 };
Jens Axboe84585002006-10-19 20:26:22 +0200680 int min_evts = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100681 struct io_u *io_u;
Jens Axboe49504212008-06-05 09:03:30 +0200682 int ret2, full;
Steven Lang06842022011-11-17 09:45:17 +0100683 enum fio_ddir ddir;
Jens Axboeebac4652005-12-08 15:25:21 +0100684
685 if (td->terminate)
686 break;
687
Jens Axboe993bf482008-11-14 13:04:53 +0100688 update_tv_cache(td);
Jens Axboe97601022007-02-18 12:47:29 +0100689
Jens Axboe993bf482008-11-14 13:04:53 +0100690 if (runtime_exceeded(td, &td->tv_cache)) {
Jens Axboe687c6a22011-07-07 12:36:59 +0200691 __update_tv_cache(td);
692 if (runtime_exceeded(td, &td->tv_cache)) {
693 td->terminate = 1;
694 break;
695 }
Jens Axboe97601022007-02-18 12:47:29 +0100696 }
Jens Axboe36167d82007-02-18 05:41:31 +0100697
Radha Ramachandrandcb48302009-06-16 08:31:29 +0200698 io_u = get_io_u(td);
699 if (!io_u)
700 break;
701
Steven Lang06842022011-11-17 09:45:17 +0100702 ddir = io_u->ddir;
703
Jens Axboeb67740d2007-07-26 12:22:30 +0200704 /*
705 * Add verification end_io handler, if asked to verify
706 * a previously written file.
707 */
Jens Axboe715d2b32009-09-04 20:53:53 +0200708 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
709 !td_rw(td)) {
Jens Axboee8462bd2009-07-06 12:59:04 +0200710 if (td->o.verify_async)
711 io_u->end_io = verify_io_u_async;
712 else
713 io_u->end_io = verify_io_u;
Jens Axboed8fab1b2008-03-06 10:25:17 +0100714 td_set_runstate(td, TD_VERIFYING);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200715 } else if (in_ramp_time(td))
716 td_set_runstate(td, TD_RAMP);
717 else
Jens Axboed8fab1b2008-03-06 10:25:17 +0100718 td_set_runstate(td, TD_RUNNING);
Jens Axboeb67740d2007-07-26 12:22:30 +0200719
Jens Axboe11786802007-03-05 18:33:22 +0100720 ret = td_io_queue(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100721 switch (ret) {
722 case FIO_Q_COMPLETED:
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200723 if (io_u->error) {
Jens Axboe54517922007-03-05 10:06:06 +0100724 ret = -io_u->error;
Radha Ramachandranf2bba182009-06-15 08:40:16 +0200725 clear_io_u(td, io_u);
726 } else if (io_u->resid) {
Jens Axboe36167d82007-02-18 05:41:31 +0100727 int bytes = io_u->xfer_buflen - io_u->resid;
Jens Axboed460eb32007-04-16 21:39:33 +0200728 struct fio_file *f = io_u->file;
Jens Axboe36167d82007-02-18 05:41:31 +0100729
Jens Axboe9e9d1642007-03-12 09:37:46 +0100730 /*
731 * zero read, fail
732 */
733 if (!bytes) {
Jens Axboe41d9f0f2008-06-02 12:19:50 +0200734 td_verror(td, EIO, "full resid");
Jens Axboe9e9d1642007-03-12 09:37:46 +0100735 put_io_u(td, io_u);
736 break;
737 }
738
Jens Axboe36167d82007-02-18 05:41:31 +0100739 io_u->xfer_buflen = io_u->resid;
740 io_u->xfer_buf += bytes;
Jens Axboe5a7c5682007-03-28 11:07:20 +0200741 io_u->offset += bytes;
742
Jens Axboeff58fce2010-08-25 12:02:08 +0200743 if (ddir_rw(io_u->ddir))
744 td->ts.short_io_u[io_u->ddir]++;
Jens Axboe30061b92007-04-17 13:31:34 +0200745
Jens Axboed460eb32007-04-16 21:39:33 +0200746 if (io_u->offset == f->real_file_size)
Jens Axboe5a7c5682007-03-28 11:07:20 +0200747 goto sync_done;
748
Jens Axboe11786802007-03-05 18:33:22 +0100749 requeue_io_u(td, &io_u);
750 } else {
Jens Axboe5a7c5682007-03-28 11:07:20 +0200751sync_done:
Jens Axboe581e7142009-06-09 12:47:16 +0200752 if (__should_check_rate(td, 0) ||
753 __should_check_rate(td, 1))
Jens Axboe993bf482008-11-14 13:04:53 +0100754 fio_gettime(&comp_time, NULL);
755
Jens Axboe581e7142009-06-09 12:47:16 +0200756 ret = io_u_sync_complete(td, io_u, bytes_done);
757 if (ret < 0)
758 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100759 }
Jens Axboe36167d82007-02-18 05:41:31 +0100760 break;
761 case FIO_Q_QUEUED:
Jens Axboe7e77dd02007-02-20 10:57:34 +0100762 /*
763 * if the engine doesn't have a commit hook,
764 * the io_u is really queued. if it does have such
765 * a hook, it has to call io_u_queued() itself.
766 */
767 if (td->io_ops->commit == NULL)
768 io_u_queued(td, io_u);
Jens Axboe36167d82007-02-18 05:41:31 +0100769 break;
Jens Axboe755200a2007-02-19 13:08:12 +0100770 case FIO_Q_BUSY:
771 requeue_io_u(td, &io_u);
Jens Axboe54517922007-03-05 10:06:06 +0100772 ret2 = td_io_commit(td);
773 if (ret2 < 0)
774 ret = ret2;
Jens Axboe755200a2007-02-19 13:08:12 +0100775 break;
Jens Axboe36167d82007-02-18 05:41:31 +0100776 default:
777 assert(ret < 0);
778 put_io_u(td, io_u);
779 break;
Jens Axboeebac4652005-12-08 15:25:21 +0100780 }
781
Steven Lang06842022011-11-17 09:45:17 +0100782 if (break_on_this_error(td, ddir, &ret))
Jens Axboe36167d82007-02-18 05:41:31 +0100783 break;
784
Jens Axboe97601022007-02-18 12:47:29 +0100785 /*
Jens Axboeccc02052011-07-14 12:45:13 +0200786 * See if we need to complete some commands. Note that we
787 * can get BUSY even without IO queued, if the system is
788 * resource starved.
Jens Axboe97601022007-02-18 12:47:29 +0100789 */
Jens Axboeccc02052011-07-14 12:45:13 +0200790 full = queue_full(td) || (ret == FIO_Q_BUSY && td->cur_depth);
Jens Axboe49504212008-06-05 09:03:30 +0200791 if (full || !td->o.iodepth_batch_complete) {
Radha Ramachandran6a962762009-11-05 08:33:49 +0100792 min_evts = min(td->o.iodepth_batch_complete,
793 td->cur_depth);
Dan Ehrenberg1f84f1d2011-07-23 13:36:44 +0200794 if (full && !min_evts && td->o.iodepth_batch_complete != 0)
Jens Axboe36167d82007-02-18 05:41:31 +0100795 min_evts = 1;
Jens Axboe49504212008-06-05 09:03:30 +0200796
Jens Axboe581e7142009-06-09 12:47:16 +0200797 if (__should_check_rate(td, 0) ||
798 __should_check_rate(td, 1))
Jens Axboe993bf482008-11-14 13:04:53 +0100799 fio_gettime(&comp_time, NULL);
Jens Axboe49504212008-06-05 09:03:30 +0200800
801 do {
Jens Axboe581e7142009-06-09 12:47:16 +0200802 ret = io_u_queued_complete(td, min_evts, bytes_done);
803 if (ret < 0)
Jens Axboe49504212008-06-05 09:03:30 +0200804 break;
805
Jens Axboe49504212008-06-05 09:03:30 +0200806 } while (full && (td->cur_depth > td->o.iodepth_low));
Jens Axboeebac4652005-12-08 15:25:21 +0100807 }
808
Jens Axboe49504212008-06-05 09:03:30 +0200809 if (ret < 0)
810 break;
Jens Axboe581e7142009-06-09 12:47:16 +0200811 if (!(bytes_done[0] + bytes_done[1]))
Jens Axboe97601022007-02-18 12:47:29 +0100812 continue;
813
Jens Axboe581e7142009-06-09 12:47:16 +0200814 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
815 if (check_min_rate(td, &comp_time, bytes_done)) {
Jens Axboe721938a2008-09-10 09:46:16 +0200816 if (exitall_on_terminate)
Jens Axboecc0df002011-10-03 20:53:32 +0200817 fio_terminate_threads(td->groupid);
Jens Axboe721938a2008-09-10 09:46:16 +0200818 td_verror(td, EIO, "check_min_rate");
819 break;
820 }
Jens Axboeebac4652005-12-08 15:25:21 +0100821 }
822
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100823 if (td->o.thinktime) {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100824 unsigned long long b;
825
826 b = td->io_blocks[0] + td->io_blocks[1];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100827 if (!(b % td->o.thinktime_blocks)) {
Jens Axboe48097d52007-02-17 06:30:44 +0100828 int left;
829
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100830 if (td->o.thinktime_spin)
Jens Axboe6dd6f2c2008-12-10 13:24:12 +0100831 usec_spin(td->o.thinktime_spin);
Jens Axboe48097d52007-02-17 06:30:44 +0100832
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100833 left = td->o.thinktime - td->o.thinktime_spin;
Jens Axboe48097d52007-02-17 06:30:44 +0100834 if (left)
835 usec_sleep(td, left);
836 }
Jens Axboe9c1f7432007-01-03 20:43:19 +0100837 }
Jens Axboeebac4652005-12-08 15:25:21 +0100838 }
839
Jens Axboed708ac52010-09-02 13:29:55 +0200840 if (td->trim_entries)
Jens Axboe18320cf2011-10-18 14:39:30 +0200841 log_err("fio: %d trim entries leaked?\n", td->trim_entries);
Jens Axboea917a8b2010-09-02 13:23:20 +0200842
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100843 if (td->o.fill_device && td->error == ENOSPC) {
844 td->error = 0;
845 td->terminate = 1;
846 }
Jens Axboe4d2413c2006-11-23 11:58:59 +0100847 if (!td->error) {
Jens Axboe3d7c3912007-02-19 13:16:12 +0100848 struct fio_file *f;
849
Jens Axboec01c0392007-02-26 12:43:42 +0100850 i = td->cur_depth;
Steven Langd06093e2011-06-13 09:47:36 +0200851 if (i) {
Jens Axboe581e7142009-06-09 12:47:16 +0200852 ret = io_u_queued_complete(td, i, NULL);
Steven Langd06093e2011-06-13 09:47:36 +0200853 if (td->o.fill_device && td->error == ENOSPC)
854 td->error = 0;
855 }
Jens Axboeebac4652005-12-08 15:25:21 +0100856
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100857 if (should_fsync(td) && td->o.end_fsync) {
Jens Axboe84585002006-10-19 20:26:22 +0200858 td_set_runstate(td, TD_FSYNCING);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100859
860 for_each_file(td, f, i) {
Jens Axboed6aed792009-06-03 08:41:15 +0200861 if (!fio_file_open(f))
Jens Axboe3d7b4852007-03-13 14:50:28 +0100862 continue;
Jens Axboe858a3d42006-10-24 14:54:44 +0200863 fio_io_sync(td, f);
Jens Axboe3d7b4852007-03-13 14:50:28 +0100864 }
Jens Axboe84585002006-10-19 20:26:22 +0200865 }
Jens Axboec01c0392007-02-26 12:43:42 +0100866 } else
867 cleanup_pending_aio(td);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100868
869 /*
870 * stop job if we failed doing any IO
871 */
872 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
873 td->done = 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100874}
875
Jens Axboeebac4652005-12-08 15:25:21 +0100876static void cleanup_io_u(struct thread_data *td)
877{
Jens Axboe01743ee2008-06-02 12:19:19 +0200878 struct flist_head *entry, *n;
Jens Axboeebac4652005-12-08 15:25:21 +0100879 struct io_u *io_u;
880
Jens Axboe01743ee2008-06-02 12:19:19 +0200881 flist_for_each_safe(entry, n, &td->io_u_freelist) {
882 io_u = flist_entry(entry, struct io_u, list);
Jens Axboeebac4652005-12-08 15:25:21 +0100883
Jens Axboe01743ee2008-06-02 12:19:19 +0200884 flist_del(&io_u->list);
Jens Axboe91aea6e2011-04-25 20:46:58 +0200885 fio_memfree(io_u, sizeof(*io_u));
Jens Axboeebac4652005-12-08 15:25:21 +0100886 }
887
Jens Axboe2f9ade32006-10-20 11:25:52 +0200888 free_io_mem(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100889}
890
891static int init_io_u(struct thread_data *td)
892{
893 struct io_u *io_u;
Jens Axboea00735e2006-11-03 08:58:08 +0100894 unsigned int max_bs;
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200895 int cl_align, i, max_units;
Jens Axboeebac4652005-12-08 15:25:21 +0100896 char *p;
897
Jens Axboe1e3c09a2008-02-04 10:48:13 +0100898 max_units = td->o.iodepth;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100899 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100900 td->orig_buffer_size = (unsigned long long) max_bs
901 * (unsigned long long) max_units;
Jens Axboe74b025b2006-12-19 15:18:14 +0100902
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100903 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
904 unsigned long bs;
905
906 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
907 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
908 }
Jens Axboeebac4652005-12-08 15:25:21 +0100909
Jens Axboec3990642007-07-19 10:17:09 +0200910 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
911 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
912 return 1;
913 }
914
Jens Axboe2f9ade32006-10-20 11:25:52 +0200915 if (allocate_io_mem(td))
916 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100917
Jens Axboe1c04bf72010-09-07 13:28:17 +0200918 if (td->o.odirect || td->o.mem_align ||
919 (td->io_ops->flags & FIO_RAWIO))
Jens Axboed529ee12009-07-01 10:33:03 +0200920 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
Jens Axboeb3f378e2007-07-20 12:39:22 +0200921 else
922 p = td->orig_buffer;
923
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200924 cl_align = os_cache_line_size();
925
Jens Axboeebac4652005-12-08 15:25:21 +0100926 for (i = 0; i < max_units; i++) {
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200927 void *ptr;
928
Jens Axboedb1cd902007-04-26 13:47:07 +0200929 if (td->terminate)
930 return 1;
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200931
Jens Axboe91aea6e2011-04-25 20:46:58 +0200932 ptr = fio_memalign(cl_align, sizeof(*io_u));
933 if (!ptr) {
934 log_err("fio: unable to allocate aligned memory\n");
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200935 break;
936 }
937
938 io_u = ptr;
Jens Axboeebac4652005-12-08 15:25:21 +0100939 memset(io_u, 0, sizeof(*io_u));
Jens Axboe01743ee2008-06-02 12:19:19 +0200940 INIT_FLIST_HEAD(&io_u->list);
Jens Axboed529ee12009-07-01 10:33:03 +0200941 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
Jens Axboeebac4652005-12-08 15:25:21 +0100942
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200943 if (!(td->io_ops->flags & FIO_NOIO)) {
Jens Axboecf00f972011-10-22 18:47:21 +0200944 io_u->buf = p;
Jens Axboed529ee12009-07-01 10:33:03 +0200945 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
Jens Axboee9459e52007-04-17 15:46:32 +0200946
Jens Axboe3545a102011-08-31 15:20:15 -0600947 if (td_write(td))
Jens Axboe5973caf2008-05-21 19:52:35 +0200948 io_u_fill_buffer(td, io_u, max_bs);
Jens Axboe3545a102011-08-31 15:20:15 -0600949 if (td_write(td) && td->o.verify_pattern_bytes) {
Radha Ramachandrancbe8d752010-07-14 08:36:07 +0200950 /*
951 * Fill the buffer with the pattern if we are
952 * going to be doing writes.
953 */
Jens Axboe7d9fb452011-01-11 22:16:49 +0100954 fill_pattern(td, io_u->buf, max_bs, io_u, 0, 0);
Radha Ramachandrancbe8d752010-07-14 08:36:07 +0200955 }
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200956 }
Jens Axboe6b9cea22006-10-30 17:00:24 +0100957
Jens Axboeb1ff3402006-02-17 11:35:58 +0100958 io_u->index = i;
Jens Axboe0c6e7512007-02-22 11:19:39 +0100959 io_u->flags = IO_U_F_FREE;
Jens Axboe01743ee2008-06-02 12:19:19 +0200960 flist_add(&io_u->list, &td->io_u_freelist);
Jens Axboecf00f972011-10-22 18:47:21 +0200961 p += max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100962 }
963
964 return 0;
965}
966
Jens Axboeda867742006-06-06 10:39:10 -0700967static int switch_ioscheduler(struct thread_data *td)
968{
969 char tmp[256], tmp2[128];
970 FILE *f;
971 int ret;
972
Jens Axboeba0fbe12007-03-09 14:34:23 +0100973 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +0200974 return 0;
975
Jens Axboeda867742006-06-06 10:39:10 -0700976 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
977
978 f = fopen(tmp, "r+");
979 if (!f) {
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200980 if (errno == ENOENT) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100981 log_err("fio: os or kernel doesn't support IO scheduler"
982 " switching\n");
Jens Axboecbf5c1d2007-04-16 12:56:00 +0200983 return 0;
984 }
985 td_verror(td, errno, "fopen iosched");
Jens Axboeda867742006-06-06 10:39:10 -0700986 return 1;
987 }
988
989 /*
990 * Set io scheduler.
991 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100992 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
Jens Axboeda867742006-06-06 10:39:10 -0700993 if (ferror(f) || ret != 1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100994 td_verror(td, errno, "fwrite");
Jens Axboeda867742006-06-06 10:39:10 -0700995 fclose(f);
996 return 1;
997 }
998
999 rewind(f);
1000
1001 /*
1002 * Read back and check that the selected scheduler is now the default.
1003 */
1004 ret = fread(tmp, 1, sizeof(tmp), f);
1005 if (ferror(f) || ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001006 td_verror(td, errno, "fread");
Jens Axboeda867742006-06-06 10:39:10 -07001007 fclose(f);
1008 return 1;
1009 }
1010
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001011 sprintf(tmp2, "[%s]", td->o.ioscheduler);
Jens Axboeda867742006-06-06 10:39:10 -07001012 if (!strstr(tmp, tmp2)) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001013 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
Jens Axboee1161c32007-02-22 19:36:48 +01001014 td_verror(td, EINVAL, "iosched_switch");
Jens Axboeda867742006-06-06 10:39:10 -07001015 fclose(f);
1016 return 1;
1017 }
1018
1019 fclose(f);
1020 return 0;
1021}
1022
Jens Axboe492158c2007-05-24 10:32:47 +02001023static int keep_running(struct thread_data *td)
1024{
1025 unsigned long long io_done;
1026
Jens Axboe20e354e2007-07-23 14:36:16 +02001027 if (td->done)
1028 return 0;
Jens Axboe492158c2007-05-24 10:32:47 +02001029 if (td->o.time_based)
1030 return 1;
1031 if (td->o.loops) {
1032 td->o.loops--;
1033 return 1;
1034 }
1035
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001036 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
1037 + td->io_skip_bytes;
Jens Axboe492158c2007-05-24 10:32:47 +02001038 if (io_done < td->o.size)
1039 return 1;
1040
1041 return 0;
1042}
1043
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001044static void reset_io_counters(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +01001045{
Jens Axboef0505a12011-10-03 12:12:03 +02001046 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001047 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
Jens Axboec8eeb9d2011-10-05 14:02:22 +02001048 td->stat_io_blocks[0] = td->stat_io_blocks[1] = 0;
1049 td->this_io_blocks[0] = td->this_io_blocks[1] = 0;
Jens Axboe20dc95c2005-12-09 10:29:35 +01001050 td->zone_bytes = 0;
Jens Axboe581e7142009-06-09 12:47:16 +02001051 td->rate_bytes[0] = td->rate_bytes[1] = 0;
1052 td->rate_blocks[0] = td->rate_blocks[1] = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001053
Jens Axboec1324df2007-02-19 19:06:41 +01001054 td->last_was_sync = 0;
1055
Jens Axboe2ba1c292008-02-01 13:16:38 +01001056 /*
1057 * reset file done count if we are to start over
1058 */
1059 if (td->o.time_based || td->o.loops)
Jens Axboe48f5abd2007-07-20 13:25:04 +02001060 td->nr_done_files = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001061}
1062
1063void reset_all_stats(struct thread_data *td)
1064{
1065 struct timeval tv;
1066 int i;
1067
1068 reset_io_counters(td);
1069
1070 for (i = 0; i < 2; i++) {
1071 td->io_bytes[i] = 0;
1072 td->io_blocks[i] = 0;
1073 td->io_issues[i] = 0;
1074 td->ts.total_io_u[i] = 0;
1075 }
Bruce Cran9b836562011-01-08 19:49:54 +01001076
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001077 fio_gettime(&tv, NULL);
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001078 td->ts.runtime[0] = 0;
1079 td->ts.runtime[1] = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001080 memcpy(&td->epoch, &tv, sizeof(tv));
1081 memcpy(&td->start, &tv, sizeof(tv));
1082}
1083
Jens Axboedf9c26b2009-03-05 10:13:58 +01001084static void clear_io_state(struct thread_data *td)
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001085{
1086 struct fio_file *f;
1087 unsigned int i;
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001088
1089 reset_io_counters(td);
Jens Axboe281f9b62007-05-23 09:34:42 +02001090
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001091 close_files(td);
Jens Axboedf9c26b2009-03-05 10:13:58 +01001092 for_each_file(td, f, i)
Jens Axboed6aed792009-06-03 08:41:15 +02001093 fio_file_clear_done(f);
Jens Axboef0153942011-07-07 15:35:27 +02001094
1095 /*
1096 * Set the same seed to get repeatable runs
1097 */
1098 td_fill_rand_seeds(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001099}
1100
Jens Axboe398e8c42009-05-20 12:10:02 +02001101static int exec_string(const char *string)
1102{
1103 int ret, newlen = strlen(string) + 1 + 8;
1104 char *str;
1105
1106 str = malloc(newlen);
1107 sprintf(str, "sh -c %s", string);
1108
1109 ret = system(str);
1110 if (ret == -1)
1111 log_err("fio: exec of cmd <%s> failed\n", str);
1112
1113 free(str);
1114 return ret;
1115}
1116
Jens Axboe906c8d72006-06-13 09:37:56 +02001117/*
1118 * Entry point for the thread based jobs. The process based jobs end up
1119 * here as well, after a little setup.
1120 */
Jens Axboeebac4652005-12-08 15:25:21 +01001121static void *thread_main(void *data)
1122{
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001123 unsigned long long elapsed;
Jens Axboeebac4652005-12-08 15:25:21 +01001124 struct thread_data *td = data;
Jens Axboee8462bd2009-07-06 12:59:04 +02001125 pthread_condattr_t attr;
Jens Axboea978ba62007-03-08 14:29:03 +01001126 int clear_state;
Jens Axboeebac4652005-12-08 15:25:21 +01001127
Jens Axboe47f767c2011-07-12 21:17:49 +02001128 if (!td->o.use_thread) {
Jens Axboeebac4652005-12-08 15:25:21 +01001129 setsid();
Jens Axboe47f767c2011-07-12 21:17:49 +02001130 td->pid = getpid();
1131 } else
1132 td->pid = gettid();
Jens Axboeebac4652005-12-08 15:25:21 +01001133
Jens Axboe5921e802008-05-30 15:02:38 +02001134 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
Jens Axboeee56ad52008-02-01 10:30:20 +01001135
Jens Axboe01743ee2008-06-02 12:19:19 +02001136 INIT_FLIST_HEAD(&td->io_u_freelist);
1137 INIT_FLIST_HEAD(&td->io_u_busylist);
1138 INIT_FLIST_HEAD(&td->io_u_requeues);
1139 INIT_FLIST_HEAD(&td->io_log_list);
1140 INIT_FLIST_HEAD(&td->io_hist_list);
Jens Axboee8462bd2009-07-06 12:59:04 +02001141 INIT_FLIST_HEAD(&td->verify_list);
Jens Axboe0d29de82010-09-01 13:54:15 +02001142 INIT_FLIST_HEAD(&td->trim_list);
Jens Axboee8462bd2009-07-06 12:59:04 +02001143 pthread_mutex_init(&td->io_u_lock, NULL);
Jens Axboebb5d7d02007-03-27 10:21:25 +02001144 td->io_hist_tree = RB_ROOT;
Jens Axboeaea47d42006-05-26 19:27:29 +02001145
Jens Axboee8462bd2009-07-06 12:59:04 +02001146 pthread_condattr_init(&attr);
1147 pthread_cond_init(&td->verify_cond, &attr);
1148 pthread_cond_init(&td->free_cond, &attr);
1149
Jens Axboe75154842006-06-01 13:56:09 +02001150 td_set_runstate(td, TD_INITIALIZED);
Jens Axboe29adda32009-01-05 19:06:39 +01001151 dprint(FD_MUTEX, "up startup_mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +01001152 fio_mutex_up(startup_mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001153 dprint(FD_MUTEX, "wait on td->mutex\n");
Jens Axboecdd18ad2008-02-27 18:58:00 +01001154 fio_mutex_down(td->mutex);
Jens Axboe29adda32009-01-05 19:06:39 +01001155 dprint(FD_MUTEX, "done waiting on td->mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001156
Jens Axboe37f56872007-03-09 12:42:35 +01001157 /*
Jens Axboecdd18ad2008-02-27 18:58:00 +01001158 * the ->mutex mutex is now no longer used, close it to avoid
Jens Axboe37f56872007-03-09 12:42:35 +01001159 * eating a file descriptor
1160 */
Jens Axboecdd18ad2008-02-27 18:58:00 +01001161 fio_mutex_remove(td->mutex);
Jens Axboe37f56872007-03-09 12:42:35 +01001162
Stefan Hajnoczidfc6e752011-01-07 20:58:05 +00001163 /*
1164 * A new gid requires privilege, so we need to do this before setting
1165 * the uid.
1166 */
Jens Axboee0b0d892009-12-08 10:10:14 +01001167 if (td->o.gid != -1U && setgid(td->o.gid)) {
1168 td_verror(td, errno, "setgid");
1169 goto err;
1170 }
Stefan Hajnoczidfc6e752011-01-07 20:58:05 +00001171 if (td->o.uid != -1U && setuid(td->o.uid)) {
1172 td_verror(td, errno, "setuid");
1173 goto err;
1174 }
Jens Axboee0b0d892009-12-08 10:10:14 +01001175
Jens Axboed84f8d42007-05-16 08:47:46 +02001176 /*
Jens Axboebe18a942011-07-08 19:25:04 +02001177 * If we have a gettimeofday() thread, make sure we exclude that
1178 * thread from this job
1179 */
1180 if (td->o.gtod_cpu)
1181 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
1182
1183 /*
1184 * Set affinity first, in case it has an impact on the memory
1185 * allocations.
1186 */
1187 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
1188 td_verror(td, errno, "cpu_set_affinity");
1189 goto err;
1190 }
1191
1192 /*
Jens Axboed84f8d42007-05-16 08:47:46 +02001193 * May alter parameters that init_io_u() will use, so we need to
1194 * do this first.
1195 */
1196 if (init_iolog(td))
1197 goto err;
1198
Jens Axboedb1cd902007-04-26 13:47:07 +02001199 if (init_io_u(td))
1200 goto err;
1201
Jens Axboee8462bd2009-07-06 12:59:04 +02001202 if (td->o.verify_async && verify_async_init(td))
1203 goto err;
1204
Jens Axboeac684782007-11-08 08:29:07 +01001205 if (td->ioprio_set) {
Jens Axboedb1cd902007-04-26 13:47:07 +02001206 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1207 td_verror(td, errno, "ioprio_set");
1208 goto err;
1209 }
1210 }
1211
Jens Axboe6adb38a2009-12-07 08:01:26 +01001212 if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt))
Jens Axboea696fa22009-12-04 10:05:02 +01001213 goto err;
1214
Jens Axboedb1cd902007-04-26 13:47:07 +02001215 if (nice(td->o.nice) == -1) {
1216 td_verror(td, errno, "nice");
1217 goto err;
1218 }
1219
1220 if (td->o.ioscheduler && switch_ioscheduler(td))
1221 goto err;
1222
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001223 if (!td->o.create_serialize && setup_files(td))
Jens Axboeebac4652005-12-08 15:25:21 +01001224 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +01001225
1226 if (td_io_init(td))
Jens Axboe21972cd2006-11-23 12:39:16 +01001227 goto err;
Jens Axboeebac4652005-12-08 15:25:21 +01001228
Jens Axboe68727072007-03-15 20:49:25 +01001229 if (init_random_map(td))
1230 goto err;
1231
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001232 if (td->o.exec_prerun) {
Jens Axboe398e8c42009-05-20 12:10:02 +02001233 if (exec_string(td->o.exec_prerun))
Jens Axboe69cfd7e2007-02-07 13:58:53 +01001234 goto err;
1235 }
Jens Axboe4e0ba8a2006-06-06 09:36:28 +02001236
Zhang, Yanminafad68f2009-05-20 11:30:55 +02001237 if (td->o.pre_read) {
1238 if (pre_read_files(td) < 0)
1239 goto err;
1240 }
1241
Jens Axboe69008992006-11-24 13:12:56 +01001242 fio_gettime(&td->epoch, NULL);
Jens Axboec8aaba12011-10-03 12:14:19 +02001243 getrusage(RUSAGE_SELF, &td->ru_start);
Jens Axboe69008992006-11-24 13:12:56 +01001244
Jens Axboea978ba62007-03-08 14:29:03 +01001245 clear_state = 0;
Jens Axboe492158c2007-05-24 10:32:47 +02001246 while (keep_running(td)) {
Jens Axboe02bcaa82006-11-24 10:42:00 +01001247 fio_gettime(&td->start, NULL);
Jens Axboec8eeb9d2011-10-05 14:02:22 +02001248 memcpy(&td->bw_sample_time, &td->start, sizeof(td->start));
1249 memcpy(&td->iops_sample_time, &td->start, sizeof(td->start));
Jens Axboe993bf482008-11-14 13:04:53 +01001250 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
Jens Axboeebac4652005-12-08 15:25:21 +01001251
Josh Carter8a322052011-11-22 09:35:29 +01001252 if (td->o.ratemin[0] || td->o.ratemin[1]) {
1253 memcpy(&td->lastrate[0], &td->bw_sample_time,
1254 sizeof(td->bw_sample_time));
1255 memcpy(&td->lastrate[1], &td->bw_sample_time,
1256 sizeof(td->bw_sample_time));
1257 }
Jens Axboeebac4652005-12-08 15:25:21 +01001258
Jens Axboedf9c26b2009-03-05 10:13:58 +01001259 if (clear_state)
1260 clear_io_state(td);
Jens Axboea978ba62007-03-08 14:29:03 +01001261
Jens Axboeebac4652005-12-08 15:25:21 +01001262 prune_io_piece_log(td);
1263
Jens Axboeba0fbe12007-03-09 14:34:23 +01001264 do_io(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001265
Jens Axboea978ba62007-03-08 14:29:03 +01001266 clear_state = 1;
1267
Jens Axboe38d77ca2007-03-21 14:20:20 +01001268 if (td_read(td) && td->io_bytes[DDIR_READ]) {
Jens Axboe0a4957c2009-06-03 10:30:33 +02001269 elapsed = utime_since_now(&td->start);
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001270 td->ts.runtime[DDIR_READ] += elapsed;
Jens Axboe38d77ca2007-03-21 14:20:20 +01001271 }
1272 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
Jens Axboe0a4957c2009-06-03 10:30:33 +02001273 elapsed = utime_since_now(&td->start);
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001274 td->ts.runtime[DDIR_WRITE] += elapsed;
Jens Axboe38d77ca2007-03-21 14:20:20 +01001275 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001276
Jens Axboeebac4652005-12-08 15:25:21 +01001277 if (td->error || td->terminate)
1278 break;
1279
Shawn Lewise84c73a2007-08-02 22:19:32 +02001280 if (!td->o.do_verify ||
1281 td->o.verify == VERIFY_NONE ||
Jens Axboeb67740d2007-07-26 12:22:30 +02001282 (td->io_ops->flags & FIO_UNIDIR))
Jens Axboeebac4652005-12-08 15:25:21 +01001283 continue;
1284
Jens Axboedf9c26b2009-03-05 10:13:58 +01001285 clear_io_state(td);
Jens Axboea978ba62007-03-08 14:29:03 +01001286
Jens Axboe02bcaa82006-11-24 10:42:00 +01001287 fio_gettime(&td->start, NULL);
Jens Axboeebac4652005-12-08 15:25:21 +01001288
1289 do_verify(td);
1290
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001291 td->ts.runtime[DDIR_READ] += utime_since_now(&td->start);
Jens Axboeebac4652005-12-08 15:25:21 +01001292
1293 if (td->error || td->terminate)
1294 break;
1295 }
1296
Jens Axboe36dff962006-11-24 13:29:20 +01001297 update_rusage_stat(td);
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +01001298 td->ts.runtime[0] = (td->ts.runtime[0] + 999) / 1000;
1299 td->ts.runtime[1] = (td->ts.runtime[1] + 999) / 1000;
Jens Axboe756867b2007-03-06 15:19:24 +01001300 td->ts.total_run_time = mtime_since_now(&td->epoch);
1301 td->ts.io_bytes[0] = td->io_bytes[0];
1302 td->ts.io_bytes[1] = td->io_bytes[1];
Jens Axboe69008992006-11-24 13:12:56 +01001303
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001304 fio_mutex_down(writeout_mutex);
Jens Axboe7b9f7332011-10-03 10:06:44 +02001305 if (td->bw_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001306 if (td->o.bw_log_file) {
Jens Axboe7b9f7332011-10-03 10:06:44 +02001307 finish_log_named(td, td->bw_log,
Jens Axboef4844702008-12-09 08:04:29 +01001308 td->o.bw_log_file, "bw");
1309 } else
Jens Axboe7b9f7332011-10-03 10:06:44 +02001310 finish_log(td, td->bw_log, "bw");
Jens Axboee3cedca2008-11-19 19:57:52 +01001311 }
Jens Axboe7b9f7332011-10-03 10:06:44 +02001312 if (td->lat_log) {
Jens Axboe02af0982010-06-24 09:59:34 +02001313 if (td->o.lat_log_file) {
Jens Axboe7b9f7332011-10-03 10:06:44 +02001314 finish_log_named(td, td->lat_log,
Jens Axboe02af0982010-06-24 09:59:34 +02001315 td->o.lat_log_file, "lat");
1316 } else
Jens Axboe7b9f7332011-10-03 10:06:44 +02001317 finish_log(td, td->lat_log, "lat");
Jens Axboe02af0982010-06-24 09:59:34 +02001318 }
Jens Axboe7b9f7332011-10-03 10:06:44 +02001319 if (td->slat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001320 if (td->o.lat_log_file) {
Jens Axboe7b9f7332011-10-03 10:06:44 +02001321 finish_log_named(td, td->slat_log,
Radha Ramachandran1ca95732009-04-29 07:23:26 +02001322 td->o.lat_log_file, "slat");
Jens Axboef4844702008-12-09 08:04:29 +01001323 } else
Jens Axboe7b9f7332011-10-03 10:06:44 +02001324 finish_log(td, td->slat_log, "slat");
Jens Axboee3cedca2008-11-19 19:57:52 +01001325 }
Jens Axboe7b9f7332011-10-03 10:06:44 +02001326 if (td->clat_log) {
Jens Axboef4844702008-12-09 08:04:29 +01001327 if (td->o.lat_log_file) {
Jens Axboe7b9f7332011-10-03 10:06:44 +02001328 finish_log_named(td, td->clat_log,
Jens Axboef4844702008-12-09 08:04:29 +01001329 td->o.lat_log_file, "clat");
1330 } else
Jens Axboe7b9f7332011-10-03 10:06:44 +02001331 finish_log(td, td->clat_log, "clat");
Jens Axboee3cedca2008-11-19 19:57:52 +01001332 }
Jens Axboec8eeb9d2011-10-05 14:02:22 +02001333 if (td->iops_log) {
1334 if (td->o.iops_log_file) {
1335 finish_log_named(td, td->iops_log,
1336 td->o.iops_log_file, "iops");
1337 } else
1338 finish_log(td, td->iops_log, "iops");
1339 }
1340
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001341 fio_mutex_up(writeout_mutex);
Jens Axboe398e8c42009-05-20 12:10:02 +02001342 if (td->o.exec_postrun)
1343 exec_string(td->o.exec_postrun);
Jens Axboeebac4652005-12-08 15:25:21 +01001344
1345 if (exitall_on_terminate)
Jens Axboecc0df002011-10-03 20:53:32 +02001346 fio_terminate_threads(td->groupid);
Jens Axboeebac4652005-12-08 15:25:21 +01001347
1348err:
Jens Axboe5bf13a52007-02-17 01:51:47 +01001349 if (td->error)
Jens Axboe4ceb30d2010-02-24 09:19:14 +01001350 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001351 td->verror);
Jens Axboeb4707ed2009-11-04 12:51:17 +01001352
1353 if (td->o.verify_async)
1354 verify_async_exit(td);
1355
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001356 close_and_free_files(td);
Jens Axboe2866c822006-10-09 15:57:48 +02001357 close_ioengine(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001358 cleanup_io_u(td);
Jens Axboe6adb38a2009-12-07 08:01:26 +01001359 cgroup_shutdown(td, &cgroup_mnt);
Jens Axboef29b25a2007-07-23 08:56:43 +02001360
Jens Axboed2ce18b2008-12-12 20:51:40 +01001361 if (td->o.cpumask_set) {
1362 int ret = fio_cpuset_exit(&td->o.cpumask);
1363
1364 td_verror(td, ret, "fio_cpuset_exit");
1365 }
Jens Axboe4e78e402008-12-12 20:40:27 +01001366
Jens Axboef29b25a2007-07-23 08:56:43 +02001367 /*
1368 * do this very late, it will log file closing as well
1369 */
1370 if (td->o.write_iolog_file)
1371 write_iolog_close(td);
1372
Jens Axboeebac4652005-12-08 15:25:21 +01001373 td_set_runstate(td, TD_EXITED);
Jens Axboe43d76802007-02-21 16:35:29 +01001374 return (void *) (unsigned long) td->error;
Jens Axboeebac4652005-12-08 15:25:21 +01001375}
1376
Jens Axboe906c8d72006-06-13 09:37:56 +02001377/*
1378 * We cannot pass the td data into a forked process, so attach the td and
1379 * pass it to the thread worker.
1380 */
Jens Axboea6418142007-02-17 04:47:18 +01001381static int fork_main(int shmid, int offset)
Jens Axboeebac4652005-12-08 15:25:21 +01001382{
1383 struct thread_data *td;
Jens Axboea6418142007-02-17 04:47:18 +01001384 void *data, *ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001385
Jens Axboe65258962011-07-09 08:31:53 +02001386#ifndef __hpux
Jens Axboeebac4652005-12-08 15:25:21 +01001387 data = shmat(shmid, NULL, 0);
1388 if (data == (void *) -1) {
Jens Axboea6418142007-02-17 04:47:18 +01001389 int __err = errno;
1390
Jens Axboeebac4652005-12-08 15:25:21 +01001391 perror("shmat");
Jens Axboea6418142007-02-17 04:47:18 +01001392 return __err;
Jens Axboeebac4652005-12-08 15:25:21 +01001393 }
Jens Axboe65258962011-07-09 08:31:53 +02001394#else
1395 /*
1396 * HP-UX inherits shm mappings?
1397 */
1398 data = threads;
1399#endif
Jens Axboeebac4652005-12-08 15:25:21 +01001400
1401 td = data + offset * sizeof(struct thread_data);
Jens Axboea6418142007-02-17 04:47:18 +01001402 ret = thread_main(td);
Jens Axboeebac4652005-12-08 15:25:21 +01001403 shmdt(data);
Jens Axboe43d76802007-02-21 16:35:29 +01001404 return (int) (unsigned long) ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001405}
1406
Jens Axboe906c8d72006-06-13 09:37:56 +02001407/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001408 * Run over the job map and reap the threads that have exited, if any.
1409 */
Jens Axboe11e950b2011-10-16 21:34:14 +02001410static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
1411 unsigned int *m_rate)
Jens Axboeebac4652005-12-08 15:25:21 +01001412{
Jens Axboe34572e22006-10-20 09:33:18 +02001413 struct thread_data *td;
Jens Axboe11e950b2011-10-16 21:34:14 +02001414 unsigned int cputhreads, realthreads, pending;
1415 int i, status, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001416
1417 /*
1418 * reap exited threads (TD_EXITED -> TD_REAPED)
1419 */
Jens Axboe1f809d12007-10-25 18:34:02 +02001420 realthreads = pending = cputhreads = 0;
Jens Axboe34572e22006-10-20 09:33:18 +02001421 for_each_td(td, i) {
Jens Axboe3707f452007-02-22 12:26:57 +01001422 int flags = 0;
Jens Axboea2f77c92007-02-22 11:53:00 +01001423
Jens Axboe84585002006-10-19 20:26:22 +02001424 /*
1425 * ->io_ops is NULL for a thread that has closed its
1426 * io engine
1427 */
Jens Axboeba0fbe12007-03-09 14:34:23 +01001428 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +02001429 cputhreads++;
Jens Axboe1f809d12007-10-25 18:34:02 +02001430 else
1431 realthreads++;
Jens Axboeb990b5c2006-09-14 09:48:22 +02001432
Aaron Carrolla1dedc12008-02-20 09:14:12 +01001433 if (!td->pid) {
1434 pending++;
1435 continue;
1436 }
1437 if (td->runstate == TD_REAPED)
Jens Axboea2f77c92007-02-22 11:53:00 +01001438 continue;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001439 if (td->o.use_thread) {
Jens Axboe3707f452007-02-22 12:26:57 +01001440 if (td->runstate == TD_EXITED) {
1441 td_set_runstate(td, TD_REAPED);
1442 goto reaped;
1443 }
1444 continue;
1445 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001446
1447 flags = WNOHANG;
1448 if (td->runstate == TD_EXITED)
1449 flags = 0;
1450
1451 /*
1452 * check if someone quit or got killed in an unusual way
1453 */
1454 ret = waitpid(td->pid, &status, flags);
Jens Axboe3707f452007-02-22 12:26:57 +01001455 if (ret < 0) {
Jens Axboea2f77c92007-02-22 11:53:00 +01001456 if (errno == ECHILD) {
Jens Axboe5921e802008-05-30 15:02:38 +02001457 log_err("fio: pid=%d disappeared %d\n",
1458 (int) td->pid, td->runstate);
Jens Axboea2f77c92007-02-22 11:53:00 +01001459 td_set_runstate(td, TD_REAPED);
1460 goto reaped;
1461 }
1462 perror("waitpid");
1463 } else if (ret == td->pid) {
1464 if (WIFSIGNALED(status)) {
Jens Axboefab6aa72007-02-17 06:19:24 +01001465 int sig = WTERMSIG(status);
1466
Bruce Cran03e20d62011-01-02 20:14:54 +01001467 if (sig != SIGTERM)
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001468 log_err("fio: pid=%d, got signal=%d\n",
Jens Axboe5921e802008-05-30 15:02:38 +02001469 (int) td->pid, sig);
Jens Axboefab6aa72007-02-17 06:19:24 +01001470 td_set_runstate(td, TD_REAPED);
1471 goto reaped;
1472 }
Jens Axboea2f77c92007-02-22 11:53:00 +01001473 if (WIFEXITED(status)) {
1474 if (WEXITSTATUS(status) && !td->error)
1475 td->error = WEXITSTATUS(status);
Jens Axboefab6aa72007-02-17 06:19:24 +01001476
Jens Axboea2f77c92007-02-22 11:53:00 +01001477 td_set_runstate(td, TD_REAPED);
1478 goto reaped;
Jens Axboea6418142007-02-17 04:47:18 +01001479 }
1480 }
Jens Axboeebac4652005-12-08 15:25:21 +01001481
Jens Axboea2f77c92007-02-22 11:53:00 +01001482 /*
1483 * thread is not dead, continue
1484 */
gurudas paie48676b2007-04-11 12:44:27 +02001485 pending++;
Jens Axboea2f77c92007-02-22 11:53:00 +01001486 continue;
Jens Axboefab6aa72007-02-17 06:19:24 +01001487reaped:
Jens Axboeebac4652005-12-08 15:25:21 +01001488 (*nr_running)--;
Jens Axboe581e7142009-06-09 12:47:16 +02001489 (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]);
1490 (*t_rate) -= (td->o.rate[0] + td->o.rate[1]);
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001491 if (!td->pid)
1492 pending--;
Jens Axboea2f77c92007-02-22 11:53:00 +01001493
1494 if (td->error)
1495 exit_value++;
Jens Axboed3eeeab2008-04-06 12:19:05 +02001496
1497 done_secs += mtime_since_now(&td->epoch) / 1000;
Jens Axboeebac4652005-12-08 15:25:21 +01001498 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001499
Jens Axboe1f809d12007-10-25 18:34:02 +02001500 if (*nr_running == cputhreads && !pending && realthreads)
Jens Axboecc0df002011-10-03 20:53:32 +02001501 fio_terminate_threads(TERMINATE_ALL);
Jens Axboeebac4652005-12-08 15:25:21 +01001502}
1503
Jens Axboe906c8d72006-06-13 09:37:56 +02001504/*
1505 * Main function for kicking off and reaping jobs, as needed.
1506 */
Jens Axboeebac4652005-12-08 15:25:21 +01001507static void run_threads(void)
1508{
Jens Axboeebac4652005-12-08 15:25:21 +01001509 struct thread_data *td;
1510 unsigned long spent;
Jens Axboe11e950b2011-10-16 21:34:14 +02001511 unsigned int i, todo, nr_running, m_rate, t_rate, nr_started;
Jens Axboefcb6ade2006-05-31 12:14:35 +02001512
Jens Axboe2f9ade32006-10-20 11:25:52 +02001513 if (fio_pin_memory())
1514 return;
Jens Axboeebac4652005-12-08 15:25:21 +01001515
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001516 if (fio_gtod_offload && fio_start_gtod_thread())
1517 return;
1518
Jens Axboecc0df002011-10-03 20:53:32 +02001519 set_sig_handlers();
1520
Jens Axboec6ae0a52006-06-12 11:04:44 +02001521 if (!terse_output) {
Jens Axboe4ceb30d2010-02-24 09:19:14 +01001522 log_info("Starting ");
Jens Axboe9cedf162007-03-12 11:29:30 +01001523 if (nr_thread)
Jens Axboe4ceb30d2010-02-24 09:19:14 +01001524 log_info("%d thread%s", nr_thread,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001525 nr_thread > 1 ? "s" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001526 if (nr_process) {
1527 if (nr_thread)
Bruce Crancc604382011-10-18 12:20:31 +02001528 log_info(" and ");
Jens Axboe4ceb30d2010-02-24 09:19:14 +01001529 log_info("%d process%s", nr_process,
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001530 nr_process > 1 ? "es" : "");
Jens Axboe9cedf162007-03-12 11:29:30 +01001531 }
Jens Axboe4ceb30d2010-02-24 09:19:14 +01001532 log_info("\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001533 fflush(stdout);
1534 }
Jens Axboec04f7ec2006-05-31 10:13:16 +02001535
Jens Axboeebac4652005-12-08 15:25:21 +01001536 todo = thread_number;
1537 nr_running = 0;
1538 nr_started = 0;
1539 m_rate = t_rate = 0;
1540
Jens Axboe34572e22006-10-20 09:33:18 +02001541 for_each_td(td, i) {
Jens Axboe263e5292006-10-18 15:37:01 +02001542 print_status_init(td->thread_number - 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001543
Jens Axboe067430b2011-09-08 14:30:24 +02001544 if (!td->o.create_serialize)
Jens Axboeebac4652005-12-08 15:25:21 +01001545 continue;
1546
1547 /*
1548 * do file setup here so it happens sequentially,
1549 * we don't want X number of threads getting their
1550 * client data interspersed on disk
1551 */
Jens Axboe53cdc682006-10-18 11:50:58 +02001552 if (setup_files(td)) {
Jens Axboe5bf13a52007-02-17 01:51:47 +01001553 exit_value++;
1554 if (td->error)
Jens Axboe5921e802008-05-30 15:02:38 +02001555 log_err("fio: pid=%d, err=%d/%s\n",
1556 (int) td->pid, td->error, td->verror);
Jens Axboeebac4652005-12-08 15:25:21 +01001557 td_set_runstate(td, TD_REAPED);
1558 todo--;
Jens Axboec69e8872008-03-01 18:50:48 +01001559 } else {
1560 struct fio_file *f;
Jens Axboe2b13e712011-01-19 14:04:16 -07001561 unsigned int j;
Jens Axboec69e8872008-03-01 18:50:48 +01001562
1563 /*
1564 * for sharing to work, each job must always open
1565 * its own files. so close them, if we opened them
1566 * for creation
1567 */
Jens Axboe2b13e712011-01-19 14:04:16 -07001568 for_each_file(td, f, j) {
Jens Axboe22a57ba2009-06-09 14:34:35 +02001569 if (fio_file_open(f))
1570 td_io_close_file(td, f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001571 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001572 }
Jens Axboeebac4652005-12-08 15:25:21 +01001573 }
1574
Jens Axboea2f77c92007-02-22 11:53:00 +01001575 set_genesis_time();
1576
Jens Axboeebac4652005-12-08 15:25:21 +01001577 while (todo) {
Jens Axboefca70352011-07-06 20:12:54 +02001578 struct thread_data *map[REAL_MAX_JOBS];
Jens Axboe75154842006-06-01 13:56:09 +02001579 struct timeval this_start;
1580 int this_jobs = 0, left;
1581
Jens Axboeebac4652005-12-08 15:25:21 +01001582 /*
1583 * create threads (TD_NOT_CREATED -> TD_CREATED)
1584 */
Jens Axboe34572e22006-10-20 09:33:18 +02001585 for_each_td(td, i) {
Jens Axboeebac4652005-12-08 15:25:21 +01001586 if (td->runstate != TD_NOT_CREATED)
1587 continue;
1588
1589 /*
1590 * never got a chance to start, killed by other
1591 * thread for some reason
1592 */
1593 if (td->terminate) {
1594 todo--;
1595 continue;
1596 }
1597
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001598 if (td->o.start_delay) {
Jens Axboe263e5292006-10-18 15:37:01 +02001599 spent = mtime_since_genesis();
Jens Axboeebac4652005-12-08 15:25:21 +01001600
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001601 if (td->o.start_delay * 1000 > spent)
Jens Axboeebac4652005-12-08 15:25:21 +01001602 continue;
1603 }
1604
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001605 if (td->o.stonewall && (nr_started || nr_running)) {
1606 dprint(FD_PROCESS, "%s: stonewall wait\n",
1607 td->o.name);
Jens Axboeebac4652005-12-08 15:25:21 +01001608 break;
Jens Axboe6f88bcb2008-03-19 10:29:07 +01001609 }
Jens Axboeebac4652005-12-08 15:25:21 +01001610
Jens Axboe067430b2011-09-08 14:30:24 +02001611 init_disk_util(td);
1612
Jens Axboe75154842006-06-01 13:56:09 +02001613 /*
1614 * Set state to created. Thread will transition
1615 * to TD_INITIALIZED when it's done setting up.
1616 */
Jens Axboeebac4652005-12-08 15:25:21 +01001617 td_set_runstate(td, TD_CREATED);
Jens Axboe75154842006-06-01 13:56:09 +02001618 map[this_jobs++] = td;
Jens Axboeebac4652005-12-08 15:25:21 +01001619 nr_started++;
1620
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001621 if (td->o.use_thread) {
Jens Axboef42c1532009-01-05 19:07:13 +01001622 int ret;
1623
Jens Axboeee56ad52008-02-01 10:30:20 +01001624 dprint(FD_PROCESS, "will pthread_create\n");
Jens Axboef42c1532009-01-05 19:07:13 +01001625 ret = pthread_create(&td->thread, NULL,
1626 thread_main, td);
1627 if (ret) {
1628 log_err("pthread_create: %s\n",
1629 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001630 nr_started--;
Jens Axboec8bb6fa2007-03-12 11:03:04 +01001631 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001632 }
Jens Axboef42c1532009-01-05 19:07:13 +01001633 ret = pthread_detach(td->thread);
1634 if (ret)
1635 log_err("pthread_detach: %s",
1636 strerror(ret));
Jens Axboeebac4652005-12-08 15:25:21 +01001637 } else {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001638 pid_t pid;
Jens Axboeee56ad52008-02-01 10:30:20 +01001639 dprint(FD_PROCESS, "will fork\n");
Jens Axboe5e1d3062008-05-23 11:55:53 +02001640 pid = fork();
1641 if (!pid) {
Jens Axboea6418142007-02-17 04:47:18 +01001642 int ret = fork_main(shm_id, i);
1643
Jens Axboe5e1d3062008-05-23 11:55:53 +02001644 _exit(ret);
1645 } else if (i == fio_debug_jobno)
1646 *fio_debug_jobp = pid;
Jens Axboeebac4652005-12-08 15:25:21 +01001647 }
Jens Axboe29adda32009-01-05 19:06:39 +01001648 dprint(FD_MUTEX, "wait on startup_mutex\n");
Jens Axboe656b1392009-07-01 23:02:10 +02001649 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1650 log_err("fio: job startup hung? exiting.\n");
Jens Axboecc0df002011-10-03 20:53:32 +02001651 fio_terminate_threads(TERMINATE_ALL);
Jens Axboe656b1392009-07-01 23:02:10 +02001652 fio_abort = 1;
1653 nr_started--;
1654 break;
1655 }
Jens Axboe29adda32009-01-05 19:06:39 +01001656 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001657 }
1658
1659 /*
Jens Axboe75154842006-06-01 13:56:09 +02001660 * Wait for the started threads to transition to
1661 * TD_INITIALIZED.
Jens Axboeebac4652005-12-08 15:25:21 +01001662 */
Jens Axboe02bcaa82006-11-24 10:42:00 +01001663 fio_gettime(&this_start, NULL);
Jens Axboe75154842006-06-01 13:56:09 +02001664 left = this_jobs;
Jens Axboe6ce15a32007-01-12 09:04:41 +01001665 while (left && !fio_abort) {
Jens Axboe75154842006-06-01 13:56:09 +02001666 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1667 break;
1668
1669 usleep(100000);
1670
1671 for (i = 0; i < this_jobs; i++) {
1672 td = map[i];
1673 if (!td)
1674 continue;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001675 if (td->runstate == TD_INITIALIZED) {
Jens Axboe75154842006-06-01 13:56:09 +02001676 map[i] = NULL;
1677 left--;
Jens Axboeb6f4d882006-06-02 10:32:51 +02001678 } else if (td->runstate >= TD_EXITED) {
1679 map[i] = NULL;
1680 left--;
1681 todo--;
1682 nr_running++; /* work-around... */
Jens Axboe75154842006-06-01 13:56:09 +02001683 }
1684 }
1685 }
1686
1687 if (left) {
Jens Axboe3b70d7e2006-06-08 21:48:46 +02001688 log_err("fio: %d jobs failed to start\n", left);
Jens Axboe75154842006-06-01 13:56:09 +02001689 for (i = 0; i < this_jobs; i++) {
1690 td = map[i];
1691 if (!td)
1692 continue;
1693 kill(td->pid, SIGTERM);
1694 }
1695 break;
1696 }
1697
1698 /*
Jens Axboeb6f4d882006-06-02 10:32:51 +02001699 * start created threads (TD_INITIALIZED -> TD_RUNNING).
Jens Axboe75154842006-06-01 13:56:09 +02001700 */
Jens Axboe34572e22006-10-20 09:33:18 +02001701 for_each_td(td, i) {
Jens Axboe75154842006-06-01 13:56:09 +02001702 if (td->runstate != TD_INITIALIZED)
Jens Axboeebac4652005-12-08 15:25:21 +01001703 continue;
1704
Jens Axboeb29ee5b2008-09-11 10:17:26 +02001705 if (in_ramp_time(td))
1706 td_set_runstate(td, TD_RAMP);
1707 else
1708 td_set_runstate(td, TD_RUNNING);
Jens Axboeebac4652005-12-08 15:25:21 +01001709 nr_running++;
1710 nr_started--;
Jens Axboe581e7142009-06-09 12:47:16 +02001711 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
1712 t_rate += td->o.rate[0] + td->o.rate[1];
Jens Axboe75154842006-06-01 13:56:09 +02001713 todo--;
Jens Axboecdd18ad2008-02-27 18:58:00 +01001714 fio_mutex_up(td->mutex);
Jens Axboeebac4652005-12-08 15:25:21 +01001715 }
1716
1717 reap_threads(&nr_running, &t_rate, &m_rate);
1718
Jens Axboe602d6ff2011-10-07 12:16:19 +02001719 if (todo) {
1720 if (is_backend)
1721 fio_server_idle_loop();
1722 else
1723 usleep(100000);
1724 }
Jens Axboeebac4652005-12-08 15:25:21 +01001725 }
1726
1727 while (nr_running) {
1728 reap_threads(&nr_running, &t_rate, &m_rate);
Jens Axboecc0df002011-10-03 20:53:32 +02001729
1730 if (is_backend)
1731 fio_server_idle_loop();
Jens Axboe70e0c312011-10-04 09:18:30 +02001732 else
1733 usleep(10000);
Jens Axboeebac4652005-12-08 15:25:21 +01001734 }
1735
1736 update_io_ticks();
Jens Axboe2f9ade32006-10-20 11:25:52 +02001737 fio_unpin_memory();
Jens Axboeebac4652005-12-08 15:25:21 +01001738}
1739
Jens Axboe50d16972011-09-29 17:45:28 -06001740int exec_run(void)
Jens Axboeebac4652005-12-08 15:25:21 +01001741{
Jens Axboe7e356b22011-10-14 10:55:16 +02001742 struct thread_data *td;
1743 int i;
1744
Jens Axboea37f69b2011-10-01 12:24:21 -06001745 if (nr_clients)
Jens Axboe37db14f2011-09-30 17:00:42 -06001746 return fio_handle_clients();
Jens Axboe7e356b22011-10-14 10:55:16 +02001747 if (exec_profile) {
1748 if (load_profile(exec_profile))
1749 return 1;
1750 free(exec_profile);
1751 exec_profile = NULL;
1752 }
Jens Axboe74929ac2009-08-05 11:42:37 +02001753 if (!thread_number)
1754 return 0;
1755
Jens Axboebb3884d2007-01-17 17:23:11 +11001756 if (write_bw_log) {
Jens Axboeb8bc8cb2011-12-01 09:04:31 +01001757 setup_log(&agg_io_log[DDIR_READ], 0);
1758 setup_log(&agg_io_log[DDIR_WRITE], 0);
Jens Axboebb3884d2007-01-17 17:23:11 +11001759 }
1760
Jens Axboecdd18ad2008-02-27 18:58:00 +01001761 startup_mutex = fio_mutex_init(0);
Bruce Cran03e20d62011-01-02 20:14:54 +01001762 if (startup_mutex == NULL)
1763 return 1;
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001764 writeout_mutex = fio_mutex_init(1);
Bruce Cran03e20d62011-01-02 20:14:54 +01001765 if (writeout_mutex == NULL)
1766 return 1;
Jens Axboe07739b52007-03-08 20:25:46 +01001767
Jens Axboea2f77c92007-02-22 11:53:00 +01001768 set_genesis_time();
Jens Axboe79857012011-01-14 14:14:58 +01001769 create_disk_util_thread();
Jens Axboeebac4652005-12-08 15:25:21 +01001770
Jens Axboedae53412009-12-04 19:54:18 +01001771 cgroup_list = smalloc(sizeof(*cgroup_list));
1772 INIT_FLIST_HEAD(cgroup_list);
1773
Jens Axboeebac4652005-12-08 15:25:21 +01001774 run_threads();
Jens Axboe6ce15a32007-01-12 09:04:41 +01001775
Jens Axboebb3884d2007-01-17 17:23:11 +11001776 if (!fio_abort) {
Jens Axboe6ce15a32007-01-12 09:04:41 +01001777 show_run_stats();
Jens Axboebb3884d2007-01-17 17:23:11 +11001778 if (write_bw_log) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001779 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1780 __finish_log(agg_io_log[DDIR_WRITE],
1781 "agg-write_bw.log");
Jens Axboebb3884d2007-01-17 17:23:11 +11001782 }
1783 }
Jens Axboeebac4652005-12-08 15:25:21 +01001784
Jens Axboe7e356b22011-10-14 10:55:16 +02001785 for_each_td(td, i)
1786 fio_options_free(td);
1787
Jens Axboedae53412009-12-04 19:54:18 +01001788 cgroup_kill(cgroup_list);
1789 sfree(cgroup_list);
Jens Axboe61eb3132010-03-31 20:05:59 +02001790 sfree(cgroup_mnt);
Jens Axboe39f22022009-12-04 11:29:12 +01001791
Jens Axboecdd18ad2008-02-27 18:58:00 +01001792 fio_mutex_remove(startup_mutex);
Fabio Checconid2bbb8d2009-05-18 12:54:50 +02001793 fio_mutex_remove(writeout_mutex);
Jens Axboe437c9b72007-02-13 18:20:37 +01001794 return exit_value;
Jens Axboeebac4652005-12-08 15:25:21 +01001795}
Jens Axboe50d16972011-09-29 17:45:28 -06001796
1797void reset_fio_state(void)
1798{
1799 groupid = 0;
1800 thread_number = 0;
1801 nr_process = 0;
1802 nr_thread = 0;
1803 done_secs = 0;
1804}
1805
Jens Axboe0ad5edc2011-10-05 12:39:06 +02001806static int endian_check(void)
1807{
1808 union {
1809 uint8_t c[8];
1810 uint64_t v;
1811 } u;
1812 int le = 0, be = 0;
1813
1814 u.v = 0x12;
1815 if (u.c[7] == 0x12)
1816 be = 1;
1817 else if (u.c[0] == 0x12)
1818 le = 1;
1819
1820#if defined(FIO_LITTLE_ENDIAN)
1821 if (be)
1822 return 1;
1823#elif defined(FIO_BIG_ENDIAN)
1824 if (le)
1825 return 1;
1826#else
1827 return 1;
1828#endif
1829
1830 if (!le && !be)
1831 return 1;
1832
1833 return 0;
1834}
1835
Jens Axboe50d16972011-09-29 17:45:28 -06001836int main(int argc, char *argv[], char *envp[])
1837{
1838 long ps;
1839
Jens Axboe0ad5edc2011-10-05 12:39:06 +02001840 if (endian_check()) {
1841 log_err("fio: endianness settings appear wrong.\n");
1842 log_err("fio: please report this to fio@vger.kernel.org\n");
1843 return 1;
1844 }
1845
Jens Axboe50d16972011-09-29 17:45:28 -06001846 arch_init(envp);
1847
1848 sinit();
1849
1850 /*
1851 * We need locale for number printing, if it isn't set then just
1852 * go with the US format.
1853 */
1854 if (!getenv("LC_NUMERIC"))
1855 setlocale(LC_NUMERIC, "en_US");
1856
1857 ps = sysconf(_SC_PAGESIZE);
1858 if (ps < 0) {
1859 log_err("Failed to get page size\n");
1860 return 1;
1861 }
1862
1863 page_size = ps;
1864 page_mask = ps - 1;
1865
1866 fio_keywords_init();
1867
1868 if (parse_options(argc, argv))
1869 return 1;
1870
1871 return exec_run();
1872}