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