5 int icacheprefetch = 1;
7 typedef struct ICache ICache;
8 typedef struct IHash IHash;
9 typedef struct ISum ISum;
35 * Hash table of IEntries
59 ih = vtmallocz(sizeof(IHash)+size*sizeof(ih->table[0]));
60 ih->table = (IEntry**)(ih+1);
67 ihashlookup(IHash *ih, u8int score[VtScoreSize], int type)
72 h = hashbits(score, ih->bits);
73 for(ie=ih->table[h]; ie; ie=ie->nexthash)
74 if((type == -1 || type == ie->ia.type) && scorecmp(score, ie->score) == 0)
80 ihashdelete(IHash *ih, IEntry *ie, char *what)
85 h = hashbits(ie->score, ih->bits);
86 for(l=&ih->table[h]; *l; l=&(*l)->nexthash)
91 fprint(2, "warning: %s %V not found in ihashdelete\n", what, ie->score);
95 ihashinsert(IHash *ih, IEntry *ie)
99 h = hashbits(ie->score, ih->bits);
100 ie->nexthash = ih->table[h];
112 if(ie->prev == nil && ie->next == nil)
114 ie->prev->next = ie->next;
115 ie->next->prev = ie->prev;
122 poplast(IEntry *list)
124 if(list->prev == list)
126 return popout(list->prev);
130 pushfirst(IEntry *list, IEntry *ie)
134 ie->next = list->next;
141 * Arena summary cache.
156 scachelookup(u64int addr)
161 for(i=0; i<icache.nsum; i++){
163 if(s->addr <= addr && addr < s->limit){
165 memmove(icache.sum+1, icache.sum, i*sizeof icache.sum[0]);
179 for(i=0; i<s->nentries; i++)
180 ihashdelete(icache.shash, &s->entries[i], "scache");
195 for(i=icache.nsum-1; i>=0; i--){
197 if(canqlock(&s->lock)){
199 memmove(icache.sum+1, icache.sum, i*sizeof icache.sum[0]);
210 scachehit(u64int addr)
212 scachelookup(addr); /* for move-to-front */
216 scachesetup(ISum *s, u64int addr)
221 s->arena = amapitoag(mainindex, addr, &addr0, &limit, &g);
233 n = asumload(s->arena, s->g, s->entries, ArenaCIGSize);
235 * n can be less then ArenaCIGSize, either if the clump group
236 * is the last in the arena and is only partially filled, or if there
237 * are corrupt clumps in the group -- those are not returned.
240 s->entries[i].ia.addr += s->addr;
241 ihashinsert(icache.shash, &s->entries[i]);
243 //fprint(2, "%T scacheload %s %d - %d entries\n", s->arena->name, s->g, n);
244 addstat(StatScachePrefetch, n);
249 scachemiss(u64int addr)
255 s = scachelookup(addr);
257 /* first time: make an entry in the cache but don't populate it yet */
261 scachesetup(s, addr);
266 /* second time: load from disk */
268 if(s->loaded || !icacheprefetch){
273 return s; /* locked */
281 initicache(u32int mem)
283 int i, entries, scache;
285 icache.full.l = &icache.lock;
287 entries = mem / (sizeof(IEntry)+sizeof(IEntry*));
288 scache = (entries/8) / ArenaCIGSize;
289 entries -= entries/8;
296 fprint(2, "icache %,lud bytes = %,lud entries; %lud scache\n", mem, entries, scache);
298 icache.clean.prev = icache.clean.next = &icache.clean;
299 icache.dirty.prev = icache.dirty.next = &icache.dirty;
300 icache.free.prev = icache.free.next = &icache.free;
302 icache.hash = mkihash(entries);
303 icache.nentries = entries;
304 setstat(StatIcacheSize, entries);
305 icache.entries = vtbrk(entries*sizeof icache.entries[0]);
306 icache.maxdirty = entries / 2;
307 for(i=0; i<entries; i++)
308 pushfirst(&icache.free, &icache.entries[i]);
310 icache.nsum = scache;
311 icache.sum = vtmallocz(scache*sizeof icache.sum[0]);
312 icache.sum[0] = vtmallocz(scache*sizeof icache.sum[0][0]);
313 icache.nsentries = scache * ArenaCIGSize;
314 icache.sentries = vtmallocz(scache*ArenaCIGSize*sizeof icache.sentries[0]);
315 icache.shash = mkihash(scache*ArenaCIGSize);
316 for(i=0; i<scache; i++){
317 icache.sum[i] = icache.sum[0] + i;
318 icache.sum[i]->entries = icache.sentries + i*ArenaCIGSize;
328 ie = poplast(&icache.clean);
331 ihashdelete(icache.hash, ie, "evictlru");
336 icacheinsert(u8int score[VtScoreSize], IAddr *ia, int state)
340 if((ie = poplast(&icache.free)) == nil && (ie = evictlru()) == nil){
341 addstat(StatIcacheStall, 1);
342 while((ie = poplast(&icache.free)) == nil && (ie = evictlru()) == nil){
343 // Could safely return here if state == IEClean.
344 // But if state == IEDirty, have to wait to make
345 // sure we don't lose an index write.
346 // Let's wait all the time.
349 rsleep(&icache.full);
351 addstat(StatIcacheStall, -1);
354 memmove(ie->score, score, VtScoreSize);
357 if(state == IEClean){
358 addstat(StatIcachePrefetch, 1);
359 pushfirst(&icache.clean, ie);
361 addstat(StatIcacheWrite, 1);
362 assert(state == IEDirty);
364 setstat(StatIcacheDirty, icache.ndirty);
366 pushfirst(&icache.dirty, ie);
368 ihashinsert(icache.hash, ie);
372 icachelookup(u8int score[VtScoreSize], int type, IAddr *ia)
377 addstat(StatIcacheLookup, 1);
378 if((ie = ihashlookup(icache.hash, score, type)) != nil){
380 if(ie->state == IEClean)
381 pushfirst(&icache.clean, ie);
382 addstat(StatIcacheHit, 1);
383 qunlock(&icache.lock);
387 if((ie = ihashlookup(icache.shash, score, type)) != nil){
389 icacheinsert(score, &ie->ia, IEClean);
390 scachehit(ie->ia.addr);
391 addstat(StatScacheHit, 1);
392 qunlock(&icache.lock);
395 addstat(StatIcacheMiss, 1);
396 qunlock(&icache.lock);
402 insertscore(u8int score[VtScoreSize], IAddr *ia, int state, AState *as)
407 icacheinsert(score, ia, state);
409 toload = scachemiss(ia->addr);
411 assert(state == IEDirty);
414 fprint(2, "%T insertscore IEDirty without as; called from %#p\n",
415 getcallerpc(&score));
417 if(icache.as.aa > as->aa)
418 fprint(2, "%T insertscore: aa moving backward: %#llux -> %#llux\n", icache.as.aa, as->aa);
422 qunlock(&icache.lock);
425 qunlock(&toload->lock);
428 if(icache.ndirty >= icache.maxdirty)
432 * It's okay not to do this under icache.lock.
433 * Calling insertscore only happens when we hold
434 * the lump, meaning any searches for this block
435 * will hit in the lump cache until after we return.
438 markbloomfilter(mainindex->bloom, score);
444 lookupscore(u8int score[VtScoreSize], int type, IAddr *ia)
449 if(icachelookup(score, type, ia) >= 0){
450 addstat(StatIcacheRead, 1);
455 addstat(StatIcacheFill, 1);
456 if(loadientry(mainindex, score, type, &d) < 0)
460 insertscore(score, &d.ia, IEClean, nil);
463 addstat2(StatIcacheRead, 1, StatIcacheReadTime, msec() - ms);
468 hashbits(u8int *sc, int bits)
472 v = (sc[0] << 24) | (sc[1] << 16) | (sc[2] << 8) | sc[3];
479 icachedirtyfrac(void)
481 return (vlong)icache.ndirty*IcacheFrac / icache.nentries;
485 * Return a singly-linked list of dirty index entries.
486 * with 32-bit hash numbers between lo and hi
487 * and address < limit.
490 icachedirty(u32int lo, u32int hi, u64int limit)
496 trace(TraceProc, "icachedirty enter");
498 for(ie = icache.dirty.next; ie != &icache.dirty; ie=ie->next){
499 if(ie->state == IEDirty && ie->ia.addr <= limit){
500 h = hashbits(ie->score, 32);
501 if(lo <= h && h <= hi){
502 ie->nextdirty = dirty;
507 qunlock(&icache.lock);
508 trace(TraceProc, "icachedirty exit");
521 qunlock(&icache.lock);
526 * The singly-linked non-circular list of index entries ie
527 * has been written to disk. Move them to the clean list.
530 icacheclean(IEntry *ie)
534 trace(TraceProc, "icacheclean enter");
537 assert(ie->state == IEDirty);
538 next = ie->nextdirty;
540 popout(ie); /* from icache.dirty */
543 pushfirst(&icache.clean, ie);
545 setstat(StatIcacheDirty, icache.ndirty);
546 rwakeupall(&icache.full);
547 qunlock(&icache.lock);
548 trace(TraceProc, "icacheclean exit");
559 while((ie = evictlru()) != nil)
560 pushfirst(&icache.free, ie);
561 for(i=0; i<icache.nsum; i++){
567 qunlock(&icache.lock);