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.
15 int nread, ncopy, nwrite;
33 u32int now; /* ticks for usage time stamps */
34 VtBlock **hash; /* hash table for finding addresses */
36 VtBlock **heap; /* heap for finding victims */
38 VtBlock *block; /* all allocated blocks */
40 uchar *mem; /* memory for all blocks and data */
41 int (*write)(VtConn*, uchar[VtScoreSize], uint, uchar*, int);
44 static void cachecheck(VtCache*);
47 vtcachealloc(VtConn *z, int blocksize, ulong nblock)
54 c = vtmallocz(sizeof(VtCache));
57 c->blocksize = (blocksize + 127) & ~127;
60 c->hash = vtmallocz(nblock*sizeof(VtBlock*));
61 c->heap = vtmallocz(nblock*sizeof(VtBlock*));
62 c->block = vtmallocz(nblock*sizeof(VtBlock));
63 c->mem = vtmallocz(nblock*c->blocksize);
67 for(i=0; i<nblock; i++){
82 * BUG This is here so that vbackup can override it and do some
83 * pipelining of writes. Arguably vtwrite or vtwritepacket or the
84 * cache itself should be providing this functionality.
87 vtcachesetwrite(VtCache *c, int (*write)(VtConn*, uchar[VtScoreSize], uint, uchar*, int))
95 vtcachefree(VtCache *c)
102 for(i=0; i<c->nblock; i++)
103 assert(c->block[i].ref == 0);
113 vtcachedump(VtCache *c)
118 for(i=0; i<c->nblock; i++){
120 print("cache block %d: type %d score %V iostate %d addr %d ref %d nlock %d\n",
121 i, b->type, b->score, b->iostate, b->addr, b->ref, b->nlock);
126 cachecheck(VtCache *c)
135 for(i = 0; i < c->nheap; i++){
136 if(c->heap[i]->heap != i)
137 sysfatal("mis-heaped at %d: %d", i, c->heap[i]->heap);
138 if(i > 0 && c->heap[(i - 1) >> 1]->used - now > c->heap[i]->used - now)
139 sysfatal("bad heap ordering");
141 if(k < c->nheap && c->heap[i]->used - now > c->heap[k]->used - now)
142 sysfatal("bad heap ordering");
144 if(k < c->nheap && c->heap[i]->used - now > c->heap[k]->used - now)
145 sysfatal("bad heap ordering");
149 for(i = 0; i < c->nblock; i++){
151 if(b->data != &c->mem[i * size])
152 sysfatal("mis-blocked at %d", i);
153 if(b->ref && b->heap == BadHeap)
155 else if(b->addr != NilBlock)
158 if(c->nheap + refed != c->nblock){
159 fprint(2, "cachecheck: nheap %d refed %d nblocks %d\n", c->nheap, refed, c->nblock);
162 assert(c->nheap + refed == c->nblock);
164 for(i = 0; i < c->nblock; i++){
167 if(1)fprint(2, "a=%ud %V ref=%d\n", b->addr, b->score, b->ref);
171 if(refed > 0)fprint(2, "cachecheck: in used %d\n", refed);
175 upheap(int i, VtBlock *b)
184 for(; i != 0; i = p){
187 if(b->used - now >= bb->used - now)
199 downheap(int i, VtBlock *b)
212 if(k + 1 < c->nheap && c->heap[k]->used - now > c->heap[k + 1]->used - now)
215 if(b->used - now <= bb->used - now)
226 * Delete a block from the heap.
227 * Called with c->lk held.
244 b = c->heap[c->nheap];
251 * Insert a block into the heap.
252 * Called with c->lk held.
257 assert(b->heap == BadHeap);
258 upheap(b->c->nheap++, b);
262 * locate the vtBlock with the oldest second to last use.
263 * remove it from the heap, and fix up the heap.
265 /* called with c->lk held */
267 vtcachebumpblock(VtCache *c)
272 * locate the vtBlock with the oldest second to last use.
273 * remove it from the heap, and fix up the heap.
277 fprint(2, "vtcachebumpblock: no free blocks in vtCache");
283 assert(b->heap == BadHeap);
287 * unchain the vtBlock from hash chain if any
290 *(b->prev) = b->next;
292 b->next->prev = b->prev;
297 if(0)fprint(2, "droping %x:%V\n", b->addr, b->score);
298 /* set vtBlock to a reasonable state */
300 b->iostate = BioEmpty;
305 * fetch a local block from the memory cache.
306 * if it's not there, load it, bumping some other Block.
307 * if we're out of free blocks, we're screwed.
310 vtcachelocal(VtCache *c, u32int addr, int type)
315 sysfatal("vtcachelocal: asked for nonexistent block 0");
317 sysfatal("vtcachelocal: asked for block #%ud; only %d blocks",
320 b = &c->block[addr-1];
321 if(b->addr == NilBlock || b->iostate != BioLocal)
322 sysfatal("vtcachelocal: block is not local");
325 sysfatal("vtcachelocal: block has wrong type %d != %d", b->type, type);
337 vtcacheallocblock(VtCache *c, int type)
342 b = vtcachebumpblock(c);
343 b->iostate = BioLocal;
345 b->addr = (b - c->block)+1;
346 vtzeroextend(type, b->data, 0, c->blocksize);
347 vtlocaltoglobal(b->addr, b->score);
357 * fetch a global (Venti) block from the memory cache.
358 * if it's not there, load it, bumping some other block.
361 vtcacheglobal(VtCache *c, uchar score[VtScoreSize], int type)
368 addr = vtglobaltolocal(score);
370 return vtcachelocal(c, addr, type);
372 h = (u32int)(score[0]|(score[1]<<8)|(score[2]<<16)|(score[3]<<24)) % c->nhash;
375 * look for the block in the cache
378 for(b = c->hash[h]; b != nil; b = b->next){
379 if(b->addr != NilBlock || memcmp(b->score, score, VtScoreSize) != 0 || b->type != type)
386 if(b->iostate == BioVentiError){
388 fprint(2, "cached read error for %V\n", score);
389 werrstr("venti i/o error");
399 b = vtcachebumpblock(c);
402 memmove(b->score, score, VtScoreSize);
403 /* chain onto correct hash */
404 b->next = c->hash[h];
407 b->next->prev = &b->next;
408 b->prev = &c->hash[h];
411 * Lock b before unlocking c, so that others wait while we read.
413 * You might think there is a race between this qlock(b) before qunlock(c)
414 * and the qlock(c) while holding a qlock(b) in vtblockwrite. However,
415 * the block here can never be the block in a vtblockwrite, so we're safe.
416 * We're certainly living on the edge.
422 n = vtread(c->z, score, type, b->data, c->blocksize);
424 werrstr("vtread %V: %r", score);
426 fprint(2, "read %V: %r\n", score);
427 b->iostate = BioVentiError;
431 vtzeroextend(type, b->data, n, c->blocksize);
432 b->iostate = BioVenti;
438 * The thread that has locked b may refer to it by
439 * multiple names. Nlock counts the number of
440 * references the locking thread holds. It will call
441 * vtblockput once per reference.
444 vtblockduplock(VtBlock *b)
446 assert(b->nlock > 0);
451 * we're done with the block.
452 * unlock it. can't use it after calling this.
455 vtblockput(VtBlock* b)
462 if(0)fprint(2, "vtblockput: %d: %x %d %d\n", getpid(), b->addr, c->nheap, b->iostate);
468 * b->nlock should probably stay at zero while
469 * the vtBlock is unlocked, but diskThread and vtSleep
470 * conspire to assume that they can just qlock(&b->lk); vtblockput(b),
471 * so we have to keep b->nlock set to 1 even
472 * when the vtBlock is unlocked.
474 assert(b->nlock == 0);
489 //if(b->addr != NilBlock) print("blockput %d\n", b->addr);
501 vtblockwrite(VtBlock *b)
503 uchar score[VtScoreSize];
508 if(b->iostate != BioLocal){
509 werrstr("vtblockwrite: not a local block");
514 n = vtzerotruncate(b->type, b->data, c->blocksize);
515 if(c->write(c->z, score, b->type, b->data, n) < 0)
518 memmove(b->score, score, VtScoreSize);
521 b->iostate = BioVenti;
522 h = (u32int)(score[0]|(score[1]<<8)|(score[2]<<16)|(score[3]<<24)) % c->nhash;
523 b->next = c->hash[h];
526 b->next->prev = &b->next;
527 b->prev = &c->hash[h];
533 vtcacheblocksize(VtCache *c)
539 vtblockcopy(VtBlock *b)
544 bb = vtcacheallocblock(b->c, b->type);
549 memmove(bb->data, b->data, b->c->blocksize);
555 vtlocaltoglobal(u32int addr, uchar score[VtScoreSize])
557 memset(score, 0, 16);
558 score[16] = addr>>24;
559 score[17] = addr>>16;
566 vtglobaltolocal(uchar score[VtScoreSize])
568 static uchar zero[16];
569 if(memcmp(score, zero, 16) != 0)
571 return (score[16]<<24)|(score[17]<<16)|(score[18]<<8)|score[19];