ref: e2964595bccfdf56e1aa94d0d2fd1a3401271a76
parent: 81092632561b4605f1fc366a9b2cfc6e11d3b57c
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Jan 5 22:00:49 EST 2022
sync: move to a new task thread.
--- a/blk.c
+++ b/blk.c
@@ -45,6 +45,26 @@
}
}
+int
+syncblk(Blk *b)
+{
+ assert(b->flag & Bfinal);
+ clrflag(b, Bqueued|Bdirty);
+ return pwrite(fs->fd, b->buf, Blksz, b->bp.addr);
+}
+
+void
+enqueue(Blk *b)
+{
+ assert(b->flag & Bdirty);
+ finalize(b);
+ if(syncblk(b) == -1){
+ ainc(&fs->broken);
+ fprint(2, "write: %r");
+ abort();
+ }
+}
+
Blk*
readblk(vlong bp, int flg)
{
--- a/dat.h
+++ b/dat.h
@@ -352,6 +352,7 @@
Tree *snext;
long memref; /* number of in-memory references to this */
+ int dirty;
int ref; /* number of on-disk references to this */
int ht;
Bptr bp;
@@ -385,6 +386,7 @@
int hdrsz; /* immutable */
QLock snaplk; /* snapshot lock */
+ Mount *mounts;
Tree *osnap;
Blk *super;
@@ -441,6 +443,8 @@
Lock;
Avltree *free;
Avltree *partial;
+ Blk **queue;
+ int nqueue;
Blk *b; /* arena block */
Blk **q; /* write queue */
vlong nq;
@@ -491,6 +495,7 @@
};
struct Mount {
+ Mount *next;
long ref;
vlong gen;
int uid;
--- a/fns.h
+++ b/fns.h
@@ -137,6 +137,7 @@
void runwrite(int, void*);
void runread(int, void*);
void runcons(int, void*);
+void runtasks(int, void*);
/* it's in libc... */
extern int cas(long*, long, long);
--- a/fs.c
+++ b/fs.c
@@ -11,19 +11,21 @@
// FIXME: hack. We sync way too often.
static char*
-updatesnap(Fid *f)
+updatemount(Mount *mnt)
{
Tree *t, *n;
char *e;
- t = f->mnt->root;
+ t = mnt->root;
+ if(!t->dirty)
+ return nil;
qlock(&fs->snaplk);
if((n = newsnap(t)) == nil){
- fprint(2, "snap: save %s: %s\n", f->mnt->name, "create snap");
+ fprint(2, "snap: save %s: %s\n", mnt->name, "create snap");
abort();
}
- if((e = labelsnap(f->mnt->name, t->gen)) != nil){
- fprint(2, "snap: save %s: %s\n", f->mnt->name, e);
+ if((e = labelsnap(mnt->name, t->gen)) != nil){
+ fprint(2, "snap: save %s: %s\n", mnt->name, e);
abort();
}
if(t->prev[0] != -1){
@@ -32,13 +34,9 @@
abort();
}
}
- f->mnt->root = n;
-fprint(2, "updated\n");
-showtreeroot(2, n);
-fprint(2, "==================================\n");
+ mnt->root = n;
closesnap(t);
qunlock(&fs->snaplk);
- sync();
return nil;
}
@@ -697,6 +695,8 @@
return;
}
+ mnt->next = fs->mounts;
+ fs->mounts = mnt;
r.type = Rattach;
r.qid = d.qid;
respond(m, &r);
@@ -953,10 +953,6 @@
rerror(m, e);
goto Out;
}
- if((e = updatesnap(f)) != nil){
- rerror(m, e);
- goto Out;
- }
r.type = Rwstat;
respond(m, &r);
}
@@ -1087,11 +1083,6 @@
r.type = Rcreate;
r.qid = d.qid;
r.iounit = f->iounit;
- if((e = updatesnap(f)) != nil){
- rerror(m, e);
- putfid(f);
- return;
- }
respond(m, &r);
putfid(f);
}
@@ -1169,11 +1160,6 @@
}
runlock(f->dent);
- if((e = updatesnap(f)) != nil){
- rerror(m, e);
- clunkfid(f);
- return;
- }
r.type = Rremove;
respond(m, &r);
clunkfid(f);
@@ -1446,12 +1432,6 @@
}
wunlock(f->dent);
- if((e = updatesnap(f)) != nil){
- rerror(m, e);
- putfid(f);
- return;
- }
-
r.type = Rwrite;
r.count = m->count;
respond(m, &r);
@@ -1519,6 +1499,7 @@
void
runwrite(int wid, void *)
{
+ Mount *mnt;
Fmsg *m;
int ao;
@@ -1544,9 +1525,12 @@
}
break;
case AOsync:
- fprint(m->a->fd, "syncing [readonly: %d]\n", m->a->halt);
+ if(m->a->fd != -1)
+ fprint(m->a->fd, "syncing [readonly: %d]\n", m->a->halt);
if(m->a->halt)
ainc(&fs->rdonly);
+ for(mnt = fs->mounts; mnt != nil; mnt = mnt->next)
+ updatemount(mnt);
sync();
freemsg(m);
break;
@@ -1574,5 +1558,29 @@
case Tstat: fsstat(m); break;
}
quiesce(wid);
+ }
+}
+
+void
+runtasks(int, void *)
+{
+ Fmsg *m;
+ Amsg *a;
+
+ while(1){
+ sleep(500);
+ m = mallocz(sizeof(Fmsg), 1);
+ a = mallocz(sizeof(Amsg), 1);
+ if(m == nil || a == nil){
+ fprint(2, "alloc sync msg: %r\n");
+ free(m);
+ free(a);
+ return;
+ }
+ a->op = AOsync;
+ a->halt = 0;
+ a->fd = -1;
+ m->a = a;
+ chsend(fs->wrchan, m);
}
}
--- a/main.c
+++ b/main.c
@@ -124,9 +124,8 @@
launch(runcons, fs->nproc++, (void*)ctlfd, "ctl");
launch(runwrite, fs->nproc++, nil, "writeio");
launch(runread, fs->nproc++, nil, "readio");
+ launch(runtasks, fs->nproc++, nil, "tasks");
// launch(runfs, fs->nproc++, (void*)srvfd, "fs");
-// launch(taskproc, fs->nproc++, nil, "tasks");
-// launch(syncproc, fs->nproc++, &fs->flushev, "sync");
assert(fs->nproc < Maxproc);
runfs(fs->nproc++, (void*)srvfd);
exits(nil);
--- a/ream.c
+++ b/ream.c
@@ -120,7 +120,6 @@
b->type = Tarena;
b->bp.addr = start;
p = b->buf + Hdrsz;
- print("b->bp.addr: %llx\n", b->bp.addr);
PBIT64(p+0, bo);
PBIT64(p+8, bh);
finalize(b);
@@ -154,7 +153,7 @@
sz = d->length;
sz = sz - (sz % Blksz) - Blksz;
- fs->narena = sz / (128*GiB);
+ fs->narena = d->length / (64*GiB);
if(fs->narena < 1)
fs->narena = 1;
if(fs->narena >= 128)
@@ -170,9 +169,9 @@
fprint(2, "reaming %d arenas:\n", fs->narena);
for(i = 0; i < fs->narena; i++){
- print("\tarena %d: %lld blocks at %lld\n", i, asz/Blksz, off);
+ print("\tarena %d: %lld blocks at %llx\n", i, asz/Blksz, off);
reamarena(&fs->arenas[i], off, asz);
- asz += off;
+ off += asz;
}
sb->type = Tsuper;
--- a/snap.c
+++ b/snap.c
@@ -19,26 +19,6 @@
}
}
-int
-syncblk(Blk *b)
-{
- assert(b->flag & Bfinal);
- clrflag(b, Bqueued|Bdirty);
- return pwrite(fs->fd, b->buf, Blksz, b->bp.addr);
-}
-
-void
-enqueue(Blk *b)
-{
- assert(b->flag & Bdirty);
- finalize(b);
- if(syncblk(b) == -1){
- ainc(&fs->broken);
- fprint(2, "write: %r");
- abort();
- }
-}
-
Tree*
openlabel(char *name)
{
@@ -291,6 +271,7 @@
r->ht = t->ht;
r->bp = t->bp;
r->gen = gen;
+ r->dirty = 0;
/* shift deadlist down */
for(i = Ndead-1; i >= 0; i--){
r->prev[i] = i == 0 ? t->gen : t->prev[i-1];
--- a/tree.c
+++ b/tree.c
@@ -1238,6 +1238,7 @@
lock(&t->lk);
t->ht += dh;
t->bp = rb->bp;
+ t->dirty = 1;
unlock(&t->lk);
freepath(t, path, npath);
free(path);