2 * libpmem: IO engine that uses PMDK libpmem to read and write data
4 * Copyright (C) 2017 Nippon Telegraph and Telephone Corporation.
5 * Copyright 2018-2021, Intel Corporation
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License,
9 * version 2 as published by the Free Software Foundation..
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
21 * IO engine that uses libpmem (part of PMDK collection) to write data
22 * and libc's memcpy to read. It requires PMDK >= 1.5.
27 * Other relevant settings:
31 * directory=/mnt/pmem0/
34 * sync=1 means that pmem_drain() is executed for each write operation.
35 * Otherwise is not and should be called on demand.
37 * direct=1 means PMEM_F_MEM_NONTEMPORAL flag is set in pmem_memcpy().
39 * The pmem device must have a DAX-capable filesystem and be mounted
40 * with DAX enabled. Directory must point to a mount point of DAX FS.
45 * mount -o dax /dev/pmem0 /mnt/pmem0
47 * See examples/libpmem.fio for complete usage example.
57 #include "../verify.h"
59 struct fio_libpmem_data {
65 static int fio_libpmem_init(struct thread_data *td)
67 struct thread_options *o = &td->o;
69 dprint(FD_IO, "o->rw_min_bs %llu\n o->fsync_blocks %u\n o->fdatasync_blocks %u\n",
70 o->rw_min_bs, o->fsync_blocks, o->fdatasync_blocks);
71 dprint(FD_IO, "DEBUG fio_libpmem_init\n");
73 if ((o->rw_min_bs & page_mask) &&
74 (o->fsync_blocks || o->fdatasync_blocks)) {
75 log_err("libpmem: mmap options dictate a minimum block size of "
76 "%llu bytes\n", (unsigned long long) page_size);
83 * This is the pmem_map_file execution function, a helper to
84 * fio_libpmem_open_file function.
86 static int fio_libpmem_file(struct thread_data *td, struct fio_file *f,
87 size_t length, off_t off)
89 struct fio_libpmem_data *fdd = FILE_ENG_DATA(f);
90 mode_t mode = S_IWUSR | S_IRUSR;
94 dprint(FD_IO, "DEBUG fio_libpmem_file\n");
95 dprint(FD_IO, "f->file_name = %s td->o.verify = %d \n", f->file_name,
97 dprint(FD_IO, "length = %ld f->fd = %d off = %ld file mode = %d \n",
98 length, f->fd, off, mode);
100 /* unmap any existing mapping */
101 if (fdd->libpmem_ptr) {
102 dprint(FD_IO,"pmem_unmap \n");
103 if (pmem_unmap(fdd->libpmem_ptr, fdd->libpmem_sz) < 0)
105 fdd->libpmem_ptr = NULL;
108 if((fdd->libpmem_ptr = pmem_map_file(f->file_name, length, PMEM_FILE_CREATE, mode, &mapped_len, &is_pmem)) == NULL) {
109 td_verror(td, errno, pmem_errormsg());
114 td_verror(td, errno, "file_name does not point to persistent memory");
118 if (td->error && fdd->libpmem_ptr)
119 pmem_unmap(fdd->libpmem_ptr, length);
124 static int fio_libpmem_open_file(struct thread_data *td, struct fio_file *f)
126 struct fio_libpmem_data *fdd;
128 dprint(FD_IO, "DEBUG fio_libpmem_open_file\n");
129 dprint(FD_IO, "f->io_size=%ld\n", f->io_size);
130 dprint(FD_IO, "td->o.size=%lld\n", td->o.size);
131 dprint(FD_IO, "td->o.iodepth=%d\n", td->o.iodepth);
132 dprint(FD_IO, "td->o.iodepth_batch=%d\n", td->o.iodepth_batch);
134 if (fio_file_open(f))
135 td_io_close_file(td, f);
137 fdd = calloc(1, sizeof(*fdd));
141 FILE_SET_ENG_DATA(f, fdd);
142 fdd->libpmem_sz = f->io_size;
143 fdd->libpmem_off = 0;
145 return fio_libpmem_file(td, f, fdd->libpmem_sz, fdd->libpmem_off);
148 static int fio_libpmem_prep(struct thread_data *td, struct io_u *io_u)
150 struct fio_file *f = io_u->file;
151 struct fio_libpmem_data *fdd = FILE_ENG_DATA(f);
153 dprint(FD_IO, "DEBUG fio_libpmem_prep\n");
154 dprint(FD_IO, "io_u->offset %llu : fdd->libpmem_off %ld : "
155 "io_u->buflen %llu : fdd->libpmem_sz %ld\n",
156 io_u->offset, fdd->libpmem_off,
157 io_u->buflen, fdd->libpmem_sz);
159 if (io_u->buflen > f->real_file_size) {
160 log_err("libpmem: bs bigger than the file size\n");
164 io_u->mmap_data = fdd->libpmem_ptr + io_u->offset - fdd->libpmem_off
169 static enum fio_q_status fio_libpmem_queue(struct thread_data *td,
174 fio_ro_check(td, io_u);
177 dprint(FD_IO, "DEBUG fio_libpmem_queue\n");
178 dprint(FD_IO, "td->o.odirect %d td->o.sync_io %d\n",
179 td->o.odirect, td->o.sync_io);
180 /* map both O_SYNC / DSYNC to not use NODRAIN */
181 flags = td->o.sync_io ? 0 : PMEM_F_MEM_NODRAIN;
182 flags |= td->o.odirect ? PMEM_F_MEM_NONTEMPORAL : PMEM_F_MEM_TEMPORAL;
184 switch (io_u->ddir) {
186 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
189 dprint(FD_IO, "DEBUG mmap_data=%p, xfer_buf=%p\n",
190 io_u->mmap_data, io_u->xfer_buf);
191 pmem_memcpy(io_u->mmap_data,
198 case DDIR_SYNC_FILE_RANGE:
202 io_u->error = EINVAL;
206 return FIO_Q_COMPLETED;
209 static int fio_libpmem_close_file(struct thread_data *td, struct fio_file *f)
211 struct fio_libpmem_data *fdd = FILE_ENG_DATA(f);
214 dprint(FD_IO, "DEBUG fio_libpmem_close_file\n");
216 if (fdd->libpmem_ptr)
217 ret = pmem_unmap(fdd->libpmem_ptr, fdd->libpmem_sz);
218 if (fio_file_open(f))
219 ret &= generic_close_file(td, f);
221 FILE_SET_ENG_DATA(f, NULL);
227 FIO_STATIC struct ioengine_ops ioengine = {
229 .version = FIO_IOOPS_VERSION,
230 .init = fio_libpmem_init,
231 .prep = fio_libpmem_prep,
232 .queue = fio_libpmem_queue,
233 .open_file = fio_libpmem_open_file,
234 .close_file = fio_libpmem_close_file,
235 .get_file_size = generic_get_file_size,
236 .prepopulate_file = generic_prepopulate_file,
237 .flags = FIO_SYNCIO | FIO_RAWIO | FIO_DISKLESSIO | FIO_NOEXTEND |
238 FIO_NODISKUTIL | FIO_BARRIER | FIO_MEMALIGN,
241 static void fio_init fio_libpmem_register(void)
243 register_ioengine(&ioengine);
246 static void fio_exit fio_libpmem_unregister(void)
248 unregister_ioengine(&ioengine);