Skip to content

Commit 8f97b48

Browse files
committed
added per tier pool class rolling average latency
1 parent 370edda commit 8f97b48

File tree

7 files changed

+128
-17
lines changed

7 files changed

+128
-17
lines changed

cachelib/allocator/Cache.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class CacheBase {
8484
CacheBase& operator=(CacheBase&&) = default;
8585

8686
// TODO: come up with some reasonable number
87-
static constexpr unsigned kMaxTiers = 8;
87+
static constexpr unsigned kMaxTiers = 2;
8888

8989
// Get a string referring to the cache name for this cache
9090
virtual const std::string getCacheName() const = 0;
@@ -103,8 +103,8 @@ class CacheBase {
103103
// @param poolId the pool id
104104
virtual PoolStats getPoolStats(PoolId poolId) const = 0;
105105

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

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

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
491491

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

495496
// TODO: per-tier
496497
(*stats_.allocAttempts)[pid][cid].inc();
@@ -589,6 +590,8 @@ CacheAllocator<CacheTrait>::allocateChainedItemInternal(
589590
const auto pid = allocator_[tid]->getAllocInfo(parent->getMemory()).poolId;
590591
const auto cid = allocator_[tid]->getAllocationClassId(pid, requiredSize);
591592

593+
util::RollingLatencyTracker rollTracker{(*stats_.classAllocLatency)[tid][pid][cid]};
594+
592595
// TODO: per-tier? Right now stats_ are not used in any public periodic
593596
// worker
594597
(*stats_.allocAttempts)[pid][cid].inc();
@@ -2677,6 +2680,7 @@ AllocationClassBaseStat CacheAllocator<CacheTrait>::getAllocationClassStats(
26772680
} else {
26782681
stats.approxFreePercent = ac.approxFreePercentage();
26792682
}
2683+
stats.allocLatencyNs = (*stats_.classAllocLatency)[tid][pid][cid];
26802684

26812685
return stats;
26822686
}

cachelib/allocator/CacheStats.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ void Stats::init() {
4242
initToZero(*fragmentationSize);
4343
initToZero(*chainedItemEvictions);
4444
initToZero(*regularItemEvictions);
45+
46+
classAllocLatency = std::make_unique<PerTierPoolClassRollingStats>();
4547
}
4648

4749
template <int>

cachelib/allocator/CacheStats.h

Lines changed: 4 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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 {
@@ -225,6 +226,14 @@ struct Stats {
225226
std::unique_ptr<PerPoolClassAtomicCounters> chainedItemEvictions{};
226227
std::unique_ptr<PerPoolClassAtomicCounters> regularItemEvictions{};
227228

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

cachelib/cachebench/cache/CacheStats.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ struct Stats {
9999
uint64_t invalidDestructorCount{0};
100100
int64_t unDestructedItemCount{0};
101101

102-
std::map<TierId, std::map<PoolId, std::map<ClassId, AllocationClassBaseStat>>> allocationClassStats;
102+
std::map<TierId, std::map<PoolId, std::map<ClassId, AllocationClassBaseStat>>>
103+
allocationClassStats;
103104

104105
std::vector<double> slabsApproxFreePercentages;
105106

@@ -125,7 +126,9 @@ struct Stats {
125126

126127
if (FLAGS_report_memory_usage_stats != "") {
127128
for (TierId tid = 0; tid < slabsApproxFreePercentages.size(); tid++) {
128-
out << folly::sformat("tid{:2} free slabs : {:.2f}%", tid, slabsApproxFreePercentages[tid]) << std::endl;
129+
out << folly::sformat("tid{:2} free slabs : {:.2f}%", tid,
130+
slabsApproxFreePercentages[tid])
131+
<< std::endl;
129132
}
130133

131134
auto formatMemory = [&](size_t bytes) -> std::tuple<std::string, double> {
@@ -149,26 +152,25 @@ struct Stats {
149152
};
150153

151154
auto foreachAC = [&](auto cb) {
152-
for (auto &tidStats : allocationClassStats) {
153-
for (auto &pidStat : tidStats.second) {
154-
for (auto &cidStat : pidStat.second) {
155+
for (auto& tidStats : allocationClassStats) {
156+
for (auto& pidStat : tidStats.second) {
157+
for (auto& cidStat : pidStat.second) {
155158
cb(tidStats.first, pidStat.first, cidStat.first, cidStat.second);
156159
}
157160
}
158161
}
159162
};
160163

161-
foreachAC([&](auto tid, auto pid, auto cid, auto stats){
164+
foreachAC([&](auto tid, auto pid, auto cid, auto stats) {
162165
auto [allocSizeSuffix, allocSize] = formatMemory(stats.allocSize);
163166
auto [memorySizeSuffix, memorySize] = formatMemory(stats.memorySize);
164-
out << folly::sformat("tid{:2} pid{:2} cid{:4} {:8.2f}{} memorySize: {:8.2f}{}",
165-
tid, pid, cid, allocSize, allocSizeSuffix, memorySize, memorySizeSuffix) << std::endl;
166-
});
167-
168-
foreachAC([&](auto tid, auto pid, auto cid, auto stats){
169-
auto [allocSizeSuffix, allocSize] = formatMemory(stats.allocSize);
170-
out << folly::sformat("tid{:2} pid{:2} cid{:4} {:8.2f}{} free: {:4.2f}%",
171-
tid, pid, cid, allocSize, allocSizeSuffix, stats.approxFreePercent) << std::endl;
167+
out << folly::sformat(
168+
"tid{:2} pid{:2} cid{:4} {:8.2f}{} memorySize:{:8.2f}{} "
169+
"free:{:4.2f}% rollingAvgAllocLatency:{:8.2f}ns",
170+
tid, pid, cid, allocSize, allocSizeSuffix, memorySize,
171+
memorySizeSuffix, stats.approxFreePercent,
172+
stats.allocLatencyNs.estimate())
173+
<< std::endl;
172174
});
173175
}
174176

cachelib/common/RollingStats.h

Lines changed: 90 additions & 0 deletions
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)