2 * Memory-only VtBlock cache.
4 * The cached Venti blocks are in the hash chains.
5 * The cached local blocks are only in the blocks array.
6 * The free blocks are in the heap, which is supposed to
7 * be indexed by second-to-last use but actually
8 * appears to be last use.
35 u32int now; /* ticks for usage time stamps */
36 VtBlock **hash; /* hash table for finding addresses */
38 VtBlock **heap; /* heap for finding victims */
40 VtBlock *block; /* all allocated blocks */
42 uchar *mem; /* memory for all blocks and data */
43 int (*write)(VtConn*, uchar[VtScoreSize], uint, uchar*, int);
46 static void cachecheck(VtCache*);
49 vtcachealloc(VtConn *z, int blocksize, ulong nblock)
56 c = vtmallocz(sizeof(VtCache));
59 c->blocksize = (blocksize + 127) & ~127;
62 c->hash = vtmallocz(nblock*sizeof(VtBlock*));
63 c->heap = vtmallocz(nblock*sizeof(VtBlock*));
64 c->block = vtmallocz(nblock*sizeof(VtBlock));
65 c->mem = vtmallocz(nblock*c->blocksize);
69 for(i=0; i<nblock; i++){
84 * BUG This is here so that vbackup can override it and do some
85 * pipelining of writes. Arguably vtwrite or vtwritepacket or the
86 * cache itself should be providing this functionality.
89 vtcachesetwrite(VtCache *c, int (*write)(VtConn*, uchar[VtScoreSize], uint, uchar*, int))
97 vtcachefree(VtCache *c)
104 for(i=0; i<c->nblock; i++)
105 assert(c->block[i].ref == 0);
115 vtcachedump(VtCache *c)
120 for(i=0; i<c->nblock; i++){
122 print("cache block %d: type %d score %V iostate %d addr %d ref %d nlock %d\n",
123 i, b->type, b->score, b->iostate, b->addr, b->ref, b->nlock);
128 cachecheck(VtCache *c)
137 for(i = 0; i < c->nheap; i++){
138 if(c->heap[i]->heap != i)
139 sysfatal("mis-heaped at %d: %d", i, c->heap[i]->heap);
140 if(i > 0 && c->heap[(i - 1) >> 1]->used - now > c->heap[i]->used - now)
141 sysfatal("bad heap ordering");
143 if(k < c->nheap && c->heap[i]->used - now > c->heap[k]->used - now)
144 sysfatal("bad heap ordering");
146 if(k < c->nheap && c->heap[i]->used - now > c->heap[k]->used - now)
147 sysfatal("bad heap ordering");
151 for(i = 0; i < c->nblock; i++){
153 if(b->data != &c->mem[i * size])
154 sysfatal("mis-blocked at %d", i);
155 if(b->ref && b->heap == BadHeap)
157 else if(b->addr != NilBlock)
160 if(c->nheap + refed != c->nblock){
161 fprint(2, "cachecheck: nheap %d refed %d nblocks %d\n", c->nheap, refed, c->nblock);
164 assert(c->nheap + refed == c->nblock);
166 for(i = 0; i < c->nblock; i++){
169 if(1)fprint(2, "a=%ud %V ref=%d\n", b->addr, b->score, b->ref);
173 if(refed > 0)fprint(2, "cachecheck: in used %d\n", refed);
177 upheap(int i, VtBlock *b)
186 for(; i != 0; i = p){
189 if(b->used - now >= bb->used - now)
201 downheap(int i, VtBlock *b)
214 if(k + 1 < c->nheap && c->heap[k]->used - now > c->heap[k + 1]->used - now)
217 if(b->used - now <= bb->used - now)
228 * Delete a block from the heap.
229 * Called with c->lk held.
246 b = c->heap[c->nheap];
253 * Insert a block into the heap.
254 * Called with c->lk held.
259 assert(b->heap == BadHeap);
260 upheap(b->c->nheap++, b);
264 * locate the vtBlock with the oldest second to last use.
265 * remove it from the heap, and fix up the heap.
267 /* called with c->lk held */
269 vtcachebumpblock(VtCache *c)
274 * locate the vtBlock with the oldest second to last use.
275 * remove it from the heap, and fix up the heap.
279 fprint(2, "vtcachebumpblock: no free blocks in vtCache");
285 assert(b->heap == BadHeap);
289 * unchain the vtBlock from hash chain if any
292 *(b->prev) = b->next;
294 b->next->prev = b->prev;
299 if(0)fprint(2, "droping %x:%V\n", b->addr, b->score);
300 /* set vtBlock to a reasonable state */
302 b->iostate = BioEmpty;
307 * fetch a local block from the memory cache.
308 * if it's not there, load it, bumping some other Block.
309 * if we're out of free blocks, we're screwed.
312 vtcachelocal(VtCache *c, u32int addr, int type)
317 sysfatal("vtcachelocal: asked for nonexistent block 0");
319 sysfatal("vtcachelocal: asked for block #%ud; only %d blocks",
322 b = &c->block[addr-1];
323 if(b->addr == NilBlock || b->iostate != BioLocal)
324 sysfatal("vtcachelocal: block is not local");
327 sysfatal("vtcachelocal: block has wrong type %d != %d", b->type, type);
339 vtcacheallocblock(VtCache *c, int type)
344 b = vtcachebumpblock(c);
345 b->iostate = BioLocal;
347 b->addr = (b - c->block)+1;
348 vtzeroextend(type, b->data, 0, c->blocksize);
349 vtlocaltoglobal(b->addr, b->score);
359 * fetch a global (Venti) block from the memory cache.
360 * if it's not there, load it, bumping some other block.
363 vtcacheglobal(VtCache *c, uchar score[VtScoreSize], int type)
370 addr = vtglobaltolocal(score);
372 return vtcachelocal(c, addr, type);
374 h = (u32int)(score[0]|(score[1]<<8)|(score[2]<<16)|(score[3]<<24)) % c->nhash;
377 * look for the block in the cache
380 for(b = c->hash[h]; b != nil; b = b->next){
381 if(b->addr != NilBlock || memcmp(b->score, score, VtScoreSize) != 0 || b->type != type)
388 if(b->iostate == BioVentiError){
390 fprint(2, "cached read error for %V\n", score);
391 werrstr("venti i/o error");
401 b = vtcachebumpblock(c);
404 memmove(b->score, score, VtScoreSize);
405 /* chain onto correct hash */
406 b->next = c->hash[h];
409 b->next->prev = &b->next;
410 b->prev = &c->hash[h];
413 * Lock b before unlocking c, so that others wait while we read.
415 * You might think there is a race between this qlock(b) before qunlock(c)
416 * and the qlock(c) while holding a qlock(b) in vtblockwrite. However,
417 * the block here can never be the block in a vtblockwrite, so we're safe.
418 * We're certainly living on the edge.
425 n = vtread(c->z, score, type, b->data, c->blocksize);
428 fprint(2, "read %V: %r\n", score);
429 b->iostate = BioVentiError;
433 vtzeroextend(type, b->data, n, c->blocksize);
434 b->iostate = BioVenti;
440 * The thread that has locked b may refer to it by
441 * multiple names. Nlock counts the number of
442 * references the locking thread holds. It will call
443 * vtblockput once per reference.
446 vtblockduplock(VtBlock *b)
448 assert(b->nlock > 0);
453 * we're done with the block.
454 * unlock it. can't use it after calling this.
457 vtblockput(VtBlock* b)
464 if(0)fprint(2, "vtblockput: %d: %x %d %d\n", getpid(), b->addr, c->nheap, b->iostate);
470 * b->nlock should probably stay at zero while
471 * the vtBlock is unlocked, but diskThread and vtSleep
472 * conspire to assume that they can just qlock(&b->lk); vtblockput(b),
473 * so we have to keep b->nlock set to 1 even
474 * when the vtBlock is unlocked.
476 assert(b->nlock == 0);
491 //if(b->addr != NilBlock) print("blockput %d\n", b->addr);
503 vtblockwrite(VtBlock *b)
505 uchar score[VtScoreSize];
510 if(b->iostate != BioLocal){
511 werrstr("vtblockwrite: not a local block");
516 n = vtzerotruncate(b->type, b->data, c->blocksize);
518 if(c->write(c->z, score, b->type, b->data, n) < 0)
521 memmove(b->score, score, VtScoreSize);
524 b->iostate = BioVenti;
525 h = (u32int)(score[0]|(score[1]<<8)|(score[2]<<16)|(score[3]<<24)) % c->nhash;
526 b->next = c->hash[h];
529 b->next->prev = &b->next;
530 b->prev = &c->hash[h];
536 vtcacheblocksize(VtCache *c)
542 vtblockcopy(VtBlock *b)
547 bb = vtcacheallocblock(b->c, b->type);
552 memmove(bb->data, b->data, b->c->blocksize);
558 vtlocaltoglobal(u32int addr, uchar score[VtScoreSize])
560 memset(score, 0, 16);
561 score[16] = addr>>24;
562 score[17] = addr>>16;
569 vtglobaltolocal(uchar score[VtScoreSize])
571 static uchar zero[16];
572 if(memcmp(score, zero, 16) != 0)
574 return (score[16]<<24)|(score[17]<<16)|(score[18]<<8)|score[19];