kernel: make mntcache effective, be carefull

This commit is contained in:
cinap_lenrek 2011-12-22 02:17:29 +01:00
parent 27830ae53e
commit ea5a23d39a
4 changed files with 32 additions and 34 deletions

View file

@ -113,10 +113,8 @@ cinit(void)
panic("cinit: no memory"); panic("cinit: no memory");
/* a better algorithm would be nice */ /* a better algorithm would be nice */
// if(conf.npage*BY2PG > 200*MB) if(conf.npage*BY2PG > 200*MB)
// maxcache = 10*MAXCACHE; maxcache = 10*MAXCACHE;
// if(conf.npage*BY2PG > 400*MB)
// maxcache = 50*MAXCACHE;
for(i = 0; i < NFILE-1; i++) { for(i = 0; i < NFILE-1; i++) {
m->next = m+1; m->next = m+1;
@ -171,17 +169,16 @@ cpage(Extent *e)
void void
cnodata(Mntcache *m) cnodata(Mntcache *m)
{ {
Extent *e, *n; Extent *e;
/* /*
* Invalidate all extent data * Invalidate all extent data
* Image lru will waste the pages * Image lru will waste the pages
*/ */
for(e = m->list; e; e = n) { while(e = m->list){
n = e->next; m->list = e->next;
extentfree(e); extentfree(e);
} }
m->list = 0;
} }
void void
@ -215,36 +212,35 @@ void
copen(Chan *c) copen(Chan *c)
{ {
int h; int h;
Extent *e, *next;
Mntcache *m, *f, **l; Mntcache *m, *f, **l;
/* directories aren't cacheable and append-only files confuse us */ /* directories aren't cacheable and append-only files confuse us */
if(c->qid.type&(QTDIR|QTAPPEND)) if(c->qid.type&(QTDIR|QTAPPEND))
return; return;
h = c->qid.path%NHASH; h = c->qid.path%NHASH;
lock(&cache); lock(&cache);
for(m = cache.hash[h]; m; m = m->hash) { for(m = cache.hash[h]; m; m = m->hash) {
if(m->qid.path == c->qid.path) if(m->qid.path == c->qid.path)
if(m->qid.type == c->qid.type) if(m->qid.type == c->qid.type)
if(m->dev == c->dev && m->type == c->type) { if(m->dev == c->dev && m->type == c->type) {
c->mcp = m;
ctail(m);
unlock(&cache);
/* File was updated, invalidate cache */ /* File was updated, invalidate cache */
if(m->qid.vers != c->qid.vers) { if(m->qid.vers != c->qid.vers){
if(!canqlock(m))
goto Busy;
m->qid.vers = c->qid.vers; m->qid.vers = c->qid.vers;
qlock(m); goto Update;
cnodata(m);
qunlock(m);
} }
ctail(m);
c->mcp = m;
unlock(&cache);
return; return;
} }
} }
/* LRU the cache headers */ /* LRU the cache headers */
m = cache.head; m = cache.head;
if(!canqlock(m))
goto Busy;
l = &cache.hash[m->qid.path%NHASH]; l = &cache.hash[m->qid.path%NHASH];
for(f = *l; f; f = f->hash) { for(f = *l; f; f = f->hash) {
if(f == m) { if(f == m) {
@ -261,20 +257,16 @@ copen(Chan *c)
l = &cache.hash[h]; l = &cache.hash[h];
m->hash = *l; m->hash = *l;
*l = m; *l = m;
Update:
ctail(m); ctail(m);
qlock(m);
c->mcp = m; c->mcp = m;
e = m->list;
m->list = 0;
unlock(&cache); unlock(&cache);
cnodata(m);
while(e) {
next = e->next;
extentfree(e);
e = next;
}
qunlock(m); qunlock(m);
return;
Busy:
unlock(&cache);
c->mcp = 0;
} }
static int static int
@ -313,6 +305,7 @@ cread(Chan *c, uchar *buf, int len, vlong off)
qlock(m); qlock(m);
if(cdev(m, c) == 0) { if(cdev(m, c) == 0) {
qunlock(m); qunlock(m);
c->mcp = 0;
return 0; return 0;
} }
@ -483,6 +476,7 @@ cupdate(Chan *c, uchar *buf, int len, vlong off)
qlock(m); qlock(m);
if(cdev(m, c) == 0) { if(cdev(m, c) == 0) {
qunlock(m); qunlock(m);
c->mcp = 0;
return; return;
} }
@ -541,7 +535,9 @@ cupdate(Chan *c, uchar *buf, int len, vlong off)
len -= o; len -= o;
offset += o; offset += o;
if(len <= 0) { if(len <= 0) {
if(f && p->start + p->len > f->start) print("CACHE: p->start=%uld p->len=%d f->start=%uld\n", p->start, p->len, f->start); if(f && p->start + p->len > f->start)
print("CACHE: p->start=%uld p->len=%d f->start=%uld\n",
p->start, p->len, f->start);
qunlock(m); qunlock(m);
return; return;
} }
@ -575,6 +571,7 @@ cwrite(Chan* c, uchar *buf, int len, vlong off)
qlock(m); qlock(m);
if(cdev(m, c) == 0) { if(cdev(m, c) == 0) {
qunlock(m); qunlock(m);
c->mcp = 0;
return; return;
} }

View file

@ -1488,9 +1488,6 @@ if(c->umh != nil){
/* save registers else error() in open has wrong value of c saved */ /* save registers else error() in open has wrong value of c saved */
saveregisters(); saveregisters();
if(omode == OEXEC)
c->flag &= ~CCACHE;
c = devtab[c->type]->open(c, omode&~OCEXEC); c = devtab[c->type]->open(c, omode&~OCEXEC);
if(omode & OCEXEC) if(omode & OCEXEC)

View file

@ -409,6 +409,7 @@ mntwalk(Chan *c, Chan *nc, char **name, int nname)
* Therefore set type to 0 for now; rootclose is known to be safe. * Therefore set type to 0 for now; rootclose is known to be safe.
*/ */
nc->type = 0; nc->type = 0;
nc->flag |= (c->flag & CCACHE);
alloc = 1; alloc = 1;
} }
wq->clone = nc; wq->clone = nc;

View file

@ -200,7 +200,7 @@ pio(Segment *s, ulong addr, ulong soff, Page **p)
Page *new; Page *new;
KMap *k; KMap *k;
Chan *c; Chan *c;
int n, ask; int n, ask, cache;
char *kaddr; char *kaddr;
ulong daddr; ulong daddr;
Page *loadrec; Page *loadrec;
@ -238,15 +238,18 @@ retry:
k = kmap(new); k = kmap(new);
kaddr = (char*)VA(k); kaddr = (char*)VA(k);
cache = c->flag & CCACHE;
while(waserror()) { while(waserror()) {
c->flag |= cache;
if(strcmp(up->errstr, Eintr) == 0) if(strcmp(up->errstr, Eintr) == 0)
continue; continue;
kunmap(k); kunmap(k);
putpage(new); putpage(new);
faulterror(Eioload, c, 0); faulterror(Eioload, c, 0);
} }
c->flag &= ~CCACHE;
n = devtab[c->type]->read(c, kaddr, ask, daddr); n = devtab[c->type]->read(c, kaddr, ask, daddr);
c->flag |= cache;
if(n != ask) if(n != ask)
faulterror(Eioload, c, 0); faulterror(Eioload, c, 0);
if(ask < BY2PG) if(ask < BY2PG)