1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*
* device DAX engine
*
* IO engine that reads/writes from files by doing memcpy to/from
* a memory mapped region of DAX enabled device.
*
* Copyright (C) 2016 Intel Corp
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License,
* version 2 as published by the Free Software Foundation..
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
/*
* device dax engine
* IO engine that access a DAX device directly for read and write data
*
* To use:
* ioengine=dev-dax
*
* Other relevant settings:
* iodepth=1
* direct=0 REQUIRED
* filename=/dev/daxN.N
* bs=2m
*
* direct should be left to 0. Using dev-dax implies that memory access
* is direct. However, dev-dax does not support O_DIRECT flag by design
* since it is not necessary.
*
* bs should adhere to the device dax alignment at minimally.
*
* libpmem.so
* By default, the dev-dax engine will let the system find the libpmem.so
* that it uses. You can use an alternative libpmem by setting the
* FIO_PMEM_LIB environment variable to the full path to the desired
* libpmem.so.
*/
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <libgen.h>
#include <libpmem.h>
#include "../fio.h"
#include "../verify.h"
/*
* Limits us to 1GiB of mapped files in total to model after
* mmap engine behavior
*/
#define MMAP_TOTAL_SZ (1 * 1024 * 1024 * 1024UL)
struct fio_devdax_data {
void *devdax_ptr;
size_t devdax_sz;
off_t devdax_off;
};
static int fio_devdax_file(struct thread_data *td, struct fio_file *f,
size_t length, off_t off)
{
struct fio_devdax_data *fdd = FILE_ENG_DATA(f);
int flags = 0;
if (td_rw(td))
flags = PROT_READ | PROT_WRITE;
else if (td_write(td)) {
flags = PROT_WRITE;
if (td->o.verify != VERIFY_NONE)
flags |= PROT_READ;
} else
flags = PROT_READ;
fdd->devdax_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off);
if (fdd->devdax_ptr == MAP_FAILED) {
fdd->devdax_ptr = NULL;
td_verror(td, errno, "mmap");
}
if (td->error && fdd->devdax_ptr)
munmap(fdd->devdax_ptr, length);
return td->error;
}
/*
* Just mmap an appropriate portion, we cannot mmap the full extent
*/
static int fio_devdax_prep_limited(struct thread_data *td, struct io_u *io_u)
{
struct fio_file *f = io_u->file;
struct fio_devdax_data *fdd = FILE_ENG_DATA(f);
if (io_u->buflen > f->real_file_size) {
log_err("dev-dax: bs too big for dev-dax engine\n");
return EIO;
}
fdd->devdax_sz = min(MMAP_TOTAL_SZ, f->real_file_size);
if (fdd->devdax_sz > f->io_size)
fdd->devdax_sz = f->io_size;
fdd->devdax_off = io_u->offset;
return fio_devdax_file(td, f, fdd->devdax_sz, fdd->devdax_off);
}
/*
* Attempt to mmap the entire file
*/
static int fio_devdax_prep_full(struct thread_data *td, struct io_u *io_u)
{
struct fio_file *f = io_u->file;
struct fio_devdax_data *fdd = FILE_ENG_DATA(f);
int ret;
if (fio_file_partial_mmap(f))
return EINVAL;
if (io_u->offset != (size_t) io_u->offset ||
f->io_size != (size_t) f->io_size) {
fio_file_set_partial_mmap(f);
return EINVAL;
}
fdd->devdax_sz = f->io_size;
fdd->devdax_off = 0;
ret = fio_devdax_file(td, f, fdd->devdax_sz, fdd->devdax_off);
if (ret)
fio_file_set_partial_mmap(f);
return ret;
}
static int fio_devdax_prep(struct thread_data *td, struct io_u *io_u)
{
struct fio_file *f = io_u->file;
struct fio_devdax_data *fdd = FILE_ENG_DATA(f);
int ret;
/*
* It fits within existing mapping, use it
*/
if (io_u->offset >= fdd->devdax_off &&
io_u->offset + io_u->buflen <= fdd->devdax_off + fdd->devdax_sz)
goto done;
/*
* unmap any existing mapping
*/
if (fdd->devdax_ptr) {
if (munmap(fdd->devdax_ptr, fdd->devdax_sz) < 0)
return errno;
fdd->devdax_ptr = NULL;
}
if (fio_devdax_prep_full(td, io_u)) {
td_clear_error(td);
ret = fio_devdax_prep_limited(td, io_u);
if (ret)
return ret;
}
done:
io_u->mmap_data = fdd->devdax_ptr + io_u->offset - fdd->devdax_off -
f->file_offset;
return 0;
}
static enum fio_q_status fio_devdax_queue(struct thread_data *td,
struct io_u *io_u)
{
fio_ro_check(td, io_u);
io_u->error = 0;
switch (io_u->ddir) {
case DDIR_READ:
memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
break;
case DDIR_WRITE:
pmem_memcpy_persist(io_u->mmap_data, io_u->xfer_buf,
io_u->xfer_buflen);
break;
case DDIR_SYNC:
case DDIR_DATASYNC:
case DDIR_SYNC_FILE_RANGE:
break;
default:
io_u->error = EINVAL;
break;
}
return FIO_Q_COMPLETED;
}
static int fio_devdax_init(struct thread_data *td)
{
struct thread_options *o = &td->o;
if ((o->rw_min_bs & page_mask) &&
(o->fsync_blocks || o->fdatasync_blocks)) {
log_err("dev-dax: mmap options dictate a minimum block size of %llu bytes\n",
(unsigned long long) page_size);
return 1;
}
return 0;
}
static int fio_devdax_open_file(struct thread_data *td, struct fio_file *f)
{
struct fio_devdax_data *fdd;
int ret;
ret = generic_open_file(td, f);
if (ret)
return ret;
fdd = calloc(1, sizeof(*fdd));
if (!fdd) {
int fio_unused __ret;
__ret = generic_close_file(td, f);
return 1;
}
FILE_SET_ENG_DATA(f, fdd);
return 0;
}
static int fio_devdax_close_file(struct thread_data *td, struct fio_file *f)
{
struct fio_devdax_data *fdd = FILE_ENG_DATA(f);
FILE_SET_ENG_DATA(f, NULL);
free(fdd);
fio_file_clear_partial_mmap(f);
return generic_close_file(td, f);
}
static int
fio_devdax_get_file_size(struct thread_data *td, struct fio_file *f)
{
char spath[PATH_MAX];
char npath[PATH_MAX];
char *rpath, *basename;
FILE *sfile;
uint64_t size;
struct stat st;
int rc;
if (fio_file_size_known(f))
return 0;
if (f->filetype != FIO_TYPE_CHAR)
return -EINVAL;
rc = stat(f->file_name, &st);
if (rc < 0) {
log_err("%s: failed to stat file %s (%s)\n",
td->o.name, f->file_name, strerror(errno));
return -errno;
}
snprintf(spath, PATH_MAX, "/sys/dev/char/%d:%d/subsystem",
major(st.st_rdev), minor(st.st_rdev));
rpath = realpath(spath, npath);
if (!rpath) {
log_err("%s: realpath on %s failed (%s)\n",
td->o.name, spath, strerror(errno));
return -errno;
}
/* check if DAX device */
basename = strrchr(rpath, '/');
if (!basename || strcmp("dax", basename+1)) {
log_err("%s: %s not a DAX device!\n",
td->o.name, f->file_name);
}
snprintf(spath, PATH_MAX, "/sys/dev/char/%d:%d/size",
major(st.st_rdev), minor(st.st_rdev));
sfile = fopen(spath, "r");
if (!sfile) {
log_err("%s: fopen on %s failed (%s)\n",
td->o.name, spath, strerror(errno));
return 1;
}
rc = fscanf(sfile, "%lu", &size);
if (rc < 0) {
log_err("%s: fscanf on %s failed (%s)\n",
td->o.name, spath, strerror(errno));
fclose(sfile);
return 1;
}
f->real_file_size = size;
fclose(sfile);
if (f->file_offset > f->real_file_size) {
log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
(unsigned long long) f->file_offset,
(unsigned long long) f->real_file_size);
return 1;
}
fio_file_set_size_known(f);
return 0;
}
FIO_STATIC struct ioengine_ops ioengine = {
.name = "dev-dax",
.version = FIO_IOOPS_VERSION,
.init = fio_devdax_init,
.prep = fio_devdax_prep,
.queue = fio_devdax_queue,
.open_file = fio_devdax_open_file,
.close_file = fio_devdax_close_file,
.get_file_size = fio_devdax_get_file_size,
.flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL,
};
static void fio_init fio_devdax_register(void)
{
register_ioengine(&ioengine);
}
static void fio_exit fio_devdax_unregister(void)
{
unregister_ioengine(&ioengine);
}
|