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