-
Notifications
You must be signed in to change notification settings - Fork 304
Extend memory tier configs to cache allocator based on NUMA bindings #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) Intel Corporation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "cachelib/allocator/tests/AllocatorMemoryTiersTest.h" | ||
|
||
namespace facebook { | ||
namespace cachelib { | ||
namespace tests { | ||
|
||
using LruAllocatorMemoryTiersTest = AllocatorMemoryTiersTest<LruAllocator>; | ||
|
||
// TODO(MEMORY_TIER): add more tests with different eviction policies | ||
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValid1) { | ||
this->testMultiTiersValid1(); | ||
} | ||
|
||
} // end of namespace tests | ||
} // end of namespace cachelib | ||
} // end of namespace facebook |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "cachelib/allocator/CacheAllocatorConfig.h" | ||
#include "cachelib/allocator/MemoryTierCacheConfig.h" | ||
#include "cachelib/allocator/tests/TestBase.h" | ||
|
||
namespace facebook { | ||
namespace cachelib { | ||
namespace tests { | ||
|
||
template <typename AllocatorT> | ||
class AllocatorMemoryTiersTest : public AllocatorTest<AllocatorT> { | ||
public: | ||
void testMultiTiersValid1() { | ||
typename AllocatorT::Config config; | ||
config.setCacheSize(100 * Slab::kSize); | ||
ASSERT_NO_THROW(config.configureMemoryTiers( | ||
{MemoryTierCacheConfig::fromShm().setRatio(1).setMemBind( | ||
std::string("0")), | ||
MemoryTierCacheConfig::fromShm().setRatio(1).setMemBind( | ||
std::string("0"))})); | ||
} | ||
}; | ||
} // namespace tests | ||
} // namespace cachelib | ||
} // namespace facebook |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* Copyright (c) Intel Corporation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <folly/Random.h> | ||
|
||
#include <numeric> | ||
|
||
#include "cachelib/allocator/CacheAllocator.h" | ||
#include "cachelib/allocator/tests/TestBase.h" | ||
|
||
namespace facebook { | ||
namespace cachelib { | ||
namespace tests { | ||
|
||
using LruAllocatorConfig = CacheAllocatorConfig<LruAllocator>; | ||
using LruMemoryTierConfigs = LruAllocatorConfig::MemoryTierConfigs; | ||
using Strings = std::vector<std::string>; | ||
using Ratios = std::vector<size_t>; | ||
|
||
constexpr size_t MB = 1024ULL * 1024ULL; | ||
constexpr size_t GB = MB * 1024ULL; | ||
|
||
const size_t defaultTotalCacheSize{1 * GB}; | ||
const std::string defaultCacheDir{"/var/metadataDir"}; | ||
|
||
template <typename Allocator> | ||
class MemoryTiersTest : public AllocatorTest<Allocator> { | ||
public: | ||
void basicCheck(LruAllocatorConfig& actualConfig, | ||
size_t expectedTotalCacheSize = defaultTotalCacheSize, | ||
const std::string& expectedCacheDir = defaultCacheDir) { | ||
EXPECT_EQ(actualConfig.getCacheSize(), expectedTotalCacheSize); | ||
auto configs = actualConfig.getMemoryTierConfigs(); | ||
|
||
size_t sum_ratios = std::accumulate( | ||
configs.begin(), configs.end(), 0UL, | ||
[](const size_t i, const MemoryTierCacheConfig& config) { | ||
return i + config.getRatio(); | ||
}); | ||
size_t sum_sizes = std::accumulate( | ||
configs.begin(), configs.end(), 0UL, | ||
[&](const size_t i, const MemoryTierCacheConfig& config) { | ||
return i + config.calculateTierSize(actualConfig.getCacheSize(), | ||
sum_ratios); | ||
}); | ||
|
||
EXPECT_GE(expectedTotalCacheSize, sum_ratios * Slab::kSize); | ||
EXPECT_LE(sum_sizes, expectedTotalCacheSize); | ||
EXPECT_GE(sum_sizes, expectedTotalCacheSize - configs.size() * Slab::kSize); | ||
} | ||
|
||
LruAllocatorConfig createTestCacheConfig( | ||
const Ratios& tierRatios = {1}, | ||
bool setPosixForShm = true, | ||
size_t cacheSize = defaultTotalCacheSize, | ||
const std::string& cacheDir = defaultCacheDir) { | ||
LruAllocatorConfig cfg; | ||
cfg.setCacheSize(cacheSize).enableCachePersistence(cacheDir); | ||
|
||
if (setPosixForShm) | ||
cfg.usePosixForShm(); | ||
LruMemoryTierConfigs tierConfigs; | ||
tierConfigs.reserve(tierRatios.size()); | ||
for (auto i = 0; i < tierRatios.size(); ++i) { | ||
tierConfigs.push_back(MemoryTierCacheConfig::fromShm() | ||
.setRatio(tierRatios[i]) | ||
.setMemBind(std::string("0"))); | ||
} | ||
|
||
cfg.configureMemoryTiers(tierConfigs); | ||
return cfg; | ||
} | ||
|
||
LruAllocatorConfig createTieredCacheConfig(size_t totalCacheSize, | ||
size_t numTiers = 2) { | ||
LruAllocatorConfig tieredCacheConfig{}; | ||
std::vector<MemoryTierCacheConfig> configs; | ||
for (auto i = 1; i <= numTiers; ++i) { | ||
configs.push_back(MemoryTierCacheConfig::fromShm().setRatio(1).setMemBind( | ||
std::string("0"))); | ||
} | ||
tieredCacheConfig.setCacheSize(totalCacheSize) | ||
.enableCachePersistence( | ||
folly::sformat("/tmp/multi-tier-test/{}", ::getpid())) | ||
.usePosixForShm() | ||
.configureMemoryTiers(configs); | ||
return tieredCacheConfig; | ||
} | ||
|
||
LruAllocatorConfig createDramCacheConfig(size_t totalCacheSize) { | ||
LruAllocatorConfig dramConfig{}; | ||
dramConfig.setCacheSize(totalCacheSize); | ||
return dramConfig; | ||
} | ||
|
||
void validatePoolSize(PoolId poolId, | ||
std::unique_ptr<LruAllocator>& allocator, | ||
size_t expectedSize) { | ||
size_t actualSize = allocator->getPool(poolId).getPoolSize(); | ||
EXPECT_EQ(actualSize, expectedSize); | ||
} | ||
|
||
void testAddPool(std::unique_ptr<LruAllocator>& alloc, | ||
size_t poolSize, | ||
bool isSizeValid = true, | ||
size_t numTiers = 2) { | ||
if (isSizeValid) { | ||
auto pool = alloc->addPool("validPoolSize", poolSize); | ||
EXPECT_LE(alloc->getPool(pool).getPoolSize(), poolSize); | ||
if (poolSize >= numTiers * Slab::kSize) | ||
EXPECT_GE(alloc->getPool(pool).getPoolSize(), | ||
poolSize - numTiers * Slab::kSize); | ||
} else { | ||
EXPECT_THROW(alloc->addPool("invalidPoolSize", poolSize), | ||
std::invalid_argument); | ||
// TODO: test this for all tiers | ||
EXPECT_EQ(alloc->getPoolIds().size(), 0); | ||
} | ||
} | ||
}; | ||
|
||
using LruMemoryTiersTest = MemoryTiersTest<LruAllocator>; | ||
|
||
TEST_F(LruMemoryTiersTest, TestValid1TierConfig) { | ||
LruAllocatorConfig cfg = createTestCacheConfig().validate(); | ||
basicCheck(cfg); | ||
} | ||
|
||
TEST_F(LruMemoryTiersTest, TestValid2TierConfig) { | ||
LruAllocatorConfig cfg = createTestCacheConfig({1, 1}); | ||
basicCheck(cfg); | ||
} | ||
|
||
TEST_F(LruMemoryTiersTest, TestValid2TierRatioConfig) { | ||
LruAllocatorConfig cfg = createTestCacheConfig({5, 2}); | ||
basicCheck(cfg); | ||
} | ||
|
||
TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigNumberOfPartitionsTooLarge) { | ||
EXPECT_THROW(createTestCacheConfig({defaultTotalCacheSize, 1}).validate(), | ||
std::invalid_argument); | ||
} | ||
|
||
TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigSizesAndRatioNotSet) { | ||
EXPECT_THROW(createTestCacheConfig({1, 0}), std::invalid_argument); | ||
} | ||
|
||
TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigRatiosCacheSizeNotSet) { | ||
EXPECT_THROW(createTestCacheConfig({1, 1}, true, | ||
/* cacheSize */ 0) | ||
.validate(), | ||
std::invalid_argument); | ||
} | ||
|
||
TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigRatioNotSet) { | ||
EXPECT_THROW(createTestCacheConfig({1, 0}), std::invalid_argument); | ||
} | ||
|
||
TEST_F(LruMemoryTiersTest, TestInvalid2TierConfigSizesNeCacheSize) { | ||
EXPECT_THROW(createTestCacheConfig({0, 0}), std::invalid_argument); | ||
} | ||
} // namespace tests | ||
} // namespace cachelib | ||
} // namespace facebook |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit