@@ -1528,13 +1528,19 @@ class CacheAllocator : public CacheBase {
15281528 // For description see allocateInternal.
15291529 //
15301530 // @param tid id a memory tier
1531+ // @param fromBgThread whether this function was called from a bg
1532+ // thread - this is used to decide whether bg thread should
1533+ // be waken in case there is no free memory
1534+ // @param evict whether to evict an item from tier tid in case there
1535+ // is not enough memory
15311536 WriteHandle allocateInternalTier (TierId tid,
15321537 PoolId id,
15331538 Key key,
15341539 uint32_t size,
15351540 uint32_t creationTime,
15361541 uint32_t expiryTime,
1537- bool fromBgThread);
1542+ bool fromBgThread,
1543+ bool evict);
15381544
15391545 // Allocate a chained item
15401546 //
@@ -2977,7 +2983,8 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
29772983 uint32_t size,
29782984 uint32_t creationTime,
29792985 uint32_t expiryTime,
2980- bool fromBgThread) {
2986+ bool fromBgThread,
2987+ bool evict) {
29812988 util::LatencyTracker tracker{stats ().allocateLatency_ };
29822989
29832990 SCOPE_FAIL { stats_.invalidAllocs .inc (); };
@@ -3002,6 +3009,9 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
30023009 }
30033010
30043011 if (memory == nullptr ) {
3012+ if (!evict) {
3013+ return {};
3014+ }
30053015 memory = findEviction (tid, pid, cid);
30063016 }
30073017
@@ -3051,7 +3061,9 @@ CacheAllocator<CacheTrait>::allocateInternal(PoolId pid,
30513061 bool fromBgThread) {
30523062 auto tid = 0 ; /* TODO: consult admission policy */
30533063 for (TierId tid = 0 ; tid < getNumTiers (); ++tid) {
3054- auto handle = allocateInternalTier (tid, pid, key, size, creationTime, expiryTime, fromBgThread);
3064+ bool evict = !config_.insertToFirstFreeTier || tid == getNumTiers () - 1 ;
3065+ auto handle = allocateInternalTier (tid, pid, key, size, creationTime,
3066+ expiryTime, fromBgThread, evict);
30553067 if (handle) return handle;
30563068 }
30573069 return {};
@@ -4220,13 +4232,16 @@ CacheAllocator<CacheTrait>::tryEvictToNextMemoryTier(
42204232
42214233 TierId nextTier = tid; // TODO - calculate this based on some admission policy
42224234 while (++nextTier < getNumTiers ()) { // try to evict down to the next memory tiers
4235+ // always evict item from the nextTier to make room for new item
4236+ bool evict = true ;
42234237 // allocateInternal might trigger another eviction
42244238 auto newItemHdl = allocateInternalTier (nextTier, pid,
42254239 item.getKey (),
42264240 item.getSize (),
42274241 item.getCreationTime (),
42284242 item.getExpiryTime (),
4229- fromBgThread);
4243+ fromBgThread,
4244+ evict);
42304245
42314246 if (newItemHdl) {
42324247
@@ -4263,13 +4278,16 @@ CacheAllocator<CacheTrait>::tryPromoteToNextMemoryTier(
42634278 auto toPromoteTier = nextTier - 1 ;
42644279 --nextTier;
42654280
4281+ // always evict item from the toPromoteTier to make room for new item
4282+ bool evict = true ;
42664283 // allocateInternal might trigger another eviction
42674284 auto newItemHdl = allocateInternalTier (toPromoteTier, pid,
42684285 item.getKey (),
42694286 item.getSize (),
42704287 item.getCreationTime (),
42714288 item.getExpiryTime (),
4272- fromBgThread);
4289+ fromBgThread,
4290+ true );
42734291
42744292 if (newItemHdl) {
42754293 XDCHECK_EQ (newItemHdl->getSize (), item.getSize ());
@@ -5611,6 +5629,7 @@ CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
56115629 const auto tid = getTierId (oldItem);
56125630 const auto allocInfo =
56135631 allocator_[tid]->getAllocInfo (static_cast <const void *>(&oldItem));
5632+ bool evict = !config_.insertToFirstFreeTier || tid == getNumTiers () - 1 ;
56145633
56155634 // Set up the destination for the move. Since oldItem would have the moving
56165635 // bit set, it won't be picked for eviction.
@@ -5620,7 +5639,8 @@ CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
56205639 oldItem.getSize (),
56215640 oldItem.getCreationTime (),
56225641 oldItem.getExpiryTime (),
5623- false );
5642+ false ,
5643+ evict);
56245644 if (!newItemHdl) {
56255645 return {};
56265646 }
0 commit comments