Change default IO engine from sync to psync
[fio.git] / engines / pmemblk.c
CommitLineData
43f3cec2
BB
1/*
2 * pmemblk: IO engine that uses NVML libpmemblk to read and write data
3 *
4 * Copyright (C) 2016 Hewlett Packard Enterprise Development LP
5 *
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..
9 *
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.
14 *
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
19 */
20
43f3cec2
BB
21/*
22 * pmemblk engine
23 *
24 * IO engine that uses libpmemblk to read and write data
25 *
f1d85c6e
BB
26 * To use:
27 * ioengine=pmemblk
43f3cec2
BB
28 *
29 * Other relevant settings:
30 * iodepth=1
31 * direct=1
32 * thread=1 REQUIRED
33 * unlink=1
34 * filename=/pmem0/fiotestfile,BSIZE,FSIZEMB
35 *
36 * thread must be set to 1 for pmemblk as multiple processes cannot
37 * open the same block pool file.
38 *
39 * iodepth should be set to 1 as pmemblk is always synchronous.
40 * Use numjobs to scale up.
41 *
42 * direct=1 is implied as pmemblk is always direct.
43 *
44 * Can set unlink to 1 to remove the block pool file after testing.
45 *
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.
50 *
f1d85c6e
BB
51 * See examples/pmemblk.fio for more.
52 *
43f3cec2
BB
53 * libpmemblk.so
54 * By default, the pmemblk engine will let the system find the libpmemblk.so
55 * that it uses. You can use an alternative libpmemblk by setting the
56 * FIO_PMEMBLK_LIB environment variable to the full path to the desired
57 * libpmemblk.so.
58 *
59 */
60
61#include <stdio.h>
62#include <stdlib.h>
63#include <unistd.h>
64#include <sys/uio.h>
65#include <errno.h>
66#include <assert.h>
67#include <dlfcn.h>
68#include <string.h>
69
70#include "../fio.h"
71
43f3cec2
BB
72/*
73 * libpmemblk
74 */
75struct PMEMblkpool_s;
76typedef struct PMEMblkpool_s PMEMblkpool;
77
c086b314
JA
78PMEMblkpool *(*pmemblk_create) (const char *, size_t, size_t, mode_t) = NULL;
79PMEMblkpool *(*pmemblk_open) (const char *, size_t) = NULL;
80void (*pmemblk_close) (PMEMblkpool *) = NULL;
81size_t(*pmemblk_nblock) (PMEMblkpool *) = NULL;
82size_t(*pmemblk_bsize) (PMEMblkpool *) = NULL;
83int (*pmemblk_read) (PMEMblkpool *, void *, off_t) = NULL;
84int (*pmemblk_write) (PMEMblkpool *, const void *, off_t) = NULL;
85
86int load_libpmemblk(const char *path)
43f3cec2 87{
c086b314 88 void *dl;
43f3cec2
BB
89
90 if (NULL == path)
91 path = "libpmemblk.so";
92
93 dl = dlopen(path, RTLD_NOW | RTLD_NODELETE);
94 if (NULL == dl)
95 goto errorout;
96
dfec8168 97 if (NULL == (pmemblk_create = dlsym(dl, "pmemblk_create")))
43f3cec2 98 goto errorout;
dfec8168 99 if (NULL == (pmemblk_open = dlsym(dl, "pmemblk_open")))
43f3cec2 100 goto errorout;
dfec8168 101 if (NULL == (pmemblk_close = dlsym(dl, "pmemblk_close")))
43f3cec2 102 goto errorout;
dfec8168 103 if (NULL == (pmemblk_nblock = dlsym(dl, "pmemblk_nblock")))
43f3cec2 104 goto errorout;
dfec8168 105 if (NULL == (pmemblk_bsize = dlsym(dl, "pmemblk_bsize")))
43f3cec2 106 goto errorout;
dfec8168 107 if (NULL == (pmemblk_read = dlsym(dl, "pmemblk_read")))
43f3cec2 108 goto errorout;
dfec8168 109 if (NULL == (pmemblk_write = dlsym(dl, "pmemblk_write")))
43f3cec2 110 goto errorout;
c086b314 111
43f3cec2
BB
112 return 0;
113
114errorout:
115 log_err("fio: unable to load libpmemblk: %s\n", dlerror());
116 if (NULL != dl)
117 dlclose(dl);
118
119 return (-1);
43f3cec2 120
c086b314 121} /* load_libpmemblk() */
43f3cec2 122
c086b314 123typedef struct fio_pmemblk_file *fio_pmemblk_file_t;
43f3cec2 124struct fio_pmemblk_file {
c086b314
JA
125 fio_pmemblk_file_t pmb_next;
126 char *pmb_filename;
127 uint64_t pmb_refcnt;
128 PMEMblkpool *pmb_pool;
129 size_t pmb_bsize;
130 size_t pmb_nblocks;
43f3cec2
BB
131};
132#define FIOFILEPMBSET(_f, _v) do { \
133 (_f)->engine_data = (uint64_t)(uintptr_t)(_v); \
134} while(0)
135#define FIOFILEPMBGET(_f) ((fio_pmemblk_file_t)((_f)->engine_data))
136
c086b314 137static fio_pmemblk_file_t Cache = NULL;
43f3cec2 138
c086b314 139static pthread_mutex_t CacheLock = PTHREAD_MUTEX_INITIALIZER;
43f3cec2 140
c086b314 141#define PMB_CREATE (0x0001) /* should create file */
43f3cec2 142
c086b314 143fio_pmemblk_file_t fio_pmemblk_cache_lookup(const char *filename)
43f3cec2 144{
c086b314 145 fio_pmemblk_file_t i;
43f3cec2
BB
146
147 for (i = Cache; i != NULL; i = i->pmb_next)
148 if (0 == strcmp(filename, i->pmb_filename))
149 return i;
150
151 return NULL;
152
c086b314 153} /* fio_pmemblk_cache_lookup() */
43f3cec2 154
c086b314 155static void fio_pmemblk_cache_insert(fio_pmemblk_file_t pmb)
43f3cec2
BB
156{
157 pmb->pmb_next = Cache;
158 Cache = pmb;
159
160 return;
161
c086b314 162} /* fio_pmemblk_cache_insert() */
43f3cec2 163
c086b314 164static void fio_pmemblk_cache_remove(fio_pmemblk_file_t pmb)
43f3cec2 165{
c086b314 166 fio_pmemblk_file_t i;
43f3cec2
BB
167
168 if (pmb == Cache) {
169 Cache = Cache->pmb_next;
170 pmb->pmb_next = NULL;
171 return;
172 }
173
174 for (i = Cache; i != NULL; i = i->pmb_next)
175 if (pmb == i->pmb_next) {
176 i->pmb_next = i->pmb_next->pmb_next;
177 pmb->pmb_next = NULL;
178 return;
179 }
180
181 return;
182
c086b314 183} /* fio_pmemblk_cache_remove() */
43f3cec2
BB
184
185/*
186 * to control block size and gross file size at the libpmemblk
187 * level, we allow the block size and file size to be appended
188 * to the file name:
189 *
190 * path[,bsize,fsizemb]
191 *
192 * note that we do not use the fio option "filesize" to dictate
193 * the file size because we can only give libpmemblk the gross
194 * file size, which is different from the net or usable file
195 * size (which is probably what fio wants).
196 *
197 * the final path without the parameters is returned in ppath.
198 * the block size and file size are returned in pbsize and fsize.
199 *
200 * note that the user should specify the file size in MiB, but
201 * we return bytes from here.
202 */
203static void
c086b314
JA
204pmb_parse_path(const char *pathspec,
205 char **ppath, uint64_t * pbsize, uint64_t * pfsize)
43f3cec2 206{
c086b314
JA
207 char *path;
208 char *s;
209 uint64_t bsize;
210 uint64_t fsizemb;
43f3cec2
BB
211
212 path = strdup(pathspec);
213 if (NULL == path) {
214 *ppath = NULL;
215 return;
216 }
217
218 /* extract sizes, if given */
219 s = strrchr(path, ',');
c086b314 220 if (s && (fsizemb = strtoull(s + 1, NULL, 10))) {
43f3cec2
BB
221 *s = 0;
222 s = strrchr(path, ',');
c086b314 223 if (s && (bsize = strtoull(s + 1, NULL, 10))) {
43f3cec2 224 *s = 0;
c086b314 225 *ppath = path;
43f3cec2
BB
226 *pbsize = bsize;
227 *pfsize = fsizemb << 20;
228 return;
229 }
230 }
231
232 /* size specs not found */
233 strcpy(path, pathspec);
c086b314 234 *ppath = path;
43f3cec2
BB
235 *pbsize = 0;
236 *pfsize = 0;
237 return;
238
c086b314 239} /* pmb_parse_path() */
43f3cec2
BB
240
241static
c086b314 242 fio_pmemblk_file_t pmb_open(const char *pathspec, int flags)
43f3cec2 243{
c086b314
JA
244 fio_pmemblk_file_t pmb;
245 char *path = NULL;
246 uint64_t bsize = 0;
247 uint64_t fsize = 0;
43f3cec2
BB
248
249 pmb_parse_path(pathspec, &path, &bsize, &fsize);
250 if (NULL == path)
251 return NULL;
252
de88999f 253 pthread_mutex_lock(&CacheLock);
43f3cec2
BB
254
255 pmb = fio_pmemblk_cache_lookup(path);
256
257 if (NULL == pmb) {
258 /* load libpmemblk if needed */
259 if (NULL == pmemblk_open)
260 if (0 != load_libpmemblk(getenv("FIO_PMEMBLK_LIB")))
261 goto error;
262
263 pmb = malloc(sizeof(*pmb));
264 if (NULL == pmb)
265 goto error;
266
267 /* try opening existing first, create it if needed */
268 pmb->pmb_pool = pmemblk_open(path, bsize);
269 if ((NULL == pmb->pmb_pool) &&
270 (ENOENT == errno) &&
c086b314
JA
271 (flags & PMB_CREATE) && (0 < fsize) && (0 < bsize)) {
272 pmb->pmb_pool =
273 pmemblk_create(path, bsize, fsize, 0644);
43f3cec2
BB
274 }
275 if (NULL == pmb->pmb_pool) {
c086b314
JA
276 log_err
277 ("fio: enable to open pmemblk pool file (errno %d)\n",
278 errno);
43f3cec2
BB
279 goto error;
280 }
281
282 pmb->pmb_filename = path;
c086b314
JA
283 pmb->pmb_next = NULL;
284 pmb->pmb_refcnt = 0;
285 pmb->pmb_bsize = pmemblk_bsize(pmb->pmb_pool);
286 pmb->pmb_nblocks = pmemblk_nblock(pmb->pmb_pool);
43f3cec2
BB
287
288 fio_pmemblk_cache_insert(pmb);
289 }
290
291 pmb->pmb_refcnt += 1;
292
de88999f 293 pthread_mutex_unlock(&CacheLock);
c086b314 294
43f3cec2
BB
295 return pmb;
296
297error:
298 if (NULL != pmb) {
299 if (NULL != pmb->pmb_pool)
300 pmemblk_close(pmb->pmb_pool);
c086b314 301 pmb->pmb_pool = NULL;
43f3cec2
BB
302 pmb->pmb_filename = NULL;
303 free(pmb);
304 }
305 if (NULL != path)
306 free(path);
de88999f
JA
307
308 pthread_mutex_unlock(&CacheLock);
43f3cec2
BB
309 return NULL;
310
c086b314 311} /* pmb_open() */
43f3cec2 312
c086b314 313static void pmb_close(fio_pmemblk_file_t pmb, const int keep)
43f3cec2 314{
de88999f 315 pthread_mutex_lock(&CacheLock);
43f3cec2
BB
316
317 pmb->pmb_refcnt--;
318
319 if (!keep && (0 == pmb->pmb_refcnt)) {
320 pmemblk_close(pmb->pmb_pool);
321 pmb->pmb_pool = NULL;
322 free(pmb->pmb_filename);
323 pmb->pmb_filename = NULL;
324 fio_pmemblk_cache_remove(pmb);
325 free(pmb);
326 }
327
de88999f 328 pthread_mutex_unlock(&CacheLock);
43f3cec2 329
c086b314 330} /* pmb_close() */
43f3cec2 331
c086b314 332static int pmb_get_flags(struct thread_data *td, uint64_t * pflags)
43f3cec2 333{
c086b314 334 static int thread_warned = 0;
43f3cec2
BB
335 static int odirect_warned = 0;
336
c086b314 337 uint64_t flags = 0;
43f3cec2
BB
338
339 if (!td->o.use_thread) {
340 if (!thread_warned) {
341 thread_warned = 1;
342 log_err("fio: must set thread=1 for pmemblk engine\n");
343 }
344 return 1;
345 }
346
347 if (!td->o.odirect && !odirect_warned) {
348 odirect_warned = 1;
349 log_info("fio: direct == 0, but pmemblk is always direct\n");
350 }
351
352 if (td->o.allow_create)
353 flags |= PMB_CREATE;
354
355 (*pflags) = flags;
356 return 0;
357
c086b314 358} /* pmb_get_flags() */
43f3cec2 359
c086b314 360static int fio_pmemblk_open_file(struct thread_data *td, struct fio_file *f)
43f3cec2 361{
c086b314
JA
362 uint64_t flags = 0;
363 fio_pmemblk_file_t pmb;
43f3cec2
BB
364
365 if (0 != pmb_get_flags(td, &flags))
366 return 1;
367
368 pmb = pmb_open(f->file_name, flags);
369 if (NULL == pmb)
370 return 1;
371
372 FIOFILEPMBSET(f, pmb);
373
374 return 0;
375
c086b314 376} /* fio_pmemblk_open_file() */
43f3cec2
BB
377
378static int
c086b314 379fio_pmemblk_close_file(struct thread_data fio_unused * td, struct fio_file *f)
43f3cec2 380{
c086b314 381 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
43f3cec2
BB
382
383 if (pmb)
384 pmb_close(pmb, 0);
385
386 FIOFILEPMBSET(f, NULL);
387
388 return 0;
389
c086b314 390} /* fio_pmemblk_close_file() */
43f3cec2 391
c086b314 392static int fio_pmemblk_get_file_size(struct thread_data *td, struct fio_file *f)
43f3cec2 393{
c086b314
JA
394 uint64_t flags = 0;
395 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
43f3cec2
BB
396
397 if (fio_file_size_known(f))
398 return 0;
399
400 if (NULL == pmb) {
401 if (0 != pmb_get_flags(td, &flags))
402 return 1;
403 pmb = pmb_open(f->file_name, flags);
404 if (NULL == pmb)
405 return 1;
406 }
407
408 f->real_file_size = pmb->pmb_bsize * pmb->pmb_nblocks;
409
410 fio_file_set_size_known(f);
411
412 if (NULL == FIOFILEPMBGET(f))
413 pmb_close(pmb, 1);
414
415 return 0;
416
c086b314 417} /* fio_pmemblk_get_file_size() */
43f3cec2 418
c086b314 419static int fio_pmemblk_queue(struct thread_data *td, struct io_u *io_u)
43f3cec2 420{
c086b314
JA
421 struct fio_file *f = io_u->file;
422 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
43f3cec2 423
c086b314
JA
424 unsigned long long off;
425 unsigned long len;
426 void *buf;
427 int (*blkop) (PMEMblkpool *, void *, off_t) = (void *)pmemblk_write;
43f3cec2
BB
428
429 fio_ro_check(td, io_u);
430
431 switch (io_u->ddir) {
432 case DDIR_READ:
433 blkop = pmemblk_read;
434 /* fall through */
435 case DDIR_WRITE:
436 off = io_u->offset;
437 len = io_u->xfer_buflen;
438
439 io_u->error = EINVAL;
440 if (0 != (off % pmb->pmb_bsize))
441 break;
442 if (0 != (len % pmb->pmb_bsize))
443 break;
444 if ((off + len) / pmb->pmb_bsize > pmb->pmb_nblocks)
445 break;
446
447 io_u->error = 0;
448 buf = io_u->xfer_buf;
449 off /= pmb->pmb_bsize;
450 len /= pmb->pmb_bsize;
451 while (0 < len) {
452 if (0 != blkop(pmb->pmb_pool, buf, off)) {
453 io_u->error = errno;
454 break;
455 }
456 buf += pmb->pmb_bsize;
457 off++;
458 len--;
459 }
460 off *= pmb->pmb_bsize;
461 len *= pmb->pmb_bsize;
462 io_u->resid = io_u->xfer_buflen - (off - io_u->offset);
463 break;
464 case DDIR_SYNC:
465 case DDIR_DATASYNC:
466 case DDIR_SYNC_FILE_RANGE:
467 /* we're always sync'd */
468 io_u->error = 0;
469 break;
470 default:
471 io_u->error = EINVAL;
472 break;
473 }
474
475 return FIO_Q_COMPLETED;
476
c086b314 477} /* fio_pmemblk_queue() */
43f3cec2 478
c086b314 479static int fio_pmemblk_unlink_file(struct thread_data *td, struct fio_file *f)
43f3cec2 480{
c086b314
JA
481 char *path = NULL;
482 uint64_t bsize = 0;
483 uint64_t fsize = 0;
43f3cec2
BB
484
485 /*
486 * we need our own unlink in case the user has specified
487 * the block and file sizes in the path name. we parse
488 * the file_name to determine the file name we actually used.
489 */
490
491 pmb_parse_path(f->file_name, &path, &bsize, &fsize);
492 if (NULL == path)
493 return 1;
494
495 unlink(path);
496 free(path);
497
498 return 0;
499
c086b314 500} /* fio_pmemblk_unlink_file() */
43f3cec2
BB
501
502struct ioengine_ops ioengine = {
c086b314
JA
503 .name = "pmemblk",
504 .version = FIO_IOOPS_VERSION,
505 .queue = fio_pmemblk_queue,
506 .open_file = fio_pmemblk_open_file,
507 .close_file = fio_pmemblk_close_file,
508 .get_file_size = fio_pmemblk_get_file_size,
509 .unlink_file = fio_pmemblk_unlink_file,
510 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL,
43f3cec2
BB
511};
512
43f3cec2
BB
513static void
514fio_init fio_pmemblk_register(void)
515{
516 register_ioengine(&ioengine);
517}
518
43f3cec2
BB
519static void
520fio_exit fio_pmemblk_unregister(void)
521{
522 unregister_ioengine(&ioengine);
523}