Commit | Line | Data |
---|---|---|
8fabefc1 KS |
1 | /* |
2 | * librpma_fio_pmem: allocates pmem using libpmem. | |
3 | * | |
4 | * Copyright 2022, Intel Corporation | |
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 | ||
16 | #include <libpmem.h> | |
17 | #include "librpma_fio.h" | |
18 | ||
19 | #define RPMA_PMEM_USED "libpmem" | |
20 | ||
21 | static int librpma_fio_pmem_map_file(struct fio_file *f, size_t size, | |
22 | struct librpma_fio_mem *mem, size_t ws_offset) | |
23 | { | |
24 | int is_pmem = 0; | |
25 | size_t size_mmap = 0; | |
26 | ||
27 | /* map the file */ | |
28 | mem->mem_ptr = pmem_map_file(f->file_name, 0 /* len */, 0 /* flags */, | |
29 | 0 /* mode */, &size_mmap, &is_pmem); | |
30 | if (mem->mem_ptr == NULL) { | |
31 | /* pmem_map_file() sets errno on failure */ | |
32 | log_err("fio: pmem_map_file(%s) failed: %s (errno %i)\n", | |
33 | f->file_name, strerror(errno), errno); | |
34 | return -1; | |
35 | } | |
36 | ||
37 | /* pmem is expected */ | |
38 | if (!is_pmem) { | |
39 | log_err("fio: %s is not located in persistent memory\n", | |
40 | f->file_name); | |
41 | goto err_unmap; | |
42 | } | |
43 | ||
44 | /* check size of allocated persistent memory */ | |
45 | if (size_mmap < ws_offset + size) { | |
46 | log_err( | |
47 | "fio: %s is too small to handle so many threads (%zu < %zu)\n", | |
48 | f->file_name, size_mmap, ws_offset + size); | |
49 | goto err_unmap; | |
50 | } | |
51 | ||
52 | log_info("fio: size of memory mapped from the file %s: %zu\n", | |
53 | f->file_name, size_mmap); | |
54 | ||
55 | mem->size_mmap = size_mmap; | |
56 | ||
57 | return 0; | |
58 | ||
59 | err_unmap: | |
60 | (void) pmem_unmap(mem->mem_ptr, size_mmap); | |
61 | return -1; | |
62 | } | |
63 | ||
64 | static inline void librpma_fio_unmap(struct librpma_fio_mem *mem) | |
65 | { | |
66 | (void) pmem_unmap(mem->mem_ptr, mem->size_mmap); | |
67 | } |