@@ -1528,13 +1528,19 @@ class CacheAllocator : public CacheBase {
1528
1528
// For description see allocateInternal.
1529
1529
//
1530
1530
// @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
1531
1536
WriteHandle allocateInternalTier (TierId tid,
1532
1537
PoolId id,
1533
1538
Key key,
1534
1539
uint32_t size,
1535
1540
uint32_t creationTime,
1536
1541
uint32_t expiryTime,
1537
- bool fromBgThread);
1542
+ bool fromBgThread,
1543
+ bool evict);
1538
1544
1539
1545
// Allocate a chained item
1540
1546
//
@@ -2977,7 +2983,8 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
2977
2983
uint32_t size,
2978
2984
uint32_t creationTime,
2979
2985
uint32_t expiryTime,
2980
- bool fromBgThread) {
2986
+ bool fromBgThread,
2987
+ bool evict) {
2981
2988
util::LatencyTracker tracker{stats ().allocateLatency_ };
2982
2989
2983
2990
SCOPE_FAIL { stats_.invalidAllocs .inc (); };
@@ -3002,6 +3009,9 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
3002
3009
}
3003
3010
3004
3011
if (memory == nullptr ) {
3012
+ if (!evict) {
3013
+ return {};
3014
+ }
3005
3015
memory = findEviction (tid, pid, cid);
3006
3016
}
3007
3017
@@ -3051,7 +3061,9 @@ CacheAllocator<CacheTrait>::allocateInternal(PoolId pid,
3051
3061
bool fromBgThread) {
3052
3062
auto tid = 0 ; /* TODO: consult admission policy */
3053
3063
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);
3055
3067
if (handle) return handle;
3056
3068
}
3057
3069
return {};
@@ -4220,13 +4232,16 @@ CacheAllocator<CacheTrait>::tryEvictToNextMemoryTier(
4220
4232
4221
4233
TierId nextTier = tid; // TODO - calculate this based on some admission policy
4222
4234
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 ;
4223
4237
// allocateInternal might trigger another eviction
4224
4238
auto newItemHdl = allocateInternalTier (nextTier, pid,
4225
4239
item.getKey (),
4226
4240
item.getSize (),
4227
4241
item.getCreationTime (),
4228
4242
item.getExpiryTime (),
4229
- fromBgThread);
4243
+ fromBgThread,
4244
+ evict);
4230
4245
4231
4246
if (newItemHdl) {
4232
4247
@@ -4263,13 +4278,16 @@ CacheAllocator<CacheTrait>::tryPromoteToNextMemoryTier(
4263
4278
auto toPromoteTier = nextTier - 1 ;
4264
4279
--nextTier;
4265
4280
4281
+ // always evict item from the toPromoteTier to make room for new item
4282
+ bool evict = true ;
4266
4283
// allocateInternal might trigger another eviction
4267
4284
auto newItemHdl = allocateInternalTier (toPromoteTier, pid,
4268
4285
item.getKey (),
4269
4286
item.getSize (),
4270
4287
item.getCreationTime (),
4271
4288
item.getExpiryTime (),
4272
- fromBgThread);
4289
+ fromBgThread,
4290
+ true );
4273
4291
4274
4292
if (newItemHdl) {
4275
4293
XDCHECK_EQ (newItemHdl->getSize (), item.getSize ());
@@ -5608,6 +5626,7 @@ CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
5608
5626
const auto tid = getTierId (oldItem);
5609
5627
const auto allocInfo =
5610
5628
allocator_[tid]->getAllocInfo (static_cast <const void *>(&oldItem));
5629
+ bool evict = !config_.insertToFirstFreeTier || tid == getNumTiers () - 1 ;
5611
5630
5612
5631
// Set up the destination for the move. Since oldItem would have the moving
5613
5632
// bit set, it won't be picked for eviction.
@@ -5617,7 +5636,8 @@ CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
5617
5636
oldItem.getSize (),
5618
5637
oldItem.getCreationTime (),
5619
5638
oldItem.getExpiryTime (),
5620
- false );
5639
+ false ,
5640
+ evict);
5621
5641
if (!newItemHdl) {
5622
5642
return {};
5623
5643
}
0 commit comments