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