@@ -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
625626template <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-
648636template <typename T>
649637CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setDefaultAllocSizes(
650638 std::set<uint32_t > allocSizes) {
@@ -886,72 +874,16 @@ CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::enableItemReaperInBackground(
886874template <typename T>
887875CacheAllocatorConfig<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
11511083template <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
11691100template <typename T>
0 commit comments