From ab202e8d72737ec3572e5f90c0a45af12effa4be Mon Sep 17 00:00:00 2001 From: Yanqin Jin Date: Fri, 4 Sep 2020 23:23:40 -0700 Subject: [PATCH] Add a new stats level to exclude tickers (#7329) Summary: Currently, application may pass a statistics object to db but later wants to reduce stats tracking overhead by setting stats level to kExceptHistogramOrTimers (the current lowest level). Tickers will still be incremented, causing up to 1% CPU. We can add a new lowest stats level `kExceptTickers` to disable ticker incrementing as well, thus reducing CPU cycles spent on tickers. Test Plan (devserver): ``` make check make clean DEBUG_LEVEL=0 make db_bench ./db_bench -perf_level=1 -stats_level=0 -statistics -benchmarks=fillseq,readrandom -duration=120 ``` Measure CPU util (%) before and after change: CPU util by rocksdb::RecordTick: 1.1 vs (<0.1) Pull Request resolved: https://github.com/facebook/rocksdb/pull/7329 Reviewed By: pdillinger Differential Revision: D23434014 Pulled By: riversand963 fbshipit-source-id: 72ff0f02a192ac476d4b0044b9f37fd4a22ff0d4 --- db/db_statistics_test.cc | 13 +++++++++++++ include/rocksdb/statistics.h | 4 ++++ monitoring/statistics.cc | 16 +++++++++++----- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/db/db_statistics_test.cc b/db/db_statistics_test.cc index c99af22d6f..d4e4c628b1 100644 --- a/db/db_statistics_test.cc +++ b/db/db_statistics_test.cc @@ -142,6 +142,19 @@ TEST_F(DBStatisticsTest, ResetStats) { } } +TEST_F(DBStatisticsTest, ExcludeTickers) { + Options options = CurrentOptions(); + options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics(); + DestroyAndReopen(options); + options.statistics->set_stats_level(StatsLevel::kExceptTickers); + ASSERT_OK(Put("foo", "value")); + ASSERT_EQ(0, options.statistics->getTickerCount(BYTES_WRITTEN)); + options.statistics->set_stats_level(StatsLevel::kExceptHistogramOrTimers); + Reopen(options); + ASSERT_EQ("value", Get("foo")); + ASSERT_GT(options.statistics->getTickerCount(BYTES_READ), 0); +} + } // namespace ROCKSDB_NAMESPACE int main(int argc, char** argv) { diff --git a/include/rocksdb/statistics.h b/include/rocksdb/statistics.h index cc99f67638..74ee6bd29b 100644 --- a/include/rocksdb/statistics.h +++ b/include/rocksdb/statistics.h @@ -488,6 +488,10 @@ struct HistogramData { // Usage: // options.statistics->set_stats_level(StatsLevel::kExceptTimeForMutex); enum StatsLevel : uint8_t { + // Disable all metrics + kDisableAll, + // Disable tickers + kExceptTickers = kDisableAll, // Disable timer stats, and skip histogram stats kExceptHistogramOrTimers, // Skip timer stats diff --git a/monitoring/statistics.cc b/monitoring/statistics.cc index 181065c439..b0f8bb30ef 100644 --- a/monitoring/statistics.cc +++ b/monitoring/statistics.cc @@ -329,11 +329,17 @@ uint64_t StatisticsImpl::getAndResetTickerCount(uint32_t tickerType) { } void StatisticsImpl::recordTick(uint32_t tickerType, uint64_t count) { - assert(tickerType < TICKER_ENUM_MAX); - per_core_stats_.Access()->tickers_[tickerType].fetch_add( - count, std::memory_order_relaxed); - if (stats_ && tickerType < TICKER_ENUM_MAX) { - stats_->recordTick(tickerType, count); + if (get_stats_level() <= StatsLevel::kExceptTickers) { + return; + } + if (tickerType < TICKER_ENUM_MAX) { + per_core_stats_.Access()->tickers_[tickerType].fetch_add( + count, std::memory_order_relaxed); + if (stats_) { + stats_->recordTick(tickerType, count); + } + } else { + assert(false); } }