From e1174306c58c8c0d5b7effa27ffa78b8b2030057 Mon Sep 17 00:00:00 2001 From: Abhishek Kona Date: Fri, 17 May 2013 10:55:34 -0700 Subject: [PATCH] [RocksDB] Simplify StopWatch implementation Summary: Make stop watch a simple implementation, instead of subclass of a virtual class Allocate stop watches off the stack instead of heap. Code is more terse now. Test Plan: make all check, db_bench with --statistics=1 Reviewers: haobo, dhruba Reviewed By: haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D10809 --- db/db_impl.cc | 8 ++----- util/stop_watch.h | 59 +++++++++++++---------------------------------- 2 files changed, 18 insertions(+), 49 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 1183ef128..3d01d5481 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1930,9 +1930,7 @@ Status DBImpl::Get(const ReadOptions& options, std::string* value) { Status s; - std::unique_ptr sw = stats::StartStopWatch(env_, - options_.statistics, - DB_GET); + StopWatch sw(env_, options_.statistics, DB_GET); SequenceNumber snapshot; MutexLock l(&mutex_); if (options.snapshot != nullptr) { @@ -2025,9 +2023,7 @@ Status DBImpl::Write(const WriteOptions& options, WriteBatch* my_batch) { w.disableWAL = options.disableWAL; w.done = false; - std::unique_ptr sw = stats::StartStopWatch(env_, - options_.statistics, - DB_WRITE); + StopWatch sw(env_, options_.statistics, DB_WRITE); MutexLock l(&mutex_); writers_.push_back(&w); while (!w.done && &w != writers_.front()) { diff --git a/util/stop_watch.h b/util/stop_watch.h index 3b21d1036..3e5798b62 100644 --- a/util/stop_watch.h +++ b/util/stop_watch.h @@ -3,41 +3,31 @@ #include "leveldb/env.h" #include "leveldb/statistics.h" -#include -namespace leveldb { +namespace leveldb { +// Auto-scoped. +// Records the statistic into the corresponding histogram. class StopWatch { public: - virtual uint64_t ElapsedMicros() = 0; - virtual ~StopWatch() {} -}; + StopWatch( + Env * const env, + std::shared_ptr statistics, + const Histograms histogram_name) : + env_(env), + start_time_(env->NowMicros()), + statistics_(statistics), + histogram_name_(histogram_name) {} -class DoNothingStopWatch : public StopWatch { - public: - virtual uint64_t ElapsedMicros() { - return 0; - } -}; -// Auto-scoped. -// Records the statistic into the corresponding histogram. -class ScopedRecordingStopWatch : public StopWatch { - public: - ScopedRecordingStopWatch(Env * const env, - std::shared_ptr statistics, - const Histograms histogram_name) : - env_(env), - start_time_(env->NowMicros()), - statistics_(statistics), - histogram_name_(histogram_name) {} - virtual uint64_t ElapsedMicros() { + uint64_t ElapsedMicros() { return env_->NowMicros() - start_time_; } - virtual ~ScopedRecordingStopWatch() { - uint64_t elapsed_time = env_->NowMicros() - start_time_; - statistics_->measureTime(histogram_name_, elapsed_time); + ~StopWatch() { + if (statistics_) { + statistics_->measureTime(histogram_name_, ElapsedMicros()); + } } private: @@ -47,22 +37,5 @@ class ScopedRecordingStopWatch : public StopWatch { const Histograms histogram_name_; }; - -namespace stats { -// Helper method -std::unique_ptr StartStopWatch(Env * const env, - std::shared_ptr stats, - Histograms histogram_name) { - assert(env); - if (stats) { - return std::unique_ptr(new ScopedRecordingStopWatch( - env, - stats, - histogram_name)); - } else { - return std::unique_ptr(new DoNothingStopWatch()); - } -}; -} // namespace stats } // namespace leveldb #endif // STORAGE_LEVELDB_UTIL_STOP_WATCH_H_