pmemblk: remove comments about an external engine
[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
21
22/*
23 * pmemblk engine
24 *
25 * IO engine that uses libpmemblk to read and write data
26 *
f1d85c6e
BB
27 * To use:
28 * ioengine=pmemblk
43f3cec2
BB
29 *
30 * Other relevant settings:
31 * iodepth=1
32 * direct=1
33 * thread=1 REQUIRED
34 * unlink=1
35 * filename=/pmem0/fiotestfile,BSIZE,FSIZEMB
36 *
37 * thread must be set to 1 for pmemblk as multiple processes cannot
38 * open the same block pool file.
39 *
40 * iodepth should be set to 1 as pmemblk is always synchronous.
41 * Use numjobs to scale up.
42 *
43 * direct=1 is implied as pmemblk is always direct.
44 *
45 * Can set unlink to 1 to remove the block pool file after testing.
46 *
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.
51 *
f1d85c6e
BB
52 * See examples/pmemblk.fio for more.
53 *
43f3cec2
BB
54 * libpmemblk.so
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
58 * libpmemblk.so.
59 *
60 */
61
62#include <stdio.h>
63#include <stdlib.h>
64#include <unistd.h>
65#include <sys/uio.h>
66#include <errno.h>
67#include <assert.h>
68#include <dlfcn.h>
69#include <string.h>
70
71#include "../fio.h"
72
73
74
75/*
76 * libpmemblk
77 */
78struct PMEMblkpool_s;
79typedef struct PMEMblkpool_s PMEMblkpool;
80
81PMEMblkpool* (*pmemblk_create_ptr)(const char*, size_t, size_t, mode_t) = NULL;
82PMEMblkpool* (*pmemblk_open_ptr)(const char*, size_t) = NULL;
83void (*pmemblk_close_ptr)(PMEMblkpool*) = NULL;
84size_t (*pmemblk_nblock_ptr)(PMEMblkpool*) = NULL;
85size_t (*pmemblk_bsize_ptr)(PMEMblkpool*) = NULL;
86int (*pmemblk_read_ptr)(PMEMblkpool*, void*, off_t) = NULL;
87int (*pmemblk_write_ptr)(PMEMblkpool*, const void*, off_t) = NULL;
88
89int
90load_libpmemblk(
91 const char* path
92)
93{
94 void* dl;
95
96 if (NULL == path)
97 path = "libpmemblk.so";
98
99 dl = dlopen(path, RTLD_NOW | RTLD_NODELETE);
100 if (NULL == dl)
101 goto errorout;
102
103 if (NULL == (pmemblk_create_ptr = dlsym(dl, "pmemblk_create")))
104 goto errorout;
105 if (NULL == (pmemblk_open_ptr = dlsym(dl, "pmemblk_open")))
106 goto errorout;
107 if (NULL == (pmemblk_close_ptr = dlsym(dl, "pmemblk_close")))
108 goto errorout;
109 if (NULL == (pmemblk_nblock_ptr = dlsym(dl, "pmemblk_nblock")))
110 goto errorout;
111 if (NULL == (pmemblk_bsize_ptr = dlsym(dl, "pmemblk_bsize")))
112 goto errorout;
113 if (NULL == (pmemblk_read_ptr = dlsym(dl, "pmemblk_read")))
114 goto errorout;
115 if (NULL == (pmemblk_write_ptr = dlsym(dl, "pmemblk_write")))
116 goto errorout;
117
118 return 0;
119
120errorout:
121 log_err("fio: unable to load libpmemblk: %s\n", dlerror());
122 if (NULL != dl)
123 dlclose(dl);
124
125 return (-1);
126
127} /* load_libpmemblk() */
128
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
136
137
138typedef struct fio_pmemblk_file* fio_pmemblk_file_t;
139struct fio_pmemblk_file {
140 fio_pmemblk_file_t pmb_next;
141 char* pmb_filename;
142 uint64_t pmb_refcnt;
143 PMEMblkpool* pmb_pool;
144 size_t pmb_bsize;
145 size_t pmb_nblocks;
146};
147#define FIOFILEPMBSET(_f, _v) do { \
148 (_f)->engine_data = (uint64_t)(uintptr_t)(_v); \
149} while(0)
150#define FIOFILEPMBGET(_f) ((fio_pmemblk_file_t)((_f)->engine_data))
151
152static fio_pmemblk_file_t Cache = NULL;
153
154static 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)
159
160#define PMB_CREATE (0x0001) /* should create file */
161
162
163fio_pmemblk_file_t
164fio_pmemblk_cache_lookup(
165 const char* filename
166)
167{
168 fio_pmemblk_file_t i;
169
170 for (i = Cache; i != NULL; i = i->pmb_next)
171 if (0 == strcmp(filename, i->pmb_filename))
172 return i;
173
174 return NULL;
175
176} /* fio_pmemblk_cache_lookup() */
177
178
179static void
180fio_pmemblk_cache_insert(
181 fio_pmemblk_file_t pmb
182)
183{
184 pmb->pmb_next = Cache;
185 Cache = pmb;
186
187 return;
188
189} /* fio_pmemblk_cache_insert() */
190
191
192static void
193fio_pmemblk_cache_remove(
194 fio_pmemblk_file_t pmb
195)
196{
197 fio_pmemblk_file_t i;
198
199 if (pmb == Cache) {
200 Cache = Cache->pmb_next;
201 pmb->pmb_next = NULL;
202 return;
203 }
204
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;
209 return;
210 }
211
212 return;
213
214} /* fio_pmemblk_cache_remove() */
215
216
217/*
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
220 * to the file name:
221 *
222 * path[,bsize,fsizemb]
223 *
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).
228 *
229 * the final path without the parameters is returned in ppath.
230 * the block size and file size are returned in pbsize and fsize.
231 *
232 * note that the user should specify the file size in MiB, but
233 * we return bytes from here.
234 */
235static void
236pmb_parse_path(
237 const char* pathspec,
238 char** ppath,
239 uint64_t* pbsize,
240 uint64_t* pfsize
241)
242{
243 char* path;
244 char* s;
245 uint64_t bsize;
246 uint64_t fsizemb;
247
248 path = strdup(pathspec);
249 if (NULL == path) {
250 *ppath = NULL;
251 return;
252 }
253
254 /* extract sizes, if given */
255 s = strrchr(path, ',');
256 if (s && (fsizemb = strtoull(s+1, NULL, 10))) {
257 *s = 0;
258 s = strrchr(path, ',');
259 if (s && (bsize = strtoull(s+1, NULL, 10))) {
260 *s = 0;
261 *ppath = path;
262 *pbsize = bsize;
263 *pfsize = fsizemb << 20;
264 return;
265 }
266 }
267
268 /* size specs not found */
269 strcpy(path, pathspec);
270 *ppath = path;
271 *pbsize = 0;
272 *pfsize = 0;
273 return;
274
275} /* pmb_parse_path() */
276
277
278static
279fio_pmemblk_file_t
280pmb_open(
281 const char* pathspec,
282 int flags
283)
284{
285 fio_pmemblk_file_t pmb;
286 char* path = NULL;
287 uint64_t bsize = 0;
288 uint64_t fsize = 0;
289
290 pmb_parse_path(pathspec, &path, &bsize, &fsize);
291 if (NULL == path)
292 return NULL;
293
294 CACHE_LOCK();
295
296 pmb = fio_pmemblk_cache_lookup(path);
297
298 if (NULL == pmb) {
299 /* load libpmemblk if needed */
300 if (NULL == pmemblk_open)
301 if (0 != load_libpmemblk(getenv("FIO_PMEMBLK_LIB")))
302 goto error;
303
304 pmb = malloc(sizeof(*pmb));
305 if (NULL == pmb)
306 goto error;
307
308 /* try opening existing first, create it if needed */
309 pmb->pmb_pool = pmemblk_open(path, bsize);
310 if ((NULL == pmb->pmb_pool) &&
311 (ENOENT == errno) &&
312 (flags & PMB_CREATE) &&
313 (0 < fsize) &&
314 (0 < bsize)) {
315 pmb->pmb_pool = pmemblk_create(path, bsize, fsize, 0644);
316 }
317 if (NULL == pmb->pmb_pool) {
318 log_err("fio: enable to open pmemblk pool file (errno %d)\n",
319 errno);
320 goto error;
321 }
322
323 pmb->pmb_filename = path;
324 pmb->pmb_next = NULL;
325 pmb->pmb_refcnt = 0;
326 pmb->pmb_bsize = pmemblk_bsize(pmb->pmb_pool);
327 pmb->pmb_nblocks = pmemblk_nblock(pmb->pmb_pool);
328
329 fio_pmemblk_cache_insert(pmb);
330 }
331
332 pmb->pmb_refcnt += 1;
333
334 CACHE_UNLOCK();
335
336 return pmb;
337
338error:
339 if (NULL != pmb) {
340 if (NULL != pmb->pmb_pool)
341 pmemblk_close(pmb->pmb_pool);
342 pmb->pmb_pool = NULL;
343 pmb->pmb_filename = NULL;
344 free(pmb);
345 }
346 if (NULL != path)
347 free(path);
348 CACHE_UNLOCK();
349 return NULL;
350
351} /* pmb_open() */
352
353
354static void
355pmb_close(
356 fio_pmemblk_file_t pmb,
357 const int keep
358)
359{
360 CACHE_LOCK();
361
362 pmb->pmb_refcnt--;
363
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);
370 free(pmb);
371 }
372
373 CACHE_UNLOCK();
374
375} /* pmb_close() */
376
377
378static int
379pmb_get_flags(
380 struct thread_data* td,
381 uint64_t* pflags
382)
383{
384 static int thread_warned = 0;
385 static int odirect_warned = 0;
386
387 uint64_t flags = 0;
388
389 if (!td->o.use_thread) {
390 if (!thread_warned) {
391 thread_warned = 1;
392 log_err("fio: must set thread=1 for pmemblk engine\n");
393 }
394 return 1;
395 }
396
397 if (!td->o.odirect && !odirect_warned) {
398 odirect_warned = 1;
399 log_info("fio: direct == 0, but pmemblk is always direct\n");
400 }
401
402 if (td->o.allow_create)
403 flags |= PMB_CREATE;
404
405 (*pflags) = flags;
406 return 0;
407
408} /* pmb_get_flags() */
409
410
411static int
412fio_pmemblk_open_file(
413 struct thread_data* td,
414 struct fio_file* f)
415{
416 uint64_t flags = 0;
417 fio_pmemblk_file_t pmb;
418
419 if (0 != pmb_get_flags(td, &flags))
420 return 1;
421
422 pmb = pmb_open(f->file_name, flags);
423 if (NULL == pmb)
424 return 1;
425
426 FIOFILEPMBSET(f, pmb);
427
428 return 0;
429
430} /* fio_pmemblk_open_file() */
431
432
433static int
434fio_pmemblk_close_file(
435 struct thread_data fio_unused* td,
436 struct fio_file* f
437)
438{
439 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
440
441 if (pmb)
442 pmb_close(pmb, 0);
443
444 FIOFILEPMBSET(f, NULL);
445
446 return 0;
447
448} /* fio_pmemblk_close_file() */
449
450
451static int
452fio_pmemblk_get_file_size(
453 struct thread_data* td,
454 struct fio_file* f
455)
456{
457 uint64_t flags = 0;
458 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
459
460 if (fio_file_size_known(f))
461 return 0;
462
463 if (NULL == pmb) {
464 if (0 != pmb_get_flags(td, &flags))
465 return 1;
466 pmb = pmb_open(f->file_name, flags);
467 if (NULL == pmb)
468 return 1;
469 }
470
471 f->real_file_size = pmb->pmb_bsize * pmb->pmb_nblocks;
472
473 fio_file_set_size_known(f);
474
475 if (NULL == FIOFILEPMBGET(f))
476 pmb_close(pmb, 1);
477
478 return 0;
479
480} /* fio_pmemblk_get_file_size() */
481
482
483static int
484fio_pmemblk_queue(
485 struct thread_data* td,
486 struct io_u* io_u)
487{
488 struct fio_file* f = io_u->file;
489 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
490
491 unsigned long long off;
492 unsigned long len;
493 void* buf;
494 int (*blkop)(PMEMblkpool*, void*, off_t) = (void*)pmemblk_write;
495
496 fio_ro_check(td, io_u);
497
498 switch (io_u->ddir) {
499 case DDIR_READ:
500 blkop = pmemblk_read;
501 /* fall through */
502 case DDIR_WRITE:
503 off = io_u->offset;
504 len = io_u->xfer_buflen;
505
506 io_u->error = EINVAL;
507 if (0 != (off % pmb->pmb_bsize))
508 break;
509 if (0 != (len % pmb->pmb_bsize))
510 break;
511 if ((off + len) / pmb->pmb_bsize > pmb->pmb_nblocks)
512 break;
513
514 io_u->error = 0;
515 buf = io_u->xfer_buf;
516 off /= pmb->pmb_bsize;
517 len /= pmb->pmb_bsize;
518 while (0 < len) {
519 if (0 != blkop(pmb->pmb_pool, buf, off)) {
520 io_u->error = errno;
521 break;
522 }
523 buf += pmb->pmb_bsize;
524 off++;
525 len--;
526 }
527 off *= pmb->pmb_bsize;
528 len *= pmb->pmb_bsize;
529 io_u->resid = io_u->xfer_buflen - (off - io_u->offset);
530 break;
531 case DDIR_SYNC:
532 case DDIR_DATASYNC:
533 case DDIR_SYNC_FILE_RANGE:
534 /* we're always sync'd */
535 io_u->error = 0;
536 break;
537 default:
538 io_u->error = EINVAL;
539 break;
540 }
541
542 return FIO_Q_COMPLETED;
543
544} /* fio_pmemblk_queue() */
545
546
547static int
548fio_pmemblk_unlink_file(
549 struct thread_data* td,
550 struct fio_file* f
551)
552{
553 char* path = NULL;
554 uint64_t bsize = 0;
555 uint64_t fsize = 0;
556
557 /*
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.
561 */
562
563 pmb_parse_path(f->file_name, &path, &bsize, &fsize);
564 if (NULL == path)
565 return 1;
566
567 unlink(path);
568 free(path);
569
570 return 0;
571
572} /* fio_pmemblk_unlink_file() */
573
574
575struct ioengine_ops ioengine = {
576 .name = "pmemblk",
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,
584};
585
586
587static void
588fio_init fio_pmemblk_register(void)
589{
590 register_ioengine(&ioengine);
591}
592
593
594static void
595fio_exit fio_pmemblk_unregister(void)
596{
597 unregister_ioengine(&ioengine);
598}
599