code: plan9front

Download patch

ref: 8ed25f24b7831eab394f14697766d55065b18822
parent: fcb9abccbbc73a4f449d55c2c7fb369db2ad139d
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Wed Jul 8 20:01:50 EDT 2015

kernel: various cleanups of imagereclaim(), pagereclaim(), freepages(), putimage()

imagereclaim(), pagereclaim():
- move imagereclaim() and pagereclaim() declarations to portfns.h
- consistently use ulong type for page counts
- name number of pages to free "pages" instead of "min"
- check for pages == 0 on entry

freepages():
- move pagechaindone() call to wakeup newpage() consumers inside
  palloc critical section.

putimage():
- use long type for refcount

--- a/sys/src/9/port/page.c
+++ b/sys/src/9/port/page.c
@@ -83,23 +83,26 @@
 }
 
 static void
-freepages(Page *head, Page *tail, int n)
+freepages(Page *head, Page *tail, ulong np)
 {
 	lock(&palloc);
 	tail->next = palloc.head;
 	palloc.head = head;
-	palloc.freecount += n;
-	unlock(&palloc);
+	palloc.freecount += np;
 	pagechaindone();
+	unlock(&palloc);
 }
 
-int
-pagereclaim(Image *i, int min)
+ulong
+pagereclaim(Image *i, ulong pages)
 {
 	Page **h, **l, **x, *p;
 	Page *fh, *ft;
-	int n;
+	ulong np;
 
+	if(pages == 0)
+		return 0;
+
 	lock(i);
 	if(i->pgref == 0){
 		unlock(i);
@@ -107,7 +110,7 @@
 	}
 	incref(i);
 
-	n = 0;
+	np = 0;
 	fh = ft = nil;
 	for(h = i->pghash; h < &i->pghash[PGHSIZE]; h++){
 		l = h;
@@ -133,16 +136,16 @@
 		else
 			ft->next = p;
 		ft = p;
-		if(++n >= min)
+		if(++np >= pages)
 			break;
 	}
 	unlock(i);
 	putimage(i);
 
-	if(n > 0)
-		freepages(fh, ft, n);
+	if(np > 0)
+		freepages(fh, ft, np);
 
-	return n;
+	return np;
 }
 
 static int
--- a/sys/src/9/port/portfns.h
+++ b/sys/src/9/port/portfns.h
@@ -131,6 +131,7 @@
 void		ilock(Lock*);
 void		interrupted(void);
 void		iunlock(Lock*);
+ulong		imagereclaim(ulong);
 long		incref(Ref*);
 void		initseg(void);
 int		iprint(char*, ...);
@@ -204,7 +205,8 @@
 void		pagechaindone(void);
 void		pagechainhead(Page*);
 void		pageinit(void);
-ulong	pagenumber(Page*);
+ulong		pagenumber(Page*);
+ulong		pagereclaim(Image*, ulong);
 void		pagersummary(void);
 void		panic(char*, ...);
 Cmdbuf*		parsecmd(char *a, int n);
--- a/sys/src/9/port/segment.c
+++ b/sys/src/9/port/segment.c
@@ -5,8 +5,6 @@
 #include	"fns.h"
 #include	"../port/error.h"
 
-int imagereclaim(int);
-
 /*
  * Attachable segment types
  */
@@ -295,20 +293,22 @@
 	return i;
 }
 
-extern int pagereclaim(Image*, int);	/* page.c */
-
-int
-imagereclaim(int min)
+ulong
+imagereclaim(ulong pages)
 {
 	static Image *i, *ie;
-	int j, n;
+	ulong np;
+	int j;
 
+	if(pages == 0)
+		return 0;
+
 	eqlock(&imagealloc.ireclaim);
 	if(i == nil){
 		i = imagealloc.list;
 		ie = &imagealloc.list[conf.nimage];
 	}
-	n = 0;
+	np = 0;
 	for(j = 0; j < conf.nimage; j++, i++){
 		if(i >= ie)
 			i = imagealloc.list;
@@ -319,14 +319,14 @@
 		 * reclaim pages from inactive images.
 		 */
 		if(imagealloc.free != nil || i->ref == i->pgref){
-			n += pagereclaim(i, min - n);
-			if(n >= min)
+			np += pagereclaim(i, pages - np);
+			if(np >= pages)
 				break;
 		}
 	}
 	qunlock(&imagealloc.ireclaim);
 
-	return n;
+	return np;
 }
 
 void
@@ -334,7 +334,7 @@
 {
 	Image *f, **l;
 	Chan *c;
-	int r;
+	long r;
 
 	if(i->notext){
 		decref(i);
--- a/sys/src/9/port/swap.c
+++ b/sys/src/9/port/swap.c
@@ -118,25 +118,22 @@
 		kproc("pager", pager, 0);
 }
 
-extern int pagereclaim(Image*,int);	/* page.c */
-extern int imagereclaim(int);		/* segment.c */
-
 static int
 reclaim(void)
 {
-	int n;
+	ulong np;
 
 	for(;;){
-		if((n = pagereclaim(&fscache, 1000)) > 0) {
-			if(0) print("reclaim: %d fscache\n", n);
-		} else if((n = pagereclaim(&swapimage, 1000)) > 0) {
-			if(0) print("reclaim: %d swap\n", n);
-		} else if((n = imagereclaim(1000)) > 0) {
-			if(0) print("reclaim: %d image\n", n);
+		if((np = pagereclaim(&fscache, 1000)) > 0) {
+			if(0) print("reclaim: %lud fscache\n", np);
+		} else if((np = pagereclaim(&swapimage, 1000)) > 0) {
+			if(0) print("reclaim: %lud swap\n", np);
+		} else if((np = imagereclaim(1000)) > 0) {
+			if(0) print("reclaim: %lud image\n", np);
 		}
 		if(!needpages(nil))
 			return 1;	/* have pages, done */
-		if(n == 0)
+		if(np == 0)
 			return 0;	/* didnt reclaim, need to swap */
 		sched();
 	}