summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Caimano <ben.caimano@10gen.com>2020-02-04 21:34:19 -0500
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-02-06 16:14:41 +0000
commit9ecd5bd7db601b007f9854516730de53d1ccd991 (patch)
tree71a0cce3220b1f5422fdc34a243c8616921afd9c
parentdb53877710ad6a840cb52dd8365c8fb7377b4bea (diff)
downloadmongo-9ecd5bd7db601b007f9854516730de53d1ccd991.tar.gz
SERVER-45857 Add time measurement class to ClockSource
This commit adds the mongo::ClockSource::StopWatch class.
-rw-r--r--src/mongo/util/clock_source.cpp3
-rw-r--r--src/mongo/util/clock_source.h41
2 files changed, 44 insertions, 0 deletions
diff --git a/src/mongo/util/clock_source.cpp b/src/mongo/util/clock_source.cpp
index 00f1a6d32c5..7fa1eb32e66 100644
--- a/src/mongo/util/clock_source.cpp
+++ b/src/mongo/util/clock_source.cpp
@@ -98,4 +98,7 @@ stdx::cv_status ClockSource::waitForConditionUntil(stdx::condition_variable& cv,
alarmInfo->cv = nullptr;
return alarmInfo->result;
}
+
+ClockSource::StopWatch::StopWatch() : StopWatch(SystemClockSource::get()){};
+
} // namespace mongo
diff --git a/src/mongo/util/clock_source.h b/src/mongo/util/clock_source.h
index f202f67f439..f57c2a35200 100644
--- a/src/mongo/util/clock_source.h
+++ b/src/mongo/util/clock_source.h
@@ -58,6 +58,40 @@ class ClockSource {
static constexpr auto kMaxTimeoutForArtificialClocks = Seconds(1);
public:
+ /**
+ * A StopWatch tracks the time that its ClockSource believes has passed since the creation of
+ * the StopWatch.
+ *
+ * For microsecond accurate metrics, use a Timer instead.
+ */
+ class StopWatch {
+ public:
+ StopWatch(ClockSource* clockSource, Date_t start)
+ : _clockSource{clockSource}, _start{start} {}
+ StopWatch(ClockSource* clockSource) : StopWatch(clockSource, clockSource->now()) {}
+ StopWatch(/** SystemClockSource::get() */);
+
+ Date_t now() noexcept {
+ return _clockSource->now();
+ }
+
+ ClockSource* getClockSource() noexcept {
+ return _clockSource;
+ }
+
+ auto start() const noexcept {
+ return _start;
+ }
+
+ auto elapsed() noexcept {
+ return now() - _start;
+ }
+
+ private:
+ ClockSource* const _clockSource;
+ const Date_t _start;
+ };
+
virtual ~ClockSource() = default;
/**
@@ -138,6 +172,13 @@ public:
return waitForConditionUntil(cv, m, now() + duration, pred, waitable);
}
+ /**
+ * Return a StopWatch that uses this ClockSource to track time
+ */
+ StopWatch makeStopWatch() {
+ return StopWatch(this);
+ }
+
protected:
bool _tracksSystemClock = true;
};