- td_verror(td, ret);
- break;
- } else if (!ret)
- continue;
-
- icd.nr = ret;
- ios_completed(td, &icd);
- if (icd.error) {
- td_verror(td, icd.error);
- break;
- }
-
- /*
- * the rate is batched for now, it should work for batches
- * of completions except the very first one which may look
- * a little bursty
- */
- gettimeofday(&e, NULL);
- usec = utime_since(&s, &e);
-
- rate_throttle(td, usec, icd.bytes_done[td->ddir]);
-
- if (check_min_rate(td, &e)) {
- td_verror(td, ENOMEM);
- break;
- }
-
- if (runtime_exceeded(td, &e))
- break;
-
- if (td->thinktime)
- usec_sleep(td, td->thinktime);
-
- if (should_fsync(td) && td->fsync_blocks &&
- (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
- sync_td(td);
- }
-
- if (td->cur_depth)
- cleanup_pending_aio(td);
-
- if (should_fsync(td) && td->end_fsync)
- sync_td(td);
-}
-
-static void cleanup_io(struct thread_data *td)
-{
- if (td->io_cleanup)
- td->io_cleanup(td);
-}
-
-static int init_io(struct thread_data *td)
-{
- if (td->io_engine == FIO_SYNCIO)
- return fio_syncio_init(td);
- else if (td->io_engine == FIO_MMAPIO)
- return fio_mmapio_init(td);
- else if (td->io_engine == FIO_LIBAIO)
- return fio_libaio_init(td);
- else if (td->io_engine == FIO_POSIXAIO)
- return fio_posixaio_init(td);
- else if (td->io_engine == FIO_SGIO)
- return fio_sgio_init(td);
- else if (td->io_engine == FIO_SPLICEIO)
- return fio_spliceio_init(td);
- else {
- fprintf(stderr, "bad io_engine %d\n", td->io_engine);
- return 1;
- }
-}
-
-static void cleanup_io_u(struct thread_data *td)
-{
- struct list_head *entry, *n;
- struct io_u *io_u;
-
- list_for_each_safe(entry, n, &td->io_u_freelist) {
- io_u = list_entry(entry, struct io_u, list);
-
- list_del(&io_u->list);
- free(io_u);
- }
-
- if (td->mem_type == MEM_MALLOC)
- free(td->orig_buffer);
- else if (td->mem_type == MEM_SHM) {
- struct shmid_ds sbuf;
-
- shmdt(td->orig_buffer);
- shmctl(td->shm_id, IPC_RMID, &sbuf);
- } else if (td->mem_type == MEM_MMAP)
- munmap(td->orig_buffer, td->orig_buffer_size);
- else
- fprintf(stderr, "Bad memory type %d\n", td->mem_type);
-
- td->orig_buffer = NULL;
-}
-
-static int init_io_u(struct thread_data *td)
-{
- struct io_u *io_u;
- int i, max_units;
- char *p;
-
- if (td->io_engine & FIO_SYNCIO)
- max_units = 1;
- else
- max_units = td->iodepth;
-
- td->orig_buffer_size = td->max_bs * max_units + MASK;
-
- if (td->mem_type == MEM_MALLOC)
- td->orig_buffer = malloc(td->orig_buffer_size);
- else if (td->mem_type == MEM_SHM) {
- td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
- if (td->shm_id < 0) {
- td_verror(td, errno);
- perror("shmget");
- return 1;
- }
-
- td->orig_buffer = shmat(td->shm_id, NULL, 0);
- if (td->orig_buffer == (void *) -1) {
- td_verror(td, errno);
- perror("shmat");
- td->orig_buffer = NULL;
- return 1;
- }
- } else if (td->mem_type == MEM_MMAP) {
- td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
- if (td->orig_buffer == MAP_FAILED) {
- td_verror(td, errno);
- perror("mmap");
- td->orig_buffer = NULL;
- return 1;
- }
- }
-
- p = ALIGN(td->orig_buffer);
- for (i = 0; i < max_units; i++) {
- io_u = malloc(sizeof(*io_u));
- memset(io_u, 0, sizeof(*io_u));
- INIT_LIST_HEAD(&io_u->list);
-
- io_u->buf = p + td->max_bs * i;
- io_u->index = i;
- list_add(&io_u->list, &td->io_u_freelist);
- }
-
- return 0;
-}
-
-static void cleanup_allocs(struct thread_data *td)
-{
- if (td->directory)
- free(td->directory);
- if (td->iolog_file)
- free(td->iolog_file);
- if (td->exec_prerun)
- free(td->exec_prerun);
- if (td->exec_postrun)
- free(td->exec_postrun);
- if (td->ioscheduler)
- free(td->ioscheduler);
- if (td->sysfs_root)
- free(td->sysfs_root);
-}
-
-static int create_file(struct thread_data *td, unsigned long long size,
- int extend)
-{
- unsigned long long left;
- unsigned int bs;
- int r, oflags;
- char *b;
-
- /*
- * unless specifically asked for overwrite, let normal io extend it
- */
- if (td_write(td) && !td->overwrite)
- return 0;
-
- if (!size) {
- fprintf(stderr, "Need size for create\n");
- td_verror(td, EINVAL);
- return 1;
- }
-
- if (!extend) {
- oflags = O_CREAT | O_TRUNC;
- printf("%s: Laying out IO file (%LuMiB)\n", td->name, size >> 20);
- } else {
- oflags = O_APPEND;
- printf("%s: Extending IO file (%Lu -> %LuMiB)\n", td->name, (td->file_size - size) >> 20, td->file_size >> 20);
- }
-
- td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
- if (td->fd < 0) {
- td_verror(td, errno);
- return 1;
- }
-
- if (!extend && ftruncate(td->fd, td->file_size) == -1) {
- td_verror(td, errno);
- return 1;
- }
-
- td->io_size = td->file_size;
- b = malloc(td->max_bs);
- memset(b, 0, td->max_bs);
-
- left = size;
- while (left && !td->terminate) {
- bs = td->max_bs;
- if (bs > left)
- bs = left;
-
- r = write(td->fd, b, bs);
-
- if (r == (int) bs) {
- left -= bs;
- continue;
- } else {
- if (r < 0)
- td_verror(td, errno);
- else
- td_verror(td, EIO);
-
- break;
- }
- }
-
- if (td->terminate)
- unlink(td->file_name);
- else if (td->create_fsync)
- fsync(td->fd);
-
- close(td->fd);
- td->fd = -1;
- free(b);
- return 0;
-}
-
-static int file_size(struct thread_data *td)
-{
- struct stat st;
-
- if (fstat(td->fd, &st) == -1) {
- td_verror(td, errno);
- return 1;
- }
-
- td->real_file_size = st.st_size;
-
- if (!td->file_size || td->file_size > td->real_file_size)
- td->file_size = td->real_file_size;
-
- td->file_size -= td->file_offset;
- return 0;
-}
-
-static int bdev_size(struct thread_data *td)
-{
- unsigned long long bytes;
- int r;
-
- r = blockdev_size(td->fd, &bytes);
- if (r) {
- td_verror(td, r);
- return 1;
- }
-
- td->real_file_size = bytes;