From 85c7ca75dc4ed305f1b2aa311943c7f849d4dd44 Mon Sep 17 00:00:00 2001 From: Peter van Dijk Date: Mon, 18 Apr 2016 16:22:06 +0200 Subject: [PATCH] introduce central gettime() function, so we can avoid clock_gettime on platforms that do not have it, like osx/mach --- m4/pdns_check_clock_gettime.m4 | 2 +- pdns/Makefile.am | 1 + pdns/delaypipe.cc | 10 ++---- pdns/dnsdist-lua2.cc | 11 ++++--- pdns/dnsdist-protobuf.cc | 3 +- pdns/dnsdist-tcp.cc | 5 +-- pdns/dnsdist-web.cc | 3 +- pdns/dnsdist.cc | 7 +++-- pdns/dnsdist.hh | 10 +++--- pdns/dnsdistdist/Makefile.am | 2 ++ pdns/dnsdistdist/gettime.cc | 1 + pdns/dnsdistdist/gettime.hh | 1 + pdns/gettime.cc | 31 +++++++++++++++++++ pdns/gettime.hh | 3 ++ pdns/pdns_recursor.cc | 3 +- pdns/recursordist/Makefile.am | 1 + pdns/recursordist/configure.ac | 2 ++ pdns/recursordist/gettime.cc | 1 + pdns/recursordist/gettime.hh | 1 + .../m4/pdns_check_clock_gettime.m4 | 1 + 20 files changed, 71 insertions(+), 28 deletions(-) create mode 120000 pdns/dnsdistdist/gettime.cc create mode 120000 pdns/dnsdistdist/gettime.hh create mode 100644 pdns/gettime.cc create mode 100644 pdns/gettime.hh create mode 120000 pdns/recursordist/gettime.cc create mode 120000 pdns/recursordist/gettime.hh create mode 120000 pdns/recursordist/m4/pdns_check_clock_gettime.m4 diff --git a/m4/pdns_check_clock_gettime.m4 b/m4/pdns_check_clock_gettime.m4 index ee1f6603c..b2d320778 100644 --- a/m4/pdns_check_clock_gettime.m4 +++ b/m4/pdns_check_clock_gettime.m4 @@ -1,6 +1,6 @@ AC_DEFUN([PDNS_CHECK_CLOCK_GETTIME],[ OLD_LIBS="$LIBS"; LIBS="" - AC_SEARCH_LIBS([clock_gettime], [rt]) + AC_SEARCH_LIBS([clock_gettime], [rt], [AC_DEFINE(HAVE_CLOCK_GETTIME, [1], [Define to 1 if you have clock_gettime])]) AC_SUBST([RT_LIBS],[$LIBS]) LIBS="$OLD_LIBS" ]) diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 2de7f37f7..4742c0b1a 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -1104,6 +1104,7 @@ testrunner_SOURCES = \ dnswriter.cc \ ednsoptions.cc ednsoptions.hh \ ednssubnet.cc \ + gettime.cc gettime.hh \ gss_context.cc gss_context.hh \ iputils.cc \ logger.cc \ diff --git a/pdns/delaypipe.cc b/pdns/delaypipe.cc index dda8d074f..1dcae0634 100644 --- a/pdns/delaypipe.cc +++ b/pdns/delaypipe.cc @@ -1,5 +1,6 @@ #include "delaypipe.hh" #include "misc.hh" +#include "gettime.hh" #include template @@ -86,14 +87,7 @@ DelayPipe::DelayPipe() : d_thread(&DelayPipe::worker, this) template void DelayPipe::gettime(struct timespec* ts) { -#ifdef __MACH__ // this is a 'limp home' solution since it doesn't do monotonic time. see http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x - struct timeval tv; - gettimeofday(&tv, 0); - ts->tv_sec = tv.tv_sec; - ts->tv_nsec = tv.tv_usec * 1000; -#else - clock_gettime(CLOCK_MONOTONIC, ts); -#endif + ::gettime(ts); } diff --git a/pdns/dnsdist-lua2.cc b/pdns/dnsdist-lua2.cc index ada1ebbd7..853770ce0 100644 --- a/pdns/dnsdist-lua2.cc +++ b/pdns/dnsdist-lua2.cc @@ -6,6 +6,7 @@ #include "sodcrypto.hh" #include "base64.hh" #include "lock.hh" +#include "gettime.hh" #include #include #include @@ -59,7 +60,7 @@ map exceedRespGen(int rate, int seconds, std::function exceedQueryGen(int rate, int seconds, std::function out; diff --git a/pdns/dnsdist-protobuf.cc b/pdns/dnsdist-protobuf.cc index d0a0d7aa9..93bab7f3f 100644 --- a/pdns/dnsdist-protobuf.cc +++ b/pdns/dnsdist-protobuf.cc @@ -2,6 +2,7 @@ #include "config.h" #include "dnsdist.hh" +#include "gettime.hh" #include "dnsparser.hh" #include "dnsdist-protobuf.hh" @@ -33,7 +34,7 @@ static void protobufFillMessage(PBDNSMessage& message, const DNSQuestion& dq) message.set_inbytes(dq.len); struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); + gettime(&ts, true); message.set_timesec(ts.tv_sec); message.set_timeusec(ts.tv_nsec / 1000); message.set_id(ntohs(dq.dh->id)); diff --git a/pdns/dnsdist-tcp.cc b/pdns/dnsdist-tcp.cc index 6bd095fe6..4adb768b7 100644 --- a/pdns/dnsdist-tcp.cc +++ b/pdns/dnsdist-tcp.cc @@ -25,6 +25,7 @@ #include "ednsoptions.hh" #include "dolog.hh" #include "lock.hh" +#include "gettime.hh" #include #include @@ -264,7 +265,7 @@ void* tcpClientThread(int pipefd) string poolname; int delayMsec=0; struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); + gettime(&now); if (!processQuery(localDynBlockNMG, localRulactions, blockFilter, dq, poolname, &delayMsec, now)) { goto drop; @@ -443,7 +444,7 @@ void* tcpClientThread(int pipefd) g_stats.responses++; struct timespec answertime; - clock_gettime(CLOCK_MONOTONIC, &answertime); + gettime(&answertime); unsigned int udiff = 1000000.0*DiffTime(now,answertime); { std::lock_guard lock(g_rings.respMutex); diff --git a/pdns/dnsdist-web.cc b/pdns/dnsdist-web.cc index f0cb43be8..7c051f8df 100644 --- a/pdns/dnsdist-web.cc +++ b/pdns/dnsdist-web.cc @@ -12,6 +12,7 @@ #include "ext/incbin/incbin.h" #include "htmlfiles.h" #include "base64.hh" +#include "gettime.hh" static bool compareAuthorization(YaHTTP::Request& req, const string &expected_password, const string& expectedApiKey) @@ -176,7 +177,7 @@ static void connectionThread(int sock, ComboAddress remote, string password, str Json::object obj; auto slow = g_dynblockNMG.getCopy(); struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); + gettime(&now); for(const auto& e: slow) { if(now < e->second.until ) { Json::object thing{{"reason", e->second.reason}, {"seconds", (double)(e->second.until.tv_sec - now.tv_sec)}, diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index b21112bd7..459dc3699 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -41,6 +41,7 @@ #include "lock.hh" #include #include "dnsdist-cache.hh" +#include "gettime.hh" #ifdef HAVE_SYSTEMD #include @@ -423,7 +424,7 @@ void* responderThread(std::shared_ptr state) { struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); + gettime(&ts); std::lock_guard lock(g_rings.respMutex); g_rings.respRing.push_back({ts, ids->origRemote, ids->qname, ids->qtype, (unsigned int)udiff, (unsigned int)got, *dh, state->remote}); } @@ -921,7 +922,7 @@ try string poolname; int delayMsec=0; struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); + gettime(&now); if (!processQuery(localDynBlock, localRulactions, blockFilter, dq, poolname, &delayMsec, now)) { @@ -1235,7 +1236,7 @@ void* healthChecksThread() dss->reuseds++; --dss->outstanding; struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); + gettime(&ts); struct dnsheader fake; memset(&fake, 0, sizeof(fake)); diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index 44d73df30..5b14ca955 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -13,6 +13,7 @@ #include "sholder.hh" #include "dnscrypt.hh" #include "dnsdist-cache.hh" +#include "gettime.hh" #ifdef HAVE_PROTOBUF #include @@ -101,19 +102,16 @@ extern struct DNSDistStats g_stats; struct StopWatch { -#ifndef CLOCK_MONOTONIC_RAW -#define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC -#endif struct timespec d_start{0,0}; void start() { - if(clock_gettime(CLOCK_MONOTONIC_RAW, &d_start) < 0) + if(gettime(&d_start) < 0) unixDie("Getting timestamp"); } double udiff() const { struct timespec now; - if(clock_gettime(CLOCK_MONOTONIC_RAW, &now) < 0) + if(gettime(&now) < 0) unixDie("Getting timestamp"); return 1000000.0*(now.tv_sec - d_start.tv_sec) + (now.tv_nsec - d_start.tv_nsec)/1000.0; @@ -121,7 +119,7 @@ struct StopWatch double udiffAndSet() { struct timespec now; - if(clock_gettime(CLOCK_MONOTONIC_RAW, &now) < 0) + if(gettime(&now) < 0) unixDie("Getting timestamp"); auto ret= 1000000.0*(now.tv_sec - d_start.tv_sec) + (now.tv_nsec - d_start.tv_nsec)/1000.0; diff --git a/pdns/dnsdistdist/Makefile.am b/pdns/dnsdistdist/Makefile.am index 16cdd557c..1b97108fb 100644 --- a/pdns/dnsdistdist/Makefile.am +++ b/pdns/dnsdistdist/Makefile.am @@ -81,6 +81,7 @@ dnsdist_SOURCES = \ ednsoptions.cc ednsoptions.hh \ ednscookies.cc ednscookies.hh \ ednssubnet.cc ednssubnet.hh \ + gettime.cc gettime.hh \ iputils.cc iputils.hh \ lock.hh \ misc.cc misc.hh \ @@ -149,6 +150,7 @@ testrunner_SOURCES = \ ednsoptions.cc ednsoptions.hh \ ednscookies.cc ednscookies.hh \ ednssubnet.cc ednssubnet.hh \ + gettime.cc gettime.hh \ iputils.cc iputils.hh \ misc.cc misc.hh \ namespaces.hh \ diff --git a/pdns/dnsdistdist/gettime.cc b/pdns/dnsdistdist/gettime.cc new file mode 120000 index 000000000..29e652d74 --- /dev/null +++ b/pdns/dnsdistdist/gettime.cc @@ -0,0 +1 @@ +../gettime.cc \ No newline at end of file diff --git a/pdns/dnsdistdist/gettime.hh b/pdns/dnsdistdist/gettime.hh new file mode 120000 index 000000000..7ee3b68b0 --- /dev/null +++ b/pdns/dnsdistdist/gettime.hh @@ -0,0 +1 @@ +../gettime.hh \ No newline at end of file diff --git a/pdns/gettime.cc b/pdns/gettime.cc new file mode 100644 index 000000000..16a6d2e2f --- /dev/null +++ b/pdns/gettime.cc @@ -0,0 +1,31 @@ +#include "gettime.hh" +#include "config.h" + +#ifdef HAVE_CLOCK_GETTIME +#include + +#ifndef CLOCK_MONOTONIC_RAW +#define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC +#endif + +int gettime(struct timespec *tp, bool needRealTime) +{ + return clock_gettime(needRealTime ? CLOCK_REALTIME : CLOCK_MONOTONIC_RAW, tp); +} + +#else +#include + +int gettime(struct timespec *tp, bool needRealTime) +{ + struct timeval tv; + + int ret = gettimeofday(&tv, NULL); + if(ret < 0) return ret; + + tp->tv_sec = tv.tv_sec; + tp->tv_nsec = tv.tv_usec * 1000; + return ret; +} + +#endif \ No newline at end of file diff --git a/pdns/gettime.hh b/pdns/gettime.hh new file mode 100644 index 000000000..a0c9ca444 --- /dev/null +++ b/pdns/gettime.hh @@ -0,0 +1,3 @@ +#pragma once + +extern int gettime(struct timespec *tp, bool needRealTime=false); diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 7fecde046..661f23d71 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -81,6 +81,7 @@ extern SortList g_sortlist; #include "validate-recursor.hh" #include "rec-lua-conf.hh" #include "ednsoptions.hh" +#include "gettime.hh" #ifdef HAVE_PROTOBUF #include @@ -648,7 +649,7 @@ static void protobufFillMessageFromDC(PBDNSMessage& message, const DNSComboWrite } struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); + gettime(&ts, true); message.set_timesec(ts.tv_sec); message.set_timeusec(ts.tv_nsec / 1000); message.set_id(ntohs(dc->d_mdp.d_header.id)); diff --git a/pdns/recursordist/Makefile.am b/pdns/recursordist/Makefile.am index 06680a02c..e58336a00 100644 --- a/pdns/recursordist/Makefile.am +++ b/pdns/recursordist/Makefile.am @@ -80,6 +80,7 @@ pdns_recursor_SOURCES = \ ednsoptions.cc ednsoptions.hh \ ednssubnet.cc ednssubnet.hh \ filterpo.cc filterpo.hh \ + gettime.cc gettime.hh \ gss_context.cc gss_context.hh \ iputils.hh iputils.cc \ ixfr.cc ixfr.hh \ diff --git a/pdns/recursordist/configure.ac b/pdns/recursordist/configure.ac index 681bd77f9..f077a5937 100644 --- a/pdns/recursordist/configure.ac +++ b/pdns/recursordist/configure.ac @@ -65,6 +65,8 @@ AC_DEFUN([PDNS_SELECT_CONTEXT_IMPL], [ fi ]) +PDNS_CHECK_CLOCK_GETTIME + BOOST_REQUIRE([1.35]) PDNS_SELECT_CONTEXT_IMPL diff --git a/pdns/recursordist/gettime.cc b/pdns/recursordist/gettime.cc new file mode 120000 index 000000000..29e652d74 --- /dev/null +++ b/pdns/recursordist/gettime.cc @@ -0,0 +1 @@ +../gettime.cc \ No newline at end of file diff --git a/pdns/recursordist/gettime.hh b/pdns/recursordist/gettime.hh new file mode 120000 index 000000000..7ee3b68b0 --- /dev/null +++ b/pdns/recursordist/gettime.hh @@ -0,0 +1 @@ +../gettime.hh \ No newline at end of file diff --git a/pdns/recursordist/m4/pdns_check_clock_gettime.m4 b/pdns/recursordist/m4/pdns_check_clock_gettime.m4 new file mode 120000 index 000000000..fdefc26d7 --- /dev/null +++ b/pdns/recursordist/m4/pdns_check_clock_gettime.m4 @@ -0,0 +1 @@ +../../../m4/pdns_check_clock_gettime.m4 \ No newline at end of file -- 2.40.0