Skip to content

Commit 5f67648

Browse files
committed
added per tier pool class rolling average latency
1 parent 955fcb1 commit 5f67648

File tree

8 files changed

+120
-3
lines changed

8 files changed

+120
-3
lines changed

cachelib/allocator/Cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class CacheBase {
8585
CacheBase(CacheBase&&) = default;
8686
CacheBase& operator=(CacheBase&&) = default;
8787

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

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ CacheAllocator<CacheTrait>::allocateInternal(PoolId pid,
325325

326326
// the allocation class in our memory allocator.
327327
const auto cid = allocator_->getAllocationClassId(pid, requiredSize);
328+
util::RollingLatencyTracker rollTracker{
329+
(*stats_.classAllocLatency)[pid][cid]};
328330

329331
(*stats_.allocAttempts)[pid][cid].inc();
330332

@@ -402,6 +404,9 @@ CacheAllocator<CacheTrait>::allocateChainedItemInternal(
402404
const auto pid = allocator_->getAllocInfo(parent->getMemory()).poolId;
403405
const auto cid = allocator_->getAllocationClassId(pid, requiredSize);
404406

407+
util::RollingLatencyTracker rollTracker{
408+
(*stats_.classAllocLatency)[pid][cid]};
409+
405410
(*stats_.allocAttempts)[pid][cid].inc();
406411

407412
void* memory = allocator_->allocate(pid, requiredSize);
@@ -2225,7 +2230,10 @@ ACStats CacheAllocator<CacheTrait>::getACStats(PoolId poolId,
22252230
ClassId classId) const {
22262231
const auto& pool = allocator_->getPool(poolId);
22272232
const auto& ac = pool.getAllocationClass(classId);
2228-
return ac.getStats();
2233+
2234+
auto stats = ac.getStats();
2235+
stats.allocLatencyNs = (*stats_.classAllocLatency)[poolId][classId];
2236+
return stats;
22292237
}
22302238

22312239
template <typename CacheTrait>

cachelib/allocator/CacheStats.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ void Stats::init() {
4444
initToZero(*fragmentationSize);
4545
initToZero(*chainedItemEvictions);
4646
initToZero(*regularItemEvictions);
47+
48+
classAllocLatency = std::make_unique<PerPoolClassRollingStats>();
4749
}
4850

4951
template <int>

cachelib/allocator/CacheStats.h

Lines changed: 1 addition & 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 {

cachelib/allocator/CacheStatsInternal.h

Lines changed: 8 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 {
@@ -229,6 +230,13 @@ struct Stats {
229230
std::unique_ptr<PerPoolClassAtomicCounters> chainedItemEvictions{};
230231
std::unique_ptr<PerPoolClassAtomicCounters> regularItemEvictions{};
231232

233+
using PerPoolClassRollingStats =
234+
std::array<std::array<util::RollingStats, MemoryAllocator::kMaxClasses>,
235+
MemoryPoolManager::kMaxPools>;
236+
237+
// rolling latency tracking for every alloc class in every pool
238+
std::unique_ptr<PerPoolClassRollingStats> classAllocLatency{};
239+
232240
// Eviction failures due to parent cannot be removed from access container
233241
AtomicCounter evictFailParentAC{0};
234242

cachelib/allocator/memory/MemoryAllocatorStats.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ struct ACStats {
4747
// true if the allocation class is full.
4848
bool full;
4949

50+
// Rolling allocation latency (in ns)
51+
util::RollingStats allocLatencyNs;
52+
5053
constexpr unsigned long long totalSlabs() const noexcept {
5154
return freeSlabs + usedSlabs;
5255
}

cachelib/cachebench/cache/CacheStats.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ struct Stats {
183183
: stats.usageFraction();
184184

185185
out << folly::sformat(
186-
"pid{:2} cid{:4} {:8.2f}{} usageFraction: {:4.2f}", pid, cid,
187-
allocSize, allocSizeSuffix, acUsageFraction)
186+
"pid{:2} cid{:4} {:8.2f}{} usageFraction: {:4.2f} "
187+
"rollingAvgAllocLatency: {:8.2f}ns",
188+
pid, cid, allocSize, allocSizeSuffix, acUsageFraction,
189+
stats.allocLatencyNs.estimate())
188190
<< std::endl;
189191
});
190192
}

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)