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