cf13a7e6c7e5bbc9b4a1b7196bab5e09d7cd66fd
[fio.git] / engines / pmemblk.c
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  *
27  * To use:
28  *   ioengine=pmemblk
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  *
52  *   See examples/pmemblk.fio for more.
53  *
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  */
78 struct PMEMblkpool_s;
79 typedef struct PMEMblkpool_s PMEMblkpool;
80
81 PMEMblkpool* (*pmemblk_create)(const char*, size_t, size_t, mode_t) = NULL;
82 PMEMblkpool* (*pmemblk_open)(const char*, size_t) = NULL;
83 void (*pmemblk_close)(PMEMblkpool*) = NULL;
84 size_t (*pmemblk_nblock)(PMEMblkpool*) = NULL;
85 size_t (*pmemblk_bsize)(PMEMblkpool*) = NULL;
86 int (*pmemblk_read)(PMEMblkpool*, void*, off_t) = NULL;
87 int (*pmemblk_write)(PMEMblkpool*, const void*, off_t) = NULL;
88
89 int
90 load_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 = dlsym(dl, "pmemblk_create")))
104                 goto errorout;
105         if (NULL == (pmemblk_open = dlsym(dl, "pmemblk_open")))
106                 goto errorout;
107         if (NULL == (pmemblk_close = dlsym(dl, "pmemblk_close")))
108                 goto errorout;
109         if (NULL == (pmemblk_nblock = dlsym(dl, "pmemblk_nblock")))
110                 goto errorout;
111         if (NULL == (pmemblk_bsize = dlsym(dl, "pmemblk_bsize")))
112                 goto errorout;
113         if (NULL == (pmemblk_read = dlsym(dl, "pmemblk_read")))
114                 goto errorout;
115         if (NULL == (pmemblk_write = dlsym(dl, "pmemblk_write")))
116                 goto errorout;
117         
118         return 0;
119
120 errorout:
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
130 typedef struct fio_pmemblk_file*   fio_pmemblk_file_t;
131 struct fio_pmemblk_file {
132         fio_pmemblk_file_t   pmb_next;
133         char*                pmb_filename;
134         uint64_t             pmb_refcnt;
135         PMEMblkpool*         pmb_pool;
136         size_t               pmb_bsize;
137         size_t               pmb_nblocks;
138 };
139 #define FIOFILEPMBSET(_f, _v)  do {                 \
140         (_f)->engine_data = (uint64_t)(uintptr_t)(_v);  \
141 } while(0)
142 #define FIOFILEPMBGET(_f)  ((fio_pmemblk_file_t)((_f)->engine_data))
143
144 static fio_pmemblk_file_t   Cache = NULL;
145
146 static pthread_mutex_t      CacheLock = PTHREAD_MUTEX_INITIALIZER;
147 #define CACHE_LOCK()  \
148         (void)pthread_mutex_lock(&CacheLock)
149 #define CACHE_UNLOCK()  \
150         (void)pthread_mutex_unlock(&CacheLock)
151
152 #define PMB_CREATE   (0x0001)  /* should create file */
153
154
155 fio_pmemblk_file_t
156 fio_pmemblk_cache_lookup(
157         const char* filename
158 )
159 {
160         fio_pmemblk_file_t   i;
161
162         for (i = Cache; i != NULL; i = i->pmb_next)
163                 if (0 == strcmp(filename, i->pmb_filename))
164                         return i;
165
166         return NULL;
167
168 }  /* fio_pmemblk_cache_lookup() */
169
170
171 static void
172 fio_pmemblk_cache_insert(
173         fio_pmemblk_file_t pmb
174 )
175 {
176         pmb->pmb_next = Cache;
177         Cache = pmb;
178
179         return;
180
181 }  /* fio_pmemblk_cache_insert() */
182
183
184 static void
185 fio_pmemblk_cache_remove(
186         fio_pmemblk_file_t pmb
187 )
188 {
189         fio_pmemblk_file_t   i;
190
191         if (pmb == Cache) {
192                 Cache = Cache->pmb_next;
193                 pmb->pmb_next = NULL;
194                 return;
195         }
196
197         for (i = Cache; i != NULL; i = i->pmb_next)
198                 if (pmb == i->pmb_next) {
199                         i->pmb_next = i->pmb_next->pmb_next;
200                         pmb->pmb_next = NULL;
201                         return;
202                 }
203
204         return;
205
206 }  /* fio_pmemblk_cache_remove() */
207
208
209 /*
210  * to control block size and gross file size at the libpmemblk
211  * level, we allow the block size and file size to be appended
212  * to the file name:
213  *
214  *   path[,bsize,fsizemb]
215  *
216  * note that we do not use the fio option "filesize" to dictate
217  * the file size because we can only give libpmemblk the gross
218  * file size, which is different from the net or usable file
219  * size (which is probably what fio wants).
220  *
221  * the final path without the parameters is returned in ppath.
222  * the block size and file size are returned in pbsize and fsize.
223  *
224  * note that the user should specify the file size in MiB, but
225  * we return bytes from here.
226  */
227 static void
228 pmb_parse_path(
229         const char* pathspec,
230         char**      ppath,
231         uint64_t*   pbsize,
232         uint64_t*   pfsize
233 )
234 {
235         char*      path;
236         char*      s;
237         uint64_t   bsize;
238         uint64_t   fsizemb;
239
240         path = strdup(pathspec);
241         if (NULL == path) {
242                 *ppath = NULL;
243                 return;
244         }
245
246         /* extract sizes, if given */
247         s = strrchr(path, ',');
248         if (s && (fsizemb = strtoull(s+1, NULL, 10))) {
249                 *s = 0;
250                 s = strrchr(path, ',');
251                 if (s && (bsize = strtoull(s+1, NULL, 10))) {
252                         *s = 0;
253                         *ppath  = path;
254                         *pbsize = bsize;
255                         *pfsize = fsizemb << 20;
256                         return;
257                 }
258         }
259
260         /* size specs not found */
261         strcpy(path, pathspec);
262         *ppath  = path;
263         *pbsize = 0;
264         *pfsize = 0;
265         return;
266
267 }  /* pmb_parse_path() */
268
269
270 static
271 fio_pmemblk_file_t
272 pmb_open(
273         const char* pathspec,
274         int         flags
275 )
276 {
277         fio_pmemblk_file_t   pmb;
278         char*                path  = NULL;
279         uint64_t             bsize = 0;
280         uint64_t             fsize = 0;
281
282         pmb_parse_path(pathspec, &path, &bsize, &fsize);
283         if (NULL == path)
284                 return NULL;
285
286         CACHE_LOCK();
287
288         pmb = fio_pmemblk_cache_lookup(path);
289
290         if (NULL == pmb) {
291                 /* load libpmemblk if needed */
292                 if (NULL == pmemblk_open)
293                         if (0 != load_libpmemblk(getenv("FIO_PMEMBLK_LIB")))
294                                 goto error;
295
296                 pmb = malloc(sizeof(*pmb));
297                 if (NULL == pmb)
298                         goto error;
299
300                 /* try opening existing first, create it if needed */
301                 pmb->pmb_pool = pmemblk_open(path, bsize);
302                 if ((NULL == pmb->pmb_pool) &&
303                     (ENOENT == errno) &&
304                     (flags & PMB_CREATE) &&
305                     (0 < fsize) &&
306                     (0 < bsize)) {
307                         pmb->pmb_pool = pmemblk_create(path, bsize, fsize, 0644);
308                 }
309                 if (NULL == pmb->pmb_pool) {
310                         log_err("fio: enable to open pmemblk pool file (errno %d)\n",
311                                 errno);
312                         goto error;
313                 }
314
315                 pmb->pmb_filename = path;
316                 pmb->pmb_next     = NULL;
317                 pmb->pmb_refcnt   = 0;
318                 pmb->pmb_bsize    = pmemblk_bsize(pmb->pmb_pool);
319                 pmb->pmb_nblocks  = pmemblk_nblock(pmb->pmb_pool);
320
321                 fio_pmemblk_cache_insert(pmb);
322         }
323
324         pmb->pmb_refcnt += 1;
325
326         CACHE_UNLOCK();
327         
328         return pmb;
329
330 error:
331         if (NULL != pmb) {
332                 if (NULL != pmb->pmb_pool)
333                         pmemblk_close(pmb->pmb_pool);
334                 pmb->pmb_pool     = NULL;
335                 pmb->pmb_filename = NULL;
336                 free(pmb);
337         }
338         if (NULL != path)
339                 free(path);
340         CACHE_UNLOCK();
341         return NULL;
342
343 }  /* pmb_open() */
344
345
346 static void
347 pmb_close(
348         fio_pmemblk_file_t pmb,
349         const int          keep
350 )
351 {
352         CACHE_LOCK();
353
354         pmb->pmb_refcnt--;
355
356         if (!keep && (0 == pmb->pmb_refcnt)) {
357                 pmemblk_close(pmb->pmb_pool);
358                 pmb->pmb_pool = NULL;
359                 free(pmb->pmb_filename);
360                 pmb->pmb_filename = NULL;
361                 fio_pmemblk_cache_remove(pmb);
362                 free(pmb);
363         }
364
365         CACHE_UNLOCK();
366
367 }  /* pmb_close() */
368
369
370 static int
371 pmb_get_flags(
372         struct thread_data* td,
373         uint64_t*           pflags
374 )
375 {
376         static int thread_warned  = 0;
377         static int odirect_warned = 0;
378
379         uint64_t   flags          = 0;
380
381         if (!td->o.use_thread) {
382                 if (!thread_warned) {
383                         thread_warned = 1;
384                         log_err("fio: must set thread=1 for pmemblk engine\n");
385                 }
386                 return 1;
387         }
388
389         if (!td->o.odirect && !odirect_warned) {
390                 odirect_warned = 1;
391                 log_info("fio: direct == 0, but pmemblk is always direct\n");
392         }
393
394         if (td->o.allow_create)
395                 flags |= PMB_CREATE;
396
397         (*pflags) = flags;
398         return 0;
399
400 }  /* pmb_get_flags() */
401
402
403 static int
404 fio_pmemblk_open_file(
405         struct thread_data* td,
406         struct fio_file*    f)
407 {
408         uint64_t             flags = 0;
409         fio_pmemblk_file_t   pmb;
410
411         if (0 != pmb_get_flags(td, &flags))
412                 return 1;
413
414         pmb = pmb_open(f->file_name, flags);
415         if (NULL == pmb)
416                 return 1;
417
418         FIOFILEPMBSET(f, pmb);
419
420         return 0;
421
422 }  /* fio_pmemblk_open_file() */
423
424
425 static int
426 fio_pmemblk_close_file(
427         struct thread_data fio_unused* td,
428         struct fio_file*               f
429 )
430 {
431         fio_pmemblk_file_t   pmb = FIOFILEPMBGET(f);
432
433         if (pmb)
434                 pmb_close(pmb, 0);
435
436         FIOFILEPMBSET(f, NULL);
437
438         return 0;
439
440 }  /* fio_pmemblk_close_file() */
441
442
443 static int
444 fio_pmemblk_get_file_size(
445         struct thread_data* td,
446         struct fio_file*    f
447 )
448 {
449         uint64_t             flags = 0;
450         fio_pmemblk_file_t   pmb   = FIOFILEPMBGET(f);
451
452         if (fio_file_size_known(f))
453                 return 0;
454
455         if (NULL == pmb) {
456                 if (0 != pmb_get_flags(td, &flags))
457                         return 1;
458                 pmb = pmb_open(f->file_name, flags);
459                 if (NULL == pmb)
460                         return 1;
461         }
462
463         f->real_file_size = pmb->pmb_bsize * pmb->pmb_nblocks;
464
465         fio_file_set_size_known(f);
466
467         if (NULL == FIOFILEPMBGET(f))
468                 pmb_close(pmb, 1);
469
470         return 0;
471
472 }  /* fio_pmemblk_get_file_size() */
473
474
475 static int
476 fio_pmemblk_queue(
477         struct thread_data* td,
478         struct io_u*        io_u)
479 {
480         struct fio_file*     f   = io_u->file;
481         fio_pmemblk_file_t   pmb = FIOFILEPMBGET(f);
482
483         unsigned long long   off;
484         unsigned long        len;
485         void*                buf;
486         int (*blkop)(PMEMblkpool*, void*, off_t) = (void*)pmemblk_write;
487
488         fio_ro_check(td, io_u);
489
490         switch (io_u->ddir) {
491         case DDIR_READ:
492                 blkop = pmemblk_read;
493                 /* fall through */
494         case DDIR_WRITE:
495                 off = io_u->offset;
496                 len = io_u->xfer_buflen;
497
498                 io_u->error = EINVAL;
499                 if (0 != (off % pmb->pmb_bsize))
500                         break;
501                 if (0 != (len % pmb->pmb_bsize))
502                         break;
503                 if ((off + len) / pmb->pmb_bsize > pmb->pmb_nblocks)
504                         break;
505
506                 io_u->error = 0;
507                 buf = io_u->xfer_buf;
508                 off /= pmb->pmb_bsize;
509                 len /= pmb->pmb_bsize;
510                 while (0 < len) {
511                         if (0 != blkop(pmb->pmb_pool, buf, off)) {
512                                 io_u->error = errno;
513                                 break;
514                         }
515                         buf += pmb->pmb_bsize;
516                         off++;
517                         len--;
518                 }
519                 off *= pmb->pmb_bsize;
520                 len *= pmb->pmb_bsize;
521                 io_u->resid = io_u->xfer_buflen - (off - io_u->offset);
522                 break;
523         case DDIR_SYNC:
524         case DDIR_DATASYNC:
525         case DDIR_SYNC_FILE_RANGE:
526                 /* we're always sync'd */
527                 io_u->error = 0;
528                 break;
529         default:
530                 io_u->error = EINVAL;
531                 break;
532         }
533
534         return FIO_Q_COMPLETED;
535
536 }  /* fio_pmemblk_queue() */
537
538
539 static int
540 fio_pmemblk_unlink_file(
541         struct thread_data* td,
542         struct fio_file*    f
543 )
544 {
545         char*      path  = NULL;
546         uint64_t   bsize = 0;
547         uint64_t   fsize = 0;
548
549         /*
550          * we need our own unlink in case the user has specified
551          * the block and file sizes in the path name.  we parse
552          * the file_name to determine the file name we actually used.
553          */
554
555         pmb_parse_path(f->file_name, &path, &bsize, &fsize);
556         if (NULL == path)
557                 return 1;
558
559         unlink(path);
560         free(path);
561
562         return 0;
563
564 }  /* fio_pmemblk_unlink_file() */
565
566
567 struct ioengine_ops ioengine = {
568         .name           = "pmemblk",
569         .version        = FIO_IOOPS_VERSION,
570         .queue          = fio_pmemblk_queue,
571         .open_file      = fio_pmemblk_open_file,
572         .close_file     = fio_pmemblk_close_file,
573         .get_file_size  = fio_pmemblk_get_file_size,
574         .unlink_file    = fio_pmemblk_unlink_file,
575         .flags          = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL,
576 };
577
578
579 static void
580 fio_init fio_pmemblk_register(void)
581 {
582         register_ioengine(&ioengine);
583 }
584
585
586 static void
587 fio_exit fio_pmemblk_unregister(void)
588 {
589         unregister_ioengine(&ioengine);
590 }
591