[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
This commit is contained in:
Abhishek Kona 2013-05-17 10:55:34 -07:00
parent 446151cd20
commit e1174306c5
2 changed files with 19 additions and 50 deletions

View File

@ -1930,9 +1930,7 @@ Status DBImpl::Get(const ReadOptions& options,
std::string* value) { std::string* value) {
Status s; Status s;
std::unique_ptr<StopWatch> sw = stats::StartStopWatch(env_, StopWatch sw(env_, options_.statistics, DB_GET);
options_.statistics,
DB_GET);
SequenceNumber snapshot; SequenceNumber snapshot;
MutexLock l(&mutex_); MutexLock l(&mutex_);
if (options.snapshot != nullptr) { if (options.snapshot != nullptr) {
@ -2025,9 +2023,7 @@ Status DBImpl::Write(const WriteOptions& options, WriteBatch* my_batch) {
w.disableWAL = options.disableWAL; w.disableWAL = options.disableWAL;
w.done = false; w.done = false;
std::unique_ptr<StopWatch> sw = stats::StartStopWatch(env_, StopWatch sw(env_, options_.statistics, DB_WRITE);
options_.statistics,
DB_WRITE);
MutexLock l(&mutex_); MutexLock l(&mutex_);
writers_.push_back(&w); writers_.push_back(&w);
while (!w.done && &w != writers_.front()) { while (!w.done && &w != writers_.front()) {

View File

@ -3,41 +3,31 @@
#include "leveldb/env.h" #include "leveldb/env.h"
#include "leveldb/statistics.h" #include "leveldb/statistics.h"
#include <iostream>
namespace leveldb { namespace leveldb {
class StopWatch {
public:
virtual uint64_t ElapsedMicros() = 0;
virtual ~StopWatch() {}
};
class DoNothingStopWatch : public StopWatch {
public:
virtual uint64_t ElapsedMicros() {
return 0;
}
};
// Auto-scoped. // Auto-scoped.
// Records the statistic into the corresponding histogram. // Records the statistic into the corresponding histogram.
class ScopedRecordingStopWatch : public StopWatch { class StopWatch {
public: public:
ScopedRecordingStopWatch(Env * const env, StopWatch(
std::shared_ptr<Statistics> statistics, Env * const env,
const Histograms histogram_name) : std::shared_ptr<Statistics> statistics,
env_(env), const Histograms histogram_name) :
start_time_(env->NowMicros()), env_(env),
statistics_(statistics), start_time_(env->NowMicros()),
histogram_name_(histogram_name) {} statistics_(statistics),
histogram_name_(histogram_name) {}
virtual uint64_t ElapsedMicros() {
uint64_t ElapsedMicros() {
return env_->NowMicros() - start_time_; return env_->NowMicros() - start_time_;
} }
virtual ~ScopedRecordingStopWatch() { ~StopWatch() {
uint64_t elapsed_time = env_->NowMicros() - start_time_; if (statistics_) {
statistics_->measureTime(histogram_name_, elapsed_time); statistics_->measureTime(histogram_name_, ElapsedMicros());
}
} }
private: private:
@ -47,22 +37,5 @@ class ScopedRecordingStopWatch : public StopWatch {
const Histograms histogram_name_; const Histograms histogram_name_;
}; };
namespace stats {
// Helper method
std::unique_ptr<StopWatch> StartStopWatch(Env * const env,
std::shared_ptr<Statistics> stats,
Histograms histogram_name) {
assert(env);
if (stats) {
return std::unique_ptr<StopWatch>(new ScopedRecordingStopWatch(
env,
stats,
histogram_name));
} else {
return std::unique_ptr<StopWatch>(new DoNothingStopWatch());
}
};
} // namespace stats
} // namespace leveldb } // namespace leveldb
#endif // STORAGE_LEVELDB_UTIL_STOP_WATCH_H_ #endif // STORAGE_LEVELDB_UTIL_STOP_WATCH_H_