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=/pmem0/fiotestfile,BSIZE,FSIZEMB
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.
44 * Can set unlink to 1 to remove the block pool file after testing.
46 * When specifying the filename, if the block pool file does not already
47 * exist, then the pmemblk engine can create the pool file if you specify
48 * the block and file sizes. BSIZE is the block size in bytes.
49 * FSIZEMB is the pool file size in MB.
51 * See examples/pmemblk.fio for more.
63 #include <libpmemblk.h>
70 typedef struct fio_pmemblk_file *fio_pmemblk_file_t;
72 struct fio_pmemblk_file {
73 fio_pmemblk_file_t pmb_next;
76 PMEMblkpool *pmb_pool;
80 #define FIOFILEPMBSET(_f, _v) do { \
81 (_f)->engine_data = (uint64_t)(uintptr_t)(_v); \
83 #define FIOFILEPMBGET(_f) ((fio_pmemblk_file_t)((_f)->engine_data))
85 static fio_pmemblk_file_t Cache;
87 static pthread_mutex_t CacheLock = PTHREAD_MUTEX_INITIALIZER;
89 #define PMB_CREATE (0x0001) /* should create file */
91 fio_pmemblk_file_t fio_pmemblk_cache_lookup(const char *filename)
95 for (i = Cache; i != NULL; i = i->pmb_next)
96 if (!strcmp(filename, i->pmb_filename))
102 static void fio_pmemblk_cache_insert(fio_pmemblk_file_t pmb)
104 pmb->pmb_next = Cache;
108 static void fio_pmemblk_cache_remove(fio_pmemblk_file_t pmb)
110 fio_pmemblk_file_t i;
113 Cache = Cache->pmb_next;
114 pmb->pmb_next = NULL;
118 for (i = Cache; i != NULL; i = i->pmb_next)
119 if (pmb == i->pmb_next) {
120 i->pmb_next = i->pmb_next->pmb_next;
121 pmb->pmb_next = NULL;
127 * to control block size and gross file size at the libpmemblk
128 * level, we allow the block size and file size to be appended
131 * path[,bsize,fsizemb]
133 * note that we do not use the fio option "filesize" to dictate
134 * the file size because we can only give libpmemblk the gross
135 * file size, which is different from the net or usable file
136 * size (which is probably what fio wants).
138 * the final path without the parameters is returned in ppath.
139 * the block size and file size are returned in pbsize and fsize.
141 * note that the user should specify the file size in MiB, but
142 * we return bytes from here.
144 static void pmb_parse_path(const char *pathspec, char **ppath, uint64_t *pbsize,
152 path = strdup(pathspec);
158 /* extract sizes, if given */
159 s = strrchr(path, ',');
160 if (s && (fsizemb = strtoull(s + 1, NULL, 10))) {
162 s = strrchr(path, ',');
163 if (s && (bsize = strtoull(s + 1, NULL, 10))) {
167 *pfsize = fsizemb << 20;
172 /* size specs not found */
173 strcpy(path, pathspec);
179 static fio_pmemblk_file_t pmb_open(const char *pathspec, int flags)
181 fio_pmemblk_file_t pmb;
186 pmb_parse_path(pathspec, &path, &bsize, &fsize);
190 pthread_mutex_lock(&CacheLock);
192 pmb = fio_pmemblk_cache_lookup(path);
194 pmb = malloc(sizeof(*pmb));
198 /* try opening existing first, create it if needed */
199 pmb->pmb_pool = pmemblk_open(path, bsize);
200 if (!pmb->pmb_pool && (errno == ENOENT) &&
201 (flags & PMB_CREATE) && (0 < fsize) && (0 < bsize)) {
203 pmemblk_create(path, bsize, fsize, 0644);
205 if (!pmb->pmb_pool) {
206 log_err("pmemblk: unable to open pmemblk pool file %s (%s)\n",
207 path, strerror(errno));
211 pmb->pmb_filename = path;
212 pmb->pmb_next = NULL;
214 pmb->pmb_bsize = pmemblk_bsize(pmb->pmb_pool);
215 pmb->pmb_nblocks = pmemblk_nblock(pmb->pmb_pool);
217 fio_pmemblk_cache_insert(pmb);
220 pmb->pmb_refcnt += 1;
222 pthread_mutex_unlock(&CacheLock);
229 pmemblk_close(pmb->pmb_pool);
230 pmb->pmb_pool = NULL;
231 pmb->pmb_filename = NULL;
237 pthread_mutex_unlock(&CacheLock);
241 static void pmb_close(fio_pmemblk_file_t pmb, const bool keep)
243 pthread_mutex_lock(&CacheLock);
247 if (!keep && !pmb->pmb_refcnt) {
248 pmemblk_close(pmb->pmb_pool);
249 pmb->pmb_pool = NULL;
250 free(pmb->pmb_filename);
251 pmb->pmb_filename = NULL;
252 fio_pmemblk_cache_remove(pmb);
256 pthread_mutex_unlock(&CacheLock);
259 static int pmb_get_flags(struct thread_data *td, uint64_t *pflags)
261 static int thread_warned = 0;
262 static int odirect_warned = 0;
266 if (!td->o.use_thread) {
267 if (!thread_warned) {
269 log_err("pmemblk: must set thread=1 for pmemblk engine\n");
274 if (!td->o.odirect && !odirect_warned) {
276 log_info("pmemblk: direct == 0, but pmemblk is always direct\n");
279 if (td->o.allow_create)
286 static int fio_pmemblk_open_file(struct thread_data *td, struct fio_file *f)
289 fio_pmemblk_file_t pmb;
291 if (pmb_get_flags(td, &flags))
294 pmb = pmb_open(f->file_name, flags);
298 FIOFILEPMBSET(f, pmb);
302 static int fio_pmemblk_close_file(struct thread_data fio_unused *td,
305 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
308 pmb_close(pmb, false);
310 FIOFILEPMBSET(f, NULL);
314 static int fio_pmemblk_get_file_size(struct thread_data *td, struct fio_file *f)
317 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
319 if (fio_file_size_known(f))
323 if (pmb_get_flags(td, &flags))
325 pmb = pmb_open(f->file_name, flags);
330 f->real_file_size = pmb->pmb_bsize * pmb->pmb_nblocks;
332 fio_file_set_size_known(f);
334 if (!FIOFILEPMBGET(f))
335 pmb_close(pmb, true);
340 static int fio_pmemblk_queue(struct thread_data *td, struct io_u *io_u)
342 struct fio_file *f = io_u->file;
343 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
345 unsigned long long off;
349 fio_ro_check(td, io_u);
351 switch (io_u->ddir) {
355 len = io_u->xfer_buflen;
357 io_u->error = EINVAL;
358 if (off % pmb->pmb_bsize)
360 if (len % pmb->pmb_bsize)
362 if ((off + len) / pmb->pmb_bsize > pmb->pmb_nblocks)
366 buf = io_u->xfer_buf;
367 off /= pmb->pmb_bsize;
368 len /= pmb->pmb_bsize;
370 if (io_u->ddir == DDIR_READ &&
371 0 != pmemblk_read(pmb->pmb_pool, buf, off)) {
374 } else if (0 != pmemblk_write(pmb->pmb_pool, buf, off)) {
378 buf += pmb->pmb_bsize;
382 off *= pmb->pmb_bsize;
383 len *= pmb->pmb_bsize;
384 io_u->resid = io_u->xfer_buflen - (off - io_u->offset);
388 case DDIR_SYNC_FILE_RANGE:
389 /* we're always sync'd */
393 io_u->error = EINVAL;
397 return FIO_Q_COMPLETED;
400 static int fio_pmemblk_unlink_file(struct thread_data *td, struct fio_file *f)
407 * we need our own unlink in case the user has specified
408 * the block and file sizes in the path name. we parse
409 * the file_name to determine the file name we actually used.
412 pmb_parse_path(f->file_name, &path, &bsize, &fsize);
421 static struct ioengine_ops ioengine = {
423 .version = FIO_IOOPS_VERSION,
424 .queue = fio_pmemblk_queue,
425 .open_file = fio_pmemblk_open_file,
426 .close_file = fio_pmemblk_close_file,
427 .get_file_size = fio_pmemblk_get_file_size,
428 .unlink_file = fio_pmemblk_unlink_file,
429 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL,
432 static void fio_init fio_pmemblk_register(void)
434 register_ioengine(&ioengine);
437 static void fio_exit fio_pmemblk_unregister(void)
439 unregister_ioengine(&ioengine);