2 * FIO engine for DAOS File System (dfs).
4 * (C) Copyright 2020-2021 Intel Corporation.
13 static bool daos_initialized;
14 static int num_threads;
15 static pthread_mutex_t daos_mutex = PTHREAD_MUTEX_INITIALIZER;
16 daos_handle_t poh; /* pool handle */
17 daos_handle_t coh; /* container handle */
18 daos_oclass_id_t cid = OC_UNKNOWN; /* object class */
19 dfs_t *dfs; /* dfs mount reference */
38 struct daos_fio_options {
40 char *pool; /* Pool UUID */
41 char *cont; /* Container UUID */
42 daos_size_t chsz; /* Chunk size */
43 char *oclass; /* object class */
44 #if !defined(DAOS_API_VERSION_MAJOR) || DAOS_API_VERSION_MAJOR < 1
45 char *svcl; /* service replica list, deprecated */
49 static struct fio_option options[] = {
52 .lname = "pool uuid or label",
53 .type = FIO_OPT_STR_STORE,
54 .off1 = offsetof(struct daos_fio_options, pool),
55 .help = "DAOS pool uuid or label",
56 .category = FIO_OPT_C_ENGINE,
57 .group = FIO_OPT_G_DFS,
61 .lname = "container uuid or label",
62 .type = FIO_OPT_STR_STORE,
63 .off1 = offsetof(struct daos_fio_options, cont),
64 .help = "DAOS container uuid or label",
65 .category = FIO_OPT_C_ENGINE,
66 .group = FIO_OPT_G_DFS,
70 .lname = "DFS chunk size",
72 .off1 = offsetof(struct daos_fio_options, chsz),
73 .help = "DFS chunk size in bytes",
74 .def = "0", /* use container default */
75 .category = FIO_OPT_C_ENGINE,
76 .group = FIO_OPT_G_DFS,
79 .name = "object_class",
80 .lname = "object class",
81 .type = FIO_OPT_STR_STORE,
82 .off1 = offsetof(struct daos_fio_options, oclass),
83 .help = "DAOS object class",
84 .category = FIO_OPT_C_ENGINE,
85 .group = FIO_OPT_G_DFS,
87 #if !defined(DAOS_API_VERSION_MAJOR) || DAOS_API_VERSION_MAJOR < 1
90 .lname = "List of service ranks",
91 .type = FIO_OPT_STR_STORE,
92 .off1 = offsetof(struct daos_fio_options, svcl),
93 .help = "List of pool replicated service ranks",
94 .category = FIO_OPT_C_ENGINE,
95 .group = FIO_OPT_G_DFS,
103 static int daos_fio_global_init(struct thread_data *td)
105 struct daos_fio_options *eo = td->eo;
106 daos_pool_info_t pool_info;
107 daos_cont_info_t co_info;
110 #if !defined(DAOS_API_VERSION_MAJOR) || DAOS_API_VERSION_MAJOR < 1
111 if (!eo->pool || !eo->cont || !eo->svcl) {
113 if (!eo->pool || !eo->cont) {
115 log_err("Missing required DAOS options\n");
120 if (rc != -DER_ALREADY && rc) {
121 log_err("Failed to initialize daos %d\n", rc);
122 td_verror(td, rc, "daos_init");
126 #if !defined(DAOS_API_VERSION_MAJOR) || \
127 (DAOS_API_VERSION_MAJOR == 1 && DAOS_API_VERSION_MINOR < 3)
128 uuid_t pool_uuid, co_uuid;
130 rc = uuid_parse(eo->pool, pool_uuid);
132 log_err("Failed to parse 'Pool uuid': %s\n", eo->pool);
133 td_verror(td, EINVAL, "uuid_parse(eo->pool)");
137 rc = uuid_parse(eo->cont, co_uuid);
139 log_err("Failed to parse 'Cont uuid': %s\n", eo->cont);
140 td_verror(td, EINVAL, "uuid_parse(eo->cont)");
145 /* Connect to the DAOS pool */
146 #if !defined(DAOS_API_VERSION_MAJOR) || DAOS_API_VERSION_MAJOR < 1
147 d_rank_list_t *svcl = NULL;
149 svcl = daos_rank_list_parse(eo->svcl, ":");
151 log_err("Failed to parse svcl\n");
152 td_verror(td, EINVAL, "daos_rank_list_parse");
156 rc = daos_pool_connect(pool_uuid, NULL, svcl, DAOS_PC_RW,
157 &poh, &pool_info, NULL);
158 d_rank_list_free(svcl);
159 #elif (DAOS_API_VERSION_MAJOR == 1 && DAOS_API_VERSION_MINOR < 3)
160 rc = daos_pool_connect(pool_uuid, NULL, DAOS_PC_RW, &poh, &pool_info,
163 rc = daos_pool_connect(eo->pool, NULL, DAOS_PC_RW, &poh, &pool_info,
167 log_err("Failed to connect to pool %d\n", rc);
168 td_verror(td, rc, "daos_pool_connect");
172 /* Open the DAOS container */
173 #if !defined(DAOS_API_VERSION_MAJOR) || \
174 (DAOS_API_VERSION_MAJOR == 1 && DAOS_API_VERSION_MINOR < 3)
175 rc = daos_cont_open(poh, co_uuid, DAOS_COO_RW, &coh, &co_info, NULL);
177 rc = daos_cont_open(poh, eo->cont, DAOS_COO_RW, &coh, &co_info, NULL);
180 log_err("Failed to open container: %d\n", rc);
181 td_verror(td, rc, "daos_cont_open");
182 (void)daos_pool_disconnect(poh, NULL);
186 /* Mount encapsulated filesystem */
187 rc = dfs_mount(poh, coh, O_RDWR, &dfs);
189 log_err("Failed to mount DFS namespace: %d\n", rc);
190 td_verror(td, rc, "dfs_mount");
191 (void)daos_pool_disconnect(poh, NULL);
192 (void)daos_cont_close(coh, NULL);
196 /* Retrieve object class to use, if specified */
198 cid = daos_oclass_name2id(eo->oclass);
203 static int daos_fio_global_cleanup()
208 rc = dfs_umount(dfs);
210 log_err("failed to umount dfs: %d\n", rc);
213 rc = daos_cont_close(coh, NULL);
215 log_err("failed to close container: %d\n", rc);
219 rc = daos_pool_disconnect(poh, NULL);
221 log_err("failed to disconnect pool: %d\n", rc);
227 log_err("failed to finalize daos: %d\n", rc);
235 static int daos_fio_setup(struct thread_data *td)
240 static int daos_fio_init(struct thread_data *td)
242 struct daos_data *dd;
245 pthread_mutex_lock(&daos_mutex);
247 dd = malloc(sizeof(*dd));
249 log_err("Failed to allocate DAOS-private data\n");
255 dd->num_ios = td->o.iodepth;
256 dd->io_us = calloc(dd->num_ios, sizeof(struct io_u *));
257 if (dd->io_us == NULL) {
258 log_err("Failed to allocate IO queue\n");
263 /* initialize DAOS stack if not already up */
264 if (!daos_initialized) {
265 rc = daos_fio_global_init(td);
268 daos_initialized = true;
271 rc = daos_eq_create(&dd->eqh);
273 log_err("Failed to create event queue: %d\n", rc);
274 td_verror(td, rc, "daos_eq_create");
278 td->io_ops_data = dd;
286 if (num_threads == 0 && daos_initialized) {
287 /* don't clobber error return value */
288 (void)daos_fio_global_cleanup();
289 daos_initialized = false;
292 pthread_mutex_unlock(&daos_mutex);
296 static void daos_fio_cleanup(struct thread_data *td)
298 struct daos_data *dd = td->io_ops_data;
304 rc = daos_eq_destroy(dd->eqh, DAOS_EQ_DESTROY_FORCE);
306 log_err("failed to destroy event queue: %d\n", rc);
307 td_verror(td, rc, "daos_eq_destroy");
313 pthread_mutex_lock(&daos_mutex);
315 if (daos_initialized && num_threads == 0) {
318 ret = daos_fio_global_cleanup();
319 if (ret < 0 && rc == 0) {
320 log_err("failed to clean up: %d\n", ret);
321 td_verror(td, ret, "daos_fio_global_cleanup");
323 daos_initialized = false;
325 pthread_mutex_unlock(&daos_mutex);
328 static int daos_fio_get_file_size(struct thread_data *td, struct fio_file *f)
330 char *file_name = f->file_name;
331 struct stat stbuf = {0};
334 dprint(FD_FILE, "dfs stat %s\n", f->file_name);
336 if (!daos_initialized)
339 rc = dfs_stat(dfs, NULL, file_name, &stbuf);
341 log_err("Failed to stat %s: %d\n", f->file_name, rc);
342 td_verror(td, rc, "dfs_stat");
346 f->real_file_size = stbuf.st_size;
350 static int daos_fio_close(struct thread_data *td, struct fio_file *f)
352 struct daos_data *dd = td->io_ops_data;
355 dprint(FD_FILE, "dfs release %s\n", f->file_name);
357 rc = dfs_release(dd->obj);
359 log_err("Failed to release %s: %d\n", f->file_name, rc);
360 td_verror(td, rc, "dfs_release");
367 static int daos_fio_open(struct thread_data *td, struct fio_file *f)
369 struct daos_data *dd = td->io_ops_data;
370 struct daos_fio_options *eo = td->eo;
374 dprint(FD_FILE, "dfs open %s (%s/%d/%d)\n",
375 f->file_name, td_write(td) & !read_only ? "rw" : "r",
376 td->o.create_on_open, td->o.allow_create);
378 if (td->o.create_on_open && td->o.allow_create)
384 if (td->o.allow_create)
386 } else if (td_read(td)) {
390 rc = dfs_open(dfs, NULL, f->file_name,
391 S_IFREG | S_IRUSR | S_IWUSR,
392 flags, cid, eo->chsz, NULL, &dd->obj);
394 log_err("Failed to open %s: %d\n", f->file_name, rc);
395 td_verror(td, rc, "dfs_open");
402 static int daos_fio_unlink(struct thread_data *td, struct fio_file *f)
406 dprint(FD_FILE, "dfs remove %s\n", f->file_name);
408 rc = dfs_remove(dfs, NULL, f->file_name, false, NULL);
410 log_err("Failed to remove %s: %d\n", f->file_name, rc);
411 td_verror(td, rc, "dfs_remove");
418 static int daos_fio_invalidate(struct thread_data *td, struct fio_file *f)
420 dprint(FD_FILE, "dfs invalidate %s\n", f->file_name);
424 static void daos_fio_io_u_free(struct thread_data *td, struct io_u *io_u)
426 struct daos_iou *io = io_u->engine_data;
429 io_u->engine_data = NULL;
434 static int daos_fio_io_u_init(struct thread_data *td, struct io_u *io_u)
438 io = malloc(sizeof(struct daos_iou));
440 td_verror(td, ENOMEM, "malloc");
444 io_u->engine_data = io;
448 static struct io_u * daos_fio_event(struct thread_data *td, int event)
450 struct daos_data *dd = td->io_ops_data;
452 return dd->io_us[event];
455 static int daos_fio_getevents(struct thread_data *td, unsigned int min,
456 unsigned int max, const struct timespec *t)
458 struct daos_data *dd = td->io_ops_data;
459 daos_event_t *evp[max];
460 unsigned int events = 0;
464 while (events < min) {
465 rc = daos_eq_poll(dd->eqh, 0, DAOS_EQ_NOWAIT, max, evp);
467 log_err("Event poll failed: %d\n", rc);
468 td_verror(td, rc, "daos_eq_poll");
472 for (i = 0; i < rc; i++) {
476 io = container_of(evp[i], struct daos_iou, ev);
478 log_err("Completion on already completed I/O\n");
482 io_u->error = io->ev.ev_error;
486 dd->io_us[events] = io_u;
488 daos_event_fini(&io->ev);
494 dprint(FD_IO, "dfs eq_pool returning %d (%u/%u)\n", events, min, max);
499 static enum fio_q_status daos_fio_queue(struct thread_data *td,
502 struct daos_data *dd = td->io_ops_data;
503 struct daos_iou *io = io_u->engine_data;
504 daos_off_t offset = io_u->offset;
507 if (dd->queued == td->o.iodepth)
511 io->sgl.sg_nr_out = 0;
512 d_iov_set(&io->iov, io_u->xfer_buf, io_u->xfer_buflen);
513 io->sgl.sg_iovs = &io->iov;
514 io->size = io_u->xfer_buflen;
516 io->complete = false;
517 rc = daos_event_init(&io->ev, dd->eqh, NULL);
519 log_err("Event init failed: %d\n", rc);
521 return FIO_Q_COMPLETED;
524 switch (io_u->ddir) {
526 rc = dfs_write(dfs, dd->obj, &io->sgl, offset, &io->ev);
528 log_err("dfs_write failed: %d\n", rc);
530 return FIO_Q_COMPLETED;
534 rc = dfs_read(dfs, dd->obj, &io->sgl, offset, &io->size,
537 log_err("dfs_read failed: %d\n", rc);
539 return FIO_Q_COMPLETED;
544 return FIO_Q_COMPLETED;
546 dprint(FD_IO, "Invalid IO type: %d\n", io_u->ddir);
547 io_u->error = -DER_INVAL;
548 return FIO_Q_COMPLETED;
555 static int daos_fio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
560 /* ioengine_ops for get_ioengine() */
561 FIO_STATIC struct ioengine_ops ioengine = {
563 .version = FIO_IOOPS_VERSION,
564 .flags = FIO_DISKLESSIO | FIO_NODISKUTIL,
566 .setup = daos_fio_setup,
567 .init = daos_fio_init,
568 .prep = daos_fio_prep,
569 .cleanup = daos_fio_cleanup,
571 .open_file = daos_fio_open,
572 .invalidate = daos_fio_invalidate,
573 .get_file_size = daos_fio_get_file_size,
574 .close_file = daos_fio_close,
575 .unlink_file = daos_fio_unlink,
577 .queue = daos_fio_queue,
578 .getevents = daos_fio_getevents,
579 .event = daos_fio_event,
580 .io_u_init = daos_fio_io_u_init,
581 .io_u_free = daos_fio_io_u_free,
583 .option_struct_size = sizeof(struct daos_fio_options),
587 static void fio_init fio_dfs_register(void)
589 register_ioengine(&ioengine);
592 static void fio_exit fio_dfs_unregister(void)
594 unregister_ioengine(&ioengine);