Skip to content

Commit b2c9cbf

Browse files
rlyerlymeta-codesync[bot]
authored andcommitted
Support huge pages for the temp shm mapping
Summary: ^ Reviewed By: AlnisM Differential Revision: D113626413 fbshipit-source-id: 851439097c0374002f7eca50a58697828f61c4c4
1 parent 99d7f23 commit b2c9cbf

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

cachelib/allocator/CacheAllocator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2760,7 +2760,8 @@ CacheAllocator<CacheTrait>::CacheAllocator(
27602760
: config.memMonitoringEnabled()},
27612761
config_(config.validate()),
27622762
tempShm_(type == InitMemType::kNone && isOnShm_
2763-
? std::make_unique<TempShmMapping>(config_.getCacheSize())
2763+
? std::make_unique<TempShmMapping>(config_.getCacheSize(),
2764+
config_.hugePageSize)
27642765
: nullptr),
27652766
shmManager_(type != InitMemType::kNone
27662767
? std::make_unique<ShmManager>(config_.cacheDir,

cachelib/allocator/TempShmMapping.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
#include <folly/logging/xlog.h>
2020
#include <sys/mman.h>
2121

22+
#include <algorithm>
23+
2224
#include "cachelib/allocator/memory/Slab.h"
2325
#include "cachelib/common/Utils.h"
2426

2527
namespace facebook::cachelib {
2628

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")),
3031
shmManager_(createShmManager(tempCacheDir_)),
31-
addr_(createShmMapping(*shmManager_.get(), size, tempCacheDir_)) {}
32+
addr_(createShmMapping(
33+
*shmManager_.get(), size, tempCacheDir_, hugePageSize)) {}
3234

3335
TempShmMapping::~TempShmMapping() {
3436
try {
@@ -61,14 +63,22 @@ std::unique_ptr<ShmManager> TempShmMapping::createShmManager(
6163

6264
void* TempShmMapping::createShmMapping(ShmManager& shmManager,
6365
size_t size,
64-
const std::string& cacheDir) {
66+
const std::string& cacheDir,
67+
const PageSize& hugePageSize) {
6568
void* addr = nullptr;
6669
void* shmAddr = nullptr;
6770
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);
7077
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;
7282
// Mark the shared memory segment to be removed on exit. This will ensure
7383
// that the segment is dropped on exit.
7484
auto& shm = shmManager.getShmByName(detail::kTempShmCacheName.str());

cachelib/allocator/TempShmMapping.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ constexpr folly::StringPiece kTempShmCacheName = "temp_shm_cache";
4141
// the cache is on a shared memory segment.
4242
class TempShmMapping {
4343
public:
44-
explicit TempShmMapping(size_t size);
44+
explicit TempShmMapping(size_t size, PageSize hugePageSize = PageSize());
4545
~TempShmMapping();
4646
// get the start of addrress.
4747
void* getAddr() const { return addr_; }
@@ -51,9 +51,9 @@ class TempShmMapping {
5151
const std::string& cacheDir);
5252
static void* createShmMapping(ShmManager& shmManager,
5353
size_t size,
54-
const std::string& cacheDir);
54+
const std::string& cacheDir,
55+
const PageSize& hugePageSize);
5556

56-
size_t size_{0};
5757
std::string tempCacheDir_;
5858
std::unique_ptr<ShmManager> shmManager_;
5959
void* addr_{nullptr};

0 commit comments

Comments
 (0)