Skip to content

Commit db0aaa4

Browse files
committed
format code + rework documentation
1 parent a474465 commit db0aaa4

File tree

9 files changed

+128
-141
lines changed

9 files changed

+128
-141
lines changed

MultiTierDataMovement.md

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ thread (green) is integrated to the CacheLib architecture.
1212

1313
## Synchronous Eviction and Promotion
1414

15-
- `disableEvictionToMemory`: Disables eviction to memory (item is always evicted to NVMe or removed
15+
- `disableEviction`: Disables eviction to memory (item is always evicted to NVMe or removed
1616
on eviction)
1717

1818
## Background Evictors
@@ -23,7 +23,7 @@ strategies and general parameters.
2323

2424
- `backgroundEvictorIntervalMilSec`: The interval that this thread runs for - by default
2525
the background evictor threads will wake up every 10 ms to scan the AllocationClasses. Also,
26-
the background evictor thead will be woken up everytime there is a failed allocation (from
26+
the background evictor thread will be woken up everytime there is a failed allocation (from
2727
a request handling thread) and the current percentage of free memory for the
2828
AllocationClass is lower than `lowEvictionAcWatermark`. This may render the interval parameter
2929
not as important when there are many allocations occuring from request handling threads.
@@ -59,7 +59,7 @@ don't set this above `10`.
5959

6060
## Background Promoters
6161

62-
The background promotes scan each class to see if there are objects to move to a lower
62+
The background promoters scan each class to see if there are objects to move to a lower
6363
tier using a given strategy. Here we document the parameters for the different
6464
strategies and general parameters.
6565

@@ -93,25 +93,3 @@ This value should correlate with `lowEvictionAcWatermark`, `highEvictionAcWaterm
9393
- `maxPromotionBatch`: The number of objects to promote in batch during BG promotion. Analogous to
9494
`maxEvictionBatch`. It's value should be lower to decrease contention on hot items.
9595

96-
## Allocation policies
97-
98-
- `maxAcAllocationWatermark`: Item is always allocated in topmost tier if at least this
99-
percentage of the AllocationClass is free.
100-
- `minAcAllocationWatermark`: Item is always allocated in bottom tier if only this percent
101-
of the AllocationClass is free. If percentage of free AllocationClasses is between `maxAcAllocationWatermark`
102-
and `minAcAllocationWatermark`: then extra checks (described below) are performed to decide where to put the element.
103-
104-
By default, allocation will always be performed from the upper tier.
105-
106-
- `acTopTierEvictionWatermark`: If there is less that this percent of free memory in topmost tier, cachelib will attempt to evict from top tier. This option takes precedence before allocationWatermarks.
107-
108-
### Extra policies (used only when percentage of free AllocationClasses is between `maxAcAllocationWatermark`
109-
and `minAcAllocationWatermark`)
110-
- `sizeThresholdPolicy`: If item is smaller than this value, always allocate it in upper tier.
111-
- `defaultTierChancePercentage`: Change (0-100%) of allocating item in top tier
112-
113-
## MMContainer options
114-
115-
- `lruInsertionPointSpec`: Can be set per tier when LRU2Q is used. Determines where new items are
116-
inserted. 0 = insert to hot queue, 1 = insert to warm queue, 2 = insert to cold queue
117-
- `markUsefulChance`: Per-tier, determines chance of moving item to the head of LRU on access

cachelib/allocator/BackgroundMover-inl.h

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,24 @@
1717
namespace facebook {
1818
namespace cachelib {
1919

20-
2120
template <typename CacheT>
22-
BackgroundMover<CacheT>::BackgroundMover(Cache& cache,
23-
std::shared_ptr<BackgroundMoverStrategy> strategy,
24-
MoverDir direction)
25-
: cache_(cache),
26-
strategy_(strategy),
27-
direction_(direction)
28-
{
29-
if (direction_ == MoverDir::Evict) {
30-
moverFunc =
31-
BackgroundMoverAPIWrapper<CacheT>::traverseAndEvictItems;
32-
33-
} else if (direction_ == MoverDir::Promote) {
34-
moverFunc =
35-
BackgroundMoverAPIWrapper<CacheT>::traverseAndPromoteItems;
36-
}
21+
BackgroundMover<CacheT>::BackgroundMover(
22+
Cache& cache,
23+
std::shared_ptr<BackgroundMoverStrategy> strategy,
24+
MoverDir direction)
25+
: cache_(cache), strategy_(strategy), direction_(direction) {
26+
if (direction_ == MoverDir::Evict) {
27+
moverFunc = BackgroundMoverAPIWrapper<CacheT>::traverseAndEvictItems;
28+
29+
} else if (direction_ == MoverDir::Promote) {
30+
moverFunc = BackgroundMoverAPIWrapper<CacheT>::traverseAndPromoteItems;
31+
}
3732
}
3833

3934
template <typename CacheT>
40-
BackgroundMover<CacheT>::~BackgroundMover() { stop(std::chrono::seconds(0)); }
35+
BackgroundMover<CacheT>::~BackgroundMover() {
36+
stop(std::chrono::seconds(0));
37+
}
4138

4239
template <typename CacheT>
4340
void BackgroundMover<CacheT>::work() {
@@ -49,14 +46,14 @@ void BackgroundMover<CacheT>::work() {
4946
}
5047

5148
template <typename CacheT>
52-
void BackgroundMover<CacheT>::setAssignedMemory(std::vector<std::tuple<TierId, PoolId, ClassId>> &&assignedMemory)
53-
{
49+
void BackgroundMover<CacheT>::setAssignedMemory(
50+
std::vector<std::tuple<TierId, PoolId, ClassId>>&& assignedMemory) {
5451
XLOG(INFO, "Class assigned to background worker:");
5552
for (auto [tid, pid, cid] : assignedMemory) {
5653
XLOGF(INFO, "Tid: {}, Pid: {}, Cid: {}", tid, pid, cid);
5754
}
5855

59-
mutex.lock_combine([this, &assignedMemory]{
56+
mutex.lock_combine([this, &assignedMemory] {
6057
this->assignedMemory_ = std::move(assignedMemory);
6158
});
6259
}
@@ -65,31 +62,28 @@ void BackgroundMover<CacheT>::setAssignedMemory(std::vector<std::tuple<TierId, P
6562
// and return those for eviction
6663
template <typename CacheT>
6764
void BackgroundMover<CacheT>::checkAndRun() {
68-
auto assignedMemory = mutex.lock_combine([this]{
69-
return assignedMemory_;
70-
});
65+
auto assignedMemory = mutex.lock_combine([this] { return assignedMemory_; });
7166

7267
unsigned int moves = 0;
7368
std::set<ClassId> classes{};
74-
auto batches = strategy_->calculateBatchSizes(cache_,assignedMemory);
69+
auto batches = strategy_->calculateBatchSizes(cache_, assignedMemory);
7570

7671
for (size_t i = 0; i < batches.size(); i++) {
7772
const auto [tid, pid, cid] = assignedMemory[i];
7873
const auto batch = batches[i];
79-
74+
8075
classes.insert(cid);
81-
const auto& mpStats = cache_.getPoolByTid(pid,tid).getStats();
76+
const auto& mpStats = cache_.getPoolByTid(pid, tid).getStats();
8277

8378
if (!batch) {
8479
continue;
8580
}
8681

87-
totalBytesMoved.add(batch * mpStats.acStats.at(cid).allocSize);
88-
89-
//try moving BATCH items from the class in order to reach free target
90-
auto moved = moverFunc(cache_,tid,pid,cid,batch);
82+
// try moving BATCH items from the class in order to reach free target
83+
auto moved = moverFunc(cache_, tid, pid, cid, batch);
9184
moves += moved;
9285
moves_per_class_[tid][pid][cid] += moved;
86+
totalBytesMoved.add(moved * mpStats.acStats.at(cid).allocSize);
9387
}
9488

9589
numTraversals.inc();

cachelib/allocator/BackgroundMover.h

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
#pragma once
1818

19-
#include <gtest/gtest_prod.h>
2019
#include <folly/concurrency/UnboundedQueue.h>
20+
#include <gtest/gtest_prod.h>
2121

22-
#include "cachelib/allocator/CacheStats.h"
23-
#include "cachelib/common/PeriodicWorker.h"
2422
#include "cachelib/allocator/BackgroundMoverStrategy.h"
23+
#include "cachelib/allocator/CacheStats.h"
2524
#include "cachelib/common/AtomicCounter.h"
26-
25+
#include "cachelib/common/PeriodicWorker.h"
2726

2827
namespace facebook {
2928
namespace cachelib {
@@ -32,23 +31,24 @@ namespace cachelib {
3231
// needed for the cache api
3332
template <typename C>
3433
struct BackgroundMoverAPIWrapper {
35-
3634
static size_t traverseAndEvictItems(C& cache,
37-
unsigned int tid, unsigned int pid, unsigned int cid, size_t batch) {
38-
return cache.traverseAndEvictItems(tid,pid,cid,batch);
35+
unsigned int tid,
36+
unsigned int pid,
37+
unsigned int cid,
38+
size_t batch) {
39+
return cache.traverseAndEvictItems(tid, pid, cid, batch);
3940
}
40-
41+
4142
static size_t traverseAndPromoteItems(C& cache,
42-
unsigned int tid, unsigned int pid, unsigned int cid, size_t batch) {
43-
return cache.traverseAndPromoteItems(tid,pid,cid,batch);
43+
unsigned int tid,
44+
unsigned int pid,
45+
unsigned int cid,
46+
size_t batch) {
47+
return cache.traverseAndPromoteItems(tid, pid, cid, batch);
4448
}
45-
4649
};
4750

48-
enum class MoverDir {
49-
Evict = 0,
50-
Promote
51-
};
51+
enum class MoverDir { Evict = 0, Promote };
5252

5353
// Periodic worker that evicts items from tiers in batches
5454
// The primary aim is to reduce insertion times for new items in the
@@ -58,35 +58,40 @@ class BackgroundMover : public PeriodicWorker {
5858
public:
5959
using Cache = CacheT;
6060
// @param cache the cache interface
61-
// @param strategy the stragey class that defines how objects are moved,
61+
// @param strategy the stragey class that defines how objects are
62+
// moved,
6263
// (promoted vs. evicted and how much)
6364
BackgroundMover(Cache& cache,
6465
std::shared_ptr<BackgroundMoverStrategy> strategy,
6566
MoverDir direction_);
6667

6768
~BackgroundMover() override;
68-
69+
6970
BackgroundMoverStats getStats() const noexcept;
70-
std::map<TierId, std::map<PoolId, std::map<ClassId, uint64_t>>> getClassStats() const noexcept;
71+
std::map<TierId, std::map<PoolId, std::map<ClassId, uint64_t>>>
72+
getClassStats() const noexcept;
7173

72-
void setAssignedMemory(std::vector<std::tuple<TierId, PoolId, ClassId>> &&assignedMemory);
74+
void setAssignedMemory(
75+
std::vector<std::tuple<TierId, PoolId, ClassId>>&& assignedMemory);
7376

7477
private:
75-
std::map<TierId, std::map<PoolId, std::map<ClassId, uint64_t>>> moves_per_class_;
78+
std::map<TierId, std::map<PoolId, std::map<ClassId, uint64_t>>>
79+
moves_per_class_;
7680
// cache allocator's interface for evicting
7781
using Item = typename Cache::Item;
78-
82+
7983
Cache& cache_;
8084
std::shared_ptr<BackgroundMoverStrategy> strategy_;
8185
MoverDir direction_;
82-
83-
std::function<size_t(Cache&, unsigned int, unsigned int, unsigned int, size_t)> moverFunc;
86+
87+
std::function<size_t(
88+
Cache&, unsigned int, unsigned int, unsigned int, size_t)>
89+
moverFunc;
8490

8591
// implements the actual logic of running the background evictor
8692
void work() override final;
8793
void checkAndRun();
8894

89-
9095
AtomicCounter numMovedItems{0};
9196
AtomicCounter numTraversals{0};
9297
AtomicCounter totalClasses{0};

cachelib/allocator/BackgroundMoverStrategy.h

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

2424
// Base class for background eviction strategy.
2525
class BackgroundMoverStrategy {
26-
27-
public:
28-
virtual std::vector<size_t> calculateBatchSizes(const CacheBase& cache,
29-
std::vector<std::tuple<TierId, PoolId, ClassId>> acVec) = 0;
26+
public:
27+
virtual std::vector<size_t> calculateBatchSizes(
28+
const CacheBase& cache,
29+
std::vector<std::tuple<TierId, PoolId, ClassId>> acVec) = 0;
3030
};
3131

3232
} // namespace cachelib

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
427427
if (backgroundEvictor_.size() && !fromBgThread && (memory == nullptr || shouldWakeupBgEvictor(tid, pid, cid))) {
428428
backgroundEvictor_[backgroundWorkerId(tid, pid, cid, backgroundEvictor_.size())]->wakeUp();
429429
}
430-
// TODO: Today disableEviction means do not evict from memory (DRAM).
430+
// TODO: Today isEvictionDisabled means do not evict from memory (DRAM).
431431
// Should we support eviction between memory tiers (e.g. from DRAM to PMEM)?
432432
if (memory == nullptr && !config_.isEvictionDisabled()) {
433433
memory = findEviction(tid, pid, cid);

cachelib/allocator/CacheAllocatorConfig.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,8 @@ class CacheAllocatorConfig {
625625
// CacheAllocator::startCacheWorkers()
626626
bool delayCacheWorkersStart{false};
627627

628-
629-
double minAcAllocationWatermark{0.0};
630-
double maxAcAllocationWatermark{0.0};
631-
632-
double promotionAcWatermark{4.0};
628+
// see MultiTierDataMovement.md
629+
double promotionAcWatermark{4.0};
633630
double lowEvictionAcWatermark{2.0};
634631
double highEvictionAcWatermark{5.0};
635632
double numDuplicateElements{0.0}; // inclusivness of the cache

cachelib/allocator/FreeThresholdStrategy.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,27 @@
2121
namespace facebook {
2222
namespace cachelib {
2323

24-
25-
26-
FreeThresholdStrategy::FreeThresholdStrategy(double lowEvictionAcWatermark, double highEvictionAcWatermark, uint64_t maxEvictionBatch, uint64_t minEvictionBatch)
27-
: lowEvictionAcWatermark(lowEvictionAcWatermark), highEvictionAcWatermark(highEvictionAcWatermark), maxEvictionBatch(maxEvictionBatch), minEvictionBatch(minEvictionBatch) {}
24+
FreeThresholdStrategy::FreeThresholdStrategy(double lowEvictionAcWatermark,
25+
double highEvictionAcWatermark,
26+
uint64_t maxEvictionBatch,
27+
uint64_t minEvictionBatch)
28+
: lowEvictionAcWatermark(lowEvictionAcWatermark),
29+
highEvictionAcWatermark(highEvictionAcWatermark),
30+
maxEvictionBatch(maxEvictionBatch),
31+
minEvictionBatch(minEvictionBatch) {}
2832

2933
std::vector<size_t> FreeThresholdStrategy::calculateBatchSizes(
30-
const CacheBase& cache, std::vector<std::tuple<TierId, PoolId, ClassId>> acVec) {
34+
const CacheBase& cache,
35+
std::vector<std::tuple<TierId, PoolId, ClassId>> acVec) {
3136
std::vector<size_t> batches{};
3237
for (auto [tid, pid, cid] : acVec) {
3338
auto stats = cache.getAllocationClassStats(tid, pid, cid);
3439
if (stats.approxFreePercent >= highEvictionAcWatermark) {
3540
batches.push_back(0);
3641
} else {
3742
auto toFreeMemPercent = highEvictionAcWatermark - stats.approxFreePercent;
38-
auto toFreeItems = static_cast<size_t>(toFreeMemPercent * stats.memorySize / stats.allocSize);
43+
auto toFreeItems = static_cast<size_t>(
44+
toFreeMemPercent * stats.memorySize / stats.allocSize);
3945
batches.push_back(toFreeItems);
4046
}
4147
}
@@ -48,17 +54,18 @@ std::vector<size_t> FreeThresholdStrategy::calculateBatchSizes(
4854
if (maxBatch == 0)
4955
return batches;
5056

51-
std::transform(batches.begin(), batches.end(), batches.begin(), [&](auto numItems){
52-
if (numItems == 0) {
53-
return 0UL;
54-
}
57+
std::transform(
58+
batches.begin(), batches.end(), batches.begin(), [&](auto numItems) {
59+
if (numItems == 0) {
60+
return 0UL;
61+
}
5562

56-
auto cappedBatchSize = maxEvictionBatch * numItems / maxBatch;
57-
if (cappedBatchSize < minEvictionBatch)
58-
return minEvictionBatch;
59-
else
60-
return cappedBatchSize;
61-
});
63+
auto cappedBatchSize = maxEvictionBatch * numItems / maxBatch;
64+
if (cappedBatchSize < minEvictionBatch)
65+
return minEvictionBatch;
66+
else
67+
return cappedBatchSize;
68+
});
6269

6370
return batches;
6471
}

cachelib/allocator/FreeThresholdStrategy.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,27 @@
1616

1717
#pragma once
1818

19-
#include "cachelib/allocator/Cache.h"
2019
#include "cachelib/allocator/BackgroundMoverStrategy.h"
20+
#include "cachelib/allocator/Cache.h"
2121

2222
namespace facebook {
2323
namespace cachelib {
2424

25-
2625
// Base class for background mover strategy.
2726
class FreeThresholdStrategy : public BackgroundMoverStrategy {
28-
29-
public:
30-
FreeThresholdStrategy(double lowEvictionAcWatermark, double highEvictionAcWatermark, uint64_t maxEvictionBatch, uint64_t minEvictionBatch);
27+
public:
28+
FreeThresholdStrategy(double lowEvictionAcWatermark,
29+
double highEvictionAcWatermark,
30+
uint64_t maxEvictionBatch,
31+
uint64_t minEvictionBatch);
3132
~FreeThresholdStrategy() {}
3233

33-
std::vector<size_t> calculateBatchSizes(const CacheBase& cache,
34-
std::vector<std::tuple<TierId, PoolId, ClassId>> acVecs);
35-
private:
36-
double lowEvictionAcWatermark{2.0};
34+
std::vector<size_t> calculateBatchSizes(
35+
const CacheBase& cache,
36+
std::vector<std::tuple<TierId, PoolId, ClassId>> acVecs);
37+
38+
private:
39+
double lowEvictionAcWatermark{2.0};
3740
double highEvictionAcWatermark{5.0};
3841
uint64_t maxEvictionBatch{40};
3942
uint64_t minEvictionBatch{5};

0 commit comments

Comments
 (0)