i think this is a working concurrent logging scheme

This commit is contained in:
Robert Morris 2014-08-28 05:57:47 -04:00
parent 71453f72f2
commit 48aa917403
5 changed files with 117 additions and 99 deletions

2
bio.c
View file

@ -80,6 +80,8 @@ bget(uint dev, uint sector)
} }
// Not cached; recycle some non-busy and clean buffer. // Not cached; recycle some non-busy and clean buffer.
// "clean" because B_DIRTY and !B_BUSY means log.c
// hasn't yet committed the changes to the buffer.
for(b = bcache.head.prev; b != &bcache.head; b = b->prev){ for(b = bcache.head.prev; b != &bcache.head; b = b->prev){
if((b->flags & B_BUSY) == 0 && (b->flags & B_DIRTY) == 0){ if((b->flags & B_BUSY) == 0 && (b->flags & B_DIRTY) == 0){
b->dev = dev; b->dev = dev;

21
log.c
View file

@ -52,6 +52,10 @@ struct log log;
static void recover_from_log(void); static void recover_from_log(void);
static void commit(); static void commit();
// statistics, delete eventually XXX.
static int maxsize;
static int maxoutstanding;
void void
initlog(void) initlog(void)
{ {
@ -131,10 +135,15 @@ begin_op(void)
while(1){ while(1){
if(log.committing){ if(log.committing){
sleep(&log, &log.lock); sleep(&log, &log.lock);
} else if(log.lh.n + (log.outstanding+1)*MAXOPBLOCKS > LOGSIZE){
// this op might exhaust log space; wait for commit.
sleep(&log, &log.lock);
} else { } else {
// XXX wait (for a commit) if log is longish.
// need to reserve to avoid over-commit of log space.
log.outstanding += 1; log.outstanding += 1;
if(log.outstanding > maxoutstanding){
maxoutstanding = log.outstanding;
cprintf("%d outstanding\n", log.outstanding);
}
release(&log.lock); release(&log.lock);
break; break;
} }
@ -155,6 +164,9 @@ end_op(void)
if(log.outstanding == 0){ if(log.outstanding == 0){
do_commit = 1; do_commit = 1;
log.committing = 1; log.committing = 1;
} else {
// begin_op() may be waiting for log space.
wakeup(&log);
} }
release(&log.lock); release(&log.lock);
@ -208,6 +220,11 @@ log_write(struct buf *b)
if (i == log.lh.n) if (i == log.lh.n)
log.lh.n++; log.lh.n++;
b->flags |= B_DIRTY; // XXX prevent eviction b->flags |= B_DIRTY; // XXX prevent eviction
if(log.lh.n > maxsize){
maxsize = log.lh.n;
cprintf("log size %d/%d\n", log.lh.n, LOGSIZE);
}
} }
//PAGEBREAK! //PAGEBREAK!

2
mkfs.c
View file

@ -13,7 +13,7 @@
#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0) #define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)
int nblocks = 985; int nblocks = (995-LOGSIZE);
int nlog = LOGSIZE; int nlog = LOGSIZE;
int ninodes = 200; int ninodes = 200;
int size = 1024; int size = 1024;

View file

@ -3,10 +3,11 @@
#define NCPU 8 // maximum number of CPUs #define NCPU 8 // maximum number of CPUs
#define NOFILE 16 // open files per process #define NOFILE 16 // open files per process
#define NFILE 100 // open files per system #define NFILE 100 // open files per system
#define NBUF 10 // size of disk block cache
#define NINODE 50 // maximum number of active i-nodes #define NINODE 50 // maximum number of active i-nodes
#define NDEV 10 // maximum major device number #define NDEV 10 // maximum major device number
#define ROOTDEV 1 // device number of file system root disk #define ROOTDEV 1 // device number of file system root disk
#define MAXARG 32 // max exec arguments #define MAXARG 32 // max exec arguments
#define LOGSIZE 10 // max data sectors in on-disk log #define MAXOPBLOCKS 10 // max # of blocks any FS op writes
#define LOGSIZE (MAXOPBLOCKS*3) // max data sectors in on-disk log
#define NBUF (MAXOPBLOCKS*3) // size of disk block cache (>= LOGSIZE)

View file

@ -512,51 +512,56 @@ sharedfd(void)
} }
} }
// two processes write two different files at the same // four processes write different files at the same
// time, to test block allocation. // time, to test block allocation.
void void
twofiles(void) fourfiles(void)
{ {
int fd, pid, i, j, n, total; int fd, pid, i, j, n, total, pi;
char *names[] = { "f0", "f1", "f2", "f3" };
char *fname; char *fname;
printf(1, "twofiles test\n"); printf(1, "fourfiles test\n");
unlink("f1"); for(pi = 0; pi < 4; pi++){
unlink("f2"); fname = names[pi];
unlink(fname);
pid = fork(); pid = fork();
if(pid < 0){ if(pid < 0){
printf(1, "fork failed\n"); printf(1, "fork failed\n");
exit(); exit();
} }
fname = pid ? "f1" : "f2"; if(pid == 0){
fd = open(fname, O_CREATE | O_RDWR); fd = open(fname, O_CREATE | O_RDWR);
if(fd < 0){ if(fd < 0){
printf(1, "create failed\n"); printf(1, "create failed\n");
exit(); exit();
} }
memset(buf, pid?'p':'c', 512); memset(buf, '0'+pi, 512);
for(i = 0; i < 12; i++){ for(i = 0; i < 12; i++){
if((n = write(fd, buf, 500)) != 500){ if((n = write(fd, buf, 500)) != 500){
printf(1, "write failed %d\n", n); printf(1, "write failed %d\n", n);
exit();
}
}
exit(); exit();
} }
} }
close(fd);
if(pid) for(pi = 0; pi < 4; pi++){
wait(); wait();
else }
exit();
for(i = 0; i < 2; i++){ for(i = 0; i < 2; i++){
fd = open(i?"f1":"f2", 0); fname = names[i];
fd = open(fname, 0);
total = 0; total = 0;
while((n = read(fd, buf, sizeof(buf))) > 0){ while((n = read(fd, buf, sizeof(buf))) > 0){
for(j = 0; j < n; j++){ for(j = 0; j < n; j++){
if(buf[j] != (i?'p':'c')){ if(buf[j] != '0'+i){
printf(1, "wrong char\n"); printf(1, "wrong char\n");
exit(); exit();
} }
@ -568,87 +573,80 @@ twofiles(void)
printf(1, "wrong length %d\n", total); printf(1, "wrong length %d\n", total);
exit(); exit();
} }
unlink(fname);
} }
unlink("f1"); printf(1, "fourfiles ok\n");
unlink("f2");
printf(1, "twofiles ok\n");
} }
// two processes create and delete different files in same directory // four processes create and delete different files in same directory
void void
createdelete(void) createdelete(void)
{ {
enum { N = 20 }; enum { N = 20 };
int pid, i, fd; int pid, i, fd, pi;
char name[32]; char name[32];
printf(1, "createdelete test\n"); printf(1, "createdelete test\n");
pid = fork();
if(pid < 0){
printf(1, "fork failed\n");
exit();
}
name[0] = pid ? 'p' : 'c'; for(pi = 0; pi < 4; pi++){
name[2] = '\0'; pid = fork();
for(i = 0; i < N; i++){ if(pid < 0){
name[1] = '0' + i; printf(1, "fork failed\n");
fd = open(name, O_CREATE | O_RDWR);
if(fd < 0){
printf(1, "create failed\n");
exit(); exit();
} }
close(fd);
if(i > 0 && (i % 2 ) == 0){ if(pid == 0){
name[1] = '0' + (i / 2); name[0] = 'p' + pi;
if(unlink(name) < 0){ name[2] = '\0';
printf(1, "unlink failed\n"); for(i = 0; i < N; i++){
name[1] = '0' + i;
fd = open(name, O_CREATE | O_RDWR);
if(fd < 0){
printf(1, "create failed\n");
exit();
}
close(fd);
if(i > 0 && (i % 2 ) == 0){
name[1] = '0' + (i / 2);
if(unlink(name) < 0){
printf(1, "unlink failed\n");
exit();
}
}
}
exit();
}
}
for(pi = 0; pi < 4; pi++){
wait();
}
name[0] = name[1] = name[2] = 0;
for(i = 0; i < N; i++){
for(pi = 0; pi < 4; pi++){
name[0] = 'p' + pi;
name[1] = '0' + i;
fd = open(name, 0);
if((i == 0 || i >= N/2) && fd < 0){
printf(1, "oops createdelete %s didn't exist\n", name);
exit();
} else if((i >= 1 && i < N/2) && fd >= 0){
printf(1, "oops createdelete %s did exist\n", name);
exit(); exit();
} }
if(fd >= 0)
close(fd);
} }
} }
if(pid==0)
exit();
else
wait();
for(i = 0; i < N; i++){ for(i = 0; i < N; i++){
name[0] = 'p'; for(pi = 0; pi < 4; pi++){
name[1] = '0' + i; name[0] = 'p' + i;
fd = open(name, 0); name[1] = '0' + i;
if((i == 0 || i >= N/2) && fd < 0){ unlink(name);
printf(1, "oops createdelete %s didn't exist\n", name);
exit();
} else if((i >= 1 && i < N/2) && fd >= 0){
printf(1, "oops createdelete %s did exist\n", name);
exit();
} }
if(fd >= 0)
close(fd);
name[0] = 'c';
name[1] = '0' + i;
fd = open(name, 0);
if((i == 0 || i >= N/2) && fd < 0){
printf(1, "oops createdelete %s didn't exist\n", name);
exit();
} else if((i >= 1 && i < N/2) && fd >= 0){
printf(1, "oops createdelete %s did exist\n", name);
exit();
}
if(fd >= 0)
close(fd);
}
for(i = 0; i < N; i++){
name[0] = 'p';
name[1] = '0' + i;
unlink(name);
name[0] = 'c';
unlink(name);
} }
printf(1, "createdelete ok\n"); printf(1, "createdelete ok\n");
@ -1716,6 +1714,12 @@ main(int argc, char *argv[])
} }
close(open("usertests.ran", O_CREATE)); close(open("usertests.ran", O_CREATE));
createdelete();
linkunlink();
concreate();
fourfiles();
sharedfd();
bigargtest(); bigargtest();
bigwrite(); bigwrite();
bigargtest(); bigargtest();
@ -1741,18 +1745,12 @@ main(int argc, char *argv[])
fourteen(); fourteen();
bigfile(); bigfile();
subdir(); subdir();
concreate();
linkunlink();
linktest(); linktest();
unlinkread(); unlinkread();
createdelete();
twofiles();
sharedfd();
dirfile(); dirfile();
iref(); iref();
forktest(); forktest();
bigdir(); // slow bigdir(); // slow
exectest(); exectest();
exit(); exit();