ref: 5c9d1ecdfb26ab64d8039d8e536d265e2cf8b774
parent: 17212ba53ebc86fc2c05055d92968bd16cb30a1b
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Oct 25 17:43:27 EDT 2021
cache: extract into its own file
--- a/blk.c
+++ b/blk.c
@@ -16,7 +16,6 @@
static vlong blkalloc_lk(Arena*);
static int blkdealloc_lk(vlong);
static void cachedel(vlong);
-static Blk *cacheblk(Blk*);
static Blk *lookupblk(vlong);
Blk*
@@ -497,7 +496,6 @@
r = -1;
a = getarena(b);
- cachedel(b);
if(freerange(a->free, b, Blksz) == -1)
goto out;
if(logop(a, b, LogFree) == -1)
@@ -597,105 +595,7 @@
return b;
}
-static Blk*
-cacheblk(Blk *b)
-{
- Bucket *bkt;
- Blk *e, *c;
- u32int h;
- /* FIXME: better hash. */
- refblk(b);
- assert(b->bp.addr != 0);
- assert(!(b->flag & Bzombie));
- h = ihash(b->bp.addr);
- bkt = &fs->cache[h % fs->cmax];
- lock(bkt);
- for(e = bkt->b; e != nil; e = e->hnext){
- if(b == e)
- goto Found;
- assert(b->bp.addr != e->bp.addr);
- }
- bkt->b = b;
-Found:
- unlock(bkt);
-
- lock(&fs->lrulk);
- if(b == fs->chead)
- goto Cached;
- if(b == fs->ctail)
- fs->ctail = b->cprev;
-
- if(b->cnext != nil)
- b->cnext->cprev = b->cprev;
- if(b->cprev != nil)
- b->cprev->cnext = b->cnext;
- if(fs->ctail == nil)
- fs->ctail = b;
- if(fs->chead != nil)
- fs->chead->cprev = b;
- if(fs->ctail == nil)
- fs->ctail = b;
- b->cnext = fs->chead;
- b->cprev = nil;
- fs->chead = b;
- if((b->flag&Bcached) == 0){
- wlock(b);
- b->flag |= Bcached;
- wunlock(b);
- fs->ccount++;
- refblk(b);
- }
- c=0;
- USED(c);
-/*
- for(c = fs->ctail; c != nil && fs->ccount >= fs->cmax; c = fs->ctail){
- fs->ctail = c->cprev;
- fs->ccount--;
- putblk(c);
- }
-*/
-Cached:
- unlock(&fs->lrulk);
- return b;
-}
-
-static void
-cachedel(vlong del)
-{
- Bucket *bkt;
- Blk *b, **p;
- u32int h;
-
- /* FIXME: better hash. */
- h = ihash(del);
-
- bkt = &fs->cache[h % fs->cmax];
- lock(bkt);
- p = &bkt->b;
- for(b = bkt->b; b != nil; b = b->hnext){
- if(b->bp.addr == del){
- *p = b->hnext;
- break;
- }
- p = &b->hnext;
- }
- unlock(bkt);
- if(b == nil)
- return;
-
- lock(&fs->lrulk);
- if(b->cnext != nil)
- b->cnext->cprev = b->cprev;
- if(b->cprev != nil)
- b->cprev->cnext = b->cnext;
- if(fs->ctail == b)
- fs->ctail = b->cprev;
- if(fs->chead == nil)
- fs->chead = b;
- unlock(&fs->lrulk);
-}
-
int
syncblk(Blk *b)
{
@@ -845,8 +745,6 @@
if(adec(&b->ref) == 0){
assert(0);
assert((b->flag & Bqueued) || !(b->flag & Bdirty));
- cachedel(b->bp.addr);
- assert(lookupblk(b->bp.addr) == nil);
free(b);
}
}
--- /dev/null
+++ b/cache.c
@@ -1,0 +1,106 @@
+#include <u.h>
+#include <libc.h>
+#include <fcall.h>
+#include <avl.h>
+
+#include "dat.h"
+#include "fns.h"
+
+Blk*
+cacheblk(Blk *b)
+{
+ Bucket *bkt;
+ Blk *e, *c;
+ u32int h;
+
+ /* FIXME: better hash. */
+ refblk(b);
+ assert(b->bp.addr != 0);
+ assert(!(b->flag & Bzombie));
+ h = ihash(b->bp.addr);
+ bkt = &fs->cache[h % fs->cmax];
+ lock(bkt);
+ for(e = bkt->b; e != nil; e = e->hnext){
+ if(b == e)
+ goto Found;
+ assert(b->bp.addr != e->bp.addr);
+ }
+ bkt->b = b;
+Found:
+ unlock(bkt);
+
+ lock(&fs->lrulk);
+ if(b == fs->chead)
+ goto Cached;
+ if(b == fs->ctail)
+ fs->ctail = b->cprev;
+
+ if(b->cnext != nil)
+ b->cnext->cprev = b->cprev;
+ if(b->cprev != nil)
+ b->cprev->cnext = b->cnext;
+ if(fs->ctail == nil)
+ fs->ctail = b;
+ if(fs->chead != nil)
+ fs->chead->cprev = b;
+ if(fs->ctail == nil)
+ fs->ctail = b;
+ b->cnext = fs->chead;
+ b->cprev = nil;
+ fs->chead = b;
+ if((b->flag&Bcached) == 0){
+ wlock(b);
+ b->flag |= Bcached;
+ wunlock(b);
+ fs->ccount++;
+ refblk(b);
+ }
+ c=0;
+ USED(c);
+/*
+ for(c = fs->ctail; c != nil && fs->ccount >= fs->cmax; c = fs->ctail){
+ fs->ctail = c->cprev;
+ fs->ccount--;
+ putblk(c);
+ }
+*/
+Cached:
+ unlock(&fs->lrulk);
+ return b;
+}
+
+static void
+cachedel(vlong del)
+{
+ Bucket *bkt;
+ Blk *b, **p;
+ u32int h;
+
+ /* FIXME: better hash. */
+ h = ihash(del);
+
+ bkt = &fs->cache[h % fs->cmax];
+ lock(bkt);
+ p = &bkt->b;
+ for(b = bkt->b; b != nil; b = b->hnext){
+ if(b->bp.addr == del){
+ *p = b->hnext;
+ break;
+ }
+ p = &b->hnext;
+ }
+ unlock(bkt);
+ if(b == nil)
+ return;
+
+ lock(&fs->lrulk);
+ if(b->cnext != nil)
+ b->cnext->cprev = b->cprev;
+ if(b->cprev != nil)
+ b->cprev->cnext = b->cnext;
+ if(fs->ctail == b)
+ fs->ctail = b->cprev;
+ if(fs->chead == nil)
+ fs->chead = b;
+ unlock(&fs->lrulk);
+}
--- a/fns.h
+++ b/fns.h
@@ -10,10 +10,10 @@
extern int debug;
Blk* newblk(int type);
-Blk* shadow(Blk*, Path*, Path*);
Blk* getroot(Tree*, int*);
Blk* getblk(Bptr, int);
Blk* refblk(Blk*);
+Blk* cacheblk(Blk*);
Blk* readblk(vlong, int);
Arena* getarena(vlong);
void putblk(Blk*);
--- a/mkfile
+++ b/mkfile
@@ -3,6 +3,7 @@
TARG=gefs
OFILES=\
blk.$O\
+ cache.$O\
check.$O\
fs.$O\
hash.$O\