Skip to content

Commit d8db9e4

Browse files
haowu14facebook-github-bot
authored andcommitted
Use CounterVisitor for NvmAdmissionPolicy
Summary: In NvmCache we use counter visitor to collect stats. This allows the function to be ignorant of the implementation of the cache. In this diff, migrate NvmAdmissionPolicy to CounterVisitor so that it can be migrated later. Reviewed By: therealgymmy Differential Revision: D40882985 fbshipit-source-id: 2d83e59ec1721f04b4b2968ed29745cb8a35a7d4
1 parent 69b9e0f commit d8db9e4

File tree

5 files changed

+35
-35
lines changed

5 files changed

+35
-35
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3660,11 +3660,11 @@ std::unordered_map<std::string, double>
36603660
CacheAllocator<CacheTrait>::getNvmCacheStatsMap() const {
36613661
auto ret = nvmCache_ ? nvmCache_->getStatsMap()
36623662
: std::unordered_map<std::string, double>{};
3663+
util::CounterVisitor visitor = [&ret](folly::StringPiece k, double v) {
3664+
ret[k.str()] = v;
3665+
};
36633666
if (nvmAdmissionPolicy_) {
3664-
auto policyStats = nvmAdmissionPolicy_->getCounters();
3665-
for (const auto& kv : policyStats) {
3666-
ret[kv.first] = kv.second;
3667-
}
3667+
nvmAdmissionPolicy_->getCounters(visitor);
36683668
}
36693669
return ret;
36703670
}

cachelib/allocator/NvmAdmissionPolicy.h

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,29 @@ class NvmAdmissionPolicy {
8181
return decision;
8282
}
8383

84-
// The method that exposes stats.
84+
// Deprecated. Please use getCounters(visitor)
8585
virtual std::unordered_map<std::string, double> getCounters() final {
86-
auto ctrs = getCountersImpl();
87-
ctrs["ap.called"] = overallCount_.get();
88-
ctrs["ap.accepted"] = accepted_.get();
89-
ctrs["ap.rejected"] = rejected_.get();
86+
std::unordered_map<std::string, double> ctrs;
87+
util::CounterVisitor visitor = [&ctrs](folly::StringPiece k, double v) {
88+
auto keyStr = k.str();
89+
ctrs.insert({std::move(keyStr), v});
90+
};
91+
getCounters(visitor);
92+
return ctrs;
93+
}
9094

91-
auto visitorUs = [&ctrs](folly::StringPiece name, double count) {
92-
ctrs[name.toString()] = count / 1000;
95+
// The method that exposes stats.
96+
virtual void getCounters(const util::CounterVisitor& visitor) final {
97+
getCountersImpl(visitor);
98+
visitor("ap.called", overallCount_.get());
99+
visitor("ap.accepted", accepted_.get());
100+
visitor("ap.rejected", rejected_.get());
101+
102+
auto visitorUs = [&visitor](folly::StringPiece name, double count) {
103+
visitor(name.toString(), count / 1000);
93104
};
94105
overallLatency_.visitQuantileEstimator(visitorUs, "ap.latency_us");
95-
ctrs["ap.ttlRejected"] = ttlRejected_.get();
96-
return ctrs;
106+
visitor("ap.ttlRejected", ttlRejected_.get());
97107
}
98108

99109
// Track access for an item.
@@ -130,9 +140,7 @@ class NvmAdmissionPolicy {
130140
// Implementation specific statistics.
131141
// Please include a prefix/postfix with the name of implementation to avoid
132142
// collision with base level stats.
133-
virtual std::unordered_map<std::string, double> getCountersImpl() {
134-
return {};
135-
}
143+
virtual void getCountersImpl(const util::CounterVisitor&) {}
136144

137145
private:
138146
util::PercentileStats overallLatency_;
@@ -184,13 +192,11 @@ class RejectFirstAP final : public NvmAdmissionPolicy<Cache> {
184192
return trackAndCheckIfSeenBefore(key);
185193
}
186194

187-
std::unordered_map<std::string, double> getCountersImpl() final override {
188-
std::unordered_map<std::string, double> ctrs;
189-
ctrs["ap.reject_first_keys_tracked"] = tracker_.numKeysTracked();
190-
ctrs["ap.reject_first_tracking_window_secs"] =
191-
tracker_.trackingWindowDurationSecs();
192-
ctrs["ap.reject_first_admits_by_dram_hit"] = admitsByDramHits_.get();
193-
return ctrs;
195+
void getCountersImpl(const util::CounterVisitor& visitor) final override {
196+
visitor("ap.reject_first_keys_tracked", tracker_.numKeysTracked());
197+
visitor("ap.reject_first_tracking_window_secs",
198+
tracker_.trackingWindowDurationSecs());
199+
visitor("ap.reject_first_admits_by_dram_hit", admitsByDramHits_.get());
194200
}
195201

196202
private:

cachelib/allocator/nvmcache/tests/NvmCacheTests.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ struct MockNvmAdmissionPolicy : public NvmAdmissionPolicy<T> {
7676
virtual bool acceptImpl(const Item&, folly::Range<ChainedItemIter>) override {
7777
return true;
7878
}
79-
virtual std::unordered_map<std::string, double> getCountersImpl() override {
80-
std::unordered_map<std::string, double> ret;
81-
ret["nvm_mock_policy"] = 1;
82-
return ret;
79+
virtual void getCountersImpl(const util::CounterVisitor& visitor) override {
80+
visitor("nvm_mock_policy", 1);
8381
}
8482
};
8583
} // namespace

cachelib/allocator/tests/NvmAdmissionPolicyTest.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
namespace facebook {
2727
namespace cachelib {
2828
namespace tests {
29-
3029
class NvmAdmissionPolicyTest : public testing::Test {
3130
public:
3231
// Expose the admission policy for testing.
@@ -50,11 +49,10 @@ class MockFailedNvmAdmissionPolicy : public NvmAdmissionPolicy<Cache> {
5049
MockFailedNvmAdmissionPolicy() = default;
5150

5251
protected:
53-
virtual std::unordered_map<std::string, double> getCountersImpl() override {
54-
std::unordered_map<std::string, double> ret;
55-
ret["nvm_mock_failed_policy"] = 1;
56-
return ret;
52+
virtual void getCountersImpl(const util::CounterVisitor& v) override {
53+
v("nvm_mock_failed_policy", 1);
5754
}
55+
5856
virtual bool acceptImpl(const Item& it,
5957
folly::Range<ChainedItemIter>) override {
6058
switch (it.getKey().size() % 3) {

cachelib/cachebench/cache/Cache.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ class RetentionAP final : public NvmAdmissionPolicy<Cache> {
7474
return true;
7575
}
7676

77-
std::unordered_map<std::string, double> getCountersImpl() final override {
78-
return {};
79-
}
77+
virtual void getCountersImpl(const util::CounterVisitor&) final override {}
8078

8179
private:
8280
const uint32_t retentionThreshold_{0};

0 commit comments

Comments
 (0)