Changeset 599
- Timestamp:
- 03/19/06 20:20:17 (7 years ago)
- Files:
-
- 1 modified
-
trunk/pdns/pdns/syncres.hh (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pdns/pdns/syncres.hh
r594 r599 16 16 #include "recursor_cache.hh" 17 17 #include <boost/tuple/tuple.hpp> 18 #include <boost/optional.hpp> 18 19 #include <boost/tuple/tuple_comparison.hpp> 19 20 #include "mtasker.hh" … … 97 98 { 98 99 public: 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 } 106 119 } 107 120 108 121 void submit(int val, struct timeval*tv = 0) 109 122 { 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 117 132 d_last=now; 118 floatfactor=exp(diff)/2.0; // might be '0.5', or 0.0001133 double factor=exp(diff)/2.0; // might be '0.5', or 0.0001 119 134 d_val=(1-factor)*val+ factor*d_val; 120 135 } 121 136 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); 131 142 d_lastget=now; 132 143 float factor=exp(diff/60.0); // is 1.0 or less … … 136 147 bool stale(time_t limit) 137 148 { 138 return limit > d_lastget ;149 return limit > d_lastget.tv_sec; 139 150 } 140 151 141 152 private: 142 153 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 145 156 float d_val; 157 bool d_needinit; 158 }; 159 160 161 class PulseRate 162 { 163 public: 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 193 private: 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; 146 206 }; 147 207 … … 150 210 { 151 211 public: 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) { } 153 214 int beginResolve(const string &qname, const QType &qtype, vector<DNSResourceRecord>&ret); 154 215 void setId(int id) … … 267 328 uint64_t nxDomains; 268 329 uint64_t noErrors; 330 PulseRate queryrate; 269 331 }; 270 332