2 * libpmem: IO engine that uses PMDK libpmem to read and write data
4 * Copyright (C) 2017 Nippon Telegraph and Telephone Corporation.
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..
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.
20 * IO engine that uses libpmem to read and write data
25 * Other relevant settings:
28 * directory=/mnt/pmem0/
31 * direct=1 means that pmem_drain() is executed for each write operation.
32 * In contrast, direct=0 means that pmem_drain() is not executed.
34 * The pmem device must have a DAX-capable filesystem and be mounted
35 * with DAX enabled. directory must point to a mount point of DAX FS.
40 * mount -o dax /dev/pmem0 /mnt/pmem0
43 * See examples/libpmem.fio for more.
47 * By default, the libpmem engine will let the system find the libpmem.so
48 * that it uses. You can use an alternative libpmem by setting the
49 * FIO_PMEM_LIB environment variable to the full path to the desired
60 #include <sys/sysmacros.h>
65 #include "../verify.h"
68 * Limits us to 1GiB of mapped files in total to model after
69 * libpmem engine behavior
71 #define MMAP_TOTAL_SZ (1 * 1024 * 1024 * 1024UL)
73 struct fio_libpmem_data {
79 #define MEGABYTE ((uintptr_t)1 << 20)
80 #define GIGABYTE ((uintptr_t)1 << 30)
81 #define PROCMAXLEN 2048 /* maximum expected line length in /proc files */
82 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
84 static bool Mmap_no_random;
85 static void *Mmap_hint;
86 static unsigned long long Mmap_align;
89 * util_map_hint_align -- choose the desired mapping alignment
91 * Use 2MB/1GB page alignment only if the mapping length is at least
92 * twice as big as the page size.
94 static inline size_t util_map_hint_align(size_t len, size_t req_align)
96 size_t align = Mmap_align;
98 dprint(FD_IO, "DEBUG util_map_hint_align\n" );
102 else if (len >= 2 * GIGABYTE)
104 else if (len >= 4 * MEGABYTE)
105 align = 2 * MEGABYTE;
107 dprint(FD_IO, "align=%d\n", (int)align);
112 static const char *sscanf_os = "%p %p";
113 #define MAP_NORESERVE 0
114 #define OS_MAPFILE "/proc/curproc/map"
116 static const char *sscanf_os = "%p-%p";
117 #define OS_MAPFILE "/proc/self/maps"
121 * util_map_hint_unused -- use /proc to determine a hint address for mmap()
123 * This is a helper function for util_map_hint().
124 * It opens up /proc/self/maps and looks for the first unused address
125 * in the process address space that is:
126 * - greater or equal 'minaddr' argument,
127 * - large enough to hold range of given length,
128 * - aligned to the specified unit.
130 * Asking for aligned address like this will allow the DAX code to use large
131 * mappings. It is not an error if mmap() ignores the hint and chooses
134 static char *util_map_hint_unused(void *minaddr, size_t len, size_t align)
136 char *lo = NULL; /* beginning of current range in maps file */
137 char *hi = NULL; /* end of current range in maps file */
138 char *raddr = minaddr; /* ignore regions below 'minaddr' */
141 MEMORY_BASIC_INFORMATION mi;
144 char line[PROCMAXLEN]; /* for fgets() */
147 dprint(FD_IO, "DEBUG util_map_hint_unused\n");
153 raddr = (char *)roundup((uintptr_t)raddr, align);
156 while ((uintptr_t)raddr < UINTPTR_MAX - len) {
157 size_t ret = VirtualQuery(raddr, &mi, sizeof(mi));
159 ERR("VirtualQuery %p", raddr);
162 dprint(FD_IO, "addr %p len %zu state %d",
163 mi.BaseAddress, mi.RegionSize, mi.State);
165 if ((mi.State != MEM_FREE) || (mi.RegionSize < len)) {
166 raddr = (char *)mi.BaseAddress + mi.RegionSize;
167 raddr = (char *)roundup((uintptr_t)raddr, align);
168 dprint(FD_IO, "nearest aligned addr %p", raddr);
170 dprint(FD_IO, "unused region of size %zu found at %p",
171 mi.RegionSize, mi.BaseAddress);
172 return mi.BaseAddress;
176 dprint(FD_IO, "end of address space reached");
179 fp = fopen(OS_MAPFILE, "r");
181 log_err("!%s\n", OS_MAPFILE);
185 while (fgets(line, PROCMAXLEN, fp) != NULL) {
186 /* check for range line */
187 if (sscanf(line, sscanf_os, &lo, &hi) == 2) {
188 dprint(FD_IO, "%p-%p\n", lo, hi);
190 if ((uintptr_t)(lo - raddr) >= len) {
191 dprint(FD_IO, "unused region of size "
196 dprint(FD_IO, "region is too small: "
203 raddr = (char *)roundup((uintptr_t)hi, align);
204 dprint(FD_IO, "nearest aligned addr %p\n",
209 dprint(FD_IO, "end of address space reached\n");
216 * Check for a case when this is the last unused range in the address
217 * space, but is not large enough. (very unlikely)
219 if ((raddr != NULL) && (UINTPTR_MAX - (uintptr_t)raddr < len)) {
220 dprint(FD_IO, "end of address space reached");
226 dprint(FD_IO, "returning %p", raddr);
232 * util_map_hint -- determine hint address for mmap()
234 * If PMEM_MMAP_HINT environment variable is not set, we let the system to pick
235 * the randomized mapping address. Otherwise, a user-defined hint address
238 * Windows Environment:
239 * XXX - Windows doesn't support large DAX pages yet, so there is
240 * no point in aligning for the same.
242 * Except for Windows Environment:
243 * ALSR in 64-bit Linux kernel uses 28-bit of randomness for mmap
244 * (bit positions 12-39), which means the base mapping address is randomized
245 * within [0..1024GB] range, with 4KB granularity. Assuming additional
246 * 1GB alignment, it results in 1024 possible locations.
248 * Configuring the hint address via PMEM_MMAP_HINT environment variable
249 * disables address randomization. In such case, the function will search for
250 * the first unused, properly aligned region of given size, above the
253 static char *util_map_hint(size_t len, size_t req_align)
259 dprint(FD_IO, "DEBUG util_map_hint\n");
260 dprint(FD_IO, "len %zu req_align %zu\n", len, req_align);
262 /* choose the desired alignment based on the requested length */
263 align = util_map_hint_align(len, req_align);
265 e = getenv("PMEM_MMAP_HINT");
268 unsigned long long val = 0;
272 val = strtoull(e, &endp, 16);
273 if (errno || endp == e) {
274 dprint(FD_IO, "Invalid PMEM_MMAP_HINT\n");
276 Mmap_hint = (void *)val;
277 Mmap_no_random = true;
278 dprint(FD_IO, "PMEM_MMAP_HINT set to %p\n", Mmap_hint);
282 if (Mmap_no_random) {
283 dprint(FD_IO, "user-defined hint %p\n", (void *)Mmap_hint);
284 addr = util_map_hint_unused((void *)Mmap_hint, len, align);
287 * Create dummy mapping to find an unused region of given size.
288 * * Request for increased size for later address alignment.
290 * Windows Environment:
291 * Use MAP_NORESERVE flag to only reserve the range of pages
292 * rather than commit. We don't want the pages to be actually
293 * backed by the operating system paging file, as the swap
294 * file is usually too small to handle terabyte pools.
296 * Except for Windows Environment:
297 * Use MAP_PRIVATE with read-only access to simulate
298 * zero cost for overcommit accounting. Note: MAP_NORESERVE
299 * flag is ignored if overcommit is disabled (mode 2).
302 addr = mmap(NULL, len + align, PROT_READ,
303 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
305 addr = mmap(NULL, len + align, PROT_READ,
306 MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0);
308 if (addr != MAP_FAILED) {
309 dprint(FD_IO, "system choice %p\n", addr);
310 munmap(addr, len + align);
311 addr = (char *)roundup((uintptr_t)addr, align);
315 dprint(FD_IO, "hint %p\n", addr);
321 * This is the mmap execution function
323 static int fio_libpmem_file(struct thread_data *td, struct fio_file *f,
324 size_t length, off_t off)
326 struct fio_libpmem_data *fdd = FILE_ENG_DATA(f);
330 dprint(FD_IO, "DEBUG fio_libpmem_file\n");
333 flags = PROT_READ | PROT_WRITE;
334 else if (td_write(td)) {
337 if (td->o.verify != VERIFY_NONE)
342 dprint(FD_IO, "f->file_name = %s td->o.verify = %d \n", f->file_name,
344 dprint(FD_IO, "length = %ld flags = %d f->fd = %d off = %ld \n",
345 length, flags, f->fd,off);
347 addr = util_map_hint(length, 0);
349 fdd->libpmem_ptr = mmap(addr, length, flags, MAP_SHARED, f->fd, off);
350 if (fdd->libpmem_ptr == MAP_FAILED) {
351 fdd->libpmem_ptr = NULL;
352 td_verror(td, errno, "mmap");
355 if (td->error && fdd->libpmem_ptr)
356 munmap(fdd->libpmem_ptr, length);
362 * XXX Just mmap an appropriate portion, we cannot mmap the full extent
364 static int fio_libpmem_prep_limited(struct thread_data *td, struct io_u *io_u)
366 struct fio_file *f = io_u->file;
367 struct fio_libpmem_data *fdd = FILE_ENG_DATA(f);
369 dprint(FD_IO, "DEBUG fio_libpmem_prep_limited\n" );
371 if (io_u->buflen > f->real_file_size) {
372 log_err("libpmem: bs too big for libpmem engine\n");
376 fdd->libpmem_sz = min(MMAP_TOTAL_SZ, f->real_file_size);
377 if (fdd->libpmem_sz > f->io_size)
378 fdd->libpmem_sz = f->io_size;
380 fdd->libpmem_off = io_u->offset;
382 return fio_libpmem_file(td, f, fdd->libpmem_sz, fdd->libpmem_off);
386 * Attempt to mmap the entire file
388 static int fio_libpmem_prep_full(struct thread_data *td, struct io_u *io_u)
390 struct fio_file *f = io_u->file;
391 struct fio_libpmem_data *fdd = FILE_ENG_DATA(f);
394 dprint(FD_IO, "DEBUG fio_libpmem_prep_full\n" );
396 if (fio_file_partial_mmap(f))
399 dprint(FD_IO," f->io_size %ld : io_u->offset %lld \n",
400 f->io_size, io_u->offset);
402 if (io_u->offset != (size_t) io_u->offset ||
403 f->io_size != (size_t) f->io_size) {
404 fio_file_set_partial_mmap(f);
407 fdd->libpmem_sz = f->io_size;
408 fdd->libpmem_off = 0;
410 ret = fio_libpmem_file(td, f, fdd->libpmem_sz, fdd->libpmem_off);
412 fio_file_set_partial_mmap(f);
417 static int fio_libpmem_prep(struct thread_data *td, struct io_u *io_u)
419 struct fio_file *f = io_u->file;
420 struct fio_libpmem_data *fdd = FILE_ENG_DATA(f);
423 dprint(FD_IO, "DEBUG fio_libpmem_prep\n" );
425 * It fits within existing mapping, use it
427 dprint(FD_IO," io_u->offset %llu : fdd->libpmem_off %llu : "
428 "io_u->buflen %llu : fdd->libpmem_sz %llu\n",
429 io_u->offset, (unsigned long long) fdd->libpmem_off,
430 io_u->buflen, (unsigned long long) fdd->libpmem_sz);
432 if (io_u->offset >= fdd->libpmem_off &&
433 (io_u->offset + io_u->buflen <=
434 fdd->libpmem_off + fdd->libpmem_sz))
438 * unmap any existing mapping
440 if (fdd->libpmem_ptr) {
441 dprint(FD_IO,"munmap \n");
442 if (munmap(fdd->libpmem_ptr, fdd->libpmem_sz) < 0)
444 fdd->libpmem_ptr = NULL;
447 if (fio_libpmem_prep_full(td, io_u)) {
449 ret = fio_libpmem_prep_limited(td, io_u);
455 io_u->mmap_data = fdd->libpmem_ptr + io_u->offset - fdd->libpmem_off
460 static enum fio_q_status fio_libpmem_queue(struct thread_data *td,
463 fio_ro_check(td, io_u);
466 dprint(FD_IO, "DEBUG fio_libpmem_queue\n");
468 switch (io_u->ddir) {
470 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
473 dprint(FD_IO, "DEBUG mmap_data=%p, xfer_buf=%p\n",
474 io_u->mmap_data, io_u->xfer_buf );
475 dprint(FD_IO,"td->o.odirect %d \n",td->o.odirect);
477 pmem_memcpy_persist(io_u->mmap_data,
481 pmem_memcpy_nodrain(io_u->mmap_data,
488 case DDIR_SYNC_FILE_RANGE:
491 io_u->error = EINVAL;
495 return FIO_Q_COMPLETED;
498 static int fio_libpmem_init(struct thread_data *td)
500 struct thread_options *o = &td->o;
502 dprint(FD_IO,"o->rw_min_bs %llu \n o->fsync_blocks %d \n o->fdatasync_blocks %d \n",
503 o->rw_min_bs,o->fsync_blocks,o->fdatasync_blocks);
504 dprint(FD_IO, "DEBUG fio_libpmem_init\n");
506 if ((o->rw_min_bs & page_mask) &&
507 (o->fsync_blocks || o->fdatasync_blocks)) {
508 log_err("libpmem: mmap options dictate a minimum block size of "
509 "%llu bytes\n", (unsigned long long) page_size);
515 static int fio_libpmem_open_file(struct thread_data *td, struct fio_file *f)
517 struct fio_libpmem_data *fdd;
520 dprint(FD_IO,"DEBUG fio_libpmem_open_file\n");
521 dprint(FD_IO,"f->io_size=%ld \n",f->io_size);
522 dprint(FD_IO,"td->o.size=%lld \n",td->o.size);
523 dprint(FD_IO,"td->o.iodepth=%d\n",td->o.iodepth);
524 dprint(FD_IO,"td->o.iodepth_batch=%d \n",td->o.iodepth_batch);
526 ret = generic_open_file(td, f);
530 fdd = calloc(1, sizeof(*fdd));
532 int fio_unused __ret;
533 __ret = generic_close_file(td, f);
537 FILE_SET_ENG_DATA(f, fdd);
542 static int fio_libpmem_close_file(struct thread_data *td, struct fio_file *f)
544 struct fio_libpmem_data *fdd = FILE_ENG_DATA(f);
546 dprint(FD_IO,"DEBUG fio_libpmem_close_file\n");
547 dprint(FD_IO,"td->o.odirect %d \n",td->o.odirect);
549 if (!td->o.odirect) {
550 dprint(FD_IO,"pmem_drain\n");
554 FILE_SET_ENG_DATA(f, NULL);
556 fio_file_clear_partial_mmap(f);
558 return generic_close_file(td, f);
561 static struct ioengine_ops ioengine = {
563 .version = FIO_IOOPS_VERSION,
564 .init = fio_libpmem_init,
565 .prep = fio_libpmem_prep,
566 .queue = fio_libpmem_queue,
567 .open_file = fio_libpmem_open_file,
568 .close_file = fio_libpmem_close_file,
569 .get_file_size = generic_get_file_size,
570 .flags = FIO_SYNCIO |FIO_NOEXTEND,
573 static void fio_init fio_libpmem_register(void)
576 Mmap_align = page_size;
578 if (Mmap_align == 0) {
582 Mmap_align = si.dwAllocationGranularity;
586 register_ioengine(&ioengine);
589 static void fio_exit fio_libpmem_unregister(void)
591 unregister_ioengine(&ioengine);