@@ -207,15 +207,15 @@ class CacheAllocatorConfig {
207207 // cachePersistence().
208208 CacheAllocatorConfig& usePosixForShm ();
209209
210- // Configures cache memory tiers. Accepts vector of MemoryTierCacheConfig.
211- // Each vector element describes configuration for a single memory cache tier .
212- // @throw std::invalid_argument if:
213- // - the size of configs is 0
214- // - memory tiers use both size and ratio parameters
210+ // Configures cache memory tiers. Each tier represents a cache region inside
211+ // byte-addressable memory such as DRAM, Pmem, CXLmem .
212+ // Accepts vector of MemoryTierCacheConfig. Each vector element describes
213+ // configuration for a single memory cache tier. Tier sizes are specified as
214+ // ratios, the number of parts of total cache size each tier would occupy.
215215 CacheAllocatorConfig& configureMemoryTiers (const MemoryTierConfigs& configs);
216216
217- // Return vector of memory tier configs .
218- MemoryTierConfigs getMemoryTierConfigs () const ;
217+ // Return reference to MemoryTierCacheConfigs .
218+ const MemoryTierConfigs& getMemoryTierConfigs () const ;
219219
220220 // This turns on a background worker that periodically scans through the
221221 // access container and look for expired items and remove them.
@@ -369,7 +369,7 @@ class CacheAllocatorConfig {
369369
370370 const std::string& getCacheName () const noexcept { return cacheName; }
371371
372- size_t getCacheSize () const noexcept ;
372+ size_t getCacheSize () const noexcept { return size; }
373373
374374 bool isUsingPosixShm () const noexcept { return usePosixShm; }
375375
@@ -391,8 +391,7 @@ class CacheAllocatorConfig {
391391 std::map<std::string, std::string> serialize () const ;
392392
393393 // The max number of memory cache tiers
394- // TODO: increase this number when multi-tier configs are enabled
395- inline static const size_t kMaxCacheMemoryTiers = 1 ;
394+ inline static const size_t kMaxCacheMemoryTiers = 2 ;
396395
397396 // Cache name for users to indentify their own cache.
398397 std::string cacheName{" " };
@@ -608,8 +607,6 @@ class CacheAllocatorConfig {
608607 friend CacheT;
609608
610609 private:
611- void validateMemoryTiersWithSize (const MemoryTierConfigs&, size_t ) const ;
612-
613610 // Configuration for memory tiers.
614611 MemoryTierConfigs memoryTierConfigs{
615612 {MemoryTierCacheConfig::fromShm ().setRatio (1 )}
@@ -642,8 +639,6 @@ CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setCacheName(
642639
643640template <typename T>
644641CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setCacheSize(size_t _size) {
645- validateMemoryTiersWithSize (this ->memoryTierConfigs , _size);
646-
647642 size = _size;
648643 constexpr size_t maxCacheSizeWithCoredump = 64'424'509'440 ; // 60GB
649644 if (size <= maxCacheSizeWithCoredump) {
@@ -897,57 +892,24 @@ CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::enableItemReaperInBackground(
897892
898893template <typename T>
899894CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::configureMemoryTiers(
900- const MemoryTierConfigs& config) {
901- if (!config.size ()) {
902- throw std::invalid_argument (" There must be at least one memory tier." );
895+ const MemoryTierConfigs& config) {
896+ if (config.size () > kMaxCacheMemoryTiers ) {
897+ throw std::invalid_argument (folly::sformat (
898+ " Too many memory tiers. The number of supported tiers is {}." ,
899+ kMaxCacheMemoryTiers ));
903900 }
904-
905- for (auto tier_config: config) {
906- auto tier_size = tier_config.getSize ();
907- auto tier_ratio = tier_config.getRatio ();
908- if ((!tier_size and !tier_ratio) || (tier_size and tier_ratio)) {
909- throw std::invalid_argument (
910- " For each memory tier either size or ratio must be set." );
911- }
901+ if (!config.size ()) {
902+ throw std::invalid_argument (
903+ " There must be at least one memory tier config." );
912904 }
913-
914- validateMemoryTiersWithSize (config, this ->size );
915-
916905 memoryTierConfigs = config;
917-
918906 return *this ;
919907}
920908
921909template <typename T>
922- typename CacheAllocatorConfig<T>::MemoryTierConfigs
910+ const typename CacheAllocatorConfig<T>::MemoryTierConfigs&
923911CacheAllocatorConfig<T>::getMemoryTierConfigs() const {
924- MemoryTierConfigs config = memoryTierConfigs;
925- size_t sum_ratios = 0 ;
926-
927- for (auto &tier_config: config) {
928- if (auto *v = std::get_if<PosixSysVSegmentOpts>(&tier_config.shmOpts )) {
929- v->usePosix = usePosixShm;
930- }
931-
932- sum_ratios += tier_config.getRatio ();
933- }
934-
935- if (sum_ratios == 0 )
936- return config;
937-
938- // if ratios are used, size must be specified
939- XDCHECK (size);
940-
941- // Convert ratios to sizes, size must be non-zero
942- size_t sum_sizes = 0 ;
943- size_t partition_size = size / sum_ratios;
944- for (auto & tier_config: config) {
945- tier_config.setSize (partition_size * tier_config.getRatio ());
946- tier_config.setRatio (0 );
947- sum_sizes += tier_config.getSize ();
948- }
949-
950- return config;
912+ return memoryTierConfigs;
951913}
952914
953915template <typename T>
@@ -1079,46 +1041,6 @@ CacheAllocatorConfig<T>::setSkipPromoteChildrenWhenParentFailed() {
10791041 return *this ;
10801042}
10811043
1082- template <typename T>
1083- size_t CacheAllocatorConfig<T>::getCacheSize() const noexcept {
1084- if (size)
1085- return size;
1086-
1087- size_t sum_sizes = 0 ;
1088- for (const auto &tier_config : getMemoryTierConfigs ()) {
1089- sum_sizes += tier_config.getSize ();
1090- }
1091-
1092- return sum_sizes;
1093- }
1094-
1095- template <typename T>
1096- void CacheAllocatorConfig<T>::validateMemoryTiersWithSize(
1097- const MemoryTierConfigs &config, size_t size) const {
1098- size_t sum_ratios = 0 ;
1099- size_t sum_sizes = 0 ;
1100-
1101- for (const auto &tier_config: config) {
1102- sum_ratios += tier_config.getRatio ();
1103- sum_sizes += tier_config.getSize ();
1104- }
1105-
1106- if (sum_ratios && sum_sizes) {
1107- throw std::invalid_argument (" Cannot mix ratios and sizes." );
1108- } else if (sum_sizes) {
1109- if (size && sum_sizes != size) {
1110- throw std::invalid_argument (
1111- " Sum of tier sizes doesn't match total cache size. "
1112- " Setting of cache total size is not required when per-tier "
1113- " sizes are specified - it is calculated as sum of tier sizes." );
1114- }
1115- } else if (!sum_ratios && !sum_sizes) {
1116- throw std::invalid_argument (
1117- " Either sum of all memory tiers sizes or sum of all ratios "
1118- " must be greater than 0." );
1119- }
1120- }
1121-
11221044template <typename T>
11231045const CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::validate() const {
11241046 // we can track tail hits only if MMType is MM2Q
@@ -1143,23 +1065,7 @@ const CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::validate() const {
11431065 " It's not allowed to enable both RemoveCB and ItemDestructor." );
11441066 }
11451067
1146- size_t sum_ratios = 0 ;
1147- for (auto tier_config: memoryTierConfigs) {
1148- sum_ratios += tier_config.getRatio ();
1149- }
1150-
1151- if (sum_ratios) {
1152- if (!size) {
1153- throw std::invalid_argument (
1154- " Total cache size must be specified when size ratios are "
1155- " used to specify memory tier sizes." );
1156- } else if (size < sum_ratios) {
1157- throw std::invalid_argument (
1158- " Sum of all tier size ratios is greater than total cache size." );
1159- }
1160- }
1161-
1162- return *this ;
1068+ return validateMemoryTiers ();
11631069}
11641070
11651071template <typename T>
0 commit comments