| 1 | /* |
| 2 | * filecreate engine |
| 3 | * |
| 4 | * IO engine that doesn't do any IO, just creates files and tracks the latency |
| 5 | * of the file creation. |
| 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <unistd.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <errno.h> |
| 11 | |
| 12 | #include "../fio.h" |
| 13 | #include "../filehash.h" |
| 14 | |
| 15 | struct fc_data { |
| 16 | enum fio_ddir stat_ddir; |
| 17 | }; |
| 18 | |
| 19 | static int open_file(struct thread_data *td, struct fio_file *f) |
| 20 | { |
| 21 | struct timespec start; |
| 22 | int do_lat = !td->o.disable_lat; |
| 23 | |
| 24 | dprint(FD_FILE, "fd open %s\n", f->file_name); |
| 25 | |
| 26 | if (f->filetype != FIO_TYPE_FILE) { |
| 27 | log_err("fio: only files are supported fallocate \n"); |
| 28 | return 1; |
| 29 | } |
| 30 | if (!strcmp(f->file_name, "-")) { |
| 31 | log_err("fio: can't read/write to stdin/out\n"); |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | if (do_lat) |
| 36 | fio_gettime(&start, NULL); |
| 37 | |
| 38 | f->fd = open(f->file_name, O_CREAT|O_RDWR, 0600); |
| 39 | |
| 40 | if (f->fd == -1) { |
| 41 | char buf[FIO_VERROR_SIZE]; |
| 42 | int e = errno; |
| 43 | |
| 44 | snprintf(buf, sizeof(buf), "open(%s)", f->file_name); |
| 45 | td_verror(td, e, buf); |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | if (do_lat) { |
| 50 | struct fc_data *data = td->io_ops_data; |
| 51 | uint64_t nsec; |
| 52 | |
| 53 | nsec = ntime_since_now(&start); |
| 54 | add_clat_sample(td, data->stat_ddir, nsec, 0, 0); |
| 55 | } |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int queue_io(struct thread_data *td, struct io_u fio_unused *io_u) |
| 61 | { |
| 62 | return FIO_Q_COMPLETED; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Ensure that we at least have a block size worth of IO to do for each |
| 67 | * file. If the job file has td->o.size < nr_files * block_size, then |
| 68 | * fio won't do anything. |
| 69 | */ |
| 70 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
| 71 | { |
| 72 | f->real_file_size = td_min_bs(td); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int init(struct thread_data *td) |
| 77 | { |
| 78 | struct fc_data *data; |
| 79 | |
| 80 | data = calloc(1, sizeof(*data)); |
| 81 | |
| 82 | if (td_read(td)) |
| 83 | data->stat_ddir = DDIR_READ; |
| 84 | else if (td_write(td)) |
| 85 | data->stat_ddir = DDIR_WRITE; |
| 86 | |
| 87 | td->io_ops_data = data; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static void cleanup(struct thread_data *td) |
| 92 | { |
| 93 | struct fc_data *data = td->io_ops_data; |
| 94 | |
| 95 | free(data); |
| 96 | } |
| 97 | |
| 98 | static struct ioengine_ops ioengine = { |
| 99 | .name = "filecreate", |
| 100 | .version = FIO_IOOPS_VERSION, |
| 101 | .init = init, |
| 102 | .cleanup = cleanup, |
| 103 | .queue = queue_io, |
| 104 | .get_file_size = get_file_size, |
| 105 | .open_file = open_file, |
| 106 | .close_file = generic_close_file, |
| 107 | .flags = FIO_DISKLESSIO | FIO_SYNCIO | FIO_FAKEIO | |
| 108 | FIO_NOSTATS | FIO_NOFILEHASH, |
| 109 | }; |
| 110 | |
| 111 | static void fio_init fio_filecreate_register(void) |
| 112 | { |
| 113 | register_ioengine(&ioengine); |
| 114 | } |
| 115 | |
| 116 | static void fio_exit fio_filecreate_unregister(void) |
| 117 | { |
| 118 | unregister_ioengine(&ioengine); |
| 119 | } |