engines/filecreate: a few fixes
[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 <unistd.h>
9 #include <fcntl.h>
10 #include <errno.h>
11
12 #include "../fio.h"
13 #include "../filehash.h"
14
15 static int open_file(struct thread_data *td, struct fio_file *f)
16 {
17         struct timespec start, end;
18         int from_hash = 0;
19         int do_lat = !td->o.disable_lat;
20
21         dprint(FD_FILE, "fd open %s\n", f->file_name);
22
23         if (f->filetype != FIO_TYPE_FILE) {
24                 log_err("fio: only files are supported fallocate \n");
25                 return 1;
26         }
27         if (!strcmp(f->file_name, "-")) {
28                 log_err("fio: can't read/write to stdin/out\n");
29                 return 1;
30         }
31
32 open_again:
33         if (do_lat)
34                 fio_gettime(&start, NULL);
35         from_hash = file_lookup_open(f, O_CREAT|O_RDWR);
36         if (do_lat) {
37                 unsigned long long nsec;
38
39                 fio_gettime(&end, NULL);
40                 nsec = ntime_since(&start, &end);
41                 add_lat_sample(td, DDIR_WRITE, nsec, 0, 0);
42         }
43
44         if (f->fd == -1) {
45                 char buf[FIO_VERROR_SIZE];
46                 int e = errno;
47
48                 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
49                 td_verror(td, e, buf);
50         }
51
52         if (!from_hash && f->fd != -1) {
53                 if (add_file_hash(f)) {
54                         int fio_unused ret;
55
56                         /*
57                          * OK to ignore, we haven't done anything with it
58                          */
59                         ret = generic_close_file(td, f);
60                         goto open_again;
61                 }
62         }
63
64         return 0;
65 }
66
67 static int queue_io(struct thread_data *td, struct io_u fio_unused *io_u)
68 {
69         return FIO_Q_COMPLETED;
70 }
71
72 /*
73  * Ensure that we at least have a block size worth of IO to do for each
74  * file. If the job file has td->o.size < nr_files * block_size, then
75  * fio won't do anything.
76  */
77 static int get_file_size(struct thread_data *td, struct fio_file *f)
78 {
79         f->real_file_size = td_min_bs(td);
80         return 0;
81 }
82
83 static struct ioengine_ops ioengine = {
84         .name           = "filecreate",
85         .version        = FIO_IOOPS_VERSION,
86         .queue          = queue_io,
87         .get_file_size  = get_file_size,
88         .open_file      = open_file,
89         .close_file     = generic_close_file,
90         .flags          = FIO_DISKLESSIO | FIO_SYNCIO | FIO_FAKEIO,
91 };
92
93 static void fio_init fio_filecreate_register(void)
94 {
95         register_ioengine(&ioengine);
96 }
97
98 static void fio_exit fio_filecreate_unregister(void)
99 {
100         unregister_ioengine(&ioengine);
101 }