shithub: riscv

Download patch

ref: b437065950b5d0234358fcfe8495f6bdb0bf3003
parent: d3e54ff2d9039c67f6fe7d99349e1c83b696b7ae
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Thu Jan 4 19:52:14 EST 2018

stats: show amount of reclaimable pages (add -r flag)

reclaimable pages are user pages that are used for
caches like the image cache, mount cache and swap cache.

--- a/sys/man/8/stats
+++ b/sys/man/8/stats
@@ -39,6 +39,9 @@
 .B "c context
 number of process context switches per second.
 .TP
+.B "d draw
+draw memory allocation size in bytes.
+.TP
 .B 
 .B "e ether
 total number of packets sent and received per second.
@@ -58,6 +61,9 @@
 The last two are averaged over all processors on a
 multiprocessor.
 .TP
+.B "k kern
+kernel memory allocation size in bytes.
+.TP
 .B "l load
 (default) system load average.
 The load is computed as a running average of
@@ -68,18 +74,16 @@
 The graph displays the fraction
 of the machine's total memory in use.
 .TP
-.B "k kern
-kernel memory allocation size in bytes.
-.TP
-.B "d draw
-draw memory allocation size in bytes.
-.TP
-.B 
 .B "n etherin,out,err
 number of packets sent and received per second, and total number of errors, displayed as separate graphs.
 .TP
 .B "p tlbpurge
 number of translation lookaside buffer flushes per second.
+.TP
+.B "r reclaim 
+total pages of reclaimable memory.
+The graph displays the fraction
+of the machine's total memory in use.
 .TP
 .B "s syscall
 number of system calls per second.
--- a/sys/src/9/port/devswap.c
+++ b/sys/src/9/port/devswap.c
@@ -517,11 +517,13 @@
 swapread(Chan *c, void *va, long n, vlong off)
 {
 	char tmp[256];		/* must be >= 18*NUMSIZE (Qswap) */
+	ulong reclaim;
 
 	switch((ulong)c->qid.path){
 	case Qdir:
 		return devdirread(c, va, n, swapdir, nelem(swapdir), devgen);
 	case Qswap:
+		reclaim = imagecached() + fscache.pgref + swapimage.pgref;
 		snprint(tmp, sizeof tmp,
 			"%llud memory\n"
 			"%llud pagesize\n"
@@ -528,6 +530,7 @@
 			"%lud kernel\n"
 			"%lud/%lud user\n"
 			"%lud/%lud swap\n"
+			"%lud/%lud reclaim\n"
 			"%llud/%llud/%llud kernel malloc\n"
 			"%llud/%llud/%llud kernel draw\n"
 			"%llud/%llud/%llud kernel secret\n",
@@ -534,8 +537,9 @@
 			(uvlong)conf.npage*BY2PG,
 			(uvlong)BY2PG,
 			conf.npage-conf.upages,
-			palloc.user-palloc.freecount-fscache.pgref-swapimage.pgref, palloc.user,
+			palloc.user-palloc.freecount-reclaim, palloc.user,
 			conf.nswap-swapalloc.free, conf.nswap,
+			reclaim, palloc.user,
 			(uvlong)mainmem->curalloc,
 			(uvlong)mainmem->cursize,
 			(uvlong)mainmem->maxsize,
--- a/sys/src/9/port/portfns.h
+++ b/sys/src/9/port/portfns.h
@@ -133,6 +133,7 @@
 void		ilock(Lock*);
 void		interrupted(void);
 void		iunlock(Lock*);
+ulong		imagecached(void);
 ulong		imagereclaim(ulong);
 long		incref(Ref*);
 void		initseg(void);
--- a/sys/src/9/port/segment.c
+++ b/sys/src/9/port/segment.c
@@ -297,6 +297,19 @@
 }
 
 ulong
+imagecached(void)
+{
+	Image *i, *ie;
+	ulong np;
+
+	np = 0;
+	ie = &imagealloc.list[conf.nimage];
+	for(i = imagealloc.list; i < ie; i++)
+		np += i->pgref;
+	return np;
+}
+
+ulong
 imagereclaim(ulong pages)
 {
 	static Image *i, *ie;
--- a/sys/src/cmd/stats.c
+++ b/sys/src/cmd/stats.c
@@ -26,12 +26,13 @@
 
 enum
 {
-	/* old /dev/swap */
+	/* /dev/swap */
 	Mem		= 0,
 	Maxmem,
 	Swap,
 	Maxswap,
-
+	Reclaim,
+	Maxreclaim,
 	Kern,
 	Maxkern,
 	Draw,
@@ -48,6 +49,7 @@
 	Load,
 	Idle,
 	InIntr,
+
 	/* /net/ether0/stats */
 	In		= 0,
 	Link,
@@ -69,7 +71,7 @@
 	int		tempfd;
 	int		disable;
 
-	uvlong		devswap[8];
+	uvlong		devswap[10];
 	uvlong		devsysstat[10];
 	uvlong		prevsysstat[10];
 	int		nproc;
@@ -120,6 +122,7 @@
 	Mload,
 	Mmem,
 	Mswap,
+	Mreclaim,
 	Mkern,
 	Mdraw,
 	Msyscall,
@@ -144,6 +147,7 @@
 	"add  load    ",
 	"add  mem     ",
 	"add  swap    ",
+	"add  reclaim ",
 	"add  kern    ",
 	"add  draw    ",
 	"add  syscall ",
@@ -167,6 +171,7 @@
 	idleval(Machine*, uvlong*, uvlong*, int),
 	memval(Machine*, uvlong*, uvlong*, int),
 	swapval(Machine*, uvlong*, uvlong*, int),
+	reclaimval(Machine*, uvlong*, uvlong*, int),
 	kernval(Machine*, uvlong*, uvlong*, int),
 	drawval(Machine*, uvlong*, uvlong*, int),
 	syscallval(Machine*, uvlong*, uvlong*, int),
@@ -192,6 +197,7 @@
 	loadval,
 	memval,
 	swapval,
+	reclaimval,
 	kernval,
 	drawval,
 	syscallval,
@@ -205,7 +211,7 @@
 Graph	*graph;
 Machine	*mach;
 char	*mysysname;
-char	argchars[] = "8bcdeEfiIkmlnpstwz";
+char	argchars[] = "8bcdeEfiIkmlnprstwz";
 int	pids[NPROC];
 int 	parity;	/* toggled to avoid patterns in textured background */
 int	nmach;
@@ -465,42 +471,58 @@
 int
 readswap(Machine *m, uvlong *a)
 {
+	static int xxx = 0;
+
 	if(strstr(m->buf, "memory\n")){
 		/* new /dev/swap - skip first 3 numbers */
 		if(!readnums(m, 7, a, 1))
 			return 0;
-		a[0] = a[3];
-		a[1] = a[4];
-		a[2] = a[5];
-		a[3] = a[6];
 
-		a[4] = 0;
-		a[5] = 0;
+		a[Mem] = a[3];
+		a[Maxmem] = a[4];
+		a[Swap] = a[5];
+		a[Maxswap] = a[6];
+
+		a[Reclaim] = 0;
+		a[Maxreclaim] = 0;
+		if(m->bufp = strstr(m->buf, "reclaim")){
+			while(m->bufp > m->buf && m->bufp[-1] != '\n')
+				m->bufp--;
+			a[Reclaim] = strtoull(m->bufp, &m->bufp, 10);
+			while(*m->bufp++ == '/')
+				a[Maxreclaim] = strtoull(m->bufp, &m->bufp, 10);
+		}
+
+		a[Kern] = 0;
+		a[Maxkern] = 0;
 		if(m->bufp = strstr(m->buf, "kernel malloc")){
 			while(m->bufp > m->buf && m->bufp[-1] != '\n')
 				m->bufp--;
-			a[4] = strtoull(m->bufp, &m->bufp, 10);
+			a[Kern] = strtoull(m->bufp, &m->bufp, 10);
 			while(*m->bufp++ == '/')
-				a[5] = strtoull(m->bufp, &m->bufp, 10);
+				a[Maxkern] = strtoull(m->bufp, &m->bufp, 10);
 		}
 
-		a[6] = 0;
-		a[7] = 0;
+		a[Draw] = 0;
+		a[Maxdraw] = 0;
 		if(m->bufp = strstr(m->buf, "kernel draw")){
 			while(m->bufp > m->buf && m->bufp[-1] != '\n')
 				m->bufp--;
-			a[6] = strtoull(m->bufp, &m->bufp, 10);
+			a[Draw] = strtoull(m->bufp, &m->bufp, 10);
 			while(*m->bufp++ == '/')
-				a[7] = strtoull(m->bufp, &m->bufp, 10);
+				a[Maxdraw] = strtoull(m->bufp, &m->bufp, 10);
 		}
 
 		return 1;
 	}
 
-	a[4] = 0;
-	a[5] = 0;
-	a[6] = 0;
-	a[7] = 0;
+	a[Reclaim] = 0;
+	a[Maxreclaim] = 0;
+	a[Kern] = 0;
+	a[Maxkern] = 0;
+	a[Draw] = 0;
+	a[Maxdraw] = 0;
+
 	return readnums(m, 4, a, 0);
 }
 
@@ -636,7 +658,7 @@
 int
 needswap(int init)
 {
-	return init | present[Mmem] | present[Mswap] | present[Mkern] | present[Mdraw];
+	return init | present[Mmem] | present[Mswap] | present[Mreclaim] | present[Mkern] | present[Mdraw];
 }
 
 
@@ -747,6 +769,15 @@
 }
 
 void
+reclaimval(Machine *m, uvlong *v, uvlong *vmax, int)
+{
+	*v = m->devswap[Reclaim];
+	*vmax = m->devswap[Maxreclaim];
+	if(*vmax == 0)
+		*vmax = 1;
+}
+
+void
 kernval(Machine *m, uvlong *v, uvlong *vmax, int)
 {
 	*v = m->devswap[Kern];
@@ -1326,6 +1357,9 @@
 		break;
 	case 'p':
 		addgraph(Mtlbpurge);
+		break;
+	case 'r':
+		addgraph(Mreclaim);
 		break;
 	case 's':
 		addgraph(Msyscall);