forked from pmem/CacheLib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBackgroundEvictor.h
More file actions
99 lines (76 loc) · 2.98 KB
/
BackgroundEvictor.h
File metadata and controls
99 lines (76 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright (c) Intel and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <gtest/gtest_prod.h>
#include <folly/concurrency/UnboundedQueue.h>
#include "cachelib/allocator/CacheStats.h"
#include "cachelib/common/PeriodicWorker.h"
#include "cachelib/allocator/BackgroundEvictorStrategy.h"
#include "cachelib/common/AtomicCounter.h"
namespace facebook {
namespace cachelib {
// wrapper that exposes the private APIs of CacheType that are specifically
// needed for the eviction.
template <typename C>
struct BackgroundEvictorAPIWrapper {
static size_t traverseAndEvictItems(C& cache,
unsigned int tid, unsigned int pid, unsigned int cid, size_t batch) {
return cache.traverseAndEvictItems(tid,pid,cid,batch);
}
};
struct BackgroundEvictorStats {
// items evicted
AtomicCounter numEvictedItems{0};
// traversals
AtomicCounter numTraversals{0};
// total class size
AtomicCounter totalClasses{0};
// item eviction size
AtomicCounter evictionSize{0};
};
// Periodic worker that evicts items from tiers in batches
// The primary aim is to reduce insertion times for new items in the
// cache
template <typename CacheT>
class BackgroundEvictor : public PeriodicWorker {
public:
using Cache = CacheT;
// @param cache the cache interface
// @param target_free the target amount of memory to keep free in
// this tier
// @param tier id memory tier to perform eviction on
BackgroundEvictor(Cache& cache,
std::shared_ptr<BackgroundEvictorStrategy> strategy);
~BackgroundEvictor() override;
BackgroundEvictionStats getStats() const noexcept;
std::map<TierId, std::map<PoolId, std::map<ClassId, uint64_t>>> getClassStats() const noexcept;
void setAssignedMemory(std::vector<std::tuple<TierId, PoolId, ClassId>> &&assignedMemory);
private:
std::map<TierId, std::map<PoolId, std::map<ClassId, uint64_t>>> evictions_per_class_;
// cache allocator's interface for evicting
using Item = typename Cache::Item;
Cache& cache_;
std::shared_ptr<BackgroundEvictorStrategy> strategy_;
// implements the actual logic of running the background evictor
void work() override final;
void checkAndRun();
BackgroundEvictorStats stats;
std::vector<std::tuple<TierId, PoolId, ClassId>> assignedMemory_;
folly::DistributedMutex mutex;
};
} // namespace cachelib
} // namespace facebook
#include "cachelib/allocator/BackgroundEvictor-inl.h"