Refactor #includes and headers
[fio.git] / engines / filecreate.c
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 int queue_io(struct thread_data *td, struct io_u fio_unused *io_u)
59 {
60         return FIO_Q_COMPLETED;
61 }
62
63 /*
64  * Ensure that we at least have a block size worth of IO to do for each
65  * file. If the job file has td->o.size < nr_files * block_size, then
66  * fio won't do anything.
67  */
68 static int get_file_size(struct thread_data *td, struct fio_file *f)
69 {
70         f->real_file_size = td_min_bs(td);
71         return 0;
72 }
73
74 static int init(struct thread_data *td)
75 {
76         struct fc_data *data;
77
78         data = calloc(1, sizeof(*data));
79
80         if (td_read(td))
81                 data->stat_ddir = DDIR_READ;
82         else if (td_write(td))
83                 data->stat_ddir = DDIR_WRITE;
84
85         td->io_ops_data = data;
86         return 0;
87 }
88
89 static void cleanup(struct thread_data *td)
90 {
91         struct fc_data *data = td->io_ops_data;
92
93         free(data);
94 }
95
96 static struct ioengine_ops ioengine = {
97         .name           = "filecreate",
98         .version        = FIO_IOOPS_VERSION,
99         .init           = init,
100         .cleanup        = cleanup,
101         .queue          = queue_io,
102         .get_file_size  = get_file_size,
103         .open_file      = open_file,
104         .close_file     = generic_close_file,
105         .flags          = FIO_DISKLESSIO | FIO_SYNCIO | FIO_FAKEIO |
106                                 FIO_NOSTATS | FIO_NOFILEHASH,
107 };
108
109 static void fio_init fio_filecreate_register(void)
110 {
111         register_ioengine(&ioengine);
112 }
113
114 static void fio_exit fio_filecreate_unregister(void)
115 {
116         unregister_ioengine(&ioengine);
117 }