Revamp the file creation code
[fio.git] / engines / null.c
1 /*
2  * null engine
3  *
4  * IO engine that doesn't do any real IO transfers, it just pretends to.
5  * The main purpose is to test fio itself.
6  *
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <assert.h>
13
14 #include "../fio.h"
15 #include "../os.h"
16
17 struct null_data {
18         struct io_u **io_us;
19         int queued;
20         int events;
21 };
22
23 static struct io_u *fio_null_event(struct thread_data *td, int event)
24 {
25         struct null_data *nd = td->io_ops->data;
26
27         return nd->io_us[event];
28 }
29
30 static int fio_null_getevents(struct thread_data *td, int min_events,
31                               int fio_unused max, struct timespec fio_unused *t)
32 {
33         struct null_data *nd = td->io_ops->data;
34         int ret = 0;
35         
36         if (min_events) {
37                 ret = nd->events;
38                 nd->events = 0;
39         }
40
41         return ret;
42 }
43
44 static int fio_null_commit(struct thread_data *td)
45 {
46         struct null_data *nd = td->io_ops->data;
47
48         if (!nd->events) {
49                 nd->events = nd->queued;
50                 nd->queued = 0;
51         }
52
53         return 0;
54 }
55
56 static int fio_null_queue(struct thread_data fio_unused *td, struct io_u *io_u)
57 {
58         struct null_data *nd = td->io_ops->data;
59
60         if (td->io_ops->flags & FIO_SYNCIO)
61                 return FIO_Q_COMPLETED;
62         if (nd->events)
63                 return FIO_Q_BUSY;
64
65         nd->io_us[nd->queued++] = io_u;
66         return FIO_Q_QUEUED;
67 }
68
69 static int fio_null_setup(struct thread_data *td)
70 {
71         struct fio_file *f;
72         unsigned int i;
73
74         for_each_file(td, f, i)
75                 f->real_file_size = -1ULL;
76
77         return 0;
78 }
79
80 static int fio_null_open(struct thread_data fio_unused *td,
81                          struct fio_file fio_unused *f)
82 {
83         f->fd = 0;
84         return 0;
85 }
86
87 static void fio_null_cleanup(struct thread_data *td)
88 {
89         struct null_data *nd = td->io_ops->data;
90
91         if (nd) {
92                 if (nd->io_us)
93                         free(nd->io_us);
94                 free(nd);
95                 td->io_ops->data = NULL;
96         }
97 }
98
99 static int fio_null_init(struct thread_data *td)
100 {
101         struct null_data *nd = malloc(sizeof(*nd));
102
103         memset(nd, 0, sizeof(*nd));
104
105         if (td->o.iodepth != 1) {
106                 nd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
107                 memset(nd->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
108         } else
109                 td->io_ops->flags |= FIO_SYNCIO;
110
111         td->io_ops->data = nd;
112         return 0;
113 }
114
115 static struct ioengine_ops ioengine = {
116         .name           = "null",
117         .version        = FIO_IOOPS_VERSION,
118         .setup          = fio_null_setup,
119         .queue          = fio_null_queue,
120         .commit         = fio_null_commit,
121         .getevents      = fio_null_getevents,
122         .event          = fio_null_event,
123         .init           = fio_null_init,
124         .cleanup        = fio_null_cleanup,
125         .open_file      = fio_null_open,
126         .flags          = FIO_DISKLESSIO,
127 };
128
129 static void fio_init fio_null_register(void)
130 {
131         register_ioengine(&ioengine);
132 }
133
134 static void fio_exit fio_null_unregister(void)
135 {
136         unregister_ioengine(&ioengine);
137 }