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;
47 static void print_thread_status(void);
49 extern unsigned long long mlock_size;
52 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
53 * will never back again. It may cycle between running/verififying/fsyncing.
54 * Once the thread reaches TD_EXITED, it is just waiting for the core to
68 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
70 static volatile int startup_sem;
72 #define TERMINATE_ALL (-1)
73 #define JOB_START_TIMEOUT (5 * 1000)
75 static void terminate_threads(int group_id)
79 for (i = 0; i < thread_number; i++) {
80 struct thread_data *td = &threads[i];
82 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
89 static void sig_handler(int sig)
94 disk_util_timer_arm();
95 print_thread_status();
98 printf("\nfio: terminating on signal\n");
100 terminate_threads(TERMINATE_ALL);
105 static int random_map_free(struct thread_data *td, unsigned long long block)
107 unsigned int idx = RAND_MAP_IDX(td, block);
108 unsigned int bit = RAND_MAP_BIT(td, block);
110 return (td->file_map[idx] & (1UL << bit)) == 0;
113 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
119 while ((*b) * td->min_bs < td->io_size) {
120 if (td->file_map[i] != -1UL) {
121 *b += ffz(td->file_map[i]);
125 *b += BLOCKS_PER_MAP;
132 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
134 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
135 unsigned int blocks = 0;
137 while (blocks < (io_u->buflen / td->min_bs)) {
138 unsigned int idx, bit;
140 if (!random_map_free(td, block))
143 idx = RAND_MAP_IDX(td, block);
144 bit = RAND_MAP_BIT(td, block);
146 assert(idx < td->num_maps);
148 td->file_map[idx] |= (1UL << bit);
153 if ((blocks * td->min_bs) < io_u->buflen)
154 io_u->buflen = blocks * td->min_bs;
157 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
159 unsigned long long b, rb;
162 if (!td->sequential) {
163 unsigned long long max_blocks = td->io_size / td->min_bs;
167 r = os_random_long(&td->random_state);
168 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
169 rb = b + (td->file_offset / td->min_bs);
171 } while (!random_map_free(td, rb) && loops);
174 if (get_next_free_block(td, &b))
178 b = td->last_pos / td->min_bs;
180 *offset = (b * td->min_bs) + td->file_offset;
181 if (*offset > td->real_file_size)
187 static unsigned int get_next_buflen(struct thread_data *td)
192 if (td->min_bs == td->max_bs)
195 r = os_random_long(&td->bsrange_state);
196 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
197 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
200 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
201 buflen = td->io_size - td->this_io_bytes[td->ddir];
206 static int check_min_rate(struct thread_data *td, struct timeval *now)
213 * allow a 2 second settle period in the beginning
215 if (mtime_since(&td->start, now) < 2000)
219 * if rate blocks is set, sample is running
221 if (td->rate_bytes) {
222 spent = mtime_since(&td->lastrate, now);
223 if (spent < td->ratecycle)
226 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
227 if (rate < td->ratemin) {
228 fprintf(f_out, "%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
230 terminate_threads(td->groupid);
235 td->rate_bytes = td->this_io_bytes[ddir];
236 memcpy(&td->lastrate, now, sizeof(*now));
240 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
244 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
250 static void fill_random_bytes(struct thread_data *td,
251 unsigned char *p, unsigned int len)
257 r = os_random_double(&td->verify_state);
260 * lrand48_r seems to be broken and only fill the bottom
261 * 32-bits, even on 64-bit archs with 64-bit longs
274 static void hexdump(void *buffer, int len)
276 unsigned char *p = buffer;
279 for (i = 0; i < len; i++)
280 fprintf(f_out, "%02x", p[i]);
281 fprintf(f_out, "\n");
284 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
286 unsigned char *p = (unsigned char *) io_u->buf;
290 c = crc32(p, hdr->len - sizeof(*hdr));
292 if (c != hdr->crc32) {
293 log_err("crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
294 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
301 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
303 unsigned char *p = (unsigned char *) io_u->buf;
304 struct md5_ctx md5_ctx;
306 memset(&md5_ctx, 0, sizeof(md5_ctx));
308 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
310 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
311 log_err("md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
312 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
313 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
320 static int verify_io_u(struct io_u *io_u)
322 struct verify_header *hdr = (struct verify_header *) io_u->buf;
325 if (hdr->fio_magic != FIO_HDR_MAGIC)
328 if (hdr->verify_type == VERIFY_MD5)
329 ret = verify_io_u_md5(hdr, io_u);
330 else if (hdr->verify_type == VERIFY_CRC32)
331 ret = verify_io_u_crc32(hdr, io_u);
333 log_err("Bad verify type %d\n", hdr->verify_type);
340 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
342 hdr->crc32 = crc32(p, len);
345 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
347 struct md5_ctx md5_ctx;
349 memset(&md5_ctx, 0, sizeof(md5_ctx));
350 md5_update(&md5_ctx, p, len);
351 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
354 static int get_rw_ddir(struct thread_data *td)
358 unsigned long elapsed;
360 gettimeofday(&now, NULL);
361 elapsed = mtime_since_now(&td->rwmix_switch);
364 * Check if it's time to seed a new data direction.
366 if (elapsed >= td->rwmixcycle) {
370 r = os_random_long(&td->rwmix_state);
371 v = 1 + (int) (100.0 * (r / (RAND_MAX + 1.0)));
372 if (v < td->rwmixread)
373 td->rwmix_ddir = DDIR_READ;
375 td->rwmix_ddir = DDIR_WRITE;
376 memcpy(&td->rwmix_switch, &now, sizeof(now));
378 return td->rwmix_ddir;
379 } else if (td_read(td))
386 * fill body of io_u->buf with random data and add a header with the
387 * crc32 or md5 sum of that data.
389 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
391 unsigned char *p = (unsigned char *) io_u->buf;
392 struct verify_header hdr;
394 hdr.fio_magic = FIO_HDR_MAGIC;
395 hdr.len = io_u->buflen;
397 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
399 if (td->verify == VERIFY_MD5) {
400 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
401 hdr.verify_type = VERIFY_MD5;
403 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
404 hdr.verify_type = VERIFY_CRC32;
407 memcpy(io_u->buf, &hdr, sizeof(hdr));
410 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
412 if (td->io_prep && td->io_prep(td, io_u))
418 void put_io_u(struct thread_data *td, struct io_u *io_u)
420 list_del(&io_u->list);
421 list_add(&io_u->list, &td->io_u_freelist);
425 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
428 * If using an iolog, grab next piece if any available.
431 return read_iolog_get(td, io_u);
434 * No log, let the seq/rand engine retrieve the next position.
436 if (!get_next_offset(td, &io_u->offset)) {
437 io_u->buflen = get_next_buflen(td);
440 io_u->ddir = get_rw_ddir(td);
443 * If using a write iolog, store this entry.
446 write_iolog_put(td, io_u);
455 #define queue_full(td) list_empty(&(td)->io_u_freelist)
457 struct io_u *__get_io_u(struct thread_data *td)
459 struct io_u *io_u = NULL;
461 if (!queue_full(td)) {
462 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
466 list_del(&io_u->list);
467 list_add(&io_u->list, &td->io_u_busylist);
474 static struct io_u *get_io_u(struct thread_data *td)
478 io_u = __get_io_u(td);
482 if (td->zone_bytes >= td->zone_size) {
484 td->last_pos += td->zone_skip;
487 if (fill_io_u(td, io_u)) {
492 if (io_u->buflen + io_u->offset > td->real_file_size)
493 io_u->buflen = td->real_file_size - io_u->offset;
500 if (!td->read_iolog && !td->sequential)
501 mark_random_map(td, io_u);
503 td->last_pos += io_u->buflen;
505 if (td->verify != VERIFY_NONE)
506 populate_io_u(td, io_u);
508 if (td_io_prep(td, io_u)) {
513 gettimeofday(&io_u->start_time, NULL);
517 static inline void td_set_runstate(struct thread_data *td, int runstate)
519 td->runstate = runstate;
522 static int get_next_verify(struct thread_data *td, struct io_u *io_u)
524 struct io_piece *ipo;
526 if (!list_empty(&td->io_hist_list)) {
527 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
529 list_del(&ipo->list);
531 io_u->offset = ipo->offset;
532 io_u->buflen = ipo->len;
533 io_u->ddir = DDIR_READ;
541 static int sync_td(struct thread_data *td)
544 return td->io_sync(td);
549 static int io_u_getevents(struct thread_data *td, int min, int max,
552 return td->io_getevents(td, min, max, t);
555 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
557 gettimeofday(&io_u->issue_time, NULL);
559 return td->io_queue(td, io_u);
562 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
564 static void io_completed(struct thread_data *td, struct io_u *io_u,
565 struct io_completion_data *icd)
570 gettimeofday(&e, NULL);
573 unsigned int bytes = io_u->buflen - io_u->resid;
574 const int idx = io_u->ddir;
576 td->io_blocks[idx]++;
577 td->io_bytes[idx] += bytes;
578 td->zone_bytes += bytes;
579 td->this_io_bytes[idx] += bytes;
581 msec = mtime_since(&io_u->issue_time, &e);
583 add_clat_sample(td, idx, msec);
584 add_bw_sample(td, idx);
586 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
587 log_io_piece(td, io_u);
589 icd->bytes_done[idx] += bytes;
591 icd->error = io_u->error;
594 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
600 icd->bytes_done[0] = icd->bytes_done[1] = 0;
602 for (i = 0; i < icd->nr; i++) {
603 io_u = td->io_event(td, i);
605 io_completed(td, io_u, icd);
610 static void cleanup_pending_aio(struct thread_data *td)
612 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
613 struct list_head *entry, *n;
614 struct io_completion_data icd;
619 * get immediately available events, if any
621 r = io_u_getevents(td, 0, td->cur_depth, &ts);
624 ios_completed(td, &icd);
628 * now cancel remaining active events
631 list_for_each_safe(entry, n, &td->io_u_busylist) {
632 io_u = list_entry(entry, struct io_u, list);
634 r = td->io_cancel(td, io_u);
641 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
644 ios_completed(td, &icd);
649 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
651 struct io_u *v_io_u = *io_u;
655 ret = verify_io_u(v_io_u);
656 put_io_u(td, v_io_u);
663 static void do_verify(struct thread_data *td)
666 struct io_u *io_u, *v_io_u = NULL;
667 struct io_completion_data icd;
670 td_set_runstate(td, TD_VERIFYING);
676 gettimeofday(&t, NULL);
677 if (runtime_exceeded(td, &t))
680 io_u = __get_io_u(td);
684 if (get_next_verify(td, io_u)) {
689 if (td_io_prep(td, io_u)) {
694 ret = io_u_queue(td, io_u);
702 * we have one pending to verify, do that while
703 * we are doing io on the next one
705 if (do_io_u_verify(td, &v_io_u))
708 ret = io_u_getevents(td, 1, 1, NULL);
715 v_io_u = td->io_event(td, 0);
718 io_completed(td, v_io_u, &icd);
721 td_verror(td, icd.error);
722 put_io_u(td, v_io_u);
728 * if we can't submit more io, we need to verify now
730 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
735 do_io_u_verify(td, &v_io_u);
738 cleanup_pending_aio(td);
740 td_set_runstate(td, TD_RUNNING);
744 * Main IO worker functions. It retrieves io_u's to process and queues
745 * and reaps them, checking for rate and errors along the way.
747 static void do_io(struct thread_data *td)
749 struct io_completion_data icd;
753 td_set_runstate(td, TD_RUNNING);
755 while (td->this_io_bytes[td->ddir] < td->io_size) {
756 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
757 struct timespec *timeout;
758 int ret, min_evts = 0;
768 memcpy(&s, &io_u->start_time, sizeof(s));
770 ret = io_u_queue(td, io_u);
777 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
779 if (td->cur_depth < td->iodepth) {
787 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
795 ios_completed(td, &icd);
797 td_verror(td, icd.error);
802 * the rate is batched for now, it should work for batches
803 * of completions except the very first one which may look
806 gettimeofday(&e, NULL);
807 usec = utime_since(&s, &e);
809 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
811 if (check_min_rate(td, &e)) {
812 td_verror(td, ENOMEM);
816 if (runtime_exceeded(td, &e))
820 usec_sleep(td, td->thinktime);
822 if (should_fsync(td) && td->fsync_blocks &&
823 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
828 cleanup_pending_aio(td);
830 if (should_fsync(td) && td->end_fsync) {
831 td_set_runstate(td, TD_FSYNCING);
836 static void cleanup_io(struct thread_data *td)
842 static int init_io(struct thread_data *td)
844 if (td->io_engine == FIO_SYNCIO)
845 return fio_syncio_init(td);
846 else if (td->io_engine == FIO_MMAPIO)
847 return fio_mmapio_init(td);
848 else if (td->io_engine == FIO_LIBAIO)
849 return fio_libaio_init(td);
850 else if (td->io_engine == FIO_POSIXAIO)
851 return fio_posixaio_init(td);
852 else if (td->io_engine == FIO_SGIO)
853 return fio_sgio_init(td);
854 else if (td->io_engine == FIO_SPLICEIO)
855 return fio_spliceio_init(td);
857 log_err("bad io_engine %d\n", td->io_engine);
862 static void cleanup_io_u(struct thread_data *td)
864 struct list_head *entry, *n;
867 list_for_each_safe(entry, n, &td->io_u_freelist) {
868 io_u = list_entry(entry, struct io_u, list);
870 list_del(&io_u->list);
874 if (td->mem_type == MEM_MALLOC)
875 free(td->orig_buffer);
876 else if (td->mem_type == MEM_SHM) {
877 struct shmid_ds sbuf;
879 shmdt(td->orig_buffer);
880 shmctl(td->shm_id, IPC_RMID, &sbuf);
881 } else if (td->mem_type == MEM_MMAP)
882 munmap(td->orig_buffer, td->orig_buffer_size);
884 log_err("Bad memory type %d\n", td->mem_type);
886 td->orig_buffer = NULL;
889 static int init_io_u(struct thread_data *td)
895 if (td->io_engine & FIO_SYNCIO)
898 max_units = td->iodepth;
900 td->orig_buffer_size = td->max_bs * max_units + MASK;
902 if (td->mem_type == MEM_MALLOC)
903 td->orig_buffer = malloc(td->orig_buffer_size);
904 else if (td->mem_type == MEM_SHM) {
905 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
906 if (td->shm_id < 0) {
907 td_verror(td, errno);
912 td->orig_buffer = shmat(td->shm_id, NULL, 0);
913 if (td->orig_buffer == (void *) -1) {
914 td_verror(td, errno);
916 td->orig_buffer = NULL;
919 } else if (td->mem_type == MEM_MMAP) {
920 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
921 if (td->orig_buffer == MAP_FAILED) {
922 td_verror(td, errno);
924 td->orig_buffer = NULL;
929 p = ALIGN(td->orig_buffer);
930 for (i = 0; i < max_units; i++) {
931 io_u = malloc(sizeof(*io_u));
932 memset(io_u, 0, sizeof(*io_u));
933 INIT_LIST_HEAD(&io_u->list);
935 io_u->buf = p + td->max_bs * i;
937 list_add(&io_u->list, &td->io_u_freelist);
943 static int create_file(struct thread_data *td, unsigned long long size,
946 unsigned long long left;
952 * unless specifically asked for overwrite, let normal io extend it
954 if (td_write(td) && !td->overwrite)
958 log_err("Need size for create\n");
959 td_verror(td, EINVAL);
964 oflags = O_CREAT | O_TRUNC;
965 fprintf(f_out, "%s: Laying out IO file (%LuMiB)\n", td->name, size >> 20);
968 fprintf(f_out, "%s: Extending IO file (%Lu -> %LuMiB)\n", td->name, (td->file_size - size) >> 20, td->file_size >> 20);
971 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
973 td_verror(td, errno);
977 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
978 td_verror(td, errno);
982 td->io_size = td->file_size;
983 b = malloc(td->max_bs);
984 memset(b, 0, td->max_bs);
987 while (left && !td->terminate) {
992 r = write(td->fd, b, bs);
999 td_verror(td, errno);
1008 unlink(td->file_name);
1009 else if (td->create_fsync)
1018 static int file_size(struct thread_data *td)
1022 if (fstat(td->fd, &st) == -1) {
1023 td_verror(td, errno);
1027 td->real_file_size = st.st_size;
1029 if (!td->file_size || td->file_size > td->real_file_size)
1030 td->file_size = td->real_file_size;
1032 td->file_size -= td->file_offset;
1036 static int bdev_size(struct thread_data *td)
1038 unsigned long long bytes;
1041 r = blockdev_size(td->fd, &bytes);
1047 td->real_file_size = bytes;
1050 * no extend possibilities, so limit size to device size if too large
1052 if (!td->file_size || td->file_size > td->real_file_size)
1053 td->file_size = td->real_file_size;
1055 td->file_size -= td->file_offset;
1059 static int get_file_size(struct thread_data *td)
1063 if (td->filetype == FIO_TYPE_FILE)
1064 ret = file_size(td);
1065 else if (td->filetype == FIO_TYPE_BD)
1066 ret = bdev_size(td);
1068 td->real_file_size = -1;
1073 if (td->file_offset > td->real_file_size) {
1074 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size);
1078 td->io_size = td->file_size;
1079 if (td->io_size == 0) {
1080 log_err("%s: no io blocks\n", td->name);
1081 td_verror(td, EINVAL);
1086 td->zone_size = td->io_size;
1088 td->total_io_size = td->io_size * td->loops;
1092 static int setup_file_mmap(struct thread_data *td)
1097 flags = PROT_READ | PROT_WRITE;
1098 else if (td_write(td)) {
1101 if (td->verify != VERIFY_NONE)
1106 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1107 if (td->mmap == MAP_FAILED) {
1109 td_verror(td, errno);
1113 if (td->invalidate_cache) {
1114 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1115 td_verror(td, errno);
1120 if (td->sequential) {
1121 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1122 td_verror(td, errno);
1126 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1127 td_verror(td, errno);
1135 static int setup_file_plain(struct thread_data *td)
1137 if (td->invalidate_cache) {
1138 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1139 td_verror(td, errno);
1144 if (td->sequential) {
1145 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1146 td_verror(td, errno);
1150 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1151 td_verror(td, errno);
1159 static int setup_file(struct thread_data *td)
1164 if (stat(td->file_name, &st) == -1) {
1165 if (errno != ENOENT) {
1166 td_verror(td, errno);
1169 if (!td->create_file) {
1170 td_verror(td, ENOENT);
1173 if (create_file(td, td->file_size, 0))
1175 } else if (td->filetype == FIO_TYPE_FILE) {
1176 if (st.st_size < (off_t) td->file_size) {
1177 if (create_file(td, td->file_size - st.st_size, 1))
1183 flags |= OS_O_DIRECT;
1185 if (td_write(td) || td_rw(td)) {
1186 if (td->filetype == FIO_TYPE_FILE) {
1197 td->fd = open(td->file_name, flags, 0600);
1199 if (td->filetype == FIO_TYPE_CHAR)
1204 td->fd = open(td->file_name, flags);
1208 td_verror(td, errno);
1212 if (get_file_size(td))
1215 if (td->io_engine != FIO_MMAPIO)
1216 return setup_file_plain(td);
1218 return setup_file_mmap(td);
1221 static int switch_ioscheduler(struct thread_data *td)
1223 char tmp[256], tmp2[128];
1227 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1229 f = fopen(tmp, "r+");
1231 td_verror(td, errno);
1238 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1239 if (ferror(f) || ret != 1) {
1240 td_verror(td, errno);
1248 * Read back and check that the selected scheduler is now the default.
1250 ret = fread(tmp, 1, sizeof(tmp), f);
1251 if (ferror(f) || ret < 0) {
1252 td_verror(td, errno);
1257 sprintf(tmp2, "[%s]", td->ioscheduler);
1258 if (!strstr(tmp, tmp2)) {
1259 log_err("fio: io scheduler %s not found\n", td->ioscheduler);
1260 td_verror(td, EINVAL);
1269 static void clear_io_state(struct thread_data *td)
1271 if (td->io_engine == FIO_SYNCIO)
1272 lseek(td->fd, SEEK_SET, 0);
1275 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1276 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1280 memset(td->file_map, 0, td->num_maps * sizeof(long));
1283 static void *thread_main(void *data)
1285 struct thread_data *td = data;
1287 if (!td->use_thread)
1292 INIT_LIST_HEAD(&td->io_u_freelist);
1293 INIT_LIST_HEAD(&td->io_u_busylist);
1294 INIT_LIST_HEAD(&td->io_hist_list);
1295 INIT_LIST_HEAD(&td->io_log_list);
1300 if (fio_setaffinity(td) == -1) {
1301 td_verror(td, errno);
1312 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1313 td_verror(td, errno);
1318 if (nice(td->nice) < 0) {
1319 td_verror(td, errno);
1323 if (init_random_state(td))
1326 if (td->ioscheduler && switch_ioscheduler(td))
1329 td_set_runstate(td, TD_INITIALIZED);
1330 fio_sem_up(&startup_sem);
1331 fio_sem_down(&td->mutex);
1333 if (!td->create_serialize && setup_file(td))
1336 gettimeofday(&td->epoch, NULL);
1338 if (td->exec_prerun)
1339 system(td->exec_prerun);
1341 while (td->loops--) {
1342 getrusage(RUSAGE_SELF, &td->ru_start);
1343 gettimeofday(&td->start, NULL);
1344 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1347 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1350 prune_io_piece_log(td);
1354 td->runtime[td->ddir] += mtime_since_now(&td->start);
1355 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1356 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1358 update_rusage_stat(td);
1360 if (td->error || td->terminate)
1363 if (td->verify == VERIFY_NONE)
1367 gettimeofday(&td->start, NULL);
1371 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1373 if (td->error || td->terminate)
1378 finish_log(td, td->bw_log, "bw");
1380 finish_log(td, td->slat_log, "slat");
1382 finish_log(td, td->clat_log, "clat");
1383 if (td->write_iolog)
1384 write_iolog_close(td);
1385 if (td->exec_postrun)
1386 system(td->exec_postrun);
1388 if (exitall_on_terminate)
1389 terminate_threads(td->groupid);
1397 munmap(td->mmap, td->file_size);
1400 td_set_runstate(td, TD_EXITED);
1405 static void *fork_main(int shmid, int offset)
1407 struct thread_data *td;
1410 data = shmat(shmid, NULL, 0);
1411 if (data == (void *) -1) {
1416 td = data + offset * sizeof(struct thread_data);
1422 static void check_str_update(struct thread_data *td)
1424 char c = run_str[td->thread_number - 1];
1426 switch (td->runstate) {
1439 } else if (td_read(td)) {
1460 case TD_INITIALIZED:
1463 case TD_NOT_CREATED:
1467 log_err("state %d\n", td->runstate);
1470 run_str[td->thread_number - 1] = c;
1473 static void eta_to_str(char *str, int eta_sec)
1475 unsigned int d, h, m, s;
1476 static int always_d, always_h;
1488 if (d || always_d) {
1490 str += sprintf(str, "%02dd:", d);
1492 if (h || always_h) {
1494 str += sprintf(str, "%02dh:", h);
1497 str += sprintf(str, "%02dm:", m);
1498 str += sprintf(str, "%02ds", s);
1501 static int thread_eta(struct thread_data *td, unsigned long elapsed)
1503 unsigned long long bytes_total, bytes_done;
1504 unsigned int eta_sec = 0;
1506 bytes_total = td->total_io_size;
1509 * if writing, bytes_total will be twice the size. If mixing,
1510 * assume a 50/50 split and thus bytes_total will be 50% larger.
1514 bytes_total = bytes_total * 3 / 2;
1518 if (td->zone_size && td->zone_skip)
1519 bytes_total /= (td->zone_skip / td->zone_size);
1521 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1524 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1525 perc = (double) bytes_done / (double) bytes_total;
1529 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1531 if (td->timeout && eta_sec > (td->timeout - elapsed))
1532 eta_sec = td->timeout - elapsed;
1533 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1534 || td->runstate == TD_INITIALIZED) {
1535 int t_eta = 0, r_eta = 0;
1538 * We can only guess - assume it'll run the full timeout
1539 * if given, otherwise assume it'll run at the specified rate.
1542 t_eta = td->timeout + td->start_delay - elapsed;
1544 r_eta = (bytes_total / 1024) / td->rate;
1545 r_eta += td->start_delay - elapsed;
1549 eta_sec = min(r_eta, t_eta);
1558 * thread is already done or waiting for fsync
1566 static void print_thread_status(void)
1568 unsigned long elapsed = time_since_now(&genesis);
1569 int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec;
1573 eta_secs = malloc(thread_number * sizeof(int));
1574 memset(eta_secs, 0, thread_number * sizeof(int));
1576 nr_pending = nr_running = t_rate = m_rate = 0;
1577 for (i = 0; i < thread_number; i++) {
1578 struct thread_data *td = &threads[i];
1580 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING||
1581 td->runstate == TD_FSYNCING) {
1584 m_rate += td->ratemin;
1585 } else if (td->runstate < TD_RUNNING)
1589 eta_secs[i] = thread_eta(td, elapsed);
1591 eta_secs[i] = INT_MAX;
1593 check_str_update(td);
1596 if (exitall_on_terminate)
1601 for (i = 0; i < thread_number; i++) {
1602 if (exitall_on_terminate) {
1603 if (eta_secs[i] < eta_sec)
1604 eta_sec = eta_secs[i];
1606 if (eta_secs[i] > eta_sec)
1607 eta_sec = eta_secs[i];
1611 if (eta_sec != INT_MAX && elapsed) {
1612 perc = (double) elapsed / (double) (elapsed + eta_sec);
1613 eta_to_str(eta_str, eta_sec);
1616 if (!nr_running && !nr_pending)
1619 printf("Threads running: %d", nr_running);
1620 if (m_rate || t_rate)
1621 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1622 if (eta_sec != INT_MAX) {
1624 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1631 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1636 * reap exited threads (TD_EXITED -> TD_REAPED)
1638 for (i = 0; i < thread_number; i++) {
1639 struct thread_data *td = &threads[i];
1641 if (td->runstate != TD_EXITED)
1644 td_set_runstate(td, TD_REAPED);
1646 if (td->use_thread) {
1649 if (pthread_join(td->thread, (void *) &ret))
1650 perror("thread_join");
1652 waitpid(td->pid, NULL, 0);
1655 (*m_rate) -= td->ratemin;
1656 (*t_rate) -= td->rate;
1660 static void fio_unpin_memory(void *pinned)
1663 if (munlock(pinned, mlock_size) < 0)
1665 munmap(pinned, mlock_size);
1669 static void *fio_pin_memory(void)
1671 unsigned long long phys_mem;
1678 * Don't allow mlock of more than real_mem-128MB
1680 phys_mem = os_phys_mem();
1682 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1683 mlock_size = phys_mem - 128 * 1024 * 1024;
1684 fprintf(f_out, "fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
1688 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1690 perror("malloc locked mem");
1693 if (mlock(ptr, mlock_size) < 0) {
1694 munmap(ptr, mlock_size);
1702 static void run_threads(void)
1704 struct thread_data *td;
1705 unsigned long spent;
1706 int i, todo, nr_running, m_rate, t_rate, nr_started;
1709 mlocked_mem = fio_pin_memory();
1711 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1714 signal(SIGINT, sig_handler);
1715 signal(SIGALRM, sig_handler);
1717 todo = thread_number;
1720 m_rate = t_rate = 0;
1722 for (i = 0; i < thread_number; i++) {
1725 run_str[td->thread_number - 1] = 'P';
1729 if (!td->create_serialize)
1733 * do file setup here so it happens sequentially,
1734 * we don't want X number of threads getting their
1735 * client data interspersed on disk
1737 if (setup_file(td)) {
1738 td_set_runstate(td, TD_REAPED);
1743 gettimeofday(&genesis, NULL);
1746 struct thread_data *map[MAX_JOBS];
1747 struct timeval this_start;
1748 int this_jobs = 0, left;
1751 * create threads (TD_NOT_CREATED -> TD_CREATED)
1753 for (i = 0; i < thread_number; i++) {
1756 if (td->runstate != TD_NOT_CREATED)
1760 * never got a chance to start, killed by other
1761 * thread for some reason
1763 if (td->terminate) {
1768 if (td->start_delay) {
1769 spent = mtime_since_now(&genesis);
1771 if (td->start_delay * 1000 > spent)
1775 if (td->stonewall && (nr_started || nr_running))
1779 * Set state to created. Thread will transition
1780 * to TD_INITIALIZED when it's done setting up.
1782 td_set_runstate(td, TD_CREATED);
1783 map[this_jobs++] = td;
1784 fio_sem_init(&startup_sem, 1);
1787 if (td->use_thread) {
1788 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1789 perror("thread_create");
1794 fio_sem_down(&startup_sem);
1796 fork_main(shm_id, i);
1803 * Wait for the started threads to transition to
1806 printf("fio: Waiting for threads to initialize...\n");
1807 gettimeofday(&this_start, NULL);
1810 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1815 for (i = 0; i < this_jobs; i++) {
1819 if (td->runstate == TD_INITIALIZED) {
1822 } else if (td->runstate >= TD_EXITED) {
1826 nr_running++; /* work-around... */
1832 log_err("fio: %d jobs failed to start\n", left);
1833 for (i = 0; i < this_jobs; i++) {
1837 kill(td->pid, SIGTERM);
1843 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1845 printf("fio: Go for launch\n");
1846 for (i = 0; i < thread_number; i++) {
1849 if (td->runstate != TD_INITIALIZED)
1852 td_set_runstate(td, TD_RUNNING);
1855 m_rate += td->ratemin;
1858 fio_sem_up(&td->mutex);
1861 reap_threads(&nr_running, &t_rate, &m_rate);
1867 while (nr_running) {
1868 reap_threads(&nr_running, &t_rate, &m_rate);
1873 fio_unpin_memory(mlocked_mem);
1876 int main(int argc, char *argv[])
1878 if (parse_options(argc, argv))
1881 if (!thread_number) {
1882 log_err("Nothing to do\n");
1886 disk_util_timer_arm();