2 * fio - the flexible io tester
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
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.
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.
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
31 #include <sys/ioctl.h>
39 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
42 int thread_number = 0;
43 static char run_str[MAX_JOBS + 1];
45 static struct timeval genesis;
46 static int temp_stall_ts;
48 static void print_thread_status(void);
50 extern unsigned long long mlock_size;
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
69 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
71 static volatile int startup_sem;
73 #define TERMINATE_ALL (-1)
74 #define JOB_START_TIMEOUT (5 * 1000)
76 static void terminate_threads(int group_id)
80 for (i = 0; i < thread_number; i++) {
81 struct thread_data *td = &threads[i];
83 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
90 static void sig_handler(int sig)
95 disk_util_timer_arm();
96 print_thread_status();
99 printf("\nfio: terminating on signal\n");
101 terminate_threads(TERMINATE_ALL);
107 * The ->file_map[] contains a map of blocks we have or have not done io
108 * to yet. Used to make sure we cover the entire range in a fair fashion.
110 static int random_map_free(struct thread_data *td, unsigned long long block)
112 unsigned int idx = RAND_MAP_IDX(td, block);
113 unsigned int bit = RAND_MAP_BIT(td, block);
115 return (td->file_map[idx] & (1UL << bit)) == 0;
119 * Return the next free block in the map.
121 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
127 while ((*b) * td->min_bs < td->io_size) {
128 if (td->file_map[i] != -1UL) {
129 *b += ffz(td->file_map[i]);
133 *b += BLOCKS_PER_MAP;
141 * Mark a given offset as used in the map.
143 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
145 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
146 unsigned int blocks = 0;
148 while (blocks < (io_u->buflen / td->min_bs)) {
149 unsigned int idx, bit;
151 if (!random_map_free(td, block))
154 idx = RAND_MAP_IDX(td, block);
155 bit = RAND_MAP_BIT(td, block);
157 assert(idx < td->num_maps);
159 td->file_map[idx] |= (1UL << bit);
164 if ((blocks * td->min_bs) < io_u->buflen)
165 io_u->buflen = blocks * td->min_bs;
169 * For random io, generate a random new block and see if it's used. Repeat
170 * until we find a free one. For sequential io, just return the end of
171 * the last io issued.
173 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
175 unsigned long long b, rb;
178 if (!td->sequential) {
179 unsigned long long max_blocks = td->io_size / td->min_bs;
183 r = os_random_long(&td->random_state);
184 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
185 rb = b + (td->file_offset / td->min_bs);
187 } while (!random_map_free(td, rb) && loops);
190 if (get_next_free_block(td, &b))
194 b = td->last_pos / td->min_bs;
196 *offset = (b * td->min_bs) + td->file_offset;
197 if (*offset > td->real_file_size)
203 static unsigned int get_next_buflen(struct thread_data *td)
208 if (td->min_bs == td->max_bs)
211 r = os_random_long(&td->bsrange_state);
212 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
213 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
216 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
217 buflen = td->io_size - td->this_io_bytes[td->ddir];
223 * Check if we are above the minimum rate given.
225 static int check_min_rate(struct thread_data *td, struct timeval *now)
232 * allow a 2 second settle period in the beginning
234 if (mtime_since(&td->start, now) < 2000)
238 * if rate blocks is set, sample is running
240 if (td->rate_bytes) {
241 spent = mtime_since(&td->lastrate, now);
242 if (spent < td->ratecycle)
245 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
246 if (rate < td->ratemin) {
247 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
249 terminate_threads(td->groupid);
254 td->rate_bytes = td->this_io_bytes[ddir];
255 memcpy(&td->lastrate, now, sizeof(*now));
259 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
263 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
269 static void fill_random_bytes(struct thread_data *td,
270 unsigned char *p, unsigned int len)
276 r = os_random_double(&td->verify_state);
279 * lrand48_r seems to be broken and only fill the bottom
280 * 32-bits, even on 64-bit archs with 64-bit longs
293 static void hexdump(void *buffer, int len)
295 unsigned char *p = buffer;
298 for (i = 0; i < len; i++)
299 fprintf(f_out, "%02x", p[i]);
300 fprintf(f_out, "\n");
303 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
305 unsigned char *p = (unsigned char *) io_u->buf;
309 c = crc32(p, hdr->len - sizeof(*hdr));
311 if (c != hdr->crc32) {
312 log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
313 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
320 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
322 unsigned char *p = (unsigned char *) io_u->buf;
323 struct md5_ctx md5_ctx;
325 memset(&md5_ctx, 0, sizeof(md5_ctx));
327 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
329 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
330 log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
331 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
332 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
339 static int verify_io_u(struct io_u *io_u)
341 struct verify_header *hdr = (struct verify_header *) io_u->buf;
344 if (hdr->fio_magic != FIO_HDR_MAGIC)
347 if (hdr->verify_type == VERIFY_MD5)
348 ret = verify_io_u_md5(hdr, io_u);
349 else if (hdr->verify_type == VERIFY_CRC32)
350 ret = verify_io_u_crc32(hdr, io_u);
352 log_err("Bad verify type %d\n", hdr->verify_type);
359 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
361 hdr->crc32 = crc32(p, len);
364 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
366 struct md5_ctx md5_ctx;
368 memset(&md5_ctx, 0, sizeof(md5_ctx));
369 md5_update(&md5_ctx, p, len);
370 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
374 * Return the data direction for the next io_u. If the job is a
375 * mixed read/write workload, check the rwmix cycle and switch if
378 static int get_rw_ddir(struct thread_data *td)
382 unsigned long elapsed;
384 gettimeofday(&now, NULL);
385 elapsed = mtime_since_now(&td->rwmix_switch);
388 * Check if it's time to seed a new data direction.
390 if (elapsed >= td->rwmixcycle) {
394 r = os_random_long(&td->rwmix_state);
395 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
396 if (v < td->rwmixread)
397 td->rwmix_ddir = DDIR_READ;
399 td->rwmix_ddir = DDIR_WRITE;
400 memcpy(&td->rwmix_switch, &now, sizeof(now));
402 return td->rwmix_ddir;
403 } else if (td_read(td))
410 * fill body of io_u->buf with random data and add a header with the
411 * crc32 or md5 sum of that data.
413 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
415 unsigned char *p = (unsigned char *) io_u->buf;
416 struct verify_header hdr;
418 hdr.fio_magic = FIO_HDR_MAGIC;
419 hdr.len = io_u->buflen;
421 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
423 if (td->verify == VERIFY_MD5) {
424 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
425 hdr.verify_type = VERIFY_MD5;
427 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
428 hdr.verify_type = VERIFY_CRC32;
431 memcpy(io_u->buf, &hdr, sizeof(hdr));
434 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
436 if (td->io_prep && td->io_prep(td, io_u))
442 void put_io_u(struct thread_data *td, struct io_u *io_u)
444 list_del(&io_u->list);
445 list_add(&io_u->list, &td->io_u_freelist);
449 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
452 * If using an iolog, grab next piece if any available.
455 return read_iolog_get(td, io_u);
458 * No log, let the seq/rand engine retrieve the next position.
460 if (!get_next_offset(td, &io_u->offset)) {
461 io_u->buflen = get_next_buflen(td);
464 io_u->ddir = get_rw_ddir(td);
467 * If using a write iolog, store this entry.
470 write_iolog_put(td, io_u);
479 #define queue_full(td) list_empty(&(td)->io_u_freelist)
481 struct io_u *__get_io_u(struct thread_data *td)
483 struct io_u *io_u = NULL;
485 if (!queue_full(td)) {
486 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
490 list_del(&io_u->list);
491 list_add(&io_u->list, &td->io_u_busylist);
499 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
500 * etc. The returned io_u is fully ready to be prepped and submitted.
502 static struct io_u *get_io_u(struct thread_data *td)
506 io_u = __get_io_u(td);
510 if (td->zone_bytes >= td->zone_size) {
512 td->last_pos += td->zone_skip;
515 if (fill_io_u(td, io_u)) {
520 if (io_u->buflen + io_u->offset > td->real_file_size)
521 io_u->buflen = td->real_file_size - io_u->offset;
528 if (!td->read_iolog && !td->sequential)
529 mark_random_map(td, io_u);
531 td->last_pos += io_u->buflen;
533 if (td->verify != VERIFY_NONE)
534 populate_io_u(td, io_u);
536 if (td_io_prep(td, io_u)) {
541 gettimeofday(&io_u->start_time, NULL);
545 static inline void td_set_runstate(struct thread_data *td, int runstate)
547 td->runstate = runstate;
550 static int get_next_verify(struct thread_data *td, struct io_u *io_u)
552 struct io_piece *ipo;
554 if (!list_empty(&td->io_hist_list)) {
555 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
557 list_del(&ipo->list);
559 io_u->offset = ipo->offset;
560 io_u->buflen = ipo->len;
561 io_u->ddir = DDIR_READ;
569 static int sync_td(struct thread_data *td)
572 return td->io_sync(td);
577 static int io_u_getevents(struct thread_data *td, int min, int max,
580 return td->io_getevents(td, min, max, t);
583 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
585 gettimeofday(&io_u->issue_time, NULL);
587 return td->io_queue(td, io_u);
590 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
592 static void io_completed(struct thread_data *td, struct io_u *io_u,
593 struct io_completion_data *icd)
598 gettimeofday(&e, NULL);
601 unsigned int bytes = io_u->buflen - io_u->resid;
602 const int idx = io_u->ddir;
604 td->io_blocks[idx]++;
605 td->io_bytes[idx] += bytes;
606 td->zone_bytes += bytes;
607 td->this_io_bytes[idx] += bytes;
609 msec = mtime_since(&io_u->issue_time, &e);
611 add_clat_sample(td, idx, msec);
612 add_bw_sample(td, idx);
614 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
615 log_io_piece(td, io_u);
617 icd->bytes_done[idx] += bytes;
619 icd->error = io_u->error;
622 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
628 icd->bytes_done[0] = icd->bytes_done[1] = 0;
630 for (i = 0; i < icd->nr; i++) {
631 io_u = td->io_event(td, i);
633 io_completed(td, io_u, icd);
639 * When job exits, we can cancel the in-flight IO if we are using async
640 * io. Attempt to do so.
642 static void cleanup_pending_aio(struct thread_data *td)
644 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
645 struct list_head *entry, *n;
646 struct io_completion_data icd;
651 * get immediately available events, if any
653 r = io_u_getevents(td, 0, td->cur_depth, &ts);
656 ios_completed(td, &icd);
660 * now cancel remaining active events
663 list_for_each_safe(entry, n, &td->io_u_busylist) {
664 io_u = list_entry(entry, struct io_u, list);
666 r = td->io_cancel(td, io_u);
673 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
676 ios_completed(td, &icd);
681 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
683 struct io_u *v_io_u = *io_u;
687 ret = verify_io_u(v_io_u);
688 put_io_u(td, v_io_u);
696 * The main verify engine. Runs over the writes we previusly submitted,
697 * reads the blocks back in, and checks the crc/md5 of the data.
699 static void do_verify(struct thread_data *td)
702 struct io_u *io_u, *v_io_u = NULL;
703 struct io_completion_data icd;
706 td_set_runstate(td, TD_VERIFYING);
712 gettimeofday(&t, NULL);
713 if (runtime_exceeded(td, &t))
716 io_u = __get_io_u(td);
720 if (get_next_verify(td, io_u)) {
725 if (td_io_prep(td, io_u)) {
730 ret = io_u_queue(td, io_u);
738 * we have one pending to verify, do that while
739 * we are doing io on the next one
741 if (do_io_u_verify(td, &v_io_u))
744 ret = io_u_getevents(td, 1, 1, NULL);
751 v_io_u = td->io_event(td, 0);
754 io_completed(td, v_io_u, &icd);
757 td_verror(td, icd.error);
758 put_io_u(td, v_io_u);
764 * if we can't submit more io, we need to verify now
766 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
771 do_io_u_verify(td, &v_io_u);
774 cleanup_pending_aio(td);
776 td_set_runstate(td, TD_RUNNING);
780 * Main IO worker function. It retrieves io_u's to process and queues
781 * and reaps them, checking for rate and errors along the way.
783 static void do_io(struct thread_data *td)
785 struct io_completion_data icd;
789 td_set_runstate(td, TD_RUNNING);
791 while (td->this_io_bytes[td->ddir] < td->io_size) {
792 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
793 struct timespec *timeout;
794 int ret, min_evts = 0;
804 memcpy(&s, &io_u->start_time, sizeof(s));
806 ret = io_u_queue(td, io_u);
813 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
815 if (td->cur_depth < td->iodepth) {
823 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
831 ios_completed(td, &icd);
833 td_verror(td, icd.error);
838 * the rate is batched for now, it should work for batches
839 * of completions except the very first one which may look
842 gettimeofday(&e, NULL);
843 usec = utime_since(&s, &e);
845 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
847 if (check_min_rate(td, &e)) {
848 td_verror(td, ENOMEM);
852 if (runtime_exceeded(td, &e))
856 usec_sleep(td, td->thinktime);
858 if (should_fsync(td) && td->fsync_blocks &&
859 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
864 cleanup_pending_aio(td);
866 if (should_fsync(td) && td->end_fsync) {
867 td_set_runstate(td, TD_FSYNCING);
872 static void cleanup_io(struct thread_data *td)
878 static int init_io(struct thread_data *td)
880 if (td->io_engine == FIO_SYNCIO)
881 return fio_syncio_init(td);
882 else if (td->io_engine == FIO_MMAPIO)
883 return fio_mmapio_init(td);
884 else if (td->io_engine == FIO_LIBAIO)
885 return fio_libaio_init(td);
886 else if (td->io_engine == FIO_POSIXAIO)
887 return fio_posixaio_init(td);
888 else if (td->io_engine == FIO_SGIO)
889 return fio_sgio_init(td);
890 else if (td->io_engine == FIO_SPLICEIO)
891 return fio_spliceio_init(td);
893 log_err("bad io_engine %d\n", td->io_engine);
898 static void cleanup_io_u(struct thread_data *td)
900 struct list_head *entry, *n;
903 list_for_each_safe(entry, n, &td->io_u_freelist) {
904 io_u = list_entry(entry, struct io_u, list);
906 list_del(&io_u->list);
910 if (td->mem_type == MEM_MALLOC)
911 free(td->orig_buffer);
912 else if (td->mem_type == MEM_SHM) {
913 struct shmid_ds sbuf;
915 shmdt(td->orig_buffer);
916 shmctl(td->shm_id, IPC_RMID, &sbuf);
917 } else if (td->mem_type == MEM_MMAP)
918 munmap(td->orig_buffer, td->orig_buffer_size);
920 log_err("Bad memory type %d\n", td->mem_type);
922 td->orig_buffer = NULL;
925 static int init_io_u(struct thread_data *td)
931 if (td->io_engine & FIO_SYNCIO)
934 max_units = td->iodepth;
936 td->orig_buffer_size = td->max_bs * max_units + MASK;
938 if (td->mem_type == MEM_MALLOC)
939 td->orig_buffer = malloc(td->orig_buffer_size);
940 else if (td->mem_type == MEM_SHM) {
941 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
942 if (td->shm_id < 0) {
943 td_verror(td, errno);
948 td->orig_buffer = shmat(td->shm_id, NULL, 0);
949 if (td->orig_buffer == (void *) -1) {
950 td_verror(td, errno);
952 td->orig_buffer = NULL;
955 } else if (td->mem_type == MEM_MMAP) {
956 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
957 if (td->orig_buffer == MAP_FAILED) {
958 td_verror(td, errno);
960 td->orig_buffer = NULL;
965 p = ALIGN(td->orig_buffer);
966 for (i = 0; i < max_units; i++) {
967 io_u = malloc(sizeof(*io_u));
968 memset(io_u, 0, sizeof(*io_u));
969 INIT_LIST_HEAD(&io_u->list);
971 io_u->buf = p + td->max_bs * i;
973 list_add(&io_u->list, &td->io_u_freelist);
979 static int create_file(struct thread_data *td, unsigned long long size)
981 unsigned long long left;
987 * unless specifically asked for overwrite, let normal io extend it
989 if (!td->overwrite) {
990 td->real_file_size = size;
995 log_err("Need size for create\n");
996 td_verror(td, EINVAL);
1001 fprintf(f_out, "%s: Laying out IO file (%LuMiB)\n",td->name,size >> 20);
1003 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1005 td_verror(td, errno);
1009 if (ftruncate(td->fd, td->file_size) == -1) {
1010 td_verror(td, errno);
1014 td->io_size = td->file_size;
1015 b = malloc(td->max_bs);
1016 memset(b, 0, td->max_bs);
1019 while (left && !td->terminate) {
1024 r = write(td->fd, b, bs);
1026 if (r == (int) bs) {
1031 td_verror(td, errno);
1040 unlink(td->file_name);
1041 else if (td->create_fsync)
1053 static int file_size(struct thread_data *td)
1057 if (td->overwrite) {
1058 if (fstat(td->fd, &st) == -1) {
1059 td_verror(td, errno);
1063 td->real_file_size = st.st_size;
1065 if (!td->file_size || td->file_size > td->real_file_size)
1066 td->file_size = td->real_file_size;
1069 td->file_size -= td->file_offset;
1073 static int bdev_size(struct thread_data *td)
1075 unsigned long long bytes;
1078 r = blockdev_size(td->fd, &bytes);
1084 td->real_file_size = bytes;
1087 * no extend possibilities, so limit size to device size if too large
1089 if (!td->file_size || td->file_size > td->real_file_size)
1090 td->file_size = td->real_file_size;
1092 td->file_size -= td->file_offset;
1096 static int get_file_size(struct thread_data *td)
1100 if (td->filetype == FIO_TYPE_FILE)
1101 ret = file_size(td);
1102 else if (td->filetype == FIO_TYPE_BD)
1103 ret = bdev_size(td);
1105 td->real_file_size = -1;
1110 if (td->file_offset > td->real_file_size) {
1111 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size);
1115 td->io_size = td->file_size;
1116 if (td->io_size == 0) {
1117 log_err("%s: no io blocks\n", td->name);
1118 td_verror(td, EINVAL);
1123 td->zone_size = td->io_size;
1125 td->total_io_size = td->io_size * td->loops;
1129 static int setup_file_mmap(struct thread_data *td)
1134 flags = PROT_READ | PROT_WRITE;
1135 else if (td_write(td)) {
1138 if (td->verify != VERIFY_NONE)
1143 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1144 if (td->mmap == MAP_FAILED) {
1146 td_verror(td, errno);
1150 if (td->invalidate_cache) {
1151 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1152 td_verror(td, errno);
1157 if (td->sequential) {
1158 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1159 td_verror(td, errno);
1163 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1164 td_verror(td, errno);
1172 static int setup_file_plain(struct thread_data *td)
1174 if (td->invalidate_cache) {
1175 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1176 td_verror(td, errno);
1181 if (td->sequential) {
1182 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1183 td_verror(td, errno);
1187 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1188 td_verror(td, errno);
1196 static int setup_file(struct thread_data *td)
1201 if (stat(td->file_name, &st) == -1) {
1202 if (errno != ENOENT) {
1203 td_verror(td, errno);
1206 if (!td->create_file) {
1207 td_verror(td, ENOENT);
1210 if (create_file(td, td->file_size))
1212 } else if (td->filetype == FIO_TYPE_FILE &&
1213 st.st_size < (off_t) td->file_size) {
1214 if (create_file(td, td->file_size))
1219 flags |= OS_O_DIRECT;
1221 if (td_write(td) || td_rw(td)) {
1222 if (td->filetype == FIO_TYPE_FILE) {
1233 td->fd = open(td->file_name, flags, 0600);
1235 if (td->filetype == FIO_TYPE_CHAR)
1240 td->fd = open(td->file_name, flags);
1244 td_verror(td, errno);
1248 if (get_file_size(td))
1251 if (td->io_engine != FIO_MMAPIO)
1252 return setup_file_plain(td);
1254 return setup_file_mmap(td);
1257 static int switch_ioscheduler(struct thread_data *td)
1259 char tmp[256], tmp2[128];
1263 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1265 f = fopen(tmp, "r+");
1267 td_verror(td, errno);
1274 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1275 if (ferror(f) || ret != 1) {
1276 td_verror(td, errno);
1284 * Read back and check that the selected scheduler is now the default.
1286 ret = fread(tmp, 1, sizeof(tmp), f);
1287 if (ferror(f) || ret < 0) {
1288 td_verror(td, errno);
1293 sprintf(tmp2, "[%s]", td->ioscheduler);
1294 if (!strstr(tmp, tmp2)) {
1295 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
1296 td_verror(td, EINVAL);
1305 static void clear_io_state(struct thread_data *td)
1307 if (td->io_engine == FIO_SYNCIO)
1308 lseek(td->fd, SEEK_SET, 0);
1311 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1312 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1316 memset(td->file_map, 0, td->num_maps * sizeof(long));
1320 * Entry point for the thread based jobs. The process based jobs end up
1321 * here as well, after a little setup.
1323 static void *thread_main(void *data)
1325 struct thread_data *td = data;
1327 if (!td->use_thread)
1332 INIT_LIST_HEAD(&td->io_u_freelist);
1333 INIT_LIST_HEAD(&td->io_u_busylist);
1334 INIT_LIST_HEAD(&td->io_hist_list);
1335 INIT_LIST_HEAD(&td->io_log_list);
1340 if (fio_setaffinity(td) == -1) {
1341 td_verror(td, errno);
1352 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1353 td_verror(td, errno);
1358 if (nice(td->nice) < 0) {
1359 td_verror(td, errno);
1363 if (init_random_state(td))
1366 if (td->ioscheduler && switch_ioscheduler(td))
1369 td_set_runstate(td, TD_INITIALIZED);
1370 fio_sem_up(&startup_sem);
1371 fio_sem_down(&td->mutex);
1373 if (!td->create_serialize && setup_file(td))
1376 gettimeofday(&td->epoch, NULL);
1378 if (td->exec_prerun)
1379 system(td->exec_prerun);
1381 while (td->loops--) {
1382 getrusage(RUSAGE_SELF, &td->ru_start);
1383 gettimeofday(&td->start, NULL);
1384 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1387 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1390 prune_io_piece_log(td);
1394 td->runtime[td->ddir] += mtime_since_now(&td->start);
1395 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1396 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1398 update_rusage_stat(td);
1400 if (td->error || td->terminate)
1403 if (td->verify == VERIFY_NONE)
1407 gettimeofday(&td->start, NULL);
1411 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1413 if (td->error || td->terminate)
1418 finish_log(td, td->bw_log, "bw");
1420 finish_log(td, td->slat_log, "slat");
1422 finish_log(td, td->clat_log, "clat");
1423 if (td->write_iolog)
1424 write_iolog_close(td);
1425 if (td->exec_postrun)
1426 system(td->exec_postrun);
1428 if (exitall_on_terminate)
1429 terminate_threads(td->groupid);
1437 munmap(td->mmap, td->file_size);
1440 td_set_runstate(td, TD_EXITED);
1446 * We cannot pass the td data into a forked process, so attach the td and
1447 * pass it to the thread worker.
1449 static void *fork_main(int shmid, int offset)
1451 struct thread_data *td;
1454 data = shmat(shmid, NULL, 0);
1455 if (data == (void *) -1) {
1460 td = data + offset * sizeof(struct thread_data);
1467 * Sets the status of the 'td' in the printed status map.
1469 static void check_str_update(struct thread_data *td)
1471 char c = run_str[td->thread_number - 1];
1473 switch (td->runstate) {
1486 } else if (td_read(td)) {
1507 case TD_INITIALIZED:
1510 case TD_NOT_CREATED:
1514 log_err("state %d\n", td->runstate);
1517 run_str[td->thread_number - 1] = c;
1521 * Convert seconds to a printable string.
1523 static void eta_to_str(char *str, int eta_sec)
1525 unsigned int d, h, m, s;
1526 static int always_d, always_h;
1538 if (d || always_d) {
1540 str += sprintf(str, "%02dd:", d);
1542 if (h || always_h) {
1544 str += sprintf(str, "%02dh:", h);
1547 str += sprintf(str, "%02dm:", m);
1548 str += sprintf(str, "%02ds", s);
1552 * Best effort calculation of the estimated pending runtime of a job.
1554 static int thread_eta(struct thread_data *td, unsigned long elapsed)
1556 unsigned long long bytes_total, bytes_done;
1557 unsigned int eta_sec = 0;
1559 bytes_total = td->total_io_size;
1562 * if writing, bytes_total will be twice the size. If mixing,
1563 * assume a 50/50 split and thus bytes_total will be 50% larger.
1567 bytes_total = bytes_total * 3 / 2;
1571 if (td->zone_size && td->zone_skip)
1572 bytes_total /= (td->zone_skip / td->zone_size);
1574 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1577 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1578 perc = (double) bytes_done / (double) bytes_total;
1582 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1584 if (td->timeout && eta_sec > (td->timeout - elapsed))
1585 eta_sec = td->timeout - elapsed;
1586 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1587 || td->runstate == TD_INITIALIZED) {
1588 int t_eta = 0, r_eta = 0;
1591 * We can only guess - assume it'll run the full timeout
1592 * if given, otherwise assume it'll run at the specified rate.
1595 t_eta = td->timeout + td->start_delay - elapsed;
1597 r_eta = (bytes_total / 1024) / td->rate;
1598 r_eta += td->start_delay - elapsed;
1602 eta_sec = min(r_eta, t_eta);
1611 * thread is already done or waiting for fsync
1620 * Print status of the jobs we know about. This includes rate estimates,
1621 * ETA, thread state, etc.
1623 static void print_thread_status(void)
1625 unsigned long elapsed = time_since_now(&genesis);
1626 int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec;
1630 if (temp_stall_ts || terse_output)
1633 eta_secs = malloc(thread_number * sizeof(int));
1634 memset(eta_secs, 0, thread_number * sizeof(int));
1636 nr_pending = nr_running = t_rate = m_rate = 0;
1637 for (i = 0; i < thread_number; i++) {
1638 struct thread_data *td = &threads[i];
1640 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING||
1641 td->runstate == TD_FSYNCING) {
1644 m_rate += td->ratemin;
1645 } else if (td->runstate < TD_RUNNING)
1649 eta_secs[i] = thread_eta(td, elapsed);
1651 eta_secs[i] = INT_MAX;
1653 check_str_update(td);
1656 if (exitall_on_terminate)
1661 for (i = 0; i < thread_number; i++) {
1662 if (exitall_on_terminate) {
1663 if (eta_secs[i] < eta_sec)
1664 eta_sec = eta_secs[i];
1666 if (eta_secs[i] > eta_sec)
1667 eta_sec = eta_secs[i];
1671 if (eta_sec != INT_MAX && elapsed) {
1672 perc = (double) elapsed / (double) (elapsed + eta_sec);
1673 eta_to_str(eta_str, eta_sec);
1676 if (!nr_running && !nr_pending)
1679 printf("Threads running: %d", nr_running);
1680 if (m_rate || t_rate)
1681 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1682 if (eta_sec != INT_MAX) {
1684 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1692 * Run over the job map and reap the threads that have exited, if any.
1694 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1699 * reap exited threads (TD_EXITED -> TD_REAPED)
1701 for (i = 0; i < thread_number; i++) {
1702 struct thread_data *td = &threads[i];
1704 if (td->runstate != TD_EXITED)
1707 td_set_runstate(td, TD_REAPED);
1709 if (td->use_thread) {
1712 if (pthread_join(td->thread, (void *) &ret))
1713 perror("thread_join");
1715 waitpid(td->pid, NULL, 0);
1718 (*m_rate) -= td->ratemin;
1719 (*t_rate) -= td->rate;
1723 static void fio_unpin_memory(void *pinned)
1726 if (munlock(pinned, mlock_size) < 0)
1728 munmap(pinned, mlock_size);
1732 static void *fio_pin_memory(void)
1734 unsigned long long phys_mem;
1741 * Don't allow mlock of more than real_mem-128MB
1743 phys_mem = os_phys_mem();
1745 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1746 mlock_size = phys_mem - 128 * 1024 * 1024;
1747 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
1751 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1753 perror("malloc locked mem");
1756 if (mlock(ptr, mlock_size) < 0) {
1757 munmap(ptr, mlock_size);
1766 * Main function for kicking off and reaping jobs, as needed.
1768 static void run_threads(void)
1770 struct thread_data *td;
1771 unsigned long spent;
1772 int i, todo, nr_running, m_rate, t_rate, nr_started;
1775 mlocked_mem = fio_pin_memory();
1777 if (!terse_output) {
1778 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1782 signal(SIGINT, sig_handler);
1783 signal(SIGALRM, sig_handler);
1785 todo = thread_number;
1788 m_rate = t_rate = 0;
1790 for (i = 0; i < thread_number; i++) {
1793 run_str[td->thread_number - 1] = 'P';
1797 if (!td->create_serialize)
1801 * do file setup here so it happens sequentially,
1802 * we don't want X number of threads getting their
1803 * client data interspersed on disk
1805 if (setup_file(td)) {
1806 td_set_runstate(td, TD_REAPED);
1811 gettimeofday(&genesis, NULL);
1814 struct thread_data *map[MAX_JOBS];
1815 struct timeval this_start;
1816 int this_jobs = 0, left;
1819 * create threads (TD_NOT_CREATED -> TD_CREATED)
1821 for (i = 0; i < thread_number; i++) {
1824 if (td->runstate != TD_NOT_CREATED)
1828 * never got a chance to start, killed by other
1829 * thread for some reason
1831 if (td->terminate) {
1836 if (td->start_delay) {
1837 spent = mtime_since_now(&genesis);
1839 if (td->start_delay * 1000 > spent)
1843 if (td->stonewall && (nr_started || nr_running))
1847 * Set state to created. Thread will transition
1848 * to TD_INITIALIZED when it's done setting up.
1850 td_set_runstate(td, TD_CREATED);
1851 map[this_jobs++] = td;
1852 fio_sem_init(&startup_sem, 1);
1855 if (td->use_thread) {
1856 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1857 perror("thread_create");
1862 fio_sem_down(&startup_sem);
1864 fork_main(shm_id, i);
1871 * Wait for the started threads to transition to
1874 gettimeofday(&this_start, NULL);
1877 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1882 for (i = 0; i < this_jobs; i++) {
1886 if (td->runstate == TD_INITIALIZED) {
1889 } else if (td->runstate >= TD_EXITED) {
1893 nr_running++; /* work-around... */
1899 log_err("fio: %d jobs failed to start\n", left);
1900 for (i = 0; i < this_jobs; i++) {
1904 kill(td->pid, SIGTERM);
1910 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1912 for (i = 0; i < thread_number; i++) {
1915 if (td->runstate != TD_INITIALIZED)
1918 td_set_runstate(td, TD_RUNNING);
1921 m_rate += td->ratemin;
1924 fio_sem_up(&td->mutex);
1927 reap_threads(&nr_running, &t_rate, &m_rate);
1933 while (nr_running) {
1934 reap_threads(&nr_running, &t_rate, &m_rate);
1939 fio_unpin_memory(mlocked_mem);
1942 int main(int argc, char *argv[])
1944 if (parse_options(argc, argv))
1947 if (!thread_number) {
1948 log_err("Nothing to do\n");
1952 disk_util_timer_arm();