Skip to content

Commit 441c35a

Browse files
Disabled memory tier size setting
1 parent 43a44d6 commit 441c35a

File tree

3 files changed

+52
-183
lines changed

3 files changed

+52
-183
lines changed

cachelib/allocator/CacheAllocatorConfig.h

Lines changed: 24 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,8 @@ class CacheAllocatorConfig {
378378
bool validateStrategy(
379379
const std::shared_ptr<PoolOptimizeStrategy>& strategy) const;
380380

381-
// check whether sizes are set for each memoriy tier and the
382-
// sum of sizes matches total cache size
383-
void validateMemoryTierSizes() const;
381+
// check that memory tier ratios are set properly
382+
bool validateMemoryTiers() const;
384383

385384
// @return a map representation of the configs
386385
std::map<std::string, std::string> serialize() const;
@@ -391,6 +390,10 @@ class CacheAllocatorConfig {
391390
// Amount of memory for this cache instance (sum of all memory tiers' sizes)
392391
size_t size = 1 * 1024 * 1024 * 1024;
393392

393+
// The max number of memory cache tiers
394+
// TODO: increase this number when multi-tier configs are enabled
395+
const size_t maxCacheMemoryTiers = 1;
396+
394397
// Directory for shared memory related metadata
395398
std::string cacheDir;
396399

@@ -595,11 +598,9 @@ class CacheAllocatorConfig {
595598
friend CacheT;
596599

597600
private:
598-
CacheAllocatorConfig& setCacheSizeImpl(size_t _size);
599-
600601
// Configuration for memory tiers.
601602
MemoryTierConfigs memoryTierConfigs{
602-
{MemoryTierCacheConfig::fromShm().setRatio(1).setSize(size)}};
603+
{MemoryTierCacheConfig::fromShm().setRatio(1)}};
603604

604605
void mergeWithPrefix(
605606
std::map<std::string, std::string>& configMap,
@@ -623,8 +624,7 @@ CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setCacheName(
623624
}
624625

625626
template <typename T>
626-
CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setCacheSizeImpl(
627-
size_t _size) {
627+
CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setCacheSize(size_t _size) {
628628
size = _size;
629629
constexpr size_t maxCacheSizeWithCoredump = 64'424'509'440; // 60GB
630630
if (size <= maxCacheSizeWithCoredump) {
@@ -633,18 +633,6 @@ CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setCacheSizeImpl(
633633
return *this;
634634
}
635635

636-
template <typename T>
637-
CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setCacheSize(size_t _size) {
638-
if (memoryTierConfigs.size() == 1) {
639-
memoryTierConfigs[0].setSize(_size);
640-
} else {
641-
throw std::invalid_argument(
642-
"Cannot set cache size after configuring memory tiers.");
643-
}
644-
645-
return setCacheSizeImpl(_size);
646-
}
647-
648636
template <typename T>
649637
CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setDefaultAllocSizes(
650638
std::set<uint32_t> allocSizes) {
@@ -886,72 +874,16 @@ CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::enableItemReaperInBackground(
886874
template <typename T>
887875
CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::configureMemoryTiers(
888876
const MemoryTierConfigs& config) {
889-
if (config.size() != 1) {
890-
throw std::invalid_argument(
891-
"Only single memory tier is currently supported.");
892-
}
893-
memoryTierConfigs = config;
894-
size_t sumRatios = 0;
895-
size_t sumSizes = 0;
896-
897-
for (const auto& tierConfig : memoryTierConfigs) {
898-
auto tierSize = tierConfig.getSize();
899-
auto tierRatio = tierConfig.getRatio();
900-
if ((sumRatios && tierSize) || (sumSizes && tierRatio)) {
901-
throw std::invalid_argument(
902-
"For each memory tier either size or ratio must be set.");
903-
}
904-
sumRatios += tierRatio;
905-
sumSizes += tierSize;
906-
}
907-
908-
if (!getCacheSize()) {
909-
if (sumRatios) {
910-
throw std::invalid_argument(
911-
"Total cache size must be specified when size ratios are "
912-
"used to specify memory tier sizes.");
913-
} else if (sumSizes) {
914-
setCacheSizeImpl(sumSizes);
915-
}
877+
if (config.size() > maxCacheMemoryTiers) {
878+
throw std::invalid_argument(folly::sformat(
879+
"Too many memory tiers. The number of supported tiers is {}.",
880+
maxCacheMemoryTiers));
916881
}
917-
918-
if (sumRatios) {
919-
if (!getCacheSize()) {
920-
throw std::invalid_argument(
921-
"Total cache size must be specified when size ratios are "
922-
"used to specify memory tier sizes.");
923-
} else {
924-
if (getCacheSize() < sumRatios) {
925-
throw std::invalid_argument(
926-
"Sum of all tier size ratios is greater than total cache size.");
927-
}
928-
// Convert ratios to sizes
929-
sumSizes = 0;
930-
size_t partitionSize = getCacheSize() / sumRatios;
931-
for (auto& tierConfig : memoryTierConfigs) {
932-
tierConfig.setSize(partitionSize * tierConfig.getRatio());
933-
sumSizes += tierConfig.getSize();
934-
}
935-
if (getCacheSize() != sumSizes) {
936-
// Adjust capacity of the last tier to account for rounding error
937-
memoryTierConfigs.back().setSize(memoryTierConfigs.back().getSize() +
938-
(getCacheSize() - sumSizes));
939-
sumSizes = getCacheSize();
940-
}
941-
}
942-
} else if (sumSizes) {
943-
if (sumSizes != getCacheSize()) {
944-
throw std::invalid_argument(
945-
"Sum of tier sizes doesn't match total cache size. "
946-
"Setting of cache total size is not required when per-tier "
947-
"sizes are specified - it is calculated as sum of tier sizes.");
948-
}
949-
} else {
882+
if (!config.size()) {
950883
throw std::invalid_argument(
951-
"Either sum of all memory tiers sizes or sum of all ratios "
952-
"must be greater than 0.");
884+
"There must be at least one memory tier config.");
953885
}
954-
886+
memoryTierConfigs = config;
955887
return *this;
956888
}
957889

@@ -1119,7 +1051,7 @@ const CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::validate() const {
11191051
"It's not allowed to enable both RemoveCB and ItemDestructor.");
11201052
}
11211053

1122-
validateMemoryTierSizes();
1054+
validateMemoryTiers();
11231055

11241056
return *this;
11251057
}
@@ -1149,21 +1081,20 @@ bool CacheAllocatorConfig<T>::validateStrategy(
11491081
}
11501082

11511083
template <typename T>
1152-
void CacheAllocatorConfig<T>::validateMemoryTierSizes() const {
1153-
size_t sumSizes = 0;
1154-
1084+
bool CacheAllocatorConfig<T>::validateMemoryTiers() const {
1085+
size_t parts = 0;
11551086
for (const auto& tierConfig : memoryTierConfigs) {
1156-
if (!tierConfig.getSize()) {
1157-
throw std::invalid_argument("Each cache tier must have size set.");
1158-
} else {
1159-
sumSizes += tierConfig.getSize();
1087+
if (!tierConfig.getRatio()) {
1088+
throw std::invalid_argument("Tier ratio must be an integer number >=1.");
11601089
}
1090+
parts += tierConfig.getRatio();
11611091
}
11621092

1163-
if (sumSizes != size) {
1093+
if (parts > size) {
11641094
throw std::invalid_argument(
1165-
"Sum of sizes of all cache tiers must equal to total cache size.");
1095+
"Sum of tier ratios must be less than total cache size.");
11661096
}
1097+
return true;
11671098
}
11681099

11691100
template <typename T>

cachelib/allocator/MemoryTierCacheConfig.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,43 @@ class MemoryTierCacheConfig {
3030
return MemoryTierCacheConfig();
3131
}
3232

33-
// Specifies size of this memory tier. Sizes of tiers must be specified by
34-
// either setting size explicitly or using ratio, mixing of the two is not
35-
// supported.
36-
MemoryTierCacheConfig& setSize(size_t _size) {
37-
size = _size;
38-
return *this;
39-
}
40-
4133
// Specifies ratio of this memory tier to other tiers. Absolute size
4234
// of each tier can be calculated as:
4335
// cacheSize * tierRatio / Sum of ratios for all tiers; the difference
4436
// between total cache size and sum of all tier sizes resulted from
4537
// round off error is accounted for when calculating the last tier's
4638
// size to make the totals equal.
4739
MemoryTierCacheConfig& setRatio(size_t _ratio) {
40+
if (!_ratio) {
41+
throw std::invalid_argument("Tier ratio must be an integer number >=1.");
42+
}
4843
ratio = _ratio;
4944
return *this;
5045
}
5146

5247
size_t getRatio() const noexcept { return ratio; }
5348

54-
size_t getSize() const noexcept { return size; }
49+
size_t calculateTierSize(size_t totalCacheSize, size_t partitionNum) {
50+
size_t partitionSize = 0;
51+
if (partitionNum > totalCacheSize) {
52+
throw std::invalid_argument(
53+
"Ratio must be less or equal to total cache size.");
54+
}
5555

56-
// Size of this memory tiers
57-
size_t size{0};
56+
if (!partitionNum) {
57+
throw std::invalid_argument(
58+
"The total number of tier ratios must be an integer number >=1.");
59+
}
60+
61+
return getRatio() * (totalCacheSize / partitionNum);
62+
}
5863

5964
// Ratio is a number of parts of the total cache size to be allocated for this
6065
// tier. E.g. if X is a total cache size, Yi are ratios specified for memory
6166
// tiers, and Y is the sum of all Yi, then size of the i-th tier
6267
// Xi = (X / Y) * Yi. For examle, to configure 2-tier cache where each
6368
// tier is a half of the total cache size, set both tiers' ratios to 1.
64-
size_t ratio{0};
69+
size_t ratio{1};
6570

6671
private:
6772
// TODO: introduce a container for tier settings when adding support for

cachelib/allocator/tests/CacheAllocatorConfigTest.cpp

Lines changed: 11 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ namespace tests {
2626
using AllocatorT = LruAllocator;
2727
using MemoryTierConfigs = CacheAllocatorConfig<AllocatorT>::MemoryTierConfigs;
2828

29-
// TODO: Increase this constant when multi-tier configs are fully enabled
30-
size_t maxNumTiers = 1;
31-
size_t defaultTotalSize = 2 * Slab::kSize * maxNumTiers;
29+
size_t defaultTotalSize = 1 * 1024LL * 1024LL * 1024LL;
3230

3331
class CacheAllocatorConfigTest : public testing::Test {};
3432

@@ -37,14 +35,6 @@ MemoryTierConfigs generateTierConfigs(size_t numTiers,
3735
return MemoryTierConfigs(numTiers, config);
3836
}
3937

40-
size_t sumTierSizes(const MemoryTierConfigs& configs) {
41-
size_t sumSizes = 0;
42-
for (const auto& config : configs) {
43-
sumSizes += config.getSize();
44-
}
45-
return sumSizes;
46-
}
47-
4838
TEST_F(CacheAllocatorConfigTest, MultipleTier0Config) {
4939
AllocatorT::Config config;
5040
// Throws if vector of tier configs is emptry
@@ -53,84 +43,27 @@ TEST_F(CacheAllocatorConfigTest, MultipleTier0Config) {
5343
}
5444

5545
TEST_F(CacheAllocatorConfigTest, MultipleTier1Config) {
56-
AllocatorT::Config config1, config2;
46+
AllocatorT::Config config;
5747
// Accepts single-tier configuration
58-
config1.setCacheSize(defaultTotalSize)
59-
.configureMemoryTiers(
60-
{MemoryTierCacheConfig::fromShm().setSize(defaultTotalSize)});
61-
config2.setCacheSize(0).configureMemoryTiers(
62-
{MemoryTierCacheConfig::fromShm().setSize(defaultTotalSize)});
48+
config.setCacheSize(defaultTotalSize)
49+
.configureMemoryTiers({MemoryTierCacheConfig::fromShm().setRatio(1)});
6350
}
6451

65-
TEST_F(CacheAllocatorConfigTest, MultipleTier2Config) {
52+
TEST_F(CacheAllocatorConfigTest, InvalidTierRatios) {
6653
AllocatorT::Config config;
67-
// Throws if vector of tier configs > 1
6854
EXPECT_THROW(config.configureMemoryTiers(generateTierConfigs(
69-
maxNumTiers + 1,
70-
MemoryTierCacheConfig::fromShm().setSize(defaultTotalSize))),
55+
config.maxCacheMemoryTiers + 1,
56+
MemoryTierCacheConfig::fromShm().setRatio(0))),
7157
std::invalid_argument);
7258
}
7359

74-
TEST_F(CacheAllocatorConfigTest, TotalCacheSizeSet0) {
75-
AllocatorT::Config config;
76-
// If total cache size is set to zero and each individual tier specifies its
77-
// size then total cache size is set to the sum of tier sizes
78-
MemoryTierConfigs tierConfigs = {
79-
MemoryTierCacheConfig::fromShm().setSize(defaultTotalSize)};
80-
config.setCacheSize(0).configureMemoryTiers(tierConfigs);
81-
ASSERT_EQ(config.getCacheSize(), sumTierSizes(config.getMemoryTierConfigs()));
82-
}
83-
8460
TEST_F(CacheAllocatorConfigTest, TotalCacheSizeLessThanRatios) {
8561
AllocatorT::Config config;
8662
// Throws if total cache size is set to 0
87-
EXPECT_THROW(
88-
config.setCacheSize(defaultTotalSize)
89-
.configureMemoryTiers({MemoryTierCacheConfig::fromShm().setRatio(
90-
defaultTotalSize + 1)}),
91-
std::invalid_argument);
92-
}
93-
94-
TEST_F(CacheAllocatorConfigTest, SumTierSizes) {
95-
AllocatorT::Config config1, config2;
96-
MemoryTierConfigs sizeConfigs = generateTierConfigs(
97-
maxNumTiers,
98-
MemoryTierCacheConfig::fromShm().setSize(defaultTotalSize / maxNumTiers));
99-
MemoryTierConfigs ratioConfigs = generateTierConfigs(
100-
maxNumTiers, MemoryTierCacheConfig::fromShm().setRatio(1));
101-
102-
config1.setCacheSize(defaultTotalSize).configureMemoryTiers(sizeConfigs);
103-
ASSERT_EQ(config1.getCacheSize(),
104-
sumTierSizes(config1.getMemoryTierConfigs()));
105-
106-
config2.setCacheSize(defaultTotalSize).configureMemoryTiers(ratioConfigs);
107-
ASSERT_EQ(config2.getCacheSize(),
108-
sumTierSizes(config2.getMemoryTierConfigs()));
109-
}
110-
111-
TEST_F(CacheAllocatorConfigTest, TotalCacheSizeReset1Tier) {
112-
AllocatorT::Config config1, config2;
113-
size_t numTiers = 1;
114-
MemoryTierConfigs sizeConfigs = generateTierConfigs(
115-
numTiers,
116-
MemoryTierCacheConfig::fromShm().setSize(defaultTotalSize / numTiers));
117-
MemoryTierConfigs ratioConfigs = generateTierConfigs(
118-
numTiers, MemoryTierCacheConfig::fromShm().setRatio(1));
119-
120-
// Resetting total cache size when there's only one tier
121-
// is considered harmless. TODO: when multiple tiers are
122-
// enabled this test needs to be expanded to check that
123-
// validate() throws if total cache size is reset to
124-
// invalid number after configureMemoryTiers is called.
125-
config1.setCacheSize(defaultTotalSize)
126-
.configureMemoryTiers(sizeConfigs)
127-
.setCacheSize(defaultTotalSize - 1);
128-
config1.validate();
129-
130-
config2.setCacheSize(defaultTotalSize)
131-
.configureMemoryTiers(ratioConfigs)
132-
.setCacheSize(defaultTotalSize - 1);
133-
config2.validate();
63+
config.setCacheSize(defaultTotalSize)
64+
.configureMemoryTiers(
65+
{MemoryTierCacheConfig::fromShm().setRatio(defaultTotalSize + 1)});
66+
EXPECT_THROW(config.validate(), std::invalid_argument);
13467
}
13568

13669
} // namespace tests

0 commit comments

Comments
 (0)