t/nvmept_trim: increase transfer size for some tests
[fio.git] / engines / pmemblk.c
1 /*
2  * pmemblk: IO engine that uses PMDK 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., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, 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  *   thread=1   REQUIRED
31  *   iodepth=1
32  *   direct=1
33  *   unlink=1
34  *   filename=/mnt/pmem0/fiotestfile,BSIZE,FSIZEMiB
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. A warning message
43  *   is printed if this is not specified.
44  *
45  *   unlink=1 removes the block pool file after testing, and is optional.
46  *
47  *   The pmem device must have a DAX-capable filesystem and be mounted
48  *   with DAX enabled.  filename must point to a file on that filesystem.
49  *
50  *   Example:
51  *     mkfs.xfs /dev/pmem0
52  *     mkdir /mnt/pmem0
53  *     mount -o dax /dev/pmem0 /mnt/pmem0
54  *
55  *   When specifying the filename, if the block pool file does not already
56  *   exist, then the pmemblk engine creates the pool file if you specify
57  *   the block and file sizes.  BSIZE is the block size in bytes.
58  *   FSIZEMB is the pool file size in MiB.
59  *
60  *   See examples/pmemblk.fio for more.
61  *
62  */
63
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <unistd.h>
67 #include <sys/uio.h>
68 #include <errno.h>
69 #include <assert.h>
70 #include <string.h>
71 #include <libpmem.h>
72 #include <libpmemblk.h>
73
74 #include "../fio.h"
75
76 /*
77  * libpmemblk
78  */
79 typedef struct fio_pmemblk_file *fio_pmemblk_file_t;
80
81 struct fio_pmemblk_file {
82         fio_pmemblk_file_t pmb_next;
83         char *pmb_filename;
84         uint64_t pmb_refcnt;
85         PMEMblkpool *pmb_pool;
86         size_t pmb_bsize;
87         size_t pmb_nblocks;
88 };
89
90 static fio_pmemblk_file_t Cache;
91
92 static pthread_mutex_t CacheLock = PTHREAD_MUTEX_INITIALIZER;
93
94 #define PMB_CREATE   (0x0001)   /* should create file */
95
96 fio_pmemblk_file_t fio_pmemblk_cache_lookup(const char *filename)
97 {
98         fio_pmemblk_file_t i;
99
100         for (i = Cache; i != NULL; i = i->pmb_next)
101                 if (!strcmp(filename, i->pmb_filename))
102                         return i;
103
104         return NULL;
105 }
106
107 static void fio_pmemblk_cache_insert(fio_pmemblk_file_t pmb)
108 {
109         pmb->pmb_next = Cache;
110         Cache = pmb;
111 }
112
113 static void fio_pmemblk_cache_remove(fio_pmemblk_file_t pmb)
114 {
115         fio_pmemblk_file_t i;
116
117         if (pmb == Cache) {
118                 Cache = Cache->pmb_next;
119                 pmb->pmb_next = NULL;
120                 return;
121         }
122
123         for (i = Cache; i != NULL; i = i->pmb_next)
124                 if (pmb == i->pmb_next) {
125                         i->pmb_next = i->pmb_next->pmb_next;
126                         pmb->pmb_next = NULL;
127                         return;
128                 }
129 }
130
131 /*
132  * to control block size and gross file size at the libpmemblk
133  * level, we allow the block size and file size to be appended
134  * to the file name:
135  *
136  *   path[,bsize,fsizemib]
137  *
138  * note that we do not use the fio option "filesize" to dictate
139  * the file size because we can only give libpmemblk the gross
140  * file size, which is different from the net or usable file
141  * size (which is probably what fio wants).
142  *
143  * the final path without the parameters is returned in ppath.
144  * the block size and file size are returned in pbsize and fsize.
145  *
146  * note that the user specifies the file size in MiB, but
147  * we return bytes from here.
148  */
149 static void pmb_parse_path(const char *pathspec, char **ppath, uint64_t *pbsize,
150                            uint64_t *pfsize)
151 {
152         char *path;
153         char *s;
154         uint64_t bsize;
155         uint64_t fsizemib;
156
157         path = strdup(pathspec);
158         if (!path) {
159                 *ppath = NULL;
160                 return;
161         }
162
163         /* extract sizes, if given */
164         s = strrchr(path, ',');
165         if (s && (fsizemib = strtoull(s + 1, NULL, 10))) {
166                 *s = 0;
167                 s = strrchr(path, ',');
168                 if (s && (bsize = strtoull(s + 1, NULL, 10))) {
169                         *s = 0;
170                         *ppath = path;
171                         *pbsize = bsize;
172                         *pfsize = fsizemib << 20;
173                         return;
174                 }
175         }
176
177         /* size specs not found */
178         strcpy(path, pathspec);
179         *ppath = path;
180         *pbsize = 0;
181         *pfsize = 0;
182 }
183
184 static fio_pmemblk_file_t pmb_open(const char *pathspec, int flags)
185 {
186         fio_pmemblk_file_t pmb;
187         char *path = NULL;
188         uint64_t bsize = 0;
189         uint64_t fsize = 0;
190
191         pmb_parse_path(pathspec, &path, &bsize, &fsize);
192         if (!path)
193                 return NULL;
194
195         pthread_mutex_lock(&CacheLock);
196
197         pmb = fio_pmemblk_cache_lookup(path);
198         if (!pmb) {
199                 pmb = malloc(sizeof(*pmb));
200                 if (!pmb)
201                         goto error;
202
203                 /* try opening existing first, create it if needed */
204                 pmb->pmb_pool = pmemblk_open(path, bsize);
205                 if (!pmb->pmb_pool && (errno == ENOENT) &&
206                     (flags & PMB_CREATE) && (0 < fsize) && (0 < bsize)) {
207                         pmb->pmb_pool =
208                             pmemblk_create(path, bsize, fsize, 0644);
209                 }
210                 if (!pmb->pmb_pool) {
211                         log_err("pmemblk: unable to open pmemblk pool file %s (%s)\n",
212                              path, strerror(errno));
213                         goto error;
214                 }
215
216                 pmb->pmb_filename = path;
217                 pmb->pmb_next = NULL;
218                 pmb->pmb_refcnt = 0;
219                 pmb->pmb_bsize = pmemblk_bsize(pmb->pmb_pool);
220                 pmb->pmb_nblocks = pmemblk_nblock(pmb->pmb_pool);
221
222                 fio_pmemblk_cache_insert(pmb);
223         } else {
224                 free(path);
225         }
226
227         pmb->pmb_refcnt += 1;
228
229         pthread_mutex_unlock(&CacheLock);
230
231         return pmb;
232
233 error:
234         if (pmb) {
235                 if (pmb->pmb_pool)
236                         pmemblk_close(pmb->pmb_pool);
237                 pmb->pmb_pool = NULL;
238                 pmb->pmb_filename = NULL;
239                 free(pmb);
240         }
241         if (path)
242                 free(path);
243
244         pthread_mutex_unlock(&CacheLock);
245         return NULL;
246 }
247
248 static void pmb_close(fio_pmemblk_file_t pmb, const bool keep)
249 {
250         pthread_mutex_lock(&CacheLock);
251
252         pmb->pmb_refcnt--;
253
254         if (!keep && !pmb->pmb_refcnt) {
255                 pmemblk_close(pmb->pmb_pool);
256                 pmb->pmb_pool = NULL;
257                 free(pmb->pmb_filename);
258                 pmb->pmb_filename = NULL;
259                 fio_pmemblk_cache_remove(pmb);
260                 free(pmb);
261         }
262
263         pthread_mutex_unlock(&CacheLock);
264 }
265
266 static int pmb_get_flags(struct thread_data *td, uint64_t *pflags)
267 {
268         static int thread_warned = 0;
269         static int odirect_warned = 0;
270
271         uint64_t flags = 0;
272
273         if (!td->o.use_thread) {
274                 if (!thread_warned) {
275                         thread_warned = 1;
276                         log_err("pmemblk: must set thread=1 for pmemblk engine\n");
277                 }
278                 return 1;
279         }
280
281         if (!td->o.odirect && !odirect_warned) {
282                 odirect_warned = 1;
283                 log_info("pmemblk: direct == 0, but pmemblk is always direct\n");
284         }
285
286         if (td->o.allow_create)
287                 flags |= PMB_CREATE;
288
289         (*pflags) = flags;
290         return 0;
291 }
292
293 static int fio_pmemblk_open_file(struct thread_data *td, struct fio_file *f)
294 {
295         uint64_t flags = 0;
296         fio_pmemblk_file_t pmb;
297
298         if (pmb_get_flags(td, &flags))
299                 return 1;
300
301         pmb = pmb_open(f->file_name, flags);
302         if (!pmb)
303                 return 1;
304
305         FILE_SET_ENG_DATA(f, pmb);
306         return 0;
307 }
308
309 static int fio_pmemblk_close_file(struct thread_data fio_unused *td,
310                                   struct fio_file *f)
311 {
312         fio_pmemblk_file_t pmb = FILE_ENG_DATA(f);
313
314         if (pmb)
315                 pmb_close(pmb, false);
316
317         FILE_SET_ENG_DATA(f, NULL);
318         return 0;
319 }
320
321 static int fio_pmemblk_get_file_size(struct thread_data *td, struct fio_file *f)
322 {
323         uint64_t flags = 0;
324         fio_pmemblk_file_t pmb = FILE_ENG_DATA(f);
325
326         if (fio_file_size_known(f))
327                 return 0;
328
329         if (!pmb) {
330                 if (pmb_get_flags(td, &flags))
331                         return 1;
332                 pmb = pmb_open(f->file_name, flags);
333                 if (!pmb)
334                         return 1;
335         }
336
337         f->real_file_size = pmb->pmb_bsize * pmb->pmb_nblocks;
338
339         fio_file_set_size_known(f);
340
341         if (!FILE_ENG_DATA(f))
342                 pmb_close(pmb, true);
343
344         return 0;
345 }
346
347 static enum fio_q_status fio_pmemblk_queue(struct thread_data *td,
348                                            struct io_u *io_u)
349 {
350         struct fio_file *f = io_u->file;
351         fio_pmemblk_file_t pmb = FILE_ENG_DATA(f);
352
353         unsigned long long off;
354         unsigned long len;
355         void *buf;
356
357         fio_ro_check(td, io_u);
358
359         switch (io_u->ddir) {
360         case DDIR_READ:
361         case DDIR_WRITE:
362                 off = io_u->offset;
363                 len = io_u->xfer_buflen;
364
365                 io_u->error = EINVAL;
366                 if (off % pmb->pmb_bsize)
367                         break;
368                 if (len % pmb->pmb_bsize)
369                         break;
370                 if ((off + len) / pmb->pmb_bsize > pmb->pmb_nblocks)
371                         break;
372
373                 io_u->error = 0;
374                 buf = io_u->xfer_buf;
375                 off /= pmb->pmb_bsize;
376                 len /= pmb->pmb_bsize;
377                 while (0 < len) {
378                         if (io_u->ddir == DDIR_READ) {
379                                 if (0 != pmemblk_read(pmb->pmb_pool, buf, off)) {
380                                         io_u->error = errno;
381                                         break;
382                                 }
383                         } else if (0 != pmemblk_write(pmb->pmb_pool, buf, off)) {
384                                 io_u->error = errno;
385                                 break;
386                         }
387                         buf += pmb->pmb_bsize;
388                         off++;
389                         len--;
390                 }
391                 off *= pmb->pmb_bsize;
392                 len *= pmb->pmb_bsize;
393                 io_u->resid = io_u->xfer_buflen - (off - io_u->offset);
394                 break;
395         case DDIR_SYNC:
396         case DDIR_DATASYNC:
397         case DDIR_SYNC_FILE_RANGE:
398                 /* we're always sync'd */
399                 io_u->error = 0;
400                 break;
401         default:
402                 io_u->error = EINVAL;
403                 break;
404         }
405
406         return FIO_Q_COMPLETED;
407 }
408
409 static int fio_pmemblk_unlink_file(struct thread_data *td, struct fio_file *f)
410 {
411         char *path = NULL;
412         uint64_t bsize = 0;
413         uint64_t fsize = 0;
414
415         /*
416          * we need our own unlink in case the user has specified
417          * the block and file sizes in the path name.  we parse
418          * the file_name to determine the file name we actually used.
419          */
420
421         pmb_parse_path(f->file_name, &path, &bsize, &fsize);
422         if (!path)
423                 return ENOENT;
424
425         unlink(path);
426         free(path);
427         return 0;
428 }
429
430 FIO_STATIC struct ioengine_ops ioengine = {
431         .name = "pmemblk",
432         .version = FIO_IOOPS_VERSION,
433         .queue = fio_pmemblk_queue,
434         .open_file = fio_pmemblk_open_file,
435         .close_file = fio_pmemblk_close_file,
436         .get_file_size = fio_pmemblk_get_file_size,
437         .unlink_file = fio_pmemblk_unlink_file,
438         .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL,
439 };
440
441 static void fio_init fio_pmemblk_register(void)
442 {
443         register_ioengine(&ioengine);
444 }
445
446 static void fio_exit fio_pmemblk_unregister(void)
447 {
448         unregister_ioengine(&ioengine);
449 }