Skip to content

Commit bf9e131

Browse files
igchorbyrnedj
authored andcommitted
Initial multi-tier support implementation
Part 1. ----------------------------------------- This includes the following: - Multi-tier allocator with TierId - allocateInternalTier - creating multi-tier allocator on shared memory Other patches can be combined/merged with this patch (such as multi-tier serialization support and improvements to eviction). We will name those compatible with Part 1 in later patches.
1 parent 6d9985d commit bf9e131

File tree

13 files changed

+507
-262
lines changed

13 files changed

+507
-262
lines changed

cachelib/allocator/BackgroundMover.h

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,19 @@ namespace facebook::cachelib {
2727
template <typename C>
2828
struct BackgroundMoverAPIWrapper {
2929
static size_t traverseAndEvictItems(C& cache,
30+
unsigned int tid,
3031
unsigned int pid,
3132
unsigned int cid,
3233
size_t batch) {
33-
return cache.traverseAndEvictItems(pid, cid, batch);
34+
return cache.traverseAndEvictItems(tid, pid, cid, batch);
3435
}
3536

3637
static size_t traverseAndPromoteItems(C& cache,
38+
unsigned int tid,
3739
unsigned int pid,
3840
unsigned int cid,
3941
size_t batch) {
40-
return cache.traverseAndPromoteItems(pid, cid, batch);
42+
return cache.traverseAndPromoteItems(tid, pid, cid, batch);
4143
}
4244
};
4345

@@ -60,24 +62,28 @@ class BackgroundMover : public PeriodicWorker {
6062
~BackgroundMover() override;
6163

6264
BackgroundMoverStats getStats() const noexcept;
63-
std::map<PoolId, std::map<ClassId, uint64_t>> getClassStats() const noexcept;
65+
std::map<TierId, std::map<PoolId, std::map<ClassId, uint64_t>>>
66+
getClassStats() const noexcept;
6467

6568
void setAssignedMemory(std::vector<MemoryDescriptorType>&& assignedMemory);
6669

6770
// return id of the worker responsible for promoting/evicting from particlar
6871
// pool and allocation calss (id is in range [0, numWorkers))
69-
static size_t workerId(PoolId pid, ClassId cid, size_t numWorkers);
72+
static size_t workerId(TierId tid, PoolId pid, ClassId cid, size_t numWorkers);
7073

7174
private:
72-
std::map<PoolId, std::map<ClassId, uint64_t>> movesPerClass_;
75+
std::map<TierId, std::map<PoolId, std::map<ClassId, uint64_t>>>
76+
movesPerClass_;
7377
// cache allocator's interface for evicting
7478
using Item = typename Cache::Item;
7579

7680
Cache& cache_;
7781
std::shared_ptr<BackgroundMoverStrategy> strategy_;
7882
MoverDir direction_;
7983

80-
std::function<size_t(Cache&, unsigned int, unsigned int, size_t)> moverFunc;
84+
std::function<size_t(
85+
Cache&, unsigned int, unsigned int, unsigned int, size_t)>
86+
moverFunc;
8187

8288
// implements the actual logic of running the background evictor
8389
void work() override final;
@@ -123,8 +129,8 @@ template <typename CacheT>
123129
void BackgroundMover<CacheT>::setAssignedMemory(
124130
std::vector<MemoryDescriptorType>&& assignedMemory) {
125131
XLOG(INFO, "Class assigned to background worker:");
126-
for (auto [pid, cid] : assignedMemory) {
127-
XLOGF(INFO, "Pid: {}, Cid: {}", pid, cid);
132+
for (auto [tid, pid, cid] : assignedMemory) {
133+
XLOGF(INFO, "Tid: {}, Pid: {}, Cid: {}", tid, pid, cid);
128134
}
129135

130136
mutex_.lock_combine([this, &assignedMemory] {
@@ -142,18 +148,18 @@ void BackgroundMover<CacheT>::checkAndRun() {
142148
auto batches = strategy_->calculateBatchSizes(cache_, assignedMemory);
143149

144150
for (size_t i = 0; i < batches.size(); i++) {
145-
const auto [pid, cid] = assignedMemory[i];
151+
const auto [tid, pid, cid] = assignedMemory[i];
146152
const auto batch = batches[i];
147153

148154
if (batch == 0) {
149155
continue;
150156
}
151-
157+
const auto& mpStats = cache_.getPoolByTid(pid, tid).getStats();
152158
// try moving BATCH items from the class in order to reach free target
153-
auto moved = moverFunc(cache_, pid, cid, batch);
159+
auto moved = moverFunc(cache_, tid, pid, cid, batch);
154160
moves += moved;
155-
movesPerClass_[pid][cid] += moved;
156-
totalBytesMoved_.add(moved * cache_.getPool(pid).getAllocSizes()[cid]);
161+
movesPerClass_[tid][pid][cid] += moved;
162+
totalBytesMoved_.add(moved * mpStats.acStats.at(cid).allocSize );
157163
}
158164

159165
numTraversals_.inc();
@@ -171,18 +177,19 @@ BackgroundMoverStats BackgroundMover<CacheT>::getStats() const noexcept {
171177
}
172178

173179
template <typename CacheT>
174-
std::map<PoolId, std::map<ClassId, uint64_t>>
180+
std::map<TierId, std::map<PoolId, std::map<ClassId, uint64_t>>>
175181
BackgroundMover<CacheT>::getClassStats() const noexcept {
176182
return movesPerClass_;
177183
}
178184

179185
template <typename CacheT>
180-
size_t BackgroundMover<CacheT>::workerId(PoolId pid,
186+
size_t BackgroundMover<CacheT>::workerId(TierId tid,
187+
PoolId pid,
181188
ClassId cid,
182189
size_t numWorkers) {
183190
XDCHECK(numWorkers);
184191

185192
// TODO: came up with some better sharding (use hashing?)
186-
return (pid + cid) % numWorkers;
193+
return (tid + pid + cid) % numWorkers;
187194
}
188195
} // namespace facebook::cachelib

cachelib/allocator/BackgroundMoverStrategy.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ namespace facebook {
2222
namespace cachelib {
2323

2424
struct MemoryDescriptorType {
25-
MemoryDescriptorType(PoolId pid, ClassId cid) : pid_(pid), cid_(cid) {}
25+
MemoryDescriptorType(TierId tid, PoolId pid, ClassId cid) :
26+
tid_(tid), pid_(pid), cid_(cid) {}
27+
TierId tid_;
2628
PoolId pid_;
2729
ClassId cid_;
2830
};

cachelib/allocator/Cache.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ class CacheBase {
9696
// @param poolId The pool id to query
9797
virtual const MemoryPool& getPool(PoolId poolId) const = 0;
9898

99+
// Get the reference to a memory pool using a tier id, for stats purposes
100+
//
101+
// @param poolId The pool id to query
102+
// @param tierId The tier of the pool id
103+
virtual const MemoryPool& getPoolByTid(PoolId poolId, TierId tid) const = 0;
104+
99105
// Get Pool specific stats (regular pools). This includes stats from the
100106
// Memory Pool and also the cache.
101107
//
@@ -106,7 +112,7 @@ class CacheBase {
106112
//
107113
// @param poolId the pool id
108114
// @param classId the class id
109-
virtual ACStats getACStats(PoolId poolId, ClassId classId) const = 0;
115+
virtual ACStats getACStats(TierId tid,PoolId poolId, ClassId classId) const = 0;
110116

111117
// @param poolId the pool id
112118
virtual AllSlabReleaseEvents getAllSlabReleaseEvents(PoolId poolId) const = 0;

0 commit comments

Comments
 (0)