[PATCH] Always create full file, don't extend
[fio.git] / fio.c
CommitLineData
ebac4655
JA
1/*
2 * fio - the flexible io tester
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
ebac4655
JA
21#include <unistd.h>
22#include <fcntl.h>
23#include <string.h>
ebac4655
JA
24#include <signal.h>
25#include <time.h>
ebac4655 26#include <assert.h>
ebac4655
JA
27#include <sys/stat.h>
28#include <sys/wait.h>
29#include <sys/ipc.h>
30#include <sys/shm.h>
31#include <sys/ioctl.h>
32#include <sys/mman.h>
33
34#include "fio.h"
35#include "os.h"
36
37#define MASK (4095)
38
39#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
40
41int groupid = 0;
42int thread_number = 0;
43static char run_str[MAX_JOBS + 1];
44int shm_id = 0;
5289b847 45static struct timeval genesis;
88c6ed80 46static int temp_stall_ts;
ebac4655 47
ebac4655
JA
48static void print_thread_status(void);
49
c04f7ec3
JA
50extern unsigned long long mlock_size;
51
ebac4655 52/*
79809113
JA
53 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
54 * will never back again. It may cycle between running/verififying/fsyncing.
55 * Once the thread reaches TD_EXITED, it is just waiting for the core to
56 * reap it.
ebac4655
JA
57 */
58enum {
59 TD_NOT_CREATED = 0,
60 TD_CREATED,
75154845 61 TD_INITIALIZED,
ebac4655
JA
62 TD_RUNNING,
63 TD_VERIFYING,
5853e5a8 64 TD_FSYNCING,
ebac4655
JA
65 TD_EXITED,
66 TD_REAPED,
67};
68
3d60d1ed 69#define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
ebac4655 70
bbfd6b00 71static volatile int startup_sem;
ebac4655
JA
72
73#define TERMINATE_ALL (-1)
75154845 74#define JOB_START_TIMEOUT (5 * 1000)
ebac4655
JA
75
76static void terminate_threads(int group_id)
77{
78 int i;
79
80 for (i = 0; i < thread_number; i++) {
81 struct thread_data *td = &threads[i];
82
83 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
84 td->terminate = 1;
85 td->start_delay = 0;
86 }
87 }
88}
89
90static void sig_handler(int sig)
91{
92 switch (sig) {
93 case SIGALRM:
94 update_io_ticks();
95 disk_util_timer_arm();
96 print_thread_status();
97 break;
98 default:
99 printf("\nfio: terminating on signal\n");
100 fflush(stdout);
101 terminate_threads(TERMINATE_ALL);
102 break;
103 }
104}
105
ebac4655
JA
106static int random_map_free(struct thread_data *td, unsigned long long block)
107{
108 unsigned int idx = RAND_MAP_IDX(td, block);
109 unsigned int bit = RAND_MAP_BIT(td, block);
110
111 return (td->file_map[idx] & (1UL << bit)) == 0;
112}
113
114static int get_next_free_block(struct thread_data *td, unsigned long long *b)
115{
116 int i;
117
118 *b = 0;
119 i = 0;
120 while ((*b) * td->min_bs < td->io_size) {
121 if (td->file_map[i] != -1UL) {
122 *b += ffz(td->file_map[i]);
123 return 0;
124 }
125
126 *b += BLOCKS_PER_MAP;
127 i++;
128 }
129
130 return 1;
131}
132
133static void mark_random_map(struct thread_data *td, struct io_u *io_u)
134{
200bc855 135 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
ebac4655
JA
136 unsigned int blocks = 0;
137
138 while (blocks < (io_u->buflen / td->min_bs)) {
139 unsigned int idx, bit;
140
141 if (!random_map_free(td, block))
142 break;
143
144 idx = RAND_MAP_IDX(td, block);
145 bit = RAND_MAP_BIT(td, block);
146
147 assert(idx < td->num_maps);
148
149 td->file_map[idx] |= (1UL << bit);
150 block++;
151 blocks++;
152 }
153
154 if ((blocks * td->min_bs) < io_u->buflen)
155 io_u->buflen = blocks * td->min_bs;
156}
157
20dc95c4
JA
158static int get_next_offset(struct thread_data *td, unsigned long long *offset)
159{
160 unsigned long long b, rb;
161 long r;
162
163 if (!td->sequential) {
085227ad 164 unsigned long long max_blocks = td->io_size / td->min_bs;
20dc95c4
JA
165 int loops = 50;
166
167 do {
6dfd46b9 168 r = os_random_long(&td->random_state);
085227ad 169 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
20dc95c4
JA
170 rb = b + (td->file_offset / td->min_bs);
171 loops--;
172 } while (!random_map_free(td, rb) && loops);
173
174 if (!loops) {
175 if (get_next_free_block(td, &b))
176 return 1;
177 }
178 } else
179 b = td->last_pos / td->min_bs;
180
181 *offset = (b * td->min_bs) + td->file_offset;
838a3cd3 182 if (*offset > td->real_file_size)
20dc95c4
JA
183 return 1;
184
185 return 0;
186}
187
188static unsigned int get_next_buflen(struct thread_data *td)
189{
190 unsigned int buflen;
191 long r;
192
193 if (td->min_bs == td->max_bs)
194 buflen = td->min_bs;
195 else {
6dfd46b9 196 r = os_random_long(&td->bsrange_state);
20dc95c4
JA
197 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
198 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
199 }
200
201 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
202 buflen = td->io_size - td->this_io_bytes[td->ddir];
203
204 return buflen;
205}
206
ebac4655
JA
207static int check_min_rate(struct thread_data *td, struct timeval *now)
208{
209 unsigned long spent;
210 unsigned long rate;
211 int ddir = td->ddir;
212
213 /*
214 * allow a 2 second settle period in the beginning
215 */
216 if (mtime_since(&td->start, now) < 2000)
217 return 0;
218
219 /*
220 * if rate blocks is set, sample is running
221 */
222 if (td->rate_bytes) {
223 spent = mtime_since(&td->lastrate, now);
224 if (spent < td->ratecycle)
225 return 0;
226
227 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
228 if (rate < td->ratemin) {
eb8bbf48 229 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
ebac4655
JA
230 if (rate_quit)
231 terminate_threads(td->groupid);
232 return 1;
233 }
234 }
235
236 td->rate_bytes = td->this_io_bytes[ddir];
237 memcpy(&td->lastrate, now, sizeof(*now));
238 return 0;
239}
240
241static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
242{
243 if (!td->timeout)
244 return 0;
245 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
246 return 1;
247
248 return 0;
249}
250
251static void fill_random_bytes(struct thread_data *td,
252 unsigned char *p, unsigned int len)
253{
254 unsigned int todo;
255 double r;
256
257 while (len) {
6dfd46b9 258 r = os_random_double(&td->verify_state);
ebac4655
JA
259
260 /*
261 * lrand48_r seems to be broken and only fill the bottom
262 * 32-bits, even on 64-bit archs with 64-bit longs
263 */
264 todo = sizeof(r);
265 if (todo > len)
266 todo = len;
267
268 memcpy(p, &r, todo);
269
270 len -= todo;
271 p += todo;
272 }
273}
274
275static void hexdump(void *buffer, int len)
276{
277 unsigned char *p = buffer;
278 int i;
279
280 for (i = 0; i < len; i++)
eb8bbf48
JA
281 fprintf(f_out, "%02x", p[i]);
282 fprintf(f_out, "\n");
ebac4655
JA
283}
284
285static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
286{
287 unsigned char *p = (unsigned char *) io_u->buf;
288 unsigned long c;
ebac4655
JA
289
290 p += sizeof(*hdr);
291 c = crc32(p, hdr->len - sizeof(*hdr));
ebac4655 292
22f78b32 293 if (c != hdr->crc32) {
3b70d7e5
JA
294 log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
295 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
22f78b32 296 return 1;
ebac4655
JA
297 }
298
22f78b32 299 return 0;
ebac4655
JA
300}
301
302static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
303{
304 unsigned char *p = (unsigned char *) io_u->buf;
305 struct md5_ctx md5_ctx;
ebac4655
JA
306
307 memset(&md5_ctx, 0, sizeof(md5_ctx));
308 p += sizeof(*hdr);
309 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
310
22f78b32 311 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
3b70d7e5 312 log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
ebac4655
JA
313 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
314 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
22f78b32 315 return 1;
ebac4655
JA
316 }
317
22f78b32 318 return 0;
ebac4655
JA
319}
320
321static int verify_io_u(struct io_u *io_u)
322{
323 struct verify_header *hdr = (struct verify_header *) io_u->buf;
324 int ret;
325
326 if (hdr->fio_magic != FIO_HDR_MAGIC)
327 return 1;
328
329 if (hdr->verify_type == VERIFY_MD5)
330 ret = verify_io_u_md5(hdr, io_u);
331 else if (hdr->verify_type == VERIFY_CRC32)
332 ret = verify_io_u_crc32(hdr, io_u);
333 else {
3b70d7e5 334 log_err("Bad verify type %d\n", hdr->verify_type);
ebac4655
JA
335 ret = 1;
336 }
337
338 return ret;
339}
340
341static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
342{
343 hdr->crc32 = crc32(p, len);
344}
345
346static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
347{
348 struct md5_ctx md5_ctx;
349
350 memset(&md5_ctx, 0, sizeof(md5_ctx));
351 md5_update(&md5_ctx, p, len);
352 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
353}
354
a6ccc7be 355static int get_rw_ddir(struct thread_data *td)
3d60d1ed 356{
a6ccc7be
JA
357 if (td_rw(td)) {
358 struct timeval now;
359 unsigned long elapsed;
3d60d1ed 360
a6ccc7be
JA
361 gettimeofday(&now, NULL);
362 elapsed = mtime_since_now(&td->rwmix_switch);
3d60d1ed 363
a6ccc7be
JA
364 /*
365 * Check if it's time to seed a new data direction.
366 */
367 if (elapsed >= td->rwmixcycle) {
c1ee2ca4 368 int v;
a6ccc7be 369 long r;
3d60d1ed 370
c1ee2ca4
JA
371 r = os_random_long(&td->rwmix_state);
372 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
a6ccc7be
JA
373 if (v < td->rwmixread)
374 td->rwmix_ddir = DDIR_READ;
375 else
376 td->rwmix_ddir = DDIR_WRITE;
377 memcpy(&td->rwmix_switch, &now, sizeof(now));
378 }
379 return td->rwmix_ddir;
380 } else if (td_read(td))
3d60d1ed
JA
381 return DDIR_READ;
382 else
383 return DDIR_WRITE;
384}
385
ebac4655
JA
386/*
387 * fill body of io_u->buf with random data and add a header with the
22f78b32 388 * crc32 or md5 sum of that data.
ebac4655
JA
389 */
390static void populate_io_u(struct thread_data *td, struct io_u *io_u)
391{
392 unsigned char *p = (unsigned char *) io_u->buf;
393 struct verify_header hdr;
394
395 hdr.fio_magic = FIO_HDR_MAGIC;
396 hdr.len = io_u->buflen;
397 p += sizeof(hdr);
398 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
399
400 if (td->verify == VERIFY_MD5) {
401 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
402 hdr.verify_type = VERIFY_MD5;
403 } else {
404 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
405 hdr.verify_type = VERIFY_CRC32;
406 }
407
408 memcpy(io_u->buf, &hdr, sizeof(hdr));
409}
410
aea47d44 411static int td_io_prep(struct thread_data *td, struct io_u *io_u)
20dc95c4 412{
20dc95c4
JA
413 if (td->io_prep && td->io_prep(td, io_u))
414 return 1;
415
416 return 0;
417}
418
b1ff3403 419void put_io_u(struct thread_data *td, struct io_u *io_u)
ebac4655
JA
420{
421 list_del(&io_u->list);
422 list_add(&io_u->list, &td->io_u_freelist);
423 td->cur_depth--;
424}
425
843a7413
JA
426static int fill_io_u(struct thread_data *td, struct io_u *io_u)
427{
428 /*
429 * If using an iolog, grab next piece if any available.
430 */
431 if (td->read_iolog)
432 return read_iolog_get(td, io_u);
433
aea47d44
JA
434 /*
435 * No log, let the seq/rand engine retrieve the next position.
436 */
437 if (!get_next_offset(td, &io_u->offset)) {
438 io_u->buflen = get_next_buflen(td);
439
440 if (io_u->buflen) {
441 io_u->ddir = get_rw_ddir(td);
843a7413
JA
442
443 /*
444 * If using a write iolog, store this entry.
445 */
446 if (td->write_iolog)
447 write_iolog_put(td, io_u);
448
aea47d44
JA
449 return 0;
450 }
451 }
452
453 return 1;
454}
455
22f78b32 456#define queue_full(td) list_empty(&(td)->io_u_freelist)
ebac4655 457
b1ff3403 458struct io_u *__get_io_u(struct thread_data *td)
ebac4655 459{
22f78b32 460 struct io_u *io_u = NULL;
ebac4655 461
22f78b32
JA
462 if (!queue_full(td)) {
463 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
464
465 io_u->error = 0;
466 io_u->resid = 0;
467 list_del(&io_u->list);
468 list_add(&io_u->list, &td->io_u_busylist);
469 td->cur_depth++;
470 }
ebac4655 471
ebac4655
JA
472 return io_u;
473}
474
ebac4655
JA
475static struct io_u *get_io_u(struct thread_data *td)
476{
477 struct io_u *io_u;
478
479 io_u = __get_io_u(td);
480 if (!io_u)
481 return NULL;
482
20dc95c4
JA
483 if (td->zone_bytes >= td->zone_size) {
484 td->zone_bytes = 0;
485 td->last_pos += td->zone_skip;
486 }
487
aea47d44 488 if (fill_io_u(td, io_u)) {
ebac4655
JA
489 put_io_u(td, io_u);
490 return NULL;
491 }
492
838a3cd3
JA
493 if (io_u->buflen + io_u->offset > td->real_file_size)
494 io_u->buflen = td->real_file_size - io_u->offset;
ebac4655
JA
495
496 if (!io_u->buflen) {
497 put_io_u(td, io_u);
498 return NULL;
499 }
500
843a7413 501 if (!td->read_iolog && !td->sequential)
ebac4655
JA
502 mark_random_map(td, io_u);
503
20dc95c4 504 td->last_pos += io_u->buflen;
ebac4655
JA
505
506 if (td->verify != VERIFY_NONE)
507 populate_io_u(td, io_u);
508
aea47d44 509 if (td_io_prep(td, io_u)) {
ebac4655
JA
510 put_io_u(td, io_u);
511 return NULL;
512 }
513
514 gettimeofday(&io_u->start_time, NULL);
515 return io_u;
516}
517
518static inline void td_set_runstate(struct thread_data *td, int runstate)
519{
ebac4655
JA
520 td->runstate = runstate;
521}
522
aea47d44 523static int get_next_verify(struct thread_data *td, struct io_u *io_u)
ebac4655
JA
524{
525 struct io_piece *ipo;
526
22f78b32
JA
527 if (!list_empty(&td->io_hist_list)) {
528 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
ebac4655 529
22f78b32 530 list_del(&ipo->list);
ebac4655 531
22f78b32
JA
532 io_u->offset = ipo->offset;
533 io_u->buflen = ipo->len;
534 io_u->ddir = DDIR_READ;
535 free(ipo);
536 return 0;
537 }
538
539 return 1;
ebac4655
JA
540}
541
ebac4655
JA
542static int sync_td(struct thread_data *td)
543{
544 if (td->io_sync)
545 return td->io_sync(td);
546
547 return 0;
548}
549
550static int io_u_getevents(struct thread_data *td, int min, int max,
551 struct timespec *t)
552{
553 return td->io_getevents(td, min, max, t);
554}
555
556static int io_u_queue(struct thread_data *td, struct io_u *io_u)
557{
558 gettimeofday(&io_u->issue_time, NULL);
559
560 return td->io_queue(td, io_u);
561}
562
563#define iocb_time(iocb) ((unsigned long) (iocb)->data)
564
565static void io_completed(struct thread_data *td, struct io_u *io_u,
566 struct io_completion_data *icd)
567{
568 struct timeval e;
569 unsigned long msec;
570
571 gettimeofday(&e, NULL);
572
573 if (!io_u->error) {
20dc95c4 574 unsigned int bytes = io_u->buflen - io_u->resid;
3d60d1ed 575 const int idx = io_u->ddir;
ebac4655
JA
576
577 td->io_blocks[idx]++;
20dc95c4
JA
578 td->io_bytes[idx] += bytes;
579 td->zone_bytes += bytes;
580 td->this_io_bytes[idx] += bytes;
ebac4655
JA
581
582 msec = mtime_since(&io_u->issue_time, &e);
583
3d60d1ed
JA
584 add_clat_sample(td, idx, msec);
585 add_bw_sample(td, idx);
ebac4655 586
3d60d1ed 587 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
ebac4655
JA
588 log_io_piece(td, io_u);
589
20dc95c4 590 icd->bytes_done[idx] += bytes;
ebac4655
JA
591 } else
592 icd->error = io_u->error;
593}
594
595static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
596{
597 struct io_u *io_u;
598 int i;
599
600 icd->error = 0;
601 icd->bytes_done[0] = icd->bytes_done[1] = 0;
602
603 for (i = 0; i < icd->nr; i++) {
604 io_u = td->io_event(td, i);
605
606 io_completed(td, io_u, icd);
607 put_io_u(td, io_u);
608 }
609}
610
611static void cleanup_pending_aio(struct thread_data *td)
612{
613 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
614 struct list_head *entry, *n;
615 struct io_completion_data icd;
616 struct io_u *io_u;
617 int r;
618
619 /*
620 * get immediately available events, if any
621 */
622 r = io_u_getevents(td, 0, td->cur_depth, &ts);
623 if (r > 0) {
624 icd.nr = r;
625 ios_completed(td, &icd);
626 }
627
628 /*
629 * now cancel remaining active events
630 */
631 if (td->io_cancel) {
632 list_for_each_safe(entry, n, &td->io_u_busylist) {
633 io_u = list_entry(entry, struct io_u, list);
634
635 r = td->io_cancel(td, io_u);
636 if (!r)
637 put_io_u(td, io_u);
638 }
639 }
640
641 if (td->cur_depth) {
642 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
643 if (r > 0) {
644 icd.nr = r;
645 ios_completed(td, &icd);
646 }
647 }
648}
649
650static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
651{
652 struct io_u *v_io_u = *io_u;
653 int ret = 0;
654
655 if (v_io_u) {
656 ret = verify_io_u(v_io_u);
657 put_io_u(td, v_io_u);
658 *io_u = NULL;
659 }
660
661 return ret;
662}
663
664static void do_verify(struct thread_data *td)
665{
666 struct timeval t;
667 struct io_u *io_u, *v_io_u = NULL;
668 struct io_completion_data icd;
669 int ret;
670
671 td_set_runstate(td, TD_VERIFYING);
672
673 do {
674 if (td->terminate)
675 break;
676
677 gettimeofday(&t, NULL);
678 if (runtime_exceeded(td, &t))
679 break;
680
681 io_u = __get_io_u(td);
682 if (!io_u)
683 break;
684
aea47d44 685 if (get_next_verify(td, io_u)) {
ebac4655
JA
686 put_io_u(td, io_u);
687 break;
688 }
689
aea47d44 690 if (td_io_prep(td, io_u)) {
ebac4655
JA
691 put_io_u(td, io_u);
692 break;
693 }
694
695 ret = io_u_queue(td, io_u);
696 if (ret) {
697 put_io_u(td, io_u);
698 td_verror(td, ret);
699 break;
700 }
701
702 /*
703 * we have one pending to verify, do that while
704 * we are doing io on the next one
705 */
706 if (do_io_u_verify(td, &v_io_u))
707 break;
708
709 ret = io_u_getevents(td, 1, 1, NULL);
710 if (ret != 1) {
711 if (ret < 0)
712 td_verror(td, ret);
713 break;
714 }
715
716 v_io_u = td->io_event(td, 0);
717 icd.nr = 1;
718 icd.error = 0;
719 io_completed(td, v_io_u, &icd);
720
721 if (icd.error) {
722 td_verror(td, icd.error);
723 put_io_u(td, v_io_u);
724 v_io_u = NULL;
725 break;
726 }
727
ebac4655
JA
728 /*
729 * if we can't submit more io, we need to verify now
730 */
731 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
732 break;
733
734 } while (1);
735
736 do_io_u_verify(td, &v_io_u);
737
738 if (td->cur_depth)
739 cleanup_pending_aio(td);
740
741 td_set_runstate(td, TD_RUNNING);
742}
743
32cd46a0
JA
744/*
745 * Main IO worker functions. It retrieves io_u's to process and queues
746 * and reaps them, checking for rate and errors along the way.
747 */
ebac4655
JA
748static void do_io(struct thread_data *td)
749{
750 struct io_completion_data icd;
751 struct timeval s, e;
752 unsigned long usec;
753
5853e5a8
JA
754 td_set_runstate(td, TD_RUNNING);
755
ebac4655
JA
756 while (td->this_io_bytes[td->ddir] < td->io_size) {
757 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
758 struct timespec *timeout;
759 int ret, min_evts = 0;
760 struct io_u *io_u;
761
762 if (td->terminate)
763 break;
764
765 io_u = get_io_u(td);
766 if (!io_u)
767 break;
768
769 memcpy(&s, &io_u->start_time, sizeof(s));
770
771 ret = io_u_queue(td, io_u);
772 if (ret) {
773 put_io_u(td, io_u);
774 td_verror(td, ret);
775 break;
776 }
777
778 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
779
780 if (td->cur_depth < td->iodepth) {
781 timeout = &ts;
782 min_evts = 0;
783 } else {
784 timeout = NULL;
785 min_evts = 1;
786 }
787
788 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
789 if (ret < 0) {
790 td_verror(td, ret);
791 break;
792 } else if (!ret)
793 continue;
794
795 icd.nr = ret;
796 ios_completed(td, &icd);
797 if (icd.error) {
798 td_verror(td, icd.error);
799 break;
800 }
801
802 /*
803 * the rate is batched for now, it should work for batches
804 * of completions except the very first one which may look
805 * a little bursty
806 */
807 gettimeofday(&e, NULL);
808 usec = utime_since(&s, &e);
809
810 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
811
812 if (check_min_rate(td, &e)) {
813 td_verror(td, ENOMEM);
814 break;
815 }
816
817 if (runtime_exceeded(td, &e))
818 break;
819
820 if (td->thinktime)
821 usec_sleep(td, td->thinktime);
822
823 if (should_fsync(td) && td->fsync_blocks &&
824 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
825 sync_td(td);
826 }
827
828 if (td->cur_depth)
829 cleanup_pending_aio(td);
830
5853e5a8
JA
831 if (should_fsync(td) && td->end_fsync) {
832 td_set_runstate(td, TD_FSYNCING);
ebac4655 833 sync_td(td);
5853e5a8 834 }
ebac4655
JA
835}
836
837static void cleanup_io(struct thread_data *td)
838{
839 if (td->io_cleanup)
840 td->io_cleanup(td);
841}
842
843static int init_io(struct thread_data *td)
844{
845 if (td->io_engine == FIO_SYNCIO)
846 return fio_syncio_init(td);
847 else if (td->io_engine == FIO_MMAPIO)
848 return fio_mmapio_init(td);
849 else if (td->io_engine == FIO_LIBAIO)
850 return fio_libaio_init(td);
851 else if (td->io_engine == FIO_POSIXAIO)
852 return fio_posixaio_init(td);
853 else if (td->io_engine == FIO_SGIO)
854 return fio_sgio_init(td);
8756e4d4
JA
855 else if (td->io_engine == FIO_SPLICEIO)
856 return fio_spliceio_init(td);
ebac4655 857 else {
3b70d7e5 858 log_err("bad io_engine %d\n", td->io_engine);
ebac4655
JA
859 return 1;
860 }
861}
862
863static void cleanup_io_u(struct thread_data *td)
864{
865 struct list_head *entry, *n;
866 struct io_u *io_u;
867
868 list_for_each_safe(entry, n, &td->io_u_freelist) {
869 io_u = list_entry(entry, struct io_u, list);
870
871 list_del(&io_u->list);
872 free(io_u);
873 }
874
875 if (td->mem_type == MEM_MALLOC)
876 free(td->orig_buffer);
877 else if (td->mem_type == MEM_SHM) {
878 struct shmid_ds sbuf;
879
880 shmdt(td->orig_buffer);
881 shmctl(td->shm_id, IPC_RMID, &sbuf);
882 } else if (td->mem_type == MEM_MMAP)
883 munmap(td->orig_buffer, td->orig_buffer_size);
884 else
3b70d7e5 885 log_err("Bad memory type %d\n", td->mem_type);
ebac4655
JA
886
887 td->orig_buffer = NULL;
888}
889
890static int init_io_u(struct thread_data *td)
891{
892 struct io_u *io_u;
893 int i, max_units;
894 char *p;
895
896 if (td->io_engine & FIO_SYNCIO)
897 max_units = 1;
898 else
899 max_units = td->iodepth;
900
901 td->orig_buffer_size = td->max_bs * max_units + MASK;
902
903 if (td->mem_type == MEM_MALLOC)
904 td->orig_buffer = malloc(td->orig_buffer_size);
905 else if (td->mem_type == MEM_SHM) {
906 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
907 if (td->shm_id < 0) {
908 td_verror(td, errno);
909 perror("shmget");
910 return 1;
911 }
912
913 td->orig_buffer = shmat(td->shm_id, NULL, 0);
914 if (td->orig_buffer == (void *) -1) {
915 td_verror(td, errno);
916 perror("shmat");
917 td->orig_buffer = NULL;
918 return 1;
919 }
920 } else if (td->mem_type == MEM_MMAP) {
921 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
922 if (td->orig_buffer == MAP_FAILED) {
923 td_verror(td, errno);
924 perror("mmap");
925 td->orig_buffer = NULL;
926 return 1;
927 }
928 }
929
ebac4655
JA
930 p = ALIGN(td->orig_buffer);
931 for (i = 0; i < max_units; i++) {
932 io_u = malloc(sizeof(*io_u));
933 memset(io_u, 0, sizeof(*io_u));
934 INIT_LIST_HEAD(&io_u->list);
935
936 io_u->buf = p + td->max_bs * i;
b1ff3403 937 io_u->index = i;
ebac4655
JA
938 list_add(&io_u->list, &td->io_u_freelist);
939 }
940
941 return 0;
942}
943
944static int create_file(struct thread_data *td, unsigned long long size,
945 int extend)
946{
947 unsigned long long left;
948 unsigned int bs;
949 int r, oflags;
950 char *b;
951
952 /*
953 * unless specifically asked for overwrite, let normal io extend it
954 */
c20d25b4
JA
955 if (td_write(td) && !td->overwrite) {
956 td->real_file_size = size;
ebac4655 957 return 0;
c20d25b4 958 }
ebac4655
JA
959
960 if (!size) {
3b70d7e5 961 log_err("Need size for create\n");
ebac4655
JA
962 td_verror(td, EINVAL);
963 return 1;
964 }
965
88c6ed80
JA
966 temp_stall_ts = 1;
967
ebac4655
JA
968 if (!extend) {
969 oflags = O_CREAT | O_TRUNC;
eb8bbf48 970 fprintf(f_out, "%s: Laying out IO file (%LuMiB)\n", td->name, size >> 20);
ebac4655
JA
971 }
972
973 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
974 if (td->fd < 0) {
975 td_verror(td, errno);
c20d25b4 976 goto done_noclose;
ebac4655
JA
977 }
978
979 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
980 td_verror(td, errno);
c20d25b4 981 goto done;
ebac4655
JA
982 }
983
984 td->io_size = td->file_size;
985 b = malloc(td->max_bs);
986 memset(b, 0, td->max_bs);
987
988 left = size;
989 while (left && !td->terminate) {
990 bs = td->max_bs;
991 if (bs > left)
992 bs = left;
993
994 r = write(td->fd, b, bs);
995
996 if (r == (int) bs) {
997 left -= bs;
998 continue;
999 } else {
1000 if (r < 0)
1001 td_verror(td, errno);
1002 else
1003 td_verror(td, EIO);
1004
1005 break;
1006 }
1007 }
1008
1009 if (td->terminate)
1010 unlink(td->file_name);
1011 else if (td->create_fsync)
1012 fsync(td->fd);
1013
c20d25b4
JA
1014 free(b);
1015done:
ebac4655
JA
1016 close(td->fd);
1017 td->fd = -1;
c20d25b4
JA
1018done_noclose:
1019 temp_stall_ts = 0;
ebac4655
JA
1020 return 0;
1021}
1022
1023static int file_size(struct thread_data *td)
1024{
1025 struct stat st;
1026
c20d25b4
JA
1027 if (td->overwrite) {
1028 if (fstat(td->fd, &st) == -1) {
1029 td_verror(td, errno);
1030 return 1;
1031 }
ebac4655 1032
c20d25b4 1033 td->real_file_size = st.st_size;
838a3cd3 1034
c20d25b4
JA
1035 if (!td->file_size || td->file_size > td->real_file_size)
1036 td->file_size = td->real_file_size;
1037 }
ebac4655 1038
e8e387c1 1039 td->file_size -= td->file_offset;
ebac4655
JA
1040 return 0;
1041}
1042
1043static int bdev_size(struct thread_data *td)
1044{
9104f874 1045 unsigned long long bytes;
ebac4655
JA
1046 int r;
1047
1048 r = blockdev_size(td->fd, &bytes);
1049 if (r) {
1050 td_verror(td, r);
1051 return 1;
1052 }
1053
838a3cd3
JA
1054 td->real_file_size = bytes;
1055
ebac4655
JA
1056 /*
1057 * no extend possibilities, so limit size to device size if too large
1058 */
838a3cd3
JA
1059 if (!td->file_size || td->file_size > td->real_file_size)
1060 td->file_size = td->real_file_size;
ebac4655 1061
e8e387c1 1062 td->file_size -= td->file_offset;
ebac4655
JA
1063 return 0;
1064}
1065
1066static int get_file_size(struct thread_data *td)
1067{
0af7b542 1068 int ret = 0;
ebac4655
JA
1069
1070 if (td->filetype == FIO_TYPE_FILE)
1071 ret = file_size(td);
0af7b542 1072 else if (td->filetype == FIO_TYPE_BD)
ebac4655 1073 ret = bdev_size(td);
0af7b542
JA
1074 else
1075 td->real_file_size = -1;
ebac4655
JA
1076
1077 if (ret)
1078 return ret;
1079
e8e387c1 1080 if (td->file_offset > td->real_file_size) {
3b70d7e5 1081 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size);
ebac4655
JA
1082 return 1;
1083 }
1084
838a3cd3 1085 td->io_size = td->file_size;
ebac4655 1086 if (td->io_size == 0) {
3b70d7e5 1087 log_err("%s: no io blocks\n", td->name);
ebac4655
JA
1088 td_verror(td, EINVAL);
1089 return 1;
1090 }
1091
20dc95c4
JA
1092 if (!td->zone_size)
1093 td->zone_size = td->io_size;
1094
ebac4655
JA
1095 td->total_io_size = td->io_size * td->loops;
1096 return 0;
1097}
1098
1099static int setup_file_mmap(struct thread_data *td)
1100{
1101 int flags;
1102
3d60d1ed
JA
1103 if (td_rw(td))
1104 flags = PROT_READ | PROT_WRITE;
1105 else if (td_write(td)) {
ebac4655
JA
1106 flags = PROT_WRITE;
1107
1108 if (td->verify != VERIFY_NONE)
1109 flags |= PROT_READ;
3d60d1ed
JA
1110 } else
1111 flags = PROT_READ;
ebac4655
JA
1112
1113 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1114 if (td->mmap == MAP_FAILED) {
1115 td->mmap = NULL;
1116 td_verror(td, errno);
1117 return 1;
1118 }
1119
1120 if (td->invalidate_cache) {
1121 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1122 td_verror(td, errno);
1123 return 1;
1124 }
1125 }
1126
1127 if (td->sequential) {
1128 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1129 td_verror(td, errno);
1130 return 1;
1131 }
1132 } else {
1133 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1134 td_verror(td, errno);
1135 return 1;
1136 }
1137 }
1138
1139 return 0;
1140}
1141
1142static int setup_file_plain(struct thread_data *td)
1143{
1144 if (td->invalidate_cache) {
1145 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1146 td_verror(td, errno);
1147 return 1;
1148 }
1149 }
1150
1151 if (td->sequential) {
1152 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1153 td_verror(td, errno);
1154 return 1;
1155 }
1156 } else {
1157 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1158 td_verror(td, errno);
1159 return 1;
1160 }
1161 }
1162
1163 return 0;
1164}
1165
1166static int setup_file(struct thread_data *td)
1167{
1168 struct stat st;
1169 int flags = 0;
1170
1171 if (stat(td->file_name, &st) == -1) {
1172 if (errno != ENOENT) {
1173 td_verror(td, errno);
1174 return 1;
1175 }
1176 if (!td->create_file) {
1177 td_verror(td, ENOENT);
1178 return 1;
1179 }
1180 if (create_file(td, td->file_size, 0))
1181 return 1;
1182 } else if (td->filetype == FIO_TYPE_FILE) {
6a0106a0 1183 if (st.st_size < (off_t) td->file_size) {
a71e13bb 1184 if (create_file(td, td->file_size, 1))
ebac4655
JA
1185 return 1;
1186 }
1187 }
1188
1189 if (td->odirect)
2c0ecd28 1190 flags |= OS_O_DIRECT;
ebac4655 1191
3d60d1ed 1192 if (td_write(td) || td_rw(td)) {
ebac4655
JA
1193 if (td->filetype == FIO_TYPE_FILE) {
1194 if (!td->overwrite)
1195 flags |= O_TRUNC;
1196
1197 flags |= O_CREAT;
1198 }
1199 if (td->sync_io)
1200 flags |= O_SYNC;
1201
1202 flags |= O_RDWR;
1203
1204 td->fd = open(td->file_name, flags, 0600);
3d60d1ed
JA
1205 } else {
1206 if (td->filetype == FIO_TYPE_CHAR)
1207 flags |= O_RDWR;
1208 else
1209 flags |= O_RDONLY;
1210
1211 td->fd = open(td->file_name, flags);
ebac4655
JA
1212 }
1213
1214 if (td->fd == -1) {
1215 td_verror(td, errno);
1216 return 1;
1217 }
1218
1219 if (get_file_size(td))
1220 return 1;
1221
1222 if (td->io_engine != FIO_MMAPIO)
1223 return setup_file_plain(td);
1224 else
1225 return setup_file_mmap(td);
1226}
1227
da86774e
JA
1228static int switch_ioscheduler(struct thread_data *td)
1229{
1230 char tmp[256], tmp2[128];
1231 FILE *f;
1232 int ret;
1233
1234 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1235
1236 f = fopen(tmp, "r+");
1237 if (!f) {
1238 td_verror(td, errno);
1239 return 1;
1240 }
1241
1242 /*
1243 * Set io scheduler.
1244 */
1245 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1246 if (ferror(f) || ret != 1) {
1247 td_verror(td, errno);
1248 fclose(f);
1249 return 1;
1250 }
1251
1252 rewind(f);
1253
1254 /*
1255 * Read back and check that the selected scheduler is now the default.
1256 */
1257 ret = fread(tmp, 1, sizeof(tmp), f);
1258 if (ferror(f) || ret < 0) {
1259 td_verror(td, errno);
1260 fclose(f);
1261 return 1;
1262 }
1263
1264 sprintf(tmp2, "[%s]", td->ioscheduler);
1265 if (!strstr(tmp, tmp2)) {
3b70d7e5 1266 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
da86774e
JA
1267 td_verror(td, EINVAL);
1268 fclose(f);
1269 return 1;
1270 }
1271
1272 fclose(f);
1273 return 0;
1274}
1275
ebac4655
JA
1276static void clear_io_state(struct thread_data *td)
1277{
1278 if (td->io_engine == FIO_SYNCIO)
1279 lseek(td->fd, SEEK_SET, 0);
1280
20dc95c4 1281 td->last_pos = 0;
ebac4655
JA
1282 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1283 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
20dc95c4 1284 td->zone_bytes = 0;
ebac4655
JA
1285
1286 if (td->file_map)
1287 memset(td->file_map, 0, td->num_maps * sizeof(long));
1288}
1289
ebac4655
JA
1290static void *thread_main(void *data)
1291{
1292 struct thread_data *td = data;
ebac4655
JA
1293
1294 if (!td->use_thread)
1295 setsid();
1296
1297 td->pid = getpid();
1298
aea47d44
JA
1299 INIT_LIST_HEAD(&td->io_u_freelist);
1300 INIT_LIST_HEAD(&td->io_u_busylist);
1301 INIT_LIST_HEAD(&td->io_hist_list);
1302 INIT_LIST_HEAD(&td->io_log_list);
1303
ebac4655
JA
1304 if (init_io_u(td))
1305 goto err;
1306
1307 if (fio_setaffinity(td) == -1) {
1308 td_verror(td, errno);
1309 goto err;
1310 }
1311
1312 if (init_io(td))
1313 goto err;
1314
aea47d44
JA
1315 if (init_iolog(td))
1316 goto err;
1317
ebac4655
JA
1318 if (td->ioprio) {
1319 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1320 td_verror(td, errno);
1321 goto err;
1322 }
1323 }
1324
b6f4d880
JA
1325 if (nice(td->nice) < 0) {
1326 td_verror(td, errno);
1327 goto err;
1328 }
1329
75154845
JA
1330 if (init_random_state(td))
1331 goto err;
1332
da86774e
JA
1333 if (td->ioscheduler && switch_ioscheduler(td))
1334 goto err;
1335
75154845 1336 td_set_runstate(td, TD_INITIALIZED);
bbfd6b00
JA
1337 fio_sem_up(&startup_sem);
1338 fio_sem_down(&td->mutex);
ebac4655
JA
1339
1340 if (!td->create_serialize && setup_file(td))
1341 goto err;
1342
ebac4655
JA
1343 gettimeofday(&td->epoch, NULL);
1344
4e0ba8af
JA
1345 if (td->exec_prerun)
1346 system(td->exec_prerun);
1347
ebac4655
JA
1348 while (td->loops--) {
1349 getrusage(RUSAGE_SELF, &td->ru_start);
1350 gettimeofday(&td->start, NULL);
1351 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1352
1353 if (td->ratemin)
1354 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1355
1356 clear_io_state(td);
1357 prune_io_piece_log(td);
1358
1359 do_io(td);
1360
1361 td->runtime[td->ddir] += mtime_since_now(&td->start);
aea47d44 1362 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
3d60d1ed
JA
1363 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1364
ebac4655
JA
1365 update_rusage_stat(td);
1366
1367 if (td->error || td->terminate)
1368 break;
1369
1370 if (td->verify == VERIFY_NONE)
1371 continue;
1372
1373 clear_io_state(td);
1374 gettimeofday(&td->start, NULL);
1375
1376 do_verify(td);
1377
1378 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1379
1380 if (td->error || td->terminate)
1381 break;
1382 }
1383
ebac4655
JA
1384 if (td->bw_log)
1385 finish_log(td, td->bw_log, "bw");
1386 if (td->slat_log)
1387 finish_log(td, td->slat_log, "slat");
1388 if (td->clat_log)
1389 finish_log(td, td->clat_log, "clat");
843a7413
JA
1390 if (td->write_iolog)
1391 write_iolog_close(td);
4e0ba8af
JA
1392 if (td->exec_postrun)
1393 system(td->exec_postrun);
ebac4655
JA
1394
1395 if (exitall_on_terminate)
1396 terminate_threads(td->groupid);
1397
1398err:
1399 if (td->fd != -1) {
1400 close(td->fd);
1401 td->fd = -1;
1402 }
1403 if (td->mmap)
1404 munmap(td->mmap, td->file_size);
1405 cleanup_io(td);
1406 cleanup_io_u(td);
ebac4655
JA
1407 td_set_runstate(td, TD_EXITED);
1408 return NULL;
1409
1410}
1411
1412static void *fork_main(int shmid, int offset)
1413{
1414 struct thread_data *td;
1415 void *data;
1416
1417 data = shmat(shmid, NULL, 0);
1418 if (data == (void *) -1) {
1419 perror("shmat");
1420 return NULL;
1421 }
1422
1423 td = data + offset * sizeof(struct thread_data);
1424 thread_main(td);
1425 shmdt(data);
1426 return NULL;
1427}
1428
ebac4655
JA
1429static void check_str_update(struct thread_data *td)
1430{
1431 char c = run_str[td->thread_number - 1];
1432
ebac4655
JA
1433 switch (td->runstate) {
1434 case TD_REAPED:
1435 c = '_';
1436 break;
1437 case TD_EXITED:
1438 c = 'E';
1439 break;
1440 case TD_RUNNING:
3d60d1ed
JA
1441 if (td_rw(td)) {
1442 if (td->sequential)
1443 c = 'M';
1444 else
1445 c = 'm';
1446 } else if (td_read(td)) {
ebac4655
JA
1447 if (td->sequential)
1448 c = 'R';
1449 else
1450 c = 'r';
1451 } else {
1452 if (td->sequential)
1453 c = 'W';
1454 else
1455 c = 'w';
1456 }
1457 break;
1458 case TD_VERIFYING:
1459 c = 'V';
1460 break;
5853e5a8
JA
1461 case TD_FSYNCING:
1462 c = 'F';
1463 break;
ebac4655
JA
1464 case TD_CREATED:
1465 c = 'C';
1466 break;
75154845
JA
1467 case TD_INITIALIZED:
1468 c = 'I';
1469 break;
ebac4655
JA
1470 case TD_NOT_CREATED:
1471 c = 'P';
1472 break;
1473 default:
3b70d7e5 1474 log_err("state %d\n", td->runstate);
ebac4655
JA
1475 }
1476
1477 run_str[td->thread_number - 1] = c;
ebac4655
JA
1478}
1479
5289b847
JA
1480static void eta_to_str(char *str, int eta_sec)
1481{
1482 unsigned int d, h, m, s;
1483 static int always_d, always_h;
1484
1485 d = h = m = s = 0;
1486
1487 s = eta_sec % 60;
1488 eta_sec /= 60;
1489 m = eta_sec % 60;
1490 eta_sec /= 60;
1491 h = eta_sec % 24;
1492 eta_sec /= 24;
1493 d = eta_sec;
1494
1495 if (d || always_d) {
1496 always_d = 1;
1497 str += sprintf(str, "%02dd:", d);
1498 }
1499 if (h || always_h) {
1500 always_h = 1;
1501 str += sprintf(str, "%02dh:", h);
1502 }
1503
1504 str += sprintf(str, "%02dm:", m);
1505 str += sprintf(str, "%02ds", s);
1506}
1507
6a0106a0
JA
1508static int thread_eta(struct thread_data *td, unsigned long elapsed)
1509{
1510 unsigned long long bytes_total, bytes_done;
1511 unsigned int eta_sec = 0;
1512
1513 bytes_total = td->total_io_size;
3d60d1ed
JA
1514
1515 /*
1516 * if writing, bytes_total will be twice the size. If mixing,
1517 * assume a 50/50 split and thus bytes_total will be 50% larger.
1518 */
1519 if (td->verify) {
1520 if (td_rw(td))
1521 bytes_total = bytes_total * 3 / 2;
1522 else
1523 bytes_total <<= 1;
1524 }
6a0106a0
JA
1525 if (td->zone_size && td->zone_skip)
1526 bytes_total /= (td->zone_skip / td->zone_size);
1527
1528 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1529 double perc;
8b611c34 1530
6a0106a0
JA
1531 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1532 perc = (double) bytes_done / (double) bytes_total;
1533 if (perc > 1.0)
1534 perc = 1.0;
1535
1536 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1537
8b611c34 1538 if (td->timeout && eta_sec > (td->timeout - elapsed))
6a0106a0 1539 eta_sec = td->timeout - elapsed;
75154845
JA
1540 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1541 || td->runstate == TD_INITIALIZED) {
6a0106a0
JA
1542 int t_eta = 0, r_eta = 0;
1543
1544 /*
1545 * We can only guess - assume it'll run the full timeout
1546 * if given, otherwise assume it'll run at the specified rate.
1547 */
1548 if (td->timeout)
1549 t_eta = td->timeout + td->start_delay - elapsed;
1550 if (td->rate) {
1551 r_eta = (bytes_total / 1024) / td->rate;
1552 r_eta += td->start_delay - elapsed;
1553 }
1554
1555 if (r_eta && t_eta)
1556 eta_sec = min(r_eta, t_eta);
1557 else if (r_eta)
1558 eta_sec = r_eta;
1559 else if (t_eta)
1560 eta_sec = t_eta;
1561 else
972cfd25 1562 eta_sec = 0;
6a0106a0
JA
1563 } else {
1564 /*
5853e5a8 1565 * thread is already done or waiting for fsync
6a0106a0
JA
1566 */
1567 eta_sec = 0;
1568 }
1569
1570 return eta_sec;
1571}
1572
ebac4655
JA
1573static void print_thread_status(void)
1574{
6a0106a0 1575 unsigned long elapsed = time_since_now(&genesis);
71a751ce 1576 int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec;
5289b847 1577 char eta_str[32];
6a0106a0
JA
1578 double perc = 0.0;
1579
c6ae0a5b 1580 if (temp_stall_ts || terse_output)
88c6ed80
JA
1581 return;
1582
6a0106a0
JA
1583 eta_secs = malloc(thread_number * sizeof(int));
1584 memset(eta_secs, 0, thread_number * sizeof(int));
ebac4655 1585
71a751ce 1586 nr_pending = nr_running = t_rate = m_rate = 0;
ebac4655
JA
1587 for (i = 0; i < thread_number; i++) {
1588 struct thread_data *td = &threads[i];
1589
5853e5a8
JA
1590 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING||
1591 td->runstate == TD_FSYNCING) {
ebac4655
JA
1592 nr_running++;
1593 t_rate += td->rate;
1594 m_rate += td->ratemin;
71a751ce
JA
1595 } else if (td->runstate < TD_RUNNING)
1596 nr_pending++;
ebac4655 1597
6a0106a0
JA
1598 if (elapsed >= 3)
1599 eta_secs[i] = thread_eta(td, elapsed);
8b611c34
JA
1600 else
1601 eta_secs[i] = INT_MAX;
ebac4655
JA
1602
1603 check_str_update(td);
1604 }
1605
6a0106a0
JA
1606 if (exitall_on_terminate)
1607 eta_sec = INT_MAX;
1608 else
1609 eta_sec = 0;
5289b847 1610
6a0106a0
JA
1611 for (i = 0; i < thread_number; i++) {
1612 if (exitall_on_terminate) {
1613 if (eta_secs[i] < eta_sec)
1614 eta_sec = eta_secs[i];
1615 } else {
1616 if (eta_secs[i] > eta_sec)
1617 eta_sec = eta_secs[i];
5289b847 1618 }
6a0106a0 1619 }
5289b847 1620
8b611c34 1621 if (eta_sec != INT_MAX && elapsed) {
6a0106a0
JA
1622 perc = (double) elapsed / (double) (elapsed + eta_sec);
1623 eta_to_str(eta_str, eta_sec);
ebac4655
JA
1624 }
1625
71a751ce 1626 if (!nr_running && !nr_pending)
f44c1d46
JA
1627 return;
1628
eb8bbf48 1629 printf("Threads running: %d", nr_running);
ebac4655
JA
1630 if (m_rate || t_rate)
1631 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
8b611c34 1632 if (eta_sec != INT_MAX) {
6a0106a0
JA
1633 perc *= 100.0;
1634 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1635 }
5289b847 1636 printf("\r");
ebac4655 1637 fflush(stdout);
6a0106a0 1638 free(eta_secs);
ebac4655
JA
1639}
1640
1641static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1642{
1643 int i;
1644
1645 /*
1646 * reap exited threads (TD_EXITED -> TD_REAPED)
1647 */
1648 for (i = 0; i < thread_number; i++) {
1649 struct thread_data *td = &threads[i];
1650
1651 if (td->runstate != TD_EXITED)
1652 continue;
1653
1654 td_set_runstate(td, TD_REAPED);
1655
1656 if (td->use_thread) {
1657 long ret;
1658
1659 if (pthread_join(td->thread, (void *) &ret))
1660 perror("thread_join");
1661 } else
1662 waitpid(td->pid, NULL, 0);
1663
1664 (*nr_running)--;
1665 (*m_rate) -= td->ratemin;
1666 (*t_rate) -= td->rate;
1667 }
1668}
1669
fcb6ade2
JA
1670static void fio_unpin_memory(void *pinned)
1671{
1672 if (pinned) {
1673 if (munlock(pinned, mlock_size) < 0)
1674 perror("munlock");
1675 munmap(pinned, mlock_size);
1676 }
1677}
1678
1679static void *fio_pin_memory(void)
1680{
32cd46a0 1681 unsigned long long phys_mem;
fcb6ade2
JA
1682 void *ptr;
1683
1684 if (!mlock_size)
1685 return NULL;
1686
1687 /*
1688 * Don't allow mlock of more than real_mem-128MB
1689 */
32cd46a0
JA
1690 phys_mem = os_phys_mem();
1691 if (phys_mem) {
1692 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1693 mlock_size = phys_mem - 128 * 1024 * 1024;
eb8bbf48 1694 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
fcb6ade2
JA
1695 }
1696 }
1697
1698 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1699 if (!ptr) {
1700 perror("malloc locked mem");
1701 return NULL;
1702 }
1703 if (mlock(ptr, mlock_size) < 0) {
1704 munmap(ptr, mlock_size);
1705 perror("mlock");
1706 return NULL;
1707 }
1708
1709 return ptr;
1710}
1711
ebac4655
JA
1712static void run_threads(void)
1713{
ebac4655
JA
1714 struct thread_data *td;
1715 unsigned long spent;
1716 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2
JA
1717 void *mlocked_mem;
1718
1719 mlocked_mem = fio_pin_memory();
ebac4655 1720
c6ae0a5b
JA
1721 if (!terse_output) {
1722 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1723 fflush(stdout);
1724 }
c04f7ec3 1725
4efa970e
JA
1726 signal(SIGINT, sig_handler);
1727 signal(SIGALRM, sig_handler);
1728
ebac4655
JA
1729 todo = thread_number;
1730 nr_running = 0;
1731 nr_started = 0;
1732 m_rate = t_rate = 0;
1733
1734 for (i = 0; i < thread_number; i++) {
1735 td = &threads[i];
1736
1737 run_str[td->thread_number - 1] = 'P';
1738
1739 init_disk_util(td);
1740
1741 if (!td->create_serialize)
1742 continue;
1743
1744 /*
1745 * do file setup here so it happens sequentially,
1746 * we don't want X number of threads getting their
1747 * client data interspersed on disk
1748 */
1749 if (setup_file(td)) {
1750 td_set_runstate(td, TD_REAPED);
1751 todo--;
1752 }
1753 }
1754
1755 gettimeofday(&genesis, NULL);
1756
1757 while (todo) {
75154845
JA
1758 struct thread_data *map[MAX_JOBS];
1759 struct timeval this_start;
1760 int this_jobs = 0, left;
1761
ebac4655
JA
1762 /*
1763 * create threads (TD_NOT_CREATED -> TD_CREATED)
1764 */
1765 for (i = 0; i < thread_number; i++) {
1766 td = &threads[i];
1767
1768 if (td->runstate != TD_NOT_CREATED)
1769 continue;
1770
1771 /*
1772 * never got a chance to start, killed by other
1773 * thread for some reason
1774 */
1775 if (td->terminate) {
1776 todo--;
1777 continue;
1778 }
1779
1780 if (td->start_delay) {
1781 spent = mtime_since_now(&genesis);
1782
1783 if (td->start_delay * 1000 > spent)
1784 continue;
1785 }
1786
1787 if (td->stonewall && (nr_started || nr_running))
1788 break;
1789
75154845
JA
1790 /*
1791 * Set state to created. Thread will transition
1792 * to TD_INITIALIZED when it's done setting up.
1793 */
ebac4655 1794 td_set_runstate(td, TD_CREATED);
75154845 1795 map[this_jobs++] = td;
bbfd6b00 1796 fio_sem_init(&startup_sem, 1);
ebac4655
JA
1797 nr_started++;
1798
1799 if (td->use_thread) {
1800 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1801 perror("thread_create");
1802 nr_started--;
1803 }
1804 } else {
1805 if (fork())
bbfd6b00 1806 fio_sem_down(&startup_sem);
ebac4655
JA
1807 else {
1808 fork_main(shm_id, i);
1809 exit(0);
1810 }
1811 }
1812 }
1813
1814 /*
75154845
JA
1815 * Wait for the started threads to transition to
1816 * TD_INITIALIZED.
ebac4655 1817 */
75154845
JA
1818 gettimeofday(&this_start, NULL);
1819 left = this_jobs;
1820 while (left) {
1821 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1822 break;
1823
1824 usleep(100000);
1825
1826 for (i = 0; i < this_jobs; i++) {
1827 td = map[i];
1828 if (!td)
1829 continue;
b6f4d880 1830 if (td->runstate == TD_INITIALIZED) {
75154845
JA
1831 map[i] = NULL;
1832 left--;
b6f4d880
JA
1833 } else if (td->runstate >= TD_EXITED) {
1834 map[i] = NULL;
1835 left--;
1836 todo--;
1837 nr_running++; /* work-around... */
75154845
JA
1838 }
1839 }
1840 }
1841
1842 if (left) {
3b70d7e5 1843 log_err("fio: %d jobs failed to start\n", left);
75154845
JA
1844 for (i = 0; i < this_jobs; i++) {
1845 td = map[i];
1846 if (!td)
1847 continue;
1848 kill(td->pid, SIGTERM);
1849 }
1850 break;
1851 }
1852
1853 /*
b6f4d880 1854 * start created threads (TD_INITIALIZED -> TD_RUNNING).
75154845 1855 */
ebac4655
JA
1856 for (i = 0; i < thread_number; i++) {
1857 td = &threads[i];
1858
75154845 1859 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
1860 continue;
1861
1862 td_set_runstate(td, TD_RUNNING);
1863 nr_running++;
1864 nr_started--;
1865 m_rate += td->ratemin;
1866 t_rate += td->rate;
75154845 1867 todo--;
bbfd6b00 1868 fio_sem_up(&td->mutex);
ebac4655
JA
1869 }
1870
1871 reap_threads(&nr_running, &t_rate, &m_rate);
1872
1873 if (todo)
1874 usleep(100000);
1875 }
1876
1877 while (nr_running) {
1878 reap_threads(&nr_running, &t_rate, &m_rate);
1879 usleep(10000);
1880 }
1881
1882 update_io_ticks();
fcb6ade2 1883 fio_unpin_memory(mlocked_mem);
ebac4655
JA
1884}
1885
ebac4655
JA
1886int main(int argc, char *argv[])
1887{
1888 if (parse_options(argc, argv))
1889 return 1;
1890
1891 if (!thread_number) {
3b70d7e5 1892 log_err("Nothing to do\n");
ebac4655
JA
1893 return 1;
1894 }
1895
1896 disk_util_timer_arm();
1897
1898 run_threads();
1899 show_run_stats();
1900
1901 return 0;
1902}