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