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>
43 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
46 int thread_number = 0;
47 static char run_str[MAX_JOBS + 1];
49 static struct timeval genesis;
51 static void print_thread_status(void);
53 extern unsigned long long mlock_size;
68 #define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
70 static sem_t 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 lrand48_r(&td->random_state, &r);
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 lrand48_r(&td->bsrange_state, &r);
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 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, 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 drand48_r(&td->verify_state, &r);
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 printf("%02x", p[i]);
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;
291 c = crc32(p, hdr->len - sizeof(*hdr));
292 ret = c != hdr->crc32;
295 fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
296 fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c);
302 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
304 unsigned char *p = (unsigned char *) io_u->buf;
305 struct md5_ctx md5_ctx;
308 memset(&md5_ctx, 0, sizeof(md5_ctx));
310 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
312 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
314 fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
315 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
316 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
322 static int verify_io_u(struct io_u *io_u)
324 struct verify_header *hdr = (struct verify_header *) io_u->buf;
327 if (hdr->fio_magic != FIO_HDR_MAGIC)
330 if (hdr->verify_type == VERIFY_MD5)
331 ret = verify_io_u_md5(hdr, io_u);
332 else if (hdr->verify_type == VERIFY_CRC32)
333 ret = verify_io_u_crc32(hdr, io_u);
335 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
342 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
344 hdr->crc32 = crc32(p, len);
347 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
349 struct md5_ctx md5_ctx;
351 memset(&md5_ctx, 0, sizeof(md5_ctx));
352 md5_update(&md5_ctx, p, len);
353 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
356 static int get_rw_ddir(struct thread_data *td)
360 unsigned long elapsed;
362 gettimeofday(&now, NULL);
363 elapsed = mtime_since_now(&td->rwmix_switch);
366 * Check if it's time to seed a new data direction.
368 if (elapsed >= td->rwmixcycle) {
372 lrand48_r(&td->random_state, &r);
373 v = 100UL * r / (unsigned long) (RAND_MAX + 1.0);
374 if (v < td->rwmixread)
375 td->rwmix_ddir = DDIR_READ;
377 td->rwmix_ddir = DDIR_WRITE;
378 memcpy(&td->rwmix_switch, &now, sizeof(now));
380 return td->rwmix_ddir;
381 } else if (td_read(td))
388 * fill body of io_u->buf with random data and add a header with the
389 * (eg) sha1sum of that data.
391 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
393 unsigned char *p = (unsigned char *) io_u->buf;
394 struct verify_header hdr;
396 hdr.fio_magic = FIO_HDR_MAGIC;
397 hdr.len = io_u->buflen;
399 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
401 if (td->verify == VERIFY_MD5) {
402 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
403 hdr.verify_type = VERIFY_MD5;
405 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
406 hdr.verify_type = VERIFY_CRC32;
409 memcpy(io_u->buf, &hdr, sizeof(hdr));
412 static int td_io_prep(struct thread_data *td, struct io_u *io_u)
414 if (td->io_prep && td->io_prep(td, io_u))
420 void put_io_u(struct thread_data *td, struct io_u *io_u)
422 list_del(&io_u->list);
423 list_add(&io_u->list, &td->io_u_freelist);
427 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
430 * If using an iolog, grab next piece if any available.
433 return read_iolog_get(td, io_u);
436 * No log, let the seq/rand engine retrieve the next position.
438 if (!get_next_offset(td, &io_u->offset)) {
439 io_u->buflen = get_next_buflen(td);
442 io_u->ddir = get_rw_ddir(td);
445 * If using a write iolog, store this entry.
448 write_iolog_put(td, io_u);
457 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
459 struct io_u *__get_io_u(struct thread_data *td)
466 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
469 list_del(&io_u->list);
470 list_add(&io_u->list, &td->io_u_busylist);
475 static struct io_u *get_io_u(struct thread_data *td)
479 io_u = __get_io_u(td);
483 if (td->zone_bytes >= td->zone_size) {
485 td->last_pos += td->zone_skip;
488 if (fill_io_u(td, io_u)) {
493 if (io_u->buflen + io_u->offset > td->real_file_size)
494 io_u->buflen = td->real_file_size - io_u->offset;
501 if (!td->read_iolog && !td->sequential)
502 mark_random_map(td, io_u);
504 td->last_pos += io_u->buflen;
506 if (td->verify != VERIFY_NONE)
507 populate_io_u(td, io_u);
509 if (td_io_prep(td, io_u)) {
514 gettimeofday(&io_u->start_time, NULL);
518 static inline void td_set_runstate(struct thread_data *td, int runstate)
520 td->old_runstate = td->runstate;
521 td->runstate = runstate;
524 static int get_next_verify(struct thread_data *td, struct io_u *io_u)
526 struct io_piece *ipo;
528 if (list_empty(&td->io_hist_list))
531 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
532 list_del(&ipo->list);
534 io_u->offset = ipo->offset;
535 io_u->buflen = ipo->len;
536 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);
743 static void do_io(struct thread_data *td)
745 struct io_completion_data icd;
749 while (td->this_io_bytes[td->ddir] < td->io_size) {
750 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
751 struct timespec *timeout;
752 int ret, min_evts = 0;
762 memcpy(&s, &io_u->start_time, sizeof(s));
764 ret = io_u_queue(td, io_u);
771 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
773 if (td->cur_depth < td->iodepth) {
781 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
789 ios_completed(td, &icd);
791 td_verror(td, icd.error);
796 * the rate is batched for now, it should work for batches
797 * of completions except the very first one which may look
800 gettimeofday(&e, NULL);
801 usec = utime_since(&s, &e);
803 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
805 if (check_min_rate(td, &e)) {
806 td_verror(td, ENOMEM);
810 if (runtime_exceeded(td, &e))
814 usec_sleep(td, td->thinktime);
816 if (should_fsync(td) && td->fsync_blocks &&
817 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
822 cleanup_pending_aio(td);
824 if (should_fsync(td) && td->end_fsync)
828 static void cleanup_io(struct thread_data *td)
834 static int init_io(struct thread_data *td)
836 if (td->io_engine == FIO_SYNCIO)
837 return fio_syncio_init(td);
838 else if (td->io_engine == FIO_MMAPIO)
839 return fio_mmapio_init(td);
840 else if (td->io_engine == FIO_LIBAIO)
841 return fio_libaio_init(td);
842 else if (td->io_engine == FIO_POSIXAIO)
843 return fio_posixaio_init(td);
844 else if (td->io_engine == FIO_SGIO)
845 return fio_sgio_init(td);
846 else if (td->io_engine == FIO_SPLICEIO)
847 return fio_spliceio_init(td);
849 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
854 static void cleanup_io_u(struct thread_data *td)
856 struct list_head *entry, *n;
859 list_for_each_safe(entry, n, &td->io_u_freelist) {
860 io_u = list_entry(entry, struct io_u, list);
862 list_del(&io_u->list);
866 if (td->mem_type == MEM_MALLOC)
867 free(td->orig_buffer);
868 else if (td->mem_type == MEM_SHM) {
869 struct shmid_ds sbuf;
871 shmdt(td->orig_buffer);
872 shmctl(td->shm_id, IPC_RMID, &sbuf);
873 } else if (td->mem_type == MEM_MMAP)
874 munmap(td->orig_buffer, td->orig_buffer_size);
876 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
878 td->orig_buffer = NULL;
881 static int init_io_u(struct thread_data *td)
887 if (td->io_engine & FIO_SYNCIO)
890 max_units = td->iodepth;
892 td->orig_buffer_size = td->max_bs * max_units + MASK;
894 if (td->mem_type == MEM_MALLOC)
895 td->orig_buffer = malloc(td->orig_buffer_size);
896 else if (td->mem_type == MEM_SHM) {
897 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
898 if (td->shm_id < 0) {
899 td_verror(td, errno);
904 td->orig_buffer = shmat(td->shm_id, NULL, 0);
905 if (td->orig_buffer == (void *) -1) {
906 td_verror(td, errno);
908 td->orig_buffer = NULL;
911 } else if (td->mem_type == MEM_MMAP) {
912 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
913 if (td->orig_buffer == MAP_FAILED) {
914 td_verror(td, errno);
916 td->orig_buffer = NULL;
921 p = ALIGN(td->orig_buffer);
922 for (i = 0; i < max_units; i++) {
923 io_u = malloc(sizeof(*io_u));
924 memset(io_u, 0, sizeof(*io_u));
925 INIT_LIST_HEAD(&io_u->list);
927 io_u->buf = p + td->max_bs * i;
929 list_add(&io_u->list, &td->io_u_freelist);
935 static void cleanup_allocs(struct thread_data *td)
940 free(td->iolog_file);
942 free(td->exec_prerun);
943 if (td->exec_postrun)
944 free(td->exec_postrun);
946 free(td->ioscheduler);
948 free(td->sysfs_root);
951 static int create_file(struct thread_data *td, unsigned long long size,
954 unsigned long long left;
960 * unless specifically asked for overwrite, let normal io extend it
962 if (td_write(td) && !td->overwrite)
966 fprintf(stderr, "Need size for create\n");
967 td_verror(td, EINVAL);
972 oflags = O_CREAT | O_TRUNC;
973 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
976 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
979 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
981 td_verror(td, errno);
985 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
986 td_verror(td, errno);
990 td->io_size = td->file_size;
991 b = malloc(td->max_bs);
992 memset(b, 0, td->max_bs);
995 while (left && !td->terminate) {
1000 r = write(td->fd, b, bs);
1002 if (r == (int) bs) {
1007 td_verror(td, errno);
1016 unlink(td->file_name);
1017 else if (td->create_fsync)
1026 static int file_size(struct thread_data *td)
1030 if (fstat(td->fd, &st) == -1) {
1031 td_verror(td, errno);
1035 td->real_file_size = st.st_size;
1037 if (!td->file_size || td->file_size > td->real_file_size)
1038 td->file_size = td->real_file_size;
1040 td->file_size -= td->file_offset;
1044 static int bdev_size(struct thread_data *td)
1046 unsigned long long bytes;
1049 r = blockdev_size(td->fd, &bytes);
1055 td->real_file_size = bytes;
1058 * no extend possibilities, so limit size to device size if too large
1060 if (!td->file_size || td->file_size > td->real_file_size)
1061 td->file_size = td->real_file_size;
1063 td->file_size -= td->file_offset;
1067 static int get_file_size(struct thread_data *td)
1071 if (td->filetype == FIO_TYPE_FILE)
1072 ret = file_size(td);
1073 else if (td->filetype == FIO_TYPE_BD)
1074 ret = bdev_size(td);
1076 td->real_file_size = -1;
1081 if (td->file_offset > td->real_file_size) {
1082 fprintf(stderr, "Client%d: offset extends end (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->real_file_size);
1086 td->io_size = td->file_size;
1087 if (td->io_size == 0) {
1088 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1089 td_verror(td, EINVAL);
1094 td->zone_size = td->io_size;
1096 td->total_io_size = td->io_size * td->loops;
1100 static int setup_file_mmap(struct thread_data *td)
1105 flags = PROT_READ | PROT_WRITE;
1106 else if (td_write(td)) {
1109 if (td->verify != VERIFY_NONE)
1114 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1115 if (td->mmap == MAP_FAILED) {
1117 td_verror(td, errno);
1121 if (td->invalidate_cache) {
1122 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1123 td_verror(td, errno);
1128 if (td->sequential) {
1129 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1130 td_verror(td, errno);
1134 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1135 td_verror(td, errno);
1143 static int setup_file_plain(struct thread_data *td)
1145 if (td->invalidate_cache) {
1146 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1147 td_verror(td, errno);
1152 if (td->sequential) {
1153 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1154 td_verror(td, errno);
1158 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1159 td_verror(td, errno);
1167 static int setup_file(struct thread_data *td)
1172 if (stat(td->file_name, &st) == -1) {
1173 if (errno != ENOENT) {
1174 td_verror(td, errno);
1177 if (!td->create_file) {
1178 td_verror(td, ENOENT);
1181 if (create_file(td, td->file_size, 0))
1183 } else if (td->filetype == FIO_TYPE_FILE) {
1184 if (st.st_size < (off_t) td->file_size) {
1185 if (create_file(td, td->file_size - st.st_size, 1))
1193 if (td_write(td) || td_rw(td)) {
1194 if (td->filetype == FIO_TYPE_FILE) {
1205 td->fd = open(td->file_name, flags, 0600);
1207 if (td->filetype == FIO_TYPE_CHAR)
1212 td->fd = open(td->file_name, flags);
1216 td_verror(td, errno);
1220 if (get_file_size(td))
1223 if (td->io_engine != FIO_MMAPIO)
1224 return setup_file_plain(td);
1226 return setup_file_mmap(td);
1229 static int switch_ioscheduler(struct thread_data *td)
1231 char tmp[256], tmp2[128];
1235 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1237 f = fopen(tmp, "r+");
1239 td_verror(td, errno);
1246 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1247 if (ferror(f) || ret != 1) {
1248 td_verror(td, errno);
1256 * Read back and check that the selected scheduler is now the default.
1258 ret = fread(tmp, 1, sizeof(tmp), f);
1259 if (ferror(f) || ret < 0) {
1260 td_verror(td, errno);
1265 sprintf(tmp2, "[%s]", td->ioscheduler);
1266 if (!strstr(tmp, tmp2)) {
1267 fprintf(stderr, "fio: io scheduler %s not found\n", td->ioscheduler);
1268 td_verror(td, EINVAL);
1277 static void clear_io_state(struct thread_data *td)
1279 if (td->io_engine == FIO_SYNCIO)
1280 lseek(td->fd, SEEK_SET, 0);
1283 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1284 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
1288 memset(td->file_map, 0, td->num_maps * sizeof(long));
1291 static void *thread_main(void *data)
1293 struct thread_data *td = data;
1295 if (!td->use_thread)
1300 INIT_LIST_HEAD(&td->io_u_freelist);
1301 INIT_LIST_HEAD(&td->io_u_busylist);
1302 INIT_LIST_HEAD(&td->io_hist_list);
1303 INIT_LIST_HEAD(&td->io_log_list);
1308 if (fio_setaffinity(td) == -1) {
1309 td_verror(td, errno);
1320 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1321 td_verror(td, errno);
1326 if (nice(td->nice) < 0) {
1327 td_verror(td, errno);
1331 if (init_random_state(td))
1334 if (td->ioscheduler && switch_ioscheduler(td))
1337 td_set_runstate(td, TD_INITIALIZED);
1338 sem_post(&startup_sem);
1339 sem_wait(&td->mutex);
1341 if (!td->create_serialize && setup_file(td))
1344 gettimeofday(&td->epoch, NULL);
1346 if (td->exec_prerun)
1347 system(td->exec_prerun);
1349 while (td->loops--) {
1350 getrusage(RUSAGE_SELF, &td->ru_start);
1351 gettimeofday(&td->start, NULL);
1352 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1355 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1358 prune_io_piece_log(td);
1362 td->runtime[td->ddir] += mtime_since_now(&td->start);
1363 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
1364 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1366 update_rusage_stat(td);
1368 if (td->error || td->terminate)
1371 if (td->verify == VERIFY_NONE)
1375 gettimeofday(&td->start, NULL);
1379 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1381 if (td->error || td->terminate)
1386 finish_log(td, td->bw_log, "bw");
1388 finish_log(td, td->slat_log, "slat");
1390 finish_log(td, td->clat_log, "clat");
1391 if (td->write_iolog)
1392 write_iolog_close(td);
1393 if (td->exec_postrun)
1394 system(td->exec_postrun);
1396 if (exitall_on_terminate)
1397 terminate_threads(td->groupid);
1405 munmap(td->mmap, td->file_size);
1409 td_set_runstate(td, TD_EXITED);
1414 static void *fork_main(int shmid, int offset)
1416 struct thread_data *td;
1419 data = shmat(shmid, NULL, 0);
1420 if (data == (void *) -1) {
1425 td = data + offset * sizeof(struct thread_data);
1431 static void check_str_update(struct thread_data *td)
1433 char c = run_str[td->thread_number - 1];
1435 if (td->runstate == td->old_runstate)
1438 switch (td->runstate) {
1451 } else if (td_read(td)) {
1469 case TD_INITIALIZED:
1472 case TD_NOT_CREATED:
1476 printf("state %d\n", td->runstate);
1479 run_str[td->thread_number - 1] = c;
1480 td->old_runstate = td->runstate;
1483 static void eta_to_str(char *str, int eta_sec)
1485 unsigned int d, h, m, s;
1486 static int always_d, always_h;
1498 if (d || always_d) {
1500 str += sprintf(str, "%02dd:", d);
1502 if (h || always_h) {
1504 str += sprintf(str, "%02dh:", h);
1507 str += sprintf(str, "%02dm:", m);
1508 str += sprintf(str, "%02ds", s);
1511 static int thread_eta(struct thread_data *td, unsigned long elapsed)
1513 unsigned long long bytes_total, bytes_done;
1514 unsigned int eta_sec = 0;
1516 bytes_total = td->total_io_size;
1519 * if writing, bytes_total will be twice the size. If mixing,
1520 * assume a 50/50 split and thus bytes_total will be 50% larger.
1524 bytes_total = bytes_total * 3 / 2;
1528 if (td->zone_size && td->zone_skip)
1529 bytes_total /= (td->zone_skip / td->zone_size);
1531 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1534 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1535 perc = (double) bytes_done / (double) bytes_total;
1539 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1541 if (td->timeout && eta_sec > (td->timeout - elapsed))
1542 eta_sec = td->timeout - elapsed;
1543 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1544 || td->runstate == TD_INITIALIZED) {
1545 int t_eta = 0, r_eta = 0;
1548 * We can only guess - assume it'll run the full timeout
1549 * if given, otherwise assume it'll run at the specified rate.
1552 t_eta = td->timeout + td->start_delay - elapsed;
1554 r_eta = (bytes_total / 1024) / td->rate;
1555 r_eta += td->start_delay - elapsed;
1559 eta_sec = min(r_eta, t_eta);
1568 * thread is already done
1576 static void print_thread_status(void)
1578 unsigned long elapsed = time_since_now(&genesis);
1579 int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec;
1583 eta_secs = malloc(thread_number * sizeof(int));
1584 memset(eta_secs, 0, thread_number * sizeof(int));
1586 nr_running = t_rate = m_rate = 0;
1587 for (i = 0; i < thread_number; i++) {
1588 struct thread_data *td = &threads[i];
1590 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){
1593 m_rate += td->ratemin;
1597 eta_secs[i] = thread_eta(td, elapsed);
1599 eta_secs[i] = INT_MAX;
1601 check_str_update(td);
1604 if (exitall_on_terminate)
1609 for (i = 0; i < thread_number; i++) {
1610 if (exitall_on_terminate) {
1611 if (eta_secs[i] < eta_sec)
1612 eta_sec = eta_secs[i];
1614 if (eta_secs[i] > eta_sec)
1615 eta_sec = eta_secs[i];
1619 if (eta_sec != INT_MAX && elapsed) {
1620 perc = (double) elapsed / (double) (elapsed + eta_sec);
1621 eta_to_str(eta_str, eta_sec);
1624 printf("Threads now running (%d)", nr_running);
1625 if (m_rate || t_rate)
1626 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1627 if (eta_sec != INT_MAX) {
1629 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1636 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1641 * reap exited threads (TD_EXITED -> TD_REAPED)
1643 for (i = 0; i < thread_number; i++) {
1644 struct thread_data *td = &threads[i];
1646 if (td->runstate != TD_EXITED)
1649 td_set_runstate(td, TD_REAPED);
1651 if (td->use_thread) {
1654 if (pthread_join(td->thread, (void *) &ret))
1655 perror("thread_join");
1657 waitpid(td->pid, NULL, 0);
1660 (*m_rate) -= td->ratemin;
1661 (*t_rate) -= td->rate;
1665 static void fio_unpin_memory(void *pinned)
1668 if (munlock(pinned, mlock_size) < 0)
1670 munmap(pinned, mlock_size);
1674 static void *fio_pin_memory(void)
1676 long pagesize, pages;
1683 * Don't allow mlock of more than real_mem-128MB
1685 pagesize = sysconf(_SC_PAGESIZE);
1686 pages = sysconf(_SC_PHYS_PAGES);
1687 if (pages != -1 && pagesize != -1) {
1688 unsigned long long real_mem = pages * pagesize;
1690 if ((mlock_size + 128 * 1024 * 1024) > real_mem) {
1691 mlock_size = real_mem - 128 * 1024 * 1024;
1692 printf("fio: limiting mlocked memory to %lluMiB\n",
1697 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1699 perror("malloc locked mem");
1702 if (mlock(ptr, mlock_size) < 0) {
1703 munmap(ptr, mlock_size);
1711 static void run_threads(void)
1713 struct thread_data *td;
1714 unsigned long spent;
1715 int i, todo, nr_running, m_rate, t_rate, nr_started;
1718 mlocked_mem = fio_pin_memory();
1720 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
1723 signal(SIGINT, sig_handler);
1724 signal(SIGALRM, sig_handler);
1726 todo = thread_number;
1729 m_rate = t_rate = 0;
1731 for (i = 0; i < thread_number; i++) {
1734 run_str[td->thread_number - 1] = 'P';
1738 if (!td->create_serialize)
1742 * do file setup here so it happens sequentially,
1743 * we don't want X number of threads getting their
1744 * client data interspersed on disk
1746 if (setup_file(td)) {
1747 td_set_runstate(td, TD_REAPED);
1752 gettimeofday(&genesis, NULL);
1755 struct thread_data *map[MAX_JOBS];
1756 struct timeval this_start;
1757 int this_jobs = 0, left;
1760 * create threads (TD_NOT_CREATED -> TD_CREATED)
1762 for (i = 0; i < thread_number; i++) {
1765 if (td->runstate != TD_NOT_CREATED)
1769 * never got a chance to start, killed by other
1770 * thread for some reason
1772 if (td->terminate) {
1777 if (td->start_delay) {
1778 spent = mtime_since_now(&genesis);
1780 if (td->start_delay * 1000 > spent)
1784 if (td->stonewall && (nr_started || nr_running))
1788 * Set state to created. Thread will transition
1789 * to TD_INITIALIZED when it's done setting up.
1791 td_set_runstate(td, TD_CREATED);
1792 map[this_jobs++] = td;
1793 sem_init(&startup_sem, 0, 1);
1796 if (td->use_thread) {
1797 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1798 perror("thread_create");
1803 sem_wait(&startup_sem);
1805 fork_main(shm_id, i);
1812 * Wait for the started threads to transition to
1815 printf("fio: Waiting for threads to initialize...\n");
1816 gettimeofday(&this_start, NULL);
1819 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1824 for (i = 0; i < this_jobs; i++) {
1828 if (td->runstate == TD_INITIALIZED) {
1831 } else if (td->runstate >= TD_EXITED) {
1835 nr_running++; /* work-around... */
1841 fprintf(stderr, "fio: %d jobs failed to start\n", left);
1842 for (i = 0; i < this_jobs; i++) {
1846 kill(td->pid, SIGTERM);
1852 * start created threads (TD_INITIALIZED -> TD_RUNNING).
1854 printf("fio: Go for launch\n");
1855 for (i = 0; i < thread_number; i++) {
1858 if (td->runstate != TD_INITIALIZED)
1861 td_set_runstate(td, TD_RUNNING);
1864 m_rate += td->ratemin;
1867 sem_post(&td->mutex);
1870 reap_threads(&nr_running, &t_rate, &m_rate);
1876 while (nr_running) {
1877 reap_threads(&nr_running, &t_rate, &m_rate);
1882 fio_unpin_memory(mlocked_mem);
1885 int main(int argc, char *argv[])
1887 if (parse_options(argc, argv))
1890 if (!thread_number) {
1891 printf("Nothing to do\n");
1895 disk_util_timer_arm();