diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-11-14 22:21:37 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2019-11-14 22:21:37 +0000 |
commit | 37754e26c8f04eb5a0d130f65e44946726fa5490 (patch) | |
tree | cc5cc48c3e7c26cd4881c93909fb14ef256b9b09 | |
parent | 52a5a424000cb4a1bcc61e601c6f4a183103e3d1 (diff) |
Use steady clock to ensure monotonically increasing clock.
16 files changed, 48 insertions, 41 deletions
diff --git a/fastos/src/vespa/fastos/timestamp.h b/fastos/src/vespa/fastos/timestamp.h index c01f93205c0..fd46d200b52 100644 --- a/fastos/src/vespa/fastos/timestamp.h +++ b/fastos/src/vespa/fastos/timestamp.h @@ -71,9 +71,7 @@ public: void start() { _startTime = this->now(); _stopTime = _startTime; } void stop() { _stopTime = this->now(); } - TimeStamp elapsedAdjusted() const { return this->adjustTick2Sec(elapsed()); } TimeStamp startTime() const { return this->adjustTick2Sec(_startTime); } - TimeStamp stopTime() const { return this->adjustTick2Sec(_stopTime); } TimeStamp elapsed() const { TimeStamp diff(_stopTime - _startTime); diff --git a/searchcore/src/tests/proton/matching/sessionmanager_test.cpp b/searchcore/src/tests/proton/matching/sessionmanager_test.cpp index 349ac411042..78fe216cb9e 100644 --- a/searchcore/src/tests/proton/matching/sessionmanager_test.cpp +++ b/searchcore/src/tests/proton/matching/sessionmanager_test.cpp @@ -34,12 +34,11 @@ void checkStats(SessionManager::Stats stats, uint32_t numInsert, TEST("require that SessionManager handles SearchSessions.") { string session_id("foo"); + fastos::TimeStamp start(100); fastos::TimeStamp doom(1000); MatchToolsFactory::UP mtf; SearchSession::OwnershipBundle owned_objects; - SearchSession::SP session( - new SearchSession(session_id, doom, std::move(mtf), - std::move(owned_objects))); + auto session = std::make_shared<SearchSession>(session_id, start, doom, std::move(mtf), std::move(owned_objects)); SessionManager session_manager(10); TEST_DO(checkStats(session_manager.getSearchStats(), 0, 0, 0, 0, 0)); @@ -60,14 +59,15 @@ TEST("require that SessionManager handles SearchSessions.") { } TEST("require that SessionManager can be explored") { + fastos::TimeStamp start(100); fastos::TimeStamp doom(1000); SessionManager session_manager(10); - session_manager.insert(SearchSession::SP(new SearchSession("foo", doom, - MatchToolsFactory::UP(), SearchSession::OwnershipBundle()))); - session_manager.insert(SearchSession::SP(new SearchSession("bar", doom, - MatchToolsFactory::UP(), SearchSession::OwnershipBundle()))); - session_manager.insert(SearchSession::SP(new SearchSession("baz", doom, - MatchToolsFactory::UP(), SearchSession::OwnershipBundle()))); + session_manager.insert(std::make_shared<SearchSession>("foo", start, doom, + MatchToolsFactory::UP(), SearchSession::OwnershipBundle())); + session_manager.insert(std::make_shared<SearchSession>("bar", start, doom, + MatchToolsFactory::UP(), SearchSession::OwnershipBundle())); + session_manager.insert(std::make_shared<SearchSession>("baz", start, doom, + MatchToolsFactory::UP(), SearchSession::OwnershipBundle())); SessionManagerExplorer explorer(session_manager); EXPECT_EQUAL(std::vector<vespalib::string>({"search"}), explorer.get_children_names()); diff --git a/searchcore/src/vespa/searchcore/proton/matching/isessioncachepruner.h b/searchcore/src/vespa/searchcore/proton/matching/isessioncachepruner.h index 1e5532564d8..749b3a5fef0 100644 --- a/searchcore/src/vespa/searchcore/proton/matching/isessioncachepruner.h +++ b/searchcore/src/vespa/searchcore/proton/matching/isessioncachepruner.h @@ -4,8 +4,7 @@ #include <vespa/fastos/timestamp.h> -namespace proton { -namespace matching { +namespace proton::matching { struct ISessionCachePruner { virtual ~ISessionCachePruner() {} @@ -13,6 +12,4 @@ struct ISessionCachePruner { virtual void pruneTimedOutSessions(fastos::TimeStamp currentTime) = 0; }; -} // namespace proton::matching -} // namespace proton - +} diff --git a/searchcore/src/vespa/searchcore/proton/matching/matcher.cpp b/searchcore/src/vespa/searchcore/proton/matching/matcher.cpp index 87c0283d228..0e00220428b 100644 --- a/searchcore/src/vespa/searchcore/proton/matching/matcher.cpp +++ b/searchcore/src/vespa/searchcore/proton/matching/matcher.cpp @@ -130,7 +130,7 @@ Matcher::create_match_tools_factory(const search::engine::Request &request, ISea ? Factor::lookup(rankProperties, _stats.softDoomFactor()) : 0.95; int64_t safeLeft = request.getTimeLeft() * factor; - fastos::TimeStamp safeDoom(fastos::ClockSystem::now() + safeLeft); + fastos::TimeStamp safeDoom(_clock.getTimeNSAssumeRunning().val() + safeLeft); if (softTimeoutEnabled) { LOG(debug, "Soft-timeout computed factor=%1.3f, used factor=%1.3f, softTimeout=%" PRId64 " softDoom=%" PRId64 " hardDoom=%" PRId64, _stats.softDoomFactor(), factor, safeLeft, safeDoom.ns(), request.getTimeOfDoom().ns()); @@ -240,7 +240,7 @@ Matcher::match(const SearchRequest &request, vespalib::ThreadBundle &threadBundl : mtf->match_limiter().getDocIdSpaceEstimate(); uint32_t estHits = mtf->estimate().estHits; if (shouldCacheSearchSession && ((result->_numFs4Hits != 0) || shouldCacheGroupingSession)) { - auto session = std::make_shared<SearchSession>(sessionId, request.getTimeOfDoom(), + auto session = std::make_shared<SearchSession>(sessionId, request.getStartTime(), request.getTimeOfDoom(), std::move(mtf), std::move(owned_objects)); session->releaseEnumGuards(); sessionMgr.insert(std::move(session)); diff --git a/searchcore/src/vespa/searchcore/proton/matching/search_session.cpp b/searchcore/src/vespa/searchcore/proton/matching/search_session.cpp index a077edcc3fe..36576997950 100644 --- a/searchcore/src/vespa/searchcore/proton/matching/search_session.cpp +++ b/searchcore/src/vespa/searchcore/proton/matching/search_session.cpp @@ -5,11 +5,11 @@ namespace proton::matching { -SearchSession::SearchSession(const SessionId &id, fastos::TimeStamp time_of_doom, +SearchSession::SearchSession(const SessionId &id, fastos::TimeStamp create_time, fastos::TimeStamp time_of_doom, std::unique_ptr<MatchToolsFactory> match_tools_factory, OwnershipBundle &&owned_objects) : _session_id(id), - _create_time(fastos::ClockSystem::now()), + _create_time(create_time), _time_of_doom(time_of_doom), _owned_objects(std::move(owned_objects)), _match_tools_factory(std::move(match_tools_factory)) @@ -21,9 +21,9 @@ SearchSession::releaseEnumGuards() { _owned_objects.context->releaseEnumGuards(); } -SearchSession::~SearchSession() { } +SearchSession::~SearchSession() = default; SearchSession::OwnershipBundle::OwnershipBundle() = default; -SearchSession::OwnershipBundle::~OwnershipBundle() { } +SearchSession::OwnershipBundle::~OwnershipBundle() = default; } diff --git a/searchcore/src/vespa/searchcore/proton/matching/search_session.h b/searchcore/src/vespa/searchcore/proton/matching/search_session.h index 9bea3fdcc5a..f8b500f2d7b 100644 --- a/searchcore/src/vespa/searchcore/proton/matching/search_session.h +++ b/searchcore/src/vespa/searchcore/proton/matching/search_session.h @@ -43,7 +43,7 @@ private: public: typedef std::shared_ptr<SearchSession> SP; - SearchSession(const SessionId &id, fastos::TimeStamp time_of_doom, + SearchSession(const SessionId &id, fastos::TimeStamp create_time, fastos::TimeStamp time_of_doom, std::unique_ptr<MatchToolsFactory> match_tools_factory, OwnershipBundle &&owned_objects); ~SearchSession(); diff --git a/searchcore/src/vespa/searchcore/proton/matching/session_manager_explorer.cpp b/searchcore/src/vespa/searchcore/proton/matching/session_manager_explorer.cpp index e456ea5b5a2..bcc59dd9742 100644 --- a/searchcore/src/vespa/searchcore/proton/matching/session_manager_explorer.cpp +++ b/searchcore/src/vespa/searchcore/proton/matching/session_manager_explorer.cpp @@ -21,7 +21,7 @@ private: public: SearchSessionExplorer(const SessionManager &manager) : _manager(manager) {} - virtual void get_state(const vespalib::slime::Inserter &inserter, bool full) const override { + void get_state(const vespalib::slime::Inserter &inserter, bool full) const override { Cursor &state = inserter.insertObject(); state.setLong("numSessions", _manager.getNumSearchSessions()); if (full) { diff --git a/searchcore/src/vespa/searchcore/proton/server/prune_session_cache_job.cpp b/searchcore/src/vespa/searchcore/proton/server/prune_session_cache_job.cpp index ab78fd7f857..2826a8da377 100644 --- a/searchcore/src/vespa/searchcore/proton/server/prune_session_cache_job.cpp +++ b/searchcore/src/vespa/searchcore/proton/server/prune_session_cache_job.cpp @@ -1,16 +1,16 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "prune_session_cache_job.h" #include <vespa/fastos/timestamp.h> +#include <chrono> -using fastos::ClockSystem; using fastos::TimeStamp; +using namespace std::chrono; namespace proton { using matching::ISessionCachePruner; -PruneSessionCacheJob::PruneSessionCacheJob(ISessionCachePruner &pruner, - double jobInterval) +PruneSessionCacheJob::PruneSessionCacheJob(ISessionCachePruner &pruner, double jobInterval) : IMaintenanceJob("prune_session_cache", jobInterval, jobInterval), _pruner(pruner) { @@ -19,7 +19,7 @@ PruneSessionCacheJob::PruneSessionCacheJob(ISessionCachePruner &pruner, bool PruneSessionCacheJob::run() { - TimeStamp now(ClockSystem::now()); + TimeStamp now(duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count()); _pruner.pruneTimedOutSessions(now); return true; } diff --git a/searchlib/src/vespa/searchlib/engine/docsumrequest.cpp b/searchlib/src/vespa/searchlib/engine/docsumrequest.cpp index 33a2c437a80..0fc258363e5 100644 --- a/searchlib/src/vespa/searchlib/engine/docsumrequest.cpp +++ b/searchlib/src/vespa/searchlib/engine/docsumrequest.cpp @@ -9,7 +9,7 @@ DocsumRequest::DocsumRequest() {} DocsumRequest::DocsumRequest(bool useRootSlime_) - : DocsumRequest(RelativeTime(std::make_unique<FastosClock>()), useRootSlime_) + : DocsumRequest(RelativeTime(std::make_unique<SteadyClock>()), useRootSlime_) {} DocsumRequest::DocsumRequest(RelativeTime relativeTime, bool useRootSlime_) diff --git a/searchlib/src/vespa/searchlib/engine/proto_rpc_adapter.cpp b/searchlib/src/vespa/searchlib/engine/proto_rpc_adapter.cpp index 46f3ae0b4c7..4fa7079fe46 100644 --- a/searchlib/src/vespa/searchlib/engine/proto_rpc_adapter.cpp +++ b/searchlib/src/vespa/searchlib/engine/proto_rpc_adapter.cpp @@ -84,7 +84,7 @@ struct SearchRequestDecoder : SearchRequest::Source::Decoder { QueryStats &stats; RelativeTime relative_time; SearchRequestDecoder(FRT_RPCRequest &rpc_in, QueryStats &stats_in) - : rpc(rpc_in), stats(stats_in), relative_time(std::make_unique<FastosClock>()) {} + : rpc(rpc_in), stats(stats_in), relative_time(std::make_unique<SteadyClock>()) {} std::unique_ptr<SearchRequest> decode() override { ProtoSearchRequest msg; stats.request_size = (*rpc.GetParams())[2]._data._len; @@ -129,7 +129,7 @@ struct DocsumRequestDecoder : DocsumRequest::Source::Decoder { DocsumStats &stats; RelativeTime relative_time; DocsumRequestDecoder(FRT_RPCRequest &rpc_in, DocsumStats &stats_in) - : rpc(rpc_in), stats(stats_in), relative_time(std::make_unique<FastosClock>()) {} + : rpc(rpc_in), stats(stats_in), relative_time(std::make_unique<SteadyClock>()) {} std::unique_ptr<DocsumRequest> decode() override { ProtoDocsumRequest msg; stats.request_size = (*rpc.GetParams())[2]._data._len; diff --git a/searchlib/src/vespa/searchlib/engine/searchrequest.cpp b/searchlib/src/vespa/searchlib/engine/searchrequest.cpp index 9ca89a8636f..b9f3e62afd7 100644 --- a/searchlib/src/vespa/searchlib/engine/searchrequest.cpp +++ b/searchlib/src/vespa/searchlib/engine/searchrequest.cpp @@ -5,7 +5,7 @@ namespace search::engine { SearchRequest::SearchRequest() - : SearchRequest(RelativeTime(std::make_unique<FastosClock>())) {} + : SearchRequest(RelativeTime(std::make_unique<SteadyClock>())) {} SearchRequest::SearchRequest(RelativeTime relativeTime) : Request(std::move(relativeTime)), diff --git a/searchlib/src/vespa/searchlib/engine/trace.cpp b/searchlib/src/vespa/searchlib/engine/trace.cpp index f9564846104..d95d07e0e98 100644 --- a/searchlib/src/vespa/searchlib/engine/trace.cpp +++ b/searchlib/src/vespa/searchlib/engine/trace.cpp @@ -3,8 +3,14 @@ #include "trace.h" #include <vespa/vespalib/data/slime/slime.h> +using namespace std::chrono; + namespace search::engine { +fastos::TimeStamp SteadyClock::now() const { + return duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count(); +} + RelativeTime::RelativeTime(std::unique_ptr<Clock> clock) : _start(clock->now()), _clock(std::move(clock)) diff --git a/searchlib/src/vespa/searchlib/engine/trace.h b/searchlib/src/vespa/searchlib/engine/trace.h index 518485f8775..962cac8c49d 100644 --- a/searchlib/src/vespa/searchlib/engine/trace.h +++ b/searchlib/src/vespa/searchlib/engine/trace.h @@ -21,6 +21,11 @@ public: fastos::TimeStamp now() const override { return fastos::ClockSystem::now(); } }; +class SteadyClock : public Clock { +public: + fastos::TimeStamp now() const override; +}; + class CountingClock : public Clock { public: CountingClock(int64_t start, int64_t increment) : _increment(increment), _nextTime(start) { } diff --git a/staging_vespalib/src/vespa/vespalib/util/clock.cpp b/staging_vespalib/src/vespa/vespalib/util/clock.cpp index c9768417914..10d075a21bd 100644 --- a/staging_vespalib/src/vespa/vespalib/util/clock.cpp +++ b/staging_vespalib/src/vespa/vespalib/util/clock.cpp @@ -5,6 +5,7 @@ #include <chrono> using namespace fastos; +using namespace std::chrono; namespace vespalib { @@ -27,7 +28,7 @@ Clock::~Clock() void Clock::setTime() const { - _timeNS = ClockSystem::adjustTick2Sec(ClockSystem::now()); + _timeNS = duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count(); } void Clock::Run(FastOS_ThreadInterface *thread, void *arguments) @@ -37,13 +38,13 @@ void Clock::Run(FastOS_ThreadInterface *thread, void *arguments) std::unique_lock<std::mutex> guard(_lock); while ( ! thread->GetBreakFlag() && !_stop) { setTime(); - _cond.wait_for(guard, std::chrono::milliseconds(_timePeriodMS)); + _cond.wait_for(guard, milliseconds(_timePeriodMS)); } _running = false; } void -Clock::stop(void) +Clock::stop() { std::lock_guard<std::mutex> guard(_lock); _stop = true; diff --git a/staging_vespalib/src/vespa/vespalib/util/clock.h b/staging_vespalib/src/vespa/vespalib/util/clock.h index 75f407ec95d..b44b1454cd4 100644 --- a/staging_vespalib/src/vespa/vespalib/util/clock.h +++ b/staging_vespalib/src/vespa/vespalib/util/clock.h @@ -21,11 +21,11 @@ private: Clock & operator = (const Clock &); mutable fastos::TimeStamp _timeNS; - int _timePeriodMS; - std::mutex _lock; - std::condition_variable _cond; - bool _stop; - bool _running; + int _timePeriodMS; + std::mutex _lock; + std::condition_variable _cond; + bool _stop; + bool _running; void setTime() const; diff --git a/staging_vespalib/src/vespa/vespalib/util/doom.h b/staging_vespalib/src/vespa/vespalib/util/doom.h index 3b7259f891d..623cc23fcc8 100644 --- a/staging_vespalib/src/vespa/vespalib/util/doom.h +++ b/staging_vespalib/src/vespa/vespalib/util/doom.h @@ -2,7 +2,7 @@ #pragma once -#include <vespa/vespalib/util/clock.h> +#include "clock.h" namespace vespalib { |