Show
Ignore:
Timestamp:
08/22/09 21:20:17 (4 years ago)
Author:
ahu
Message:

repair cache cleaning logic, which would never run in certain benchmark situations, plus retune the actual cleaning process.
Thanks to Marcus Goller for reporting this issue.

Files:
1 modified

Legend:

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

    r1365 r1399  
    2929{ 
    3030  pthread_rwlock_init(&d_mut,0); 
    31   d_hit=d_miss=0; 
     31  d_ops = 0; 
    3232 
    3333  d_ttl=-1; 
     
    5151{ 
    5252  extern StatBag S; 
    53   if(!((d_hit+d_miss)%150000)) { 
    54     cleanup(); 
    55   } 
    5653 
    5754  if(d_ttl<0)  
    5855    getTTLS(); 
     56 
     57  if(!((d_ops++) % 300000)) { 
     58    cleanup(); 
     59  } 
     60 
    5961 
    6062  if(d_doRecursion && p->d.rd) { // wants recursion 
    6163    if(!d_recursivettl) { 
    6264      (*d_statnummiss)++; 
    63       d_miss++; 
    6465      return 0; 
    6566    } 
     
    6869    if(!d_ttl) { 
    6970      (*d_statnummiss)++; 
    70       d_miss++; 
    7171      return 0; 
    7272    } 
     
    8686    } 
    8787 
    88     if(!((d_hit+d_miss)%30000)) { 
    89       *d_statnumentries=d_map.size(); // needs lock 
    90     } 
    91     haveSomething=getEntry(p->qdomain, p->qtype, PacketCache::PACKETCACHE, value, -1, packetMeritsRecursion); 
     88    haveSomething=getEntryLocked(p->qdomain, p->qtype, PacketCache::PACKETCACHE, value, -1, packetMeritsRecursion); 
    9289  } 
    9390  if(haveSomething) { 
    9491    (*d_statnumhit)++; 
    95     d_hit++; 
    9692    if(cached->noparse(value.c_str(), value.size()) < 0) { 
    9793      return 0; 
     
    10399  //  cerr<<"Packet cache miss for '"<<p->qdomain<<"', merits: "<<packetMeritsRecursion<<endl; 
    104100  (*d_statnummiss)++; 
    105   d_miss++; 
    106101  return 0; // bummer 
    107102} 
     
    133128void PacketCache::insert(const string &qname, const QType& qtype, CacheEntryType cet, const string& value, unsigned int ttl, int zoneID, bool meritsRecursion) 
    134129{ 
     130  if(!((d_ops++) % 300000)) { 
     131    cleanup(); 
     132  } 
     133 
    135134  if(!ttl) 
    136135    return; 
     
    249248bool PacketCache::getEntry(const string &qname, const QType& qtype, CacheEntryType cet, string& value, int zoneID, bool meritsRecursion) 
    250249{ 
     250  if(d_ttl<0)  
     251    getTTLS(); 
     252 
     253  if(!((d_ops++) % 300000)) { 
     254    cleanup(); 
     255  } 
     256 
    251257  TryReadLock l(&d_mut); // take a readlock here 
    252258  if(!l.gotIt()) { 
     
    254260    return false; 
    255261  } 
     262  return getEntryLocked(qname, qtype, cet, value, zoneID, meritsRecursion); 
     263} 
     264 
     265bool PacketCache::getEntryLocked(const string &qname, const QType& qtype, CacheEntryType cet, string& value, int zoneID, bool meritsRecursion) 
     266{ 
    256267 
    257268  uint16_t qt = qtype.getCode(); 
     
    261272  if(ret) 
    262273    value = i->value; 
    263    
    264   //  cerr<<"Cache hit: "<<(int)cet<<", "<<ret<<endl; 
    265274   
    266275  return ret; 
     
    317326 
    318327  unsigned int lookAt=0; 
    319   // two modes - if toTrim is 0, just look through 10000 records and nuke everything that is expired 
     328  // two modes - if toTrim is 0, just look through 10%  of the cache and nuke everything that is expired 
    320329  // otherwise, scan first 5*toTrim records, and stop once we've nuked enough 
    321330  if(toTrim) 
     
    333342  typedef cmap_t::nth_index<1>::type sequence_t; 
    334343  sequence_t& sidx=d_map.get<1>(); 
    335   unsigned int erased=0; 
    336   for(sequence_t::iterator i=sidx.begin(); i != sidx.end();) { 
     344  unsigned int erased=0, lookedAt=0; 
     345  for(sequence_t::iterator i=sidx.begin(); i != sidx.end(); lookedAt++) { 
    337346    if(i->ttd < now) { 
    338347      sidx.erase(i++); 
     
    345354      break; 
    346355 
     356    if(lookedAt > lookAt) 
     357      break; 
    347358  } 
    348359  //  cerr<<"erased: "<<erased<<endl;