2 * pmemblk: IO engine that uses PMDK 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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, 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;
90 static fio_pmemblk_file_t Cache;
92 static pthread_mutex_t CacheLock = PTHREAD_MUTEX_INITIALIZER;
94 #define PMB_CREATE (0x0001) /* should create file */
96 fio_pmemblk_file_t fio_pmemblk_cache_lookup(const char *filename)
100 for (i = Cache; i != NULL; i = i->pmb_next)
101 if (!strcmp(filename, i->pmb_filename))
107 static void fio_pmemblk_cache_insert(fio_pmemblk_file_t pmb)
109 pmb->pmb_next = Cache;
113 static void fio_pmemblk_cache_remove(fio_pmemblk_file_t pmb)
115 fio_pmemblk_file_t i;
118 Cache = Cache->pmb_next;
119 pmb->pmb_next = NULL;
123 for (i = Cache; i != NULL; i = i->pmb_next)
124 if (pmb == i->pmb_next) {
125 i->pmb_next = i->pmb_next->pmb_next;
126 pmb->pmb_next = NULL;
132 * to control block size and gross file size at the libpmemblk
133 * level, we allow the block size and file size to be appended
136 * path[,bsize,fsizemib]
138 * note that we do not use the fio option "filesize" to dictate
139 * the file size because we can only give libpmemblk the gross
140 * file size, which is different from the net or usable file
141 * size (which is probably what fio wants).
143 * the final path without the parameters is returned in ppath.
144 * the block size and file size are returned in pbsize and fsize.
146 * note that the user specifies the file size in MiB, but
147 * we return bytes from here.
149 static void pmb_parse_path(const char *pathspec, char **ppath, uint64_t *pbsize,
157 path = strdup(pathspec);
163 /* extract sizes, if given */
164 s = strrchr(path, ',');
165 if (s && (fsizemib = strtoull(s + 1, NULL, 10))) {
167 s = strrchr(path, ',');
168 if (s && (bsize = strtoull(s + 1, NULL, 10))) {
172 *pfsize = fsizemib << 20;
177 /* size specs not found */
178 strcpy(path, pathspec);
184 static fio_pmemblk_file_t pmb_open(const char *pathspec, int flags)
186 fio_pmemblk_file_t pmb;
191 pmb_parse_path(pathspec, &path, &bsize, &fsize);
195 pthread_mutex_lock(&CacheLock);
197 pmb = fio_pmemblk_cache_lookup(path);
199 pmb = malloc(sizeof(*pmb));
203 /* try opening existing first, create it if needed */
204 pmb->pmb_pool = pmemblk_open(path, bsize);
205 if (!pmb->pmb_pool && (errno == ENOENT) &&
206 (flags & PMB_CREATE) && (0 < fsize) && (0 < bsize)) {
208 pmemblk_create(path, bsize, fsize, 0644);
210 if (!pmb->pmb_pool) {
211 log_err("pmemblk: unable to open pmemblk pool file %s (%s)\n",
212 path, strerror(errno));
216 pmb->pmb_filename = path;
217 pmb->pmb_next = NULL;
219 pmb->pmb_bsize = pmemblk_bsize(pmb->pmb_pool);
220 pmb->pmb_nblocks = pmemblk_nblock(pmb->pmb_pool);
222 fio_pmemblk_cache_insert(pmb);
227 pmb->pmb_refcnt += 1;
229 pthread_mutex_unlock(&CacheLock);
236 pmemblk_close(pmb->pmb_pool);
237 pmb->pmb_pool = NULL;
238 pmb->pmb_filename = NULL;
244 pthread_mutex_unlock(&CacheLock);
248 static void pmb_close(fio_pmemblk_file_t pmb, const bool keep)
250 pthread_mutex_lock(&CacheLock);
254 if (!keep && !pmb->pmb_refcnt) {
255 pmemblk_close(pmb->pmb_pool);
256 pmb->pmb_pool = NULL;
257 free(pmb->pmb_filename);
258 pmb->pmb_filename = NULL;
259 fio_pmemblk_cache_remove(pmb);
263 pthread_mutex_unlock(&CacheLock);
266 static int pmb_get_flags(struct thread_data *td, uint64_t *pflags)
268 static int thread_warned = 0;
269 static int odirect_warned = 0;
273 if (!td->o.use_thread) {
274 if (!thread_warned) {
276 log_err("pmemblk: must set thread=1 for pmemblk engine\n");
281 if (!td->o.odirect && !odirect_warned) {
283 log_info("pmemblk: direct == 0, but pmemblk is always direct\n");
286 if (td->o.allow_create)
293 static int fio_pmemblk_open_file(struct thread_data *td, struct fio_file *f)
296 fio_pmemblk_file_t pmb;
298 if (pmb_get_flags(td, &flags))
301 pmb = pmb_open(f->file_name, flags);
305 FILE_SET_ENG_DATA(f, pmb);
309 static int fio_pmemblk_close_file(struct thread_data fio_unused *td,
312 fio_pmemblk_file_t pmb = FILE_ENG_DATA(f);
315 pmb_close(pmb, false);
317 FILE_SET_ENG_DATA(f, NULL);
321 static int fio_pmemblk_get_file_size(struct thread_data *td, struct fio_file *f)
324 fio_pmemblk_file_t pmb = FILE_ENG_DATA(f);
326 if (fio_file_size_known(f))
330 if (pmb_get_flags(td, &flags))
332 pmb = pmb_open(f->file_name, flags);
337 f->real_file_size = pmb->pmb_bsize * pmb->pmb_nblocks;
339 fio_file_set_size_known(f);
341 if (!FILE_ENG_DATA(f))
342 pmb_close(pmb, true);
347 static enum fio_q_status fio_pmemblk_queue(struct thread_data *td,
350 struct fio_file *f = io_u->file;
351 fio_pmemblk_file_t pmb = FILE_ENG_DATA(f);
353 unsigned long long off;
357 fio_ro_check(td, io_u);
359 switch (io_u->ddir) {
363 len = io_u->xfer_buflen;
365 io_u->error = EINVAL;
366 if (off % pmb->pmb_bsize)
368 if (len % pmb->pmb_bsize)
370 if ((off + len) / pmb->pmb_bsize > pmb->pmb_nblocks)
374 buf = io_u->xfer_buf;
375 off /= pmb->pmb_bsize;
376 len /= pmb->pmb_bsize;
378 if (io_u->ddir == DDIR_READ) {
379 if (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 FIO_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);