diff options
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | vespalib/src/tests/clock/.gitignore | 5 | ||||
-rw-r--r-- | vespalib/src/tests/clock/CMakeLists.txt | 14 | ||||
-rw-r--r-- | vespalib/src/tests/clock/clock_benchmark.cpp | 170 | ||||
-rw-r--r-- | vespalib/src/tests/clock/clock_test.cpp | 29 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/CMakeLists.txt | 1 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/clock.cpp | 15 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/clock.h | 34 |
8 files changed, 0 insertions, 269 deletions
diff --git a/vespalib/CMakeLists.txt b/vespalib/CMakeLists.txt index 83711ab8200..49a4cca1e1c 100644 --- a/vespalib/CMakeLists.txt +++ b/vespalib/CMakeLists.txt @@ -42,7 +42,6 @@ vespa_define_module( src/tests/btree/btree-scan-speed src/tests/btree/btree-stress src/tests/btree/btree_store - src/tests/clock src/tests/component src/tests/compress src/tests/compression diff --git a/vespalib/src/tests/clock/.gitignore b/vespalib/src/tests/clock/.gitignore deleted file mode 100644 index 96861fcc5d3..00000000000 --- a/vespalib/src/tests/clock/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -.depend -Makefile -clock_test -vespalib_clock_test_app -vespalib_clock_benchmark_app diff --git a/vespalib/src/tests/clock/CMakeLists.txt b/vespalib/src/tests/clock/CMakeLists.txt deleted file mode 100644 index 55c4ca55299..00000000000 --- a/vespalib/src/tests/clock/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -vespa_add_executable(vespalib_clock_benchmark_app TEST - SOURCES - clock_benchmark.cpp - DEPENDS - vespalib -) -vespa_add_executable(vespalib_clock_test_app TEST - SOURCES - clock_test.cpp - DEPENDS - vespalib -) -vespa_add_test(NAME vespalib_clock_test_app COMMAND vespalib_clock_test_app) diff --git a/vespalib/src/tests/clock/clock_benchmark.cpp b/vespalib/src/tests/clock/clock_benchmark.cpp deleted file mode 100644 index 81a228f820f..00000000000 --- a/vespalib/src/tests/clock/clock_benchmark.cpp +++ /dev/null @@ -1,170 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#include <vespa/vespalib/util/clock.h> -#include <vespa/vespalib/util/invokeserviceimpl.h> -#include <cassert> -#include <vector> -#include <atomic> -#include <cinttypes> -#include <cstring> -#include <condition_variable> -#include <mutex> -#include <thread> - -using vespalib::Clock; -using vespalib::steady_time; -using vespalib::steady_clock; -using vespalib::duration; -using vespalib::to_s; - -struct UpdateClock { - virtual ~UpdateClock() {} - virtual void update() = 0; -}; - -struct NSValue : public UpdateClock { - void update() override { _value = std::chrono::steady_clock::now().time_since_epoch().count(); } - int64_t _value; -}; - -struct NSVolatile : public UpdateClock { - void update() override { _value = std::chrono::steady_clock::now().time_since_epoch().count(); } - volatile int64_t _value; -}; -struct NSAtomic : public UpdateClock { - void update() override { _value.store(std::chrono::steady_clock::now().time_since_epoch().count()); } - std::atomic<int64_t> _value; -}; - -class TestClock -{ -private: - int _timePeriodMS; - std::mutex _lock; - std::condition_variable _cond; - UpdateClock &_clock; - bool _stop; - std::thread _thread; - - void run(); - -public: - TestClock(UpdateClock & clock, double timePeriod) - : _timePeriodMS(static_cast<uint32_t>(timePeriod*1000)), - _lock(), - _cond(), - _clock(clock), - _stop(false), - _thread() - { - _thread = std::thread([this](){run();}); - } - ~TestClock() { - { - std::lock_guard<std::mutex> guard(_lock); - _stop = true; - _cond.notify_all(); - } - _thread.join(); - } -}; - -void TestClock::run() -{ - std::unique_lock<std::mutex> guard(_lock); - while (!_stop) { - _clock.update(); - _cond.wait_for(guard, std::chrono::milliseconds(_timePeriodMS)); - } -} - -template<typename Func> -struct Sampler { - Sampler(Func func, uint64_t samples) - : _samples(samples), - _count(), - _func(func), - _thread() - { - memset(_count, 0, sizeof(_count)); - _thread = std::thread([this](){run();}); - } - void run() { - steady_time prev = _func(); - for (uint64_t samples = 0; samples < _samples; ++samples) { - 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)]++; - } - } - uint64_t _samples; - uint64_t _count[3]; - Func _func; - std::thread _thread; -}; - -template<typename Func> -void benchmark(const char * desc, uint64_t samples, uint32_t numThreads, Func func) { - std::vector<std::unique_ptr<Sampler<Func>>> threads; - threads.reserve(numThreads); - steady_time start = steady_clock::now(); - for (uint32_t i(0); i < numThreads; i++) { - threads.push_back(std::make_unique<Sampler<Func>>(func, samples)); - } - uint64_t count[3]; - memset(count, 0, sizeof(count)); - for (const auto & sampler : threads) { - sampler->_thread.join(); - for (uint32_t i(0); i < 3; i++) { - count[i] += sampler->_count[i]; - } - } - printf("%s: Took %" PRId64 " clock samples in %2.3f with [%" PRId64 ", %" PRId64 ", %" PRId64 "] counts\n", desc, samples, to_s(steady_clock::now() - start), count[0], count[1], count[2]); -} - -int -main(int argc, char *argv[]) -{ - if (argc != 4) { - fprintf(stderr, "usage: %s <frequency> <numThreads> <samples>\n", argv[0]); - return 1; - } - uint64_t frequency = atoll(argv[1]); - uint32_t numThreads = atoi(argv[2]); - uint64_t samples = atoll(argv[3]); - NSValue nsValue; - NSVolatile nsVolatile; - NSAtomic nsAtomic; - vespalib::InvokeServiceImpl invoker(vespalib::from_s(1.0/frequency)); - Clock clock(invoker.nowRef()); - TestClock nsClock(nsValue, 1.0/frequency); - TestClock nsVolatileClock(nsVolatile, 1.0/frequency); - TestClock nsAtomicClock(nsAtomic, 1.0/frequency); - - benchmark("vespalib::Clock", samples, numThreads, [&clock]() { - return clock.getTimeNS(); - }); - benchmark("uint64_t", samples, numThreads, [&nsValue]() { - return steady_time (duration(nsValue._value)); - }); - benchmark("volatile uint64_t", samples, numThreads, [&nsVolatile]() { - return steady_time(duration(nsVolatile._value)); - }); - benchmark("memory_order_relaxed", samples, numThreads, [&nsAtomic]() { - return steady_time(duration(nsAtomic._value.load(std::memory_order_relaxed))); - }); - benchmark("memory_order_consume", samples, numThreads, [&nsAtomic]() { - return steady_time(duration(nsAtomic._value.load(std::memory_order_consume))); - }); - benchmark("memory_order_acquire", samples, numThreads, [&nsAtomic]() { - return steady_time(duration(nsAtomic._value.load(std::memory_order_acquire))); - }); - benchmark("memory_order_seq_cst", samples, numThreads, [&nsAtomic]() { - return steady_time(duration(nsAtomic._value.load(std::memory_order_seq_cst))); - }); - benchmark("vespalib::steady_time::now()", samples, numThreads, []() { - return steady_clock::now(); - }); - return 0; -} diff --git a/vespalib/src/tests/clock/clock_test.cpp b/vespalib/src/tests/clock/clock_test.cpp deleted file mode 100644 index 667035bdbde..00000000000 --- a/vespalib/src/tests/clock/clock_test.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#include <vespa/vespalib/testkit/testapp.h> -#include <vespa/vespalib/util/clock.h> -#include <vespa/vespalib/util/invokeserviceimpl.h> -#include <thread> - -using vespalib::Clock; -using vespalib::duration; -using vespalib::steady_time; -using vespalib::steady_clock; - -void waitForMovement(steady_time start, Clock & clock, vespalib::duration timeout) { - steady_time startOsClock = steady_clock::now(); - while ((clock.getTimeNS() <= start) && ((steady_clock::now() - startOsClock) < timeout)) { - std::this_thread::sleep_for(1ms); - } -} - -TEST("Test that clock is ticking forward") { - vespalib::InvokeServiceImpl invoker(50ms); - Clock clock(invoker.nowRef()); - steady_time start = clock.getTimeNS(); - waitForMovement(start, clock, 10s); - steady_time stop = clock.getTimeNS(); - EXPECT_TRUE(stop > start); -} - -TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/vespalib/src/vespa/vespalib/util/CMakeLists.txt b/vespalib/src/vespa/vespalib/util/CMakeLists.txt index e69152a443c..518cbe337fa 100644 --- a/vespalib/src/vespa/vespalib/util/CMakeLists.txt +++ b/vespalib/src/vespa/vespalib/util/CMakeLists.txt @@ -17,7 +17,6 @@ vespa_add_library(vespalib_vespalib_util OBJECT box.cpp cgroup_resource_limits.cpp classname.cpp - clock.cpp compress.cpp compressor.cpp count_down_latch.cpp diff --git a/vespalib/src/vespa/vespalib/util/clock.cpp b/vespalib/src/vespa/vespalib/util/clock.cpp deleted file mode 100644 index 7bfefa9906f..00000000000 --- a/vespalib/src/vespa/vespalib/util/clock.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#include "clock.h" -#include <cassert> -namespace vespalib { - -Clock::Clock(const std::atomic<steady_time> & source) noexcept - : _timeNS(source) -{ - static_assert(std::atomic<steady_time>::is_always_lock_free); -} - -Clock::~Clock() = default; - -} diff --git a/vespalib/src/vespa/vespalib/util/clock.h b/vespalib/src/vespa/vespalib/util/clock.h deleted file mode 100644 index ab4671f7bfb..00000000000 --- a/vespalib/src/vespa/vespalib/util/clock.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#pragma once - -#include <vespa/vespalib/util/time.h> -#include <atomic> -#include <memory> - -namespace vespalib { - -/** - * TODO Remove abstraction. Can use atomic ref directly - * It is intended used where you want to check the time with low cost, but where - * resolution is not that important. - */ - -class Clock -{ -private: - const std::atomic<steady_time> &_timeNS; -public: - 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 noexcept { - return vespalib::steady_time(_timeNS.load(std::memory_order_relaxed)); - } - const std::atomic<steady_time> & nowRef() const { return _timeNS; } -}; - -} |