pmemblk: Clarify fsize is in MiB not MB
[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:
0cbb3f53 30 * thread=1 REQUIRED
43f3cec2
BB
31 * iodepth=1
32 * direct=1
43f3cec2 33 * unlink=1
0cbb3f53 34 * filename=/mnt/pmem0/fiotestfile,BSIZE,FSIZEMiB
43f3cec2
BB
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 *
0cbb3f53
RE
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.
43f3cec2 49 *
0cbb3f53
RE
50 * Example:
51 * mkfs.xfs /dev/pmem0
52 * mkdir /mnt/pmem0
53 * mount -o dax /dev/pmem0 /mnt/pmem0
43f3cec2
BB
54 *
55 * When specifying the filename, if the block pool file does not already
0cbb3f53 56 * exist, then the pmemblk engine creates the pool file if you specify
43f3cec2 57 * the block and file sizes. BSIZE is the block size in bytes.
0cbb3f53 58 * FSIZEMB is the pool file size in MiB.
43f3cec2 59 *
f1d85c6e
BB
60 * See examples/pmemblk.fio for more.
61 *
43f3cec2
BB
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>
43f3cec2 70#include <string.h>
cf8775b8
RE
71#include <libpmem.h>
72#include <libpmemblk.h>
43f3cec2
BB
73
74#include "../fio.h"
75
43f3cec2
BB
76/*
77 * libpmemblk
78 */
c086b314 79typedef struct fio_pmemblk_file *fio_pmemblk_file_t;
630431cc 80
43f3cec2 81struct fio_pmemblk_file {
c086b314
JA
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;
43f3cec2
BB
88};
89#define FIOFILEPMBSET(_f, _v) do { \
90 (_f)->engine_data = (uint64_t)(uintptr_t)(_v); \
91} while(0)
92#define FIOFILEPMBGET(_f) ((fio_pmemblk_file_t)((_f)->engine_data))
93
630431cc 94static fio_pmemblk_file_t Cache;
43f3cec2 95
c086b314 96static pthread_mutex_t CacheLock = PTHREAD_MUTEX_INITIALIZER;
43f3cec2 97
c086b314 98#define PMB_CREATE (0x0001) /* should create file */
43f3cec2 99
c086b314 100fio_pmemblk_file_t fio_pmemblk_cache_lookup(const char *filename)
43f3cec2 101{
c086b314 102 fio_pmemblk_file_t i;
43f3cec2
BB
103
104 for (i = Cache; i != NULL; i = i->pmb_next)
630431cc 105 if (!strcmp(filename, i->pmb_filename))
43f3cec2
BB
106 return i;
107
108 return NULL;
630431cc 109}
43f3cec2 110
c086b314 111static void fio_pmemblk_cache_insert(fio_pmemblk_file_t pmb)
43f3cec2
BB
112{
113 pmb->pmb_next = Cache;
114 Cache = pmb;
630431cc 115}
43f3cec2 116
c086b314 117static void fio_pmemblk_cache_remove(fio_pmemblk_file_t pmb)
43f3cec2 118{
c086b314 119 fio_pmemblk_file_t i;
43f3cec2
BB
120
121 if (pmb == Cache) {
122 Cache = Cache->pmb_next;
123 pmb->pmb_next = NULL;
124 return;
125 }
126
127 for (i = Cache; i != NULL; i = i->pmb_next)
128 if (pmb == i->pmb_next) {
129 i->pmb_next = i->pmb_next->pmb_next;
130 pmb->pmb_next = NULL;
131 return;
132 }
630431cc 133}
43f3cec2
BB
134
135/*
136 * to control block size and gross file size at the libpmemblk
137 * level, we allow the block size and file size to be appended
138 * to the file name:
139 *
0cbb3f53 140 * path[,bsize,fsizemib]
43f3cec2
BB
141 *
142 * note that we do not use the fio option "filesize" to dictate
143 * the file size because we can only give libpmemblk the gross
144 * file size, which is different from the net or usable file
145 * size (which is probably what fio wants).
146 *
147 * the final path without the parameters is returned in ppath.
148 * the block size and file size are returned in pbsize and fsize.
149 *
0cbb3f53 150 * note that the user specifies the file size in MiB, but
43f3cec2
BB
151 * we return bytes from here.
152 */
630431cc
JA
153static void pmb_parse_path(const char *pathspec, char **ppath, uint64_t *pbsize,
154 uint64_t *pfsize)
43f3cec2 155{
c086b314
JA
156 char *path;
157 char *s;
158 uint64_t bsize;
0cbb3f53 159 uint64_t fsizemib;
43f3cec2
BB
160
161 path = strdup(pathspec);
630431cc 162 if (!path) {
43f3cec2
BB
163 *ppath = NULL;
164 return;
165 }
166
167 /* extract sizes, if given */
168 s = strrchr(path, ',');
0cbb3f53 169 if (s && (fsizemib = strtoull(s + 1, NULL, 10))) {
43f3cec2
BB
170 *s = 0;
171 s = strrchr(path, ',');
c086b314 172 if (s && (bsize = strtoull(s + 1, NULL, 10))) {
43f3cec2 173 *s = 0;
c086b314 174 *ppath = path;
43f3cec2 175 *pbsize = bsize;
0cbb3f53 176 *pfsize = fsizemib << 20;
43f3cec2
BB
177 return;
178 }
179 }
180
181 /* size specs not found */
182 strcpy(path, pathspec);
c086b314 183 *ppath = path;
43f3cec2
BB
184 *pbsize = 0;
185 *pfsize = 0;
630431cc 186}
43f3cec2 187
630431cc 188static fio_pmemblk_file_t pmb_open(const char *pathspec, int flags)
43f3cec2 189{
c086b314
JA
190 fio_pmemblk_file_t pmb;
191 char *path = NULL;
192 uint64_t bsize = 0;
193 uint64_t fsize = 0;
43f3cec2
BB
194
195 pmb_parse_path(pathspec, &path, &bsize, &fsize);
630431cc 196 if (!path)
43f3cec2
BB
197 return NULL;
198
de88999f 199 pthread_mutex_lock(&CacheLock);
43f3cec2
BB
200
201 pmb = fio_pmemblk_cache_lookup(path);
630431cc 202 if (!pmb) {
43f3cec2 203 pmb = malloc(sizeof(*pmb));
630431cc 204 if (!pmb)
43f3cec2
BB
205 goto error;
206
207 /* try opening existing first, create it if needed */
208 pmb->pmb_pool = pmemblk_open(path, bsize);
630431cc 209 if (!pmb->pmb_pool && (errno == ENOENT) &&
c086b314
JA
210 (flags & PMB_CREATE) && (0 < fsize) && (0 < bsize)) {
211 pmb->pmb_pool =
212 pmemblk_create(path, bsize, fsize, 0644);
43f3cec2 213 }
630431cc 214 if (!pmb->pmb_pool) {
a0998600
RE
215 log_err("pmemblk: unable to open pmemblk pool file %s (%s)\n",
216 path, strerror(errno));
43f3cec2
BB
217 goto error;
218 }
219
220 pmb->pmb_filename = path;
c086b314
JA
221 pmb->pmb_next = NULL;
222 pmb->pmb_refcnt = 0;
223 pmb->pmb_bsize = pmemblk_bsize(pmb->pmb_pool);
224 pmb->pmb_nblocks = pmemblk_nblock(pmb->pmb_pool);
43f3cec2
BB
225
226 fio_pmemblk_cache_insert(pmb);
227 }
228
229 pmb->pmb_refcnt += 1;
230
de88999f 231 pthread_mutex_unlock(&CacheLock);
c086b314 232
43f3cec2
BB
233 return pmb;
234
235error:
630431cc
JA
236 if (pmb) {
237 if (pmb->pmb_pool)
43f3cec2 238 pmemblk_close(pmb->pmb_pool);
c086b314 239 pmb->pmb_pool = NULL;
43f3cec2
BB
240 pmb->pmb_filename = NULL;
241 free(pmb);
242 }
630431cc 243 if (path)
43f3cec2 244 free(path);
de88999f
JA
245
246 pthread_mutex_unlock(&CacheLock);
43f3cec2 247 return NULL;
630431cc 248}
43f3cec2 249
630431cc 250static void pmb_close(fio_pmemblk_file_t pmb, const bool keep)
43f3cec2 251{
de88999f 252 pthread_mutex_lock(&CacheLock);
43f3cec2
BB
253
254 pmb->pmb_refcnt--;
255
630431cc 256 if (!keep && !pmb->pmb_refcnt) {
43f3cec2
BB
257 pmemblk_close(pmb->pmb_pool);
258 pmb->pmb_pool = NULL;
259 free(pmb->pmb_filename);
260 pmb->pmb_filename = NULL;
261 fio_pmemblk_cache_remove(pmb);
262 free(pmb);
263 }
264
de88999f 265 pthread_mutex_unlock(&CacheLock);
630431cc 266}
43f3cec2 267
630431cc 268static int pmb_get_flags(struct thread_data *td, uint64_t *pflags)
43f3cec2 269{
c086b314 270 static int thread_warned = 0;
43f3cec2
BB
271 static int odirect_warned = 0;
272
c086b314 273 uint64_t flags = 0;
43f3cec2
BB
274
275 if (!td->o.use_thread) {
276 if (!thread_warned) {
277 thread_warned = 1;
a0998600 278 log_err("pmemblk: must set thread=1 for pmemblk engine\n");
43f3cec2
BB
279 }
280 return 1;
281 }
282
283 if (!td->o.odirect && !odirect_warned) {
284 odirect_warned = 1;
a0998600 285 log_info("pmemblk: direct == 0, but pmemblk is always direct\n");
43f3cec2
BB
286 }
287
288 if (td->o.allow_create)
289 flags |= PMB_CREATE;
290
291 (*pflags) = flags;
292 return 0;
630431cc 293}
43f3cec2 294
c086b314 295static int fio_pmemblk_open_file(struct thread_data *td, struct fio_file *f)
43f3cec2 296{
c086b314
JA
297 uint64_t flags = 0;
298 fio_pmemblk_file_t pmb;
43f3cec2 299
630431cc 300 if (pmb_get_flags(td, &flags))
43f3cec2
BB
301 return 1;
302
303 pmb = pmb_open(f->file_name, flags);
630431cc 304 if (!pmb)
43f3cec2
BB
305 return 1;
306
307 FIOFILEPMBSET(f, pmb);
43f3cec2 308 return 0;
630431cc 309}
43f3cec2 310
630431cc
JA
311static int fio_pmemblk_close_file(struct thread_data fio_unused *td,
312 struct fio_file *f)
43f3cec2 313{
c086b314 314 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
43f3cec2
BB
315
316 if (pmb)
630431cc 317 pmb_close(pmb, false);
43f3cec2
BB
318
319 FIOFILEPMBSET(f, NULL);
43f3cec2 320 return 0;
630431cc 321}
43f3cec2 322
c086b314 323static int fio_pmemblk_get_file_size(struct thread_data *td, struct fio_file *f)
43f3cec2 324{
c086b314
JA
325 uint64_t flags = 0;
326 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
43f3cec2
BB
327
328 if (fio_file_size_known(f))
329 return 0;
330
630431cc
JA
331 if (!pmb) {
332 if (pmb_get_flags(td, &flags))
43f3cec2
BB
333 return 1;
334 pmb = pmb_open(f->file_name, flags);
630431cc 335 if (!pmb)
43f3cec2
BB
336 return 1;
337 }
338
339 f->real_file_size = pmb->pmb_bsize * pmb->pmb_nblocks;
340
341 fio_file_set_size_known(f);
342
630431cc
JA
343 if (!FIOFILEPMBGET(f))
344 pmb_close(pmb, true);
43f3cec2
BB
345
346 return 0;
630431cc 347}
43f3cec2 348
c086b314 349static int fio_pmemblk_queue(struct thread_data *td, struct io_u *io_u)
43f3cec2 350{
c086b314
JA
351 struct fio_file *f = io_u->file;
352 fio_pmemblk_file_t pmb = FIOFILEPMBGET(f);
43f3cec2 353
c086b314
JA
354 unsigned long long off;
355 unsigned long len;
356 void *buf;
43f3cec2
BB
357
358 fio_ro_check(td, io_u);
359
360 switch (io_u->ddir) {
361 case DDIR_READ:
43f3cec2
BB
362 case DDIR_WRITE:
363 off = io_u->offset;
364 len = io_u->xfer_buflen;
365
366 io_u->error = EINVAL;
630431cc 367 if (off % pmb->pmb_bsize)
43f3cec2 368 break;
630431cc 369 if (len % pmb->pmb_bsize)
43f3cec2
BB
370 break;
371 if ((off + len) / pmb->pmb_bsize > pmb->pmb_nblocks)
372 break;
373
374 io_u->error = 0;
375 buf = io_u->xfer_buf;
376 off /= pmb->pmb_bsize;
377 len /= pmb->pmb_bsize;
378 while (0 < len) {
cf8775b8
RE
379 if (io_u->ddir == DDIR_READ &&
380 0 != pmemblk_read(pmb->pmb_pool, buf, off)) {
381 io_u->error = errno;
382 break;
383 } else if (0 != pmemblk_write(pmb->pmb_pool, buf, off)) {
43f3cec2
BB
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;
630431cc 407}
43f3cec2 408
c086b314 409static int fio_pmemblk_unlink_file(struct thread_data *td, struct fio_file *f)
43f3cec2 410{
c086b314
JA
411 char *path = NULL;
412 uint64_t bsize = 0;
413 uint64_t fsize = 0;
43f3cec2
BB
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);
630431cc 422 if (!path)
2442c935 423 return ENOENT;
43f3cec2
BB
424
425 unlink(path);
426 free(path);
43f3cec2 427 return 0;
630431cc 428}
43f3cec2 429
13690c10 430static struct ioengine_ops ioengine = {
c086b314
JA
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,
43f3cec2
BB
439};
440
630431cc 441static void fio_init fio_pmemblk_register(void)
43f3cec2
BB
442{
443 register_ioengine(&ioengine);
444}
445
630431cc 446static void fio_exit fio_pmemblk_unregister(void)
43f3cec2
BB
447{
448 unregister_ioengine(&ioengine);
449}