2 * fio - the flexible io tester
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/ioctl.h>
40 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
43 int thread_number = 0;
44 static char run_str[MAX_JOBS + 1];
46 static struct timeval genesis;
47 static int temp_stall_ts;
49 static void print_thread_status(void);
51 extern unsigned long long mlock_size;
54 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
55 * will never back again. It may cycle between running/verififying/fsyncing.
56 * Once the thread reaches TD_EXITED, it is just waiting for the core to
70 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
72 static volatile int startup_sem;
74 #define TERMINATE_ALL (-1)
75 #define JOB_START_TIMEOUT (5 * 1000)
77 static void terminate_threads(int group_id)
81 for (i = 0; i < thread_number; i++) {
82 struct thread_data *td = &threads[i];
84 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
91 static void sig_handler(int sig)
96 disk_util_timer_arm();
97 print_thread_status();
100 printf("\nfio: terminating on signal\n");
102 terminate_threads(TERMINATE_ALL);
108 * The ->file_map[] contains a map of blocks we have or have not done io
109 * to yet. Used to make sure we cover the entire range in a fair fashion.
111 static int random_map_free(struct thread_data *td, unsigned long long block)
113 unsigned int idx = RAND_MAP_IDX(td, block);
114 unsigned int bit = RAND_MAP_BIT(td, block);
116 return (td->file_map[idx] & (1UL << bit)) == 0;
120 * Return the next free block in the map.
122 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
128 while ((*b) * td->min_bs < td->io_size) {
129 if (td->file_map[i] != -1UL) {
130 *b += ffz(td->file_map[i]);
134 *b += BLOCKS_PER_MAP;
142 * Mark a given offset as used in the map.
144 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
146 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
147 unsigned int blocks = 0;
149 while (blocks < (io_u->buflen / td->min_bs)) {
150 unsigned int idx, bit;
152 if (!random_map_free(td, block))
155 idx = RAND_MAP_IDX(td, block);
156 bit = RAND_MAP_BIT(td, block);
158 assert(idx < td->num_maps);
160 td->file_map[idx] |= (1UL << bit);
165 if ((blocks * td->min_bs) < io_u->buflen)
166 io_u->buflen = blocks * td->min_bs;
170 * For random io, generate a random new block and see if it's used. Repeat
171 * until we find a free one. For sequential io, just return the end of
172 * the last io issued.
174 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
176 unsigned long long b, rb;
179 if (!td->sequential) {
180 unsigned long long max_blocks = td->io_size / td->min_bs;
184 r = os_random_long(&td->random_state);
185 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
186 rb = b + (td->file_offset / td->min_bs);
188 } while (!random_map_free(td, rb) && loops);
191 if (get_next_free_block(td, &b))
195 b = td->last_pos / td->min_bs;
197 *offset = (b * td->min_bs) + td->file_offset;
198 if (*offset > td->real_file_size)
204 static unsigned int get_next_buflen(struct thread_data *td)
209 if (td->min_bs == td->max_bs)
212 r = os_random_long(&td->bsrange_state);
213 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
214 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
217 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
218 buflen = td->io_size - td->this_io_bytes[td->ddir];
224 * Check if we are above the minimum rate given.
226 static int check_min_rate(struct thread_data *td, struct timeval *now)
233 * allow a 2 second settle period in the beginning
235 if (mtime_since(&td->start, now) < 2000)
239 * if rate blocks is set, sample is running
241 if (td->rate_bytes) {
242 spent = mtime_since(&td->lastrate, now);
243 if (spent < td->ratecycle)
246 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
247 if (rate < td->ratemin) {
248 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
250 terminate_threads(td->groupid);
255 td->rate_bytes = td->this_io_bytes[ddir];
256 memcpy(&td->lastrate, now, sizeof(*now));
260 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
264 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
270 static void fill_random_bytes(struct thread_data *td,
271 unsigned char *p, unsigned int len)
277 r = os_random_double(&td->verify_state);
280 * lrand48_r seems to be broken and only fill the bottom
281 * 32-bits, even on 64-bit archs with 64-bit longs
294 static void hexdump(void *buffer, int len)
296 unsigned char *p = buffer;
299 for (i = 0; i < len; i++)
300 fprintf(f_out, "%02x", p[i]);
301 fprintf(f_out, "\n");
304 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
306 unsigned char *p = (unsigned char *) io_u->buf;
310 c = crc32(p, hdr->len - sizeof(*hdr));
312 if (c != hdr->crc32) {
313 log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
314 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
321 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
323 unsigned char *p = (unsigned char *) io_u->buf;
324 struct md5_ctx md5_ctx;
326 memset(&md5_ctx, 0, sizeof(md5_ctx));
328 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
330 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
331 log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
332 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
333 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
340 static int verify_io_u(struct io_u *io_u)
342 struct verify_header *hdr = (struct verify_header *) io_u->buf;
345 if (hdr->fio_magic != FIO_HDR_MAGIC)
348 if (hdr->verify_type == VERIFY_MD5)
349 ret = verify_io_u_md5(hdr, io_u);
350 else if (hdr->verify_type == VERIFY_CRC32)
351 ret = verify_io_u_crc32(hdr, io_u);
353 log_err("Bad verify type %d\n", hdr->verify_type);
360 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
362 hdr->crc32 = crc32(p, len);
365 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
367 struct md5_ctx md5_ctx;
369 memset(&md5_ctx, 0, sizeof(md5_ctx));
370 md5_update(&md5_ctx, p, len);
371 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
375 * Return the data direction for the next io_u. If the job is a
376 * mixed read/write workload, check the rwmix cycle and switch if
379 static int get_rw_ddir(struct thread_data *td)
383 unsigned long elapsed;
385 gettimeofday(&now, NULL);
386 elapsed = mtime_since_now(&td->rwmix_switch);
389 * Check if it's time to seed a new data direction.
391 if (elapsed >= td->rwmixcycle) {
395 r = os_random_long(&td->rwmix_state);
396 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
397 if (v < td->rwmixread)
398 td->rwmix_ddir = DDIR_READ;
400 td->rwmix_ddir = DDIR_WRITE;
401 memcpy(&td->rwmix_switch, &now, sizeof(now));
403 return td->rwmix_ddir;
404 } else if (td_read(td))
411 * fill body of io_u->buf with random data and add a header with the
412 * crc32 or md5 sum of that data.
414 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
416 unsigned char *p = (unsigned char *) io_u->buf;
417 struct verify_header hdr;
419 hdr.fio_magic = FIO_HDR_MAGIC;
420 hdr.len = io_u->buflen;
422 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
424 if (td->verify == VERIFY_MD5) {
425 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
426 hdr.verify_type = VERIFY_MD5;
428 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
429 hdr.verify_type = VERIFY_CRC32;
432 memcpy(io_u->buf, &hdr, sizeof(hdr));
435 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
437 if (td->io_prep && td->io_prep(td, io_u))
443 void put_io_u(struct thread_data *td, struct io_u *io_u)
445 list_del(&io_u->list);
446 list_add(&io_u->list, &td->io_u_freelist);
450 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
453 * If using an iolog, grab next piece if any available.
456 return read_iolog_get(td, io_u);
459 * No log, let the seq/rand engine retrieve the next position.
461 if (!get_next_offset(td, &io_u->offset)) {
462 io_u->buflen = get_next_buflen(td);
465 io_u->ddir = get_rw_ddir(td);
468 * If using a write iolog, store this entry.
471 write_iolog_put(td, io_u);
480 #define queue_full(td) list_empty(&(td)->io_u_freelist)
482 struct io_u *__get_io_u(struct thread_data *td)
484 struct io_u *io_u = NULL;
486 if (!queue_full(td)) {
487 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
491 list_del(&io_u->list);
492 list_add(&io_u->list, &td->io_u_busylist);
500 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
501 * etc. The returned io_u is fully ready to be prepped and submitted.
503 static struct io_u *get_io_u(struct thread_data *td)
507 io_u = __get_io_u(td);
511 if (td->zone_bytes >= td->zone_size) {
513 td->last_pos += td->zone_skip;
516 if (fill_io_u(td, io_u)) {
521 if (io_u->buflen + io_u->offset > td->real_file_size)
522 io_u->buflen = td->real_file_size - io_u->offset;
529 if (!td->read_iolog && !td->sequential)
530 mark_random_map(td, io_u);
532 td->last_pos += io_u->buflen;
534 if (td->verify != VERIFY_NONE)
535 populate_io_u(td, io_u);
537 if (td_io_prep(td, io_u)) {
542 gettimeofday(&io_u->start_time, NULL);
546 static inline void td_set_runstate(struct thread_data *td, int runstate)
548 td->runstate = runstate;
551 static int get_next_verify(struct thread_data *td, struct io_u *io_u)
553 struct io_piece *ipo;
555 if (!list_empty(&td->io_hist_list)) {
556 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
558 list_del(&ipo->list);
560 io_u->offset = ipo->offset;
561 io_u->buflen = ipo->len;
562 io_u->ddir = DDIR_READ;
570 static int sync_td(struct thread_data *td)
573 return td->io_sync(td);
578 static int io_u_getevents(struct thread_data *td, int min, int max,
581 return td->io_getevents(td, min, max, t);
584 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
586 gettimeofday(&io_u->issue_time, NULL);
588 return td->io_queue(td, io_u);
591 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
593 static void io_completed(struct thread_data *td, struct io_u *io_u,
594 struct io_completion_data *icd)
599 gettimeofday(&e, NULL);
602 unsigned int bytes = io_u->buflen - io_u->resid;
603 const int idx = io_u->ddir;
605 td->io_blocks[idx]++;
606 td->io_bytes[idx] += bytes;
607 td->zone_bytes += bytes;
608 td->this_io_bytes[idx] += bytes;
610 msec = mtime_since(&io_u->issue_time, &e);
612 add_clat_sample(td, idx, msec);
613 add_bw_sample(td, idx);
615 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
616 log_io_piece(td, io_u);
618 icd->bytes_done[idx] += bytes;
620 icd->error = io_u->error;
623 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
629 icd->bytes_done[0] = icd->bytes_done[1] = 0;
631 for (i = 0; i < icd->nr; i++) {
632 io_u = td->io_event(td, i);
634 io_completed(td, io_u, icd);
640 * When job exits, we can cancel the in-flight IO if we are using async
641 * io. Attempt to do so.
643 static void cleanup_pending_aio(struct thread_data *td)
645 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
646 struct list_head *entry, *n;
647 struct io_completion_data icd;
652 * get immediately available events, if any
654 r = io_u_getevents(td, 0, td->cur_depth, &ts);
657 ios_completed(td, &icd);
661 * now cancel remaining active events
664 list_for_each_safe(entry, n, &td->io_u_busylist) {
665 io_u = list_entry(entry, struct io_u, list);
667 r = td->io_cancel(td, io_u);
674 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
677 ios_completed(td, &icd);
682 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
684 struct io_u *v_io_u = *io_u;
688 ret = verify_io_u(v_io_u);
689 put_io_u(td, v_io_u);
697 * The main verify engine. Runs over the writes we previusly submitted,
698 * reads the blocks back in, and checks the crc/md5 of the data.
700 static void do_verify(struct thread_data *td)
703 struct io_u *io_u, *v_io_u = NULL;
704 struct io_completion_data icd;
707 td_set_runstate(td, TD_VERIFYING);
713 gettimeofday(&t, NULL);
714 if (runtime_exceeded(td, &t))
717 io_u = __get_io_u(td);
721 if (get_next_verify(td, io_u)) {
726 if (td_io_prep(td, io_u)) {
731 ret = io_u_queue(td, io_u);
739 * we have one pending to verify, do that while
740 * we are doing io on the next one
742 if (do_io_u_verify(td, &v_io_u))
745 ret = io_u_getevents(td, 1, 1, NULL);
752 v_io_u = td->io_event(td, 0);
755 io_completed(td, v_io_u, &icd);
758 td_verror(td, icd.error);
759 put_io_u(td, v_io_u);
765 * if we can't submit more io, we need to verify now
767 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
772 do_io_u_verify(td, &v_io_u);
775 cleanup_pending_aio(td);
777 td_set_runstate(td, TD_RUNNING);
781 * Not really an io thread, all it does is burn CPU cycles in the specified
784 static void do_cpuio(struct thread_data *td)
787 int split = 100 / td->cpuload;
790 while (!td->terminate) {
791 gettimeofday(&e, NULL);
793 if (runtime_exceeded(td, &e))
799 usec_sleep(td, 10000);
806 * Main IO worker function. It retrieves io_u's to process and queues
807 * and reaps them, checking for rate and errors along the way.
809 static void do_io(struct thread_data *td)
811 struct io_completion_data icd;
815 td_set_runstate(td, TD_RUNNING);
817 while (td->this_io_bytes[td->ddir] < td->io_size) {
818 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
819 struct timespec *timeout;
820 int ret, min_evts = 0;
830 memcpy(&s, &io_u->start_time, sizeof(s));
832 ret = io_u_queue(td, io_u);
839 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
841 if (td->cur_depth < td->iodepth) {
849 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
857 ios_completed(td, &icd);
859 td_verror(td, icd.error);
864 * the rate is batched for now, it should work for batches
865 * of completions except the very first one which may look
868 gettimeofday(&e, NULL);
869 usec = utime_since(&s, &e);
871 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
873 if (check_min_rate(td, &e)) {
874 td_verror(td, ENOMEM);
878 if (runtime_exceeded(td, &e))
882 usec_sleep(td, td->thinktime);
884 if (should_fsync(td) && td->fsync_blocks &&
885 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
890 cleanup_pending_aio(td);
892 if (should_fsync(td) && td->end_fsync) {
893 td_set_runstate(td, TD_FSYNCING);
898 static void cleanup_io(struct thread_data *td)
904 static int init_io(struct thread_data *td)
906 if (td->io_engine == FIO_SYNCIO)
907 return fio_syncio_init(td);
908 else if (td->io_engine == FIO_MMAPIO)
909 return fio_mmapio_init(td);
910 else if (td->io_engine == FIO_LIBAIO)
911 return fio_libaio_init(td);
912 else if (td->io_engine == FIO_POSIXAIO)
913 return fio_posixaio_init(td);
914 else if (td->io_engine == FIO_SGIO)
915 return fio_sgio_init(td);
916 else if (td->io_engine == FIO_SPLICEIO)
917 return fio_spliceio_init(td);
918 else if (td->io_engine == FIO_CPUIO)
919 return fio_cpuio_init(td);
921 log_err("bad io_engine %d\n", td->io_engine);
926 static void cleanup_io_u(struct thread_data *td)
928 struct list_head *entry, *n;
931 list_for_each_safe(entry, n, &td->io_u_freelist) {
932 io_u = list_entry(entry, struct io_u, list);
934 list_del(&io_u->list);
938 if (td->mem_type == MEM_MALLOC)
939 free(td->orig_buffer);
940 else if (td->mem_type == MEM_SHM) {
941 struct shmid_ds sbuf;
943 shmdt(td->orig_buffer);
944 shmctl(td->shm_id, IPC_RMID, &sbuf);
945 } else if (td->mem_type == MEM_MMAP)
946 munmap(td->orig_buffer, td->orig_buffer_size);
948 log_err("Bad memory type %d\n", td->mem_type);
950 td->orig_buffer = NULL;
953 static int init_io_u(struct thread_data *td)
959 if (td->io_engine == FIO_CPUIO)
962 if (td->io_engine & FIO_SYNCIO)
965 max_units = td->iodepth;
967 td->orig_buffer_size = td->max_bs * max_units + MASK;
969 if (td->mem_type == MEM_MALLOC)
970 td->orig_buffer = malloc(td->orig_buffer_size);
971 else if (td->mem_type == MEM_SHM) {
972 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
973 if (td->shm_id < 0) {
974 td_verror(td, errno);
979 td->orig_buffer = shmat(td->shm_id, NULL, 0);
980 if (td->orig_buffer == (void *) -1) {
981 td_verror(td, errno);
983 td->orig_buffer = NULL;
986 } else if (td->mem_type == MEM_MMAP) {
987 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
988 if (td->orig_buffer == MAP_FAILED) {
989 td_verror(td, errno);
991 td->orig_buffer = NULL;
996 p = ALIGN(td->orig_buffer);
997 for (i = 0; i < max_units; i++) {
998 io_u = malloc(sizeof(*io_u));
999 memset(io_u, 0, sizeof(*io_u));
1000 INIT_LIST_HEAD(&io_u->list);
1002 io_u->buf = p + td->max_bs * i;
1004 list_add(&io_u->list, &td->io_u_freelist);
1010 static int create_file(struct thread_data *td, unsigned long long size)
1012 unsigned long long left;
1018 * unless specifically asked for overwrite, let normal io extend it
1020 if (!td->overwrite) {
1021 td->real_file_size = size;
1026 log_err("Need size for create\n");
1027 td_verror(td, EINVAL);
1032 fprintf(f_out, "%s: Laying out IO file (%LuMiB)\n",td->name,size >> 20);
1034 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1036 td_verror(td, errno);
1040 if (ftruncate(td->fd, td->file_size) == -1) {
1041 td_verror(td, errno);
1045 td->io_size = td->file_size;
1046 b = malloc(td->max_bs);
1047 memset(b, 0, td->max_bs);
1050 while (left && !td->terminate) {
1055 r = write(td->fd, b, bs);
1057 if (r == (int) bs) {
1062 td_verror(td, errno);
1071 unlink(td->file_name);
1072 else if (td->create_fsync)
1084 static int file_size(struct thread_data *td)
1088 if (td->overwrite) {
1089 if (fstat(td->fd, &st) == -1) {
1090 td_verror(td, errno);
1094 td->real_file_size = st.st_size;
1096 if (!td->file_size || td->file_size > td->real_file_size)
1097 td->file_size = td->real_file_size;
1100 td->file_size -= td->file_offset;
1104 static int bdev_size(struct thread_data *td)
1106 unsigned long long bytes;
1109 r = blockdev_size(td->fd, &bytes);
1115 td->real_file_size = bytes;
1118 * no extend possibilities, so limit size to device size if too large
1120 if (!td->file_size || td->file_size > td->real_file_size)
1121 td->file_size = td->real_file_size;
1123 td->file_size -= td->file_offset;
1127 static int get_file_size(struct thread_data *td)
1131 if (td->filetype == FIO_TYPE_FILE)
1132 ret = file_size(td);
1133 else if (td->filetype == FIO_TYPE_BD)
1134 ret = bdev_size(td);
1136 td->real_file_size = -1;
1141 if (td->file_offset > td->real_file_size) {
1142 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size);
1146 td->io_size = td->file_size;
1147 if (td->io_size == 0) {
1148 log_err("%s: no io blocks\n", td->name);
1149 td_verror(td, EINVAL);
1154 td->zone_size = td->io_size;
1156 td->total_io_size = td->io_size * td->loops;
1160 static int setup_file_mmap(struct thread_data *td)
1165 flags = PROT_READ | PROT_WRITE;
1166 else if (td_write(td)) {
1169 if (td->verify != VERIFY_NONE)
1174 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1175 if (td->mmap == MAP_FAILED) {
1177 td_verror(td, errno);
1181 if (td->invalidate_cache) {
1182 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1183 td_verror(td, errno);
1188 if (td->sequential) {
1189 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1190 td_verror(td, errno);
1194 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1195 td_verror(td, errno);
1203 static int setup_file_plain(struct thread_data *td)
1205 if (td->invalidate_cache) {
1206 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1207 td_verror(td, errno);
1212 if (td->sequential) {
1213 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1214 td_verror(td, errno);
1218 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1219 td_verror(td, errno);
1227 static int setup_file(struct thread_data *td)
1232 if (td->io_engine == FIO_CPUIO)
1235 if (stat(td->file_name, &st) == -1) {
1236 if (errno != ENOENT) {
1237 td_verror(td, errno);
1240 if (!td->create_file) {
1241 td_verror(td, ENOENT);
1244 if (create_file(td, td->file_size))
1246 } else if (td->filetype == FIO_TYPE_FILE &&
1247 st.st_size < (off_t) td->file_size) {
1248 if (create_file(td, td->file_size))
1253 flags |= OS_O_DIRECT;
1255 if (td_write(td) || td_rw(td)) {
1256 if (td->filetype == FIO_TYPE_FILE) {
1267 td->fd = open(td->file_name, flags, 0600);
1269 if (td->filetype == FIO_TYPE_CHAR)
1274 td->fd = open(td->file_name, flags);
1278 td_verror(td, errno);
1282 if (get_file_size(td))
1285 if (td->io_engine != FIO_MMAPIO)
1286 return setup_file_plain(td);
1288 return setup_file_mmap(td);
1291 static int switch_ioscheduler(struct thread_data *td)
1293 char tmp[256], tmp2[128];
1297 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1299 f = fopen(tmp, "r+");
1301 td_verror(td, errno);
1308 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1309 if (ferror(f) || ret != 1) {
1310 td_verror(td, errno);
1318 * Read back and check that the selected scheduler is now the default.
1320 ret = fread(tmp, 1, sizeof(tmp), f);
1321 if (ferror(f) || ret < 0) {
1322 td_verror(td, errno);
1327 sprintf(tmp2, "[%s]", td->ioscheduler);
1328 if (!strstr(tmp, tmp2)) {
1329 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
1330 td_verror(td, EINVAL);
1339 static void clear_io_state(struct thread_data *td)
1341 if (td->io_engine == FIO_SYNCIO)
1342 lseek(td->fd, SEEK_SET, 0);
1345 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1346 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1350 memset(td->file_map, 0, td->num_maps * sizeof(long));
1354 * Entry point for the thread based jobs. The process based jobs end up
1355 * here as well, after a little setup.
1357 static void *thread_main(void *data)
1359 struct thread_data *td = data;
1361 if (!td->use_thread)
1366 INIT_LIST_HEAD(&td->io_u_freelist);
1367 INIT_LIST_HEAD(&td->io_u_busylist);
1368 INIT_LIST_HEAD(&td->io_hist_list);
1369 INIT_LIST_HEAD(&td->io_log_list);
1374 if (fio_setaffinity(td) == -1) {
1375 td_verror(td, errno);
1386 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1387 td_verror(td, errno);
1392 if (nice(td->nice) == -1) {
1393 td_verror(td, errno);
1397 if (init_random_state(td))
1400 if (td->ioscheduler && switch_ioscheduler(td))
1403 td_set_runstate(td, TD_INITIALIZED);
1404 fio_sem_up(&startup_sem);
1405 fio_sem_down(&td->mutex);
1407 if (!td->create_serialize && setup_file(td))
1410 gettimeofday(&td->epoch, NULL);
1412 if (td->exec_prerun)
1413 system(td->exec_prerun);
1415 while (td->loops--) {
1416 getrusage(RUSAGE_SELF, &td->ru_start);
1417 gettimeofday(&td->start, NULL);
1418 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1421 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1424 prune_io_piece_log(td);
1426 if (td->io_engine == FIO_CPUIO)
1431 td->runtime[td->ddir] += mtime_since_now(&td->start);
1432 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1433 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1435 update_rusage_stat(td);
1437 if (td->error || td->terminate)
1440 if (td->verify == VERIFY_NONE)
1444 gettimeofday(&td->start, NULL);
1448 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1450 if (td->error || td->terminate)
1455 finish_log(td, td->bw_log, "bw");
1457 finish_log(td, td->slat_log, "slat");
1459 finish_log(td, td->clat_log, "clat");
1460 if (td->write_iolog)
1461 write_iolog_close(td);
1462 if (td->exec_postrun)
1463 system(td->exec_postrun);
1465 if (exitall_on_terminate)
1466 terminate_threads(td->groupid);
1474 munmap(td->mmap, td->file_size);
1477 td_set_runstate(td, TD_EXITED);
1483 * We cannot pass the td data into a forked process, so attach the td and
1484 * pass it to the thread worker.
1486 static void *fork_main(int shmid, int offset)
1488 struct thread_data *td;
1491 data = shmat(shmid, NULL, 0);
1492 if (data == (void *) -1) {
1497 td = data + offset * sizeof(struct thread_data);
1504 * Sets the status of the 'td' in the printed status map.
1506 static void check_str_update(struct thread_data *td)
1508 char c = run_str[td->thread_number - 1];
1510 switch (td->runstate) {
1523 } else if (td_read(td)) {
1544 case TD_INITIALIZED:
1547 case TD_NOT_CREATED:
1551 log_err("state %d\n", td->runstate);
1554 run_str[td->thread_number - 1] = c;
1558 * Convert seconds to a printable string.
1560 static void eta_to_str(char *str, int eta_sec)
1562 unsigned int d, h, m, s;
1563 static int always_d, always_h;
1575 if (d || always_d) {
1577 str += sprintf(str, "%02dd:", d);
1579 if (h || always_h) {
1581 str += sprintf(str, "%02dh:", h);
1584 str += sprintf(str, "%02dm:", m);
1585 str += sprintf(str, "%02ds", s);
1589 * Best effort calculation of the estimated pending runtime of a job.
1591 static int thread_eta(struct thread_data *td, unsigned long elapsed)
1593 unsigned long long bytes_total, bytes_done;
1594 unsigned int eta_sec = 0;
1596 bytes_total = td->total_io_size;
1599 * if writing, bytes_total will be twice the size. If mixing,
1600 * assume a 50/50 split and thus bytes_total will be 50% larger.
1604 bytes_total = bytes_total * 3 / 2;
1608 if (td->zone_size && td->zone_skip)
1609 bytes_total /= (td->zone_skip / td->zone_size);
1611 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1614 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1615 perc = (double) bytes_done / (double) bytes_total;
1619 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1621 if (td->timeout && eta_sec > (td->timeout - elapsed))
1622 eta_sec = td->timeout - elapsed;
1623 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1624 || td->runstate == TD_INITIALIZED) {
1625 int t_eta = 0, r_eta = 0;
1628 * We can only guess - assume it'll run the full timeout
1629 * if given, otherwise assume it'll run at the specified rate.
1632 t_eta = td->timeout + td->start_delay - elapsed;
1634 r_eta = (bytes_total / 1024) / td->rate;
1635 r_eta += td->start_delay - elapsed;
1639 eta_sec = min(r_eta, t_eta);
1648 * thread is already done or waiting for fsync
1657 * Print status of the jobs we know about. This includes rate estimates,
1658 * ETA, thread state, etc.
1660 static void print_thread_status(void)
1662 unsigned long elapsed = time_since_now(&genesis);
1663 int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec;
1667 if (temp_stall_ts || terse_output)
1670 eta_secs = malloc(thread_number * sizeof(int));
1671 memset(eta_secs, 0, thread_number * sizeof(int));
1673 nr_pending = nr_running = t_rate = m_rate = 0;
1674 for (i = 0; i < thread_number; i++) {
1675 struct thread_data *td = &threads[i];
1677 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING||
1678 td->runstate == TD_FSYNCING) {
1681 m_rate += td->ratemin;
1682 } else if (td->runstate < TD_RUNNING)
1686 eta_secs[i] = thread_eta(td, elapsed);
1688 eta_secs[i] = INT_MAX;
1690 check_str_update(td);
1693 if (exitall_on_terminate)
1698 for (i = 0; i < thread_number; i++) {
1699 if (exitall_on_terminate) {
1700 if (eta_secs[i] < eta_sec)
1701 eta_sec = eta_secs[i];
1703 if (eta_secs[i] > eta_sec)
1704 eta_sec = eta_secs[i];
1708 if (eta_sec != INT_MAX && elapsed) {
1709 perc = (double) elapsed / (double) (elapsed + eta_sec);
1710 eta_to_str(eta_str, eta_sec);
1713 if (!nr_running && !nr_pending)
1716 printf("Threads running: %d", nr_running);
1717 if (m_rate || t_rate)
1718 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1719 if (eta_sec != INT_MAX && nr_running) {
1721 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1729 * Run over the job map and reap the threads that have exited, if any.
1731 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1736 * reap exited threads (TD_EXITED -> TD_REAPED)
1738 for (i = 0, cputhreads = 0; i < thread_number; i++) {
1739 struct thread_data *td = &threads[i];
1741 if (td->io_engine == FIO_CPUIO)
1744 if (td->runstate != TD_EXITED)
1747 td_set_runstate(td, TD_REAPED);
1749 if (td->use_thread) {
1752 if (pthread_join(td->thread, (void *) &ret))
1753 perror("thread_join");
1755 waitpid(td->pid, NULL, 0);
1758 (*m_rate) -= td->ratemin;
1759 (*t_rate) -= td->rate;
1762 if (*nr_running == cputhreads)
1763 terminate_threads(TERMINATE_ALL);
1766 static void fio_unpin_memory(void *pinned)
1769 if (munlock(pinned, mlock_size) < 0)
1771 munmap(pinned, mlock_size);
1775 static void *fio_pin_memory(void)
1777 unsigned long long phys_mem;
1784 * Don't allow mlock of more than real_mem-128MB
1786 phys_mem = os_phys_mem();
1788 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1789 mlock_size = phys_mem - 128 * 1024 * 1024;
1790 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
1794 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1796 perror("malloc locked mem");
1799 if (mlock(ptr, mlock_size) < 0) {
1800 munmap(ptr, mlock_size);
1809 * Main function for kicking off and reaping jobs, as needed.
1811 static void run_threads(void)
1813 struct thread_data *td;
1814 unsigned long spent;
1815 int i, todo, nr_running, m_rate, t_rate, nr_started;
1818 mlocked_mem = fio_pin_memory();
1820 if (!terse_output) {
1821 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1825 signal(SIGINT, sig_handler);
1826 signal(SIGALRM, sig_handler);
1828 todo = thread_number;
1831 m_rate = t_rate = 0;
1833 for (i = 0; i < thread_number; i++) {
1836 run_str[td->thread_number - 1] = 'P';
1840 if (!td->create_serialize)
1844 * do file setup here so it happens sequentially,
1845 * we don't want X number of threads getting their
1846 * client data interspersed on disk
1848 if (setup_file(td)) {
1849 td_set_runstate(td, TD_REAPED);
1854 gettimeofday(&genesis, NULL);
1857 struct thread_data *map[MAX_JOBS];
1858 struct timeval this_start;
1859 int this_jobs = 0, left;
1862 * create threads (TD_NOT_CREATED -> TD_CREATED)
1864 for (i = 0; i < thread_number; i++) {
1867 if (td->runstate != TD_NOT_CREATED)
1871 * never got a chance to start, killed by other
1872 * thread for some reason
1874 if (td->terminate) {
1879 if (td->start_delay) {
1880 spent = mtime_since_now(&genesis);
1882 if (td->start_delay * 1000 > spent)
1886 if (td->stonewall && (nr_started || nr_running))
1890 * Set state to created. Thread will transition
1891 * to TD_INITIALIZED when it's done setting up.
1893 td_set_runstate(td, TD_CREATED);
1894 map[this_jobs++] = td;
1895 fio_sem_init(&startup_sem, 1);
1898 if (td->use_thread) {
1899 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1900 perror("thread_create");
1905 fio_sem_down(&startup_sem);
1907 fork_main(shm_id, i);
1914 * Wait for the started threads to transition to
1917 gettimeofday(&this_start, NULL);
1920 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1925 for (i = 0; i < this_jobs; i++) {
1929 if (td->runstate == TD_INITIALIZED) {
1932 } else if (td->runstate >= TD_EXITED) {
1936 nr_running++; /* work-around... */
1942 log_err("fio: %d jobs failed to start\n", left);
1943 for (i = 0; i < this_jobs; i++) {
1947 kill(td->pid, SIGTERM);
1953 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1955 for (i = 0; i < thread_number; i++) {
1958 if (td->runstate != TD_INITIALIZED)
1961 td_set_runstate(td, TD_RUNNING);
1964 m_rate += td->ratemin;
1967 fio_sem_up(&td->mutex);
1970 reap_threads(&nr_running, &t_rate, &m_rate);
1976 while (nr_running) {
1977 reap_threads(&nr_running, &t_rate, &m_rate);
1982 fio_unpin_memory(mlocked_mem);
1985 int main(int argc, char *argv[])
1987 if (parse_options(argc, argv))
1990 if (!thread_number) {
1991 log_err("Nothing to do\n");
1995 disk_util_timer_arm();