glusterfs: update for new API
[fio.git] / engines / pmemblk.c
CommitLineData
43f3cec2 1/*
363a5f65 2 * pmemblk: IO engine that uses PMDK libpmemblk to read and write data
43f3cec2
BB
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
fa07eaa6
SW
17 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
43f3cec2
BB
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 88};
43f3cec2 89
630431cc 90static fio_pmemblk_file_t Cache;
43f3cec2 91
c086b314 92static pthread_mutex_t CacheLock = PTHREAD_MUTEX_INITIALIZER;
43f3cec2 93
c086b314 94#define PMB_CREATE (0x0001) /* should create file */
43f3cec2 95
c086b314 96fio_pmemblk_file_t fio_pmemblk_cache_lookup(const char *filename)
43f3cec2 97{
c086b314 98 fio_pmemblk_file_t i;
43f3cec2
BB
99
100 for (i = Cache; i != NULL; i = i->pmb_next)
630431cc 101 if (!strcmp(filename, i->pmb_filename))
43f3cec2
BB
102 return i;
103
104 return NULL;
630431cc 105}
43f3cec2 106
c086b314 107static void fio_pmemblk_cache_insert(fio_pmemblk_file_t pmb)
43f3cec2
BB
108{
109 pmb->pmb_next = Cache;
110 Cache = pmb;
630431cc 111}
43f3cec2 112
c086b314 113static void fio_pmemblk_cache_remove(fio_pmemblk_file_t pmb)
43f3cec2 114{
c086b314 115 fio_pmemblk_file_t i;
43f3cec2
BB
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 }
630431cc 129}
43f3cec2
BB
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 *
0cbb3f53 136 * path[,bsize,fsizemib]
43f3cec2
BB
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 *
0cbb3f53 146 * note that the user specifies the file size in MiB, but
43f3cec2
BB
147 * we return bytes from here.
148 */
630431cc
JA
149static void pmb_parse_path(const char *pathspec, char **ppath, uint64_t *pbsize,
150 uint64_t *pfsize)
43f3cec2 151{
c086b314
JA
152 char *path;
153 char *s;
154 uint64_t bsize;
0cbb3f53 155 uint64_t fsizemib;
43f3cec2
BB
156
157 path = strdup(pathspec);
630431cc 158 if (!path) {
43f3cec2
BB
159 *ppath = NULL;
160 return;
161 }
162
163 /* extract sizes, if given */
164 s = strrchr(path, ',');
0cbb3f53 165 if (s && (fsizemib = strtoull(s + 1, NULL, 10))) {
43f3cec2
BB
166 *s = 0;
167 s = strrchr(path, ',');
c086b314 168 if (s && (bsize = strtoull(s + 1, NULL, 10))) {
43f3cec2 169 *s = 0;
c086b314 170 *ppath = path;
43f3cec2 171 *pbsize = bsize;
0cbb3f53 172 *pfsize = fsizemib << 20;
43f3cec2
BB
173 return;
174 }
175 }
176
177 /* size specs not found */
178 strcpy(path, pathspec);
c086b314 179 *ppath = path;
43f3cec2
BB
180 *pbsize = 0;
181 *pfsize = 0;
630431cc 182}
43f3cec2 183
630431cc 184static fio_pmemblk_file_t pmb_open(const char *pathspec, int flags)
43f3cec2 185{
c086b314
JA
186 fio_pmemblk_file_t pmb;
187 char *path = NULL;
188 uint64_t bsize = 0;
189 uint64_t fsize = 0;
43f3cec2
BB
190
191 pmb_parse_path(pathspec, &path, &bsize, &fsize);
630431cc 192 if (!path)
43f3cec2
BB
193 return NULL;
194
de88999f 195 pthread_mutex_lock(&CacheLock);
43f3cec2
BB
196
197 pmb = fio_pmemblk_cache_lookup(path);
630431cc 198 if (!pmb) {
43f3cec2 199 pmb = malloc(sizeof(*pmb));
630431cc 200 if (!pmb)
43f3cec2
BB
201 goto error;
202
203 /* try opening existing first, create it if needed */
204 pmb->pmb_pool = pmemblk_open(path, bsize);
630431cc 205 if (!pmb->pmb_pool && (errno == ENOENT) &&
c086b314
JA
206 (flags & PMB_CREATE) && (0 < fsize) && (0 < bsize)) {
207 pmb->pmb_pool =
208 pmemblk_create(path, bsize, fsize, 0644);
43f3cec2 209 }
630431cc 210 if (!pmb->pmb_pool) {
a0998600
RE
211 log_err("pmemblk: unable to open pmemblk pool file %s (%s)\n",
212 path, strerror(errno));
43f3cec2
BB
213 goto error;
214 }
215
216 pmb->pmb_filename = path;
c086b314
JA
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);
43f3cec2
BB
221
222 fio_pmemblk_cache_insert(pmb);
223 }
224
225 pmb->pmb_refcnt += 1;
226
de88999f 227 pthread_mutex_unlock(&CacheLock);
c086b314 228
43f3cec2
BB
229 return pmb;
230
231error:
630431cc
JA
232 if (pmb) {
233 if (pmb->pmb_pool)
43f3cec2 234 pmemblk_close(pmb->pmb_pool);
c086b314 235 pmb->pmb_pool = NULL;
43f3cec2
BB
236 pmb->pmb_filename = NULL;
237 free(pmb);
238 }
630431cc 239 if (path)
43f3cec2 240 free(path);
de88999f
JA
241
242 pthread_mutex_unlock(&CacheLock);
43f3cec2 243 return NULL;
630431cc 244}
43f3cec2 245
630431cc 246static void pmb_close(fio_pmemblk_file_t pmb, const bool keep)
43f3cec2 247{
de88999f 248 pthread_mutex_lock(&CacheLock);
43f3cec2
BB
249
250 pmb->pmb_refcnt--;
251
630431cc 252 if (!keep && !pmb->pmb_refcnt) {
43f3cec2
BB
253 pmemblk_close(pmb->pmb_pool);
254 pmb->pmb_pool = NULL;
255 free(pmb->pmb_filename);
256 pmb->pmb_filename = NULL;
257 fio_pmemblk_cache_remove(pmb);
258 free(pmb);
259 }
260
de88999f 261 pthread_mutex_unlock(&CacheLock);
630431cc 262}
43f3cec2 263
630431cc 264static int pmb_get_flags(struct thread_data *td, uint64_t *pflags)
43f3cec2 265{
c086b314 266 static int thread_warned = 0;
43f3cec2
BB
267 static int odirect_warned = 0;
268
c086b314 269 uint64_t flags = 0;
43f3cec2
BB
270
271 if (!td->o.use_thread) {
272 if (!thread_warned) {
273 thread_warned = 1;
a0998600 274 log_err("pmemblk: must set thread=1 for pmemblk engine\n");
43f3cec2
BB
275 }
276 return 1;
277 }
278
279 if (!td->o.odirect && !odirect_warned) {
280 odirect_warned = 1;
a0998600 281 log_info("pmemblk: direct == 0, but pmemblk is always direct\n");
43f3cec2
BB
282 }
283
284 if (td->o.allow_create)
285 flags |= PMB_CREATE;
286
287 (*pflags) = flags;
288 return 0;
630431cc 289}
43f3cec2 290
c086b314 291static int fio_pmemblk_open_file(struct thread_data *td, struct fio_file *f)
43f3cec2 292{
c086b314
JA
293 uint64_t flags = 0;
294 fio_pmemblk_file_t pmb;
43f3cec2 295
630431cc 296 if (pmb_get_flags(td, &flags))
43f3cec2
BB
297 return 1;
298
299 pmb = pmb_open(f->file_name, flags);
630431cc 300 if (!pmb)
43f3cec2
BB
301 return 1;
302
710bf9c5 303 FILE_SET_ENG_DATA(f, pmb);
43f3cec2 304 return 0;
630431cc 305}
43f3cec2 306
630431cc
JA
307static int fio_pmemblk_close_file(struct thread_data fio_unused *td,
308 struct fio_file *f)
43f3cec2 309{
710bf9c5 310 fio_pmemblk_file_t pmb = FILE_ENG_DATA(f);
43f3cec2
BB
311
312 if (pmb)
630431cc 313 pmb_close(pmb, false);
43f3cec2 314
710bf9c5 315 FILE_SET_ENG_DATA(f, NULL);
43f3cec2 316 return 0;
630431cc 317}
43f3cec2 318
c086b314 319static int fio_pmemblk_get_file_size(struct thread_data *td, struct fio_file *f)
43f3cec2 320{
c086b314 321 uint64_t flags = 0;
710bf9c5 322 fio_pmemblk_file_t pmb = FILE_ENG_DATA(f);
43f3cec2
BB
323
324 if (fio_file_size_known(f))
325 return 0;
326
630431cc
JA
327 if (!pmb) {
328 if (pmb_get_flags(td, &flags))
43f3cec2
BB
329 return 1;
330 pmb = pmb_open(f->file_name, flags);
630431cc 331 if (!pmb)
43f3cec2
BB
332 return 1;
333 }
334
335 f->real_file_size = pmb->pmb_bsize * pmb->pmb_nblocks;
336
337 fio_file_set_size_known(f);
338
710bf9c5 339 if (!FILE_ENG_DATA(f))
630431cc 340 pmb_close(pmb, true);
43f3cec2
BB
341
342 return 0;
630431cc 343}
43f3cec2 344
2e4ef4fb
JA
345static enum fio_q_status fio_pmemblk_queue(struct thread_data *td,
346 struct io_u *io_u)
43f3cec2 347{
c086b314 348 struct fio_file *f = io_u->file;
710bf9c5 349 fio_pmemblk_file_t pmb = FILE_ENG_DATA(f);
43f3cec2 350
c086b314
JA
351 unsigned long long off;
352 unsigned long len;
353 void *buf;
43f3cec2
BB
354
355 fio_ro_check(td, io_u);
356
357 switch (io_u->ddir) {
358 case DDIR_READ:
43f3cec2
BB
359 case DDIR_WRITE:
360 off = io_u->offset;
361 len = io_u->xfer_buflen;
362
363 io_u->error = EINVAL;
630431cc 364 if (off % pmb->pmb_bsize)
43f3cec2 365 break;
630431cc 366 if (len % pmb->pmb_bsize)
43f3cec2
BB
367 break;
368 if ((off + len) / pmb->pmb_bsize > pmb->pmb_nblocks)
369 break;
370
371 io_u->error = 0;
372 buf = io_u->xfer_buf;
373 off /= pmb->pmb_bsize;
374 len /= pmb->pmb_bsize;
375 while (0 < len) {
cf8775b8
RE
376 if (io_u->ddir == DDIR_READ &&
377 0 != pmemblk_read(pmb->pmb_pool, buf, off)) {
378 io_u->error = errno;
379 break;
380 } else if (0 != pmemblk_write(pmb->pmb_pool, buf, off)) {
43f3cec2
BB
381 io_u->error = errno;
382 break;
383 }
384 buf += pmb->pmb_bsize;
385 off++;
386 len--;
387 }
388 off *= pmb->pmb_bsize;
389 len *= pmb->pmb_bsize;
390 io_u->resid = io_u->xfer_buflen - (off - io_u->offset);
391 break;
392 case DDIR_SYNC:
393 case DDIR_DATASYNC:
394 case DDIR_SYNC_FILE_RANGE:
395 /* we're always sync'd */
396 io_u->error = 0;
397 break;
398 default:
399 io_u->error = EINVAL;
400 break;
401 }
402
403 return FIO_Q_COMPLETED;
630431cc 404}
43f3cec2 405
c086b314 406static int fio_pmemblk_unlink_file(struct thread_data *td, struct fio_file *f)
43f3cec2 407{
c086b314
JA
408 char *path = NULL;
409 uint64_t bsize = 0;
410 uint64_t fsize = 0;
43f3cec2
BB
411
412 /*
413 * we need our own unlink in case the user has specified
414 * the block and file sizes in the path name. we parse
415 * the file_name to determine the file name we actually used.
416 */
417
418 pmb_parse_path(f->file_name, &path, &bsize, &fsize);
630431cc 419 if (!path)
2442c935 420 return ENOENT;
43f3cec2
BB
421
422 unlink(path);
423 free(path);
43f3cec2 424 return 0;
630431cc 425}
43f3cec2 426
13690c10 427static struct ioengine_ops ioengine = {
c086b314
JA
428 .name = "pmemblk",
429 .version = FIO_IOOPS_VERSION,
430 .queue = fio_pmemblk_queue,
431 .open_file = fio_pmemblk_open_file,
432 .close_file = fio_pmemblk_close_file,
433 .get_file_size = fio_pmemblk_get_file_size,
434 .unlink_file = fio_pmemblk_unlink_file,
435 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL,
43f3cec2
BB
436};
437
630431cc 438static void fio_init fio_pmemblk_register(void)
43f3cec2
BB
439{
440 register_ioengine(&ioengine);
441}
442
630431cc 443static void fio_exit fio_pmemblk_unregister(void)
43f3cec2
BB
444{
445 unregister_ioengine(&ioengine);
446}