Commit | Line | Data |
---|---|---|
fc5c0345 DG |
1 | /* |
2 | * rbd engine | |
3 | * | |
4 | * IO engine using Ceph's librbd to test RADOS Block Devices. | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <rbd/librbd.h> | |
9 | ||
10 | #include "../fio.h" | |
d220c761 | 11 | #include "../optgroup.h" |
a4c4c346 | 12 | #ifdef CONFIG_RBD_BLKIN |
13 | #include <zipkin_c.h> | |
14 | #endif | |
fc5c0345 | 15 | |
53b6c979 PL |
16 | #ifdef CONFIG_RBD_POLL |
17 | /* add for poll */ | |
18 | #include <poll.h> | |
19 | #include <sys/eventfd.h> | |
20 | #endif | |
21 | ||
fc5c0345 DG |
22 | struct fio_rbd_iou { |
23 | struct io_u *io_u; | |
d8b64af2 | 24 | rbd_completion_t completion; |
d8b64af2 | 25 | int io_seen; |
b8ecbef6 | 26 | int io_complete; |
550beaad | 27 | #ifdef CONFIG_RBD_BLKIN |
a4c4c346 | 28 | struct blkin_trace_info info; |
550beaad | 29 | #endif |
fc5c0345 DG |
30 | }; |
31 | ||
32 | struct rbd_data { | |
33 | rados_t cluster; | |
34 | rados_ioctx_t io_ctx; | |
35 | rbd_image_t image; | |
36 | struct io_u **aio_events; | |
6f9961ac | 37 | struct io_u **sort_events; |
53b6c979 PL |
38 | #ifdef CONFIG_RBD_POLL |
39 | int fd; /* add for poll */ | |
40 | #endif | |
fc5c0345 DG |
41 | }; |
42 | ||
43 | struct rbd_options { | |
a1f871c7 | 44 | void *pad; |
6e20c6e7 | 45 | char *cluster_name; |
fc5c0345 DG |
46 | char *rbd_name; |
47 | char *pool_name; | |
48 | char *client_name; | |
d7d702c7 | 49 | int busy_poll; |
fc5c0345 DG |
50 | }; |
51 | ||
52 | static struct fio_option options[] = { | |
6e20c6e7 T |
53 | { |
54 | .name = "clustername", | |
55 | .lname = "ceph cluster name", | |
56 | .type = FIO_OPT_STR_STORE, | |
57 | .help = "Cluster name for ceph", | |
58 | .off1 = offsetof(struct rbd_options, cluster_name), | |
59 | .category = FIO_OPT_C_ENGINE, | |
60 | .group = FIO_OPT_G_RBD, | |
61 | }, | |
fc5c0345 | 62 | { |
d8b64af2 JA |
63 | .name = "rbdname", |
64 | .lname = "rbd engine rbdname", | |
65 | .type = FIO_OPT_STR_STORE, | |
66 | .help = "RBD name for RBD engine", | |
67 | .off1 = offsetof(struct rbd_options, rbd_name), | |
68 | .category = FIO_OPT_C_ENGINE, | |
69 | .group = FIO_OPT_G_RBD, | |
70 | }, | |
fc5c0345 | 71 | { |
d7d702c7 JA |
72 | .name = "pool", |
73 | .lname = "rbd engine pool", | |
74 | .type = FIO_OPT_STR_STORE, | |
75 | .help = "Name of the pool hosting the RBD for the RBD engine", | |
76 | .off1 = offsetof(struct rbd_options, pool_name), | |
77 | .category = FIO_OPT_C_ENGINE, | |
78 | .group = FIO_OPT_G_RBD, | |
d8b64af2 | 79 | }, |
fc5c0345 | 80 | { |
d7d702c7 JA |
81 | .name = "clientname", |
82 | .lname = "rbd engine clientname", | |
83 | .type = FIO_OPT_STR_STORE, | |
84 | .help = "Name of the ceph client to access the RBD for the RBD engine", | |
85 | .off1 = offsetof(struct rbd_options, client_name), | |
86 | .category = FIO_OPT_C_ENGINE, | |
87 | .group = FIO_OPT_G_RBD, | |
88 | }, | |
89 | { | |
90 | .name = "busy_poll", | |
91 | .lname = "Busy poll", | |
92 | .type = FIO_OPT_BOOL, | |
93 | .help = "Busy poll for completions instead of sleeping", | |
fea585d4 | 94 | .off1 = offsetof(struct rbd_options, busy_poll), |
d7d702c7 JA |
95 | .def = "0", |
96 | .category = FIO_OPT_C_ENGINE, | |
97 | .group = FIO_OPT_G_RBD, | |
d8b64af2 | 98 | }, |
fc5c0345 | 99 | { |
d8b64af2 JA |
100 | .name = NULL, |
101 | }, | |
fc5c0345 DG |
102 | }; |
103 | ||
104 | static int _fio_setup_rbd_data(struct thread_data *td, | |
105 | struct rbd_data **rbd_data_ptr) | |
106 | { | |
6f9961ac | 107 | struct rbd_data *rbd; |
fc5c0345 | 108 | |
565e784d | 109 | if (td->io_ops_data) |
fc5c0345 DG |
110 | return 0; |
111 | ||
6f9961ac JA |
112 | rbd = calloc(1, sizeof(struct rbd_data)); |
113 | if (!rbd) | |
fc5c0345 DG |
114 | goto failed; |
115 | ||
53b6c979 PL |
116 | #ifdef CONFIG_RBD_POLL |
117 | /* add for poll, init fd: -1 */ | |
118 | rbd->fd = -1; | |
119 | #endif | |
120 | ||
6f9961ac JA |
121 | rbd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *)); |
122 | if (!rbd->aio_events) | |
fc5c0345 DG |
123 | goto failed; |
124 | ||
6f9961ac JA |
125 | rbd->sort_events = calloc(td->o.iodepth, sizeof(struct io_u *)); |
126 | if (!rbd->sort_events) | |
127 | goto failed; | |
fc5c0345 | 128 | |
6f9961ac | 129 | *rbd_data_ptr = rbd; |
fc5c0345 DG |
130 | return 0; |
131 | ||
132 | failed: | |
5a4adfd2 JQ |
133 | if (rbd) { |
134 | if (rbd->aio_events) | |
135 | free(rbd->aio_events); | |
136 | if (rbd->sort_events) | |
137 | free(rbd->sort_events); | |
6f9961ac | 138 | free(rbd); |
5a4adfd2 | 139 | } |
fc5c0345 DG |
140 | return 1; |
141 | ||
142 | } | |
143 | ||
144 | static int _fio_rbd_connect(struct thread_data *td) | |
145 | { | |
565e784d | 146 | struct rbd_data *rbd = td->io_ops_data; |
fc5c0345 DG |
147 | struct rbd_options *o = td->eo; |
148 | int r; | |
149 | ||
6e20c6e7 T |
150 | if (o->cluster_name) { |
151 | char *client_name = NULL; | |
152 | ||
153 | /* | |
89e5ec96 | 154 | * If we specify cluser name, the rados_create2 |
6e20c6e7 T |
155 | * will not assume 'client.'. name is considered |
156 | * as a full type.id namestr | |
157 | */ | |
89e5ec96 JQ |
158 | if (o->client_name) { |
159 | if (!index(o->client_name, '.')) { | |
160 | client_name = calloc(1, strlen("client.") + | |
161 | strlen(o->client_name) + 1); | |
162 | strcat(client_name, "client."); | |
6ec0dcd1 JQ |
163 | strcat(client_name, o->client_name); |
164 | } else { | |
165 | client_name = o->client_name; | |
89e5ec96 | 166 | } |
6e20c6e7 | 167 | } |
6ec0dcd1 | 168 | |
6e20c6e7 | 169 | r = rados_create2(&rbd->cluster, o->cluster_name, |
6ec0dcd1 JQ |
170 | client_name, 0); |
171 | ||
172 | if (client_name && !index(o->client_name, '.')) | |
173 | free(client_name); | |
6e20c6e7 T |
174 | } else |
175 | r = rados_create(&rbd->cluster, o->client_name); | |
176 | ||
fc5c0345 DG |
177 | if (r < 0) { |
178 | log_err("rados_create failed.\n"); | |
179 | goto failed_early; | |
180 | } | |
181 | ||
6f9961ac | 182 | r = rados_conf_read_file(rbd->cluster, NULL); |
fc5c0345 DG |
183 | if (r < 0) { |
184 | log_err("rados_conf_read_file failed.\n"); | |
185 | goto failed_early; | |
186 | } | |
187 | ||
6f9961ac | 188 | r = rados_connect(rbd->cluster); |
fc5c0345 DG |
189 | if (r < 0) { |
190 | log_err("rados_connect failed.\n"); | |
191 | goto failed_shutdown; | |
192 | } | |
193 | ||
6f9961ac | 194 | r = rados_ioctx_create(rbd->cluster, o->pool_name, &rbd->io_ctx); |
fc5c0345 DG |
195 | if (r < 0) { |
196 | log_err("rados_ioctx_create failed.\n"); | |
197 | goto failed_shutdown; | |
198 | } | |
199 | ||
6f9961ac | 200 | r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ ); |
fc5c0345 DG |
201 | if (r < 0) { |
202 | log_err("rbd_open failed.\n"); | |
203 | goto failed_open; | |
204 | } | |
53b6c979 PL |
205 | |
206 | #ifdef CONFIG_RBD_POLL | |
207 | /* add for rbd poll */ | |
208 | rbd->fd = eventfd(0, EFD_NONBLOCK); | |
209 | if (rbd->fd < 0) { | |
210 | log_err("eventfd failed.\n"); | |
211 | goto failed_open; | |
212 | } | |
213 | ||
214 | r = rbd_set_image_notification(rbd->image, rbd->fd, EVENT_TYPE_EVENTFD); | |
215 | if (r < 0) { | |
216 | log_err("rbd_set_image_notification failed.\n"); | |
217 | goto failed_notify; | |
218 | } | |
219 | #endif | |
220 | ||
fc5c0345 DG |
221 | return 0; |
222 | ||
53b6c979 PL |
223 | #ifdef CONFIG_RBD_POLL |
224 | failed_notify: | |
225 | close(rbd->fd); | |
226 | rbd->fd = -1; | |
227 | #endif | |
228 | ||
fc5c0345 | 229 | failed_open: |
6f9961ac JA |
230 | rados_ioctx_destroy(rbd->io_ctx); |
231 | rbd->io_ctx = NULL; | |
fc5c0345 | 232 | failed_shutdown: |
6f9961ac JA |
233 | rados_shutdown(rbd->cluster); |
234 | rbd->cluster = NULL; | |
fc5c0345 DG |
235 | failed_early: |
236 | return 1; | |
237 | } | |
238 | ||
6f9961ac | 239 | static void _fio_rbd_disconnect(struct rbd_data *rbd) |
fc5c0345 | 240 | { |
6f9961ac | 241 | if (!rbd) |
fc5c0345 DG |
242 | return; |
243 | ||
53b6c979 PL |
244 | #ifdef CONFIG_RBD_POLL |
245 | /* close eventfd */ | |
246 | if (rbd->fd >= 0) { | |
247 | close(rbd->fd); | |
248 | rbd->fd = -1; | |
249 | } | |
250 | #endif | |
251 | ||
fc5c0345 | 252 | /* shutdown everything */ |
6f9961ac JA |
253 | if (rbd->image) { |
254 | rbd_close(rbd->image); | |
255 | rbd->image = NULL; | |
fc5c0345 DG |
256 | } |
257 | ||
6f9961ac JA |
258 | if (rbd->io_ctx) { |
259 | rados_ioctx_destroy(rbd->io_ctx); | |
260 | rbd->io_ctx = NULL; | |
fc5c0345 DG |
261 | } |
262 | ||
6f9961ac JA |
263 | if (rbd->cluster) { |
264 | rados_shutdown(rbd->cluster); | |
265 | rbd->cluster = NULL; | |
fc5c0345 DG |
266 | } |
267 | } | |
268 | ||
d8b64af2 | 269 | static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data) |
fc5c0345 | 270 | { |
dbf388d2 JA |
271 | struct fio_rbd_iou *fri = data; |
272 | struct io_u *io_u = fri->io_u; | |
d8b64af2 | 273 | ssize_t ret; |
fc5c0345 | 274 | |
d8b64af2 JA |
275 | /* |
276 | * Looks like return value is 0 for success, or < 0 for | |
277 | * a specific error. So we have to assume that it can't do | |
278 | * partial completions. | |
279 | */ | |
280 | ret = rbd_aio_get_return_value(fri->completion); | |
281 | if (ret < 0) { | |
282 | io_u->error = ret; | |
283 | io_u->resid = io_u->xfer_buflen; | |
284 | } else | |
285 | io_u->error = 0; | |
20cf5aab JD |
286 | |
287 | fri->io_complete = 1; | |
d8b64af2 | 288 | } |
fc5c0345 | 289 | |
d8b64af2 JA |
290 | static struct io_u *fio_rbd_event(struct thread_data *td, int event) |
291 | { | |
565e784d | 292 | struct rbd_data *rbd = td->io_ops_data; |
fc5c0345 | 293 | |
6f9961ac | 294 | return rbd->aio_events[event]; |
fc5c0345 DG |
295 | } |
296 | ||
6f9961ac | 297 | static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u, |
d8b64af2 | 298 | unsigned int *events) |
fc5c0345 | 299 | { |
d8b64af2 | 300 | struct fio_rbd_iou *fri = io_u->engine_data; |
fc5c0345 | 301 | |
b8ecbef6 | 302 | if (fri->io_complete) { |
d8b64af2 | 303 | fri->io_seen = 1; |
6f9961ac | 304 | rbd->aio_events[*events] = io_u; |
d8b64af2 | 305 | (*events)++; |
fc5c0345 | 306 | |
d8b64af2 JA |
307 | rbd_aio_release(fri->completion); |
308 | return 1; | |
309 | } | |
fc5c0345 | 310 | |
d8b64af2 | 311 | return 0; |
fc5c0345 DG |
312 | } |
313 | ||
6f9961ac JA |
314 | static inline int rbd_io_u_seen(struct io_u *io_u) |
315 | { | |
316 | struct fio_rbd_iou *fri = io_u->engine_data; | |
317 | ||
318 | return fri->io_seen; | |
319 | } | |
320 | ||
321 | static void rbd_io_u_wait_complete(struct io_u *io_u) | |
322 | { | |
323 | struct fio_rbd_iou *fri = io_u->engine_data; | |
324 | ||
325 | rbd_aio_wait_for_complete(fri->completion); | |
326 | } | |
327 | ||
328 | static int rbd_io_u_cmp(const void *p1, const void *p2) | |
329 | { | |
330 | const struct io_u **a = (const struct io_u **) p1; | |
331 | const struct io_u **b = (const struct io_u **) p2; | |
332 | uint64_t at, bt; | |
333 | ||
334 | at = utime_since_now(&(*a)->start_time); | |
335 | bt = utime_since_now(&(*b)->start_time); | |
336 | ||
337 | if (at < bt) | |
338 | return -1; | |
339 | else if (at == bt) | |
340 | return 0; | |
341 | else | |
342 | return 1; | |
343 | } | |
344 | ||
d8b64af2 JA |
345 | static int rbd_iter_events(struct thread_data *td, unsigned int *events, |
346 | unsigned int min_evts, int wait) | |
82340a9f | 347 | { |
565e784d | 348 | struct rbd_data *rbd = td->io_ops_data; |
d8b64af2 JA |
349 | unsigned int this_events = 0; |
350 | struct io_u *io_u; | |
53b6c979 PL |
351 | int i, sidx = 0; |
352 | ||
353 | #ifdef CONFIG_RBD_POLL | |
354 | int ret = 0; | |
355 | int event_num = 0; | |
356 | struct fio_rbd_iou *fri = NULL; | |
357 | rbd_completion_t comps[min_evts]; | |
82340a9f | 358 | |
53b6c979 PL |
359 | struct pollfd pfd; |
360 | pfd.fd = rbd->fd; | |
361 | pfd.events = POLLIN; | |
362 | ||
363 | ret = poll(&pfd, 1, -1); | |
364 | if (ret <= 0) { | |
365 | return 0; | |
366 | } | |
367 | ||
368 | assert(pfd.revents & POLLIN); | |
369 | ||
370 | event_num = rbd_poll_io_events(rbd->image, comps, min_evts); | |
371 | ||
372 | for (i = 0; i < event_num; i++) { | |
373 | fri = rbd_aio_get_arg(comps[i]); | |
374 | io_u = fri->io_u; | |
375 | #else | |
d8b64af2 | 376 | io_u_qiter(&td->io_u_all, io_u, i) { |
53b6c979 | 377 | #endif |
d8b64af2 JA |
378 | if (!(io_u->flags & IO_U_F_FLIGHT)) |
379 | continue; | |
6f9961ac | 380 | if (rbd_io_u_seen(io_u)) |
d8b64af2 | 381 | continue; |
82340a9f | 382 | |
6f9961ac | 383 | if (fri_check_complete(rbd, io_u, events)) |
d8b64af2 | 384 | this_events++; |
6f9961ac JA |
385 | else if (wait) |
386 | rbd->sort_events[sidx++] = io_u; | |
387 | } | |
82340a9f | 388 | |
6f9961ac JA |
389 | if (!wait || !sidx) |
390 | return this_events; | |
391 | ||
392 | /* | |
393 | * Sort events, oldest issue first, then wait on as many as we | |
394 | * need in order of age. If we have enough events, stop waiting, | |
395 | * and just check if any of the older ones are done. | |
396 | */ | |
397 | if (sidx > 1) | |
398 | qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp); | |
399 | ||
400 | for (i = 0; i < sidx; i++) { | |
401 | io_u = rbd->sort_events[i]; | |
402 | ||
403 | if (fri_check_complete(rbd, io_u, events)) { | |
404 | this_events++; | |
405 | continue; | |
d8b64af2 | 406 | } |
6f9961ac JA |
407 | |
408 | /* | |
409 | * Stop waiting when we have enough, but continue checking | |
410 | * all pending IOs if they are complete. | |
411 | */ | |
d8b64af2 | 412 | if (*events >= min_evts) |
6f9961ac JA |
413 | continue; |
414 | ||
415 | rbd_io_u_wait_complete(io_u); | |
416 | ||
417 | if (fri_check_complete(rbd, io_u, events)) | |
418 | this_events++; | |
d8b64af2 | 419 | } |
fc5c0345 | 420 | |
d8b64af2 | 421 | return this_events; |
fc5c0345 DG |
422 | } |
423 | ||
424 | static int fio_rbd_getevents(struct thread_data *td, unsigned int min, | |
1f440ece | 425 | unsigned int max, const struct timespec *t) |
fc5c0345 | 426 | { |
d8b64af2 | 427 | unsigned int this_events, events = 0; |
d7d702c7 | 428 | struct rbd_options *o = td->eo; |
d8b64af2 | 429 | int wait = 0; |
fc5c0345 DG |
430 | |
431 | do { | |
d8b64af2 | 432 | this_events = rbd_iter_events(td, &events, min, wait); |
fc5c0345 | 433 | |
d8b64af2 | 434 | if (events >= min) |
fc5c0345 | 435 | break; |
d8b64af2 JA |
436 | if (this_events) |
437 | continue; | |
fc5c0345 | 438 | |
d7d702c7 JA |
439 | if (!o->busy_poll) |
440 | wait = 1; | |
441 | else | |
442 | nop; | |
fc5c0345 DG |
443 | } while (1); |
444 | ||
445 | return events; | |
446 | } | |
447 | ||
448 | static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u) | |
449 | { | |
565e784d | 450 | struct rbd_data *rbd = td->io_ops_data; |
d8b64af2 JA |
451 | struct fio_rbd_iou *fri = io_u->engine_data; |
452 | int r = -1; | |
fc5c0345 DG |
453 | |
454 | fio_ro_check(td, io_u); | |
455 | ||
d8b64af2 | 456 | fri->io_seen = 0; |
b8ecbef6 | 457 | fri->io_complete = 0; |
d8b64af2 | 458 | |
dbf388d2 | 459 | r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb, |
d8b64af2 | 460 | &fri->completion); |
dbf388d2 JA |
461 | if (r < 0) { |
462 | log_err("rbd_aio_create_completion failed.\n"); | |
463 | goto failed; | |
464 | } | |
fc5c0345 | 465 | |
dbf388d2 | 466 | if (io_u->ddir == DDIR_WRITE) { |
a4c4c346 | 467 | #ifdef CONFIG_RBD_BLKIN |
468 | blkin_init_trace_info(&fri->info); | |
469 | r = rbd_aio_write_traced(rbd->image, io_u->offset, io_u->xfer_buflen, | |
470 | io_u->xfer_buf, fri->completion, &fri->info); | |
471 | #else | |
6f9961ac JA |
472 | r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen, |
473 | io_u->xfer_buf, fri->completion); | |
a4c4c346 | 474 | #endif |
fc5c0345 DG |
475 | if (r < 0) { |
476 | log_err("rbd_aio_write failed.\n"); | |
dbf388d2 | 477 | goto failed_comp; |
fc5c0345 DG |
478 | } |
479 | ||
480 | } else if (io_u->ddir == DDIR_READ) { | |
a4c4c346 | 481 | #ifdef CONFIG_RBD_BLKIN |
482 | blkin_init_trace_info(&fri->info); | |
483 | r = rbd_aio_read_traced(rbd->image, io_u->offset, io_u->xfer_buflen, | |
484 | io_u->xfer_buf, fri->completion, &fri->info); | |
485 | #else | |
6f9961ac JA |
486 | r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen, |
487 | io_u->xfer_buf, fri->completion); | |
a4c4c346 | 488 | #endif |
fc5c0345 DG |
489 | |
490 | if (r < 0) { | |
491 | log_err("rbd_aio_read failed.\n"); | |
dbf388d2 | 492 | goto failed_comp; |
fc5c0345 | 493 | } |
dbf388d2 | 494 | } else if (io_u->ddir == DDIR_TRIM) { |
6f9961ac JA |
495 | r = rbd_aio_discard(rbd->image, io_u->offset, |
496 | io_u->xfer_buflen, fri->completion); | |
82340a9f | 497 | if (r < 0) { |
dbf388d2 JA |
498 | log_err("rbd_aio_discard failed.\n"); |
499 | goto failed_comp; | |
82340a9f | 500 | } |
dbf388d2 | 501 | } else if (io_u->ddir == DDIR_SYNC) { |
6f9961ac | 502 | r = rbd_aio_flush(rbd->image, fri->completion); |
fc5c0345 DG |
503 | if (r < 0) { |
504 | log_err("rbd_flush failed.\n"); | |
dbf388d2 | 505 | goto failed_comp; |
fc5c0345 | 506 | } |
fc5c0345 DG |
507 | } else { |
508 | dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__, | |
509 | io_u->ddir); | |
dbf388d2 | 510 | goto failed_comp; |
fc5c0345 DG |
511 | } |
512 | ||
513 | return FIO_Q_QUEUED; | |
dbf388d2 JA |
514 | failed_comp: |
515 | rbd_aio_release(fri->completion); | |
fc5c0345 DG |
516 | failed: |
517 | io_u->error = r; | |
518 | td_verror(td, io_u->error, "xfer"); | |
519 | return FIO_Q_COMPLETED; | |
520 | } | |
521 | ||
522 | static int fio_rbd_init(struct thread_data *td) | |
523 | { | |
524 | int r; | |
525 | ||
526 | r = _fio_rbd_connect(td); | |
527 | if (r) { | |
528 | log_err("fio_rbd_connect failed, return code: %d .\n", r); | |
529 | goto failed; | |
530 | } | |
531 | ||
532 | return 0; | |
533 | ||
534 | failed: | |
535 | return 1; | |
fc5c0345 DG |
536 | } |
537 | ||
538 | static void fio_rbd_cleanup(struct thread_data *td) | |
539 | { | |
565e784d | 540 | struct rbd_data *rbd = td->io_ops_data; |
fc5c0345 | 541 | |
6f9961ac JA |
542 | if (rbd) { |
543 | _fio_rbd_disconnect(rbd); | |
544 | free(rbd->aio_events); | |
545 | free(rbd->sort_events); | |
546 | free(rbd); | |
fc5c0345 | 547 | } |
fc5c0345 DG |
548 | } |
549 | ||
550 | static int fio_rbd_setup(struct thread_data *td) | |
551 | { | |
fc5c0345 DG |
552 | rbd_image_info_t info; |
553 | struct fio_file *f; | |
6f9961ac | 554 | struct rbd_data *rbd = NULL; |
fc5c0345 | 555 | int major, minor, extra; |
6f9961ac | 556 | int r; |
fc5c0345 DG |
557 | |
558 | /* log version of librbd. No cluster connection required. */ | |
559 | rbd_version(&major, &minor, &extra); | |
560 | log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra); | |
561 | ||
562 | /* allocate engine specific structure to deal with librbd. */ | |
6f9961ac | 563 | r = _fio_setup_rbd_data(td, &rbd); |
fc5c0345 DG |
564 | if (r) { |
565 | log_err("fio_setup_rbd_data failed.\n"); | |
566 | goto cleanup; | |
567 | } | |
565e784d | 568 | td->io_ops_data = rbd; |
fc5c0345 | 569 | |
d8b64af2 JA |
570 | /* librbd does not allow us to run first in the main thread and later |
571 | * in a fork child. It needs to be the same process context all the | |
572 | * time. | |
fc5c0345 DG |
573 | */ |
574 | td->o.use_thread = 1; | |
575 | ||
576 | /* connect in the main thread to determine to determine | |
577 | * the size of the given RADOS block device. And disconnect | |
578 | * later on. | |
579 | */ | |
580 | r = _fio_rbd_connect(td); | |
581 | if (r) { | |
582 | log_err("fio_rbd_connect failed.\n"); | |
583 | goto cleanup; | |
584 | } | |
585 | ||
586 | /* get size of the RADOS block device */ | |
6f9961ac | 587 | r = rbd_stat(rbd->image, &info, sizeof(info)); |
fc5c0345 DG |
588 | if (r < 0) { |
589 | log_err("rbd_status failed.\n"); | |
590 | goto disconnect; | |
591 | } | |
592 | dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size); | |
593 | ||
594 | /* taken from "net" engine. Pretend we deal with files, | |
595 | * even if we do not have any ideas about files. | |
596 | * The size of the RBD is set instead of a artificial file. | |
597 | */ | |
598 | if (!td->files_index) { | |
5903e7b7 | 599 | add_file(td, td->o.filename ? : "rbd", 0, 0); |
fc5c0345 | 600 | td->o.nr_files = td->o.nr_files ? : 1; |
b53f2c54 | 601 | td->o.open_files++; |
fc5c0345 DG |
602 | } |
603 | f = td->files[0]; | |
604 | f->real_file_size = info.size; | |
605 | ||
606 | /* disconnect, then we were only connected to determine | |
607 | * the size of the RBD. | |
608 | */ | |
6f9961ac | 609 | _fio_rbd_disconnect(rbd); |
fc5c0345 DG |
610 | return 0; |
611 | ||
612 | disconnect: | |
6f9961ac | 613 | _fio_rbd_disconnect(rbd); |
fc5c0345 DG |
614 | cleanup: |
615 | fio_rbd_cleanup(td); | |
616 | return r; | |
617 | } | |
618 | ||
619 | static int fio_rbd_open(struct thread_data *td, struct fio_file *f) | |
620 | { | |
621 | return 0; | |
622 | } | |
623 | ||
d9b100fc JA |
624 | static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f) |
625 | { | |
903b2812 | 626 | #if defined(CONFIG_RBD_INVAL) |
565e784d | 627 | struct rbd_data *rbd = td->io_ops_data; |
903b2812 | 628 | |
6f9961ac | 629 | return rbd_invalidate_cache(rbd->image); |
903b2812 | 630 | #else |
d9b100fc | 631 | return 0; |
903b2812 | 632 | #endif |
d9b100fc JA |
633 | } |
634 | ||
fc5c0345 DG |
635 | static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u) |
636 | { | |
d8b64af2 | 637 | struct fio_rbd_iou *fri = io_u->engine_data; |
fc5c0345 | 638 | |
d8b64af2 | 639 | if (fri) { |
fc5c0345 | 640 | io_u->engine_data = NULL; |
d8b64af2 | 641 | free(fri); |
fc5c0345 DG |
642 | } |
643 | } | |
644 | ||
645 | static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u) | |
646 | { | |
d8b64af2 | 647 | struct fio_rbd_iou *fri; |
fc5c0345 | 648 | |
d8b64af2 JA |
649 | fri = calloc(1, sizeof(*fri)); |
650 | fri->io_u = io_u; | |
651 | io_u->engine_data = fri; | |
fc5c0345 DG |
652 | return 0; |
653 | } | |
654 | ||
10aa136b | 655 | static struct ioengine_ops ioengine = { |
d9b100fc JA |
656 | .name = "rbd", |
657 | .version = FIO_IOOPS_VERSION, | |
658 | .setup = fio_rbd_setup, | |
659 | .init = fio_rbd_init, | |
660 | .queue = fio_rbd_queue, | |
661 | .getevents = fio_rbd_getevents, | |
662 | .event = fio_rbd_event, | |
663 | .cleanup = fio_rbd_cleanup, | |
664 | .open_file = fio_rbd_open, | |
665 | .invalidate = fio_rbd_invalidate, | |
666 | .options = options, | |
667 | .io_u_init = fio_rbd_io_u_init, | |
668 | .io_u_free = fio_rbd_io_u_free, | |
669 | .option_struct_size = sizeof(struct rbd_options), | |
fc5c0345 DG |
670 | }; |
671 | ||
672 | static void fio_init fio_rbd_register(void) | |
673 | { | |
674 | register_ioengine(&ioengine); | |
675 | } | |
676 | ||
677 | static void fio_exit fio_rbd_unregister(void) | |
678 | { | |
679 | unregister_ioengine(&ioengine); | |
680 | } |