summaryrefslogtreecommitdiffstats
path: root/staging_vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-03-01 05:42:38 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2022-03-03 11:39:52 +0000
commit64c08801e0ac5094fb111c5fe3ea63cc7597506d (patch)
treea34f000d2743b864e22e322d9ce94b655c86ac97 /staging_vespalib
parent6f78fdc9750ad3ac03b14166b6838c628487458c (diff)
Let the InvocationService drive the clock instead of having its own ticking loop.
Also use sleep_until to get intervals indendant of invoke cost as long as cost is within interval. This also also saves a clock sample and simplifies implementation.
Diffstat (limited to 'staging_vespalib')
-rw-r--r--staging_vespalib/src/tests/clock/clock_benchmark.cpp6
-rw-r--r--staging_vespalib/src/tests/clock/clock_test.cpp10
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt1
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/clock.cpp31
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/clock.h29
-rw-r--r--staging_vespalib/src/vespa/vespalib/util/doom.h4
6 files changed, 19 insertions, 62 deletions
diff --git a/staging_vespalib/src/tests/clock/clock_benchmark.cpp b/staging_vespalib/src/tests/clock/clock_benchmark.cpp
index c8d3e2a5aff..a51d83ae4dc 100644
--- a/staging_vespalib/src/tests/clock/clock_benchmark.cpp
+++ b/staging_vespalib/src/tests/clock/clock_benchmark.cpp
@@ -139,17 +139,16 @@ main(int , char *argv[])
NSVolatile nsVolatile;
NSAtomic nsAtomic;
vespalib::InvokeServiceImpl invoker(vespalib::from_s(1.0/frequency));
- Clock clock;
+ Clock clock(invoker.nowPtr());
TestClock nsClock(nsValue, 1.0/frequency);
TestClock nsVolatileClock(nsVolatile, 1.0/frequency);
TestClock nsAtomicClock(nsAtomic, 1.0/frequency);
- clock.start(invoker);
assert(pool.NewThread(&nsClock, nullptr) != nullptr);
assert(pool.NewThread(&nsVolatileClock, nullptr) != nullptr);
assert(pool.NewThread(&nsAtomicClock, nullptr) != nullptr);
benchmark("vespalib::Clock", pool, samples, numThreads, [&clock]() {
- return clock.getTimeNSAssumeRunning();
+ return clock.getTimeNS();
});
benchmark("uint64_t", pool, samples, numThreads, [&nsValue]() {
return steady_time (duration(nsValue._value));
@@ -175,6 +174,5 @@ main(int , char *argv[])
});
pool.Close();
- clock.stop();
return 0;
}
diff --git a/staging_vespalib/src/tests/clock/clock_test.cpp b/staging_vespalib/src/tests/clock/clock_test.cpp
index 2c6cbc0c876..b8ee4d7cc64 100644
--- a/staging_vespalib/src/tests/clock/clock_test.cpp
+++ b/staging_vespalib/src/tests/clock/clock_test.cpp
@@ -19,19 +19,11 @@ void waitForMovement(steady_time start, Clock & clock, vespalib::duration timeou
TEST("Test that clock is ticking forward") {
vespalib::InvokeServiceImpl invoker(50ms);
- Clock clock;
- clock.start(invoker);
+ Clock clock(invoker.nowPtr());
steady_time start = clock.getTimeNS();
waitForMovement(start, clock, 10s);
steady_time stop = clock.getTimeNS();
EXPECT_TRUE(stop > start);
- std::this_thread::sleep_for(1s);
- start = clock.getTimeNS();
- waitForMovement(start, clock, 10s);
- clock.stop();
- steady_time stop2 = clock.getTimeNS();
- EXPECT_TRUE(stop2 > stop);
- EXPECT_TRUE(vespalib::count_ms(stop2 - stop) > 1000);
}
TEST_MAIN() { TEST_RUN_ALL(); } \ No newline at end of file
diff --git a/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt b/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt
index e69dd36d6f5..2912ac2397b 100644
--- a/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt
+++ b/staging_vespalib/src/vespa/vespalib/util/CMakeLists.txt
@@ -23,6 +23,7 @@ vespa_add_library(staging_vespalib_vespalib_util OBJECT
shutdownguard.cpp
scheduledexecutor.cpp
singleexecutor.cpp
+ testclock.cpp
xmlserializable.cpp
xmlstream.cpp
DEPENDS
diff --git a/staging_vespalib/src/vespa/vespalib/util/clock.cpp b/staging_vespalib/src/vespa/vespalib/util/clock.cpp
index a4b9ed0fd5d..ad9d6d534f9 100644
--- a/staging_vespalib/src/vespa/vespalib/util/clock.cpp
+++ b/staging_vespalib/src/vespa/vespalib/util/clock.cpp
@@ -1,37 +1,16 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "clock.h"
-#include <vespa/vespalib/util/invokeservice.h>
-
+#include <cassert>
namespace vespalib {
-Clock::Clock() :
- _timeNS(0u),
- _running(false),
- _invokeRegistration()
+Clock::Clock(const std::atomic<steady_time> * source) noexcept
+ : _timeNS(source)
{
- setTime();
+ assert(_timeNS != nullptr);
+ static_assert(std::atomic<steady_time>::is_always_lock_free);
}
Clock::~Clock() = default;
-void Clock::setTime() const
-{
- _timeNS.store(count_ns(steady_clock::now().time_since_epoch()), std::memory_order_relaxed);
-}
-
-void
-Clock::start(InvokeService & invoker)
-{
- _running.store(true, std::memory_order_relaxed);
- _invokeRegistration = invoker.registerInvoke([this]() { setTime(); });
-}
-
-void
-Clock::stop()
-{
- _running.store(false, std::memory_order_relaxed);
- _invokeRegistration.reset();
-}
-
}
diff --git a/staging_vespalib/src/vespa/vespalib/util/clock.h b/staging_vespalib/src/vespa/vespalib/util/clock.h
index d4cffc200fe..0d160a98654 100644
--- a/staging_vespalib/src/vespa/vespalib/util/clock.h
+++ b/staging_vespalib/src/vespa/vespalib/util/clock.h
@@ -7,9 +7,6 @@
namespace vespalib {
-class IDestructorCallback;
-class InvokeService;
-
/**
* Clock is a clock that updates the time at defined intervals.
* It is intended used where you want to check the time with low cost, but where
@@ -19,28 +16,18 @@ class InvokeService;
class Clock
{
private:
- mutable std::atomic<int64_t> _timeNS;
- std::atomic<bool> _running;
- std::unique_ptr<IDestructorCallback> _invokeRegistration;
-
- void setTime() const;
+ const std::atomic<steady_time> *_timeNS;
public:
- Clock();
+ Clock(const std::atomic<steady_time> * source) noexcept;
+ Clock(const Clock &) = delete;
+ Clock & operator =(const Clock &) = delete;
+ Clock(Clock &&) = delete;
+ Clock & operator =(Clock &&) = delete;
~Clock();
- vespalib::steady_time getTimeNS() const {
- if (!_running) {
- setTime();
- }
- return getTimeNSAssumeRunning();
- }
- vespalib::steady_time getTimeNSAssumeRunning() const {
- return vespalib::steady_time(std::chrono::nanoseconds(_timeNS.load(std::memory_order_relaxed)));
+ vespalib::steady_time getTimeNS() const noexcept {
+ return vespalib::steady_time(_timeNS->load(std::memory_order_relaxed));
}
-
- void start(InvokeService & invoker);
- void stop();
};
}
-
diff --git a/staging_vespalib/src/vespa/vespalib/util/doom.h b/staging_vespalib/src/vespa/vespalib/util/doom.h
index b9a7e76b8af..e7db0795fb7 100644
--- a/staging_vespalib/src/vespa/vespalib/util/doom.h
+++ b/staging_vespalib/src/vespa/vespalib/util/doom.h
@@ -14,8 +14,8 @@ public:
Doom(const Clock &clock, steady_time softDoom,
steady_time hardDoom, bool explicitSoftDoom);
- bool soft_doom() const { return (_clock.getTimeNSAssumeRunning() > _softDoom); }
- bool hard_doom() const { return (_clock.getTimeNSAssumeRunning() > _hardDoom); }
+ bool soft_doom() const { return (_clock.getTimeNS() > _softDoom); }
+ bool hard_doom() const { return (_clock.getTimeNS() > _hardDoom); }
duration soft_left() const { return _softDoom - _clock.getTimeNS(); }
duration hard_left() const { return _hardDoom - _clock.getTimeNS(); }
bool isExplicitSoftDoom() const { return _isExplicitSoftDoom; }