@@ -424,7 +424,8 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
424
424
uint32_t size,
425
425
uint32_t creationTime,
426
426
uint32_t expiryTime,
427
- bool fromBgThread) {
427
+ bool fromBgThread,
428
+ bool evict) {
428
429
util::LatencyTracker tracker{stats ().allocateLatency_ };
429
430
430
431
SCOPE_FAIL { stats_.invalidAllocs .inc (); };
@@ -445,7 +446,9 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
445
446
backgroundEvictor_[backgroundWorkerId (tid, pid, cid, backgroundEvictor_.size ())]->wakeUp ();
446
447
}
447
448
448
- if (memory == nullptr ) {
449
+ if (memory == nullptr && !evict) {
450
+ return {};
451
+ } else if (memory == nullptr ) {
449
452
memory = findEviction (tid, pid, cid);
450
453
}
451
454
@@ -495,7 +498,8 @@ CacheAllocator<CacheTrait>::allocateInternal(PoolId pid,
495
498
bool fromBgThread) {
496
499
auto tid = 0 ; /* TODO: consult admission policy */
497
500
for (TierId tid = 0 ; tid < getNumTiers (); ++tid) {
498
- auto handle = allocateInternalTier (tid, pid, key, size, creationTime, expiryTime, fromBgThread);
501
+ bool evict = !config_.insertToFirstFreeTier || tid == getNumTiers () - 1 ;
502
+ auto handle = allocateInternalTier (tid, pid, key, size, creationTime, expiryTime, fromBgThread, evict);
499
503
if (handle) return handle;
500
504
}
501
505
return {};
@@ -1829,13 +1833,17 @@ CacheAllocator<CacheTrait>::tryEvictToNextMemoryTier(
1829
1833
1830
1834
TierId nextTier = tid; // TODO - calculate this based on some admission policy
1831
1835
while (++nextTier < getNumTiers ()) { // try to evict down to the next memory tiers
1836
+ // always evict item from the nextTier to make room for new item
1837
+ bool evict = true ;
1838
+
1832
1839
// allocateInternal might trigger another eviction
1833
1840
auto newItemHdl = allocateInternalTier (nextTier, pid,
1834
1841
item.getKey (),
1835
1842
item.getSize (),
1836
1843
item.getCreationTime (),
1837
1844
item.getExpiryTime (),
1838
- fromBgThread);
1845
+ fromBgThread,
1846
+ evict);
1839
1847
1840
1848
if (newItemHdl) {
1841
1849
XDCHECK_EQ (newItemHdl->getSize (), item.getSize ());
@@ -1871,13 +1879,17 @@ CacheAllocator<CacheTrait>::tryPromoteToNextMemoryTier(
1871
1879
auto toPromoteTier = nextTier - 1 ;
1872
1880
--nextTier;
1873
1881
1882
+ // always evict item from the toPromoteTier to make room for new item
1883
+ bool evict = true ;
1884
+
1874
1885
// allocateInternal might trigger another eviction
1875
1886
auto newItemHdl = allocateInternalTier (toPromoteTier, pid,
1876
1887
item.getKey (),
1877
1888
item.getSize (),
1878
1889
item.getCreationTime (),
1879
1890
item.getExpiryTime (),
1880
- fromBgThread);
1891
+ fromBgThread,
1892
+ true );
1881
1893
1882
1894
if (newItemHdl) {
1883
1895
XDCHECK_EQ (newItemHdl->getSize (), item.getSize ());
@@ -3251,6 +3263,8 @@ CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
3251
3263
3252
3264
const auto allocInfo =
3253
3265
allocator_[getTierId (oldItem)]->getAllocInfo (static_cast <const void *>(&oldItem));
3266
+
3267
+ bool evict = !config_.insertToFirstFreeTier || getTierId (oldItem) == getNumTiers () - 1 ;
3254
3268
3255
3269
// Set up the destination for the move. Since oldItem would have the moving
3256
3270
// bit set, it won't be picked for eviction.
@@ -3260,7 +3274,8 @@ CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
3260
3274
oldItem.getSize (),
3261
3275
oldItem.getCreationTime (),
3262
3276
oldItem.getExpiryTime (),
3263
- false );
3277
+ false ,
3278
+ evict);
3264
3279
if (!newItemHdl) {
3265
3280
return {};
3266
3281
}
0 commit comments