Skip to content

Commit 7b01549

Browse files
byrnedjvinser52
authored andcommitted
Adds createPutToken and switches findEviction
to utilize combined locking.
1 parent 4a95c4b commit 7b01549

File tree

4 files changed

+36
-33
lines changed

4 files changed

+36
-33
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,12 +1344,9 @@ CacheAllocator<CacheTrait>::getNextCandidate(PoolId pid,
13441344
? &toRecycle_->asChainedItem().getParentItem(compressor_)
13451345
: toRecycle_;
13461346

1347-
const bool evictToNvmCache = shouldWriteToNvmCache(*candidate_);
1348-
auto putToken = evictToNvmCache
1349-
? nvmCache_->createPutToken(candidate_->getKey())
1350-
: typename NvmCacheT::PutToken{};
1347+
auto putToken = createPutToken(*candidate_);
13511348

1352-
if (evictToNvmCache && !putToken.isValid()) {
1349+
if (shouldWriteToNvmCache(*candidate_) && !putToken.isValid()) {
13531350
stats_.evictFailConcurrentFill.inc();
13541351
++itr;
13551352
continue;
@@ -1972,13 +1969,13 @@ std::vector<std::string> CacheAllocator<CacheTrait>::dumpEvictionIterator(
19721969
std::vector<std::string> content;
19731970

19741971
auto& mm = *mmContainers_[pid][cid];
1975-
auto evictItr = mm.getEvictionIterator();
1976-
size_t i = 0;
1977-
while (evictItr && i < numItems) {
1978-
content.push_back(evictItr->toString());
1979-
++evictItr;
1980-
++i;
1981-
}
1972+
1973+
mm.withEvictionIterator([&content, numItems](auto&& itr) {
1974+
while (itr && content.size() < numItems) {
1975+
content.push_back(itr->toString());
1976+
++itr;
1977+
}
1978+
});
19821979

19831980
return content;
19841981
}
@@ -2619,6 +2616,7 @@ bool CacheAllocator<CacheTrait>::moveForSlabRelease(Item& oldItem) {
26192616
template <typename CacheTrait>
26202617
typename CacheAllocator<CacheTrait>::WriteHandle
26212618
CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
2619+
XDCHECK(oldItem.isMoving());
26222620
if (oldItem.isChainedItem()) {
26232621
const Item& parentItem = oldItem.asChainedItem().getParentItem(compressor_);
26242622

@@ -2632,7 +2630,7 @@ CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
26322630
XDCHECK_EQ(newItemHdl->getSize(), oldChainedItem.getSize());
26332631
XDCHECK_EQ(reinterpret_cast<uintptr_t>(&parentItem),
26342632
reinterpret_cast<uintptr_t>(
2635-
&oldChainedItem.getParentItem(compressor_)));
2633+
&newItemHdl->asChainedItem().getParentItem(compressor_)));
26362634

26372635
return newItemHdl;
26382636
}

cachelib/allocator/CacheAllocator.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,11 +1482,11 @@ class CacheAllocator : public CacheBase {
14821482
// Given an existing item, allocate a new one for the
14831483
// existing one to later be moved into.
14841484
//
1485-
// @param oldItem the item we want to allocate a new item for
1485+
// @param item reference to the item we want to allocate a new item for
14861486
//
14871487
// @return handle to the newly allocated item
14881488
//
1489-
WriteHandle allocateNewItemForOldItem(const Item& oldItem);
1489+
WriteHandle allocateNewItemForOldItem(const Item& item);
14901490

14911491
// internal helper that grabs a refcounted handle to the item. This does
14921492
// not record the access to reflect in the mmContainer.
@@ -1545,7 +1545,7 @@ class CacheAllocator : public CacheBase {
15451545
// callback is responsible for copying the contents and fixing the semantics
15461546
// of chained item.
15471547
//
1548-
// @param oldItem Reference to the item being moved
1548+
// @param oldItem item being moved
15491549
// @param newItemHdl Reference to the handle of the new item being moved into
15501550
//
15511551
// @return true If the move was completed, and the containers were updated
@@ -1981,18 +1981,14 @@ class CacheAllocator : public CacheBase {
19811981
std::optional<bool> saveNvmCache();
19821982
void saveRamCache();
19831983

1984-
static bool itemExclusivePredicate(const Item& item) {
1985-
return item.getRefCount() == 0;
1984+
static bool itemSlabMovePredicate(const Item& item) {
1985+
return item.isMoving() && item.getRefCount() == 0;
19861986
}
19871987

19881988
static bool itemExpiryPredicate(const Item& item) {
19891989
return item.getRefCount() == 1 && item.isExpired();
19901990
}
19911991

1992-
static bool parentEvictForSlabReleasePredicate(const Item& item) {
1993-
return item.getRefCount() == 1 && !item.isMoving();
1994-
}
1995-
19961992
std::unique_ptr<Deserializer> createDeserializer();
19971993

19981994
// Execute func on each item. `func` can throw exception but must ensure

cachelib/allocator/MM2Q.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class MM2Q {
6868
enum LruType { Warm, WarmTail, Hot, Cold, ColdTail, NumTypes };
6969

7070
// Config class for MM2Q
71+
// TODO: implement support for useCombinedLockForIterators
7172
struct Config {
7273
// Create from serialized config
7374
explicit Config(SerializationConfigType configState)

cachelib/allocator/tests/BaseAllocatorTest.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4183,15 +4183,16 @@ class BaseAllocatorTest : public AllocatorTest<AllocatorT> {
41834183
// Check that item is in the expected container.
41844184
bool findItem(AllocatorT& allocator, typename AllocatorT::Item* item) {
41854185
auto& container = allocator.getMMContainer(*item);
4186-
auto itr = container.getEvictionIterator();
41874186
bool found = false;
4188-
while (itr) {
4189-
if (itr.get() == item) {
4190-
found = true;
4191-
break;
4187+
container.withEvictionIterator([&found, &item](auto&& itr) {
4188+
while (itr) {
4189+
if (itr.get() == item) {
4190+
found = true;
4191+
break;
4192+
}
4193+
++itr;
41924194
}
4193-
++itr;
4194-
}
4195+
});
41954196
return found;
41964197
}
41974198

@@ -5483,8 +5484,12 @@ class BaseAllocatorTest : public AllocatorTest<AllocatorT> {
54835484
ASSERT_TRUE(big->isInMMContainer());
54845485

54855486
auto& mmContainer = alloc.getMMContainer(*big);
5486-
auto itr = mmContainer.getEvictionIterator();
5487-
ASSERT_EQ(big.get(), &(*itr));
5487+
5488+
typename AllocatorT::Item* evictionCandidate = nullptr;
5489+
mmContainer.withEvictionIterator(
5490+
[&evictionCandidate](auto&& itr) { evictionCandidate = itr.get(); });
5491+
5492+
ASSERT_EQ(big.get(), evictionCandidate);
54885493

54895494
alloc.remove("hello");
54905495
}
@@ -5498,8 +5503,11 @@ class BaseAllocatorTest : public AllocatorTest<AllocatorT> {
54985503
ASSERT_TRUE(small2->isInMMContainer());
54995504

55005505
auto& mmContainer = alloc.getMMContainer(*small2);
5501-
auto itr = mmContainer.getEvictionIterator();
5502-
ASSERT_EQ(small2.get(), &(*itr));
5506+
5507+
typename AllocatorT::Item* evictionCandidate = nullptr;
5508+
mmContainer.withEvictionIterator(
5509+
[&evictionCandidate](auto&& itr) { evictionCandidate = itr.get(); });
5510+
ASSERT_EQ(small2.get(), evictionCandidate);
55035511

55045512
alloc.remove("hello");
55055513
}

0 commit comments

Comments
 (0)