@@ -27,28 +27,34 @@ template <typename C>
27
27
struct BackgroundMoverAPIWrapper {
28
28
// traverse the cache and move items from one tier to another
29
29
// @param cache the cache interface
30
+ // @param tid the tier to traverse
30
31
// @param pid the pool id to traverse
31
32
// @param cid the class id to traverse
32
33
// @param evictionBatch number of items to evict in one go
33
34
// @param promotionBatch number of items to promote in one go
34
35
// @return pair of number of items evicted and promoted
35
36
static std::pair<size_t , size_t > traverseAndMoveItems (C& cache,
37
+ TierId tid,
36
38
PoolId pid,
37
39
ClassId cid,
38
40
size_t evictionBatch,
39
41
size_t promotionBatch) {
40
- return cache.traverseAndMoveItems (pid, cid, evictionBatch, promotionBatch);
42
+ return cache.traverseAndMoveItems (tid, pid, cid, evictionBatch, promotionBatch);
41
43
}
42
44
static std::pair<size_t , double > getApproxUsage (C& cache,
45
+ TierId tid,
43
46
PoolId pid,
44
47
ClassId cid) {
45
- const auto & pool = cache.getPool (pid);
48
+ const auto & pool = cache.getPoolByTid (pid, tid );
46
49
// we wait until all slabs are allocated before we start evicting
47
50
if (!pool.allSlabsAllocated ()) {
48
51
return {0 , 0.0 };
49
52
}
50
53
return pool.getApproxUsage (cid);
51
54
}
55
+ static unsigned int getNumTiers (C& cache) {
56
+ return cache.getNumTiers ();
57
+ }
52
58
};
53
59
54
60
// Periodic worker that evicts items from tiers in batches
@@ -78,7 +84,7 @@ class BackgroundMover : public PeriodicWorker {
78
84
79
85
// return id of the worker responsible for promoting/evicting from particlar
80
86
// pool and allocation calss (id is in range [0, numWorkers))
81
- static size_t workerId (PoolId pid, ClassId cid, size_t numWorkers);
87
+ static size_t workerId (TierId tid, PoolId pid, ClassId cid, size_t numWorkers);
82
88
83
89
private:
84
90
struct TraversalStats {
@@ -135,7 +141,9 @@ BackgroundMover<CacheT>::BackgroundMover(Cache& cache,
135
141
: cache_(cache),
136
142
evictionBatch_ (evictionBatch),
137
143
promotionBatch_(promotionBatch),
138
- targetFree_(targetFree) {}
144
+ targetFree_(targetFree) {
145
+ numTiers_ = BackgroundMoverAPIWrapper<CacheT>::getNumTiers (cache_);
146
+ }
139
147
140
148
template <typename CacheT>
141
149
void BackgroundMover<CacheT>::TraversalStats::recordTraversalTime(
@@ -170,8 +178,8 @@ template <typename CacheT>
170
178
void BackgroundMover<CacheT>::setAssignedMemory(
171
179
std::vector<MemoryDescriptorType>&& assignedMemory) {
172
180
XLOG (INFO, " Class assigned to background worker:" );
173
- for (auto [pid, cid] : assignedMemory) {
174
- XLOGF (INFO, " Pid: {}, Cid: {}" , pid, cid);
181
+ for (auto [tid, pid, cid] : assignedMemory) {
182
+ XLOGF (INFO, " Tid: {}, Pid: {}, Cid: {}" , tid , pid, cid);
175
183
}
176
184
177
185
mutex_.lock_combine ([this , &assignedMemory] {
@@ -185,10 +193,10 @@ BackgroundMover<CacheT>::getNumItemsToFree(
185
193
const std::vector<MemoryDescriptorType>& assignedMemory) {
186
194
std::map<MemoryDescriptorType, size_t > toFree;
187
195
for (const auto & md : assignedMemory) {
188
- const auto [pid, cid] = md;
196
+ const auto [tid, pid, cid] = md;
189
197
const auto & pool = cache_.getPool (pid);
190
198
const auto [activeItems, usage] =
191
- BackgroundMoverAPIWrapper<CacheT>::getApproxUsage (cache_, pid, cid);
199
+ BackgroundMoverAPIWrapper<CacheT>::getApproxUsage (cache_, tid, pid, cid);
192
200
if (usage < 1 - targetFree_) {
193
201
toFree[md] = 0 ;
194
202
} else {
@@ -210,7 +218,7 @@ void BackgroundMover<CacheT>::checkAndRun() {
210
218
while (true ) {
211
219
bool allDone = true ;
212
220
for (auto md : assignedMemory) {
213
- const auto [pid, cid] = md;
221
+ const auto [tid, pid, cid] = md;
214
222
size_t evictionBatch = evictionBatch_;
215
223
size_t promotionBatch = 0 ; // will enable with multi-tier support
216
224
if (toFree[md] == 0 ) {
@@ -224,7 +232,7 @@ void BackgroundMover<CacheT>::checkAndRun() {
224
232
const auto begin = util::getCurrentTimeNs ();
225
233
// try moving BATCH items from the class in order to reach free target
226
234
auto moved = BackgroundMoverAPIWrapper<CacheT>::traverseAndMoveItems (
227
- cache_, pid, cid, evictionBatch, promotionBatch);
235
+ cache_, tid, pid, cid, evictionBatch, promotionBatch);
228
236
numEvictedItems_ += moved.first ;
229
237
toFree[md] > moved.first ? toFree[md] -= moved.first : toFree[md] = 0 ;
230
238
numPromotedItems_ += moved.second ;
@@ -263,12 +271,13 @@ BackgroundMoverStats BackgroundMover<CacheT>::getStats() const noexcept {
263
271
}
264
272
265
273
template <typename CacheT>
266
- size_t BackgroundMover<CacheT>::workerId(PoolId pid,
274
+ size_t BackgroundMover<CacheT>::workerId(TierId tid,
275
+ PoolId pid,
267
276
ClassId cid,
268
277
size_t numWorkers) {
269
278
XDCHECK (numWorkers);
270
279
271
280
// TODO: came up with some better sharding (use hashing?)
272
- return (pid + cid) % numWorkers;
281
+ return (tid + pid + cid) % numWorkers;
273
282
}
274
283
}; // namespace facebook::cachelib
0 commit comments