From 1bd3431f7cc9dd647a2dd9511e8c142301783ead Mon Sep 17 00:00:00 2001 From: Lei Jin Date: Mon, 28 Jul 2014 12:22:37 -0700 Subject: [PATCH] Change StopWatch interface Summary: So that we can avoid calling NowSecs() in MakeRoomForWrite twice Test Plan: make all check Reviewers: yhchiang, igor, sdong Reviewed By: sdong Subscribers: leveldb Differential Revision: https://reviews.facebook.net/D20529 --- db/db_impl.cc | 18 +++++++----------- util/stop_watch.h | 27 +++++++++++++++++---------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 6edd62f93..1f7012743 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -4156,9 +4156,8 @@ Status DBImpl::MakeRoomForWrite( mutex_.Unlock(); uint64_t delayed; { - StopWatch sw(env_, stats_, STALL_L0_SLOWDOWN_COUNT, true); + StopWatch sw(env_, stats_, STALL_L0_SLOWDOWN_COUNT, &delayed); env_->SleepForMicroseconds(slowdown); - delayed = sw.ElapsedMicros(); } RecordTick(stats_, STALL_L0_SLOWDOWN_MICROS, delayed); allow_delay = false; // Do not delay a single write more than once @@ -4184,13 +4183,12 @@ Status DBImpl::MakeRoomForWrite( } uint64_t stall; { - StopWatch sw(env_, stats_, STALL_MEMTABLE_COMPACTION_COUNT, true); + StopWatch sw(env_, stats_, STALL_MEMTABLE_COMPACTION_COUNT, &stall); if (!has_timeout) { bg_cv_.Wait(); } else { bg_cv_.TimedWait(expiration_time); } - stall = sw.ElapsedMicros(); } RecordTick(stats_, STALL_MEMTABLE_COMPACTION_MICROS, stall); cfd->internal_stats()->AddCFStats( @@ -4201,13 +4199,12 @@ Status DBImpl::MakeRoomForWrite( cfd->GetName().c_str()); uint64_t stall; { - StopWatch sw(env_, stats_, STALL_L0_NUM_FILES_COUNT, true); + StopWatch sw(env_, stats_, STALL_L0_NUM_FILES_COUNT, &stall); if (!has_timeout) { bg_cv_.Wait(); } else { bg_cv_.TimedWait(expiration_time); } - stall = sw.ElapsedMicros(); } RecordTick(stats_, STALL_L0_NUM_FILES_MICROS, stall); cfd->internal_stats()->AddCFStats( @@ -4219,9 +4216,8 @@ Status DBImpl::MakeRoomForWrite( mutex_.Unlock(); uint64_t delayed; { - StopWatch sw(env_, stats_, HARD_RATE_LIMIT_DELAY_COUNT, true); + StopWatch sw(env_, stats_, HARD_RATE_LIMIT_DELAY_COUNT, &delayed); env_->SleepForMicroseconds(1000); - delayed = sw.ElapsedMicros(); } // Make sure the following value doesn't round to zero. uint64_t rate_limit = std::max((delayed / 1000), (uint64_t) 1); @@ -4241,16 +4237,16 @@ Status DBImpl::MakeRoomForWrite( // TODO: add statistics uint64_t slowdown = SlowdownAmount(score, cfd->options()->soft_rate_limit, cfd->options()->hard_rate_limit); + uint64_t elapsed = 0; mutex_.Unlock(); { - StopWatch sw(env_, stats_, SOFT_RATE_LIMIT_DELAY_COUNT, true); + StopWatch sw(env_, stats_, SOFT_RATE_LIMIT_DELAY_COUNT, &elapsed); env_->SleepForMicroseconds(slowdown); - slowdown = sw.ElapsedMicros(); rate_limit_delay_millis += slowdown; } allow_soft_rate_limit_delay = false; mutex_.Lock(); - cfd->internal_stats()->RecordLevelNSlowdown(max_level, slowdown, true); + cfd->internal_stats()->RecordLevelNSlowdown(max_level, elapsed, true); } else { unique_ptr lfile; log::Writer* new_log = nullptr; diff --git a/util/stop_watch.h b/util/stop_watch.h index 370a73307..3637533bb 100644 --- a/util/stop_watch.h +++ b/util/stop_watch.h @@ -9,25 +9,31 @@ namespace rocksdb { // Auto-scoped. -// Records the statistic into the corresponding histogram. +// Records the measure time into the corresponding histogram if statistics +// is not nullptr. It is also saved into *elapsed if the pointer is not nullptr. class StopWatch { public: StopWatch(Env * const env, Statistics* statistics, - const uint32_t hist_type, bool force_enable = false) + const uint32_t hist_type, + uint64_t* elapsed = nullptr) : env_(env), statistics_(statistics), hist_type_(hist_type), - enabled_(statistics && statistics->HistEnabledForType(hist_type)), - start_time_(enabled_ || force_enable ? env->NowMicros() : 0) { + elapsed_(elapsed), + stats_enabled_(statistics && statistics->HistEnabledForType(hist_type)), + start_time_((stats_enabled_ || elapsed != nullptr) ? + env->NowMicros() : 0) { } - uint64_t ElapsedMicros() const { - return env_->NowMicros() - start_time_; - } ~StopWatch() { - if (enabled_) { - statistics_->measureTime(hist_type_, ElapsedMicros()); + if (elapsed_) { + *elapsed_ = env_->NowMicros() - start_time_; + } + if (stats_enabled_) { + statistics_->measureTime(hist_type_, + (elapsed_ != nullptr) ? *elapsed_ : + (env_->NowMicros() - start_time_)); } } @@ -35,7 +41,8 @@ class StopWatch { Env* const env_; Statistics* statistics_; const uint32_t hist_type_; - bool enabled_; + uint64_t* elapsed_; + bool stats_enabled_; const uint64_t start_time_; };