Commit | Line | Data |
---|---|---|
a94ea28b | 1 | /* |
da751ca9 JA |
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. | |
a94ea28b | 6 | * |
46a67478 DG |
7 | * It also can act as external C++ engine - compiled with: |
8 | * | |
966fcbd4 | 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 | |
46a67478 | 14 | * |
a94ea28b JA |
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" | |
a94ea28b | 23 | |
65afa5f2 JA |
24 | struct null_data { |
25 | struct io_u **io_us; | |
26 | int queued; | |
27 | int events; | |
28 | }; | |
29 | ||
966fcbd4 | 30 | static struct io_u *null_event(struct null_data *nd, int event) |
65afa5f2 | 31 | { |
65afa5f2 JA |
32 | return nd->io_us[event]; |
33 | } | |
34 | ||
966fcbd4 | 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) | |
65afa5f2 | 38 | { |
7401c088 | 39 | int ret = 0; |
966fcbd4 | 40 | |
7401c088 JA |
41 | if (min_events) { |
42 | ret = nd->events; | |
43 | nd->events = 0; | |
44 | } | |
65afa5f2 | 45 | |
65afa5f2 JA |
46 | return ret; |
47 | } | |
48 | ||
966fcbd4 | 49 | static int null_commit(struct thread_data *td, struct null_data *nd) |
65afa5f2 | 50 | { |
ed8bd849 | 51 | if (!nd->events) { |
46a67478 | 52 | #ifndef FIO_EXTERNAL_ENGINE |
838bc709 | 53 | io_u_mark_submit(td, nd->queued); |
46a67478 | 54 | #endif |
ed8bd849 JA |
55 | nd->events = nd->queued; |
56 | nd->queued = 0; | |
57 | } | |
58 | ||
65afa5f2 JA |
59 | return 0; |
60 | } | |
61 | ||
966fcbd4 | 62 | static int null_queue(struct thread_data *td, struct null_data *nd, |
63 | struct io_u *io_u) | |
a94ea28b | 64 | { |
7101d9c2 JA |
65 | fio_ro_check(td, io_u); |
66 | ||
65afa5f2 JA |
67 | if (td->io_ops->flags & FIO_SYNCIO) |
68 | return FIO_Q_COMPLETED; | |
ed8bd849 JA |
69 | if (nd->events) |
70 | return FIO_Q_BUSY; | |
65afa5f2 JA |
71 | |
72 | nd->io_us[nd->queued++] = io_u; | |
73 | return FIO_Q_QUEUED; | |
a94ea28b JA |
74 | } |
75 | ||
966fcbd4 | 76 | static int null_open(struct null_data fio_unused *nd, |
77 | struct fio_file fio_unused *f) | |
b5af8293 JA |
78 | { |
79 | return 0; | |
80 | } | |
81 | ||
966fcbd4 | 82 | static void null_cleanup(struct null_data *nd) |
65afa5f2 | 83 | { |
65afa5f2 | 84 | if (nd) { |
02a3d83f | 85 | free(nd->io_us); |
65afa5f2 | 86 | free(nd); |
65afa5f2 JA |
87 | } |
88 | } | |
89 | ||
7746976c | 90 | static struct null_data *null_init(struct thread_data *td) |
65afa5f2 | 91 | { |
7746976c | 92 | struct null_data *nd = (struct null_data *) malloc(sizeof(*nd)); |
65afa5f2 JA |
93 | |
94 | memset(nd, 0, sizeof(*nd)); | |
95 | ||
2dc1bbeb | 96 | if (td->o.iodepth != 1) { |
46a67478 | 97 | nd->io_us = (struct io_u **) malloc(td->o.iodepth * sizeof(struct io_u *)); |
2dc1bbeb | 98 | memset(nd->io_us, 0, td->o.iodepth * sizeof(struct io_u *)); |
65afa5f2 JA |
99 | } else |
100 | td->io_ops->flags |= FIO_SYNCIO; | |
101 | ||
7746976c | 102 | return nd; |
65afa5f2 JA |
103 | } |
104 | ||
46a67478 | 105 | #ifndef __cplusplus |
966fcbd4 | 106 | |
107 | static struct io_u *fio_null_event(struct thread_data *td, int event) | |
108 | { | |
ca65714c | 109 | return null_event(td->io_ops_data, event); |
966fcbd4 | 110 | } |
111 | ||
112 | static int fio_null_getevents(struct thread_data *td, unsigned int min_events, | |
113 | unsigned int max, const struct timespec *t) | |
114 | { | |
ca65714c | 115 | struct null_data *nd = td->io_ops_data; |
966fcbd4 | 116 | return null_getevents(nd, min_events, max, t); |
117 | } | |
118 | ||
119 | static int fio_null_commit(struct thread_data *td) | |
120 | { | |
ca65714c | 121 | return null_commit(td, td->io_ops_data); |
966fcbd4 | 122 | } |
123 | ||
124 | static int fio_null_queue(struct thread_data *td, struct io_u *io_u) | |
125 | { | |
ca65714c | 126 | return null_queue(td, td->io_ops_data, io_u); |
966fcbd4 | 127 | } |
128 | ||
129 | static int fio_null_open(struct thread_data *td, struct fio_file *f) | |
130 | { | |
ca65714c | 131 | return null_open(td->io_ops_data, f); |
966fcbd4 | 132 | } |
133 | ||
134 | static void fio_null_cleanup(struct thread_data *td) | |
135 | { | |
ca65714c | 136 | null_cleanup(td->io_ops_data); |
966fcbd4 | 137 | } |
138 | ||
139 | static int fio_null_init(struct thread_data *td) | |
140 | { | |
7746976c TK |
141 | td->io_ops_data = null_init(td); |
142 | assert(td->io_ops_data); | |
143 | return 0; | |
966fcbd4 | 144 | } |
145 | ||
a94ea28b JA |
146 | static struct ioengine_ops ioengine = { |
147 | .name = "null", | |
148 | .version = FIO_IOOPS_VERSION, | |
a94ea28b | 149 | .queue = fio_null_queue, |
65afa5f2 JA |
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, | |
b5af8293 | 155 | .open_file = fio_null_open, |
5c57c084 | 156 | .flags = FIO_DISKLESSIO | FIO_FAKEIO, |
a94ea28b JA |
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 | } | |
46a67478 DG |
168 | |
169 | #else | |
170 | ||
171 | #ifdef FIO_EXTERNAL_ENGINE | |
966fcbd4 | 172 | |
173 | struct NullData { | |
174 | NullData(struct thread_data *td) | |
175 | { | |
7746976c TK |
176 | impl_ = null_init(td); |
177 | assert(impl_); | |
966fcbd4 | 178 | } |
179 | ||
180 | ~NullData() | |
181 | { | |
182 | null_cleanup(impl_); | |
183 | } | |
184 | ||
185 | static NullData *get(struct thread_data *td) | |
186 | { | |
187 | return reinterpret_cast<NullData *>(td->io_ops_data); | |
188 | } | |
189 | ||
190 | io_u *fio_null_event(struct thread_data *, int event) | |
191 | { | |
192 | return null_event(impl_, event); | |
193 | } | |
194 | ||
195 | int fio_null_getevents(struct thread_data *, unsigned int min_events, | |
196 | unsigned int max, const struct timespec *t) | |
197 | { | |
198 | return null_getevents(impl_, min_events, max, t); | |
199 | } | |
200 | ||
201 | int fio_null_commit(struct thread_data *td) | |
202 | { | |
203 | return null_commit(td, impl_); | |
204 | } | |
205 | ||
206 | int fio_null_queue(struct thread_data *td, struct io_u *io_u) | |
207 | { | |
208 | return null_queue(td, impl_, io_u); | |
209 | } | |
210 | ||
211 | int fio_null_open(struct thread_data *, struct fio_file *f) | |
212 | { | |
213 | return null_open(impl_, f); | |
214 | } | |
215 | ||
7caf10f1 | 216 | private: |
966fcbd4 | 217 | struct null_data *impl_; |
218 | }; | |
219 | ||
46a67478 | 220 | extern "C" { |
966fcbd4 | 221 | |
222 | static struct io_u *fio_null_event(struct thread_data *td, int event) | |
223 | { | |
224 | return NullData::get(td)->fio_null_event(td, event); | |
225 | } | |
226 | ||
227 | static int fio_null_getevents(struct thread_data *td, unsigned int min_events, | |
228 | unsigned int max, const struct timespec *t) | |
229 | { | |
230 | return NullData::get(td)->fio_null_getevents(td, min_events, max, t); | |
231 | } | |
232 | ||
233 | static int fio_null_commit(struct thread_data *td) | |
234 | { | |
235 | return NullData::get(td)->fio_null_commit(td); | |
236 | } | |
237 | ||
238 | static int fio_null_queue(struct thread_data *td, struct io_u *io_u) | |
239 | { | |
240 | return NullData::get(td)->fio_null_queue(td, io_u); | |
241 | } | |
242 | ||
243 | static int fio_null_open(struct thread_data *td, struct fio_file *f) | |
244 | { | |
245 | return NullData::get(td)->fio_null_open(td, f); | |
246 | } | |
247 | ||
248 | static int fio_null_init(struct thread_data *td) | |
249 | { | |
250 | td->io_ops_data = new NullData(td); | |
251 | return 0; | |
252 | } | |
253 | ||
254 | static void fio_null_cleanup(struct thread_data *td) | |
255 | { | |
256 | delete NullData::get(td); | |
257 | } | |
258 | ||
247f299a | 259 | static struct ioengine_ops ioengine; |
8d6ecac2 JA |
260 | void get_ioengine(struct ioengine_ops **ioengine_ptr) |
261 | { | |
247f299a TK |
262 | *ioengine_ptr = &ioengine; |
263 | ||
264 | ioengine.name = "cpp_null"; | |
265 | ioengine.version = FIO_IOOPS_VERSION; | |
266 | ioengine.queue = fio_null_queue; | |
267 | ioengine.commit = fio_null_commit; | |
268 | ioengine.getevents = fio_null_getevents; | |
269 | ioengine.event = fio_null_event; | |
270 | ioengine.init = fio_null_init; | |
271 | ioengine.cleanup = fio_null_cleanup; | |
272 | ioengine.open_file = fio_null_open; | |
273 | ioengine.flags = FIO_DISKLESSIO | FIO_FAKEIO; | |
46a67478 DG |
274 | } |
275 | } | |
276 | #endif /* FIO_EXTERNAL_ENGINE */ | |
277 | ||
278 | #endif /* __cplusplus */ |