Skip to content

Commit b29b1be

Browse files
committed
initial impl of bkgrd eviction
1 parent 950394b commit b29b1be

18 files changed

+623
-185
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) Intel 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+
18+
19+
namespace facebook {
20+
namespace cachelib {
21+
22+
23+
template <typename CacheT>
24+
BackgroundEvictor<CacheT>::BackgroundEvictor(Cache& cache,
25+
std::shared_ptr<BackgroundEvictorStrategy> strategy,
26+
unsigned int tid)
27+
: cache_(cache),
28+
strategy_(strategy),
29+
tid_(tid) {
30+
31+
}
32+
33+
template <typename CacheT>
34+
BackgroundEvictor<CacheT>::~BackgroundEvictor() { stop(std::chrono::seconds(0)); }
35+
36+
template <typename CacheT>
37+
void BackgroundEvictor<CacheT>::work() {
38+
try {
39+
for (const auto pid : cache_.getRegularPoolIds()) {
40+
//check if we exceed threshold (per class basis static right now)
41+
checkAndRun(pid);
42+
}
43+
} catch (const std::exception& ex) {
44+
XLOGF(ERR, "BackgroundEvictor interrupted due to exception: {}", ex.what());
45+
}
46+
}
47+
48+
49+
// Look for classes that exceed the target memory capacity
50+
// and return those for eviction
51+
template <typename CacheT>
52+
void BackgroundEvictor<CacheT>::checkAndRun(PoolId pid) {
53+
54+
const auto& mpStats = cache_.getPoolByTid(pid,tid_).getStats();
55+
for (auto& cid : mpStats.classIds) {
56+
if (strategy_->shouldEvict(cache_,tid_,pid,cid)) {
57+
unsigned int batch = strategy_->calculateBatchSize(cache_,tid_,pid,cid);
58+
//try evicting BATCH items from the class in order to reach free target
59+
unsigned int evicted =
60+
BackgroundEvictorAPIWrapper<CacheT>::traverseAndEvictItems(cache_,
61+
tid_,pid,cid,batch);
62+
numEvictedItems_ += evicted;
63+
}
64+
}
65+
runCount_ = runCount_ + 1;
66+
}
67+
68+
template <typename CacheT>
69+
BackgroundEvictorStats BackgroundEvictor<CacheT>::getStats() const noexcept {
70+
BackgroundEvictorStats stats;
71+
stats.numEvictedItems = numEvictedItems_.load(std::memory_order_relaxed);
72+
stats.numTraversals = runCount_.load(std::memory_order_relaxed);
73+
return stats;
74+
}
75+
76+
} // namespace cachelib
77+
} // namespace facebook

cachelib/allocator/BackgroundEvictor.cpp

Lines changed: 0 additions & 139 deletions
This file was deleted.

cachelib/allocator/BackgroundEvictor.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,61 @@
1818

1919
#include <gtest/gtest_prod.h>
2020

21-
#include "cachelib/allocator/Cache.h"
2221
#include "cachelib/allocator/CacheStats.h"
2322
#include "cachelib/common/PeriodicWorker.h"
23+
#include "cachelib/allocator/BackgroundEvictorStrategy.h"
24+
2425

2526
namespace facebook {
2627
namespace cachelib {
2728

29+
// wrapper that exposes the private APIs of CacheType that are specifically
30+
// needed for the eviction.
31+
template <typename C>
32+
struct BackgroundEvictorAPIWrapper {
33+
34+
static unsigned int traverseAndEvictItems(C& cache,
35+
unsigned int tid, unsigned int pid, unsigned int cid, unsigned int batch) {
36+
return cache.traverseAndEvictItems(tid,pid,cid,batch);
37+
}
38+
};
39+
2840
// Periodic worker that evicts items from tiers in batches
2941
// The primary aim is to reduce insertion times for new items in the
3042
// cache
43+
template <typename CacheT>
3144
class BackgroundEvictor : public PeriodicWorker {
3245
public:
46+
using Cache = CacheT;
3347
// @param cache the cache interface
3448
// @param target_free the target amount of memory to keep free in
3549
// this tier
3650
// @param tier id memory tier to perform eviction on
37-
BackgroundEvictor(CacheBase& cache,
38-
double targetFree
51+
BackgroundEvictor(Cache& cache,
52+
std::shared_ptr<BackgroundEvictorStrategy> strategy,
3953
unsigned int tid);
4054

4155
~BackgroundEvictor() override;
4256

57+
BackgroundEvictorStats getStats() const noexcept;
4358

4459
private:
4560
// cache allocator's interface for evicting
46-
CacheBase& cache_;
47-
double targetFree_;
61+
62+
using Item = typename Cache::Item;
63+
64+
Cache& cache_;
65+
std::shared_ptr<BackgroundEvictorStrategy> strategy_;
4866
unsigned int tid_;
4967

5068
// implements the actual logic of running the background evictor
51-
void work() final;
69+
void work() override final;
70+
void checkAndRun(PoolId pid);
71+
72+
std::atomic<uint64_t> numEvictedItems_{0};
73+
std::atomic<uint64_t> runCount_{0};
5274
};
5375
} // namespace cachelib
5476
} // namespace facebook
77+
78+
#include "cachelib/allocator/BackgroundEvictor-inl.h"
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
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
#pragma once
18+
19+
#include "cachelib/allocator/Cache.h"
20+
21+
namespace facebook {
22+
namespace cachelib {
23+
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+
//};
33+
34+
// Base class for background eviction strategy.
35+
class BackgroundEvictorStrategy {
36+
37+
public:
38+
BackgroundEvictorStrategy() {}
39+
40+
~BackgroundEvictorStrategy() {}
41+
42+
unsigned int calculateBatchSize(const CacheBase& cache,
43+
unsigned int tid,
44+
PoolId pid,
45+
ClassId cid );
46+
47+
bool shouldEvict(const CacheBase& cache,
48+
unsigned int tid,
49+
PoolId pid,
50+
ClassId cid );
51+
52+
53+
};
54+
55+
} // namespace cachelib
56+
} // namespace facebook

0 commit comments

Comments
 (0)