-
Notifications
You must be signed in to change notification settings - Fork 313
First set of changes to cache configuration API to enable multi-tier caches #138
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dcca025
These changes introduce per-tier cache configuration required to impl…
victoria-mcgrath 8b9f1f3
Changed ratio parameter from double to size_t, added newline at the e…
victoria-mcgrath c83bff4
Added examples of types of memory where a cache tier can be created
victoria-mcgrath 808ab4e
Clarified the meaning of total cache size as a sum of all tier sizes;…
victoria-mcgrath d86302e
Deleted method that reset total cache size when configuring tiers
victoria-mcgrath 8037f6d
A typo in setCacheSize?
jiayuebao 54fa491
Fixed typo
victoria-mcgrath 49c72f2
Corrected one of the new tests
victoria-mcgrath 43a44d6
Added setting default size for default tier to match default cache si…
victoria-mcgrath 441c35a
Disabled memory tier size setting
victoria-mcgrath f942a49
Addressed reviewer's comments
victoria-mcgrath 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "cachelib/shm/ShmCommon.h" | ||
|
|
||
| namespace facebook { | ||
| namespace cachelib { | ||
| class MemoryTierCacheConfig { | ||
| public: | ||
| // Creates instance of MemoryTierCacheConfig for Posix/SysV Shared memory. | ||
| static MemoryTierCacheConfig fromShm() { | ||
| // TODO: expand this method when adding support for file-mapped memory | ||
| return MemoryTierCacheConfig(); | ||
| } | ||
|
|
||
| // Specifies ratio of this memory tier to other tiers. Absolute size | ||
| // of each tier can be calculated as: | ||
| // cacheSize * tierRatio / Sum of ratios for all tiers. | ||
| MemoryTierCacheConfig& setRatio(size_t _ratio) { | ||
| if (!_ratio) { | ||
| throw std::invalid_argument("Tier ratio must be an integer number >=1."); | ||
| } | ||
| ratio = _ratio; | ||
| return *this; | ||
| } | ||
|
|
||
| size_t getRatio() const noexcept { return ratio; } | ||
|
|
||
| size_t calculateTierSize(size_t totalCacheSize, size_t partitionNum) { | ||
| // TODO: Call this method when tiers are enabled in allocator | ||
| // to calculate tier sizes in bytes. | ||
| if (!partitionNum) { | ||
| throw std::invalid_argument( | ||
| "The total number of tier ratios must be an integer number >=1."); | ||
| } | ||
|
|
||
| size_t partitionSize = 0; | ||
| if (partitionNum > totalCacheSize) { | ||
| throw std::invalid_argument( | ||
| "Ratio must be less or equal to total cache size."); | ||
| } | ||
|
|
||
| return getRatio() * (totalCacheSize / partitionNum); | ||
| } | ||
|
|
||
| // Ratio is a number of parts of the total cache size to be allocated for this | ||
| // tier. E.g. if X is a total cache size, Yi are ratios specified for memory | ||
| // tiers, and Y is the sum of all Yi, then size of the i-th tier | ||
| // Xi = (X / Y) * Yi. For examle, to configure 2-tier cache where each | ||
| // tier is a half of the total cache size, set both tiers' ratios to 1. | ||
| size_t ratio{1}; | ||
|
|
||
| private: | ||
| // TODO: introduce a container for tier settings when adding support for | ||
| // file-mapped memory | ||
| MemoryTierCacheConfig() = default; | ||
| }; | ||
| } // 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #include "cachelib/allocator/CacheAllocatorConfig.h" | ||
| #include "cachelib/allocator/MemoryTierCacheConfig.h" | ||
| #include "cachelib/allocator/tests/TestBase.h" | ||
|
|
||
| namespace facebook { | ||
| namespace cachelib { | ||
|
|
||
| namespace tests { | ||
|
|
||
| using AllocatorT = LruAllocator; | ||
| using MemoryTierConfigs = CacheAllocatorConfig<AllocatorT>::MemoryTierConfigs; | ||
|
|
||
| size_t defaultTotalSize = 1 * 1024LL * 1024LL * 1024LL; | ||
|
|
||
| class CacheAllocatorConfigTest : public testing::Test {}; | ||
|
|
||
| MemoryTierConfigs generateTierConfigs(size_t numTiers, | ||
| MemoryTierCacheConfig& config) { | ||
| return MemoryTierConfigs(numTiers, config); | ||
| } | ||
|
|
||
| TEST_F(CacheAllocatorConfigTest, MultipleTier0Config) { | ||
| AllocatorT::Config config; | ||
| // Throws if vector of tier configs is emptry | ||
| EXPECT_THROW(config.configureMemoryTiers(MemoryTierConfigs()), | ||
| std::invalid_argument); | ||
| } | ||
|
|
||
| TEST_F(CacheAllocatorConfigTest, MultipleTier1Config) { | ||
| AllocatorT::Config config; | ||
| // Accepts single-tier configuration | ||
| config.setCacheSize(defaultTotalSize) | ||
| .configureMemoryTiers({MemoryTierCacheConfig::fromShm().setRatio(1)}); | ||
victoria-mcgrath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| config.validateMemoryTiers(); | ||
| } | ||
|
|
||
| TEST_F(CacheAllocatorConfigTest, InvalidTierRatios) { | ||
| AllocatorT::Config config; | ||
| EXPECT_THROW(config.configureMemoryTiers(generateTierConfigs( | ||
| config.maxCacheMemoryTiers + 1, | ||
| MemoryTierCacheConfig::fromShm().setRatio(0))), | ||
| std::invalid_argument); | ||
| } | ||
|
|
||
| TEST_F(CacheAllocatorConfigTest, TotalCacheSizeLessThanRatios) { | ||
| AllocatorT::Config config; | ||
| // Throws if total cache size is set to 0 | ||
| config.setCacheSize(defaultTotalSize) | ||
| .configureMemoryTiers( | ||
| {MemoryTierCacheConfig::fromShm().setRatio(defaultTotalSize + 1)}); | ||
| EXPECT_THROW(config.validate(), 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.
Uh oh!
There was an error while loading. Please reload this page.