Skip to content

Commit 1b4a986

Browse files
committed
polling vs. tasks options
1 parent 8401d08 commit 1b4a986

12 files changed

+88
-33
lines changed

cachelib/allocator/BackgroundEvictor-inl.h

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,31 @@ BackgroundEvictor<CacheT>::~BackgroundEvictor() { stop(std::chrono::seconds(0));
3535

3636
template <typename CacheT>
3737
void BackgroundEvictor<CacheT>::work() {
38-
try {
39-
for (const auto pid : cache_.getRegularPoolIds()) {
40-
//check if the pool is full - probably should be if tier is full
41-
if (cache_.getPoolByTid(pid,tid_).allSlabsAllocated()) {
42-
checkAndRun(pid);
38+
if (strategy_->poll_) {
39+
try {
40+
for (const auto pid : cache_.getRegularPoolIds()) {
41+
//check if the pool is full - probably should be if tier is full
42+
if (cache_.getPoolByTid(pid,tid_).allSlabsAllocated()) {
43+
checkAndRun(pid);
44+
}
4345
}
46+
} catch (const std::exception& ex) {
47+
XLOGF(ERR, "BackgroundEvictor interrupted due to exception: {}", ex.what());
4448
}
45-
} catch (const std::exception& ex) {
46-
XLOGF(ERR, "BackgroundEvictor interrupted due to exception: {}", ex.what());
49+
} else {
50+
//when an eviction for a given pid,cid at tier 0 is triggered this will be run
51+
while (1) {
52+
std::pair p = tasks_.dequeue();
53+
unsigned int pid = p.first;
54+
unsigned int cid = p.second;
55+
unsigned int batch = strategy_->calculateBatchSize(cache_,tid_,pid,cid);
56+
//try evicting BATCH items from the class in order to reach free target
57+
unsigned int evicted =
58+
BackgroundEvictorAPIWrapper<CacheT>::traverseAndEvictItems(cache_,
59+
tid_,pid,cid,batch);
60+
runCount_ = runCount_ + 1;
61+
}
62+
4763
}
4864
}
4965

cachelib/allocator/BackgroundEvictor.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#pragma once
1818

1919
#include <gtest/gtest_prod.h>
20+
#include <folly/concurrency/UnboundedQueue.h>
2021

2122
#include "cachelib/allocator/CacheStats.h"
2223
#include "cachelib/common/PeriodicWorker.h"
@@ -53,7 +54,10 @@ class BackgroundEvictor : public PeriodicWorker {
5354
unsigned int tid);
5455

5556
~BackgroundEvictor() override;
56-
57+
58+
void schedule(size_t pid, size_t cid) {
59+
tasks_.enqueue(std::make_pair(pid,cid));
60+
}
5761
BackgroundEvictorStats getStats() const noexcept;
5862

5963
private:
@@ -64,6 +68,7 @@ class BackgroundEvictor : public PeriodicWorker {
6468
Cache& cache_;
6569
std::shared_ptr<BackgroundEvictorStrategy> strategy_;
6670
unsigned int tid_;
71+
folly::UMPSCQueue<std::pair<size_t,size_t>,true> tasks_;
6772

6873
// implements the actual logic of running the background evictor
6974
void work() override final;

cachelib/allocator/BackgroundEvictorStrategy.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,14 @@
2121
namespace facebook {
2222
namespace cachelib {
2323

24-
//struct RebalanceContext {
25-
// // Victim and Receiver must belong to the same pool
26-
// ClassId victimClassId{Slab::kInvalidClassId};
27-
// ClassId receiverClassId{Slab::kInvalidClassId};
28-
//
29-
// RebalanceContext() = default;
30-
// RebalanceContext(ClassId victim, ClassId receiver)
31-
// : victimClassId(victim), receiverClassId(receiver) {}
32-
//};
3324

3425
// Base class for background eviction strategy.
3526
class BackgroundEvictorStrategy {
3627

3728
public:
3829

30+
BackgroundEvictorStrategy(bool poll) : poll_{poll} {};
31+
3932
virtual unsigned int calculateBatchSize(const CacheBase& cache,
4033
unsigned int tid,
4134
PoolId pid,
@@ -46,6 +39,8 @@ class BackgroundEvictorStrategy {
4639
PoolId pid,
4740
ClassId cid ) = 0;
4841

42+
// if we should poll every n ms or wait for tasks in queue
43+
bool poll_{false};
4944

5045
};
5146

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "cachelib/allocator/BackgroundEvictorStrategy.h"
18+
19+
#include <folly/logging/xlog.h>
20+
21+
namespace facebook {
22+
namespace cachelib {
23+
24+
25+
bool BackgroundEvictorStrategy::shouldEvict(const CacheBase& cache,
26+
unsigned int tid,
27+
PoolId pid,
28+
ClassId cid ) {
29+
return true;
30+
}
31+
32+
33+
unsigned int BackgroundEvictorStrategy::calculateBatchSize(const CacheBase& cache,
34+
unsigned int tid,
35+
PoolId pid,
36+
ClassId cid ) {
37+
return 1;
38+
}
39+
40+
41+
} // namespace cachelib
42+
} // namespace facebook

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
382382
// Should we support eviction between memory tiers (e.g. from DRAM to PMEM)?
383383
if (memory == nullptr && !config_.disableEviction) {
384384
memory = findEviction(tid, pid, cid);
385+
backgroundEvictor_->schedule(pid,cid);
385386
}
386387

387388
ItemHandle handle;

cachelib/allocator/CacheAllocator.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,11 +1789,6 @@ class CacheAllocator : public CacheBase {
17891789
if(toReleaseHandle) {
17901790
movedToNextTier = true;
17911791
} else {
1792-
//toReleaseHandle =
1793-
// itr->isChainedItem()
1794-
// ? advanceIteratorAndTryEvictChainedItem(tid, pid, itr)
1795-
// : advanceIteratorAndTryEvictRegularItem(tid, pid, mmContainer, itr);
1796-
//just bump the iterator and move on with life
17971792
++itr;
17981793
}
17991794

@@ -1805,7 +1800,6 @@ class CacheAllocator : public CacheBase {
18051800
}
18061801
++evictions;
18071802

1808-
18091803
// we must be the last handle and for chained items, this will be
18101804
// the parent.
18111805
XDCHECK(toReleaseHandle.get() == candidate || candidate->isChainedItem());
@@ -1816,7 +1810,7 @@ class CacheAllocator : public CacheBase {
18161810
// an already zero refcount, which will throw exception
18171811
auto& itemToRelease = *toReleaseHandle.release();
18181812

1819-
// Decrementing the refcount because we want to recycle the item
1813+
// Decrementing the refcount because we will call releaseBackToAllocator
18201814
const auto ref = decRef(itemToRelease);
18211815
XDCHECK_EQ(0u, ref);
18221816

cachelib/allocator/FreeThresholdStrategy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ namespace cachelib {
2323

2424

2525

26-
FreeThresholdStrategy::FreeThresholdStrategy(double freeThreshold)
27-
: freeThreshold_(freeThreshold) {}
26+
FreeThresholdStrategy::FreeThresholdStrategy(double freeThreshold, bool poll)
27+
: BackgroundEvictorStrategy(poll), freeThreshold_(freeThreshold) {}
2828

2929
bool FreeThresholdStrategy::shouldEvict(const CacheBase& cache,
3030
unsigned int tid,

cachelib/allocator/FreeThresholdStrategy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class FreeThresholdStrategy : public BackgroundEvictorStrategy {
2828

2929
public:
3030

31-
FreeThresholdStrategy(double freeThreshold);
31+
FreeThresholdStrategy(double freeThreshold, bool poll);
3232

3333
~FreeThresholdStrategy() {}
3434

cachelib/allocator/KeepFreeStrategy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace facebook {
2222
namespace cachelib {
2323

2424

25-
KeepFreeStrategy::KeepFreeStrategy(unsigned int nKeepFree)
26-
: nKeepFree_(nKeepFree) {}
25+
KeepFreeStrategy::KeepFreeStrategy(unsigned int nKeepFree, bool poll)
26+
: BackgroundEvictorStrategy(poll), nKeepFree_(nKeepFree) {}
2727

2828
bool KeepFreeStrategy::shouldEvict(const CacheBase& cache,
2929
unsigned int tid,

cachelib/allocator/KeepFreeStrategy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class KeepFreeStrategy : public BackgroundEvictorStrategy {
2626

2727
public:
2828

29-
KeepFreeStrategy(unsigned int nKeepFree);
29+
KeepFreeStrategy(unsigned int nKeepFree, bool poll);
3030

3131
~KeepFreeStrategy() {}
3232

0 commit comments

Comments
 (0)