aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-08-28 13:26:44 +0200
committerGitHub <noreply@github.com>2019-08-28 13:26:44 +0200
commit3da400b8c6e95b139a43d12b362f7eddb0aacd87 (patch)
tree8b0b6fda28629ff1250769e5338e8707b0755ffc
parent2fbebc6f826294f3f48089b7cb75bb785ba25935 (diff)
parent5348c1c5a9e72031f27dcf39d456036a911bcfcd (diff)
Merge pull request #10404 from vespa-engine/balder/avoid-time-calls
Avoid calling time(nullptr).
-rw-r--r--config/src/vespa/config/frt/frtconnection.cpp15
-rw-r--r--config/src/vespa/config/frt/frtconnection.h4
-rw-r--r--config/src/vespa/config/frt/frtconnectionpool.cpp8
-rw-r--r--configd/src/apps/sentinel/config-handler.cpp2
-rw-r--r--configd/src/apps/sentinel/metrics.cpp5
-rw-r--r--configd/src/apps/sentinel/service.cpp7
-rw-r--r--fastos/src/vespa/fastos/timestamp.cpp7
-rw-r--r--fastos/src/vespa/fastos/timestamp.h3
-rw-r--r--logd/src/logd/watcher.cpp6
-rw-r--r--metrics/src/vespa/metrics/metricmanager.cpp3
-rw-r--r--metrics/src/vespa/metrics/state_api_adapter.cpp22
-rw-r--r--slobrok/src/vespa/slobrok/server/metrics_producer.cpp16
-rw-r--r--slobrok/src/vespa/slobrok/server/metrics_producer.h2
-rw-r--r--storage/src/vespa/storage/config/distributorconfiguration.cpp10
-rw-r--r--storage/src/vespa/storage/tools/throttlingsim.cpp6
-rw-r--r--vespalib/src/vespa/vespalib/util/random.cpp3
-rw-r--r--vespalog/src/logger/runserver.cpp29
17 files changed, 89 insertions, 59 deletions
diff --git a/config/src/vespa/config/frt/frtconnection.cpp b/config/src/vespa/config/frt/frtconnection.cpp
index bcdec9a4537..702cc06d29d 100644
--- a/config/src/vespa/config/frt/frtconnection.cpp
+++ b/config/src/vespa/config/frt/frtconnection.cpp
@@ -27,16 +27,16 @@ FRTConnection::FRTConnection(const vespalib::string& address, FRT_Supervisor& su
FRTConnection::~FRTConnection()
{
- if (_target != NULL) {
+ if (_target != nullptr) {
_target->SubRef();
- _target = NULL;
+ _target = nullptr;
}
}
FRT_Target *
FRTConnection::getTarget()
{
- if (_target == NULL) {
+ if (_target == nullptr) {
_target = _supervisor.GetTarget(_address.c_str());
} else if ( ! _target->IsValid()) {
_target->SubRef();
@@ -102,8 +102,7 @@ void FRTConnection::calculateSuspension(ErrorType type)
}
break;
}
- int64_t now = time(0);
- now *= 1000;
+ int64_t now = milliSecsSinceEpoch();
_suspendedUntil = now + delay;
if (_suspendWarned < (now - 5000)) {
char date[32];
@@ -121,4 +120,10 @@ FRTConnection::allocRPCRequest() {
return _supervisor.AllocRPCRequest();
}
+using namespace std::chrono;
+int64_t
+FRTConnection::milliSecsSinceEpoch() {
+ return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
+}
+
}
diff --git a/config/src/vespa/config/frt/frtconnection.h b/config/src/vespa/config/frt/frtconnection.h
index df57cfae92f..bde3e79b83c 100644
--- a/config/src/vespa/config/frt/frtconnection.h
+++ b/config/src/vespa/config/frt/frtconnection.h
@@ -34,7 +34,7 @@ public:
enum ErrorType { TRANSIENT, FATAL };
FRTConnection(const vespalib::string & address, FRT_Supervisor & supervisor, const TimingValues & timingValues);
- ~FRTConnection();
+ ~FRTConnection() override;
FRT_RPCRequest * allocRPCRequest() override;
void invoke(FRT_RPCRequest * req, double timeout, FRT_IRequestWait * waiter) override;
@@ -48,7 +48,7 @@ public:
void setTransientDelay(int64_t delay) override { _transientDelay = delay; }
int64_t getFatalDelay() { return _fatalDelay; }
int64_t getMaxFatalDelay() { return getFatalDelay() * 6; }
- void setFatalDelay(int64_t delay) { _fatalDelay = delay; }
+ static int64_t milliSecsSinceEpoch();
};
} // namespace config
diff --git a/config/src/vespa/config/frt/frtconnectionpool.cpp b/config/src/vespa/config/frt/frtconnectionpool.cpp
index b7440ceb7f0..5dccf033ab7 100644
--- a/config/src/vespa/config/frt/frtconnectionpool.cpp
+++ b/config/src/vespa/config/frt/frtconnectionpool.cpp
@@ -102,14 +102,15 @@ FRTConnectionPool::getNextHashBased()
return nextFRTConnection;
}
+
+
const std::vector<FRTConnection *> &
FRTConnectionPool::getReadySources(std::vector<FRTConnection*> & readySources) const
{
readySources.clear();
for (const auto & entry : _connections) {
FRTConnection* source = entry.second.get();
- int64_t tnow = time(0);
- tnow *= 1000;
+ int64_t tnow = FRTConnection::milliSecsSinceEpoch();
int64_t timestamp = tnow;
if (source->getSuspendedUntil() < timestamp) {
readySources.push_back(source);
@@ -124,8 +125,7 @@ FRTConnectionPool::getSuspendedSources(std::vector<FRTConnection*> & suspendedSo
suspendedSources.clear();
for (const auto & entry : _connections) {
FRTConnection* source = entry.second.get();
- int64_t tnow = time(0);
- tnow *= 1000;
+ int64_t tnow = FRTConnection::milliSecsSinceEpoch();
int64_t timestamp = tnow;
if (source->getSuspendedUntil() >= timestamp) {
suspendedSources.push_back(source);
diff --git a/configd/src/apps/sentinel/config-handler.cpp b/configd/src/apps/sentinel/config-handler.cpp
index 0f624da2bf0..a73a5f01f33 100644
--- a/configd/src/apps/sentinel/config-handler.cpp
+++ b/configd/src/apps/sentinel/config-handler.cpp
@@ -44,7 +44,7 @@ ConfigHandler::ConfigHandler()
_startMetrics(),
_stateApi()
{
- _startMetrics.startedTime = time(nullptr);
+ _startMetrics.startedTime = fastos::time();
}
ConfigHandler::~ConfigHandler()
diff --git a/configd/src/apps/sentinel/metrics.cpp b/configd/src/apps/sentinel/metrics.cpp
index 812ab56cd15..bb10c94ae12 100644
--- a/configd/src/apps/sentinel/metrics.cpp
+++ b/configd/src/apps/sentinel/metrics.cpp
@@ -2,6 +2,7 @@
#include "metrics.h"
#include <vespa/vespalib/metrics/simple_metrics.h>
+#include <vespa/fastos/timestamp.h>
namespace config::sentinel {
@@ -13,7 +14,7 @@ StartMetrics::StartMetrics()
producer(metrics),
currentlyRunningServices(0),
totalRestartsCounter(0),
- startedTime(time(nullptr)),
+ startedTime(fastos::time()),
sentinel_restarts(metrics->counter("sentinel.restarts",
"how many times sentinel restarted a service")),
sentinel_totalRestarts(metrics->gauge("sentinel.totalRestarts",
@@ -32,7 +33,7 @@ StartMetrics::~StartMetrics() = default;
void
StartMetrics::maybeLog()
{
- uint32_t curTime = time(nullptr);
+ uint32_t curTime = fastos::time();
sentinel_totalRestarts.sample(totalRestartsCounter);
sentinel_running.sample(currentlyRunningServices);
sentinel_uptime.sample(curTime - startedTime);
diff --git a/configd/src/apps/sentinel/service.cpp b/configd/src/apps/sentinel/service.cpp
index 941ea690024..22f9681a2ff 100644
--- a/configd/src/apps/sentinel/service.cpp
+++ b/configd/src/apps/sentinel/service.cpp
@@ -4,6 +4,7 @@
#include "output-connection.h"
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/signalhandler.h>
+#include <vespa/fastos/timestamp.h>
#include <csignal>
#include <unistd.h>
@@ -156,7 +157,7 @@ Service::start()
LOG(warning, "tried to start '%s' in REMOVING state", name().c_str());
return;
}
- time_t now = time(0);
+ time_t now = fastos::time();
_last_start = now;
// make a pipe, close the good ends of it, mark it close-on-exec
@@ -322,7 +323,7 @@ Service::youExited(int status)
if (! expectedDeath) {
// make sure the service does not restart in a tight loop:
- time_t now = time(0);
+ time_t now = fastos::time();
unsigned int diff = now - _last_start;
if (diff < MAX_RESTART_PENALTY) {
incrementRestartPenalty();
@@ -420,7 +421,7 @@ bool
Service::wantsRestart() const
{
if (_state == RESTARTING) {
- time_t now = time(0);
+ time_t now = fastos::time();
if (now > _last_start + _restartPenalty) {
return true;
}
diff --git a/fastos/src/vespa/fastos/timestamp.cpp b/fastos/src/vespa/fastos/timestamp.cpp
index a662dd32a21..bc865448e5a 100644
--- a/fastos/src/vespa/fastos/timestamp.cpp
+++ b/fastos/src/vespa/fastos/timestamp.cpp
@@ -1,8 +1,11 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "timestamp.h"
+#include <chrono>
#include <cmath>
#include <sys/time.h>
+using namespace std::chrono;
+
namespace fastos {
const TimeStamp::TimeT TimeStamp::MILLI;
@@ -39,4 +42,8 @@ int64_t ClockSystem::now()
return ns;
}
+time_t time() {
+ return system_clock::to_time_t(system_clock::now());
+}
+
}
diff --git a/fastos/src/vespa/fastos/timestamp.h b/fastos/src/vespa/fastos/timestamp.h
index 06bf7af922c..c01f93205c0 100644
--- a/fastos/src/vespa/fastos/timestamp.h
+++ b/fastos/src/vespa/fastos/timestamp.h
@@ -84,6 +84,9 @@ private:
TimeStamp _stopTime;
};
+time_t time();
+
+
typedef StopWatchT<ClockSystem> TickStopWatch;
typedef TickStopWatch StopWatch;
diff --git a/logd/src/logd/watcher.cpp b/logd/src/logd/watcher.cpp
index 103432e0136..1ef9288f4da 100644
--- a/logd/src/logd/watcher.cpp
+++ b/logd/src/logd/watcher.cpp
@@ -236,7 +236,7 @@ Watcher::watchfile()
already.st_dev = sb.st_dev;
already.st_ino = sb.st_ino;
- time_t now = time(nullptr);
+ time_t now = fastos::time();
bool wantrotate = (now > created + _confsubscriber.getRotateAge())
|| (sb.st_size > _confsubscriber.getRotateSize());
@@ -351,10 +351,10 @@ Watcher::removeOldLogs(const char *prefix)
}
if (S_ISREG(sb.st_mode)) {
if (sb.st_mtime +
- _confsubscriber.getRemoveAge() * 86400 < time(nullptr))
+ _confsubscriber.getRemoveAge() * 86400 < fastos::time())
{
LOG(info, "removing %s, too old (%f days)", fname,
- (double)(time(nullptr)-sb.st_mtime)/86400.0);
+ (double)(fastos::time()-sb.st_mtime)/86400.0);
if (unlink(fname) != 0) {
LOG(warning, "cannot remove %s: %s",
diff --git a/metrics/src/vespa/metrics/metricmanager.cpp b/metrics/src/vespa/metrics/metricmanager.cpp
index dcc02696e2d..40622b8c786 100644
--- a/metrics/src/vespa/metrics/metricmanager.cpp
+++ b/metrics/src/vespa/metrics/metricmanager.cpp
@@ -10,6 +10,7 @@
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/vespalib/stllike/hashtable.hpp>
+#include <vespa/fastos/timestamp.h>
#include <sstream>
#include <algorithm>
@@ -25,7 +26,7 @@ MetricManager::ConsumerSpec::~ConsumerSpec() = default;
time_t
MetricManager::Timer::getTime() const {
- return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
+ return fastos::time();
}
void
diff --git a/metrics/src/vespa/metrics/state_api_adapter.cpp b/metrics/src/vespa/metrics/state_api_adapter.cpp
index ebb4fb4fd49..f2e5108872e 100644
--- a/metrics/src/vespa/metrics/state_api_adapter.cpp
+++ b/metrics/src/vespa/metrics/state_api_adapter.cpp
@@ -3,7 +3,7 @@
#include "state_api_adapter.h"
#include "metricmanager.h"
#include <vespa/vespalib/stllike/asciistream.h>
-
+#include <vespa/fastos/timestamp.h>
namespace metrics {
@@ -15,8 +15,7 @@ StateApiAdapter::getMetrics(const vespalib::string &consumer)
if (periods.empty()) {
return ""; // no configuration yet
}
- const metrics::MetricSnapshot &snapshot(
- _manager.getMetricSnapshot(guard, periods[0]));
+ const metrics::MetricSnapshot &snapshot(_manager.getMetricSnapshot(guard, periods[0]));
vespalib::asciistream json;
vespalib::JsonStream stream(json);
metrics::JsonWriter metricJsonWriter(stream);
@@ -31,16 +30,13 @@ StateApiAdapter::getTotalMetrics(const vespalib::string &consumer)
_manager.updateMetrics(true);
metrics::MetricLockGuard guard(_manager.getMetricLock());
_manager.checkMetricsAltered(guard);
- time_t currentTime = time(0);
- std::unique_ptr<metrics::MetricSnapshot> generated(
- new metrics::MetricSnapshot(
- "Total metrics from start until current time", 0,
- _manager.getTotalMetricSnapshot(guard).getMetrics(),
- true));
- _manager.getActiveMetrics(guard)
- .addToSnapshot(*generated, false, currentTime);
- generated->setFromTime(
- _manager.getTotalMetricSnapshot(guard).getFromTime());
+ time_t currentTime = fastos::time();
+ auto generated = std::make_unique<metrics::MetricSnapshot>(
+ "Total metrics from start until current time", 0,
+ _manager.getTotalMetricSnapshot(guard).getMetrics(),
+ true);
+ _manager.getActiveMetrics(guard).addToSnapshot(*generated, false, currentTime);
+ generated->setFromTime(_manager.getTotalMetricSnapshot(guard).getFromTime());
const metrics::MetricSnapshot &snapshot = *generated;
vespalib::asciistream json;
vespalib::JsonStream stream(json);
diff --git a/slobrok/src/vespa/slobrok/server/metrics_producer.cpp b/slobrok/src/vespa/slobrok/server/metrics_producer.cpp
index 28959e94d35..1b3f7fe5a82 100644
--- a/slobrok/src/vespa/slobrok/server/metrics_producer.cpp
+++ b/slobrok/src/vespa/slobrok/server/metrics_producer.cpp
@@ -7,8 +7,15 @@
namespace slobrok {
+using namespace std::chrono;
+
namespace {
+time_t
+secondsSinceEpoch() {
+ return duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
+}
+
class MetricsSnapshotter : public FNET_Task
{
MetricsProducer &_owner;
@@ -100,14 +107,13 @@ MetricsProducer::MetricsProducer(const RPCHooks &hooks,
: _rpcHooks(hooks),
_lastMetrics(RPCHooks::Metrics::zero()),
_producer(),
- _startTime(time(NULL)),
+ _startTime(secondsSinceEpoch()),
_lastSnapshotStart(_startTime),
_snapshotter(new MetricsSnapshotter(transport, *this))
{
}
-MetricsProducer::~MetricsProducer() {
-}
+MetricsProducer::~MetricsProducer() = default;
vespalib::string
MetricsProducer::getMetrics(const vespalib::string &consumer)
@@ -118,7 +124,7 @@ MetricsProducer::getMetrics(const vespalib::string &consumer)
vespalib::string
MetricsProducer::getTotalMetrics(const vespalib::string &)
{
- uint32_t now = time(NULL);
+ uint32_t now = secondsSinceEpoch();
RPCHooks::Metrics current = _rpcHooks.getMetrics();
RPCHooks::Metrics start = RPCHooks::Metrics::zero();
return makeSnapshot(start, current, _startTime, now);
@@ -128,7 +134,7 @@ MetricsProducer::getTotalMetrics(const vespalib::string &)
void
MetricsProducer::snapshot()
{
- uint32_t now = time(NULL);
+ uint32_t now = secondsSinceEpoch();
RPCHooks::Metrics current = _rpcHooks.getMetrics();
_producer.setMetrics(makeSnapshot(_lastMetrics, current, _lastSnapshotStart, now));
_lastMetrics = current;
diff --git a/slobrok/src/vespa/slobrok/server/metrics_producer.h b/slobrok/src/vespa/slobrok/server/metrics_producer.h
index 535988f9a2e..460844a6d87 100644
--- a/slobrok/src/vespa/slobrok/server/metrics_producer.h
+++ b/slobrok/src/vespa/slobrok/server/metrics_producer.h
@@ -26,7 +26,7 @@ public:
void snapshot();
MetricsProducer(const RPCHooks &hooks, FNET_Transport &transport);
- ~MetricsProducer();
+ ~MetricsProducer() override;
};
diff --git a/storage/src/vespa/storage/config/distributorconfiguration.cpp b/storage/src/vespa/storage/config/distributorconfiguration.cpp
index 294ce56f536..d2dd74693c8 100644
--- a/storage/src/vespa/storage/config/distributorconfiguration.cpp
+++ b/storage/src/vespa/storage/config/distributorconfiguration.cpp
@@ -3,11 +3,14 @@
#include <vespa/document/select/parser.h>
#include <vespa/document/select/traversingvisitor.h>
#include <vespa/vespalib/util/exceptions.h>
+#include <vespa/fastos/timestamp.h>
#include <sstream>
#include <vespa/log/log.h>
LOG_SETUP(".distributorconfiguration");
+using namespace std::chrono;
+
namespace storage {
DistributorConfiguration::DistributorConfiguration(StorageComponent& component)
@@ -38,7 +41,7 @@ DistributorConfiguration::DistributorConfiguration(StorageComponent& component)
_minimumReplicaCountingMode(ReplicaCountingMode::TRUSTED)
{ }
-DistributorConfiguration::~DistributorConfiguration() { }
+DistributorConfiguration::~DistributorConfiguration() = default;
namespace {
@@ -60,8 +63,7 @@ DistributorConfiguration::containsTimeStatement(const std::string& documentSelec
{
TimeVisitor visitor;
try {
- document::select::Parser parser(*_component.getTypeRepo(),
- _component.getBucketIdFactory());
+ document::select::Parser parser(*_component.getTypeRepo(), _component.getBucketIdFactory());
std::unique_ptr<document::select::Node> node = parser.parse(documentSelection);
node->visit(visitor);
@@ -123,7 +125,7 @@ DistributorConfiguration::configure(const vespa::config::content::core::StorDist
// Always changes.
_lastGarbageCollectionChange = 1;
} else if (_garbageCollectionSelection != config.garbagecollection.selectiontoremove) {
- _lastGarbageCollectionChange = time(NULL);
+ _lastGarbageCollectionChange = fastos::time();
}
_garbageCollectionSelection = config.garbagecollection.selectiontoremove;
diff --git a/storage/src/vespa/storage/tools/throttlingsim.cpp b/storage/src/vespa/storage/tools/throttlingsim.cpp
index 97f5af3518e..52cd81c6fbd 100644
--- a/storage/src/vespa/storage/tools/throttlingsim.cpp
+++ b/storage/src/vespa/storage/tools/throttlingsim.cpp
@@ -270,7 +270,7 @@ BusyCounterBalancingClient::BusyCounterBalancingClient(Messaging& msgng, int win
void BusyCounterBalancingClient::run() {
- // int startTime = time(NULL);
+ // int startTime = fastos::time();
while (running()) {
{
@@ -456,9 +456,9 @@ ThrottlingApp::Main()
m.clients.push_back(c);
}
*/
- int timeNow = time(NULL);
+ int timeNow = fastos::time();
- while (time(NULL) - timeNow < 240) {
+ while (fastos::time() - timeNow < 240) {
m.print();
m.period.SetNow();
sleep(2);
diff --git a/vespalib/src/vespa/vespalib/util/random.cpp b/vespalib/src/vespa/vespalib/util/random.cpp
index 0f29e398ca6..50ff41e3407 100644
--- a/vespalib/src/vespa/vespalib/util/random.cpp
+++ b/vespalib/src/vespa/vespalib/util/random.cpp
@@ -5,6 +5,7 @@
#include <cstring>
#include <ctime>
#include <unistd.h>
+#include <chrono>
namespace vespalib {
@@ -30,7 +31,7 @@ RandomGen::RandomGen() :
_state(0)
{
unsigned long seed = getpid();
- seed ^= time(0);
+ seed ^= std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
char hn[32];
memset(hn, 0, sizeof(hn));
gethostname(hn, 32);
diff --git a/vespalog/src/logger/runserver.cpp b/vespalog/src/logger/runserver.cpp
index be61ee2551d..8ea669697ee 100644
--- a/vespalog/src/logger/runserver.cpp
+++ b/vespalog/src/logger/runserver.cpp
@@ -1,25 +1,22 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
+#include <cstdlib>
+#include <cstring>
+#include <cstdio>
#include <fcntl.h>
-#include <errno.h>
+#include <cerrno>
#include <unistd.h>
-#include <time.h>
-#include <csignal>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
-#include <sys/time.h>
#include <sys/resource.h>
#include <sys/file.h>
-#include <fcntl.h>
#include <vespa/defaults.h>
#include <vespa/log/llparser.h>
#include "llreader.h"
#include <vespa/log/log.h>
+#include <chrono>
LOG_SETUP("runserver");
@@ -38,6 +35,16 @@ void termsig(int sig) {
}
}
+namespace {
+
+using namespace std::chrono;
+
+time_t steady_time() {
+ return duration_cast<seconds>(steady_clock::now().time_since_epoch()).count();
+}
+
+}
+
class PidFile
{
private:
@@ -461,14 +468,14 @@ int main(int argc, char *argv[])
try {
mypf.writePid();
do {
- time_t laststart = time(NULL);
+ time_t laststart = steady_time();
stat = loop(service, argv+optind);
if (restart > 0 && !gotstopsig) {
- int wt = restart + laststart - time(NULL);
+ int wt = restart + laststart - steady_time();
if (wt < 0) wt = 0;
LOG(info, "will restart in %d seconds", wt);
}
- while (!gotstopsig && time(NULL) - laststart < restart) {
+ while (!gotstopsig && steady_time() - laststart < restart) {
sleep(1);
}
} while (!gotstopsig && restart > 0);