Commit | Line | Data |
---|---|---|
8fabefc1 KS |
1 | /* |
2 | * librpma_fio_pmem2: allocates pmem using libpmem2. | |
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 <libpmem2.h> | |
17 | #include "librpma_fio.h" | |
18 | ||
19 | #define RPMA_PMEM_USED "libpmem2" | |
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 fd; | |
25 | struct pmem2_config *cfg = NULL; | |
26 | struct pmem2_map *map = NULL; | |
27 | struct pmem2_source *src = NULL; | |
28 | ||
29 | size_t size_mmap; | |
30 | ||
31 | if((fd = open(f->file_name, O_RDWR)) < 0) { | |
32 | log_err("fio: cannot open fio file\n"); | |
33 | return -1; | |
34 | } | |
35 | ||
36 | if (pmem2_source_from_fd(&src, fd) != 0) { | |
37 | log_err("fio: pmem2_source_from_fd() failed\n"); | |
38 | goto err_close; | |
39 | } | |
40 | ||
41 | if (pmem2_config_new(&cfg) != 0) { | |
42 | log_err("fio: pmem2_config_new() failed\n"); | |
43 | goto err_source_delete; | |
44 | } | |
45 | ||
46 | if (pmem2_config_set_required_store_granularity(cfg, | |
47 | PMEM2_GRANULARITY_CACHE_LINE) != 0) { | |
48 | log_err("fio: pmem2_config_set_required_store_granularity() failed: %s\n", pmem2_errormsg()); | |
49 | goto err_config_delete; | |
50 | } | |
51 | ||
52 | if (pmem2_map_new(&map, cfg, src) != 0) { | |
53 | log_err("fio: pmem2_map_new(%s) failed: %s\n", f->file_name, pmem2_errormsg()); | |
54 | goto err_config_delete; | |
55 | } | |
56 | ||
57 | size_mmap = pmem2_map_get_size(map); | |
58 | ||
59 | /* check size of allocated persistent memory */ | |
60 | if (size_mmap < ws_offset + size) { | |
61 | log_err( | |
62 | "fio: %s is too small to handle so many threads (%zu < %zu)\n", | |
63 | f->file_name, size_mmap, ws_offset + size); | |
64 | goto err_map_delete; | |
65 | } | |
66 | ||
67 | mem->mem_ptr = pmem2_map_get_address(map); | |
68 | mem->size_mmap = size_mmap; | |
69 | mem->map = map; | |
70 | pmem2_config_delete(&cfg); | |
71 | pmem2_source_delete(&src); | |
72 | close(fd); | |
73 | ||
74 | return 0; | |
75 | ||
76 | err_map_delete: | |
77 | pmem2_map_delete(&map); | |
78 | err_config_delete: | |
79 | pmem2_config_delete(&cfg); | |
80 | err_source_delete: | |
81 | pmem2_source_delete(&src); | |
82 | err_close: | |
83 | close(fd); | |
84 | ||
85 | return -1; | |
86 | } | |
87 | ||
88 | static inline void librpma_fio_unmap(struct librpma_fio_mem *mem) | |
89 | { | |
90 | (void) pmem2_map_delete(&mem->map); | |
91 | } |