Skip to content

Commit 0a7ed05

Browse files
igchorvictoria-mcgrath
authored andcommitted
Issue75 rebased (#88)
* #75: Use actual tier sizes (rounded down to slab size and decreased by header size) when creating new memory pools * Added getPoolSize method to calculate combined pool size for all tiers; added pool size validation to tests * Explicitly specified type for totalCacheSize to avoid overflow * Minor test change * Reworked tests * Minor change * Deleted redundant tests * Deleted unused constant * First set of changes to cache configuration API to enable multi-tier caches (facebook#138) Summary: These changes introduce per-tier cache configuration required to implement features discussed here: facebook#102. These specific changes enable single DRAM tier configs only which are compatible with the current version of cachelib. Configuration API will be expanded as multi-tier changes in other parts of the library are introduced. Pull Request resolved: facebook#138 Reviewed By: therealgymmy Differential Revision: D36189766 Pulled By: jiayuebao fbshipit-source-id: 947aa0cd800ea6accffc1b7b6b0c9693aa7fc0a5 Co-authored-by: Victoria McGrath <[email protected]>
1 parent 5b4ec2a commit 0a7ed05

8 files changed

+194
-70
lines changed

cachelib/allocator/CacheAllocator-inl.h

+13
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ ShmSegmentOpts CacheAllocator<CacheTrait>::createShmCacheOpts(TierId tid) {
125125
ShmSegmentOpts opts;
126126
opts.alignment = sizeof(Slab);
127127
opts.typeOpts = memoryTierConfigs[tid].getShmTypeOpts();
128+
if (auto *v = std::get_if<PosixSysVSegmentOpts>(&opts.typeOpts)) {
129+
v->usePosix = config_.usePosixShm;
130+
}
128131

129132
return opts;
130133
}
@@ -2508,6 +2511,16 @@ const std::string CacheAllocator<CacheTrait>::getCacheName() const {
25082511
return config_.cacheName;
25092512
}
25102513

2514+
template <typename CacheTrait>
2515+
size_t CacheAllocator<CacheTrait>::getPoolSize(PoolId poolId) const {
2516+
size_t poolSize = 0;
2517+
for (auto& allocator: allocator_) {
2518+
const auto& pool = allocator->getPool(poolId);
2519+
poolSize += pool.getPoolSize();
2520+
}
2521+
return poolSize;
2522+
}
2523+
25112524
template <typename CacheTrait>
25122525
PoolStats CacheAllocator<CacheTrait>::getPoolStats(PoolId poolId) const {
25132526
const auto& pool = allocator_[currentTier()]->getPool(poolId);

cachelib/allocator/CacheAllocator.h

+3
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,9 @@ class CacheAllocator : public CacheBase {
11121112
// whether it is object-cache
11131113
bool isObjectCache() const override final { return false; }
11141114

1115+
// combined pool size for all memory tiers
1116+
size_t getPoolSize(PoolId pid) const;
1117+
11151118
// pool stats by pool id
11161119
PoolStats getPoolStats(PoolId pid) const override final;
11171120

cachelib/allocator/CacheAllocatorConfig.h

-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <string>
2727

2828
#include "cachelib/allocator/Cache.h"
29-
#include "cachelib/allocator/MemoryTierCacheConfig.h"
3029
#include "cachelib/allocator/MM2Q.h"
3130
#include "cachelib/allocator/MemoryMonitor.h"
3231
#include "cachelib/allocator/MemoryTierCacheConfig.h"
@@ -392,7 +391,6 @@ class CacheAllocatorConfig {
392391
std::map<std::string, std::string> serialize() const;
393392

394393
// The max number of memory cache tiers
395-
// TODO: increase this number when multi-tier configs are enabled
396394
inline static const size_t kMaxCacheMemoryTiers = 2;
397395

398396
// Cache name for users to indentify their own cache.
@@ -901,11 +899,6 @@ CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::configureMemoryTiers(
901899
template <typename T>
902900
const typename CacheAllocatorConfig<T>::MemoryTierConfigs&
903901
CacheAllocatorConfig<T>::getMemoryTierConfigs() const {
904-
for (auto &tier_config: memoryTierConfigs) {
905-
if (auto *v = std::get_if<PosixSysVSegmentOpts>(&tier_config.shmOpts)) {
906-
const_cast<PosixSysVSegmentOpts*>(v)->usePosix = usePosixShm;
907-
}
908-
}
909902
return memoryTierConfigs;
910903
}
911904

cachelib/allocator/MemoryTierCacheConfig.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class MemoryTierCacheConfig {
7171

7272
const ShmTypeOpts& getShmTypeOpts() const noexcept { return shmOpts; }
7373

74+
private:
7475
// Ratio is a number of parts of the total cache size to be allocated for this
7576
// tier. E.g. if X is a total cache size, Yi are ratios specified for memory
7677
// tiers, and Y is the sum of all Yi, then size of the i-th tier
@@ -81,10 +82,6 @@ class MemoryTierCacheConfig {
8182
// Options specific to shm type
8283
ShmTypeOpts shmOpts;
8384

84-
private:
85-
// TODO: introduce a container for tier settings when adding support for
86-
// file-mapped memory
87-
8885
MemoryTierCacheConfig() = default;
8986
};
9087
} // namespace cachelib

cachelib/allocator/memory/SlabAllocator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
using namespace facebook::cachelib;
4141

4242
namespace {
43-
size_t roundDownToSlabSize(size_t size) { return size - (size % sizeof(Slab)); }
43+
static inline size_t roundDownToSlabSize(size_t size) { return size - (size % sizeof(Slab)); }
4444
} // namespace
4545

4646
// definitions to avoid ODR violation.

cachelib/allocator/tests/AllocatorMemoryTiersTest.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ namespace tests {
2323
using LruAllocatorMemoryTiersTest = AllocatorMemoryTiersTest<LruAllocator>;
2424

2525
// TODO(MEMORY_TIER): add more tests with different eviction policies
26-
TEST_F(LruAllocatorMemoryTiersTest, MultiTiers) { this->testMultiTiers(); }
26+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersInvalid) { this->testMultiTiersInvalid(); }
27+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValid) { this->testMultiTiersValid(); }
28+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValidMixed) { this->testMultiTiersValidMixed(); }
2729

2830
} // end of namespace tests
2931
} // end of namespace cachelib

cachelib/allocator/tests/AllocatorMemoryTiersTest.h

+43-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace tests {
2727
template <typename AllocatorT>
2828
class AllocatorMemoryTiersTest : public AllocatorTest<AllocatorT> {
2929
public:
30-
void testMultiTiers() {
30+
void testMultiTiersInvalid() {
3131
typename AllocatorT::Config config;
3232
config.setCacheSize(100 * Slab::kSize);
3333
config.configureMemoryTiers({
@@ -41,6 +41,48 @@ class AllocatorMemoryTiersTest : public AllocatorTest<AllocatorT> {
4141
ASSERT_THROW(std::make_unique<AllocatorT>(AllocatorT::SharedMemNew, config),
4242
std::invalid_argument);
4343
}
44+
45+
void testMultiTiersValid() {
46+
typename AllocatorT::Config config;
47+
config.setCacheSize(100 * Slab::kSize);
48+
config.enableCachePersistence("/tmp");
49+
config.usePosixForShm();
50+
config.configureMemoryTiers({
51+
MemoryTierCacheConfig::fromFile("/tmp/a" + std::to_string(::getpid()))
52+
.setRatio(1),
53+
MemoryTierCacheConfig::fromFile("/tmp/b" + std::to_string(::getpid()))
54+
.setRatio(1)
55+
});
56+
57+
auto alloc = std::make_unique<AllocatorT>(AllocatorT::SharedMemNew, config);
58+
ASSERT(alloc != nullptr);
59+
60+
auto pool = alloc->addPool("default", alloc->getCacheMemoryStats().cacheSize);
61+
auto handle = alloc->allocate(pool, "key", std::string("value").size());
62+
ASSERT(handle != nullptr);
63+
ASSERT_NO_THROW(alloc->insertOrReplace(handle));
64+
}
65+
66+
void testMultiTiersValidMixed() {
67+
typename AllocatorT::Config config;
68+
config.setCacheSize(100 * Slab::kSize);
69+
config.enableCachePersistence("/tmp");
70+
config.usePosixForShm();
71+
config.configureMemoryTiers({
72+
MemoryTierCacheConfig::fromShm()
73+
.setRatio(1),
74+
MemoryTierCacheConfig::fromFile("/tmp/b" + std::to_string(::getpid()))
75+
.setRatio(1)
76+
});
77+
78+
auto alloc = std::make_unique<AllocatorT>(AllocatorT::SharedMemNew, config);
79+
ASSERT(alloc != nullptr);
80+
81+
auto pool = alloc->addPool("default", alloc->getCacheMemoryStats().cacheSize);
82+
auto handle = alloc->allocate(pool, "key", std::string("value").size());
83+
ASSERT(handle != nullptr);
84+
ASSERT_NO_THROW(alloc->insertOrReplace(handle));
85+
}
4486
};
4587
} // namespace tests
4688
} // namespace cachelib

0 commit comments

Comments
 (0)