Add support for replaying blktrace trim/discard
[fio.git] / fio.c
CommitLineData
ebac4655
JA
1/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
aae22ca7 5 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
ebac4655 6 *
8e9fe637
JA
7 * The license below covers all files distributed with fio unless otherwise
8 * noted in the file itself.
9 *
ebac4655 10 * This program is free software; you can redistribute it and/or modify
8e9fe637
JA
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
ebac4655
JA
13 *
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 */
ebac4655
JA
24#include <unistd.h>
25#include <fcntl.h>
26#include <string.h>
ebac4655
JA
27#include <signal.h>
28#include <time.h>
dbe1125e 29#include <locale.h>
36167d82 30#include <assert.h>
ebac4655
JA
31#include <sys/stat.h>
32#include <sys/wait.h>
33#include <sys/ipc.h>
34#include <sys/shm.h>
ebac4655
JA
35#include <sys/mman.h>
36
37#include "fio.h"
210f43b3 38#include "hash.h"
2e5cdb11 39#include "smalloc.h"
4f5af7b2 40#include "verify.h"
7c9b1bce 41#include "diskutil.h"
a696fa2a 42#include "cgroup.h"
07b3232d 43#include "profile.h"
d54a144d 44#include "lib/rand.h"
ebac4655 45
cfc99db7
JA
46unsigned long page_mask;
47unsigned long page_size;
d529ee19
JA
48
49#define PAGE_ALIGN(buf) \
29d610e1 50 (char *) (((unsigned long) (buf) + page_mask) & ~page_mask)
ebac4655
JA
51
52int groupid = 0;
53int thread_number = 0;
9cedf167
JA
54int nr_process = 0;
55int nr_thread = 0;
ebac4655 56int shm_id = 0;
53cdc686 57int temp_stall_ts;
d3eeeabc 58unsigned long done_secs = 0;
ebac4655 59
cdd18ad8 60static struct fio_mutex *startup_mutex;
d2bbb8d9 61static struct fio_mutex *writeout_mutex;
6ce15a32 62static volatile int fio_abort;
437c9b71 63static int exit_value;
ccb0fa24 64static struct itimerval itimer;
be4ecfdf 65static pthread_t gtod_thread;
dae5341c 66static struct flist_head *cgroup_list;
6adb38a1 67static char *cgroup_mnt;
ebac4655 68
bb3884d8
JA
69struct io_log *agg_io_log[2];
70
ebac4655 71#define TERMINATE_ALL (-1)
75154845 72#define JOB_START_TIMEOUT (5 * 1000)
ebac4655 73
b29ee5b3 74void td_set_runstate(struct thread_data *td, int runstate)
6ce15a32 75{
d8fab1b6
JA
76 if (td->runstate == runstate)
77 return;
78
5921e80c
JA
79 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
80 td->runstate, runstate);
6ce15a32
JA
81 td->runstate = runstate;
82}
83
390c40e2 84static void terminate_threads(int group_id)
ebac4655 85{
34572e28 86 struct thread_data *td;
ebac4655
JA
87 int i;
88
6f88bcb0
JA
89 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
90
34572e28 91 for_each_td(td, i) {
ebac4655 92 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
6f88bcb0 93 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
5921e80c 94 td->o.name, (int) td->pid);
ad830ed7
JA
95 td->terminate = 1;
96 td->o.start_delay = 0;
97
7a93ab19
JA
98 /*
99 * if the thread is running, just let it exit
100 */
101 if (td->runstate < TD_RUNNING)
c1302d44 102 kill(td->pid, SIGQUIT);
f63f7f3b
JA
103 else {
104 struct ioengine_ops *ops = td->io_ops;
105
106 if (ops && (ops->flags & FIO_SIGQUIT))
107 kill(td->pid, SIGQUIT);
108 }
ebac4655
JA
109 }
110 }
111}
112
ccb0fa24
JA
113static void status_timer_arm(void)
114{
115 itimer.it_value.tv_sec = 0;
116 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
117 setitimer(ITIMER_REAL, &itimer, NULL);
118}
119
02444ad1 120static void sig_alrm(int fio_unused sig)
ebac4655 121{
697a606c 122 if (threads) {
5ec10eaa 123 update_io_ticks();
5ec10eaa 124 print_thread_status();
ccb0fa24 125 status_timer_arm();
697a606c
JA
126 }
127}
128
5f8f9726
JA
129/*
130 * Happens on thread runs with ctrl-c, ignore our own SIGQUIT
131 */
132static void sig_quit(int sig)
133{
134}
135
697a606c
JA
136static void sig_int(int sig)
137{
138 if (threads) {
4ceb30d4 139 log_info("\nfio: terminating on signal %d\n", sig);
5ec10eaa
JA
140 fflush(stdout);
141 terminate_threads(TERMINATE_ALL);
ebac4655
JA
142 }
143}
144
697a606c
JA
145static void set_sig_handlers(void)
146{
147 struct sigaction act;
148
149 memset(&act, 0, sizeof(act));
150 act.sa_handler = sig_alrm;
151 act.sa_flags = SA_RESTART;
152 sigaction(SIGALRM, &act, NULL);
153
154 memset(&act, 0, sizeof(act));
155 act.sa_handler = sig_int;
156 act.sa_flags = SA_RESTART;
157 sigaction(SIGINT, &act, NULL);
de79c915 158
5f8f9726
JA
159 memset(&act, 0, sizeof(act));
160 act.sa_handler = sig_quit;
161 act.sa_flags = SA_RESTART;
162 sigaction(SIGQUIT, &act, NULL);
697a606c
JA
163}
164
906c8d75
JA
165/*
166 * Check if we are above the minimum rate given.
167 */
581e7141
JA
168static int __check_min_rate(struct thread_data *td, struct timeval *now,
169 enum td_ddir ddir)
ebac4655 170{
0904200b 171 unsigned long long bytes = 0;
4e991c23 172 unsigned long iops = 0;
ebac4655
JA
173 unsigned long spent;
174 unsigned long rate;
581e7141
JA
175 unsigned int ratemin = 0;
176 unsigned int rate_iops = 0;
177 unsigned int rate_iops_min = 0;
ebac4655 178
ff58fced
JA
179 assert(ddir_rw(ddir));
180
d982d873
JA
181 if (!td->o.ratemin[ddir] && !td->o.rate_iops_min[ddir])
182 return 0;
183
ebac4655
JA
184 /*
185 * allow a 2 second settle period in the beginning
186 */
187 if (mtime_since(&td->start, now) < 2000)
188 return 0;
189
581e7141
JA
190 iops += td->io_blocks[ddir];
191 bytes += td->this_io_bytes[ddir];
192 ratemin += td->o.ratemin[ddir];
193 rate_iops += td->o.rate_iops[ddir];
194 rate_iops_min += td->o.rate_iops_min[ddir];
0904200b 195
ebac4655
JA
196 /*
197 * if rate blocks is set, sample is running
198 */
581e7141
JA
199 if (td->rate_bytes[ddir] || td->rate_blocks[ddir]) {
200 spent = mtime_since(&td->lastrate[ddir], now);
2dc1bbeb 201 if (spent < td->o.ratecycle)
ebac4655
JA
202 return 0;
203
581e7141 204 if (td->o.rate[ddir]) {
4e991c23
JA
205 /*
206 * check bandwidth specified rate
207 */
581e7141 208 if (bytes < td->rate_bytes[ddir]) {
5ec10eaa 209 log_err("%s: min rate %u not met\n", td->o.name,
581e7141 210 ratemin);
4e991c23
JA
211 return 1;
212 } else {
581e7141
JA
213 rate = ((bytes - td->rate_bytes[ddir]) * 1000) / spent;
214 if (rate < ratemin ||
215 bytes < td->rate_bytes[ddir]) {
5ec10eaa 216 log_err("%s: min rate %u not met, got"
b22989b9 217 " %luKB/sec\n", td->o.name,
581e7141 218 ratemin, rate);
4e991c23
JA
219 return 1;
220 }
221 }
413dd459 222 } else {
4e991c23
JA
223 /*
224 * checks iops specified rate
225 */
581e7141 226 if (iops < rate_iops) {
5ec10eaa 227 log_err("%s: min iops rate %u not met\n",
581e7141 228 td->o.name, rate_iops);
413dd459 229 return 1;
4e991c23 230 } else {
581e7141
JA
231 rate = ((iops - td->rate_blocks[ddir]) * 1000) / spent;
232 if (rate < rate_iops_min ||
233 iops < td->rate_blocks[ddir]) {
5ec10eaa
JA
234 log_err("%s: min iops rate %u not met,"
235 " got %lu\n", td->o.name,
581e7141 236 rate_iops_min, rate);
4e991c23 237 }
413dd459 238 }
ebac4655
JA
239 }
240 }
241
581e7141
JA
242 td->rate_bytes[ddir] = bytes;
243 td->rate_blocks[ddir] = iops;
244 memcpy(&td->lastrate[ddir], now, sizeof(*now));
ebac4655
JA
245 return 0;
246}
247
581e7141
JA
248static int check_min_rate(struct thread_data *td, struct timeval *now,
249 unsigned long *bytes_done)
250{
251 int ret = 0;
252
253 if (bytes_done[0])
254 ret |= __check_min_rate(td, now, 0);
255 if (bytes_done[1])
256 ret |= __check_min_rate(td, now, 1);
257
258 return ret;
259}
260
ebac4655
JA
261static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
262{
2dc1bbeb 263 if (!td->o.timeout)
ebac4655 264 return 0;
2dc1bbeb 265 if (mtime_since(&td->epoch, t) >= td->o.timeout * 1000)
ebac4655
JA
266 return 1;
267
268 return 0;
269}
270
906c8d75
JA
271/*
272 * When job exits, we can cancel the in-flight IO if we are using async
273 * io. Attempt to do so.
274 */
ebac4655
JA
275static void cleanup_pending_aio(struct thread_data *td)
276{
01743ee1 277 struct flist_head *entry, *n;
ebac4655
JA
278 struct io_u *io_u;
279 int r;
280
281 /*
282 * get immediately available events, if any
283 */
581e7141 284 r = io_u_queued_complete(td, 0, NULL);
b2fdda43
JA
285 if (r < 0)
286 return;
ebac4655
JA
287
288 /*
289 * now cancel remaining active events
290 */
2866c82d 291 if (td->io_ops->cancel) {
01743ee1
JA
292 flist_for_each_safe(entry, n, &td->io_u_busylist) {
293 io_u = flist_entry(entry, struct io_u, list);
ebac4655 294
0c6e7517
JA
295 /*
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");
ebac4655 302 put_io_u(td, io_u);
0c6e7517
JA
303 } else {
304 r = td->io_ops->cancel(td, io_u);
305 if (!r)
306 put_io_u(td, io_u);
307 }
ebac4655
JA
308 }
309 }
310
97601024 311 if (td->cur_depth)
581e7141 312 r = io_u_queued_complete(td, td->cur_depth, NULL);
ebac4655
JA
313}
314
858a3d47
JA
315/*
316 * 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);
858a3d47
JA
322 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
755200a3 335requeue:
858a3d47 336 ret = td_io_queue(td, io_u);
36167d82 337 if (ret < 0) {
e1161c32 338 td_verror(td, io_u->error, "td_io_queue");
858a3d47 339 put_io_u(td, io_u);
858a3d47 340 return 1;
36167d82 341 } else if (ret == FIO_Q_QUEUED) {
581e7141 342 if (io_u_queued_complete(td, 1, NULL) < 0)
36167d82 343 return 1;
36167d82
JA
344 } else if (ret == FIO_Q_COMPLETED) {
345 if (io_u->error) {
e1161c32 346 td_verror(td, io_u->error, "td_io_queue");
36167d82
JA
347 return 1;
348 }
858a3d47 349
581e7141 350 if (io_u_sync_complete(td, io_u, NULL) < 0)
b2fdda43 351 return 1;
755200a3
JA
352 } else if (ret == FIO_Q_BUSY) {
353 if (td_io_commit(td))
354 return 1;
355 goto requeue;
858a3d47
JA
356 }
357
358 return 0;
359}
360
993bf48b
JA
361static 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
f2bba182
RR
367static int break_on_this_error(struct thread_data *td, int *retptr)
368{
369 int ret = *retptr;
370
371 if (ret < 0 || td->error) {
372 int err;
373
1ec99eea
JA
374 if (!td->o.continue_on_error)
375 return 1;
f2bba182
RR
376
377 if (ret < 0)
378 err = -ret;
379 else
380 err = td->error;
381
f2bba182
RR
382 if (td_non_fatal_error(err)) {
383 /*
384 * Continue with the I/Os in case of
385 * a non fatal error.
386 */
8a4f8a24 387 update_error_count(td, err);
f2bba182
RR
388 td_clear_error(td);
389 *retptr = 0;
390 return 0;
8a4f8a24
RR
391 } else if (td->o.fill_device && err == ENOSPC) {
392 /*
393 * We expect to hit this error if
394 * fill_device option is set.
395 */
396 td_clear_error(td);
397 td->terminate = 1;
398 return 1;
f2bba182
RR
399 } else {
400 /*
401 * Stop the I/O in case of a fatal
402 * error.
403 */
8a4f8a24 404 update_error_count(td, err);
f2bba182
RR
405 return 1;
406 }
407 }
408
409 return 0;
410}
411
906c8d75 412/*
34403fb1 413 * The main verify engine. Runs over the writes we previously submitted,
906c8d75
JA
414 * reads the blocks back in, and checks the crc/md5 of the data.
415 */
1e97cce9 416static void do_verify(struct thread_data *td)
ebac4655 417{
53cdc686 418 struct fio_file *f;
36167d82 419 struct io_u *io_u;
af52b345
JA
420 int ret, min_events;
421 unsigned int i;
e5b401d4 422
008618a8
JA
423 dprint(FD_VERIFY, "starting loop\n");
424
e5b401d4
JA
425 /*
426 * sync io first and invalidate cache, to make sure we really
427 * read from disk.
428 */
429 for_each_file(td, f, i) {
d6aed795 430 if (!fio_file_open(f))
3d7b485f 431 continue;
b2fdda43
JA
432 if (fio_io_sync(td, f))
433 break;
434 if (file_invalidate_cache(td, f))
435 break;
e5b401d4 436 }
ebac4655 437
b2fdda43
JA
438 if (td->error)
439 return;
440
ebac4655
JA
441 td_set_runstate(td, TD_VERIFYING);
442
36167d82
JA
443 io_u = NULL;
444 while (!td->terminate) {
4950421a 445 int ret2, full;
5451792e 446
993bf48b
JA
447 update_tv_cache(td);
448
449 if (runtime_exceeded(td, &td->tv_cache)) {
90a87d4b 450 td->terminate = 1;
02bcaa8c 451 break;
069c2918 452 }
02bcaa8c 453
dcb4830d
RR
454 io_u = __get_io_u(td);
455 if (!io_u)
456 break;
457
069c2918
JA
458 if (get_next_verify(td, io_u)) {
459 put_io_u(td, io_u);
ebac4655 460 break;
069c2918 461 }
ebac4655 462
069c2918
JA
463 if (td_io_prep(td, io_u)) {
464 put_io_u(td, io_u);
53cdc686 465 break;
069c2918 466 }
d7762cf8 467
e8462bd8
JA
468 if (td->o.verify_async)
469 io_u->end_io = verify_io_u_async;
470 else
471 io_u->end_io = verify_io_u;
53cdc686 472
11786802 473 ret = td_io_queue(td, io_u);
36167d82
JA
474 switch (ret) {
475 case FIO_Q_COMPLETED:
f2bba182 476 if (io_u->error) {
22819ec2 477 ret = -io_u->error;
f2bba182
RR
478 clear_io_u(td, io_u);
479 } else if (io_u->resid) {
36167d82 480 int bytes = io_u->xfer_buflen - io_u->resid;
d460eb31 481 struct fio_file *f = io_u->file;
ebac4655 482
9e9d164e
JA
483 /*
484 * zero read, fail
485 */
486 if (!bytes) {
41d9f0fd 487 td_verror(td, EIO, "full resid");
9e9d164e
JA
488 put_io_u(td, io_u);
489 break;
490 }
8400d9b2 491
36167d82
JA
492 io_u->xfer_buflen = io_u->resid;
493 io_u->xfer_buf += bytes;
8400d9b2
JA
494 io_u->offset += bytes;
495
ff58fced
JA
496 if (ddir_rw(io_u->ddir))
497 td->ts.short_io_u[io_u->ddir]++;
30061b97 498
d460eb31 499 if (io_u->offset == f->real_file_size)
8400d9b2
JA
500 goto sync_done;
501
11786802
JA
502 requeue_io_u(td, &io_u);
503 } else {
8400d9b2 504sync_done:
581e7141 505 ret = io_u_sync_complete(td, io_u, NULL);
11786802
JA
506 if (ret < 0)
507 break;
36167d82 508 }
36167d82
JA
509 continue;
510 case FIO_Q_QUEUED:
511 break;
755200a3
JA
512 case FIO_Q_BUSY:
513 requeue_io_u(td, &io_u);
5451792e
JA
514 ret2 = td_io_commit(td);
515 if (ret2 < 0)
516 ret = ret2;
755200a3 517 break;
36167d82
JA
518 default:
519 assert(ret < 0);
e1161c32 520 td_verror(td, -ret, "td_io_queue");
ebac4655
JA
521 break;
522 }
523
f2bba182 524 if (break_on_this_error(td, &ret))
3af6ef39
JA
525 break;
526
ebac4655 527 /*
3af6ef39
JA
528 * if we can queue more, do so. but check if there are
529 * completed io_u's first.
ebac4655 530 */
4950421a
JA
531 full = queue_full(td) || ret == FIO_Q_BUSY;
532 if (full || !td->o.iodepth_batch_complete) {
6a96276c
RR
533 min_events = min(td->o.iodepth_batch_complete,
534 td->cur_depth);
4950421a 535 if (full && !min_events)
838bc709 536 min_events = 1;
e916b390 537
4950421a
JA
538 do {
539 /*
540 * Reap required number of io units, if any,
541 * and do the verification on them through
542 * the callback handler
543 */
581e7141 544 if (io_u_queued_complete(td, min_events, NULL) < 0) {
4950421a
JA
545 ret = -1;
546 break;
547 }
548 } while (full && (td->cur_depth > td->o.iodepth_low));
549 }
550 if (ret < 0)
ebac4655 551 break;
36167d82 552 }
ebac4655 553
c01c0395
JA
554 if (!td->error) {
555 min_events = td->cur_depth;
556
557 if (min_events)
581e7141 558 ret = io_u_queued_complete(td, min_events, NULL);
c01c0395 559 } else
ebac4655
JA
560 cleanup_pending_aio(td);
561
562 td_set_runstate(td, TD_RUNNING);
008618a8
JA
563
564 dprint(FD_VERIFY, "exiting loop\n");
ebac4655
JA
565}
566
32cd46a0 567/*
906c8d75 568 * Main IO worker function. It retrieves io_u's to process and queues
32cd46a0
JA
569 * and reaps them, checking for rate and errors along the way.
570 */
ebac4655
JA
571static void do_io(struct thread_data *td)
572{
af52b345
JA
573 unsigned int i;
574 int ret = 0;
ebac4655 575
b29ee5b3
JA
576 if (in_ramp_time(td))
577 td_set_runstate(td, TD_RAMP);
578 else
579 td_set_runstate(td, TD_RUNNING);
5853e5a8 580
457bf399
GO
581 while ( (td->o.read_iolog_file && !flist_empty(&td->io_log_list)) ||
582 ((td->this_io_bytes[0] + td->this_io_bytes[1]) < td->o.size) ) {
97601024 583 struct timeval comp_time;
581e7141 584 unsigned long bytes_done[2] = { 0, 0 };
84585003 585 int min_evts = 0;
ebac4655 586 struct io_u *io_u;
4950421a 587 int ret2, full;
ebac4655
JA
588
589 if (td->terminate)
590 break;
591
993bf48b 592 update_tv_cache(td);
97601024 593
993bf48b 594 if (runtime_exceeded(td, &td->tv_cache)) {
90a87d4b 595 td->terminate = 1;
97601024
JA
596 break;
597 }
36167d82 598
dcb4830d
RR
599 io_u = get_io_u(td);
600 if (!io_u)
601 break;
602
b67740d3
JA
603 /*
604 * Add verification end_io handler, if asked to verify
605 * a previously written file.
606 */
715d2b3e
JA
607 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_READ &&
608 !td_rw(td)) {
e8462bd8
JA
609 if (td->o.verify_async)
610 io_u->end_io = verify_io_u_async;
611 else
612 io_u->end_io = verify_io_u;
d8fab1b6 613 td_set_runstate(td, TD_VERIFYING);
b29ee5b3
JA
614 } else if (in_ramp_time(td))
615 td_set_runstate(td, TD_RAMP);
616 else
d8fab1b6 617 td_set_runstate(td, TD_RUNNING);
b67740d3 618
11786802 619 ret = td_io_queue(td, io_u);
36167d82
JA
620 switch (ret) {
621 case FIO_Q_COMPLETED:
f2bba182 622 if (io_u->error) {
5451792e 623 ret = -io_u->error;
f2bba182
RR
624 clear_io_u(td, io_u);
625 } else if (io_u->resid) {
36167d82 626 int bytes = io_u->xfer_buflen - io_u->resid;
d460eb31 627 struct fio_file *f = io_u->file;
36167d82 628
9e9d164e
JA
629 /*
630 * zero read, fail
631 */
632 if (!bytes) {
41d9f0fd 633 td_verror(td, EIO, "full resid");
9e9d164e
JA
634 put_io_u(td, io_u);
635 break;
636 }
637
cec6b55d 638 io_u->xfer_buflen = io_u->resid;
36167d82 639 io_u->xfer_buf += bytes;
5a7c5680
JA
640 io_u->offset += bytes;
641
ff58fced
JA
642 if (ddir_rw(io_u->ddir))
643 td->ts.short_io_u[io_u->ddir]++;
30061b97 644
d460eb31 645 if (io_u->offset == f->real_file_size)
5a7c5680
JA
646 goto sync_done;
647
11786802
JA
648 requeue_io_u(td, &io_u);
649 } else {
5a7c5680 650sync_done:
581e7141
JA
651 if (__should_check_rate(td, 0) ||
652 __should_check_rate(td, 1))
993bf48b
JA
653 fio_gettime(&comp_time, NULL);
654
581e7141
JA
655 ret = io_u_sync_complete(td, io_u, bytes_done);
656 if (ret < 0)
657 break;
cec6b55d 658 }
36167d82
JA
659 break;
660 case FIO_Q_QUEUED:
7e77dd02
JA
661 /*
662 * if the engine doesn't have a commit hook,
663 * the io_u is really queued. if it does have such
664 * a hook, it has to call io_u_queued() itself.
665 */
666 if (td->io_ops->commit == NULL)
667 io_u_queued(td, io_u);
36167d82 668 break;
755200a3
JA
669 case FIO_Q_BUSY:
670 requeue_io_u(td, &io_u);
5451792e
JA
671 ret2 = td_io_commit(td);
672 if (ret2 < 0)
673 ret = ret2;
755200a3 674 break;
36167d82
JA
675 default:
676 assert(ret < 0);
677 put_io_u(td, io_u);
678 break;
ebac4655
JA
679 }
680
f2bba182 681 if (break_on_this_error(td, &ret))
36167d82
JA
682 break;
683
97601024
JA
684 /*
685 * See if we need to complete some commands
686 */
4950421a
JA
687 full = queue_full(td) || ret == FIO_Q_BUSY;
688 if (full || !td->o.iodepth_batch_complete) {
6a96276c
RR
689 min_evts = min(td->o.iodepth_batch_complete,
690 td->cur_depth);
4950421a 691 if (full && !min_evts)
36167d82 692 min_evts = 1;
4950421a 693
581e7141
JA
694 if (__should_check_rate(td, 0) ||
695 __should_check_rate(td, 1))
993bf48b 696 fio_gettime(&comp_time, NULL);
4950421a
JA
697
698 do {
581e7141
JA
699 ret = io_u_queued_complete(td, min_evts, bytes_done);
700 if (ret < 0)
4950421a
JA
701 break;
702
4950421a 703 } while (full && (td->cur_depth > td->o.iodepth_low));
ebac4655
JA
704 }
705
4950421a
JA
706 if (ret < 0)
707 break;
581e7141 708 if (!(bytes_done[0] + bytes_done[1]))
97601024
JA
709 continue;
710
581e7141
JA
711 if (!in_ramp_time(td) && should_check_rate(td, bytes_done)) {
712 if (check_min_rate(td, &comp_time, bytes_done)) {
721938ae
JA
713 if (exitall_on_terminate)
714 terminate_threads(td->groupid);
715 td_verror(td, EIO, "check_min_rate");
716 break;
717 }
ebac4655
JA
718 }
719
2dc1bbeb 720 if (td->o.thinktime) {
9c1f7434
JA
721 unsigned long long b;
722
723 b = td->io_blocks[0] + td->io_blocks[1];
2dc1bbeb 724 if (!(b % td->o.thinktime_blocks)) {
48097d5c
JA
725 int left;
726
2dc1bbeb 727 if (td->o.thinktime_spin)
6dd6f2cd 728 usec_spin(td->o.thinktime_spin);
48097d5c 729
2dc1bbeb 730 left = td->o.thinktime - td->o.thinktime_spin;
48097d5c
JA
731 if (left)
732 usec_sleep(td, left);
733 }
9c1f7434 734 }
ebac4655
JA
735 }
736
aa31f1f1
SL
737 if (td->o.fill_device && td->error == ENOSPC) {
738 td->error = 0;
739 td->terminate = 1;
740 }
4d2413c6 741 if (!td->error) {
3d7c391d
JA
742 struct fio_file *f;
743
c01c0395
JA
744 i = td->cur_depth;
745 if (i)
581e7141 746 ret = io_u_queued_complete(td, i, NULL);
ebac4655 747
2dc1bbeb 748 if (should_fsync(td) && td->o.end_fsync) {
84585003 749 td_set_runstate(td, TD_FSYNCING);
3d7b485f
JA
750
751 for_each_file(td, f, i) {
d6aed795 752 if (!fio_file_open(f))
3d7b485f 753 continue;
858a3d47 754 fio_io_sync(td, f);
3d7b485f 755 }
84585003 756 }
c01c0395
JA
757 } else
758 cleanup_pending_aio(td);
2ba1c290
JA
759
760 /*
761 * stop job if we failed doing any IO
762 */
763 if ((td->this_io_bytes[0] + td->this_io_bytes[1]) == 0)
764 td->done = 1;
ebac4655
JA
765}
766
ebac4655
JA
767static void cleanup_io_u(struct thread_data *td)
768{
01743ee1 769 struct flist_head *entry, *n;
ebac4655
JA
770 struct io_u *io_u;
771
01743ee1
JA
772 flist_for_each_safe(entry, n, &td->io_u_freelist) {
773 io_u = flist_entry(entry, struct io_u, list);
ebac4655 774
01743ee1 775 flist_del(&io_u->list);
ebac4655
JA
776 free(io_u);
777 }
778
2f9ade3c 779 free_io_mem(td);
ebac4655
JA
780}
781
782static int init_io_u(struct thread_data *td)
783{
784 struct io_u *io_u;
a00735e6 785 unsigned int max_bs;
eb7ccf38 786 int cl_align, i, max_units;
ebac4655
JA
787 char *p;
788
1e3c09af 789 max_units = td->o.iodepth;
2dc1bbeb 790 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
5ec10eaa
JA
791 td->orig_buffer_size = (unsigned long long) max_bs
792 * (unsigned long long) max_units;
793
794 if (td->o.mem_type == MEM_SHMHUGE || td->o.mem_type == MEM_MMAPHUGE) {
795 unsigned long bs;
74b025b0 796
5ec10eaa
JA
797 bs = td->orig_buffer_size + td->o.hugepage_size - 1;
798 td->orig_buffer_size = bs & ~(td->o.hugepage_size - 1);
799 }
ebac4655 800
c3990645
JA
801 if (td->orig_buffer_size != (size_t) td->orig_buffer_size) {
802 log_err("fio: IO memory too large. Reduce max_bs or iodepth\n");
803 return 1;
804 }
805
2f9ade3c
JA
806 if (allocate_io_mem(td))
807 return 1;
ebac4655 808
fc966bef 809 if (td->o.odirect || td->o.mem_align)
d529ee19 810 p = PAGE_ALIGN(td->orig_buffer) + td->o.mem_align;
b3f378e9
JA
811 else
812 p = td->orig_buffer;
813
eb7ccf38
JA
814 cl_align = os_cache_line_size();
815
ebac4655 816 for (i = 0; i < max_units; i++) {
eb7ccf38
JA
817 void *ptr;
818
db1cd90b
JA
819 if (td->terminate)
820 return 1;
eb7ccf38
JA
821
822 if (posix_memalign(&ptr, cl_align, sizeof(*io_u))) {
823 log_err("fio: posix_memalign=%s\n", strerror(errno));
824 break;
825 }
826
827 io_u = ptr;
ebac4655 828 memset(io_u, 0, sizeof(*io_u));
01743ee1 829 INIT_FLIST_HEAD(&io_u->list);
d529ee19 830 dprint(FD_MEM, "io_u alloc %p, index %u\n", io_u, i);
ebac4655 831
b4c5e1ac
JA
832 if (!(td->io_ops->flags & FIO_NOIO)) {
833 io_u->buf = p + max_bs * i;
d529ee19 834 dprint(FD_MEM, "io_u %p, mem %p\n", io_u, io_u->buf);
e9459e5a 835
5973cafb
JA
836 if (td_write(td) && !td->o.refill_buffers)
837 io_u_fill_buffer(td, io_u, max_bs);
cbe8d756
RR
838 else if (td_write(td) && td->o.verify_pattern_bytes) {
839 /*
840 * Fill the buffer with the pattern if we are
841 * going to be doing writes.
842 */
843 fill_pattern(td, io_u->buf, max_bs, io_u);
844 }
b4c5e1ac 845 }
6b9cea23 846
b1ff3403 847 io_u->index = i;
0c6e7517 848 io_u->flags = IO_U_F_FREE;
01743ee1 849 flist_add(&io_u->list, &td->io_u_freelist);
ebac4655
JA
850 }
851
852 return 0;
853}
854
da86774e
JA
855static int switch_ioscheduler(struct thread_data *td)
856{
857 char tmp[256], tmp2[128];
858 FILE *f;
859 int ret;
860
ba0fbe10 861 if (td->io_ops->flags & FIO_DISKLESSIO)
f48b467c
JA
862 return 0;
863
da86774e
JA
864 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
865
866 f = fopen(tmp, "r+");
867 if (!f) {
cbf5c1d7 868 if (errno == ENOENT) {
5ec10eaa
JA
869 log_err("fio: os or kernel doesn't support IO scheduler"
870 " switching\n");
cbf5c1d7
JA
871 return 0;
872 }
873 td_verror(td, errno, "fopen iosched");
da86774e
JA
874 return 1;
875 }
876
877 /*
878 * Set io scheduler.
879 */
2dc1bbeb 880 ret = fwrite(td->o.ioscheduler, strlen(td->o.ioscheduler), 1, f);
da86774e 881 if (ferror(f) || ret != 1) {
e1161c32 882 td_verror(td, errno, "fwrite");
da86774e
JA
883 fclose(f);
884 return 1;
885 }
886
887 rewind(f);
888
889 /*
890 * Read back and check that the selected scheduler is now the default.
891 */
892 ret = fread(tmp, 1, sizeof(tmp), f);
893 if (ferror(f) || ret < 0) {
e1161c32 894 td_verror(td, errno, "fread");
da86774e
JA
895 fclose(f);
896 return 1;
897 }
898
2dc1bbeb 899 sprintf(tmp2, "[%s]", td->o.ioscheduler);
da86774e 900 if (!strstr(tmp, tmp2)) {
2dc1bbeb 901 log_err("fio: io scheduler %s not found\n", td->o.ioscheduler);
e1161c32 902 td_verror(td, EINVAL, "iosched_switch");
da86774e
JA
903 fclose(f);
904 return 1;
905 }
906
907 fclose(f);
908 return 0;
909}
910
492158cf
JA
911static int keep_running(struct thread_data *td)
912{
913 unsigned long long io_done;
914
20e354ef
JA
915 if (td->done)
916 return 0;
492158cf
JA
917 if (td->o.time_based)
918 return 1;
919 if (td->o.loops) {
920 td->o.loops--;
921 return 1;
922 }
923
5ec10eaa
JA
924 io_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]
925 + td->io_skip_bytes;
492158cf
JA
926 if (io_done < td->o.size)
927 return 1;
928
929 return 0;
930}
931
b29ee5b3 932static void reset_io_counters(struct thread_data *td)
ebac4655 933{
756867bd 934 td->ts.stat_io_bytes[0] = td->ts.stat_io_bytes[1] = 0;
ebac4655 935 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
20dc95c4 936 td->zone_bytes = 0;
581e7141
JA
937 td->rate_bytes[0] = td->rate_bytes[1] = 0;
938 td->rate_blocks[0] = td->rate_blocks[1] = 0;
ebac4655 939
c1324df1
JA
940 td->last_was_sync = 0;
941
2ba1c290
JA
942 /*
943 * reset file done count if we are to start over
944 */
945 if (td->o.time_based || td->o.loops)
48f5abd3 946 td->nr_done_files = 0;
5bfc35d7
JA
947
948 /*
949 * Set the same seed to get repeatable runs
950 */
951 td_fill_rand_seeds(td);
b29ee5b3
JA
952}
953
954void reset_all_stats(struct thread_data *td)
955{
956 struct timeval tv;
957 int i;
958
959 reset_io_counters(td);
960
961 for (i = 0; i < 2; i++) {
962 td->io_bytes[i] = 0;
963 td->io_blocks[i] = 0;
964 td->io_issues[i] = 0;
965 td->ts.total_io_u[i] = 0;
966 }
967
968 fio_gettime(&tv, NULL);
969 memcpy(&td->epoch, &tv, sizeof(tv));
970 memcpy(&td->start, &tv, sizeof(tv));
971}
972
df9c26b1 973static void clear_io_state(struct thread_data *td)
b29ee5b3
JA
974{
975 struct fio_file *f;
976 unsigned int i;
b29ee5b3
JA
977
978 reset_io_counters(td);
281f9b63 979
24ffd2c2 980 close_files(td);
df9c26b1 981 for_each_file(td, f, i)
d6aed795 982 fio_file_clear_done(f);
ebac4655
JA
983}
984
398e8c4d
JA
985static int exec_string(const char *string)
986{
987 int ret, newlen = strlen(string) + 1 + 8;
988 char *str;
989
990 str = malloc(newlen);
991 sprintf(str, "sh -c %s", string);
992
993 ret = system(str);
994 if (ret == -1)
995 log_err("fio: exec of cmd <%s> failed\n", str);
996
997 free(str);
998 return ret;
999}
1000
906c8d75
JA
1001/*
1002 * Entry point for the thread based jobs. The process based jobs end up
1003 * here as well, after a little setup.
1004 */
ebac4655
JA
1005static void *thread_main(void *data)
1006{
10927316 1007 unsigned long long runtime[2], elapsed;
ebac4655 1008 struct thread_data *td = data;
e8462bd8 1009 pthread_condattr_t attr;
a978ba68 1010 int clear_state;
ebac4655 1011
2dc1bbeb 1012 if (!td->o.use_thread)
ebac4655
JA
1013 setsid();
1014
1015 td->pid = getpid();
1016
5921e80c 1017 dprint(FD_PROCESS, "jobs pid=%d started\n", (int) td->pid);
ee56ad50 1018
01743ee1
JA
1019 INIT_FLIST_HEAD(&td->io_u_freelist);
1020 INIT_FLIST_HEAD(&td->io_u_busylist);
1021 INIT_FLIST_HEAD(&td->io_u_requeues);
1022 INIT_FLIST_HEAD(&td->io_log_list);
1023 INIT_FLIST_HEAD(&td->io_hist_list);
e8462bd8
JA
1024 INIT_FLIST_HEAD(&td->verify_list);
1025 pthread_mutex_init(&td->io_u_lock, NULL);
bb5d7d0b 1026 td->io_hist_tree = RB_ROOT;
aea47d44 1027
e8462bd8
JA
1028 pthread_condattr_init(&attr);
1029 pthread_cond_init(&td->verify_cond, &attr);
1030 pthread_cond_init(&td->free_cond, &attr);
1031
db1cd90b 1032 td_set_runstate(td, TD_INITIALIZED);
29adda3c 1033 dprint(FD_MUTEX, "up startup_mutex\n");
cdd18ad8 1034 fio_mutex_up(startup_mutex);
29adda3c 1035 dprint(FD_MUTEX, "wait on td->mutex\n");
cdd18ad8 1036 fio_mutex_down(td->mutex);
29adda3c 1037 dprint(FD_MUTEX, "done waiting on td->mutex\n");
db1cd90b
JA
1038
1039 /*
cdd18ad8 1040 * the ->mutex mutex is now no longer used, close it to avoid
db1cd90b
JA
1041 * eating a file descriptor
1042 */
cdd18ad8 1043 fio_mutex_remove(td->mutex);
db1cd90b 1044
e0b0d892
JA
1045 if (td->o.uid != -1U && setuid(td->o.uid)) {
1046 td_verror(td, errno, "setuid");
1047 goto err;
1048 }
1049 if (td->o.gid != -1U && setgid(td->o.gid)) {
1050 td_verror(td, errno, "setgid");
1051 goto err;
1052 }
1053
d84f8d49
JA
1054 /*
1055 * May alter parameters that init_io_u() will use, so we need to
1056 * do this first.
1057 */
1058 if (init_iolog(td))
1059 goto err;
1060
ebac4655 1061 if (init_io_u(td))
db1cd90b 1062 goto err;
ebac4655 1063
e8462bd8
JA
1064 if (td->o.verify_async && verify_async_init(td))
1065 goto err;
1066
1067 if (td->o.cpumask_set && fio_setaffinity(td->pid, td->o.cpumask) == -1) {
e1161c32 1068 td_verror(td, errno, "cpu_set_affinity");
db1cd90b 1069 goto err;
ebac4655
JA
1070 }
1071
f484470b
JA
1072 /*
1073 * If we have a gettimeofday() thread, make sure we exclude that
1074 * thread from this job
1075 */
be4ecfdf 1076 if (td->o.gtod_cpu) {
be4ecfdf 1077 fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
e8462bd8 1078 if (fio_setaffinity(td->pid, td->o.cpumask) == -1) {
be4ecfdf
JA
1079 td_verror(td, errno, "cpu_set_affinity");
1080 goto err;
1081 }
1082 }
1083
ac684785 1084 if (td->ioprio_set) {
ebac4655 1085 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
e1161c32 1086 td_verror(td, errno, "ioprio_set");
db1cd90b 1087 goto err;
ebac4655
JA
1088 }
1089 }
1090
6adb38a1 1091 if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt))
a696fa2a
JA
1092 goto err;
1093
2dc1bbeb 1094 if (nice(td->o.nice) == -1) {
e1161c32 1095 td_verror(td, errno, "nice");
db1cd90b 1096 goto err;
b6f4d880
JA
1097 }
1098
2dc1bbeb 1099 if (td->o.ioscheduler && switch_ioscheduler(td))
db1cd90b 1100 goto err;
37f56873 1101
2dc1bbeb 1102 if (!td->o.create_serialize && setup_files(td))
ebac4655
JA
1103 goto err;
1104
7d6c5283
JA
1105 if (td_io_init(td))
1106 goto err;
1107
68727076
JA
1108 if (init_random_map(td))
1109 goto err;
1110
2dc1bbeb 1111 if (td->o.exec_prerun) {
398e8c4d 1112 if (exec_string(td->o.exec_prerun))
69cfd7e0
JA
1113 goto err;
1114 }
4e0ba8af 1115
afad68f7
ZY
1116 if (td->o.pre_read) {
1117 if (pre_read_files(td) < 0)
1118 goto err;
1119 }
1120
69008999 1121 fio_gettime(&td->epoch, NULL);
756867bd 1122 getrusage(RUSAGE_SELF, &td->ts.ru_start);
69008999
JA
1123
1124 runtime[0] = runtime[1] = 0;
a978ba68 1125 clear_state = 0;
492158cf 1126 while (keep_running(td)) {
02bcaa8c 1127 fio_gettime(&td->start, NULL);
c5d3d790
JA
1128 memcpy(&td->ts.stat_sample_time[0], &td->start,
1129 sizeof(td->start));
1130 memcpy(&td->ts.stat_sample_time[1], &td->start,
1131 sizeof(td->start));
993bf48b 1132 memcpy(&td->tv_cache, &td->start, sizeof(td->start));
ebac4655 1133
cf2dd8d6 1134 if (td->o.ratemin[0] || td->o.ratemin[1])
5ec10eaa
JA
1135 memcpy(&td->lastrate, &td->ts.stat_sample_time,
1136 sizeof(td->lastrate));
ebac4655 1137
df9c26b1
JA
1138 if (clear_state)
1139 clear_io_state(td);
a978ba68 1140
ebac4655
JA
1141 prune_io_piece_log(td);
1142
ba0fbe10 1143 do_io(td);
ebac4655 1144
a978ba68
JA
1145 clear_state = 1;
1146
38d77cae 1147 if (td_read(td) && td->io_bytes[DDIR_READ]) {
0a4957c6 1148 elapsed = utime_since_now(&td->start);
38d77cae
JA
1149 runtime[DDIR_READ] += elapsed;
1150 }
1151 if (td_write(td) && td->io_bytes[DDIR_WRITE]) {
0a4957c6 1152 elapsed = utime_since_now(&td->start);
38d77cae
JA
1153 runtime[DDIR_WRITE] += elapsed;
1154 }
5ec10eaa 1155
ebac4655
JA
1156 if (td->error || td->terminate)
1157 break;
1158
e84c73a8
SL
1159 if (!td->o.do_verify ||
1160 td->o.verify == VERIFY_NONE ||
b67740d3 1161 (td->io_ops->flags & FIO_UNIDIR))
ebac4655
JA
1162 continue;
1163
df9c26b1 1164 clear_io_state(td);
a978ba68 1165
02bcaa8c 1166 fio_gettime(&td->start, NULL);
ebac4655
JA
1167
1168 do_verify(td);
1169
69008999 1170 runtime[DDIR_READ] += utime_since_now(&td->start);
ebac4655
JA
1171
1172 if (td->error || td->terminate)
1173 break;
1174 }
1175
36dff966 1176 update_rusage_stat(td);
47f0cc48
YHJT
1177 td->ts.runtime[0] = (runtime[0] + 999) / 1000;
1178 td->ts.runtime[1] = (runtime[1] + 999) / 1000;
756867bd
JA
1179 td->ts.total_run_time = mtime_since_now(&td->epoch);
1180 td->ts.io_bytes[0] = td->io_bytes[0];
1181 td->ts.io_bytes[1] = td->io_bytes[1];
1182
d2bbb8d9 1183 fio_mutex_down(writeout_mutex);
e3cedca7 1184 if (td->ts.bw_log) {
f484470b
JA
1185 if (td->o.bw_log_file) {
1186 finish_log_named(td, td->ts.bw_log,
1187 td->o.bw_log_file, "bw");
1188 } else
e3cedca7
JA
1189 finish_log(td, td->ts.bw_log, "bw");
1190 }
02af0988
JA
1191 if (td->ts.lat_log) {
1192 if (td->o.lat_log_file) {
1193 finish_log_named(td, td->ts.lat_log,
1194 td->o.lat_log_file, "lat");
1195 } else
1196 finish_log(td, td->ts.lat_log, "lat");
1197 }
e3cedca7 1198 if (td->ts.slat_log) {
f484470b
JA
1199 if (td->o.lat_log_file) {
1200 finish_log_named(td, td->ts.slat_log,
1ca95738 1201 td->o.lat_log_file, "slat");
f484470b 1202 } else
e3cedca7
JA
1203 finish_log(td, td->ts.slat_log, "slat");
1204 }
1205 if (td->ts.clat_log) {
f484470b
JA
1206 if (td->o.lat_log_file) {
1207 finish_log_named(td, td->ts.clat_log,
1208 td->o.lat_log_file, "clat");
1209 } else
e3cedca7
JA
1210 finish_log(td, td->ts.clat_log, "clat");
1211 }
d2bbb8d9 1212 fio_mutex_up(writeout_mutex);
398e8c4d
JA
1213 if (td->o.exec_postrun)
1214 exec_string(td->o.exec_postrun);
ebac4655
JA
1215
1216 if (exitall_on_terminate)
390c40e2 1217 terminate_threads(td->groupid);
ebac4655
JA
1218
1219err:
5bf13a5a 1220 if (td->error)
4ceb30d4 1221 log_info("fio: pid=%d, err=%d/%s\n", (int) td->pid, td->error,
5ec10eaa 1222 td->verror);
b4707ed7
JA
1223
1224 if (td->o.verify_async)
1225 verify_async_exit(td);
1226
24ffd2c2 1227 close_and_free_files(td);
2866c82d 1228 close_ioengine(td);
ebac4655 1229 cleanup_io_u(td);
6adb38a1 1230 cgroup_shutdown(td, &cgroup_mnt);
f29b25a3 1231
d2ce18b5
JA
1232 if (td->o.cpumask_set) {
1233 int ret = fio_cpuset_exit(&td->o.cpumask);
1234
1235 td_verror(td, ret, "fio_cpuset_exit");
1236 }
4e78e405 1237
f29b25a3
JA
1238 /*
1239 * do this very late, it will log file closing as well
1240 */
1241 if (td->o.write_iolog_file)
1242 write_iolog_close(td);
1243
d23bb327 1244 options_mem_free(td);
ebac4655 1245 td_set_runstate(td, TD_EXITED);
43d76807 1246 return (void *) (unsigned long) td->error;
ebac4655
JA
1247}
1248
906c8d75
JA
1249/*
1250 * We cannot pass the td data into a forked process, so attach the td and
1251 * pass it to the thread worker.
1252 */
a6418147 1253static int fork_main(int shmid, int offset)
ebac4655
JA
1254{
1255 struct thread_data *td;
a6418147 1256 void *data, *ret;
ebac4655
JA
1257
1258 data = shmat(shmid, NULL, 0);
1259 if (data == (void *) -1) {
a6418147
JA
1260 int __err = errno;
1261
ebac4655 1262 perror("shmat");
a6418147 1263 return __err;
ebac4655
JA
1264 }
1265
1266 td = data + offset * sizeof(struct thread_data);
a6418147 1267 ret = thread_main(td);
ebac4655 1268 shmdt(data);
43d76807 1269 return (int) (unsigned long) ret;
ebac4655
JA
1270}
1271
906c8d75
JA
1272/*
1273 * Run over the job map and reap the threads that have exited, if any.
1274 */
ebac4655
JA
1275static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1276{
34572e28 1277 struct thread_data *td;
1f809d15 1278 int i, cputhreads, realthreads, pending, status, ret;
ebac4655
JA
1279
1280 /*
1281 * reap exited threads (TD_EXITED -> TD_REAPED)
1282 */
1f809d15 1283 realthreads = pending = cputhreads = 0;
34572e28 1284 for_each_td(td, i) {
3707f45b 1285 int flags = 0;
a2f77c9f 1286
84585003
JA
1287 /*
1288 * ->io_ops is NULL for a thread that has closed its
1289 * io engine
1290 */
ba0fbe10 1291 if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
b990b5c0 1292 cputhreads++;
1f809d15
JA
1293 else
1294 realthreads++;
b990b5c0 1295
a1dedc1f
AC
1296 if (!td->pid) {
1297 pending++;
1298 continue;
1299 }
1300 if (td->runstate == TD_REAPED)
a2f77c9f 1301 continue;
2dc1bbeb 1302 if (td->o.use_thread) {
3707f45b
JA
1303 if (td->runstate == TD_EXITED) {
1304 td_set_runstate(td, TD_REAPED);
1305 goto reaped;
1306 }
1307 continue;
1308 }
a2f77c9f
JA
1309
1310 flags = WNOHANG;
1311 if (td->runstate == TD_EXITED)
1312 flags = 0;
1313
1314 /*
1315 * check if someone quit or got killed in an unusual way
1316 */
1317 ret = waitpid(td->pid, &status, flags);
3707f45b 1318 if (ret < 0) {
a2f77c9f 1319 if (errno == ECHILD) {
5921e80c
JA
1320 log_err("fio: pid=%d disappeared %d\n",
1321 (int) td->pid, td->runstate);
a2f77c9f
JA
1322 td_set_runstate(td, TD_REAPED);
1323 goto reaped;
1324 }
1325 perror("waitpid");
1326 } else if (ret == td->pid) {
1327 if (WIFSIGNALED(status)) {
fab6aa71
JA
1328 int sig = WTERMSIG(status);
1329
d9244941 1330 if (sig != SIGQUIT)
5ec10eaa 1331 log_err("fio: pid=%d, got signal=%d\n",
5921e80c 1332 (int) td->pid, sig);
fab6aa71
JA
1333 td_set_runstate(td, TD_REAPED);
1334 goto reaped;
1335 }
a2f77c9f
JA
1336 if (WIFEXITED(status)) {
1337 if (WEXITSTATUS(status) && !td->error)
1338 td->error = WEXITSTATUS(status);
a2f77c9f 1339
a2f77c9f
JA
1340 td_set_runstate(td, TD_REAPED);
1341 goto reaped;
a6418147
JA
1342 }
1343 }
ebac4655 1344
a2f77c9f
JA
1345 /*
1346 * thread is not dead, continue
1347 */
e48676ba 1348 pending++;
a2f77c9f 1349 continue;
fab6aa71 1350reaped:
ebac4655 1351 (*nr_running)--;
581e7141
JA
1352 (*m_rate) -= (td->o.ratemin[0] + td->o.ratemin[1]);
1353 (*t_rate) -= (td->o.rate[0] + td->o.rate[1]);
6f88bcb0
JA
1354 if (!td->pid)
1355 pending--;
a2f77c9f
JA
1356
1357 if (td->error)
1358 exit_value++;
d3eeeabc
JA
1359
1360 done_secs += mtime_since_now(&td->epoch) / 1000;
ebac4655 1361 }
b990b5c0 1362
1f809d15 1363 if (*nr_running == cputhreads && !pending && realthreads)
390c40e2 1364 terminate_threads(TERMINATE_ALL);
ebac4655
JA
1365}
1366
be4ecfdf
JA
1367static void *gtod_thread_main(void *data)
1368{
1369 fio_mutex_up(startup_mutex);
1370
1371 /*
1372 * As long as we have jobs around, update the clock. It would be nice
1373 * to have some way of NOT hammering that CPU with gettimeofday(),
1374 * but I'm not sure what to use outside of a simple CPU nop to relax
1375 * it - we don't want to lose precision.
1376 */
1377 while (threads) {
1378 fio_gtod_update();
1379 nop;
1380 }
1381
1382 return NULL;
1383}
1384
1385static int fio_start_gtod_thread(void)
1386{
304a47c7 1387 pthread_attr_t attr;
f42c153d
JA
1388 int ret;
1389
304a47c7
VA
1390 pthread_attr_init(&attr);
1391 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1392 ret = pthread_create(&gtod_thread, &attr, gtod_thread_main, NULL);
1393 pthread_attr_destroy(&attr);
f42c153d
JA
1394 if (ret) {
1395 log_err("Can't create gtod thread: %s\n", strerror(ret));
be4ecfdf
JA
1396 return 1;
1397 }
f42c153d
JA
1398
1399 ret = pthread_detach(gtod_thread);
1400 if (ret) {
1401 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
be4ecfdf
JA
1402 return 1;
1403 }
1404
29adda3c 1405 dprint(FD_MUTEX, "wait on startup_mutex\n");
be4ecfdf 1406 fio_mutex_down(startup_mutex);
29adda3c 1407 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
be4ecfdf
JA
1408 return 0;
1409}
1410
906c8d75
JA
1411/*
1412 * Main function for kicking off and reaping jobs, as needed.
1413 */
ebac4655
JA
1414static void run_threads(void)
1415{
ebac4655
JA
1416 struct thread_data *td;
1417 unsigned long spent;
1418 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2 1419
2f9ade3c
JA
1420 if (fio_pin_memory())
1421 return;
ebac4655 1422
be4ecfdf
JA
1423 if (fio_gtod_offload && fio_start_gtod_thread())
1424 return;
1425
c6ae0a5b 1426 if (!terse_output) {
4ceb30d4 1427 log_info("Starting ");
9cedf167 1428 if (nr_thread)
4ceb30d4 1429 log_info("%d thread%s", nr_thread,
5ec10eaa 1430 nr_thread > 1 ? "s" : "");
9cedf167
JA
1431 if (nr_process) {
1432 if (nr_thread)
1433 printf(" and ");
4ceb30d4 1434 log_info("%d process%s", nr_process,
5ec10eaa 1435 nr_process > 1 ? "es" : "");
9cedf167 1436 }
4ceb30d4 1437 log_info("\n");
c6ae0a5b
JA
1438 fflush(stdout);
1439 }
c04f7ec3 1440
697a606c 1441 set_sig_handlers();
4efa970e 1442
ebac4655
JA
1443 todo = thread_number;
1444 nr_running = 0;
1445 nr_started = 0;
1446 m_rate = t_rate = 0;
1447
34572e28 1448 for_each_td(td, i) {
263e529f 1449 print_status_init(td->thread_number - 1);
ebac4655 1450
2dc1bbeb 1451 if (!td->o.create_serialize) {
380cf265 1452 init_disk_util(td);
ebac4655 1453 continue;
380cf265 1454 }
ebac4655
JA
1455
1456 /*
1457 * do file setup here so it happens sequentially,
1458 * we don't want X number of threads getting their
1459 * client data interspersed on disk
1460 */
53cdc686 1461 if (setup_files(td)) {
5bf13a5a
JA
1462 exit_value++;
1463 if (td->error)
5921e80c
JA
1464 log_err("fio: pid=%d, err=%d/%s\n",
1465 (int) td->pid, td->error, td->verror);
ebac4655
JA
1466 td_set_runstate(td, TD_REAPED);
1467 todo--;
c69e8875
JA
1468 } else {
1469 struct fio_file *f;
1470 unsigned int i;
1471
1472 /*
1473 * for sharing to work, each job must always open
1474 * its own files. so close them, if we opened them
1475 * for creation
1476 */
22a57ba8
JA
1477 for_each_file(td, f, i) {
1478 if (fio_file_open(f))
1479 td_io_close_file(td, f);
22a57ba8 1480 }
5ec10eaa 1481 }
380cf265
JA
1482
1483 init_disk_util(td);
ebac4655
JA
1484 }
1485
a2f77c9f
JA
1486 set_genesis_time();
1487
ebac4655 1488 while (todo) {
75154845
JA
1489 struct thread_data *map[MAX_JOBS];
1490 struct timeval this_start;
1491 int this_jobs = 0, left;
1492
ebac4655
JA
1493 /*
1494 * create threads (TD_NOT_CREATED -> TD_CREATED)
1495 */
34572e28 1496 for_each_td(td, i) {
ebac4655
JA
1497 if (td->runstate != TD_NOT_CREATED)
1498 continue;
1499
1500 /*
1501 * never got a chance to start, killed by other
1502 * thread for some reason
1503 */
1504 if (td->terminate) {
1505 todo--;
1506 continue;
1507 }
1508
2dc1bbeb 1509 if (td->o.start_delay) {
263e529f 1510 spent = mtime_since_genesis();
ebac4655 1511
2dc1bbeb 1512 if (td->o.start_delay * 1000 > spent)
ebac4655
JA
1513 continue;
1514 }
1515
6f88bcb0
JA
1516 if (td->o.stonewall && (nr_started || nr_running)) {
1517 dprint(FD_PROCESS, "%s: stonewall wait\n",
1518 td->o.name);
ebac4655 1519 break;
6f88bcb0 1520 }
ebac4655 1521
75154845
JA
1522 /*
1523 * Set state to created. Thread will transition
1524 * to TD_INITIALIZED when it's done setting up.
1525 */
ebac4655 1526 td_set_runstate(td, TD_CREATED);
75154845 1527 map[this_jobs++] = td;
ebac4655
JA
1528 nr_started++;
1529
2dc1bbeb 1530 if (td->o.use_thread) {
f42c153d
JA
1531 int ret;
1532
ee56ad50 1533 dprint(FD_PROCESS, "will pthread_create\n");
f42c153d
JA
1534 ret = pthread_create(&td->thread, NULL,
1535 thread_main, td);
1536 if (ret) {
1537 log_err("pthread_create: %s\n",
1538 strerror(ret));
ebac4655 1539 nr_started--;
c8bb6faf 1540 break;
ebac4655 1541 }
f42c153d
JA
1542 ret = pthread_detach(td->thread);
1543 if (ret)
1544 log_err("pthread_detach: %s",
1545 strerror(ret));
ebac4655 1546 } else {
5e1d306e 1547 pid_t pid;
ee56ad50 1548 dprint(FD_PROCESS, "will fork\n");
5e1d306e
JA
1549 pid = fork();
1550 if (!pid) {
a6418147
JA
1551 int ret = fork_main(shm_id, i);
1552
5e1d306e
JA
1553 _exit(ret);
1554 } else if (i == fio_debug_jobno)
1555 *fio_debug_jobp = pid;
ebac4655 1556 }
29adda3c 1557 dprint(FD_MUTEX, "wait on startup_mutex\n");
656b1393
JA
1558 if (fio_mutex_down_timeout(startup_mutex, 10)) {
1559 log_err("fio: job startup hung? exiting.\n");
1560 terminate_threads(TERMINATE_ALL);
1561 fio_abort = 1;
1562 nr_started--;
1563 break;
1564 }
29adda3c 1565 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
ebac4655
JA
1566 }
1567
1568 /*
75154845
JA
1569 * Wait for the started threads to transition to
1570 * TD_INITIALIZED.
ebac4655 1571 */
02bcaa8c 1572 fio_gettime(&this_start, NULL);
75154845 1573 left = this_jobs;
6ce15a32 1574 while (left && !fio_abort) {
75154845
JA
1575 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1576 break;
1577
1578 usleep(100000);
1579
1580 for (i = 0; i < this_jobs; i++) {
1581 td = map[i];
1582 if (!td)
1583 continue;
b6f4d880 1584 if (td->runstate == TD_INITIALIZED) {
75154845
JA
1585 map[i] = NULL;
1586 left--;
b6f4d880
JA
1587 } else if (td->runstate >= TD_EXITED) {
1588 map[i] = NULL;
1589 left--;
1590 todo--;
1591 nr_running++; /* work-around... */
75154845
JA
1592 }
1593 }
1594 }
1595
1596 if (left) {
3b70d7e5 1597 log_err("fio: %d jobs failed to start\n", left);
75154845
JA
1598 for (i = 0; i < this_jobs; i++) {
1599 td = map[i];
1600 if (!td)
1601 continue;
1602 kill(td->pid, SIGTERM);
1603 }
1604 break;
1605 }
1606
1607 /*
b6f4d880 1608 * start created threads (TD_INITIALIZED -> TD_RUNNING).
75154845 1609 */
34572e28 1610 for_each_td(td, i) {
75154845 1611 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
1612 continue;
1613
b29ee5b3
JA
1614 if (in_ramp_time(td))
1615 td_set_runstate(td, TD_RAMP);
1616 else
1617 td_set_runstate(td, TD_RUNNING);
ebac4655
JA
1618 nr_running++;
1619 nr_started--;
581e7141
JA
1620 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
1621 t_rate += td->o.rate[0] + td->o.rate[1];
75154845 1622 todo--;
cdd18ad8 1623 fio_mutex_up(td->mutex);
ebac4655
JA
1624 }
1625
1626 reap_threads(&nr_running, &t_rate, &m_rate);
1627
1628 if (todo)
1629 usleep(100000);
1630 }
1631
1632 while (nr_running) {
1633 reap_threads(&nr_running, &t_rate, &m_rate);
1634 usleep(10000);
1635 }
1636
1637 update_io_ticks();
2f9ade3c 1638 fio_unpin_memory();
ebac4655
JA
1639}
1640
ebac4655
JA
1641int main(int argc, char *argv[])
1642{
29d610e1
JA
1643 long ps;
1644
2e5cdb11 1645 sinit();
d54a144d 1646 init_rand(&__fio_rand_state);
2e5cdb11 1647
dbe1125e
JA
1648 /*
1649 * We need locale for number printing, if it isn't set then just
1650 * go with the US format.
1651 */
1652 if (!getenv("LC_NUMERIC"))
1653 setlocale(LC_NUMERIC, "en_US");
1654
29d610e1
JA
1655 ps = sysconf(_SC_PAGESIZE);
1656 if (ps < 0) {
1657 log_err("Failed to get page size\n");
1658 return 1;
1659 }
1660
cfc99db7 1661 page_size = ps;
29d610e1
JA
1662 page_mask = ps - 1;
1663
74929ac2
JA
1664 fio_keywords_init();
1665
1666 if (parse_options(argc, argv))
1667 return 1;
1668
07b3232d
JA
1669 if (exec_profile && load_profile(exec_profile))
1670 return 1;
1671
74929ac2
JA
1672 if (!thread_number)
1673 return 0;
1674
bb3884d8
JA
1675 if (write_bw_log) {
1676 setup_log(&agg_io_log[DDIR_READ]);
1677 setup_log(&agg_io_log[DDIR_WRITE]);
1678 }
1679
cdd18ad8 1680 startup_mutex = fio_mutex_init(0);
d2bbb8d9 1681 writeout_mutex = fio_mutex_init(1);
07739b57 1682
a2f77c9f
JA
1683 set_genesis_time();
1684
ccb0fa24 1685 status_timer_arm();
ebac4655 1686
dae5341c
JA
1687 cgroup_list = smalloc(sizeof(*cgroup_list));
1688 INIT_FLIST_HEAD(cgroup_list);
1689
ebac4655 1690 run_threads();
6ce15a32 1691
bb3884d8 1692 if (!fio_abort) {
6ce15a32 1693 show_run_stats();
bb3884d8 1694 if (write_bw_log) {
5ec10eaa
JA
1695 __finish_log(agg_io_log[DDIR_READ], "agg-read_bw.log");
1696 __finish_log(agg_io_log[DDIR_WRITE],
1697 "agg-write_bw.log");
bb3884d8
JA
1698 }
1699 }
ebac4655 1700
dae5341c
JA
1701 cgroup_kill(cgroup_list);
1702 sfree(cgroup_list);
61eb313e 1703 sfree(cgroup_mnt);
39f22027 1704
cdd18ad8 1705 fio_mutex_remove(startup_mutex);
d2bbb8d9 1706 fio_mutex_remove(writeout_mutex);
437c9b71 1707 return exit_value;
ebac4655 1708}