Skip to content

Commit c33686e

Browse files
guptaskvinser52
authored andcommitted
added per pool class rolling average latency (upstream PR version)
fix for rolling stats (on multi-tier to be followed by multi-tier rolling stats implementation in the following commit)
1 parent 6d521d6 commit c33686e

File tree

6 files changed

+117
-5
lines changed

6 files changed

+117
-5
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
403403

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

407409
// TODO: per-tier
408410
(*stats_.allocAttempts)[pid][cid].inc();
@@ -508,8 +510,9 @@ CacheAllocator<CacheTrait>::allocateChainedItemInternal(const Item& parent,
508510
const auto pid = allocator_[tid]->getAllocInfo(parent.getMemory()).poolId;
509511
const auto cid = allocator_[tid]->getAllocationClassId(pid, requiredSize);
510512

511-
// TODO: per-tier? Right now stats_ are not used in any public periodic
512-
// worker
513+
util::RollingLatencyTracker rollTracker{
514+
(*stats_.classAllocLatency)[pid][cid]};
515+
513516
(*stats_.allocAttempts)[pid][cid].inc();
514517

515518
void* memory = allocator_[tid]->allocate(pid, requiredSize);
@@ -2531,7 +2534,10 @@ ACStats CacheAllocator<CacheTrait>::getACStats(TierId tid,
25312534
ClassId classId) const {
25322535
const auto& pool = allocator_[tid]->getPool(poolId);
25332536
const auto& ac = pool.getAllocationClass(classId);
2534-
return ac.getStats();
2537+
2538+
auto stats = ac.getStats();
2539+
stats.allocLatencyNs = (*stats_.classAllocLatency)[poolId][classId];
2540+
return stats;
25352541
}
25362542

25372543
template <typename CacheTrait>

cachelib/allocator/CacheStats.cpp

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

4850
template <int>

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <unordered_map>
2323

2424
#include "cachelib/allocator/memory/Slab.h"
25+
#include "cachelib/common/RollingStats.h"
2526

2627
namespace facebook {
2728
namespace cachelib {
@@ -49,6 +50,9 @@ struct ACStats {
4950
// true if the allocation class is full.
5051
bool full;
5152

53+
// Rolling allocation latency (in ns)
54+
util::RollingStats allocLatencyNs;
55+
5256
constexpr unsigned long long totalSlabs() const noexcept {
5357
return freeSlabs + usedSlabs;
5458
}

cachelib/cachebench/cache/CacheStats.h

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

225225
out << folly::sformat(
226-
"tid{:2} pid{:2} cid{:4} {:8.2f}{} usageFraction: {:4.2f}",
227-
tid, pid, cid, allocSize, allocSizeSuffix, acUsageFraction)
226+
"tid{:2} pid{:2} cid{:4} {:8.2f}{} usageFraction: {:4.2f} "
227+
"rollingAvgAllocLatency: {:8.2f}ns",
228+
tid, pid, cid, allocSize, allocSizeSuffix, acUsageFraction,
229+
stats.allocLatencyNs.estimate())
228230
<< std::endl;
229231
});
230232
}

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)