284aaf398328177d11bcf9fd549090aa42e7d5c1
[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 static struct ioengine_ops ioengine = {
73         .name           = "filecreate",
74         .version        = FIO_IOOPS_VERSION,
75         .open_file      = open_file,
76         .queue          = queue_io,
77         .close_file     = generic_close_file,
78         .flags          = FIO_DISKLESSIO | FIO_SYNCIO | FIO_FAKEIO,
79 };
80
81 static void fio_init fio_filecreate_register(void)
82 {
83         register_ioengine(&ioengine);
84 }
85
86 static void fio_exit fio_filecreate_unregister(void)
87 {
88         unregister_ioengine(&ioengine);
89 }