2 * pmemblk: IO engine that uses NVML libpmemblk to read and write data
4 * Copyright (C) 2016 Hewlett Packard Enterprise Development LP
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License,
8 * version 2 as published by the Free Software Foundation..
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the Free
17 * Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA
25 * IO engine that uses libpmemblk to read and write data
30 * Other relevant settings:
35 * filename=/pmem0/fiotestfile,BSIZE,FSIZEMB
37 * thread must be set to 1 for pmemblk as multiple processes cannot
38 * open the same block pool file.
40 * iodepth should be set to 1 as pmemblk is always synchronous.
41 * Use numjobs to scale up.
43 * direct=1 is implied as pmemblk is always direct.
45 * Can set unlink to 1 to remove the block pool file after testing.
47 * When specifying the filename, if the block pool file does not already
48 * exist, then the pmemblk engine can create the pool file if you specify
49 * the block and file sizes. BSIZE is the block size in bytes.
50 * FSIZEMB is the pool file size in MB.
52 * See examples/pmemblk.fio for more.
55 * By default, the pmemblk engine will let the system find the libpmemblk.so
56 * that it uses. You can use an alternative libpmemblk by setting the
57 * FIO_PMEMBLK_LIB environment variable to the full path to the desired
79 typedef struct PMEMblkpool_s PMEMblkpool;
81 PMEMblkpool* (*pmemblk_create)(const char*, size_t, size_t, mode_t) = NULL;
82 PMEMblkpool* (*pmemblk_open)(const char*, size_t) = NULL;
83 void (*pmemblk_close)(PMEMblkpool*) = NULL;
84 size_t (*pmemblk_nblock)(PMEMblkpool*) = NULL;
85 size_t (*pmemblk_bsize)(PMEMblkpool*) = NULL;
86 int (*pmemblk_read)(PMEMblkpool*, void*, off_t) = NULL;
87 int (*pmemblk_write)(PMEMblkpool*, const void*, off_t) = NULL;
97 path = "libpmemblk.so";
99 dl = dlopen(path, RTLD_NOW | RTLD_NODELETE);
103 if (NULL == (pmemblk_create = dlsym(dl, "pmemblk_create")))
105 if (NULL == (pmemblk_open = dlsym(dl, "pmemblk_open")))
107 if (NULL == (pmemblk_close = dlsym(dl, "pmemblk_close")))
109 if (NULL == (pmemblk_nblock = dlsym(dl, "pmemblk_nblock")))
111 if (NULL == (pmemblk_bsize = dlsym(dl, "pmemblk_bsize")))
113 if (NULL == (pmemblk_read = dlsym(dl, "pmemblk_read")))
115 if (NULL == (pmemblk_write = dlsym(dl, "pmemblk_write")))
121 log_err("fio: unable to load libpmemblk: %s\n", dlerror());
127 } /* load_libpmemblk() */
130 typedef struct fio_pmemblk_file* fio_pmemblk_file_t;
131 struct fio_pmemblk_file {
132 fio_pmemblk_file_t pmb_next;
135 PMEMblkpool* pmb_pool;
139 #define FIOFILEPMBSET(_f, _v) do { \
140 (_f)->engine_data = (uint64_t)(uintptr_t)(_v); \
142 #define FIOFILEPMBGET(_f) ((fio_pmemblk_file_t)((_f)->engine_data))
144 static fio_pmemblk_file_t Cache = NULL;
146 static pthread_mutex_t CacheLock = PTHREAD_MUTEX_INITIALIZER;
147 #define CACHE_LOCK() \
148 (void)pthread_mutex_lock(&CacheLock)
149 #define CACHE_UNLOCK() \
150 (void)pthread_mutex_unlock(&CacheLock)
152 #define PMB_CREATE (0x0001) /* should create file */
156 fio_pmemblk_cache_lookup(
160 fio_pmemblk_file_t i;
162 for (i = Cache; i != NULL; i = i->pmb_next)
163 if (0 == strcmp(filename, i->pmb_filename))
168 } /* fio_pmemblk_cache_lookup() */
172 fio_pmemblk_cache_insert(
173 fio_pmemblk_file_t pmb
176 pmb->pmb_next = Cache;
181 } /* fio_pmemblk_cache_insert() */
185 fio_pmemblk_cache_remove(
186 fio_pmemblk_file_t pmb
189 fio_pmemblk_file_t i;
192 Cache = Cache->pmb_next;
193 pmb->pmb_next = NULL;
197 for (i = Cache; i != NULL; i = i->pmb_next)
198 if (pmb == i->pmb_next) {
199 i->pmb_next = i->pmb_next->pmb_next;
200 pmb->pmb_next = NULL;
206 } /* fio_pmemblk_cache_remove() */
210 * to control block size and gross file size at the libpmemblk
211 * level, we allow the block size and file size to be appended
214 * path[,bsize,fsizemb]
216 * note that we do not use the fio option "filesize" to dictate
217 * the file size because we can only give libpmemblk the gross
218 * file size, which is different from the net or usable file
219 * size (which is probably what fio wants).
221 * the final path without the parameters is returned in ppath.
222 * the block size and file size are returned in pbsize and fsize.
224 * note that the user should specify the file size in MiB, but
225 * we return bytes from here.
229 const char* pathspec,
240 path = strdup(pathspec);
246 /* extract sizes, if given */
247 s = strrchr(path, ',');
248 if (s && (fsizemb = strtoull(s+1, NULL, 10))) {
250 s = strrchr(path, ',');
251 if (s && (bsize = strtoull(s+1, NULL, 10))) {
255 *pfsize = fsizemb << 20;
260 /* size specs not found */
261 strcpy(path, pathspec);
267 } /* pmb_parse_path() */
273 const char* pathspec,
277 fio_pmemblk_file_t pmb;
282 pmb_parse_path(pathspec, &path, &bsize, &fsize);
288 pmb = fio_pmemblk_cache_lookup(path);
291 /* load libpmemblk if needed */
292 if (NULL == pmemblk_open)
293 if (0 != load_libpmemblk(getenv("FIO_PMEMBLK_LIB")))
296 pmb = malloc(sizeof(*pmb));
300 /* try opening existing first, create it if needed */
301 pmb->pmb_pool = pmemblk_open(path, bsize);
302 if ((NULL == pmb->pmb_pool) &&
304 (flags & PMB_CREATE) &&
307 pmb->pmb_pool = pmemblk_create(path, bsize, fsize, 0644);
309 if (NULL == pmb->pmb_pool) {
310 log_err("fio: enable to open pmemblk pool file (errno %d)\n",
315 pmb->pmb_filename = path;
316 pmb->pmb_next = NULL;
318 pmb->pmb_bsize = pmemblk_bsize(pmb->pmb_pool);
319 pmb->pmb_nblocks = pmemblk_nblock(pmb->pmb_pool);
321 fio_pmemblk_cache_insert(pmb);
324 pmb->pmb_refcnt += 1;
332 if (NULL != pmb->pmb_pool)
333 pmemblk_close(pmb->pmb_pool);
334 pmb->pmb_pool = NULL;
335 pmb->pmb_filename = NULL;
348 fio_pmemblk_file_t pmb,
356 if (!keep && (0 == pmb->pmb_refcnt)) {
357 pmemblk_close(pmb->pmb_pool);
358 pmb->pmb_pool = NULL;
359 free(pmb->pmb_filename);
360 pmb->pmb_filename = NULL;
361 fio_pmemblk_cache_remove(pmb);
372 struct thread_data* td,
376 static int thread_warned = 0;
377 static int odirect_warned = 0;
381 if (!td->o.use_thread) {
382 if (!thread_warned) {
384 log_err("fio: must set thread=1 for pmemblk engine\n");
389 if (!td->o.odirect && !odirect_warned) {
391 log_info("fio: direct == 0, but pmemblk is always direct\n");
394 if (td->o.allow_create)
400 } /* pmb_get_flags() */
404 fio_pmemblk_open_file(
405 struct thread_data* td,
409 fio_pmemblk_file_t pmb;
411 if (0 != pmb_get_flags(td, &flags))
414 pmb = pmb_open(f->file_name, flags);
418 FIOFILEPMBSET(f, pmb);
422 } /* fio_pmemblk_open_file() */
426 fio_pmemblk_close_file(
427 struct thread_data fio_unused* td,
431 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
436 FIOFILEPMBSET(f, NULL);
440 } /* fio_pmemblk_close_file() */
444 fio_pmemblk_get_file_size(
445 struct thread_data* td,
450 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
452 if (fio_file_size_known(f))
456 if (0 != pmb_get_flags(td, &flags))
458 pmb = pmb_open(f->file_name, flags);
463 f->real_file_size = pmb->pmb_bsize * pmb->pmb_nblocks;
465 fio_file_set_size_known(f);
467 if (NULL == FIOFILEPMBGET(f))
472 } /* fio_pmemblk_get_file_size() */
477 struct thread_data* td,
480 struct fio_file* f = io_u->file;
481 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
483 unsigned long long off;
486 int (*blkop)(PMEMblkpool*, void*, off_t) = (void*)pmemblk_write;
488 fio_ro_check(td, io_u);
490 switch (io_u->ddir) {
492 blkop = pmemblk_read;
496 len = io_u->xfer_buflen;
498 io_u->error = EINVAL;
499 if (0 != (off % pmb->pmb_bsize))
501 if (0 != (len % pmb->pmb_bsize))
503 if ((off + len) / pmb->pmb_bsize > pmb->pmb_nblocks)
507 buf = io_u->xfer_buf;
508 off /= pmb->pmb_bsize;
509 len /= pmb->pmb_bsize;
511 if (0 != blkop(pmb->pmb_pool, buf, off)) {
515 buf += pmb->pmb_bsize;
519 off *= pmb->pmb_bsize;
520 len *= pmb->pmb_bsize;
521 io_u->resid = io_u->xfer_buflen - (off - io_u->offset);
525 case DDIR_SYNC_FILE_RANGE:
526 /* we're always sync'd */
530 io_u->error = EINVAL;
534 return FIO_Q_COMPLETED;
536 } /* fio_pmemblk_queue() */
540 fio_pmemblk_unlink_file(
541 struct thread_data* td,
550 * we need our own unlink in case the user has specified
551 * the block and file sizes in the path name. we parse
552 * the file_name to determine the file name we actually used.
555 pmb_parse_path(f->file_name, &path, &bsize, &fsize);
564 } /* fio_pmemblk_unlink_file() */
567 struct ioengine_ops ioengine = {
569 .version = FIO_IOOPS_VERSION,
570 .queue = fio_pmemblk_queue,
571 .open_file = fio_pmemblk_open_file,
572 .close_file = fio_pmemblk_close_file,
573 .get_file_size = fio_pmemblk_get_file_size,
574 .unlink_file = fio_pmemblk_unlink_file,
575 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL,
580 fio_init fio_pmemblk_register(void)
582 register_ioengine(&ioengine);
587 fio_exit fio_pmemblk_unregister(void)
589 unregister_ioengine(&ioengine);