Changeset 599

Show
Ignore:
Timestamp:
03/19/06 20:20:17 (7 years ago)
Author:
ahu
Message:

optimize DecayingEwma? to no longer gettimeofday in constructor, add PulseRate?

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pdns/pdns/syncres.hh

    r594 r599  
    1616#include "recursor_cache.hh" 
    1717#include <boost/tuple/tuple.hpp> 
     18#include <boost/optional.hpp> 
    1819#include <boost/tuple/tuple_comparison.hpp> 
    1920#include "mtasker.hh" 
     
    9798{ 
    9899public: 
    99   DecayingEwma() : d_last(getTime()) , d_lastget(d_last),  d_val(0.0) { 
    100  
    101   } 
    102  
    103   DecayingEwma(const DecayingEwma& orig) : d_last(orig.d_last), d_lastget(orig.d_lastget), d_val(orig.d_val) 
    104   { 
    105  
     100  DecayingEwma() :  d_val(0.0)  
     101  { 
     102    d_needinit=true; 
     103    d_lastget=d_last; 
     104  } 
     105 
     106  DecayingEwma(const DecayingEwma& orig) : d_last(orig.d_last),  d_lastget(orig.d_lastget), d_val(orig.d_val), d_needinit(orig.d_needinit) 
     107  { 
     108  } 
     109 
     110  struct timeval getOrMakeTime(struct timeval* tv) 
     111  { 
     112    if(tv) 
     113      return *tv; 
     114    else { 
     115      struct timeval ret; 
     116      gettimeofday(&ret, 0); 
     117      return ret; 
     118    } 
    106119  } 
    107120 
    108121  void submit(int val, struct timeval*tv = 0) 
    109122  { 
    110     float now; 
    111     if(tv) 
    112       now=tv->tv_sec + tv->tv_usec/1000000.0; 
    113     else 
    114       now=getTime(); 
    115  
    116     float diff=d_last-now; 
     123    struct timeval now=getOrMakeTime(tv); 
     124 
     125    if(d_needinit) { 
     126      d_last=now; 
     127      d_needinit=false; 
     128    } 
     129 
     130    float diff= makeFloat(d_last - now); 
     131 
    117132    d_last=now; 
    118     float factor=exp(diff)/2.0; // might be '0.5', or 0.0001 
     133    double factor=exp(diff)/2.0; // might be '0.5', or 0.0001 
    119134    d_val=(1-factor)*val+ factor*d_val;  
    120135  } 
    121136 
    122   float get(struct timeval*tv = 0) 
    123   { 
    124     float now; 
    125     if(tv) 
    126       now=tv->tv_sec + tv->tv_usec/1000000.0; 
    127     else 
    128       now=getTime(); 
    129  
    130     float diff=d_lastget-now; 
     137 
     138  double get(struct timeval*tv = 0) 
     139  { 
     140    struct timeval now=getOrMakeTime(tv); 
     141    float diff=makeFloat(d_lastget-now); 
    131142    d_lastget=now; 
    132143    float factor=exp(diff/60.0); // is 1.0 or less 
     
    136147  bool stale(time_t limit)  
    137148  { 
    138     return limit > d_lastget; 
     149    return limit > d_lastget.tv_sec; 
    139150  } 
    140151 
    141152private: 
    142153  DecayingEwma& operator=(const DecayingEwma&); 
    143   float d_last; 
    144   float d_lastget; 
     154  struct timeval d_last;          // stores time 
     155  struct timeval d_lastget;       // stores time 
    145156  float d_val; 
     157  bool d_needinit; 
     158}; 
     159 
     160 
     161class PulseRate 
     162{ 
     163public: 
     164  PulseRate() :  d_val(0.0)  
     165  { 
     166    gettimeofday(&d_last, 0); 
     167  } 
     168 
     169  PulseRate(const PulseRate& orig) : d_last(orig.d_last), d_val(orig.d_val) 
     170  { 
     171  } 
     172 
     173  void pulse(const struct timeval& now) 
     174  { 
     175    //    cout<<"about to submit: "<< 1000.0*makeFloat(now - d_last)<<"\n"; 
     176    submit((int)(1000.0*(makeFloat(now-d_last))), now); 
     177  } 
     178 
     179  optional<float> get(struct timeval& now, unsigned int limit) const 
     180  { 
     181    optional<float> ret; 
     182    float diff=makeFloat(now - d_last); 
     183    if(diff < limit) 
     184      ret=d_val; 
     185    return ret; 
     186  } 
     187 
     188  bool stale(time_t limit)  
     189  { 
     190    return limit > d_last.tv_sec; 
     191  } 
     192 
     193private: 
     194  void submit(int val, const struct timeval& now) 
     195  { 
     196    float diff= makeFloat(d_last - now); 
     197 
     198    d_last=now; 
     199    double factor=exp(diff/2.0)/2.0; // might be '0.5', or 0.0001 
     200    d_val=(1-factor)*val+ factor*d_val;  
     201  } 
     202 
     203  PulseRate& operator=(const PulseRate&); 
     204  struct timeval d_last;          // stores time 
     205  float d_val; 
    146206}; 
    147207 
     
    150210{ 
    151211public: 
    152   SyncRes() : d_outqueries(0), d_tcpoutqueries(0), d_throttledqueries(0), d_timeouts(0), d_cacheonly(false), d_nocache(false) { gettimeofday(&d_now, 0); } 
     212  explicit SyncRes(const struct timeval& now) : d_outqueries(0), d_tcpoutqueries(0), d_throttledqueries(0), d_timeouts(0),  
     213                                                d_cacheonly(false), d_nocache(false), d_now(now) { } 
    153214  int beginResolve(const string &qname, const QType &qtype, vector<DNSResourceRecord>&ret); 
    154215  void setId(int id) 
     
    267328  uint64_t nxDomains; 
    268329  uint64_t noErrors; 
     330  PulseRate queryrate; 
    269331}; 
    270332