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