diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-12-06 20:17:56 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2019-12-09 20:43:38 +0000 |
commit | 9156592055b871be41b1f634ee37842854dc73a4 (patch) | |
tree | 9fe512730a78b950ed07ee7f84a330821a109062 /staging_vespalib | |
parent | 3a8c63f34d8554573b42b0c3749e44ad4f43fb0e (diff) |
Use std::chrono.
Diffstat (limited to 'staging_vespalib')
-rw-r--r-- | staging_vespalib/src/tests/clock/clock_benchmark.cpp | 35 | ||||
-rw-r--r-- | staging_vespalib/src/tests/clock/clock_test.cpp | 12 | ||||
-rw-r--r-- | staging_vespalib/src/vespa/vespalib/util/clock.cpp | 2 | ||||
-rw-r--r-- | staging_vespalib/src/vespa/vespalib/util/clock.h | 8 | ||||
-rw-r--r-- | staging_vespalib/src/vespa/vespalib/util/doom.cpp | 4 | ||||
-rw-r--r-- | staging_vespalib/src/vespa/vespalib/util/doom.h | 18 |
6 files changed, 41 insertions, 38 deletions
diff --git a/staging_vespalib/src/tests/clock/clock_benchmark.cpp b/staging_vespalib/src/tests/clock/clock_benchmark.cpp index a9618d50682..1a4cb20e338 100644 --- a/staging_vespalib/src/tests/clock/clock_benchmark.cpp +++ b/staging_vespalib/src/tests/clock/clock_benchmark.cpp @@ -10,7 +10,10 @@ #include <mutex> using vespalib::Clock; -using fastos::TimeStamp; +using vespalib::steady_time; +using vespalib::steady_clock; +using vespalib::duration; +using vespalib::to_s; struct UpdateClock { virtual ~UpdateClock() {} @@ -89,12 +92,12 @@ struct Sampler : public SamplerBase { { } void Run(FastOS_ThreadInterface *, void *) override { uint64_t samples; - fastos::SteadyTimeStamp prev = _func(); + steady_time prev = _func(); for (samples = 0; (samples < _samples); samples++) { - fastos::SteadyTimeStamp now = _func(); - fastos::TimeStamp diff = now - prev; - if (diff > 0) prev = now; - _count[1 + ((diff == 0) ? 0 : (diff > 0) ? 1 : -1)]++; + steady_time now = _func(); + duration diff = now - prev; + if (diff > duration::zero()) prev = now; + _count[1 + ((diff == duration::zero()) ? 0 : (diff > duration::zero()) ? 1 : -1)]++; } } @@ -105,7 +108,7 @@ template<typename Func> void benchmark(const char * desc, FastOS_ThreadPool & pool, uint64_t samples, uint32_t numThreads, Func func) { std::vector<std::unique_ptr<SamplerBase>> threads; threads.reserve(numThreads); - fastos::SteadyTimeStamp start = fastos::ClockSteady::now(); + steady_time start = steady_clock::now(); for (uint32_t i(0); i < numThreads; i++) { SamplerBase * sampler = new Sampler<Func>(func, i); sampler->_samples = samples; @@ -120,7 +123,7 @@ void benchmark(const char * desc, FastOS_ThreadPool & pool, uint64_t samples, ui count[i] += sampler->_count[i]; } } - printf("%s: Took %ld clock samples in %2.3f with [%ld, %ld, %ld] counts\n", desc, samples, (fastos::ClockSteady::now() - start).sec(), count[0], count[1], count[2]); + printf("%s: Took %ld clock samples in %2.3f with [%ld, %ld, %ld] counts\n", desc, samples, to_s(steady_clock ::now() - start), count[0], count[1], count[2]); } int @@ -146,26 +149,26 @@ main(int , char *argv[]) return clock.getTimeNSAssumeRunning(); }); benchmark("uint64_t", pool, samples, numThreads, [&nsValue]() { - return fastos::SteadyTimeStamp(nsValue._value) ; + return steady_time (duration(nsValue._value)); }); benchmark("volatile uint64_t", pool, samples, numThreads, [&nsVolatile]() { - return fastos::SteadyTimeStamp(nsVolatile._value) ; + return steady_time(duration(nsVolatile._value)); }); benchmark("memory_order_relaxed", pool, samples, numThreads, [&nsAtomic]() { - return fastos::SteadyTimeStamp(nsAtomic._value.load(std::memory_order_relaxed)) ; + return steady_time(duration(nsAtomic._value.load(std::memory_order_relaxed))); }); benchmark("memory_order_consume", pool, samples, numThreads, [&nsAtomic]() { - return fastos::SteadyTimeStamp(nsAtomic._value.load(std::memory_order_consume)) ; + return steady_time(duration(nsAtomic._value.load(std::memory_order_consume))); }); benchmark("memory_order_acquire", pool, samples, numThreads, [&nsAtomic]() { - return fastos::SteadyTimeStamp(nsAtomic._value.load(std::memory_order_acquire)) ; + return steady_time(duration(nsAtomic._value.load(std::memory_order_acquire))); }); benchmark("memory_order_seq_cst", pool, samples, numThreads, [&nsAtomic]() { - return fastos::SteadyTimeStamp(nsAtomic._value.load(std::memory_order_seq_cst)) ; + return steady_time(duration(nsAtomic._value.load(std::memory_order_seq_cst))); }); - benchmark("fastos::ClockSteady::now()", pool, samples, numThreads, []() { - return fastos::ClockSteady::now(); + benchmark("vespalib::steady_time::now()", pool, samples, numThreads, []() { + return steady_clock::now(); }); pool.Close(); diff --git a/staging_vespalib/src/tests/clock/clock_test.cpp b/staging_vespalib/src/tests/clock/clock_test.cpp index b5650244a45..4a06787fce5 100644 --- a/staging_vespalib/src/tests/clock/clock_test.cpp +++ b/staging_vespalib/src/tests/clock/clock_test.cpp @@ -2,11 +2,11 @@ #include <vespa/vespalib/testkit/testapp.h> #include <vespa/vespalib/util/clock.h> -#include <vespa/vespalib/util/time.h> #include <vespa/fastos/thread.h> using vespalib::Clock; -using fastos::TimeStamp; +using vespalib::duration; +using vespalib::steady_time; TEST("Test that clock is ticking forward") { @@ -14,15 +14,15 @@ TEST("Test that clock is ticking forward") { Clock clock(0.050); FastOS_ThreadPool pool(0x10000); ASSERT_TRUE(pool.NewThread(clock.getRunnable(), nullptr) != nullptr); - fastos::SteadyTimeStamp start = clock.getTimeNS(); + steady_time start = clock.getTimeNS(); std::this_thread::sleep_for(5s); - fastos::SteadyTimeStamp stop = clock.getTimeNS(); + steady_time stop = clock.getTimeNS(); EXPECT_TRUE(stop > start); std::this_thread::sleep_for(6s); clock.stop(); - fastos::SteadyTimeStamp stop2 = clock.getTimeNS(); + steady_time stop2 = clock.getTimeNS(); EXPECT_TRUE(stop2 > stop); - EXPECT_TRUE((stop2 - stop)/TimeStamp::MICRO > 1000); + 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/clock.cpp b/staging_vespalib/src/vespa/vespalib/util/clock.cpp index cd2a13029ab..0a7f89781e8 100644 --- a/staging_vespalib/src/vespa/vespalib/util/clock.cpp +++ b/staging_vespalib/src/vespa/vespalib/util/clock.cpp @@ -80,7 +80,7 @@ Clock::~Clock() void Clock::setTime() const { - _timeNS.store(fastos::ClockSteady::now() - fastos::SteadyTimeStamp::ZERO, std::memory_order_relaxed); + _timeNS.store(count_ns(steady_clock::now().time_since_epoch()), std::memory_order_relaxed); } void diff --git a/staging_vespalib/src/vespa/vespalib/util/clock.h b/staging_vespalib/src/vespa/vespalib/util/clock.h index e9e1ebace3f..d506b0c7e16 100644 --- a/staging_vespalib/src/vespa/vespalib/util/clock.h +++ b/staging_vespalib/src/vespa/vespalib/util/clock.h @@ -1,7 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #pragma once -#include <vespa/fastos/timestamp.h> +#include <vespa/vespalib/util/time.h> #include <atomic> #include <memory> @@ -31,14 +31,14 @@ public: Clock(double timePeriod=0.100); ~Clock(); - fastos::SteadyTimeStamp getTimeNS() const { + vespalib::steady_time getTimeNS() const { if (!_running) { setTime(); } return getTimeNSAssumeRunning(); } - fastos::SteadyTimeStamp getTimeNSAssumeRunning() const { - return fastos::SteadyTimeStamp(_timeNS.load(std::memory_order_relaxed)); + vespalib::steady_time getTimeNSAssumeRunning() const { + return vespalib::steady_time(std::chrono::nanoseconds(_timeNS.load(std::memory_order_relaxed))); } void stop(); diff --git a/staging_vespalib/src/vespa/vespalib/util/doom.cpp b/staging_vespalib/src/vespa/vespalib/util/doom.cpp index 87b24799721..329f4cad913 100644 --- a/staging_vespalib/src/vespa/vespalib/util/doom.cpp +++ b/staging_vespalib/src/vespa/vespalib/util/doom.cpp @@ -4,8 +4,8 @@ namespace vespalib { -Doom::Doom(const vespalib::Clock &clock, fastos::SteadyTimeStamp softDoom, - fastos::SteadyTimeStamp hardDoom, bool explicitSoftDoom) +Doom::Doom(const vespalib::Clock &clock, steady_time softDoom, + steady_time hardDoom, bool explicitSoftDoom) : _clock(clock), _softDoom(softDoom), _hardDoom(hardDoom), diff --git a/staging_vespalib/src/vespa/vespalib/util/doom.h b/staging_vespalib/src/vespa/vespalib/util/doom.h index d85c3dc9084..dc3d124fc71 100644 --- a/staging_vespalib/src/vespa/vespalib/util/doom.h +++ b/staging_vespalib/src/vespa/vespalib/util/doom.h @@ -8,22 +8,22 @@ namespace vespalib { class Doom { public: - Doom(const vespalib::Clock &clock, fastos::SteadyTimeStamp doom) + Doom(const Clock &clock, steady_time doom) : Doom(clock, doom, doom, false) {} - Doom(const vespalib::Clock &clock, fastos::SteadyTimeStamp softDoom, - fastos::SteadyTimeStamp hardDoom, bool explicitSoftDoom); + Doom(const Clock &clock, vespalib::steady_time softDoom, + steady_time hardDoom, bool explicitSoftDoom); bool soft_doom() const { return (_clock.getTimeNSAssumeRunning() > _softDoom); } bool hard_doom() const { return (_clock.getTimeNSAssumeRunning() > _hardDoom); } - fastos::TimeStamp soft_left() const { return _softDoom - _clock.getTimeNS(); } - fastos::TimeStamp hard_left() const { return _hardDoom - _clock.getTimeNS(); } + duration soft_left() const { return _softDoom - _clock.getTimeNS(); } + duration hard_left() const { return _hardDoom - _clock.getTimeNS(); } bool isExplicitSoftDoom() const { return _isExplicitSoftDoom; } private: - const vespalib::Clock &_clock; - fastos::SteadyTimeStamp _softDoom; - fastos::SteadyTimeStamp _hardDoom; - bool _isExplicitSoftDoom; + const Clock &_clock; + steady_time _softDoom; + steady_time _hardDoom; + bool _isExplicitSoftDoom; }; } |