Skip to content

Commit

Permalink
Hide extra ref when item is marked moving
Browse files Browse the repository at this point in the history
and just return the actual ref count
  • Loading branch information
igchor committed Nov 7, 2022
1 parent 2ffdc5b commit ccd35ab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
17 changes: 4 additions & 13 deletions cachelib/allocator/CacheAllocator-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,7 @@ void CacheAllocator<CacheTrait>::addChainedItem(WriteHandle& parent,
// Parent will decrement the refcount upon release. Since this is an
// internal refcount, we dont include it in active handle tracking.
child->incRef();
auto ref = child->getRefCount();
// ref == 3 if child is moving
XDCHECK(ref == 2u || ref == 3);
XDCHECK_EQ(2u, child->getRefCount());

invalidateNvm(*parent);
if (auto eventTracker = getEventTracker()) {
Expand Down Expand Up @@ -555,10 +553,7 @@ void CacheAllocator<CacheTrait>::transferChainLocked(WriteHandle& parent,
ChainedItem* curr = &headHandle->asChainedItem();
const auto newParentPtr = compressor_.compress(newParent.get());
while (curr) {
if (!curr->isMoving())
XDCHECK_EQ(curr == headHandle.get() ? 2u : 1u, curr->getRefCount());
else
XDCHECK_EQ(curr == headHandle.get() ? 3u : 2u, curr->getRefCount());
XDCHECK_EQ(curr == headHandle.get() ? 2u : 1u, curr->getRefCount());
XDCHECK(curr->isInMMContainer());
curr->changeKey(newParentPtr);
curr = curr->getNext(compressor_);
Expand Down Expand Up @@ -654,7 +649,7 @@ CacheAllocator<CacheTrait>::replaceChainedItemLocked(Item& oldItem,
WriteHandle newItemHdl,
const Item& parent) {
XDCHECK(newItemHdl != nullptr);
XDCHECK_GE(2u, oldItem.getRefCount());
XDCHECK_GE(1u, oldItem.getRefCount());

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

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

XDCHECK(!oldItem.hasChainedItem());
XDCHECK(newItemHdl->hasChainedItem());

// drop the handle, no need to decRef since we relied on
// item being moved
oldItemHdl.release();
}
newItemHdl.unmarkNascent();
return true;
Expand Down
2 changes: 1 addition & 1 deletion cachelib/allocator/CacheAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,7 @@ class CacheAllocator : public CacheBase {
void saveRamCache();

static bool itemSlabMovePredicate(const Item& item) {
return item.isMoving() && item.getRefCount() == 1;
return item.isMoving() && item.getRefCount() == 0;
}

static bool itemExpiryPredicate(const Item& item) {
Expand Down
17 changes: 15 additions & 2 deletions cachelib/allocator/Refcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,18 @@ class FOLLY_PACK_ATTR RefcountWithFlags {
}
}

// Return refcount excluding control bits and flags
Value getAccessRef() const noexcept { return getRaw() & kAccessRefMask; }
// Return refcount excluding control bits and flags.
Value getAccessRef() const noexcept {
auto raw = getRaw();
auto accessRef = raw & kAccessRefMask;

if ((raw & getAdminRef<kExclusive>()) && accessRef >= 1) {
// if item is moving, ignore the extra ref
return accessRef - static_cast<Value>(1);
} else {
return accessRef;
}
}

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

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

Expand Down

0 comments on commit ccd35ab

Please sign in to comment.