Skip to content

Commit 9d1cfc7

Browse files
igchorvinser52
authored andcommitted
Add option to insert items to first free tier (#87)
instead of always inserting to topmost tier
1 parent a288dd1 commit 9d1cfc7

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
415415
uint32_t size,
416416
uint32_t creationTime,
417417
uint32_t expiryTime,
418-
bool fromBgThread) {
418+
bool fromBgThread,
419+
bool evict) {
419420
util::LatencyTracker tracker{stats().allocateLatency_};
420421

421422
SCOPE_FAIL { stats_.invalidAllocs.inc(); };
@@ -440,6 +441,9 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
440441
}
441442

442443
if (memory == nullptr) {
444+
if (!evict) {
445+
return {};
446+
}
443447
memory = findEviction(tid, pid, cid);
444448
}
445449

@@ -489,7 +493,8 @@ CacheAllocator<CacheTrait>::allocateInternal(PoolId pid,
489493
bool fromBgThread) {
490494
auto tid = 0; /* TODO: consult admission policy */
491495
for(TierId tid = 0; tid < getNumTiers(); ++tid) {
492-
auto handle = allocateInternalTier(tid, pid, key, size, creationTime, expiryTime, fromBgThread);
496+
bool evict = !config_.insertToFirstFreeTier || tid == getNumTiers() - 1;
497+
auto handle = allocateInternalTier(tid, pid, key, size, creationTime, expiryTime, fromBgThread, evict);
493498
if (handle) return handle;
494499
}
495500
return {};
@@ -1650,13 +1655,17 @@ CacheAllocator<CacheTrait>::tryEvictToNextMemoryTier(
16501655

16511656
TierId nextTier = tid; // TODO - calculate this based on some admission policy
16521657
while (++nextTier < getNumTiers()) { // try to evict down to the next memory tiers
1658+
// always evict item from the nextTier to make room for new item
1659+
bool evict = true;
1660+
16531661
// allocateInternal might trigger another eviction
16541662
auto newItemHdl = allocateInternalTier(nextTier, pid,
16551663
item.getKey(),
16561664
item.getSize(),
16571665
item.getCreationTime(),
16581666
item.getExpiryTime(),
1659-
fromBgThread);
1667+
fromBgThread,
1668+
evict);
16601669

16611670
if (newItemHdl) {
16621671

@@ -1693,13 +1702,17 @@ CacheAllocator<CacheTrait>::tryPromoteToNextMemoryTier(
16931702
auto toPromoteTier = nextTier - 1;
16941703
--nextTier;
16951704

1705+
// always evict item from the toPromoteTier to make room for new item
1706+
bool evict = true;
1707+
16961708
// allocateInternal might trigger another eviction
16971709
auto newItemHdl = allocateInternalTier(toPromoteTier, pid,
16981710
item.getKey(),
16991711
item.getSize(),
17001712
item.getCreationTime(),
17011713
item.getExpiryTime(),
1702-
fromBgThread);
1714+
fromBgThread,
1715+
true);
17031716

17041717
if (newItemHdl) {
17051718
XDCHECK_EQ(newItemHdl->getSize(), item.getSize());
@@ -3038,6 +3051,8 @@ CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
30383051

30393052
const auto allocInfo =
30403053
allocator_[getTierId(oldItem)]->getAllocInfo(static_cast<const void*>(&oldItem));
3054+
3055+
bool evict = !config_.insertToFirstFreeTier || getTierId(oldItem) == getNumTiers() - 1;
30413056

30423057
// Set up the destination for the move. Since oldItem would have the moving
30433058
// bit set, it won't be picked for eviction.
@@ -3047,7 +3062,8 @@ CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
30473062
oldItem.getSize(),
30483063
oldItem.getCreationTime(),
30493064
oldItem.getExpiryTime(),
3050-
false);
3065+
false,
3066+
evict);
30513067
if (!newItemHdl) {
30523068
return {};
30533069
}

cachelib/allocator/CacheAllocator.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1535,13 +1535,19 @@ class CacheAllocator : public CacheBase {
15351535
// For description see allocateInternal.
15361536
//
15371537
// @param tid id a memory tier
1538+
// @param fromBgThread whether this function was called from a bg
1539+
// thread - this is used to decide whether bg thread should
1540+
// be waken in case there is no free memory
1541+
// @param evict whether to evict an item from tier tid in case there
1542+
// is not enough memory
15381543
WriteHandle allocateInternalTier(TierId tid,
15391544
PoolId id,
15401545
Key key,
15411546
uint32_t size,
15421547
uint32_t creationTime,
15431548
uint32_t expiryTime,
1544-
bool fromBgThread);
1549+
bool fromBgThread,
1550+
bool evict);
15451551

15461552
// Allocate a chained item
15471553
//

cachelib/allocator/CacheAllocatorConfig.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ class CacheAllocatorConfig {
313313
// Library team if you find yourself customizing this.
314314
CacheAllocatorConfig& setThrottlerConfig(util::Throttler::Config config);
315315

316+
// Insert items to first free memory tier
317+
CacheAllocatorConfig& enableInsertToFirstFreeTier();
318+
316319
// Passes in a callback to initialize an event tracker when the allocator
317320
// starts
318321
CacheAllocatorConfig& setEventTracker(EventTrackerSharedPtr&&);
@@ -539,6 +542,11 @@ class CacheAllocatorConfig {
539542
// ABOVE are the config for various cache workers
540543
//
541544

545+
// if turned off, always insert new elements to topmost memory tier.
546+
// if turned on, insert new element to first free memory tier or evict memory
547+
// from the bottom one if memory cache is full
548+
bool insertToFirstFreeTier = false;
549+
542550
// the number of tries to search for an item to evict
543551
// 0 means it's infinite
544552
unsigned int evictionSearchTries{50};
@@ -673,6 +681,12 @@ class CacheAllocatorConfig {
673681
{MemoryTierCacheConfig::fromShm().setRatio(1)}};
674682
};
675683

684+
template <typename T>
685+
CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::enableInsertToFirstFreeTier() {
686+
insertToFirstFreeTier = true;
687+
return *this;
688+
}
689+
676690
template <typename T>
677691
CacheAllocatorConfig<T>& CacheAllocatorConfig<T>::setCacheName(
678692
const std::string& _cacheName) {
@@ -1254,6 +1268,7 @@ std::map<std::string, std::string> CacheAllocatorConfig<T>::serialize() const {
12541268
configMap["nvmAdmissionMinTTL"] = std::to_string(nvmAdmissionMinTTL);
12551269
configMap["delayCacheWorkersStart"] =
12561270
delayCacheWorkersStart ? "true" : "false";
1271+
configMap["insertToFirstFreeTier"] = std::to_string(insertToFirstFreeTier);
12571272
mergeWithPrefix(configMap, throttleConfig.serialize(), "throttleConfig");
12581273
mergeWithPrefix(configMap,
12591274
chainedItemAccessConfig.serialize(),

cachelib/cachebench/cache/Cache-inl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ Cache<Allocator>::Cache(const CacheConfig& config,
104104
allocatorConfig_.configureMemoryTiers(config_.memoryTierConfigs);
105105
}
106106

107+
allocatorConfig_.insertToFirstFreeTier = config_.insertToFirstFreeTier;
108+
107109
auto cleanupGuard = folly::makeGuard([&] {
108110
if (!nvmCacheFilePath_.empty()) {
109111
util::removePath(nvmCacheFilePath_);

cachelib/cachebench/util/CacheConfig.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ CacheConfig::CacheConfig(const folly::dynamic& configJson) {
4949
JSONSetVal(configJson, tryLockUpdate);
5050
JSONSetVal(configJson, lruIpSpec);
5151
JSONSetVal(configJson, useCombinedLockForIterators);
52+
53+
JSONSetVal(configJson, insertToFirstFreeTier);
5254

5355
JSONSetVal(configJson, lru2qHotPct);
5456
JSONSetVal(configJson, lru2qColdPct);

cachelib/cachebench/util/CacheConfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ struct CacheConfig : public JSONConfig {
9797
bool lruUpdateOnRead{true};
9898
bool tryLockUpdate{false};
9999
bool useCombinedLockForIterators{true};
100+
101+
bool insertToFirstFreeTier{false};
100102

101103
// LRU param
102104
uint64_t lruIpSpec{0};

0 commit comments

Comments
 (0)