Skip to content

Commit

Permalink
adaptive packetcache cleaning interval
Browse files Browse the repository at this point in the history
  • Loading branch information
mind04 committed Oct 1, 2016
1 parent 8c03126 commit 8c879d4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
29 changes: 29 additions & 0 deletions pdns/packetcache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ PacketCache::PacketCache()
d_ttl=-1;
d_recursivettl=-1;

d_lastclean=time(0);
d_cleanskipped=false;
d_nextclean=d_cleaninterval=4096;

S.declare("packetcache-hit");
S.declare("packetcache-miss");
S.declare("packetcache-size");
Expand Down Expand Up @@ -429,3 +433,28 @@ void PacketCache::cleanup()

DLOG(L<<"Done with cache clean"<<endl);
}

void PacketCache::cleanupIfNeeded()
{
if (d_ops++ == d_nextclean) {
int timediff = max((int)(time(0) - d_lastclean), 1);

DLOG(L<<"cleaninterval: "<<d_cleaninterval<<", timediff: "<<timediff<<endl);

if (d_cleaninterval == 300000 && timediff < 30) {
d_cleanskipped = true;
d_nextclean += d_cleaninterval;

DLOG(L<<"cleaning skipped, timediff: "<<timediff<<endl);

return;
}
if(d_cleanskipped) {
d_cleanskipped = false;
}

d_nextclean += d_cleaninterval;
d_lastclean=time(0);
cleanup();
}
}
15 changes: 5 additions & 10 deletions pdns/packetcache.hh
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,7 @@ public:


int size() { return *d_statnumentries; } //!< number of entries in the cache
void cleanupIfNeeded()
{
if(!(++d_ops % 300000)) {
if(d_lastclean + 30 < time(0)) {
d_lastclean=time(0);
cleanup();
}
}
}
void cleanupIfNeeded();
void cleanup(); //!< force the cache to preen itself from expired packets
int purge();
int purge(const std::string& match); // could be $ terminated. Is not a dnsname!
Expand Down Expand Up @@ -147,7 +139,10 @@ private:
}

AtomicCounter d_ops;
time_t d_lastclean{0}; // doesn't need to be atomic
time_t d_lastclean; // doesn't need to be atomic
unsigned long d_nextclean;
int d_cleaninterval;
bool d_cleanskipped;
AtomicCounter *d_statnumhit;
AtomicCounter *d_statnummiss;
AtomicCounter *d_statnumentries;
Expand Down
13 changes: 0 additions & 13 deletions regression-tests/startpdns

This file was deleted.

0 comments on commit 8c879d4

Please sign in to comment.