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