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