Skip to content

Commit ccd35ab

Browse files
committed
Hide extra ref when item is marked moving
and just return the actual ref count
1 parent 2ffdc5b commit ccd35ab

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

cachelib/allocator/CacheAllocator-inl.h

+4-13
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,7 @@ void CacheAllocator<CacheTrait>::addChainedItem(WriteHandle& parent,
463463
// Parent will decrement the refcount upon release. Since this is an
464464
// internal refcount, we dont include it in active handle tracking.
465465
child->incRef();
466-
auto ref = child->getRefCount();
467-
// ref == 3 if child is moving
468-
XDCHECK(ref == 2u || ref == 3);
466+
XDCHECK_EQ(2u, child->getRefCount());
469467

470468
invalidateNvm(*parent);
471469
if (auto eventTracker = getEventTracker()) {
@@ -555,10 +553,7 @@ void CacheAllocator<CacheTrait>::transferChainLocked(WriteHandle& parent,
555553
ChainedItem* curr = &headHandle->asChainedItem();
556554
const auto newParentPtr = compressor_.compress(newParent.get());
557555
while (curr) {
558-
if (!curr->isMoving())
559-
XDCHECK_EQ(curr == headHandle.get() ? 2u : 1u, curr->getRefCount());
560-
else
561-
XDCHECK_EQ(curr == headHandle.get() ? 3u : 2u, curr->getRefCount());
556+
XDCHECK_EQ(curr == headHandle.get() ? 2u : 1u, curr->getRefCount());
562557
XDCHECK(curr->isInMMContainer());
563558
curr->changeKey(newParentPtr);
564559
curr = curr->getNext(compressor_);
@@ -654,7 +649,7 @@ CacheAllocator<CacheTrait>::replaceChainedItemLocked(Item& oldItem,
654649
WriteHandle newItemHdl,
655650
const Item& parent) {
656651
XDCHECK(newItemHdl != nullptr);
657-
XDCHECK_GE(2u, oldItem.getRefCount());
652+
XDCHECK_GE(1u, oldItem.getRefCount());
658653

659654
// grab the handle to the old item so that we can return this. Also, we need
660655
// to drop the refcount the parent holds on oldItem by manually calling
@@ -1157,7 +1152,7 @@ bool CacheAllocator<CacheTrait>::moveRegularItem(Item& oldItem,
11571152

11581153
// no one can add or remove chained items at this point
11591154
if (oldItem.hasChainedItem()) {
1160-
auto oldItemHdl = WriteHandle{&oldItem, *this};
1155+
auto oldItemHdl = acquire(&oldItem);
11611156
XDCHECK_EQ(1u, oldItemHdl->getRefCount()) << oldItemHdl->toString();
11621157
XDCHECK(!newItemHdl->hasChainedItem()) << newItemHdl->toString();
11631158
try {
@@ -1171,10 +1166,6 @@ bool CacheAllocator<CacheTrait>::moveRegularItem(Item& oldItem,
11711166

11721167
XDCHECK(!oldItem.hasChainedItem());
11731168
XDCHECK(newItemHdl->hasChainedItem());
1174-
1175-
// drop the handle, no need to decRef since we relied on
1176-
// item being moved
1177-
oldItemHdl.release();
11781169
}
11791170
newItemHdl.unmarkNascent();
11801171
return true;

cachelib/allocator/CacheAllocator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1906,7 +1906,7 @@ class CacheAllocator : public CacheBase {
19061906
void saveRamCache();
19071907

19081908
static bool itemSlabMovePredicate(const Item& item) {
1909-
return item.isMoving() && item.getRefCount() == 1;
1909+
return item.isMoving() && item.getRefCount() == 0;
19101910
}
19111911

19121912
static bool itemExpiryPredicate(const Item& item) {

cachelib/allocator/Refcount.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,18 @@ class FOLLY_PACK_ATTR RefcountWithFlags {
200200
}
201201
}
202202

203-
// Return refcount excluding control bits and flags
204-
Value getAccessRef() const noexcept { return getRaw() & kAccessRefMask; }
203+
// Return refcount excluding control bits and flags.
204+
Value getAccessRef() const noexcept {
205+
auto raw = getRaw();
206+
auto accessRef = raw & kAccessRefMask;
207+
208+
if ((raw & getAdminRef<kExclusive>()) && accessRef >= 1) {
209+
// if item is moving, ignore the extra ref
210+
return accessRef - static_cast<Value>(1);
211+
} else {
212+
return accessRef;
213+
}
214+
}
205215

206216
// Return access ref and the admin ref bits
207217
Value getRefWithAccessAndAdmin() const noexcept {
@@ -331,6 +341,9 @@ class FOLLY_PACK_ATTR RefcountWithFlags {
331341
};
332342

333343
auto newValue = [](const Value curValue) {
344+
// Set exclusive flag and make the ref count non-zero (to distinguish
345+
// from exclusive case). This extra ref will not be reported to the
346+
// user/
334347
return (curValue + static_cast<Value>(1)) | getAdminRef<kExclusive>();
335348
};
336349

0 commit comments

Comments
 (0)