|
19 | 19 | #include <folly/logging/xlog.h> |
20 | 20 | #include <sys/mman.h> |
21 | 21 |
|
| 22 | +#include <algorithm> |
| 23 | + |
22 | 24 | #include "cachelib/allocator/memory/Slab.h" |
23 | 25 | #include "cachelib/common/Utils.h" |
24 | 26 |
|
25 | 27 | namespace facebook::cachelib { |
26 | 28 |
|
27 | | -TempShmMapping::TempShmMapping(size_t size) |
28 | | - : size_(size), |
29 | | - tempCacheDir_(util::getUniqueTempDir("cachedir")), |
| 29 | +TempShmMapping::TempShmMapping(size_t size, PageSize hugePageSize) |
| 30 | + : tempCacheDir_(util::getUniqueTempDir("cachedir")), |
30 | 31 | shmManager_(createShmManager(tempCacheDir_)), |
31 | | - addr_(createShmMapping(*shmManager_.get(), size, tempCacheDir_)) {} |
| 32 | + addr_(createShmMapping( |
| 33 | + *shmManager_.get(), size, tempCacheDir_, hugePageSize)) {} |
32 | 34 |
|
33 | 35 | TempShmMapping::~TempShmMapping() { |
34 | 36 | try { |
@@ -61,14 +63,22 @@ std::unique_ptr<ShmManager> TempShmMapping::createShmManager( |
61 | 63 |
|
62 | 64 | void* TempShmMapping::createShmMapping(ShmManager& shmManager, |
63 | 65 | size_t size, |
64 | | - const std::string& cacheDir) { |
| 66 | + const std::string& cacheDir, |
| 67 | + const PageSize& hugePageSize) { |
65 | 68 | void* addr = nullptr; |
66 | 69 | void* shmAddr = nullptr; |
67 | 70 | try { |
68 | | - addr = |
69 | | - util::mmapAlignedZeroedMemory(sizeof(Slab), size, true /* readOnly */); |
| 71 | + PageSize ps(hugePageSize); |
| 72 | + size = ps.getPageAlignedSize(size); |
| 73 | + const size_t alignment = std::max(sizeof(Slab), hugePageSize.getPageSize()); |
| 74 | + addr = util::mmapAlignedZeroedMemory(alignment, size, true /* readOnly */); |
| 75 | + ShmSegmentOpts opts; |
| 76 | + opts.pageSize = PageSize(hugePageSize); |
70 | 77 | shmAddr = |
71 | | - shmManager.createShm(detail::kTempShmCacheName.str(), size, addr).addr; |
| 78 | + shmManager |
| 79 | + .createShm( |
| 80 | + detail::kTempShmCacheName.str(), size, addr, std::move(opts)) |
| 81 | + .addr; |
72 | 82 | // Mark the shared memory segment to be removed on exit. This will ensure |
73 | 83 | // that the segment is dropped on exit. |
74 | 84 | auto& shm = shmManager.getShmByName(detail::kTempShmCacheName.str()); |
|
0 commit comments