Skip to content

Commit 43ad67f

Browse files
guptaskvinser52
authored andcommitted
added per tier pool class rolling average latency
1 parent 8478fda commit 43ad67f

File tree

7 files changed

+131
-17
lines changed

7 files changed

+131
-17
lines changed

cachelib/allocator/Cache.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class CacheBase {
8383
CacheBase(CacheBase&&) = default;
8484
CacheBase& operator=(CacheBase&&) = default;
8585

86+
// TODO: come up with some reasonable number
87+
static constexpr unsigned kMaxTiers = 2;
88+
8689
// Get a string referring to the cache name for this cache
8790
virtual const std::string getCacheName() const = 0;
8891

@@ -100,8 +103,8 @@ class CacheBase {
100103
// @param poolId the pool id
101104
virtual PoolStats getPoolStats(PoolId poolId) const = 0;
102105

103-
virtual AllocationClassBaseStat getAllocationClassStats(TierId, PoolId pid, ClassId cid)
104-
const = 0;
106+
virtual AllocationClassBaseStat getAllocationClassStats(
107+
TierId, PoolId pid, ClassId cid) const = 0;
105108

106109
// @param poolId the pool id
107110
virtual AllSlabReleaseEvents getAllSlabReleaseEvents(PoolId poolId) const = 0;

cachelib/allocator/CacheAllocator-inl.h

+4
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
399399

400400
// the allocation class in our memory allocator.
401401
const auto cid = allocator_[tid]->getAllocationClassId(pid, requiredSize);
402+
util::RollingLatencyTracker rollTracker{(*stats_.classAllocLatency)[tid][pid][cid]};
402403

403404
// TODO: per-tier
404405
(*stats_.allocAttempts)[pid][cid].inc();
@@ -497,6 +498,8 @@ CacheAllocator<CacheTrait>::allocateChainedItemInternal(
497498
const auto pid = allocator_[tid]->getAllocInfo(parent->getMemory()).poolId;
498499
const auto cid = allocator_[tid]->getAllocationClassId(pid, requiredSize);
499500

501+
util::RollingLatencyTracker rollTracker{(*stats_.classAllocLatency)[tid][pid][cid]};
502+
500503
// TODO: per-tier? Right now stats_ are not used in any public periodic
501504
// worker
502505
(*stats_.allocAttempts)[pid][cid].inc();
@@ -2603,6 +2606,7 @@ AllocationClassBaseStat CacheAllocator<CacheTrait>::getAllocationClassStats(
26032606
} else {
26042607
stats.approxFreePercent = ac.approxFreePercentage();
26052608
}
2609+
stats.allocLatencyNs = (*stats_.classAllocLatency)[tid][pid][cid];
26062610

26072611
return stats;
26082612
}

cachelib/allocator/CacheStats.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ void Stats::init() {
4444
initToZero(*fragmentationSize);
4545
initToZero(*chainedItemEvictions);
4646
initToZero(*regularItemEvictions);
47+
48+
classAllocLatency = std::make_unique<PerTierPoolClassRollingStats>();
4749
}
4850

4951
template <int>
5052
struct SizeVerify {};
5153

5254
void Stats::populateGlobalCacheStats(GlobalCacheStats& ret) const {
5355
#ifndef SKIP_SIZE_VERIFY
54-
SizeVerify<sizeof(Stats)> a = SizeVerify<16160>{};
56+
SizeVerify<sizeof(Stats)> a = SizeVerify<16176>{};
5557
std::ignore = a;
5658
#endif
5759
ret.numCacheGets = numCacheGets.get();

cachelib/allocator/CacheStats.h

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "cachelib/allocator/memory/Slab.h"
2626
#include "cachelib/common/FastStats.h"
2727
#include "cachelib/common/PercentileStats.h"
28+
#include "cachelib/common/RollingStats.h"
2829
#include "cachelib/common/Time.h"
2930

3031
namespace facebook {
@@ -104,6 +105,9 @@ struct AllocationClassBaseStat {
104105

105106
// percent of free memory in this class
106107
double approxFreePercent{0.0};
108+
109+
// Rolling allocation latency (in ns)
110+
util::RollingStats allocLatencyNs;
107111
};
108112

109113
// cache related stats for a given allocation class.

cachelib/allocator/CacheStatsInternal.h

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "cachelib/allocator/Cache.h"
2222
#include "cachelib/allocator/memory/MemoryAllocator.h"
2323
#include "cachelib/common/AtomicCounter.h"
24+
#include "cachelib/common/RollingStats.h"
2425

2526
namespace facebook {
2627
namespace cachelib {
@@ -226,6 +227,14 @@ struct Stats {
226227
std::unique_ptr<PerPoolClassAtomicCounters> chainedItemEvictions{};
227228
std::unique_ptr<PerPoolClassAtomicCounters> regularItemEvictions{};
228229

230+
using PerTierPoolClassRollingStats = std::array<
231+
std::array<std::array<util::RollingStats, MemoryAllocator::kMaxClasses>,
232+
MemoryPoolManager::kMaxPools>,
233+
CacheBase::kMaxTiers>;
234+
235+
// rolling latency tracking for every alloc class in every pool
236+
std::unique_ptr<PerTierPoolClassRollingStats> classAllocLatency{};
237+
229238
// Eviction failures due to parent cannot be removed from access container
230239
AtomicCounter evictFailParentAC{0};
231240

cachelib/cachebench/cache/CacheStats.h

+16-14
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ struct Stats {
100100
uint64_t invalidDestructorCount{0};
101101
int64_t unDestructedItemCount{0};
102102

103-
std::map<TierId, std::map<PoolId, std::map<ClassId, AllocationClassBaseStat>>> allocationClassStats;
103+
std::map<TierId, std::map<PoolId, std::map<ClassId, AllocationClassBaseStat>>>
104+
allocationClassStats;
104105

105106
std::vector<double> slabsApproxFreePercentages;
106107

@@ -137,7 +138,9 @@ struct Stats {
137138

138139
if (FLAGS_report_memory_usage_stats != "") {
139140
for (TierId tid = 0; tid < slabsApproxFreePercentages.size(); tid++) {
140-
out << folly::sformat("tid{:2} free slabs : {:.2f}%", tid, slabsApproxFreePercentages[tid]) << std::endl;
141+
out << folly::sformat("tid{:2} free slabs : {:.2f}%", tid,
142+
slabsApproxFreePercentages[tid])
143+
<< std::endl;
141144
}
142145

143146
auto formatMemory = [&](size_t bytes) -> std::tuple<std::string, double> {
@@ -161,26 +164,25 @@ struct Stats {
161164
};
162165

163166
auto foreachAC = [&](auto cb) {
164-
for (auto &tidStats : allocationClassStats) {
165-
for (auto &pidStat : tidStats.second) {
166-
for (auto &cidStat : pidStat.second) {
167+
for (auto& tidStats : allocationClassStats) {
168+
for (auto& pidStat : tidStats.second) {
169+
for (auto& cidStat : pidStat.second) {
167170
cb(tidStats.first, pidStat.first, cidStat.first, cidStat.second);
168171
}
169172
}
170173
}
171174
};
172175

173-
foreachAC([&](auto tid, auto pid, auto cid, auto stats){
176+
foreachAC([&](auto tid, auto pid, auto cid, auto stats) {
174177
auto [allocSizeSuffix, allocSize] = formatMemory(stats.allocSize);
175178
auto [memorySizeSuffix, memorySize] = formatMemory(stats.memorySize);
176-
out << folly::sformat("tid{:2} pid{:2} cid{:4} {:8.2f}{} memorySize: {:8.2f}{}",
177-
tid, pid, cid, allocSize, allocSizeSuffix, memorySize, memorySizeSuffix) << std::endl;
178-
});
179-
180-
foreachAC([&](auto tid, auto pid, auto cid, auto stats){
181-
auto [allocSizeSuffix, allocSize] = formatMemory(stats.allocSize);
182-
out << folly::sformat("tid{:2} pid{:2} cid{:4} {:8.2f}{} free: {:4.2f}%",
183-
tid, pid, cid, allocSize, allocSizeSuffix, stats.approxFreePercent) << std::endl;
179+
out << folly::sformat(
180+
"tid{:2} pid{:2} cid{:4} {:8.2f}{} memorySize:{:8.2f}{} "
181+
"free:{:4.2f}% rollingAvgAllocLatency:{:8.2f}ns",
182+
tid, pid, cid, allocSize, allocSizeSuffix, memorySize,
183+
memorySizeSuffix, stats.approxFreePercent,
184+
stats.allocLatencyNs.estimate())
185+
<< std::endl;
184186
});
185187
}
186188

cachelib/common/RollingStats.h

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 <folly/Range.h>
20+
#include <folly/logging/xlog.h>
21+
22+
#include "cachelib/common/Utils.h"
23+
24+
namespace facebook {
25+
namespace cachelib {
26+
namespace util {
27+
28+
class RollingStats {
29+
public:
30+
// track latency by taking the value of duration directly.
31+
void trackValue(double value) {
32+
// This is a highly unlikely scenario where
33+
// cnt_ reaches numerical limits. Skip update
34+
// of the rolling average anymore.
35+
if (cnt_ == std::numeric_limits<uint64_t>::max()) {
36+
cnt_ = 0;
37+
return;
38+
}
39+
auto ratio = static_cast<double>(cnt_) / (cnt_ + 1);
40+
avg_ *= ratio;
41+
++cnt_;
42+
avg_ += value / cnt_;
43+
}
44+
45+
// Return the rolling average.
46+
double estimate() { return avg_; }
47+
48+
private:
49+
double avg_{0};
50+
uint64_t cnt_{0};
51+
};
52+
53+
class RollingLatencyTracker {
54+
public:
55+
explicit RollingLatencyTracker(RollingStats& stats)
56+
: stats_(&stats), begin_(std::chrono::steady_clock::now()) {}
57+
RollingLatencyTracker() {}
58+
~RollingLatencyTracker() {
59+
if (stats_) {
60+
auto tp = std::chrono::steady_clock::now();
61+
auto diffNanos =
62+
std::chrono::duration_cast<std::chrono::nanoseconds>(tp - begin_)
63+
.count();
64+
stats_->trackValue(static_cast<double>(diffNanos));
65+
}
66+
}
67+
68+
RollingLatencyTracker(const RollingLatencyTracker&) = delete;
69+
RollingLatencyTracker& operator=(const RollingLatencyTracker&) = delete;
70+
71+
RollingLatencyTracker(RollingLatencyTracker&& rhs) noexcept
72+
: stats_(rhs.stats_), begin_(rhs.begin_) {
73+
rhs.stats_ = nullptr;
74+
}
75+
76+
RollingLatencyTracker& operator=(RollingLatencyTracker&& rhs) noexcept {
77+
if (this != &rhs) {
78+
this->~RollingLatencyTracker();
79+
new (this) RollingLatencyTracker(std::move(rhs));
80+
}
81+
return *this;
82+
}
83+
84+
private:
85+
RollingStats* stats_{nullptr};
86+
std::chrono::time_point<std::chrono::steady_clock> begin_;
87+
};
88+
} // namespace util
89+
} // namespace cachelib
90+
} // namespace facebook

0 commit comments

Comments
 (0)