-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("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
- } else {
- oflags = O_APPEND;
- printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (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;
-
- /*
- * no extend possibilities, so limit size to device size if too large
- */
- 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 get_file_size(struct thread_data *td)
-{
- int ret = 0;
-
- if (td->filetype == FIO_TYPE_FILE)
- ret = file_size(td);
- else if (td->filetype == FIO_TYPE_BD)
- ret = bdev_size(td);
- else
- td->real_file_size = -1;
-
- if (ret)
- return ret;
-
- if (td->file_offset > td->real_file_size) {
- fprintf(stderr, "Client%d: offset extends end (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->real_file_size);
- return 1;
- }
-
- td->io_size = td->file_size;
- if (td->io_size == 0) {
- fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
- td_verror(td, EINVAL);
- return 1;
- }
-
- if (!td->zone_size)
- td->zone_size = td->io_size;
-
- td->total_io_size = td->io_size * td->loops;
- return 0;
-}
-
-static int setup_file_mmap(struct thread_data *td)
-{
- int flags;
-
- if (td_rw(td))
- flags = PROT_READ | PROT_WRITE;
- else if (td_write(td)) {
- flags = PROT_WRITE;
-
- if (td->verify != VERIFY_NONE)
- flags |= PROT_READ;
- } else
- flags = PROT_READ;
-
- td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
- if (td->mmap == MAP_FAILED) {
- td->mmap = NULL;
- td_verror(td, errno);
- return 1;
- }
-
- if (td->invalidate_cache) {
- if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
- td_verror(td, errno);
- return 1;
- }
- }
-
- if (td->sequential) {
- if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
- td_verror(td, errno);
- return 1;
- }
- } else {
- if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
- td_verror(td, errno);
- return 1;
- }
- }
-
- return 0;
-}
-
-static int setup_file_plain(struct thread_data *td)
-{
- if (td->invalidate_cache) {
- if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
- td_verror(td, errno);
- return 1;
- }
- }
-
- if (td->sequential) {
- if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
- td_verror(td, errno);
- return 1;
- }
- } else {
- if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
- td_verror(td, errno);
- return 1;
- }
- }
-
- return 0;
-}
-
-static int setup_file(struct thread_data *td)
-{
- struct stat st;
- int flags = 0;
-
- if (stat(td->file_name, &st) == -1) {
- if (errno != ENOENT) {
- td_verror(td, errno);
- return 1;
- }
- if (!td->create_file) {
- td_verror(td, ENOENT);
- return 1;
- }
- if (create_file(td, td->file_size, 0))
- return 1;
- } else if (td->filetype == FIO_TYPE_FILE) {
- if (st.st_size < (off_t) td->file_size) {
- if (create_file(td, td->file_size - st.st_size, 1))
- return 1;
- }
- }
-
- if (td->odirect)
- flags |= O_DIRECT;
-
- if (td_write(td) || td_rw(td)) {
- if (td->filetype == FIO_TYPE_FILE) {
- if (!td->overwrite)
- flags |= O_TRUNC;
-
- flags |= O_CREAT;
- }
- if (td->sync_io)
- flags |= O_SYNC;
-
- flags |= O_RDWR;
-
- td->fd = open(td->file_name, flags, 0600);
- } else {
- if (td->filetype == FIO_TYPE_CHAR)
- flags |= O_RDWR;
- else
- flags |= O_RDONLY;
-
- td->fd = open(td->file_name, flags);
- }
-
- if (td->fd == -1) {
- td_verror(td, errno);
- return 1;
- }
-
- if (get_file_size(td))
- return 1;
-
- if (td->io_engine != FIO_MMAPIO)
- return setup_file_plain(td);
- else
- return setup_file_mmap(td);
-}
-
-static int check_dev_match(dev_t dev, char *path)
-{
- unsigned int major, minor;
- char line[256], *p;
- FILE *f;
-
- f = fopen(path, "r");
- if (!f) {
- perror("open path");
- return 1;
- }
-
- p = fgets(line, sizeof(line), f);
- if (!p) {
- fclose(f);
- return 1;
- }
-
- if (sscanf(p, "%u:%u", &major, &minor) != 2) {
- fclose(f);
- return 1;
- }
-
- if (((major << 8) | minor) == dev) {
- fclose(f);
- return 0;
- }
-
- fclose(f);
- return 1;
-}
-
-static int find_block_dir(dev_t dev, char *path)
-{
- struct dirent *dir;
- struct stat st;
- int found = 0;
- DIR *D;
-
- D = opendir(path);
- if (!D)
- return 0;
-
- while ((dir = readdir(D)) != NULL) {
- char full_path[256];
-
- if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
- continue;
- if (!strcmp(dir->d_name, "device"))
- continue;
-
- sprintf(full_path, "%s/%s", path, dir->d_name);
-
- if (!strcmp(dir->d_name, "dev")) {
- if (!check_dev_match(dev, full_path)) {
- found = 1;
- break;
- }
- }
-
- if (stat(full_path, &st) == -1) {
- perror("stat");
- break;
- }
-
- if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
- continue;
-
- found = find_block_dir(dev, full_path);
- if (found) {
- strcpy(path, full_path);
- break;
- }
- }
-
- closedir(D);
- return found;
-}
-
-static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
-{
- unsigned in_flight;
- char line[256];
- FILE *f;
- char *p;
-
- f = fopen(du->path, "r");
- if (!f)
- return 1;
-
- p = fgets(line, sizeof(line), f);
- if (!p) {
- fclose(f);
- return 1;
- }
-
- if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) {
- fclose(f);
- return 1;
- }
-
- fclose(f);
- return 0;
-}
-
-static void update_io_tick_disk(struct disk_util *du)
-{
- struct disk_util_stat __dus, *dus, *ldus;
- struct timeval t;
-
- if (get_io_ticks(du, &__dus))
- return;
-
- dus = &du->dus;
- ldus = &du->last_dus;
-
- dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
- dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
- dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
- dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
- dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
- dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
- dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
- dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
- dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
- dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
-
- gettimeofday(&t, NULL);
- du->msec += mtime_since(&du->time, &t);
- memcpy(&du->time, &t, sizeof(t));
- memcpy(ldus, &__dus, sizeof(__dus));
-}
-
-static void update_io_ticks(void)
-{
- struct list_head *entry;
- struct disk_util *du;
-
- list_for_each(entry, &disk_list) {
- du = list_entry(entry, struct disk_util, list);
- update_io_tick_disk(du);
- }
-}
-
-static int disk_util_exists(dev_t dev)
-{
- struct list_head *entry;
- struct disk_util *du;
-
- list_for_each(entry, &disk_list) {
- du = list_entry(entry, struct disk_util, list);
-
- if (du->dev == dev)
- return 1;
- }
-
- return 0;
-}
-
-static void disk_util_add(dev_t dev, char *path)
-{
- struct disk_util *du = malloc(sizeof(*du));
-
- memset(du, 0, sizeof(*du));
- INIT_LIST_HEAD(&du->list);
- sprintf(du->path, "%s/stat", path);
- du->name = strdup(basename(path));
- du->dev = dev;
-
- gettimeofday(&du->time, NULL);
- get_io_ticks(du, &du->last_dus);
-
- list_add_tail(&du->list, &disk_list);
-}
-
-static void init_disk_util(struct thread_data *td)
-{
- struct stat st;
- char foo[256], tmp[256];
- dev_t dev;
- char *p;
-
- if (!td->do_disk_util)
- return;
-
- if (!stat(td->file_name, &st)) {
- if (S_ISBLK(st.st_mode))
- dev = st.st_rdev;
- else
- dev = st.st_dev;
- } else {
- /*
- * must be a file, open "." in that path
- */
- strcpy(foo, td->file_name);
- p = dirname(foo);
- if (stat(p, &st)) {
- perror("disk util stat");
- return;
- }
-
- dev = st.st_dev;
- }
-
- if (disk_util_exists(dev))
- return;
-
- sprintf(foo, "/sys/block");
- if (!find_block_dir(dev, foo))
- return;
-
- /*
- * If there's a ../queue/ directory there, we are inside a partition.
- * Check if that is the case and jump back. For loop/md/dm etc we
- * are already in the right spot.
- */
- sprintf(tmp, "%s/../queue", foo);
- if (!stat(tmp, &st)) {
- p = dirname(foo);
- sprintf(tmp, "%s/queue", p);
- if (stat(tmp, &st)) {
- fprintf(stderr, "unknown sysfs layout\n");
- return;
- }
- sprintf(foo, "%s", p);
- }
-
- td->sysfs_root = strdup(foo);
- disk_util_add(dev, foo);
-}
-
-static int switch_ioscheduler(struct thread_data *td)
-{
- char tmp[256], tmp2[128];
- FILE *f;
- int ret;
-
- sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);