code: mafs

Download patch

ref: d58c4a62c233099a351c26b900085a4004d15595
parent: ed15895273e750a388552171d4888e075bc3ea59
author: 9ferno <gophone2015@gmail.com>
date: Tue Oct 25 11:35:57 EDT 2022

Revert "stopped searching through the whole linked list to find an Iobuf to steal"

This reverts commit 9073565b4482a85a72d82508adb76496975734d6.

--- a/iobuf.c
+++ b/iobuf.c
@@ -31,21 +31,6 @@
 	return p;
 }
 
-Iobuf *
-changelru(Iobuf *lru, Iobuf *p)
-{
-	/* remove p from its current position in the lru circular buffer */
-	p->back->fore = p->fore;
-	p->fore->back = p->back;
-
-	/* make p the hb->link and put it at the back of existing link */
-	p->fore = lru;
-	p->back = lru->back;
-	lru->back = p;
-	p->back->fore = p;
-	return p;
-}
-
 /*
 	Get the Iobuf of the disk block at addr from the buffer cache
 	for my use.
@@ -66,7 +51,7 @@
 getbuf(u64 blkno, u8 readonly, u8 freshalloc)
 {
 	Hiob *hp;
-	Iobuf *s, *p, *steal;
+	Iobuf *s, *p;
 	s8 ncollisions;
 
 	hp = &hiob[blkno%nbuckets];
@@ -76,7 +61,6 @@
 				" hiob 0x%p hp 0x%p readonly %d\n",
 			blkno, blkno%nbuckets, getcallerpc(&blkno),
 			hiob, hp, readonly);
-	steal = nil;
 	qlock(hp);
 	s = hp->link;
 	if(s == nil)
@@ -85,7 +69,16 @@
 		ncollisions++;
 		if(p->blkno == blkno){
 			if(p != s){
-				hp->link = changelru(s, p);
+				/* remove p from its current position in the lru circular buffer */
+				p->back->fore = p->fore;
+				p->fore->back = p->back;
+
+				/* make p the hb->link and put it at the back of existing link */
+				p->fore = s;
+				p->back = s->back;
+				s->back = p;
+				p->back->fore = p;
+				hp->link = p;
 			}
 			incref(p);
 			qunlock(hp);
@@ -105,9 +98,6 @@
 			decref(p);
 			return p;
 		}
-		if(p->readers == 0 && p->writer == 0 &&
-			p->ref == 0 && p->dirties == 0)
-			steal = p;
 		p = p->fore;
 		if(p == s)
 			break;
@@ -114,24 +104,26 @@
 	}
 
 	/* maxed out our allowed number of collisions,
-		try to steal the oldest Iobuf without any ref's.
+		try to steal an older Iobuf without any ref's.
 		Ncollisions is a soft limit.
 	 */
-	if(ncollisions >= Ncollisions &&
-		steal != nil && canwlock(steal)){
-		if(steal->readers == 0 && steal->writer == 0 &&
-			steal->ref == 0 && steal->dirties == 0){
-			hp->link = p = changelru(s, steal);
-			if(chatty9p > 4)
-				dprint("	stealing iobuf 0x%p for blkno %llud\n",
-						p, blkno);
-			goto found;	/* p is wlock() */
-		} else
-			/* cannot use steal, just use a new buffer
-				instead of seearching through the linked list
-				to find another unused Iobuf.
-			 */
-			wunlock(steal);
+	if(ncollisions >= Ncollisions){
+Another:
+		do{
+			p = s->back;
+			if(p->ref == 0 && p->dirties == 0 && canwlock(p)){
+				if(p->dirties > 0){
+					wunlock(p);
+					goto Another;
+				}
+				hp->link = p;
+				if(chatty9p > 4)
+					dprint("	stealing iobuf 0x%p for blkno %llud\n",
+							p, blkno);
+				goto found;	/* p is wlock() */
+			}
+			s = p;
+		}while(p != hp->link);
 	}
 
 	/* no unlocked blocks available; add a new one */