Changeset 469

Show
Ignore:
Timestamp:
09/02/05 16:53:40 (8 years ago)
Author:
ahu
Message:

remove quite a number of gettimeofday calls

Location:
trunk/pdns/pdns
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/pdns/pdns/pdns_recursor.cc

    r464 r469  
    483483}; 
    484484 
     485#if 0 
     486 
     487#include <execinfo.h> 
     488 
     489static void maketrace() 
     490{ 
     491  void *array[20]; //only care about last 17 functions (3 taken with tracing support) 
     492  size_t size; 
     493  char **strings; 
     494  size_t i; 
     495 
     496  size = backtrace (array, 20); 
     497  strings = backtrace_symbols (array, size); //Need -rdynamic gcc (linker) flag for this to work 
     498 
     499  for (i = 0; i < size; i++) //skip useless functions 
     500    cout<<strings[i]<<"\n"; 
     501  cout<<"--"<<endl; 
     502} 
     503 
     504extern "C" { 
     505int gettimeofday (struct timeval *__restrict __tv, 
     506                  __timezone_ptr_t __tz) 
     507{ 
     508  maketrace(); 
     509  return 0; 
     510} 
     511 
     512} 
     513#endif  
     514 
    485515int main(int argc, char **argv)  
    486516{ 
  • trunk/pdns/pdns/syncres.cc

    r440 r469  
    118118    LOG<<prefix<<qname<<": Checking if we have NS in cache for '"<<subdomain<<"'"<<endl; 
    119119    set<DNSResourceRecord>ns; 
    120     if(RC.get(d_now, subdomain,QType(QType::NS),&ns)>0) { 
     120    if(RC.get(d_now.tv_sec, subdomain,QType(QType::NS),&ns)>0) { 
    121121 
    122122      for(set<DNSResourceRecord>::const_iterator k=ns.begin();k!=ns.end();++k) { 
    123         if(k->ttl > (unsigned int)d_now ) {  
     123        if(k->ttl > (unsigned int)d_now.tv_sec ) {  
    124124          set<DNSResourceRecord>aset; 
    125           if(!endsOn(k->content,subdomain) || RC.get(d_now, k->content,QType(QType::A),&aset) > 5) { 
     125          if(!endsOn(k->content,subdomain) || RC.get(d_now.tv_sec, k->content,QType(QType::A),&aset) > 5) { 
    126126            bestns.insert(*k); 
    127127            LOG<<prefix<<qname<<": NS (with ip, or non-glue) in cache for '"<<subdomain<<"' -> '"<<k->content<<"'"<<endl; 
    128128            LOG<<prefix<<qname<<": within bailiwick: "<<endsOn(k->content,subdomain); 
    129129            if(!aset.empty()) 
    130               L<<", in cache, ttl="<<(unsigned int)(((time_t)aset.begin()->ttl- d_now ))<<endl; 
     130              L<<", in cache, ttl="<<(unsigned int)(((time_t)aset.begin()->ttl- d_now.tv_sec ))<<endl; 
    131131            else 
    132132              L<<", not in cache"<<endl; 
     
    184184  LOG<<prefix<<qname<<": Looking for CNAME cache hit of '"<<tuple<<"'"<<endl; 
    185185  set<DNSResourceRecord> cset; 
    186   if(RC.get(d_now, qname,QType(QType::CNAME),&cset) > 0) { 
     186  if(RC.get(d_now.tv_sec, qname,QType(QType::CNAME),&cset) > 0) { 
    187187 
    188188    for(set<DNSResourceRecord>::const_iterator j=cset.begin();j!=cset.end();++j) { 
    189       if(j->ttl>(unsigned int) d_now) { 
     189      if(j->ttl>(unsigned int) d_now.tv_sec) { 
    190190        LOG<<prefix<<qname<<": Found cache CNAME hit for '"<<tuple<<"' to '"<<j->content<<"'"<<endl;     
    191191        DNSResourceRecord rr=*j; 
    192         rr.ttl-=d_now; 
     192        rr.ttl-=d_now.tv_sec; 
    193193        ret.push_back(rr); 
    194194        if(!(qtype==QType(QType::CNAME))) {// perhaps they really wanted a CNAME! 
     
    220220    res=0; 
    221221    map<string,NegCacheEntry>::const_iterator ni=s_negcache.find(toLower(qname)); 
    222     if(d_now < ni->second.ttd) { 
    223       sttl=ni->second.ttd - d_now; 
     222    if(d_now.tv_sec < ni->second.ttd) { 
     223      sttl=ni->second.ttd - d_now.tv_sec; 
    224224      LOG<<prefix<<qname<<": Entire record '"<<toLower(qname)<<"', is negatively cached for another "<<sttl<<" seconds"<<endl; 
    225225      res=RCode::NXDomain;  
     
    242242    map<string,NegCacheEntry>::const_iterator ni=s_negcache.find(tuple); 
    243243    if(ni!=s_negcache.end()) { 
    244       if(d_now < ni->second.ttd) { 
    245         sttl=ni->second.ttd - d_now; 
     244      if(d_now.tv_sec < ni->second.ttd) { 
     245        sttl=ni->second.ttd - d_now.tv_sec; 
    246246        LOG<<prefix<<qname<<": "<<qtype.getName()<<" is negatively cached for another "<<sttl<<" seconds"<<endl; 
    247247        res=RCode::NoError; // only this record doesn't exist 
     
    259259  set<DNSResourceRecord> cset; 
    260260  bool found=false, expired=false; 
    261   if(RC.get(d_now, sqname,sqt,&cset)>0) { 
     261  if(RC.get(d_now.tv_sec, sqname,sqt,&cset)>0) { 
    262262    LOG<<prefix<<qname<<": Found cache hit for "<<sqt.getName()<<": "; 
    263263    for(set<DNSResourceRecord>::const_iterator j=cset.begin();j!=cset.end();++j) { 
    264264      LOG<<j->content; 
    265       if(j->ttl>(unsigned int) d_now) { 
     265      if(j->ttl>(unsigned int) d_now.tv_sec) { 
    266266        DNSResourceRecord rr=*j; 
    267         rr.ttl-=d_now; 
     267        rr.ttl-=d_now.tv_sec; 
    268268        if(giveNegative) { 
    269269          rr.d_place=DNSResourceRecord::AUTHORITY; 
     
    322322  for(set<string>::const_iterator i=nameservers.begin();i!=nameservers.end();++i) { 
    323323    rnameservers.push_back(*i); 
    324     speeds[*i]=nsSpeeds[toLower(*i)].get(); 
     324    DecayingEwma& temp=nsSpeeds[toLower(*i)]; 
     325    speeds[*i]=temp.get(&d_now); 
    325326  } 
    326327  random_shuffle(rnameservers.begin(),rnameservers.end()); 
     
    339340    L<<endl; 
    340341  } 
    341  
    342342  return rnameservers; 
    343343} 
     
    378378      bool doTCP=false; 
    379379 
    380       if(s_throttle.shouldThrottle(d_now, remoteIP+"|"+qname+"|"+qtype.getName())) { 
     380      if(s_throttle.shouldThrottle(d_now.tv_sec, remoteIP+"|"+qname+"|"+qtype.getName())) { 
    381381        LOG<<prefix<<qname<<": query throttled "<<endl; 
    382382        s_throttledqueries++; 
     
    403403            LOG<<prefix<<qname<<": error resolving"<<endl; 
    404404 
    405           nsSpeeds[toLower(*tns)].submit(1000000); // 1 sec 
     405          nsSpeeds[toLower(*tns)].submit(1000000, &d_now); // 1 sec 
    406406           
    407           s_throttle.throttle(d_now, remoteIP+"|"+qname+"|"+qtype.getName(),20,5); 
     407          s_throttle.throttle(d_now.tv_sec, remoteIP+"|"+qname+"|"+qtype.getName(),20,5); 
    408408          continue; 
    409409        } 
    410         d_now=time(0); 
     410        gettimeofday(&d_now, 0); 
    411411      } 
    412412 
     
    425425      if(d_lwr.d_rcode==RCode::ServFail) { 
    426426        LOG<<prefix<<qname<<": "<<*tns<<" returned a ServFail, trying sibling NS"<<endl; 
    427         s_throttle.throttle(d_now,remoteIP+"|"+qname+"|"+qtype.getName(),60,3); 
     427        s_throttle.throttle(d_now.tv_sec,remoteIP+"|"+qname+"|"+qtype.getName(),60,3); 
    428428        continue; 
    429429      } 
     
    452452            DNSResourceRecord rr=*i; 
    453453            rr.d_place=DNSResourceRecord::ANSWER; 
    454             rr.ttl+=d_now; 
     454            rr.ttl+=d_now.tv_sec; 
    455455            //    rr.ttl=time(0)+10+10*rr.qtype.getCode(); 
    456456            tcache[toLower(i->qname)+"|"+i->qtype.getName()].insert(rr); 
     
    489489          NegCacheEntry ne; 
    490490          ne.name=i->qname; 
    491           ne.ttd=d_now + i->ttl; 
     491          ne.ttd=d_now.tv_sec + i->ttl; 
    492492          s_negcache[toLower(qname)]=ne; 
    493493          negindic=true; 
     
    528528          NegCacheEntry ne; 
    529529          ne.name=i->qname; 
    530           ne.ttd=d_now + i->ttl; 
     530          ne.ttd=d_now.tv_sec + i->ttl; 
    531531          s_negcache[toLower(qname)+"|"+qtype.getName()]=ne; 
    532532          negindic=true; 
     
    560560      else { 
    561561        LOG<<prefix<<qname<<": status=NS "<<*tns<<" is lame for '"<<auth<<"', trying sibling NS"<<endl; 
    562         s_throttle.throttle(d_now, remoteIP+"|"+qname+"|"+qtype.getName(),60,0); 
     562        s_throttle.throttle(d_now.tv_sec, remoteIP+"|"+qname+"|"+qtype.getName(),60,0); 
    563563      } 
    564564    } 
     
    607607    DNSResourceRecord ns=*k; 
    608608    ns.d_place=DNSResourceRecord::AUTHORITY; 
    609     ns.ttl-=d_now; 
     609    ns.ttl-=d_now.tv_sec; 
    610610    ret.push_back(ns); 
    611611  } 
  • trunk/pdns/pdns/syncres.hh

    r466 r469  
    1212#include "misc.hh" 
    1313#include "lwres.hh" 
    14  
     14#include <boost/utility.hpp> 
    1515 
    1616/* external functions, opaque to us */ 
     
    8888{ 
    8989public: 
    90   DecayingEwma() : d_last(getTime()) , d_lastget(time(0)),  d_val(0.0) { 
    91   } 
    92   void submit(int val)  
    93   { 
    94     double now=getTime(); 
     90  DecayingEwma() : d_last(getTime()) , d_lastget(d_last),  d_val(0.0) { 
     91 
     92  } 
     93 
     94  DecayingEwma(const DecayingEwma& orig) : d_last(orig.d_last), d_lastget(orig.d_lastget), d_val(orig.d_val) 
     95  { 
     96 
     97  } 
     98 
     99  void submit(int val, struct timeval*tv = 0) 
     100  { 
     101    double now; 
     102    if(tv) 
     103      now=tv->tv_sec + tv->tv_usec/1000000.0; 
     104    else 
     105      now=getTime(); 
     106 
    95107    double diff=d_last-now; 
    96108    d_last=now; 
     
    98110    d_val=(1-factor)*val+ factor*d_val;  
    99111  } 
    100   double get() 
    101   { 
    102     double now=getTime(); 
     112 
     113  double get(struct timeval*tv = 0) 
     114  { 
     115    double now; 
     116    if(tv) 
     117      now=tv->tv_sec + tv->tv_usec/1000000.0; 
     118    else 
     119      now=getTime(); 
     120 
    103121    double diff=d_lastget-now; 
    104122    d_lastget=now; 
     
    108126 
    109127private: 
    110  
     128  DecayingEwma& operator=(const DecayingEwma&); 
    111129  double d_last; 
    112130  double d_lastget; 
     
    118136{ 
    119137public: 
    120   SyncRes() : d_outqueries(0), d_tcpoutqueries(0), d_throttledqueries(0), d_timeouts(0), d_cacheonly(false), d_nocache(false), d_now(time(0)) {} 
     138  SyncRes() : d_outqueries(0), d_tcpoutqueries(0), d_throttledqueries(0), d_timeouts(0), d_cacheonly(false), d_nocache(false) { gettimeofday(&d_now, 0); } 
    121139  int beginResolve(const string &qname, const QType &qtype, vector<DNSResourceRecord>&ret); 
    122140  void setId(int id) 
     
    172190  bool d_nocache; 
    173191  LWRes d_lwr; 
    174   time_t d_now; 
     192  struct timeval d_now; 
    175193 
    176194  struct GetBestNSAnswer