diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-02-28 21:35:51 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2023-03-01 17:01:45 +0000 |
commit | 488ef94f55b4cc9d1430d02867771d7fd60472fd (patch) | |
tree | 443c68228b1772ff0e2c318c5434dbed5117a5a2 | |
parent | e98ea0903b425c5b118d38ddd63a3165d024adb6 (diff) |
Use type safe time inside metric manager.
-rw-r--r-- | metrics/src/tests/snapshottest.cpp | 5 | ||||
-rw-r--r-- | metrics/src/vespa/metrics/metricmanager.cpp | 34 | ||||
-rw-r--r-- | metrics/src/vespa/metrics/metricmanager.h | 4 | ||||
-rw-r--r-- | metrics/src/vespa/metrics/metricsnapshot.h | 1 |
4 files changed, 21 insertions, 23 deletions
diff --git a/metrics/src/tests/snapshottest.cpp b/metrics/src/tests/snapshottest.cpp index 4d2ea96c36d..52bcaf600ca 100644 --- a/metrics/src/tests/snapshottest.cpp +++ b/metrics/src/tests/snapshottest.cpp @@ -4,7 +4,6 @@ #include <vespa/metrics/metrics.h> #include <vespa/metrics/summetric.hpp> #include <vespa/vespalib/gtest/gtest.h> -#include <vespa/vespalib/util/size_literals.h> namespace metrics { @@ -166,8 +165,8 @@ void ASSERT_VALUE(int32_t value, const MetricSnapshot & snapshot, const char *na } struct SnapshotTest : public ::testing::Test { - time_t tick(MetricManager& mgr, time_t currentTime) { - return mgr.tick(mgr.getMetricLock(), currentTime); + void tick(MetricManager& mgr, time_t currentTime) { + mgr.tick(mgr.getMetricLock(), time_point(vespalib::from_s(currentTime))); } }; diff --git a/metrics/src/vespa/metrics/metricmanager.cpp b/metrics/src/vespa/metrics/metricmanager.cpp index e1bc2f14e1b..c25c6a31fe7 100644 --- a/metrics/src/vespa/metrics/metricmanager.cpp +++ b/metrics/src/vespa/metrics/metricmanager.cpp @@ -645,7 +645,7 @@ MetricManager::updateMetrics(bool includeSnapshotOnlyHooks) // Ensure we're not in the way of the background thread MetricLockGuard sync(_waiter); LOG(debug, "Giving %zu periodic update hooks.", _periodicUpdateHooks.size()); - updatePeriodicMetrics(sync, 0, true); + updatePeriodicMetrics(sync, time_point(), true); if (includeSnapshotOnlyHooks) { LOG(debug, "Giving %zu snapshot update hooks.", _snapshotUpdateHooks.size()); updateSnapshotMetrics(sync); @@ -653,13 +653,12 @@ MetricManager::updateMetrics(bool includeSnapshotOnlyHooks) } // When this is called, the thread monitor lock has already been grabbed -time_t -MetricManager::updatePeriodicMetrics(const MetricLockGuard & guard, time_t updateTimeS, bool outOfSchedule) +time_point +MetricManager::updatePeriodicMetrics(const MetricLockGuard & guard, time_point updateTime, bool outOfSchedule) { assertMetricLockLocked(guard); - time_t nextUpdateTime = std::numeric_limits<time_t>::max(); + time_point nextUpdateTime = time_point::max(); time_point preTime = _timer->getTimeInMilliSecs(); - time_point updateTime = time_point(from_s(updateTimeS)); for (auto hook : _periodicUpdateHooks) { if (hook->expired(updateTime)) { hook->updateMetrics(guard); @@ -683,7 +682,7 @@ MetricManager::updatePeriodicMetrics(const MetricLockGuard & guard, time_t updat _periodicHookLatency.addValue(count_ms(postTime - preTime)); preTime = postTime; } - nextUpdateTime = std::min(nextUpdateTime, count_s(hook->getNextCall().time_since_epoch())); + nextUpdateTime = std::min(nextUpdateTime, hook->getNextCall()); } return nextUpdateTime; } @@ -747,10 +746,9 @@ MetricManager::run() _snapshots[0]->getSnapshot().setToTime(currentTime); while (!stop_requested()) { currentTime = _timer->getTime(); - time_t currentTimeS = count_s(currentTime.time_since_epoch()); - time_t next = tick(sync, currentTimeS); - if (currentTimeS < next) { - vespalib::duration wait_time = from_s(next - currentTimeS); + time_point next = tick(sync, currentTime); + if (currentTime < next) { + vespalib::duration wait_time = next - currentTime; _cond.wait_for(sync, wait_time); _sleepTimes.addValue(count_ms(wait_time)); } else { @@ -759,10 +757,10 @@ MetricManager::run() } } -time_t -MetricManager::tick(const MetricLockGuard & guard, time_t currentTime) +time_point +MetricManager::tick(const MetricLockGuard & guard, time_point currentTime) { - LOG(spam, "Worker thread starting to process for time %" PRIu64 ".", static_cast<uint64_t>(currentTime)); + LOG(spam, "Worker thread starting to process for time %s.", to_string(currentTime).c_str()); // Check for new config and reconfigure if (_configSubscriber && _configSubscriber->nextConfigNow()) { @@ -775,8 +773,8 @@ MetricManager::tick(const MetricLockGuard & guard, time_t currentTime) checkMetricsAltered(guard); // Set next work time to the time we want to take next snapshot. - time_t nextWorkTime = count_s(_snapshots[0]->getToTime().time_since_epoch()) + _snapshots[0]->getPeriod(); - time_t nextUpdateHookTime; + time_point nextWorkTime = _snapshots[0]->getNextWorkTime(); + time_point nextUpdateHookTime; if (nextWorkTime <= currentTime) { // If taking a new snapshot or logging, force calls to all // update hooks. @@ -790,12 +788,12 @@ MetricManager::tick(const MetricLockGuard & guard, time_t currentTime) nextUpdateHookTime = updatePeriodicMetrics(guard, currentTime, false); } // Do snapshotting if it is time - if (nextWorkTime <= currentTime) takeSnapshots(guard, system_time(from_s(nextWorkTime))); + if (nextWorkTime <= currentTime) takeSnapshots(guard, nextWorkTime); - _lastProcessedTime.store(nextWorkTime <= currentTime ? nextWorkTime : currentTime, std::memory_order_relaxed); + _lastProcessedTime.store(count_s((nextWorkTime <= currentTime ? nextWorkTime : currentTime).time_since_epoch()), std::memory_order_relaxed); LOG(spam, "Worker thread done with processing for time %" PRIu64 ".", static_cast<uint64_t>(_lastProcessedTime.load(std::memory_order_relaxed))); - time_t next = count_s(_snapshots[0]->getToTime().time_since_epoch()) + _snapshots[0]->getPeriod(); + time_point next = _snapshots[0]->getNextWorkTime(); next = std::min(next, nextUpdateHookTime); return next; } diff --git a/metrics/src/vespa/metrics/metricmanager.h b/metrics/src/vespa/metrics/metricmanager.h index e793ef80b7f..d771081c1cc 100644 --- a/metrics/src/vespa/metrics/metricmanager.h +++ b/metrics/src/vespa/metrics/metricmanager.h @@ -274,7 +274,7 @@ private: void configure(const MetricLockGuard & guard, std::unique_ptr<MetricsmanagerConfig> conf); void run(); - time_t tick(const MetricLockGuard & guard, time_t currentTime); + time_point tick(const MetricLockGuard & guard, time_point currentTime); /** * Utility function for updating periodic metrics. * @@ -284,7 +284,7 @@ private: * without adjusting schedule for next update. * @return Time of next hook to be called in the future. */ - time_t updatePeriodicMetrics(const MetricLockGuard & guard, time_t updateTime, bool outOfSchedule); + time_point updatePeriodicMetrics(const MetricLockGuard & guard, time_point updateTime, bool outOfSchedule); void updateSnapshotMetrics(const MetricLockGuard & guard); void handleMetricsAltered(const MetricLockGuard & guard); diff --git a/metrics/src/vespa/metrics/metricsnapshot.h b/metrics/src/vespa/metrics/metricsnapshot.h index 16b46760957..6bb37f8223d 100644 --- a/metrics/src/vespa/metrics/metricsnapshot.h +++ b/metrics/src/vespa/metrics/metricsnapshot.h @@ -90,6 +90,7 @@ public: uint32_t getPeriod() const { return _current->getPeriod(); } system_time getFromTime() const { return _current->getFromTime(); } system_time getToTime() const { return _current->getToTime(); } + system_time getNextWorkTime() const { return getToTime() + vespalib::from_s(getPeriod()); } uint32_t getCount() const { return _count; } uint32_t getBuilderCount() const { return _builderCount; } MetricSnapshot& getSnapshot(bool temporary = false) { |