|
| 1 | +/* |
| 2 | + * Copyright (c) Intel Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <folly/Random.h> |
| 18 | + |
| 19 | +#include <numeric> |
| 20 | + |
| 21 | +#include "cachelib/allocator/CacheAllocator.h" |
| 22 | +#include "cachelib/allocator/tests/TestBase.h" |
| 23 | + |
| 24 | +namespace facebook { |
| 25 | +namespace cachelib { |
| 26 | +namespace tests { |
| 27 | + |
| 28 | +using LruAllocatorConfig = CacheAllocatorConfig<LruAllocator>; |
| 29 | +using LruMemoryTierConfigs = LruAllocatorConfig::MemoryTierConfigs; |
| 30 | +using Strings = std::vector<std::string>; |
| 31 | +using Ratios = std::vector<size_t>; |
| 32 | + |
| 33 | +constexpr size_t MB = 1024ULL * 1024ULL; |
| 34 | +constexpr size_t GB = MB * 1024ULL; |
| 35 | + |
| 36 | +const size_t defaultTotalCacheSize{1 * GB}; |
| 37 | +const std::string defaultCacheDir{"/var/metadataDir"}; |
| 38 | + |
| 39 | +template <typename Allocator> |
| 40 | +class MemoryTiersTest: public AllocatorTest<Allocator> { |
| 41 | +public: |
| 42 | + void basicCheck( |
| 43 | + LruAllocatorConfig& actualConfig, |
| 44 | + size_t expectedTotalCacheSize = defaultTotalCacheSize, |
| 45 | + const std::string& expectedCacheDir = defaultCacheDir) { |
| 46 | + EXPECT_EQ(actualConfig.getCacheSize(), expectedTotalCacheSize); |
| 47 | + auto configs = actualConfig.getMemoryTierConfigs(); |
| 48 | + |
| 49 | + size_t sum_ratios = std::accumulate(configs.begin(), configs.end(), 0UL, |
| 50 | + [](const size_t i, const MemoryTierCacheConfig& config) { return i + config.getRatio();}); |
| 51 | + size_t sum_sizes = std::accumulate(configs.begin(), configs.end(), 0UL, |
| 52 | + [&](const size_t i, const MemoryTierCacheConfig& config) { |
| 53 | + return i + config.calculateTierSize(actualConfig.getCacheSize(), sum_ratios); |
| 54 | + }); |
| 55 | + |
| 56 | + EXPECT_GE(expectedTotalCacheSize, sum_ratios * Slab::kSize); |
| 57 | + EXPECT_LE(sum_sizes, expectedTotalCacheSize); |
| 58 | + EXPECT_GE(sum_sizes, expectedTotalCacheSize - configs.size() * Slab::kSize); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + LruAllocatorConfig createTestCacheConfig( |
| 63 | + const Ratios& tierRatios = {1}, |
| 64 | + bool setPosixForShm = true, |
| 65 | + size_t cacheSize = defaultTotalCacheSize, |
| 66 | + const std::string& cacheDir = defaultCacheDir) { |
| 67 | + LruAllocatorConfig cfg; |
| 68 | + cfg.setCacheSize(cacheSize) |
| 69 | + .enableCachePersistence(cacheDir); |
| 70 | + |
| 71 | + if (setPosixForShm) |
| 72 | + cfg.usePosixForShm(); |
| 73 | + LruMemoryTierConfigs tierConfigs; |
| 74 | + tierConfigs.reserve(tierRatios.size()); |
| 75 | + for(auto i = 0; i < tierRatios.size(); ++i) { |
| 76 | + tierConfigs.push_back(MemoryTierCacheConfig::fromShm() |
| 77 | + .setRatio(tierRatios[i]) |
| 78 | + .setMemBind(std::string("0"))); |
| 79 | + } |
| 80 | + |
| 81 | + cfg.configureMemoryTiers(tierConfigs); |
| 82 | + return cfg; |
| 83 | + } |
| 84 | + |
| 85 | + LruAllocatorConfig createTieredCacheConfig(size_t totalCacheSize, |
| 86 | + size_t numTiers = 2) { |
| 87 | + LruAllocatorConfig tieredCacheConfig{}; |
| 88 | + std::vector<MemoryTierCacheConfig> configs; |
| 89 | + for (auto i = 1; i <= numTiers; ++i) { |
| 90 | + configs.push_back(MemoryTierCacheConfig::fromShm() |
| 91 | + .setRatio(1) |
| 92 | + .setMemBind(std::string("0"))); |
| 93 | + } |
| 94 | + tieredCacheConfig.setCacheSize(totalCacheSize) |
| 95 | + .enableCachePersistence( |
| 96 | + folly::sformat("/tmp/multi-tier-test/{}", ::getpid())) |
| 97 | + .usePosixForShm() |
| 98 | + .configureMemoryTiers(configs); |
| 99 | + return tieredCacheConfig; |
| 100 | + } |
| 101 | + |
| 102 | + LruAllocatorConfig createDramCacheConfig(size_t totalCacheSize) { |
| 103 | + LruAllocatorConfig dramConfig{}; |
| 104 | + dramConfig.setCacheSize(totalCacheSize); |
| 105 | + return dramConfig; |
| 106 | + } |
| 107 | + |
| 108 | + void validatePoolSize(PoolId poolId, |
| 109 | + std::unique_ptr<LruAllocator>& allocator, |
| 110 | + size_t expectedSize) { |
| 111 | + size_t actualSize = allocator->getPool(poolId).getPoolSize(); |
| 112 | + EXPECT_EQ(actualSize, expectedSize); |
| 113 | + } |
| 114 | + |
| 115 | + void testAddPool(std::unique_ptr<LruAllocator>& alloc, |
| 116 | + size_t poolSize, |
| 117 | + bool isSizeValid = true, |
| 118 | + size_t numTiers = 2) { |
| 119 | + if (isSizeValid) { |
| 120 | + auto pool = alloc->addPool("validPoolSize", poolSize); |
| 121 | + EXPECT_LE(alloc->getPool(pool).getPoolSize(), poolSize); |
| 122 | + if (poolSize >= numTiers * Slab::kSize) |
| 123 | + EXPECT_GE(alloc->getPool(pool).getPoolSize(), poolSize - numTiers * Slab::kSize); |
| 124 | + } else { |
| 125 | + EXPECT_THROW(alloc->addPool("invalidPoolSize", poolSize), |
| 126 | + std::invalid_argument); |
| 127 | + // TODO: test this for all tiers |
| 128 | + EXPECT_EQ(alloc->getPoolIds().size(), 0); |
| 129 | + } |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +using LruMemoryTiersTest = MemoryTiersTest<LruAllocator>; |
| 134 | + |
| 135 | +TEST_F(LruMemoryTiersTest, TestValid1TierConfig) { |
| 136 | + LruAllocatorConfig cfg = createTestCacheConfig().validate(); |
| 137 | + basicCheck(cfg); |
| 138 | +} |
| 139 | + |
| 140 | +TEST_F(LruMemoryTiersTest, TestValid2TierConfig) { |
| 141 | + LruAllocatorConfig cfg = createTestCacheConfig({1, 1}); |
| 142 | + basicCheck(cfg); |
| 143 | +} |
| 144 | + |
| 145 | +TEST_F(LruMemoryTiersTest, TestValid2TierRatioConfig) { |
| 146 | + LruAllocatorConfig cfg = createTestCacheConfig({5, 2}); |
| 147 | + basicCheck(cfg); |
| 148 | +} |
| 149 | + |
| 150 | +TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigNumberOfPartitionsTooLarge) { |
| 151 | + EXPECT_THROW(createTestCacheConfig({defaultTotalCacheSize, 1}).validate(), |
| 152 | + std::invalid_argument); |
| 153 | +} |
| 154 | + |
| 155 | +TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigSizesAndRatioNotSet) { |
| 156 | + EXPECT_THROW(createTestCacheConfig({1, 0}), |
| 157 | + std::invalid_argument); |
| 158 | +} |
| 159 | + |
| 160 | +TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigRatiosCacheSizeNotSet) { |
| 161 | + EXPECT_THROW(createTestCacheConfig({1, 1}, true, |
| 162 | + /* cacheSize */ 0).validate(), |
| 163 | + std::invalid_argument); |
| 164 | +} |
| 165 | + |
| 166 | +TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigRatioNotSet) { |
| 167 | + EXPECT_THROW( |
| 168 | + createTestCacheConfig({1, 0}), |
| 169 | + std::invalid_argument); |
| 170 | +} |
| 171 | + |
| 172 | +TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigSizesNeCacheSize) { |
| 173 | + EXPECT_THROW(createTestCacheConfig({0, 0}), |
| 174 | + std::invalid_argument); |
| 175 | +} |
| 176 | +} // namespace tests |
| 177 | +} // namespace cachelib |
| 178 | +} // namespace facebook |
0 commit comments