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
24 * IO engine that uses libpmemblk to read and write data
29 * Other relevant settings:
34 * filename=/mnt/pmem0/fiotestfile,BSIZE,FSIZEMiB
36 * thread must be set to 1 for pmemblk as multiple processes cannot
37 * open the same block pool file.
39 * iodepth should be set to 1 as pmemblk is always synchronous.
40 * Use numjobs to scale up.
42 * direct=1 is implied as pmemblk is always direct. A warning message
43 * is printed if this is not specified.
45 * unlink=1 removes the block pool file after testing, and is optional.
47 * The pmem device must have a DAX-capable filesystem and be mounted
48 * with DAX enabled. filename must point to a file on that filesystem.
53 * mount -o dax /dev/pmem0 /mnt/pmem0
55 * When specifying the filename, if the block pool file does not already
56 * exist, then the pmemblk engine creates the pool file if you specify
57 * the block and file sizes. BSIZE is the block size in bytes.
58 * FSIZEMB is the pool file size in MiB.
60 * See examples/pmemblk.fio for more.
72 #include <libpmemblk.h>
79 typedef struct fio_pmemblk_file *fio_pmemblk_file_t;
81 struct fio_pmemblk_file {
82 fio_pmemblk_file_t pmb_next;
85 PMEMblkpool *pmb_pool;
89 #define FIOFILEPMBSET(_f, _v) do { \
90 (_f)->engine_data = (uint64_t)(uintptr_t)(_v); \
92 #define FIOFILEPMBGET(_f) ((fio_pmemblk_file_t)((_f)->engine_data))
94 static fio_pmemblk_file_t Cache;
96 static pthread_mutex_t CacheLock = PTHREAD_MUTEX_INITIALIZER;
98 #define PMB_CREATE (0x0001) /* should create file */
100 fio_pmemblk_file_t fio_pmemblk_cache_lookup(const char *filename)
102 fio_pmemblk_file_t i;
104 for (i = Cache; i != NULL; i = i->pmb_next)
105 if (!strcmp(filename, i->pmb_filename))
111 static void fio_pmemblk_cache_insert(fio_pmemblk_file_t pmb)
113 pmb->pmb_next = Cache;
117 static void fio_pmemblk_cache_remove(fio_pmemblk_file_t pmb)
119 fio_pmemblk_file_t i;
122 Cache = Cache->pmb_next;
123 pmb->pmb_next = NULL;
127 for (i = Cache; i != NULL; i = i->pmb_next)
128 if (pmb == i->pmb_next) {
129 i->pmb_next = i->pmb_next->pmb_next;
130 pmb->pmb_next = NULL;
136 * to control block size and gross file size at the libpmemblk
137 * level, we allow the block size and file size to be appended
140 * path[,bsize,fsizemib]
142 * note that we do not use the fio option "filesize" to dictate
143 * the file size because we can only give libpmemblk the gross
144 * file size, which is different from the net or usable file
145 * size (which is probably what fio wants).
147 * the final path without the parameters is returned in ppath.
148 * the block size and file size are returned in pbsize and fsize.
150 * note that the user specifies the file size in MiB, but
151 * we return bytes from here.
153 static void pmb_parse_path(const char *pathspec, char **ppath, uint64_t *pbsize,
161 path = strdup(pathspec);
167 /* extract sizes, if given */
168 s = strrchr(path, ',');
169 if (s && (fsizemib = strtoull(s + 1, NULL, 10))) {
171 s = strrchr(path, ',');
172 if (s && (bsize = strtoull(s + 1, NULL, 10))) {
176 *pfsize = fsizemib << 20;
181 /* size specs not found */
182 strcpy(path, pathspec);
188 static fio_pmemblk_file_t pmb_open(const char *pathspec, int flags)
190 fio_pmemblk_file_t pmb;
195 pmb_parse_path(pathspec, &path, &bsize, &fsize);
199 pthread_mutex_lock(&CacheLock);
201 pmb = fio_pmemblk_cache_lookup(path);
203 pmb = malloc(sizeof(*pmb));
207 /* try opening existing first, create it if needed */
208 pmb->pmb_pool = pmemblk_open(path, bsize);
209 if (!pmb->pmb_pool && (errno == ENOENT) &&
210 (flags & PMB_CREATE) && (0 < fsize) && (0 < bsize)) {
212 pmemblk_create(path, bsize, fsize, 0644);
214 if (!pmb->pmb_pool) {
215 log_err("pmemblk: unable to open pmemblk pool file %s (%s)\n",
216 path, strerror(errno));
220 pmb->pmb_filename = path;
221 pmb->pmb_next = NULL;
223 pmb->pmb_bsize = pmemblk_bsize(pmb->pmb_pool);
224 pmb->pmb_nblocks = pmemblk_nblock(pmb->pmb_pool);
226 fio_pmemblk_cache_insert(pmb);
229 pmb->pmb_refcnt += 1;
231 pthread_mutex_unlock(&CacheLock);
238 pmemblk_close(pmb->pmb_pool);
239 pmb->pmb_pool = NULL;
240 pmb->pmb_filename = NULL;
246 pthread_mutex_unlock(&CacheLock);
250 static void pmb_close(fio_pmemblk_file_t pmb, const bool keep)
252 pthread_mutex_lock(&CacheLock);
256 if (!keep && !pmb->pmb_refcnt) {
257 pmemblk_close(pmb->pmb_pool);
258 pmb->pmb_pool = NULL;
259 free(pmb->pmb_filename);
260 pmb->pmb_filename = NULL;
261 fio_pmemblk_cache_remove(pmb);
265 pthread_mutex_unlock(&CacheLock);
268 static int pmb_get_flags(struct thread_data *td, uint64_t *pflags)
270 static int thread_warned = 0;
271 static int odirect_warned = 0;
275 if (!td->o.use_thread) {
276 if (!thread_warned) {
278 log_err("pmemblk: must set thread=1 for pmemblk engine\n");
283 if (!td->o.odirect && !odirect_warned) {
285 log_info("pmemblk: direct == 0, but pmemblk is always direct\n");
288 if (td->o.allow_create)
295 static int fio_pmemblk_open_file(struct thread_data *td, struct fio_file *f)
298 fio_pmemblk_file_t pmb;
300 if (pmb_get_flags(td, &flags))
303 pmb = pmb_open(f->file_name, flags);
307 FIOFILEPMBSET(f, pmb);
311 static int fio_pmemblk_close_file(struct thread_data fio_unused *td,
314 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
317 pmb_close(pmb, false);
319 FIOFILEPMBSET(f, NULL);
323 static int fio_pmemblk_get_file_size(struct thread_data *td, struct fio_file *f)
326 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
328 if (fio_file_size_known(f))
332 if (pmb_get_flags(td, &flags))
334 pmb = pmb_open(f->file_name, flags);
339 f->real_file_size = pmb->pmb_bsize * pmb->pmb_nblocks;
341 fio_file_set_size_known(f);
343 if (!FIOFILEPMBGET(f))
344 pmb_close(pmb, true);
349 static int fio_pmemblk_queue(struct thread_data *td, struct io_u *io_u)
351 struct fio_file *f = io_u->file;
352 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
354 unsigned long long off;
358 fio_ro_check(td, io_u);
360 switch (io_u->ddir) {
364 len = io_u->xfer_buflen;
366 io_u->error = EINVAL;
367 if (off % pmb->pmb_bsize)
369 if (len % pmb->pmb_bsize)
371 if ((off + len) / pmb->pmb_bsize > pmb->pmb_nblocks)
375 buf = io_u->xfer_buf;
376 off /= pmb->pmb_bsize;
377 len /= pmb->pmb_bsize;
379 if (io_u->ddir == DDIR_READ &&
380 0 != pmemblk_read(pmb->pmb_pool, buf, off)) {
383 } else if (0 != pmemblk_write(pmb->pmb_pool, buf, off)) {
387 buf += pmb->pmb_bsize;
391 off *= pmb->pmb_bsize;
392 len *= pmb->pmb_bsize;
393 io_u->resid = io_u->xfer_buflen - (off - io_u->offset);
397 case DDIR_SYNC_FILE_RANGE:
398 /* we're always sync'd */
402 io_u->error = EINVAL;
406 return FIO_Q_COMPLETED;
409 static int fio_pmemblk_unlink_file(struct thread_data *td, struct fio_file *f)
416 * we need our own unlink in case the user has specified
417 * the block and file sizes in the path name. we parse
418 * the file_name to determine the file name we actually used.
421 pmb_parse_path(f->file_name, &path, &bsize, &fsize);
430 static struct ioengine_ops ioengine = {
432 .version = FIO_IOOPS_VERSION,
433 .queue = fio_pmemblk_queue,
434 .open_file = fio_pmemblk_open_file,
435 .close_file = fio_pmemblk_close_file,
436 .get_file_size = fio_pmemblk_get_file_size,
437 .unlink_file = fio_pmemblk_unlink_file,
438 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL,
441 static void fio_init fio_pmemblk_register(void)
443 register_ioengine(&ioengine);
446 static void fio_exit fio_pmemblk_unregister(void)
448 unregister_ioengine(&ioengine);