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