shithub: gefs

Download patch

ref: d828be291c9d41905eee8a92ac51326589e0ac93
parent: a81f100786ca7f0a1927c993f3f18a50b2480fb2
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Jul 9 21:16:00 EDT 2023

blk: leak fewer blocks

--- a/blk.c
+++ b/blk.c
@@ -247,17 +247,12 @@
 mklogblk(vlong o)
 {
 	Blk *lb;
-	char *p;
 
 	if((lb = cachepluck()) == nil)
 		return nil;
 	initblk(lb, o, -1, Tlog);
-
 	lb->logsz = Loghashsz;
-	p = lb->data + lb->logsz;
-	PACK64(p+0, o|LogAlloc1);
 	finalize(lb);
-
 	if(syncblk(lb) == -1){
 		dropblk(lb);
 		return nil;
@@ -291,7 +286,7 @@
  * recursion.
  */
 static int
-logappend(Arena *a, vlong off, vlong len, int op, Blk **tl)
+logappend(Arena *a, vlong off, vlong len, int op, int defer)
 {
 	vlong o, ao;
 	Blk *nl, *lb;
@@ -298,7 +293,7 @@
 	char *p, *name;
 
 	nl = nil;
-	lb = *tl;
+	lb = defer ? a->defertl : a->logtl;
 	assert((off & 0xff) == 0);
 	assert(op == LogAlloc || op == LogFree || op == LogSync);
 	if(op != LogSync){
@@ -310,9 +305,10 @@
 	case LogAlloc:	name = "alloc";	break;
 	case LogFree:	name = "free";	break;
 	case LogSync:	name = "sync";	break;
+	case LogChain:	name = "chain";	break;
 	default:	name = "???";	break;
 	}
-assert(lb == nil || lb->logsz > 0);
+	assert(lb == nil || lb->logsz > 0);
 	dprint("logop %llx+%llx@%x: %s\n", off, len, lb?lb->logsz:-1, name);
 	/*
 	 * move to the next block when we have
@@ -330,7 +326,10 @@
 		if(chainlog(lb, nl->bp.addr) == -1)
 			return -1;
 		lb = nl;
-		*tl = nl;
+		if(defer)
+			a->defertl = lb;
+		else
+			a->logtl = lb;
 		a->nlog++;
 	}
 
@@ -356,7 +355,7 @@
 	 * current allocation. so that we don't
 	 * reorder allocs and frees.
 	 */
-	if(nl != nil){
+	if(nl != nil && !defer){
 		p = lb->data + lb->logsz;
 		ao = nl->bp.addr|LogAlloc1;
 		PACK64(p, ao);
@@ -429,6 +428,7 @@
 			dprint("\tlog@%d alloc: %llx+%llx\n", i, off, len);
 			if(grabrange(a->free, off & ~0xff, len) == -1)
 				return -1;
+			a->used += len;
 			break;
 		case LogFree:
 		case LogFree1:
@@ -436,6 +436,7 @@
 			dprint("\tlog@%d free: %llx+%llx\n", i, off, len);
 			if(freerange(a->free, off & ~0xff, len) == -1)
 				return -1;
+			a->used -= len;
 			break;
 		default:
 			n = 0;
@@ -618,7 +619,7 @@
 int
 syncbarrier(Arena *a, vlong gen)
 {
-	if(logappend(a, gen<<8, 0, LogSync, &a->logtl) == -1)
+	if(logappend(a, gen<<8, 0, LogSync, 0) == -1)
 		return -1;
 	if(a->loghd.addr == -1)
 		a->loghd = a->logtl->bp;
@@ -673,7 +674,7 @@
 
 	r = -1;
 	a = getarena(b);
-	if(logappend(a, b, Blksz, LogFree, &a->logtl) == -1)
+	if(logappend(a, b, Blksz, LogFree, 0) == -1)
 		return -1;
 	if(a->loghd.addr == -1)
 		a->loghd = a->logtl->bp;
@@ -695,7 +696,7 @@
 	lock(a);
 	r = -1;
 	a = getarena(b);
-	if(logappend(a, b, Blksz, LogFree, &a->defertl) == -1)
+	if(logappend(a, b, Blksz, LogFree, 1) == -1)
 		return -1;
 	if(a->deferhd.addr == -1)
 		a->deferhd = a->defertl->bp;
@@ -735,7 +736,7 @@
 		unlock(a);
 		goto Again;
 	}
-	if(logappend(a, b, Blksz, LogAlloc, &a->logtl) == -1){
+	if(logappend(a, b, Blksz, LogAlloc, 0) == -1){
 		unlock(a);
 		return -1;
 	}
@@ -1179,6 +1180,8 @@
 		a = &fs->arenas[i];
 		lock(a);
 		if(a->deferhd.addr != -1){
+print("sync\n");
+			finalize(a->defertl);
 			chainlog(a->logtl, a->deferhd.addr);
 			loadlog(a, a->deferhd);
 			a->logtl = a->defertl;
--- a/fs.c
+++ b/fs.c
@@ -2222,7 +2222,8 @@
 	Amsg *a;
 
 	while(1){
-		sleep(5000);
+		//sleep(5000);
+sleep(30*1000);
 		m = mallocz(sizeof(Fmsg), 1);
 		a = mallocz(sizeof(Amsg), 1);
 		if(m == nil || a == nil){
--- a/load.c
+++ b/load.c
@@ -47,6 +47,7 @@
 		return -1;
 	a->hd = hd;
 	a->tl = tl;
+	a->used = a->size;
 	return 0;
 }