Skip to content

Commit caf3d60

Browse files
committed
Adds createPutToken and switches findEviction
to utilize combined locking.
1 parent e3f4a3c commit caf3d60

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

cachelib/allocator/CacheAllocator.h

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,11 +1481,11 @@ class CacheAllocator : public CacheBase {
14811481
// Given an existing item, allocate a new one for the
14821482
// existing one to later be moved into.
14831483
//
1484-
// @param oldItem the item we want to allocate a new item for
1484+
// @param item reference to the item we want to allocate a new item for
14851485
//
14861486
// @return handle to the newly allocated item
14871487
//
1488-
WriteHandle allocateNewItemForOldItem(const Item& oldItem);
1488+
WriteHandle allocateNewItemForOldItem(const Item& item);
14891489

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

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

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

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

19971993
// Execute func on each item. `func` can throw exception but must ensure
@@ -3663,12 +3659,9 @@ CacheAllocator<CacheTrait>::getNextCandidate(PoolId pid,
36633659
? &toRecycle_->asChainedItem().getParentItem(compressor_)
36643660
: toRecycle_;
36653661

3666-
const bool evictToNvmCache = shouldWriteToNvmCache(*candidate_);
3667-
auto putToken = evictToNvmCache
3668-
? nvmCache_->createPutToken(candidate_->getKey())
3669-
: typename NvmCacheT::PutToken{};
3662+
auto putToken = createPutToken(*candidate_);
36703663

3671-
if (evictToNvmCache && !putToken.isValid()) {
3664+
if (shouldWriteToNvmCache(*candidate_) && !putToken.isValid()) {
36723665
stats_.evictFailConcurrentFill.inc();
36733666
++itr;
36743667
continue;
@@ -4291,13 +4284,13 @@ std::vector<std::string> CacheAllocator<CacheTrait>::dumpEvictionIterator(
42914284
std::vector<std::string> content;
42924285

42934286
auto& mm = *mmContainers_[pid][cid];
4294-
auto evictItr = mm.getEvictionIterator();
4295-
size_t i = 0;
4296-
while (evictItr && i < numItems) {
4297-
content.push_back(evictItr->toString());
4298-
++evictItr;
4299-
++i;
4300-
}
4287+
4288+
mm.withEvictionIterator([&content, numItems](auto&& itr) {
4289+
while (itr && content.size() < numItems) {
4290+
content.push_back(itr->toString());
4291+
++itr;
4292+
}
4293+
});
43014294

43024295
return content;
43034296
}
@@ -4942,6 +4935,7 @@ bool CacheAllocator<CacheTrait>::moveForSlabRelease(Item& oldItem) {
49424935
template <typename CacheTrait>
49434936
typename CacheAllocator<CacheTrait>::WriteHandle
49444937
CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
4938+
XDCHECK(oldItem.isMoving());
49454939
if (oldItem.isChainedItem()) {
49464940
const Item& parentItem = oldItem.asChainedItem().getParentItem(compressor_);
49474941

@@ -4955,7 +4949,7 @@ CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
49554949
XDCHECK_EQ(newItemHdl->getSize(), oldChainedItem.getSize());
49564950
XDCHECK_EQ(reinterpret_cast<uintptr_t>(&parentItem),
49574951
reinterpret_cast<uintptr_t>(
4958-
&oldChainedItem.getParentItem(compressor_)));
4952+
&newItemHdl->asChainedItem().getParentItem(compressor_)));
49594953

49604954
return newItemHdl;
49614955
}

cachelib/allocator/MM2Q.h

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

6868
// Config class for MM2Q
69+
// TODO: implement support for useCombinedLockForIterators
6970
struct Config {
7071
// Create from serialized config
7172
explicit Config(SerializationConfigType configState)

cachelib/allocator/tests/BaseAllocatorTest.h

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

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

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

54885493
alloc.remove("hello");
54895494
}
@@ -5497,8 +5502,11 @@ class BaseAllocatorTest : public AllocatorTest<AllocatorT> {
54975502
ASSERT_TRUE(small2->isInMMContainer());
54985503

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

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

0 commit comments

Comments
 (0)