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;
64 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
66 static sem_t startup_sem;
68 #define TERMINATE_ALL (-1)
69 #define JOB_START_TIMEOUT (5 * 1000)
71 static void terminate_threads(int group_id)
75 for (i = 0; i < thread_number; i++) {
76 struct thread_data *td = &threads[i];
78 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
85 static void sig_handler(int sig)
90 disk_util_timer_arm();
91 print_thread_status();
94 printf("\nfio: terminating on signal\n");
96 terminate_threads(TERMINATE_ALL);
101 static int random_map_free(struct thread_data *td, unsigned long long block)
103 unsigned int idx = RAND_MAP_IDX(td, block);
104 unsigned int bit = RAND_MAP_BIT(td, block);
106 return (td->file_map[idx] & (1UL << bit)) == 0;
109 static int get_next_free_block(struct thread_data *td, unsigned long long *b)
115 while ((*b) * td->min_bs < td->io_size) {
116 if (td->file_map[i] != -1UL) {
117 *b += ffz(td->file_map[i]);
121 *b += BLOCKS_PER_MAP;
128 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
130 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
131 unsigned int blocks = 0;
133 while (blocks < (io_u->buflen / td->min_bs)) {
134 unsigned int idx, bit;
136 if (!random_map_free(td, block))
139 idx = RAND_MAP_IDX(td, block);
140 bit = RAND_MAP_BIT(td, block);
142 assert(idx < td->num_maps);
144 td->file_map[idx] |= (1UL << bit);
149 if ((blocks * td->min_bs) < io_u->buflen)
150 io_u->buflen = blocks * td->min_bs;
153 static int get_next_offset(struct thread_data *td, unsigned long long *offset)
155 unsigned long long b, rb;
158 if (!td->sequential) {
159 unsigned long long max_blocks = td->io_size / td->min_bs;
163 lrand48_r(&td->random_state, &r);
164 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
165 rb = b + (td->file_offset / td->min_bs);
167 } while (!random_map_free(td, rb) && loops);
170 if (get_next_free_block(td, &b))
174 b = td->last_pos / td->min_bs;
176 *offset = (b * td->min_bs) + td->file_offset;
177 if (*offset > td->real_file_size)
183 static unsigned int get_next_buflen(struct thread_data *td)
188 if (td->min_bs == td->max_bs)
191 lrand48_r(&td->bsrange_state, &r);
192 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
193 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
196 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
197 buflen = td->io_size - td->this_io_bytes[td->ddir];
202 static int check_min_rate(struct thread_data *td, struct timeval *now)
209 * allow a 2 second settle period in the beginning
211 if (mtime_since(&td->start, now) < 2000)
215 * if rate blocks is set, sample is running
217 if (td->rate_bytes) {
218 spent = mtime_since(&td->lastrate, now);
219 if (spent < td->ratecycle)
222 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
223 if (rate < td->ratemin) {
224 printf("%s: min rate %d not met, got %ldKiB/sec\n", td->name, td->ratemin, rate);
226 terminate_threads(td->groupid);
231 td->rate_bytes = td->this_io_bytes[ddir];
232 memcpy(&td->lastrate, now, sizeof(*now));
236 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
240 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
246 static void fill_random_bytes(struct thread_data *td,
247 unsigned char *p, unsigned int len)
253 drand48_r(&td->verify_state, &r);
256 * lrand48_r seems to be broken and only fill the bottom
257 * 32-bits, even on 64-bit archs with 64-bit longs
270 static void hexdump(void *buffer, int len)
272 unsigned char *p = buffer;
275 for (i = 0; i < len; i++)
276 printf("%02x", p[i]);
280 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
282 unsigned char *p = (unsigned char *) io_u->buf;
286 c = crc32(p, hdr->len - sizeof(*hdr));
288 if (c != hdr->crc32) {
289 fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
290 fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c);
297 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
299 unsigned char *p = (unsigned char *) io_u->buf;
300 struct md5_ctx md5_ctx;
302 memset(&md5_ctx, 0, sizeof(md5_ctx));
304 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
306 if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
307 fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
308 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
309 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
316 static int verify_io_u(struct io_u *io_u)
318 struct verify_header *hdr = (struct verify_header *) io_u->buf;
321 if (hdr->fio_magic != FIO_HDR_MAGIC)
324 if (hdr->verify_type == VERIFY_MD5)
325 ret = verify_io_u_md5(hdr, io_u);
326 else if (hdr->verify_type == VERIFY_CRC32)
327 ret = verify_io_u_crc32(hdr, io_u);
329 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
336 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
338 hdr->crc32 = crc32(p, len);
341 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
343 struct md5_ctx md5_ctx;
345 memset(&md5_ctx, 0, sizeof(md5_ctx));
346 md5_update(&md5_ctx, p, len);
347 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
350 static int get_rw_ddir(struct thread_data *td)
354 unsigned long elapsed;
356 gettimeofday(&now, NULL);
357 elapsed = mtime_since_now(&td->rwmix_switch);
360 * Check if it's time to seed a new data direction.
362 if (elapsed >= td->rwmixcycle) {
366 lrand48_r(&td->random_state, &r);
367 v = 100UL * r / (unsigned long) (RAND_MAX + 1.0);
368 if (v < td->rwmixread)
369 td->rwmix_ddir = DDIR_READ;
371 td->rwmix_ddir = DDIR_WRITE;
372 memcpy(&td->rwmix_switch, &now, sizeof(now));
374 return td->rwmix_ddir;
375 } else if (td_read(td))
382 * fill body of io_u->buf with random data and add a header with the
383 * crc32 or md5 sum of that data.
385 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
387 unsigned char *p = (unsigned char *) io_u->buf;
388 struct verify_header hdr;
390 hdr.fio_magic = FIO_HDR_MAGIC;
391 hdr.len = io_u->buflen;
393 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
395 if (td->verify == VERIFY_MD5) {
396 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
397 hdr.verify_type = VERIFY_MD5;
399 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
400 hdr.verify_type = VERIFY_CRC32;
403 memcpy(io_u->buf, &hdr, sizeof(hdr));
406 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
408 if (td->io_prep && td->io_prep(td, io_u))
414 void put_io_u(struct thread_data *td, struct io_u *io_u)
416 list_del(&io_u->list);
417 list_add(&io_u->list, &td->io_u_freelist);
421 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
424 * If using an iolog, grab next piece if any available.
427 return read_iolog_get(td, io_u);
430 * No log, let the seq/rand engine retrieve the next position.
432 if (!get_next_offset(td, &io_u->offset)) {
433 io_u->buflen = get_next_buflen(td);
436 io_u->ddir = get_rw_ddir(td);
439 * If using a write iolog, store this entry.
442 write_iolog_put(td, io_u);
451 #define queue_full(td) list_empty(&(td)->io_u_freelist)
453 struct io_u *__get_io_u(struct thread_data *td)
455 struct io_u *io_u = NULL;
457 if (!queue_full(td)) {
458 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
462 list_del(&io_u->list);
463 list_add(&io_u->list, &td->io_u_busylist);
470 static struct io_u *get_io_u(struct thread_data *td)
474 io_u = __get_io_u(td);
478 if (td->zone_bytes >= td->zone_size) {
480 td->last_pos += td->zone_skip;
483 if (fill_io_u(td, io_u)) {
488 if (io_u->buflen + io_u->offset > td->real_file_size)
489 io_u->buflen = td->real_file_size - io_u->offset;
496 if (!td->read_iolog && !td->sequential)
497 mark_random_map(td, io_u);
499 td->last_pos += io_u->buflen;
501 if (td->verify != VERIFY_NONE)
502 populate_io_u(td, io_u);
504 if (td_io_prep(td, io_u)) {
509 gettimeofday(&io_u->start_time, NULL);
513 static inline void td_set_runstate(struct thread_data *td, int runstate)
515 td->old_runstate = td->runstate;
516 td->runstate = runstate;
519 static int get_next_verify(struct thread_data *td, struct io_u *io_u)
521 struct io_piece *ipo;
523 if (!list_empty(&td->io_hist_list)) {
524 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
526 list_del(&ipo->list);
528 io_u->offset = ipo->offset;
529 io_u->buflen = ipo->len;
530 io_u->ddir = DDIR_READ;
538 static int sync_td(struct thread_data *td)
541 return td->io_sync(td);
546 static int io_u_getevents(struct thread_data *td, int min, int max,
549 return td->io_getevents(td, min, max, t);
552 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
554 gettimeofday(&io_u->issue_time, NULL);
556 return td->io_queue(td, io_u);
559 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
561 static void io_completed(struct thread_data *td, struct io_u *io_u,
562 struct io_completion_data *icd)
567 gettimeofday(&e, NULL);
570 unsigned int bytes = io_u->buflen - io_u->resid;
571 const int idx = io_u->ddir;
573 td->io_blocks[idx]++;
574 td->io_bytes[idx] += bytes;
575 td->zone_bytes += bytes;
576 td->this_io_bytes[idx] += bytes;
578 msec = mtime_since(&io_u->issue_time, &e);
580 add_clat_sample(td, idx, msec);
581 add_bw_sample(td, idx);
583 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
584 log_io_piece(td, io_u);
586 icd->bytes_done[idx] += bytes;
588 icd->error = io_u->error;
591 static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
597 icd->bytes_done[0] = icd->bytes_done[1] = 0;
599 for (i = 0; i < icd->nr; i++) {
600 io_u = td->io_event(td, i);
602 io_completed(td, io_u, icd);
607 static void cleanup_pending_aio(struct thread_data *td)
609 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
610 struct list_head *entry, *n;
611 struct io_completion_data icd;
616 * get immediately available events, if any
618 r = io_u_getevents(td, 0, td->cur_depth, &ts);
621 ios_completed(td, &icd);
625 * now cancel remaining active events
628 list_for_each_safe(entry, n, &td->io_u_busylist) {
629 io_u = list_entry(entry, struct io_u, list);
631 r = td->io_cancel(td, io_u);
638 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
641 ios_completed(td, &icd);
646 static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
648 struct io_u *v_io_u = *io_u;
652 ret = verify_io_u(v_io_u);
653 put_io_u(td, v_io_u);
660 static void do_verify(struct thread_data *td)
663 struct io_u *io_u, *v_io_u = NULL;
664 struct io_completion_data icd;
667 td_set_runstate(td, TD_VERIFYING);
673 gettimeofday(&t, NULL);
674 if (runtime_exceeded(td, &t))
677 io_u = __get_io_u(td);
681 if (get_next_verify(td, io_u)) {
686 if (td_io_prep(td, io_u)) {
691 ret = io_u_queue(td, io_u);
699 * we have one pending to verify, do that while
700 * we are doing io on the next one
702 if (do_io_u_verify(td, &v_io_u))
705 ret = io_u_getevents(td, 1, 1, NULL);
712 v_io_u = td->io_event(td, 0);
715 io_completed(td, v_io_u, &icd);
718 td_verror(td, icd.error);
719 put_io_u(td, v_io_u);
725 * if we can't submit more io, we need to verify now
727 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
732 do_io_u_verify(td, &v_io_u);
735 cleanup_pending_aio(td);
737 td_set_runstate(td, TD_RUNNING);
741 * Main IO worker functions. It retrieves io_u's to process and queues
742 * and reaps them, checking for rate and errors along the way.
744 static void do_io(struct thread_data *td)
746 struct io_completion_data icd;
750 while (td->this_io_bytes[td->ddir] < td->io_size) {
751 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
752 struct timespec *timeout;
753 int ret, min_evts = 0;
763 memcpy(&s, &io_u->start_time, sizeof(s));
765 ret = io_u_queue(td, io_u);
772 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
774 if (td->cur_depth < td->iodepth) {
782 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
790 ios_completed(td, &icd);
792 td_verror(td, icd.error);
797 * the rate is batched for now, it should work for batches
798 * of completions except the very first one which may look
801 gettimeofday(&e, NULL);
802 usec = utime_since(&s, &e);
804 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
806 if (check_min_rate(td, &e)) {
807 td_verror(td, ENOMEM);
811 if (runtime_exceeded(td, &e))
815 usec_sleep(td, td->thinktime);
817 if (should_fsync(td) && td->fsync_blocks &&
818 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
823 cleanup_pending_aio(td);
825 if (should_fsync(td) && td->end_fsync)
829 static void cleanup_io(struct thread_data *td)
835 static int init_io(struct thread_data *td)
837 if (td->io_engine == FIO_SYNCIO)
838 return fio_syncio_init(td);
839 else if (td->io_engine == FIO_MMAPIO)
840 return fio_mmapio_init(td);
841 else if (td->io_engine == FIO_LIBAIO)
842 return fio_libaio_init(td);
843 else if (td->io_engine == FIO_POSIXAIO)
844 return fio_posixaio_init(td);
845 else if (td->io_engine == FIO_SGIO)
846 return fio_sgio_init(td);
847 else if (td->io_engine == FIO_SPLICEIO)
848 return fio_spliceio_init(td);
850 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
855 static void cleanup_io_u(struct thread_data *td)
857 struct list_head *entry, *n;
860 list_for_each_safe(entry, n, &td->io_u_freelist) {
861 io_u = list_entry(entry, struct io_u, list);
863 list_del(&io_u->list);
867 if (td->mem_type == MEM_MALLOC)
868 free(td->orig_buffer);
869 else if (td->mem_type == MEM_SHM) {
870 struct shmid_ds sbuf;
872 shmdt(td->orig_buffer);
873 shmctl(td->shm_id, IPC_RMID, &sbuf);
874 } else if (td->mem_type == MEM_MMAP)
875 munmap(td->orig_buffer, td->orig_buffer_size);
877 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
879 td->orig_buffer = NULL;
882 static int init_io_u(struct thread_data *td)
888 if (td->io_engine & FIO_SYNCIO)
891 max_units = td->iodepth;
893 td->orig_buffer_size = td->max_bs * max_units + MASK;
895 if (td->mem_type == MEM_MALLOC)
896 td->orig_buffer = malloc(td->orig_buffer_size);
897 else if (td->mem_type == MEM_SHM) {
898 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
899 if (td->shm_id < 0) {
900 td_verror(td, errno);
905 td->orig_buffer = shmat(td->shm_id, NULL, 0);
906 if (td->orig_buffer == (void *) -1) {
907 td_verror(td, errno);
909 td->orig_buffer = NULL;
912 } else if (td->mem_type == MEM_MMAP) {
913 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
914 if (td->orig_buffer == MAP_FAILED) {
915 td_verror(td, errno);
917 td->orig_buffer = NULL;
922 p = ALIGN(td->orig_buffer);
923 for (i = 0; i < max_units; i++) {
924 io_u = malloc(sizeof(*io_u));
925 memset(io_u, 0, sizeof(*io_u));
926 INIT_LIST_HEAD(&io_u->list);
928 io_u->buf = p + td->max_bs * i;
930 list_add(&io_u->list, &td->io_u_freelist);
936 static void cleanup_allocs(struct thread_data *td)
941 free(td->iolog_file);
943 free(td->exec_prerun);
944 if (td->exec_postrun)
945 free(td->exec_postrun);
947 free(td->ioscheduler);
949 free(td->sysfs_root);
952 static int create_file(struct thread_data *td, unsigned long long size,
955 unsigned long long left;
961 * unless specifically asked for overwrite, let normal io extend it
963 if (td_write(td) && !td->overwrite)
967 fprintf(stderr, "Need size for create\n");
968 td_verror(td, EINVAL);
973 oflags = O_CREAT | O_TRUNC;
974 printf("%s: Laying out IO file (%LuMiB)\n", td->name, size >> 20);
977 printf("%s: Extending IO file (%Lu -> %LuMiB)\n", td->name, (td->file_size - size) >> 20, td->file_size >> 20);
980 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
982 td_verror(td, errno);
986 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
987 td_verror(td, errno);
991 td->io_size = td->file_size;
992 b = malloc(td->max_bs);
993 memset(b, 0, td->max_bs);
996 while (left && !td->terminate) {
1001 r = write(td->fd, b, bs);
1003 if (r == (int) bs) {
1008 td_verror(td, errno);
1017 unlink(td->file_name);
1018 else if (td->create_fsync)
1027 static int file_size(struct thread_data *td)
1031 if (fstat(td->fd, &st) == -1) {
1032 td_verror(td, errno);
1036 td->real_file_size = st.st_size;
1038 if (!td->file_size || td->file_size > td->real_file_size)
1039 td->file_size = td->real_file_size;
1041 td->file_size -= td->file_offset;
1045 static int bdev_size(struct thread_data *td)
1047 unsigned long long bytes;
1050 r = blockdev_size(td->fd, &bytes);
1056 td->real_file_size = bytes;
1059 * no extend possibilities, so limit size to device size if too large
1061 if (!td->file_size || td->file_size > td->real_file_size)
1062 td->file_size = td->real_file_size;
1064 td->file_size -= td->file_offset;
1068 static int get_file_size(struct thread_data *td)
1072 if (td->filetype == FIO_TYPE_FILE)
1073 ret = file_size(td);
1074 else if (td->filetype == FIO_TYPE_BD)
1075 ret = bdev_size(td);
1077 td->real_file_size = -1;
1082 if (td->file_offset > td->real_file_size) {
1083 fprintf(stderr, "%s: offset extends end (%Lu > %Lu)\n", td->name, td->file_offset, td->real_file_size);
1087 td->io_size = td->file_size;
1088 if (td->io_size == 0) {
1089 fprintf(stderr, "%s: no io blocks\n", td->name);
1090 td_verror(td, EINVAL);
1095 td->zone_size = td->io_size;
1097 td->total_io_size = td->io_size * td->loops;
1101 static int setup_file_mmap(struct thread_data *td)
1106 flags = PROT_READ | PROT_WRITE;
1107 else if (td_write(td)) {
1110 if (td->verify != VERIFY_NONE)
1115 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1116 if (td->mmap == MAP_FAILED) {
1118 td_verror(td, errno);
1122 if (td->invalidate_cache) {
1123 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1124 td_verror(td, errno);
1129 if (td->sequential) {
1130 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1131 td_verror(td, errno);
1135 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1136 td_verror(td, errno);
1144 static int setup_file_plain(struct thread_data *td)
1146 if (td->invalidate_cache) {
1147 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1148 td_verror(td, errno);
1153 if (td->sequential) {
1154 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1155 td_verror(td, errno);
1159 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1160 td_verror(td, errno);
1168 static int setup_file(struct thread_data *td)
1173 if (stat(td->file_name, &st) == -1) {
1174 if (errno != ENOENT) {
1175 td_verror(td, errno);
1178 if (!td->create_file) {
1179 td_verror(td, ENOENT);
1182 if (create_file(td, td->file_size, 0))
1184 } else if (td->filetype == FIO_TYPE_FILE) {
1185 if (st.st_size < (off_t) td->file_size) {
1186 if (create_file(td, td->file_size - st.st_size, 1))
1194 if (td_write(td) || td_rw(td)) {
1195 if (td->filetype == FIO_TYPE_FILE) {
1206 td->fd = open(td->file_name, flags, 0600);
1208 if (td->filetype == FIO_TYPE_CHAR)
1213 td->fd = open(td->file_name, flags);
1217 td_verror(td, errno);
1221 if (get_file_size(td))
1224 if (td->io_engine != FIO_MMAPIO)
1225 return setup_file_plain(td);
1227 return setup_file_mmap(td);
1230 static int switch_ioscheduler(struct thread_data *td)
1232 char tmp[256], tmp2[128];
1236 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1238 f = fopen(tmp, "r+");
1240 td_verror(td, errno);
1247 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1248 if (ferror(f) || ret != 1) {
1249 td_verror(td, errno);
1257 * Read back and check that the selected scheduler is now the default.
1259 ret = fread(tmp, 1, sizeof(tmp), f);
1260 if (ferror(f) || ret < 0) {
1261 td_verror(td, errno);
1266 sprintf(tmp2, "[%s]", td->ioscheduler);
1267 if (!strstr(tmp, tmp2)) {
1268 fprintf(stderr, "fio: io scheduler %s not found\n", td->ioscheduler);
1269 td_verror(td, EINVAL);
1278 static void clear_io_state(struct thread_data *td)
1280 if (td->io_engine == FIO_SYNCIO)
1281 lseek(td->fd, SEEK_SET, 0);
1284 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1285 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1289 memset(td->file_map, 0, td->num_maps * sizeof(long));
1292 static void *thread_main(void *data)
1294 struct thread_data *td = data;
1296 if (!td->use_thread)
1301 INIT_LIST_HEAD(&td->io_u_freelist);
1302 INIT_LIST_HEAD(&td->io_u_busylist);
1303 INIT_LIST_HEAD(&td->io_hist_list);
1304 INIT_LIST_HEAD(&td->io_log_list);
1309 if (fio_setaffinity(td) == -1) {
1310 td_verror(td, errno);
1321 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1322 td_verror(td, errno);
1327 if (nice(td->nice) < 0) {
1328 td_verror(td, errno);
1332 if (init_random_state(td))
1335 if (td->ioscheduler && switch_ioscheduler(td))
1338 td_set_runstate(td, TD_INITIALIZED);
1339 sem_post(&startup_sem);
1340 sem_wait(&td->mutex);
1342 if (!td->create_serialize && setup_file(td))
1345 gettimeofday(&td->epoch, NULL);
1347 if (td->exec_prerun)
1348 system(td->exec_prerun);
1350 while (td->loops--) {
1351 getrusage(RUSAGE_SELF, &td->ru_start);
1352 gettimeofday(&td->start, NULL);
1353 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1356 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1359 prune_io_piece_log(td);
1363 td->runtime[td->ddir] += mtime_since_now(&td->start);
1364 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1365 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1367 update_rusage_stat(td);
1369 if (td->error || td->terminate)
1372 if (td->verify == VERIFY_NONE)
1376 gettimeofday(&td->start, NULL);
1380 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1382 if (td->error || td->terminate)
1387 finish_log(td, td->bw_log, "bw");
1389 finish_log(td, td->slat_log, "slat");
1391 finish_log(td, td->clat_log, "clat");
1392 if (td->write_iolog)
1393 write_iolog_close(td);
1394 if (td->exec_postrun)
1395 system(td->exec_postrun);
1397 if (exitall_on_terminate)
1398 terminate_threads(td->groupid);
1406 munmap(td->mmap, td->file_size);
1410 td_set_runstate(td, TD_EXITED);
1415 static void *fork_main(int shmid, int offset)
1417 struct thread_data *td;
1420 data = shmat(shmid, NULL, 0);
1421 if (data == (void *) -1) {
1426 td = data + offset * sizeof(struct thread_data);
1432 static void check_str_update(struct thread_data *td)
1434 char c = run_str[td->thread_number - 1];
1436 if (td->runstate == td->old_runstate)
1439 switch (td->runstate) {
1452 } else if (td_read(td)) {
1470 case TD_INITIALIZED:
1473 case TD_NOT_CREATED:
1477 printf("state %d\n", td->runstate);
1480 run_str[td->thread_number - 1] = c;
1481 td->old_runstate = td->runstate;
1484 static void eta_to_str(char *str, int eta_sec)
1486 unsigned int d, h, m, s;
1487 static int always_d, always_h;
1499 if (d || always_d) {
1501 str += sprintf(str, "%02dd:", d);
1503 if (h || always_h) {
1505 str += sprintf(str, "%02dh:", h);
1508 str += sprintf(str, "%02dm:", m);
1509 str += sprintf(str, "%02ds", s);
1512 static int thread_eta(struct thread_data *td, unsigned long elapsed)
1514 unsigned long long bytes_total, bytes_done;
1515 unsigned int eta_sec = 0;
1517 bytes_total = td->total_io_size;
1520 * if writing, bytes_total will be twice the size. If mixing,
1521 * assume a 50/50 split and thus bytes_total will be 50% larger.
1525 bytes_total = bytes_total * 3 / 2;
1529 if (td->zone_size && td->zone_skip)
1530 bytes_total /= (td->zone_skip / td->zone_size);
1532 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1535 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1536 perc = (double) bytes_done / (double) bytes_total;
1540 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1542 if (td->timeout && eta_sec > (td->timeout - elapsed))
1543 eta_sec = td->timeout - elapsed;
1544 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1545 || td->runstate == TD_INITIALIZED) {
1546 int t_eta = 0, r_eta = 0;
1549 * We can only guess - assume it'll run the full timeout
1550 * if given, otherwise assume it'll run at the specified rate.
1553 t_eta = td->timeout + td->start_delay - elapsed;
1555 r_eta = (bytes_total / 1024) / td->rate;
1556 r_eta += td->start_delay - elapsed;
1560 eta_sec = min(r_eta, t_eta);
1569 * thread is already done
1577 static void print_thread_status(void)
1579 unsigned long elapsed = time_since_now(&genesis);
1580 int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec;
1584 eta_secs = malloc(thread_number * sizeof(int));
1585 memset(eta_secs, 0, thread_number * sizeof(int));
1587 nr_running = t_rate = m_rate = 0;
1588 for (i = 0; i < thread_number; i++) {
1589 struct thread_data *td = &threads[i];
1591 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){
1594 m_rate += td->ratemin;
1598 eta_secs[i] = thread_eta(td, elapsed);
1600 eta_secs[i] = INT_MAX;
1602 check_str_update(td);
1605 if (exitall_on_terminate)
1610 for (i = 0; i < thread_number; i++) {
1611 if (exitall_on_terminate) {
1612 if (eta_secs[i] < eta_sec)
1613 eta_sec = eta_secs[i];
1615 if (eta_secs[i] > eta_sec)
1616 eta_sec = eta_secs[i];
1620 if (eta_sec != INT_MAX && elapsed) {
1621 perc = (double) elapsed / (double) (elapsed + eta_sec);
1622 eta_to_str(eta_str, eta_sec);
1625 printf("Threads now running (%d)", nr_running);
1626 if (m_rate || t_rate)
1627 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1628 if (eta_sec != INT_MAX) {
1630 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1637 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1642 * reap exited threads (TD_EXITED -> TD_REAPED)
1644 for (i = 0; i < thread_number; i++) {
1645 struct thread_data *td = &threads[i];
1647 if (td->runstate != TD_EXITED)
1650 td_set_runstate(td, TD_REAPED);
1652 if (td->use_thread) {
1655 if (pthread_join(td->thread, (void *) &ret))
1656 perror("thread_join");
1658 waitpid(td->pid, NULL, 0);
1661 (*m_rate) -= td->ratemin;
1662 (*t_rate) -= td->rate;
1666 static void fio_unpin_memory(void *pinned)
1669 if (munlock(pinned, mlock_size) < 0)
1671 munmap(pinned, mlock_size);
1675 static void *fio_pin_memory(void)
1677 unsigned long long phys_mem;
1684 * Don't allow mlock of more than real_mem-128MB
1686 phys_mem = os_phys_mem();
1688 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
1689 mlock_size = phys_mem - 128 * 1024 * 1024;
1690 printf("fio: limiting mlocked memory to %lluMiB\n",
1695 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1697 perror("malloc locked mem");
1700 if (mlock(ptr, mlock_size) < 0) {
1701 munmap(ptr, mlock_size);
1709 static void run_threads(void)
1711 struct thread_data *td;
1712 unsigned long spent;
1713 int i, todo, nr_running, m_rate, t_rate, nr_started;
1716 mlocked_mem = fio_pin_memory();
1718 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1721 signal(SIGINT, sig_handler);
1722 signal(SIGALRM, sig_handler);
1724 todo = thread_number;
1727 m_rate = t_rate = 0;
1729 for (i = 0; i < thread_number; i++) {
1732 run_str[td->thread_number - 1] = 'P';
1736 if (!td->create_serialize)
1740 * do file setup here so it happens sequentially,
1741 * we don't want X number of threads getting their
1742 * client data interspersed on disk
1744 if (setup_file(td)) {
1745 td_set_runstate(td, TD_REAPED);
1750 gettimeofday(&genesis, NULL);
1753 struct thread_data *map[MAX_JOBS];
1754 struct timeval this_start;
1755 int this_jobs = 0, left;
1758 * create threads (TD_NOT_CREATED -> TD_CREATED)
1760 for (i = 0; i < thread_number; i++) {
1763 if (td->runstate != TD_NOT_CREATED)
1767 * never got a chance to start, killed by other
1768 * thread for some reason
1770 if (td->terminate) {
1775 if (td->start_delay) {
1776 spent = mtime_since_now(&genesis);
1778 if (td->start_delay * 1000 > spent)
1782 if (td->stonewall && (nr_started || nr_running))
1786 * Set state to created. Thread will transition
1787 * to TD_INITIALIZED when it's done setting up.
1789 td_set_runstate(td, TD_CREATED);
1790 map[this_jobs++] = td;
1791 sem_init(&startup_sem, 0, 1);
1794 if (td->use_thread) {
1795 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1796 perror("thread_create");
1801 sem_wait(&startup_sem);
1803 fork_main(shm_id, i);
1810 * Wait for the started threads to transition to
1813 printf("fio: Waiting for threads to initialize...\n");
1814 gettimeofday(&this_start, NULL);
1817 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1822 for (i = 0; i < this_jobs; i++) {
1826 if (td->runstate == TD_INITIALIZED) {
1829 } else if (td->runstate >= TD_EXITED) {
1833 nr_running++; /* work-around... */
1839 fprintf(stderr, "fio: %d jobs failed to start\n", left);
1840 for (i = 0; i < this_jobs; i++) {
1844 kill(td->pid, SIGTERM);
1850 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1852 printf("fio: Go for launch\n");
1853 for (i = 0; i < thread_number; i++) {
1856 if (td->runstate != TD_INITIALIZED)
1859 td_set_runstate(td, TD_RUNNING);
1862 m_rate += td->ratemin;
1865 sem_post(&td->mutex);
1868 reap_threads(&nr_running, &t_rate, &m_rate);
1874 while (nr_running) {
1875 reap_threads(&nr_running, &t_rate, &m_rate);
1880 fio_unpin_memory(mlocked_mem);
1883 int main(int argc, char *argv[])
1885 if (parse_options(argc, argv))
1888 if (!thread_number) {
1889 printf("Nothing to do\n");
1893 disk_util_timer_arm();