fio: ioengine flag cleanup
[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         if (td->o.odirect) {
231                 r = rados_conf_set(rbd->cluster, "rbd_cache", "false");
232                 if (r < 0) {
233                         log_info("failed to disable RBD in-memory cache\n");
234                 }
235         }
236
237         r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ );
238         if (r < 0) {
239                 log_err("rbd_open failed.\n");
240                 goto failed_open;
241         }
242
243         if (!td->o.odirect) {
244                 /*
245                  * ensure cache enables writeback/around mode unless explicitly
246                  * configured for writethrough mode
247                  */
248                 r = rbd_flush(rbd->image);
249                 if (r < 0) {
250                         log_info("rbd: failed to issue initial flush\n");
251                 }
252         }
253
254         if (!_fio_rbd_setup_poll(rbd))
255                 goto failed_poll;
256
257         return 0;
258
259 failed_poll:
260         rbd_close(rbd->image);
261         rbd->image = NULL;
262 failed_open:
263         rados_ioctx_destroy(rbd->io_ctx);
264         rbd->io_ctx = NULL;
265 failed_shutdown:
266         rados_shutdown(rbd->cluster);
267         rbd->cluster = NULL;
268 failed_early:
269         return 1;
270 }
271
272 static void _fio_rbd_disconnect(struct rbd_data *rbd)
273 {
274         if (!rbd)
275                 return;
276
277         /* close eventfd */
278         if (rbd->fd != -1) {
279                 close(rbd->fd);
280                 rbd->fd = -1;
281         }
282
283         /* shutdown everything */
284         if (rbd->image) {
285                 rbd_close(rbd->image);
286                 rbd->image = NULL;
287         }
288
289         if (rbd->io_ctx) {
290                 rados_ioctx_destroy(rbd->io_ctx);
291                 rbd->io_ctx = NULL;
292         }
293
294         if (rbd->cluster) {
295                 rados_shutdown(rbd->cluster);
296                 rbd->cluster = NULL;
297         }
298 }
299
300 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
301 {
302         struct fio_rbd_iou *fri = data;
303         struct io_u *io_u = fri->io_u;
304         ssize_t ret;
305
306         /*
307          * Looks like return value is 0 for success, or < 0 for
308          * a specific error. So we have to assume that it can't do
309          * partial completions.
310          */
311         ret = rbd_aio_get_return_value(fri->completion);
312         if (ret < 0) {
313                 io_u->error = -ret;
314                 io_u->resid = io_u->xfer_buflen;
315         } else
316                 io_u->error = 0;
317
318         fri->io_complete = 1;
319 }
320
321 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
322 {
323         struct rbd_data *rbd = td->io_ops_data;
324
325         return rbd->aio_events[event];
326 }
327
328 static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u,
329                                      unsigned int *events)
330 {
331         struct fio_rbd_iou *fri = io_u->engine_data;
332
333         if (fri->io_complete) {
334                 fri->io_seen = 1;
335                 rbd->aio_events[*events] = io_u;
336                 (*events)++;
337
338                 rbd_aio_release(fri->completion);
339                 return 1;
340         }
341
342         return 0;
343 }
344
345 #ifndef CONFIG_RBD_POLL
346 static inline int rbd_io_u_seen(struct io_u *io_u)
347 {
348         struct fio_rbd_iou *fri = io_u->engine_data;
349
350         return fri->io_seen;
351 }
352 #endif
353
354 static void rbd_io_u_wait_complete(struct io_u *io_u)
355 {
356         struct fio_rbd_iou *fri = io_u->engine_data;
357
358         rbd_aio_wait_for_complete(fri->completion);
359 }
360
361 static int rbd_io_u_cmp(const void *p1, const void *p2)
362 {
363         const struct io_u **a = (const struct io_u **) p1;
364         const struct io_u **b = (const struct io_u **) p2;
365         uint64_t at, bt;
366
367         at = utime_since_now(&(*a)->start_time);
368         bt = utime_since_now(&(*b)->start_time);
369
370         if (at < bt)
371                 return -1;
372         else if (at == bt)
373                 return 0;
374         else
375                 return 1;
376 }
377
378 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
379                            unsigned int min_evts, int wait)
380 {
381         struct rbd_data *rbd = td->io_ops_data;
382         unsigned int this_events = 0;
383         struct io_u *io_u;
384         int i, sidx = 0;
385
386 #ifdef CONFIG_RBD_POLL
387         int ret = 0;
388         int event_num = 0;
389         struct fio_rbd_iou *fri = NULL;
390         rbd_completion_t comps[min_evts];
391         uint64_t counter;
392         bool completed;
393
394         struct pollfd pfd;
395         pfd.fd = rbd->fd;
396         pfd.events = POLLIN;
397
398         ret = poll(&pfd, 1, wait ? -1 : 0);
399         if (ret <= 0)
400                 return 0;
401         if (!(pfd.revents & POLLIN))
402                 return 0;
403
404         event_num = rbd_poll_io_events(rbd->image, comps, min_evts);
405
406         for (i = 0; i < event_num; i++) {
407                 fri = rbd_aio_get_arg(comps[i]);
408                 io_u = fri->io_u;
409
410                 /* best effort to decrement the semaphore */
411                 ret = read(rbd->fd, &counter, sizeof(counter));
412                 if (ret <= 0)
413                         log_err("rbd_iter_events failed to decrement semaphore.\n");
414
415                 completed = fri_check_complete(rbd, io_u, events);
416                 assert(completed);
417
418                 this_events++;
419         }
420 #else
421         io_u_qiter(&td->io_u_all, io_u, i) {
422                 if (!(io_u->flags & IO_U_F_FLIGHT))
423                         continue;
424                 if (rbd_io_u_seen(io_u))
425                         continue;
426
427                 if (fri_check_complete(rbd, io_u, events))
428                         this_events++;
429                 else if (wait)
430                         rbd->sort_events[sidx++] = io_u;
431         }
432 #endif
433
434         if (!wait || !sidx)
435                 return this_events;
436
437         /*
438          * Sort events, oldest issue first, then wait on as many as we
439          * need in order of age. If we have enough events, stop waiting,
440          * and just check if any of the older ones are done.
441          */
442         if (sidx > 1)
443                 qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
444
445         for (i = 0; i < sidx; i++) {
446                 io_u = rbd->sort_events[i];
447
448                 if (fri_check_complete(rbd, io_u, events)) {
449                         this_events++;
450                         continue;
451                 }
452
453                 /*
454                  * Stop waiting when we have enough, but continue checking
455                  * all pending IOs if they are complete.
456                  */
457                 if (*events >= min_evts)
458                         continue;
459
460                 rbd_io_u_wait_complete(io_u);
461
462                 if (fri_check_complete(rbd, io_u, events))
463                         this_events++;
464         }
465
466         return this_events;
467 }
468
469 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
470                              unsigned int max, const struct timespec *t)
471 {
472         unsigned int this_events, events = 0;
473         struct rbd_options *o = td->eo;
474         int wait = 0;
475
476         do {
477                 this_events = rbd_iter_events(td, &events, min, wait);
478
479                 if (events >= min)
480                         break;
481                 if (this_events)
482                         continue;
483
484                 if (!o->busy_poll)
485                         wait = 1;
486                 else
487                         nop;
488         } while (1);
489
490         return events;
491 }
492
493 static enum fio_q_status fio_rbd_queue(struct thread_data *td,
494                                        struct io_u *io_u)
495 {
496         struct rbd_data *rbd = td->io_ops_data;
497         struct fio_rbd_iou *fri = io_u->engine_data;
498         int r = -1;
499
500         fio_ro_check(td, io_u);
501
502         fri->io_seen = 0;
503         fri->io_complete = 0;
504
505         r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
506                                                 &fri->completion);
507         if (r < 0) {
508                 log_err("rbd_aio_create_completion failed.\n");
509                 goto failed;
510         }
511
512         if (io_u->ddir == DDIR_WRITE) {
513                 r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen,
514                                          io_u->xfer_buf, fri->completion);
515                 if (r < 0) {
516                         log_err("rbd_aio_write failed.\n");
517                         goto failed_comp;
518                 }
519
520         } else if (io_u->ddir == DDIR_READ) {
521                 r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen,
522                                         io_u->xfer_buf, fri->completion);
523
524                 if (r < 0) {
525                         log_err("rbd_aio_read failed.\n");
526                         goto failed_comp;
527                 }
528         } else if (io_u->ddir == DDIR_TRIM) {
529                 r = rbd_aio_discard(rbd->image, io_u->offset,
530                                         io_u->xfer_buflen, fri->completion);
531                 if (r < 0) {
532                         log_err("rbd_aio_discard failed.\n");
533                         goto failed_comp;
534                 }
535         } else if (io_u->ddir == DDIR_SYNC) {
536                 r = rbd_aio_flush(rbd->image, fri->completion);
537                 if (r < 0) {
538                         log_err("rbd_flush failed.\n");
539                         goto failed_comp;
540                 }
541         } else {
542                 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
543                        io_u->ddir);
544                 r = -EINVAL;
545                 goto failed_comp;
546         }
547
548         return FIO_Q_QUEUED;
549 failed_comp:
550         rbd_aio_release(fri->completion);
551 failed:
552         io_u->error = -r;
553         td_verror(td, io_u->error, "xfer");
554         return FIO_Q_COMPLETED;
555 }
556
557 static int fio_rbd_init(struct thread_data *td)
558 {
559         int r;
560         struct rbd_data *rbd = td->io_ops_data;
561
562         if (rbd->connected)
563                 return 0;
564
565         r = _fio_rbd_connect(td);
566         if (r) {
567                 log_err("fio_rbd_connect failed, return code: %d .\n", r);
568                 goto failed;
569         }
570
571         return 0;
572
573 failed:
574         return 1;
575 }
576
577 static void fio_rbd_cleanup(struct thread_data *td)
578 {
579         struct rbd_data *rbd = td->io_ops_data;
580
581         if (rbd) {
582                 _fio_rbd_disconnect(rbd);
583                 free(rbd->aio_events);
584                 free(rbd->sort_events);
585                 free(rbd);
586         }
587 }
588
589 static int fio_rbd_setup(struct thread_data *td)
590 {
591         rbd_image_info_t info;
592         struct fio_file *f;
593         struct rbd_data *rbd = NULL;
594         int r;
595
596         /* allocate engine specific structure to deal with librbd. */
597         r = _fio_setup_rbd_data(td, &rbd);
598         if (r) {
599                 log_err("fio_setup_rbd_data failed.\n");
600                 goto cleanup;
601         }
602         td->io_ops_data = rbd;
603
604         /* librbd does not allow us to run first in the main thread and later
605          * in a fork child. It needs to be the same process context all the
606          * time. 
607          */
608         td->o.use_thread = 1;
609
610         /* connect in the main thread to determine to determine
611          * the size of the given RADOS block device. And disconnect
612          * later on.
613          */
614         r = _fio_rbd_connect(td);
615         if (r) {
616                 log_err("fio_rbd_connect failed.\n");
617                 goto cleanup;
618         }
619         rbd->connected = true;
620
621         /* get size of the RADOS block device */
622         r = rbd_stat(rbd->image, &info, sizeof(info));
623         if (r < 0) {
624                 log_err("rbd_status failed.\n");
625                 goto cleanup;
626         } else if (info.size == 0) {
627                 log_err("image size should be larger than zero.\n");
628                 r = -EINVAL;
629                 goto cleanup;
630         }
631
632         dprint(FD_IO, "rbd-engine: image size: %" PRIu64 "\n", info.size);
633
634         /* taken from "net" engine. Pretend we deal with files,
635          * even if we do not have any ideas about files.
636          * The size of the RBD is set instead of a artificial file.
637          */
638         if (!td->files_index) {
639                 add_file(td, td->o.filename ? : "rbd", 0, 0);
640                 td->o.nr_files = td->o.nr_files ? : 1;
641                 td->o.open_files++;
642         }
643         f = td->files[0];
644         f->real_file_size = info.size;
645
646         return 0;
647
648 cleanup:
649         fio_rbd_cleanup(td);
650         return r;
651 }
652
653 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
654 {
655         return 0;
656 }
657
658 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
659 {
660 #if defined(CONFIG_RBD_INVAL)
661         struct rbd_data *rbd = td->io_ops_data;
662
663         return rbd_invalidate_cache(rbd->image);
664 #else
665         return 0;
666 #endif
667 }
668
669 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
670 {
671         struct fio_rbd_iou *fri = io_u->engine_data;
672
673         if (fri) {
674                 io_u->engine_data = NULL;
675                 free(fri);
676         }
677 }
678
679 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
680 {
681         struct fio_rbd_iou *fri;
682
683         fri = calloc(1, sizeof(*fri));
684         fri->io_u = io_u;
685         io_u->engine_data = fri;
686         return 0;
687 }
688
689 FIO_STATIC struct ioengine_ops ioengine = {
690         .name                   = "rbd",
691         .version                = FIO_IOOPS_VERSION,
692         .setup                  = fio_rbd_setup,
693         .init                   = fio_rbd_init,
694         .queue                  = fio_rbd_queue,
695         .getevents              = fio_rbd_getevents,
696         .event                  = fio_rbd_event,
697         .cleanup                = fio_rbd_cleanup,
698         .open_file              = fio_rbd_open,
699         .invalidate             = fio_rbd_invalidate,
700         .options                = options,
701         .io_u_init              = fio_rbd_io_u_init,
702         .io_u_free              = fio_rbd_io_u_free,
703         .option_struct_size     = sizeof(struct rbd_options),
704 };
705
706 static void fio_init fio_rbd_register(void)
707 {
708         register_ioengine(&ioengine);
709 }
710
711 static void fio_exit fio_rbd_unregister(void)
712 {
713         unregister_ioengine(&ioengine);
714 }