@@ -27,17 +27,19 @@ namespace facebook::cachelib {
2727template <typename C>
2828struct 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>
123129void 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
173179template <typename CacheT>
174- std::map<PoolId, std::map<ClassId, uint64_t >>
180+ std::map<TierId, std::map< PoolId, std::map<ClassId, uint64_t > >>
175181BackgroundMover<CacheT>::getClassStats() const noexcept {
176182 return movesPerClass_;
177183}
178184
179185template <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
0 commit comments