]> granicus.if.org Git - pdns/commitdiff
introduce central gettime() function, so we can avoid clock_gettime on platforms...
authorPeter van Dijk <peter.van.dijk@powerdns.com>
Mon, 18 Apr 2016 14:22:06 +0000 (16:22 +0200)
committerPeter van Dijk <peter.van.dijk@powerdns.com>
Fri, 20 May 2016 10:02:08 +0000 (12:02 +0200)
20 files changed:
m4/pdns_check_clock_gettime.m4
pdns/Makefile.am
pdns/delaypipe.cc
pdns/dnsdist-lua2.cc
pdns/dnsdist-protobuf.cc
pdns/dnsdist-tcp.cc
pdns/dnsdist-web.cc
pdns/dnsdist.cc
pdns/dnsdist.hh
pdns/dnsdistdist/Makefile.am
pdns/dnsdistdist/gettime.cc [new symlink]
pdns/dnsdistdist/gettime.hh [new symlink]
pdns/gettime.cc [new file with mode: 0644]
pdns/gettime.hh [new file with mode: 0644]
pdns/pdns_recursor.cc
pdns/recursordist/Makefile.am
pdns/recursordist/configure.ac
pdns/recursordist/gettime.cc [new symlink]
pdns/recursordist/gettime.hh [new symlink]
pdns/recursordist/m4/pdns_check_clock_gettime.m4 [new symlink]

index ee1f6603c3f4b37979ed10eda20f011dbc039f5a..b2d320778e8e8dc06604ffea50eb7298a016f064 100644 (file)
@@ -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"
 ])
index 2de7f37f7cec2493ae9afdb27ac02c5dc37dab2b..4742c0b1a7d80d3aa82869a853358a158207dd06 100644 (file)
@@ -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 \
index dda8d074fe6fba8e2727ab2d94da83c6e35c4dd0..1dcae06347c3779f909d7ec414c15960cf8c81e1 100644 (file)
@@ -1,5 +1,6 @@
 #include "delaypipe.hh"
 #include "misc.hh"
+#include "gettime.hh"
 #include <thread>
 
 template<class T>
@@ -86,14 +87,7 @@ DelayPipe<T>::DelayPipe() : d_thread(&DelayPipe<T>::worker, this)
 template<class T>
 void DelayPipe<T>::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);
 }
 
 
index ada1ebbd77859fabd218f5ad9aa9a12145900316..853770ce08fd306ff5bb7ff65f1a87b74cb34749 100644 (file)
@@ -6,6 +6,7 @@
 #include "sodcrypto.hh"
 #include "base64.hh"
 #include "lock.hh"
+#include "gettime.hh"
 #include <map>
 #include <fstream>
 #include <boost/logic/tribool.hpp>
@@ -59,7 +60,7 @@ map<ComboAddress,int> exceedRespGen(int rate, int seconds, std::function<void(co
 {
   counts_t counts;
   struct timespec cutoff, mintime, now;
-  clock_gettime(CLOCK_MONOTONIC, &now);
+  gettime(&now);
   cutoff = mintime = now;
   cutoff.tv_sec -= seconds;
 
@@ -82,7 +83,7 @@ map<ComboAddress,int> exceedQueryGen(int rate, int seconds, std::function<void(c
 {
   counts_t counts;
   struct timespec cutoff, mintime, now;
-  clock_gettime(CLOCK_MONOTONIC, &now);
+  gettime(&now);
   cutoff = mintime = now;
   cutoff.tv_sec -= seconds;
 
@@ -140,7 +141,7 @@ void moreLua(bool client)
       setLuaNoSideEffect();
       auto slow = g_dynblockNMG.getCopy();
       struct timespec now;
-      clock_gettime(CLOCK_MONOTONIC, &now);
+      gettime(&now);
       boost::format fmt("%-24s %8d %8d %s\n");
       g_outputBuffer = (fmt % "Netmask" % "Seconds" % "Blocks" % "Reason").str();
       for(const auto& e: slow) {
@@ -160,7 +161,7 @@ void moreLua(bool client)
                            setLuaSideEffect();
                           auto slow = g_dynblockNMG.getCopy();
                           struct timespec until, now;
-                          clock_gettime(CLOCK_MONOTONIC, &now);
+                          gettime(&now);
                           until=now;
                            int actualSeconds = seconds ? *seconds : 10;
                           until.tv_sec += actualSeconds; 
@@ -296,7 +297,7 @@ void moreLua(bool client)
       
       unsigned int num=0;
       struct timespec now;
-      clock_gettime(CLOCK_MONOTONIC, &now);
+      gettime(&now);
             
       std::multimap<struct timespec, string> out;
 
index d0a0d7aa9aea4f70b85dbc32d7e07c59ef12a992..93bab7f3ff4d284bed32bce6f59db592e59b7fd3 100644 (file)
@@ -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));
index 6bd095fe64ad9356afa243cc973b40e175e25c48..4adb768b78c3ba7cce0e43053ca6062cffab4482 100644 (file)
@@ -25,6 +25,7 @@
 #include "ednsoptions.hh"
 #include "dolog.hh"
 #include "lock.hh"
+#include "gettime.hh"
 #include <thread>
 #include <atomic>
 
@@ -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<std::mutex> lock(g_rings.respMutex);
index f0cb43be8227615330e860993a3d9ecf703a2974..7c051f8df494fd037cc1047f15465be98bd21d0e 100644 (file)
@@ -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)},
index b21112bd711a36a833d1296c835e5b0bfe3fd785..459dc36991cbab476cfed2d15daa59cf2e7dc640 100644 (file)
@@ -41,6 +41,7 @@
 #include "lock.hh"
 #include <getopt.h>
 #include "dnsdist-cache.hh"
+#include "gettime.hh"
 
 #ifdef HAVE_SYSTEMD
 #include <systemd/sd-daemon.h>
@@ -423,7 +424,7 @@ void* responderThread(std::shared_ptr<DownstreamState> state)
 
     {
       struct timespec ts;
-      clock_gettime(CLOCK_MONOTONIC, &ts);
+      gettime(&ts);
       std::lock_guard<std::mutex> 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));
index 44d73df30f71a1938af98650d9f1c31369724f7e..5b14ca955ecc860de455404c1ce45a2c1b629df2 100644 (file)
@@ -13,6 +13,7 @@
 #include "sholder.hh"
 #include "dnscrypt.hh"
 #include "dnsdist-cache.hh"
+#include "gettime.hh"
 
 #ifdef HAVE_PROTOBUF
 #include <boost/uuid/uuid.hpp>
@@ -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;
index 16cdd557cb3f60c3a11212d310d0807a57cf9492..1b97108fbc373db5118e5f170d11c64ca6acd9d2 100644 (file)
@@ -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 (symlink)
index 0000000..29e652d
--- /dev/null
@@ -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 (symlink)
index 0000000..7ee3b68
--- /dev/null
@@ -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 (file)
index 0000000..16a6d2e
--- /dev/null
@@ -0,0 +1,31 @@
+#include "gettime.hh"
+#include "config.h"
+
+#ifdef HAVE_CLOCK_GETTIME
+#include <time.h>
+
+#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 <sys/time.h>
+
+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 (file)
index 0000000..a0c9ca4
--- /dev/null
@@ -0,0 +1,3 @@
+#pragma once
+
+extern int gettime(struct timespec *tp, bool needRealTime=false);
index 7fecde0463c0b9b6924cf932f96f0309954db849..661f23d710c7012d1397b489de7fb67daccb1142 100644 (file)
@@ -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 <boost/uuid/uuid.hpp>
@@ -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));
index 06680a02cd872dc4b6343b89cf8f25067705a9bf..e58336a00b9c3e4a63c390781a0095ad5716532a 100644 (file)
@@ -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 \
index 681bd77f9e77a1c2e8e80be67f4963bd7331a919..f077a5937ecc884564bdbb64ee9a76953e2013d0 100644 (file)
@@ -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 (symlink)
index 0000000..29e652d
--- /dev/null
@@ -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 (symlink)
index 0000000..7ee3b68
--- /dev/null
@@ -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 (symlink)
index 0000000..fdefc26
--- /dev/null
@@ -0,0 +1 @@
+../../../m4/pdns_check_clock_gettime.m4
\ No newline at end of file