Skip to content

Commit 48add28

Browse files
committed
feat: Report number of lookups to the AsyncDataCache (#15430)
Add runtime stats for number of lookups to the AsyncDataCache.
1 parent 5c9f9dd commit 48add28

File tree

7 files changed

+23
-3
lines changed

7 files changed

+23
-3
lines changed

velox/common/base/Counters.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ void registerVeloxMetrics() {
162162
DEFINE_METRIC(
163163
kMetricMemoryCacheSumEvictScore, facebook::velox::StatType::SUM);
164164

165+
// Number of lookups since last counter retrieval.
166+
DEFINE_METRIC(kMetricMemoryCacheNumLookups, facebook::velox::StatType::SUM);
167+
165168
// Number of hits (saved IO) since last counter retrieval. The first hit to a
166169
// prefetched entry does not count.
167170
DEFINE_METRIC(kMetricMemoryCacheNumHits, facebook::velox::StatType::SUM);

velox/common/base/Counters.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ constexpr std::string_view kMetricMemoryCacheTotalPrefetchBytes{
220220
constexpr std::string_view kMetricMemoryCacheSumEvictScore{
221221
"velox.memory_cache_sum_evict_score"};
222222

223+
constexpr std::string_view kMetricMemoryCacheNumLookups{
224+
"velox.memory_cache_num_lookups"};
225+
223226
constexpr std::string_view kMetricMemoryCacheNumHits{
224227
"velox.memory_cache_num_hits"};
225228

velox/common/base/PeriodicStatsReporter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ void PeriodicStatsReporter::reportCacheStats() {
162162
// Memory cache cumulative stats.
163163
const auto deltaCacheStats = cacheStats - lastCacheStats_;
164164

165+
REPORT_IF_NOT_ZERO(kMetricMemoryCacheNumLookups, deltaCacheStats.numLookup);
165166
REPORT_IF_NOT_ZERO(kMetricMemoryCacheNumHits, deltaCacheStats.numHit);
166167
REPORT_IF_NOT_ZERO(kMetricMemoryCacheHitBytes, deltaCacheStats.hitBytes);
167168
REPORT_IF_NOT_ZERO(kMetricMemoryCacheNumNew, deltaCacheStats.numNew);

velox/common/base/tests/StatsReporterTest.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ TEST_F(PeriodicStatsReporterTest, basic) {
462462
ASSERT_EQ(
463463
counterMap.count(std::string(kMetricMemoryAllocatorTotalUsedBytes)), 1);
464464
// Check deltas are not reported
465+
ASSERT_EQ(counterMap.count(std::string(kMetricMemoryCacheNumLookups)), 0);
465466
ASSERT_EQ(counterMap.count(std::string(kMetricMemoryCacheNumHits)), 0);
466467
ASSERT_EQ(counterMap.count(std::string(kMetricMemoryCacheHitBytes)), 0);
467468
ASSERT_EQ(counterMap.count(std::string(kMetricMemoryCacheNumNew)), 0);
@@ -507,7 +508,7 @@ TEST_F(PeriodicStatsReporterTest, basic) {
507508
counterMap.count(std::string(kMetricSsdCacheRecoveredEntries)), 0);
508509
ASSERT_EQ(
509510
counterMap.count(std::string(kMetricSsdCacheReadWithoutChecksum)), 0);
510-
ASSERT_EQ(counterMap.size(), 24);
511+
ASSERT_EQ(counterMap.size(), 25);
511512
}
512513

513514
// Update stats
@@ -536,7 +537,8 @@ TEST_F(PeriodicStatsReporterTest, basic) {
536537
newSsdStats->readWithoutChecksumChecks = 10;
537538
newSsdStats->entriesRecovered = 10;
538539
cache.updateStats(
539-
{.numHit = 10,
540+
{.numLookup = 10,
541+
.numHit = 10,
540542
.hitBytes = 10,
541543
.numNew = 10,
542544
.numEvict = 10,
@@ -559,6 +561,7 @@ TEST_F(PeriodicStatsReporterTest, basic) {
559561
// Check delta stats are reported
560562
{
561563
std::lock_guard<std::mutex> l(reporter_->m);
564+
ASSERT_EQ(counterMap.count(std::string(kMetricMemoryCacheNumLookups)), 1);
562565
ASSERT_EQ(counterMap.count(std::string(kMetricMemoryCacheNumHits)), 1);
563566
ASSERT_EQ(counterMap.count(std::string(kMetricMemoryCacheHitBytes)), 1);
564567
ASSERT_EQ(counterMap.count(std::string(kMetricMemoryCacheNumNew)), 1);
@@ -604,7 +607,7 @@ TEST_F(PeriodicStatsReporterTest, basic) {
604607
counterMap.count(std::string(kMetricSsdCacheRecoveredEntries)), 1);
605608
ASSERT_EQ(
606609
counterMap.count(std::string(kMetricSsdCacheReadWithoutChecksum)), 1);
607-
ASSERT_EQ(counterMap.size(), 56);
610+
ASSERT_EQ(counterMap.size(), 57);
608611
}
609612
}
610613

velox/common/caching/AsyncDataCache.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ CachePin CacheShard::findOrCreate(
171171
{
172172
std::lock_guard<std::mutex> l(mutex_);
173173
++eventCounter_;
174+
++numLookup_;
174175
auto it = entryMap_.find(key);
175176
if (it != entryMap_.end()) {
176177
auto* foundEntry = it->second;
@@ -549,6 +550,7 @@ void CacheShard::updateStats(CacheStats& stats) {
549550
++stats.numTinyEntries;
550551
}
551552
}
553+
stats.numLookup += numLookup_;
552554
stats.numHit += numHit_;
553555
stats.hitBytes += hitBytes_;
554556
stats.numNew += numNew_;
@@ -638,6 +640,7 @@ bool CacheShard::removeFileEntries(
638640
639641
CacheStats CacheStats::operator-(const CacheStats& other) const {
640642
CacheStats result;
643+
result.numLookup = numLookup - other.numLookup;
641644
result.numHit = numHit - other.numHit;
642645
result.hitBytes = hitBytes - other.hitBytes;
643646
result.numNew = numNew - other.numNew;

velox/common/caching/AsyncDataCache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ struct CacheStats {
516516

517517
/// ============= Cumulative stats =============
518518

519+
/// Number of lookups.
520+
int64_t numLookup{0};
519521
/// Number of hits (saved IO). The first hit to a prefetched entry does not
520522
/// count.
521523
int64_t numHit{0};
@@ -668,6 +670,8 @@ class CacheShard {
668670
uint32_t eventCounter_{0};
669671
// Maximum retainable entry score(). Anything above this is evictable.
670672
int32_t evictionThreshold_{kNoThreshold};
673+
// Cumulative count of cache reads.
674+
uint64_t numLookup_{0};
671675
// Cumulative count of cache hits.
672676
uint64_t numHit_{0};
673677
// Cumulative Sum of bytes in cache hits.

velox/docs/monitoring/metrics.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ Cache
330330
- Sum
331331
- Sum of scores of evicted entries. This serves to infer an average lifetime
332332
for entries in cache.
333+
* - memory_cache_num_lookups
334+
- Sum
335+
- Number of lookups since last counter retrieval.
333336
* - memory_cache_num_hits
334337
- Sum
335338
- Number of hits (saved IO) since last counter retrieval. The first hit to a

0 commit comments

Comments
 (0)