8c26ad7179aa1589720e7d6e3d4e84e4261d3cc8
[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  * It also can act as external C++ engine - compiled with:
8  *
9  * g++ -O2 -g -shared -rdynamic -fPIC -o cpp_null null.c -DFIO_EXTERNAL_ENGINE
10  *
11  * to test it execute:
12  *
13  * LD_LIBRARY_PATH=./engines ./fio examples/cpp_null.fio
14  *
15  */
16 #include <stdlib.h>
17 #include <assert.h>
18
19 #include "../fio.h"
20
21 struct null_data {
22         struct io_u **io_us;
23         int queued;
24         int events;
25 };
26
27 static struct io_u *null_event(struct null_data *nd, int event)
28 {
29         return nd->io_us[event];
30 }
31
32 static int null_getevents(struct null_data *nd, unsigned int min_events,
33                           unsigned int fio_unused max,
34                           const struct timespec fio_unused *t)
35 {
36         int ret = 0;
37
38         if (min_events) {
39                 ret = nd->events;
40                 nd->events = 0;
41         }
42
43         return ret;
44 }
45
46 static int null_commit(struct thread_data *td, struct null_data *nd)
47 {
48         if (!nd->events) {
49 #ifndef FIO_EXTERNAL_ENGINE
50                 io_u_mark_submit(td, nd->queued);
51 #endif
52                 nd->events = nd->queued;
53                 nd->queued = 0;
54         }
55
56         return 0;
57 }
58
59 static int null_queue(struct thread_data *td, struct null_data *nd,
60                       struct io_u *io_u)
61 {
62         fio_ro_check(td, io_u);
63
64         if (td->io_ops->flags & FIO_SYNCIO)
65                 return FIO_Q_COMPLETED;
66         if (nd->events)
67                 return FIO_Q_BUSY;
68
69         nd->io_us[nd->queued++] = io_u;
70         return FIO_Q_QUEUED;
71 }
72
73 static int null_open(struct null_data fio_unused *nd,
74                      struct fio_file fio_unused *f)
75 {
76         return 0;
77 }
78
79 static void null_cleanup(struct null_data *nd)
80 {
81         if (nd) {
82                 free(nd->io_us);
83                 free(nd);
84         }
85 }
86
87 static struct null_data *null_init(struct thread_data *td)
88 {
89         struct null_data *nd = (struct null_data *) malloc(sizeof(*nd));
90
91         memset(nd, 0, sizeof(*nd));
92
93         if (td->o.iodepth != 1) {
94                 nd->io_us = (struct io_u **) malloc(td->o.iodepth * sizeof(struct io_u *));
95                 memset(nd->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
96         } else
97                 td->io_ops->flags |= FIO_SYNCIO;
98
99         return nd;
100 }
101
102 #ifndef __cplusplus
103
104 static struct io_u *fio_null_event(struct thread_data *td, int event)
105 {
106         return null_event(td->io_ops_data, event);
107 }
108
109 static int fio_null_getevents(struct thread_data *td, unsigned int min_events,
110                               unsigned int max, const struct timespec *t)
111 {
112         struct null_data *nd = td->io_ops_data;
113         return null_getevents(nd, min_events, max, t);
114 }
115
116 static int fio_null_commit(struct thread_data *td)
117 {
118         return null_commit(td, td->io_ops_data);
119 }
120
121 static int fio_null_queue(struct thread_data *td, struct io_u *io_u)
122 {
123         return null_queue(td, td->io_ops_data, io_u);
124 }
125
126 static int fio_null_open(struct thread_data *td, struct fio_file *f)
127 {
128         return null_open(td->io_ops_data, f);
129 }
130
131 static void fio_null_cleanup(struct thread_data *td)
132 {
133         null_cleanup(td->io_ops_data);
134 }
135
136 static int fio_null_init(struct thread_data *td)
137 {
138         td->io_ops_data = null_init(td);
139         assert(td->io_ops_data);
140         return 0;
141 }
142
143 static struct ioengine_ops ioengine = {
144         .name           = "null",
145         .version        = FIO_IOOPS_VERSION,
146         .queue          = fio_null_queue,
147         .commit         = fio_null_commit,
148         .getevents      = fio_null_getevents,
149         .event          = fio_null_event,
150         .init           = fio_null_init,
151         .cleanup        = fio_null_cleanup,
152         .open_file      = fio_null_open,
153         .flags          = FIO_DISKLESSIO | FIO_FAKEIO,
154 };
155
156 static void fio_init fio_null_register(void)
157 {
158         register_ioengine(&ioengine);
159 }
160
161 static void fio_exit fio_null_unregister(void)
162 {
163         unregister_ioengine(&ioengine);
164 }
165
166 #else
167
168 #ifdef FIO_EXTERNAL_ENGINE
169
170 struct NullData {
171         NullData(struct thread_data *td)
172         {
173                 impl_ = null_init(td);
174                 assert(impl_);
175         }
176
177         ~NullData()
178         {
179                 null_cleanup(impl_);
180         }
181
182         static NullData *get(struct thread_data *td)
183         {
184                 return reinterpret_cast<NullData *>(td->io_ops_data);
185         }
186
187         io_u *fio_null_event(struct thread_data *, int event)
188         {
189                 return null_event(impl_, event);
190         }
191
192         int fio_null_getevents(struct thread_data *, unsigned int min_events,
193                                unsigned int max, const struct timespec *t)
194         {
195                 return null_getevents(impl_, min_events, max, t);
196         }
197
198         int fio_null_commit(struct thread_data *td)
199         {
200                 return null_commit(td, impl_);
201         }
202
203         int fio_null_queue(struct thread_data *td, struct io_u *io_u)
204         {
205                 return null_queue(td, impl_, io_u);
206         }
207
208         int fio_null_open(struct thread_data *, struct fio_file *f)
209         {
210                 return null_open(impl_, f);
211         }
212
213 private:
214         struct null_data *impl_;
215 };
216
217 extern "C" {
218
219 static struct io_u *fio_null_event(struct thread_data *td, int event)
220 {
221         return NullData::get(td)->fio_null_event(td, event);
222 }
223
224 static int fio_null_getevents(struct thread_data *td, unsigned int min_events,
225                               unsigned int max, const struct timespec *t)
226 {
227         return NullData::get(td)->fio_null_getevents(td, min_events, max, t);
228 }
229
230 static int fio_null_commit(struct thread_data *td)
231 {
232         return NullData::get(td)->fio_null_commit(td);
233 }
234
235 static int fio_null_queue(struct thread_data *td, struct io_u *io_u)
236 {
237         return NullData::get(td)->fio_null_queue(td, io_u);
238 }
239
240 static int fio_null_open(struct thread_data *td, struct fio_file *f)
241 {
242         return NullData::get(td)->fio_null_open(td, f);
243 }
244
245 static int fio_null_init(struct thread_data *td)
246 {
247         td->io_ops_data = new NullData(td);
248         return 0;
249 }
250
251 static void fio_null_cleanup(struct thread_data *td)
252 {
253         delete NullData::get(td);
254 }
255
256 static struct ioengine_ops ioengine;
257 void get_ioengine(struct ioengine_ops **ioengine_ptr)
258 {
259         *ioengine_ptr = &ioengine;
260
261         ioengine.name           = "cpp_null";
262         ioengine.version        = FIO_IOOPS_VERSION;
263         ioengine.queue          = fio_null_queue;
264         ioengine.commit         = fio_null_commit;
265         ioengine.getevents      = fio_null_getevents;
266         ioengine.event          = fio_null_event;
267         ioengine.init           = fio_null_init;
268         ioengine.cleanup        = fio_null_cleanup;
269         ioengine.open_file      = fio_null_open;
270         ioengine.flags          = FIO_DISKLESSIO | FIO_FAKEIO;
271 }
272 }
273 #endif /* FIO_EXTERNAL_ENGINE */
274
275 #endif /* __cplusplus */