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 | 15 | */ |
a94ea28b | 16 | #include <stdlib.h> |
a94ea28b JA |
17 | #include <assert.h> |
18 | ||
19 | #include "../fio.h" | |
a94ea28b | 20 | |
65afa5f2 JA |
21 | struct null_data { |
22 | struct io_u **io_us; | |
23 | int queued; | |
24 | int events; | |
25 | }; | |
26 | ||
966fcbd4 | 27 | static struct io_u *null_event(struct null_data *nd, int event) |
65afa5f2 | 28 | { |
65afa5f2 JA |
29 | return nd->io_us[event]; |
30 | } | |
31 | ||
966fcbd4 | 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) | |
65afa5f2 | 35 | { |
7401c088 | 36 | int ret = 0; |
966fcbd4 | 37 | |
7401c088 JA |
38 | if (min_events) { |
39 | ret = nd->events; | |
40 | nd->events = 0; | |
41 | } | |
65afa5f2 | 42 | |
65afa5f2 JA |
43 | return ret; |
44 | } | |
45 | ||
966fcbd4 | 46 | static int null_commit(struct thread_data *td, struct null_data *nd) |
65afa5f2 | 47 | { |
ed8bd849 | 48 | if (!nd->events) { |
46a67478 | 49 | #ifndef FIO_EXTERNAL_ENGINE |
838bc709 | 50 | io_u_mark_submit(td, nd->queued); |
46a67478 | 51 | #endif |
ed8bd849 JA |
52 | nd->events = nd->queued; |
53 | nd->queued = 0; | |
54 | } | |
55 | ||
65afa5f2 JA |
56 | return 0; |
57 | } | |
58 | ||
2e4ef4fb JA |
59 | static enum fio_q_status null_queue(struct thread_data *td, |
60 | struct null_data *nd, struct io_u *io_u) | |
a94ea28b | 61 | { |
7101d9c2 JA |
62 | fio_ro_check(td, io_u); |
63 | ||
65afa5f2 JA |
64 | if (td->io_ops->flags & FIO_SYNCIO) |
65 | return FIO_Q_COMPLETED; | |
ed8bd849 JA |
66 | if (nd->events) |
67 | return FIO_Q_BUSY; | |
65afa5f2 JA |
68 | |
69 | nd->io_us[nd->queued++] = io_u; | |
70 | return FIO_Q_QUEUED; | |
a94ea28b JA |
71 | } |
72 | ||
966fcbd4 | 73 | static int null_open(struct null_data fio_unused *nd, |
74 | struct fio_file fio_unused *f) | |
b5af8293 JA |
75 | { |
76 | return 0; | |
77 | } | |
78 | ||
966fcbd4 | 79 | static void null_cleanup(struct null_data *nd) |
65afa5f2 | 80 | { |
65afa5f2 | 81 | if (nd) { |
02a3d83f | 82 | free(nd->io_us); |
65afa5f2 | 83 | free(nd); |
65afa5f2 JA |
84 | } |
85 | } | |
86 | ||
7746976c | 87 | static struct null_data *null_init(struct thread_data *td) |
65afa5f2 | 88 | { |
7746976c | 89 | struct null_data *nd = (struct null_data *) malloc(sizeof(*nd)); |
65afa5f2 JA |
90 | |
91 | memset(nd, 0, sizeof(*nd)); | |
92 | ||
2dc1bbeb | 93 | if (td->o.iodepth != 1) { |
46a67478 | 94 | nd->io_us = (struct io_u **) malloc(td->o.iodepth * sizeof(struct io_u *)); |
2dc1bbeb | 95 | memset(nd->io_us, 0, td->o.iodepth * sizeof(struct io_u *)); |
65afa5f2 JA |
96 | } else |
97 | td->io_ops->flags |= FIO_SYNCIO; | |
98 | ||
7746976c | 99 | return nd; |
65afa5f2 JA |
100 | } |
101 | ||
46a67478 | 102 | #ifndef __cplusplus |
966fcbd4 | 103 | |
104 | static struct io_u *fio_null_event(struct thread_data *td, int event) | |
105 | { | |
ca65714c | 106 | return null_event(td->io_ops_data, event); |
966fcbd4 | 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 | { | |
ca65714c | 112 | struct null_data *nd = td->io_ops_data; |
966fcbd4 | 113 | return null_getevents(nd, min_events, max, t); |
114 | } | |
115 | ||
116 | static int fio_null_commit(struct thread_data *td) | |
117 | { | |
ca65714c | 118 | return null_commit(td, td->io_ops_data); |
966fcbd4 | 119 | } |
120 | ||
2e4ef4fb JA |
121 | static enum fio_q_status fio_null_queue(struct thread_data *td, |
122 | struct io_u *io_u) | |
966fcbd4 | 123 | { |
ca65714c | 124 | return null_queue(td, td->io_ops_data, io_u); |
966fcbd4 | 125 | } |
126 | ||
127 | static int fio_null_open(struct thread_data *td, struct fio_file *f) | |
128 | { | |
ca65714c | 129 | return null_open(td->io_ops_data, f); |
966fcbd4 | 130 | } |
131 | ||
132 | static void fio_null_cleanup(struct thread_data *td) | |
133 | { | |
ca65714c | 134 | null_cleanup(td->io_ops_data); |
966fcbd4 | 135 | } |
136 | ||
137 | static int fio_null_init(struct thread_data *td) | |
138 | { | |
7746976c TK |
139 | td->io_ops_data = null_init(td); |
140 | assert(td->io_ops_data); | |
141 | return 0; | |
966fcbd4 | 142 | } |
143 | ||
a94ea28b JA |
144 | static struct ioengine_ops ioengine = { |
145 | .name = "null", | |
146 | .version = FIO_IOOPS_VERSION, | |
a94ea28b | 147 | .queue = fio_null_queue, |
65afa5f2 JA |
148 | .commit = fio_null_commit, |
149 | .getevents = fio_null_getevents, | |
150 | .event = fio_null_event, | |
151 | .init = fio_null_init, | |
152 | .cleanup = fio_null_cleanup, | |
b5af8293 | 153 | .open_file = fio_null_open, |
5c57c084 | 154 | .flags = FIO_DISKLESSIO | FIO_FAKEIO, |
a94ea28b JA |
155 | }; |
156 | ||
157 | static void fio_init fio_null_register(void) | |
158 | { | |
159 | register_ioengine(&ioengine); | |
160 | } | |
161 | ||
162 | static void fio_exit fio_null_unregister(void) | |
163 | { | |
164 | unregister_ioengine(&ioengine); | |
165 | } | |
46a67478 DG |
166 | |
167 | #else | |
168 | ||
169 | #ifdef FIO_EXTERNAL_ENGINE | |
966fcbd4 | 170 | |
171 | struct NullData { | |
172 | NullData(struct thread_data *td) | |
173 | { | |
7746976c TK |
174 | impl_ = null_init(td); |
175 | assert(impl_); | |
966fcbd4 | 176 | } |
177 | ||
178 | ~NullData() | |
179 | { | |
180 | null_cleanup(impl_); | |
181 | } | |
182 | ||
183 | static NullData *get(struct thread_data *td) | |
184 | { | |
185 | return reinterpret_cast<NullData *>(td->io_ops_data); | |
186 | } | |
187 | ||
188 | io_u *fio_null_event(struct thread_data *, int event) | |
189 | { | |
190 | return null_event(impl_, event); | |
191 | } | |
192 | ||
193 | int fio_null_getevents(struct thread_data *, unsigned int min_events, | |
194 | unsigned int max, const struct timespec *t) | |
195 | { | |
196 | return null_getevents(impl_, min_events, max, t); | |
197 | } | |
198 | ||
199 | int fio_null_commit(struct thread_data *td) | |
200 | { | |
201 | return null_commit(td, impl_); | |
202 | } | |
203 | ||
204 | int fio_null_queue(struct thread_data *td, struct io_u *io_u) | |
205 | { | |
206 | return null_queue(td, impl_, io_u); | |
207 | } | |
208 | ||
209 | int fio_null_open(struct thread_data *, struct fio_file *f) | |
210 | { | |
211 | return null_open(impl_, f); | |
212 | } | |
213 | ||
7caf10f1 | 214 | private: |
966fcbd4 | 215 | struct null_data *impl_; |
216 | }; | |
217 | ||
46a67478 | 218 | extern "C" { |
966fcbd4 | 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 | ||
247f299a | 257 | static struct ioengine_ops ioengine; |
8d6ecac2 JA |
258 | void get_ioengine(struct ioengine_ops **ioengine_ptr) |
259 | { | |
247f299a TK |
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; | |
46a67478 DG |
272 | } |
273 | } | |
274 | #endif /* FIO_EXTERNAL_ENGINE */ | |
275 | ||
276 | #endif /* __cplusplus */ |