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_ptr)(const char*, size_t, size_t, mode_t) = NULL;
82 PMEMblkpool* (*pmemblk_open_ptr)(const char*, size_t) = NULL;
83 void (*pmemblk_close_ptr)(PMEMblkpool*) = NULL;
84 size_t (*pmemblk_nblock_ptr)(PMEMblkpool*) = NULL;
85 size_t (*pmemblk_bsize_ptr)(PMEMblkpool*) = NULL;
86 int (*pmemblk_read_ptr)(PMEMblkpool*, void*, off_t) = NULL;
87 int (*pmemblk_write_ptr)(PMEMblkpool*, const void*, off_t) = NULL;
97 path = "libpmemblk.so";
99 dl = dlopen(path, RTLD_NOW | RTLD_NODELETE);
103 if (NULL == (pmemblk_create_ptr = dlsym(dl, "pmemblk_create")))
105 if (NULL == (pmemblk_open_ptr = dlsym(dl, "pmemblk_open")))
107 if (NULL == (pmemblk_close_ptr = dlsym(dl, "pmemblk_close")))
109 if (NULL == (pmemblk_nblock_ptr = dlsym(dl, "pmemblk_nblock")))
111 if (NULL == (pmemblk_bsize_ptr = dlsym(dl, "pmemblk_bsize")))
113 if (NULL == (pmemblk_read_ptr = dlsym(dl, "pmemblk_read")))
115 if (NULL == (pmemblk_write_ptr = dlsym(dl, "pmemblk_write")))
121 log_err("fio: unable to load libpmemblk: %s\n", dlerror());
127 } /* load_libpmemblk() */
129 #define pmemblk_create pmemblk_create_ptr
130 #define pmemblk_open pmemblk_open_ptr
131 #define pmemblk_close pmemblk_close_ptr
132 #define pmemblk_nblock pmemblk_nblock_ptr
133 #define pmemblk_bsize pmemblk_bsize_ptr
134 #define pmemblk_read pmemblk_read_ptr
135 #define pmemblk_write pmemblk_write_ptr
138 typedef struct fio_pmemblk_file* fio_pmemblk_file_t;
139 struct fio_pmemblk_file {
140 fio_pmemblk_file_t pmb_next;
143 PMEMblkpool* pmb_pool;
147 #define FIOFILEPMBSET(_f, _v) do { \
148 (_f)->engine_data = (uint64_t)(uintptr_t)(_v); \
150 #define FIOFILEPMBGET(_f) ((fio_pmemblk_file_t)((_f)->engine_data))
152 static fio_pmemblk_file_t Cache = NULL;
154 static pthread_mutex_t CacheLock = PTHREAD_MUTEX_INITIALIZER;
155 #define CACHE_LOCK() \
156 (void)pthread_mutex_lock(&CacheLock)
157 #define CACHE_UNLOCK() \
158 (void)pthread_mutex_unlock(&CacheLock)
160 #define PMB_CREATE (0x0001) /* should create file */
164 fio_pmemblk_cache_lookup(
168 fio_pmemblk_file_t i;
170 for (i = Cache; i != NULL; i = i->pmb_next)
171 if (0 == strcmp(filename, i->pmb_filename))
176 } /* fio_pmemblk_cache_lookup() */
180 fio_pmemblk_cache_insert(
181 fio_pmemblk_file_t pmb
184 pmb->pmb_next = Cache;
189 } /* fio_pmemblk_cache_insert() */
193 fio_pmemblk_cache_remove(
194 fio_pmemblk_file_t pmb
197 fio_pmemblk_file_t i;
200 Cache = Cache->pmb_next;
201 pmb->pmb_next = NULL;
205 for (i = Cache; i != NULL; i = i->pmb_next)
206 if (pmb == i->pmb_next) {
207 i->pmb_next = i->pmb_next->pmb_next;
208 pmb->pmb_next = NULL;
214 } /* fio_pmemblk_cache_remove() */
218 * to control block size and gross file size at the libpmemblk
219 * level, we allow the block size and file size to be appended
222 * path[,bsize,fsizemb]
224 * note that we do not use the fio option "filesize" to dictate
225 * the file size because we can only give libpmemblk the gross
226 * file size, which is different from the net or usable file
227 * size (which is probably what fio wants).
229 * the final path without the parameters is returned in ppath.
230 * the block size and file size are returned in pbsize and fsize.
232 * note that the user should specify the file size in MiB, but
233 * we return bytes from here.
237 const char* pathspec,
248 path = strdup(pathspec);
254 /* extract sizes, if given */
255 s = strrchr(path, ',');
256 if (s && (fsizemb = strtoull(s+1, NULL, 10))) {
258 s = strrchr(path, ',');
259 if (s && (bsize = strtoull(s+1, NULL, 10))) {
263 *pfsize = fsizemb << 20;
268 /* size specs not found */
269 strcpy(path, pathspec);
275 } /* pmb_parse_path() */
281 const char* pathspec,
285 fio_pmemblk_file_t pmb;
290 pmb_parse_path(pathspec, &path, &bsize, &fsize);
296 pmb = fio_pmemblk_cache_lookup(path);
299 /* load libpmemblk if needed */
300 if (NULL == pmemblk_open)
301 if (0 != load_libpmemblk(getenv("FIO_PMEMBLK_LIB")))
304 pmb = malloc(sizeof(*pmb));
308 /* try opening existing first, create it if needed */
309 pmb->pmb_pool = pmemblk_open(path, bsize);
310 if ((NULL == pmb->pmb_pool) &&
312 (flags & PMB_CREATE) &&
315 pmb->pmb_pool = pmemblk_create(path, bsize, fsize, 0644);
317 if (NULL == pmb->pmb_pool) {
318 log_err("fio: enable to open pmemblk pool file (errno %d)\n",
323 pmb->pmb_filename = path;
324 pmb->pmb_next = NULL;
326 pmb->pmb_bsize = pmemblk_bsize(pmb->pmb_pool);
327 pmb->pmb_nblocks = pmemblk_nblock(pmb->pmb_pool);
329 fio_pmemblk_cache_insert(pmb);
332 pmb->pmb_refcnt += 1;
340 if (NULL != pmb->pmb_pool)
341 pmemblk_close(pmb->pmb_pool);
342 pmb->pmb_pool = NULL;
343 pmb->pmb_filename = NULL;
356 fio_pmemblk_file_t pmb,
364 if (!keep && (0 == pmb->pmb_refcnt)) {
365 pmemblk_close(pmb->pmb_pool);
366 pmb->pmb_pool = NULL;
367 free(pmb->pmb_filename);
368 pmb->pmb_filename = NULL;
369 fio_pmemblk_cache_remove(pmb);
380 struct thread_data* td,
384 static int thread_warned = 0;
385 static int odirect_warned = 0;
389 if (!td->o.use_thread) {
390 if (!thread_warned) {
392 log_err("fio: must set thread=1 for pmemblk engine\n");
397 if (!td->o.odirect && !odirect_warned) {
399 log_info("fio: direct == 0, but pmemblk is always direct\n");
402 if (td->o.allow_create)
408 } /* pmb_get_flags() */
412 fio_pmemblk_open_file(
413 struct thread_data* td,
417 fio_pmemblk_file_t pmb;
419 if (0 != pmb_get_flags(td, &flags))
422 pmb = pmb_open(f->file_name, flags);
426 FIOFILEPMBSET(f, pmb);
430 } /* fio_pmemblk_open_file() */
434 fio_pmemblk_close_file(
435 struct thread_data fio_unused* td,
439 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
444 FIOFILEPMBSET(f, NULL);
448 } /* fio_pmemblk_close_file() */
452 fio_pmemblk_get_file_size(
453 struct thread_data* td,
458 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
460 if (fio_file_size_known(f))
464 if (0 != pmb_get_flags(td, &flags))
466 pmb = pmb_open(f->file_name, flags);
471 f->real_file_size = pmb->pmb_bsize * pmb->pmb_nblocks;
473 fio_file_set_size_known(f);
475 if (NULL == FIOFILEPMBGET(f))
480 } /* fio_pmemblk_get_file_size() */
485 struct thread_data* td,
488 struct fio_file* f = io_u->file;
489 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
491 unsigned long long off;
494 int (*blkop)(PMEMblkpool*, void*, off_t) = (void*)pmemblk_write;
496 fio_ro_check(td, io_u);
498 switch (io_u->ddir) {
500 blkop = pmemblk_read;
504 len = io_u->xfer_buflen;
506 io_u->error = EINVAL;
507 if (0 != (off % pmb->pmb_bsize))
509 if (0 != (len % pmb->pmb_bsize))
511 if ((off + len) / pmb->pmb_bsize > pmb->pmb_nblocks)
515 buf = io_u->xfer_buf;
516 off /= pmb->pmb_bsize;
517 len /= pmb->pmb_bsize;
519 if (0 != blkop(pmb->pmb_pool, buf, off)) {
523 buf += pmb->pmb_bsize;
527 off *= pmb->pmb_bsize;
528 len *= pmb->pmb_bsize;
529 io_u->resid = io_u->xfer_buflen - (off - io_u->offset);
533 case DDIR_SYNC_FILE_RANGE:
534 /* we're always sync'd */
538 io_u->error = EINVAL;
542 return FIO_Q_COMPLETED;
544 } /* fio_pmemblk_queue() */
548 fio_pmemblk_unlink_file(
549 struct thread_data* td,
558 * we need our own unlink in case the user has specified
559 * the block and file sizes in the path name. we parse
560 * the file_name to determine the file name we actually used.
563 pmb_parse_path(f->file_name, &path, &bsize, &fsize);
572 } /* fio_pmemblk_unlink_file() */
575 struct ioengine_ops ioengine = {
577 .version = FIO_IOOPS_VERSION,
578 .queue = fio_pmemblk_queue,
579 .open_file = fio_pmemblk_open_file,
580 .close_file = fio_pmemblk_close_file,
581 .get_file_size = fio_pmemblk_get_file_size,
582 .unlink_file = fio_pmemblk_unlink_file,
583 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL,
588 fio_init fio_pmemblk_register(void)
590 register_ioengine(&ioengine);
595 fio_exit fio_pmemblk_unregister(void)
597 unregister_ioengine(&ioengine);