@@ -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 }
0 commit comments