Skip to content

Commit 888ffd6

Browse files
Rob Lyerlymeta-codesync[bot]
authored andcommitted
Few cleanups from D85438965
Summary: Clean up some comments & names to be more appropriate. Also simplify `updateNumSlabsToAdvise()` to be a bit less verbose. Reviewed By: pbhandar2 Differential Revision: D87228068 fbshipit-source-id: f4834bf0772e50368cf69511dfd65558d8cf565b
1 parent 3bfb284 commit 888ffd6

File tree

6 files changed

+17
-21
lines changed

6 files changed

+17
-21
lines changed

cachelib/allocator/CacheAllocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5880,7 +5880,7 @@ bool CacheAllocator<CacheTrait>::startNewMemMonitor(
58805880
MemoryMonitor::Config config,
58815881
std::shared_ptr<RebalanceStrategy> strategy) {
58825882
if (!startNewWorker("MemoryMonitor", memMonitor_, interval, *this, config,
5883-
strategy, allocator_->getNumSlabsToAdvise())) {
5883+
strategy, allocator_->getNumSlabsAdvised())) {
58845884
return false;
58855885
}
58865886

cachelib/allocator/MemoryMonitor.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,17 @@ void MemoryMonitor::work() {
8080

8181
void MemoryMonitor::updateNumSlabsToAdvise(int32_t numSlabs) {
8282
auto curNumSlabsToAdvise = numSlabsToAdvise_.load(std::memory_order_acquire);
83+
size_t newNumSlabsToAdvise;
8384
do {
85+
newNumSlabsToAdvise = curNumSlabsToAdvise + numSlabs;
8486
if (numSlabs < 0 &&
8587
static_cast<uint64_t>(-numSlabs) > curNumSlabsToAdvise) {
8688
throw std::invalid_argument(
8789
folly::sformat("Invalid numSlabs {} to update numSlabsToAdvise {}",
8890
numSlabs, curNumSlabsToAdvise));
8991
}
90-
91-
auto newNumSlabsToAdvise = curNumSlabsToAdvise + numSlabs;
92-
if (numSlabsToAdvise_.compare_exchange_weak(curNumSlabsToAdvise,
93-
newNumSlabsToAdvise,
94-
std::memory_order_acq_rel)) {
95-
break;
96-
}
97-
} while (true);
92+
} while (!numSlabsToAdvise_.compare_exchange_weak(
93+
curNumSlabsToAdvise, newNumSlabsToAdvise, std::memory_order_acq_rel));
9894
}
9995

10096
void MemoryMonitor::checkFreeMemory() {

cachelib/allocator/MemoryMonitor.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,9 @@ class MemoryMonitor : public PeriodicWorker {
218218
}
219219

220220
private:
221-
// updates the number of slabs to be advised by numSlabs. This would
222-
// either increment (to advise away more slabs) or decrement (to reclaim
223-
// some of the previously advised away slabs) the numSlabsToAdvise_ by
224-
// numSlabs.
221+
// updates the number of slabs to be advised by numSlabs. This would either
222+
// increment (to advise away more slabs) or decrement (to reclaim some of the
223+
// previously advised away slabs) numSlabsToAdvise_ by numSlabs.
225224
//
226225
// @param numSlabs number of slabs to add-to/subtract-from numSlabToAdvise_
227226
//

cachelib/allocator/memory/MemoryAllocator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -738,9 +738,9 @@ class MemoryAllocator {
738738
return alignedSize;
739739
}
740740

741-
// calculate total number of slabs that can be advised away across all pools
742-
size_t getNumSlabsToAdvise() const noexcept {
743-
return memoryPoolManager_.getNumSlabsToAdvise();
741+
// get the total number of slabs advised away across all pools
742+
size_t getNumSlabsAdvised() const noexcept {
743+
return memoryPoolManager_.getNumSlabsAdvised();
744744
}
745745

746746
// calculate the number of slabs to be advised/reclaimed in each pool

cachelib/allocator/memory/MemoryPoolManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ MemoryPoolManager::MemoryPoolManager(
6666
poolsByName_.insert(kv);
6767
}
6868

69-
auto slabsAdvised = getNumSlabsToAdvise();
69+
auto slabsAdvised = getNumSlabsAdvised();
7070
auto slabAllocAdvised = slabAlloc_.numSlabsReclaimable();
7171
if (slabsAdvised != slabAllocAdvised) {
7272
throw std::logic_error(folly::sformat(
@@ -76,7 +76,7 @@ MemoryPoolManager::MemoryPoolManager(
7676
}
7777
}
7878

79-
size_t MemoryPoolManager::getNumSlabsToAdvise() const noexcept {
79+
size_t MemoryPoolManager::getNumSlabsAdvised() const noexcept {
8080
size_t slabsAdvised = 0;
8181
auto maxPools = nextPoolId_.load(std::memory_order_acquire);
8282
for (auto i = 0; i < maxPools; i++) {

cachelib/allocator/memory/MemoryPoolManager.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ class MemoryPoolManager {
152152
return getRemainingSizeLocked();
153153
}
154154

155-
// calculate total number of slabs that can be advised away across all pools
156-
size_t getNumSlabsToAdvise() const noexcept;
155+
// get the total number of slabs advised away across all pools
156+
size_t getNumSlabsAdvised() const noexcept;
157157

158158
// return total memory currently advised away
159159
size_t getAdvisedMemorySize() const noexcept {
@@ -167,7 +167,8 @@ class MemoryPoolManager {
167167

168168
// calculate the number of slabs to be advised/reclaimed in each pool
169169
//
170-
// @param poolIds list of pools to process
170+
// @param numSlabsToAdvise total number of slabs to be advised/reclaimed
171+
// @param poolIds list of pools to process
171172
//
172173
// @return vector of pairs with first value as poolId and second value
173174
// the number of slabs to advise or number of slabs to reclaim

0 commit comments

Comments
 (0)