shithub: riscv

Download patch

ref: 91f79ce9fab7c9920b6f331ca074d58c17755fc9
parent: c8e25d2a18c0395431abc5818a1d2f0561b0181f
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Wed Aug 17 13:38:46 EDT 2022

kernel: allocate notes in heap

de-bloat the proc structure by allocating notes
with on the heap instead of embedding them in
the proc structure.

This saves around 640 bytes per process.

--- a/sys/src/9/bcm64/trap.c
+++ b/sys/src/9/bcm64/trap.c
@@ -384,13 +384,13 @@
 		break;
 	
 	default:
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 	
 	case NDFLT:
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
-		pexit(up->lastnote.msg, up->lastnote.flag != NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag != NDebug);
 	}
 }
 
--- a/sys/src/9/cycv/trap.c
+++ b/sys/src/9/cycv/trap.c
@@ -395,13 +395,13 @@
 		break;
 	
 	default:
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 	
 	case NDFLT:
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
-		pexit(up->lastnote.msg, up->lastnote.flag != NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag != NDebug);
 	}
 }
 
--- a/sys/src/9/imx8/trap.c
+++ b/sys/src/9/imx8/trap.c
@@ -384,13 +384,13 @@
 		break;
 	
 	default:
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 	
 	case NDFLT:
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
-		pexit(up->lastnote.msg, up->lastnote.flag != NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag != NDebug);
 	}
 }
 
--- a/sys/src/9/kw/syscall.c
+++ b/sys/src/9/kw/syscall.c
@@ -81,13 +81,13 @@
 		cur->r0 = (uintptr)nf->arg0;
 		break;
 	default:
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 		/*FALLTHROUGH*/
 	case NDFLT:
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
-		pexit(up->lastnote.msg, up->lastnote.flag != NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag != NDebug);
 	}
 }
 
--- a/sys/src/9/mtx/trap.c
+++ b/sys/src/9/mtx/trap.c
@@ -779,13 +779,13 @@
 		break;
 
 	default:
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 		/* fall through */
 		
 	case NDFLT:
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
-		pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag!=NDebug);
 	}
 }
--- a/sys/src/9/omap/syscall.c
+++ b/sys/src/9/omap/syscall.c
@@ -81,13 +81,13 @@
 		cur->r0 = (uintptr)nf->arg0;
 		break;
 	default:
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 		/*FALLTHROUGH*/
 	case NDFLT:
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
-		pexit(up->lastnote.msg, up->lastnote.flag != NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag != NDebug);
 	}
 }
 
--- a/sys/src/9/pc/trap.c
+++ b/sys/src/9/pc/trap.c
@@ -685,14 +685,14 @@
 		break;
 
 	default:
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 		/* fall through */
 
 	case NDFLT:
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
-		pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag!=NDebug);
 	}
 }
 
--- a/sys/src/9/pc64/trap.c
+++ b/sys/src/9/pc64/trap.c
@@ -671,14 +671,14 @@
 		break;
 
 	default:
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 		/* fall through */
 
 	case NDFLT:
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
-		pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag!=NDebug);
 	}
 }
 
--- a/sys/src/9/port/devproc.c
+++ b/sys/src/9/port/devproc.c
@@ -1138,14 +1138,18 @@
 		if(p->nnote == 0)
 			n = 0;
 		else {
-			i = strlen(p->note[0].msg) + 1;
+			assert(p->note[0] != nil);
+			i = strlen(p->note[0]->msg) + 1;
 			if(i < n)
 				n = i;
-			memmove(va, p->note[0].msg, n-1);
+			memmove(va, p->note[0]->msg, n-1);
 			((char*)va)[n-1] = '\0';
+			free(p->note[0]);
 			if(--p->nnote == 0)
 				p->notepending = 0;
-			memmove(p->note, p->note+1, p->nnote*sizeof(Note));
+			else
+				memmove(&p->note[0], &p->note[1], p->nnote*sizeof(Note*));
+			p->note[p->nnote] = nil;
 		}
 		break;
 
--- a/sys/src/9/port/portdat.h
+++ b/sys/src/9/port/portdat.h
@@ -745,10 +745,10 @@
 	Chan	*slash;
 	Chan	*dot;
 
-	Note	note[NNOTE];
+	Note	*note[NNOTE];
 	short	nnote;
 	short	notified;	/* sysnoted is due */
-	Note	lastnote;
+	Note	*lastnote;
 	int	(*notify)(void*, char*);
 
 	Lock	*lockwait;
--- a/sys/src/9/port/portfns.h
+++ b/sys/src/9/port/portfns.h
@@ -121,6 +121,7 @@
 void		freeb(Block*);
 void		freeblist(Block*);
 int		freebroken(void);
+void		freenotes(Proc*);
 void		freepages(Page*, Page*, ulong);
 void		freepte(Segment*, Pte*);
 void		getcolor(ulong, ulong*, ulong*, ulong*);
--- a/sys/src/9/port/proc.c
+++ b/sys/src/9/port/proc.c
@@ -912,32 +912,35 @@
 	if(up->nnote == 0)
 		return nil;
 	assert(up->nnote > 0);
+	assert(up->note[0] != nil);
 
 	/* hold off user notes during note handling */
-	if(up->notified && up->note[0].flag == NUser)
+	if(up->notified && up->note[0]->flag == NUser)
 		return nil;
 
-	memmove(&up->lastnote, &up->note[0], sizeof(Note));
-	if(--up->nnote)
-		memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
+	free(up->lastnote);
+	up->lastnote = up->note[0];
+	if(--up->nnote > 0)
+		memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note*));
+	up->note[up->nnote] = nil;
 
-	if(u != nil && strncmp(up->lastnote.msg, "sys:", 4) == 0){
-		int l = strlen(up->lastnote.msg);
+	if(u != nil && strncmp(up->lastnote->msg, "sys:", 4) == 0){
+		int l = strlen(up->lastnote->msg);
 		assert(l < ERRMAX);
-		snprint(up->lastnote.msg+l, ERRMAX-l, " pc=%#p", u->pc);
+		snprint(up->lastnote->msg+l, ERRMAX-l, " pc=%#p", u->pc);
 	}
 
 	if(up->notify == nil || up->notified){
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug){
+		if(up->lastnote->flag == NDebug){
 			up->fpstate &= ~FPillegal;
-			pprint("suicide: %s\n", up->lastnote.msg);
+			pprint("suicide: %s\n", up->lastnote->msg);
 		}
-		pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
+		pexit(up->lastnote->msg, up->lastnote->flag!=NDebug);
 	}
 	up->notified = 1;
 
-	return up->lastnote.msg;
+	return up->lastnote->msg;
 }
 
 /*
@@ -948,10 +951,11 @@
  *  lock if we can't get the r->lock and retrying.
  */
 int
-postnote(Proc *p, int dolock, char *n, int flag)
+postnote(Proc *p, int dolock, char *msg, int flag)
 {
 	int s, ret;
 	QLock *q;
+	Note *n;
 
 	if(p == nil)
 		return 0;
@@ -965,14 +969,22 @@
 		return 0;
 	}
 
-	if(n != nil && flag != NUser && (p->notify == nil || p->notified))
-		p->nnote = 0;
-
 	ret = 0;
-	if(p->nnote < NNOTE && n != nil) {
-		kstrcpy(p->note[p->nnote].msg, n, ERRMAX);
-		p->note[p->nnote++].flag = flag;
-		ret = 1;
+	if(msg != nil){
+		if(flag != NUser && (p->notify == nil || p->notified))
+			freenotes(p);
+		if(p->nnote < NNOTE){
+			if(flag != NUser)
+				n = smalloc(sizeof(Note));
+			else
+				n = malloc(sizeof(Note));
+			if(n != nil){
+				kstrcpy(n->msg, msg, ERRMAX);
+				n->flag = flag;
+				p->note[p->nnote++] = n;
+				ret = 1;
+			}
+		}
 	}
 	p->notepending = 1;
 	if(dolock)
@@ -1121,6 +1133,15 @@
 }
 
 void
+freenotes(Proc *p)
+{
+	while(p->nnote > 0){
+		free(p->note[--p->nnote]);
+		up->note[p->nnote] = nil;
+	}
+}
+
+void
 pexit(char *exitstr, int freemem)
 {
 	Proc *p;
@@ -1231,6 +1252,11 @@
 		free(wq);
 	}
 
+	freenotes(up);
+	free(up->lastnote);
+	up->lastnote = nil;
+	up->notified = 0;
+
 	/* release debuggers */
 	if(up->pdbg != nil) {
 		wakeup(&up->pdbg->sleep);
@@ -1464,9 +1490,10 @@
 	}
 
 	p->nnote = 0;
-	p->notify = nil;
+ 	p->notify = nil;
 	p->notified = 0;
 	p->notepending = 0;
+	p->lastnote = nil;
 
 	p->procmode = 0640;
 	p->privatemem = 1;
--- a/sys/src/9/port/sysproc.c
+++ b/sys/src/9/port/sysproc.c
@@ -105,12 +105,12 @@
 	p->dot = up->dot;
 	incref(p->dot);
 
-	memmove(p->note, up->note, sizeof(p->note));
-	p->nnote = up->nnote;
-	p->notify = up->notify;
+	p->nnote = 0;
+ 	p->notify = up->notify;
 	p->notified = 0;
 	p->notepending = 0;
-	p->lastnote = up->lastnote;
+	p->lastnote = nil;
+
 	if((flag & RFNOTEG) == 0)
 		p->noteid = up->noteid;
 
@@ -599,7 +599,9 @@
 	up->nargs = n;
 	up->setargs = 0;
 
-	up->nnote = 0;
+	freenotes(up);
+	free(up->lastnote);
+	up->lastnote = nil;
 	up->notify = nil;
 	up->notified = 0;
 	up->privatemem = 0;
--- a/sys/src/9/ppc/trap.c
+++ b/sys/src/9/ppc/trap.c
@@ -774,14 +774,14 @@
 		break;
 
 	default:
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 		/* fall through */
 		
 	case NDFLT:
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
-		pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag!=NDebug);
 	}
 	up->fpstate &= ~FPillegal;
 	if (up->fpstate == FPactive)
--- a/sys/src/9/sgi/trap.c
+++ b/sys/src/9/sgi/trap.c
@@ -587,14 +587,14 @@
 
 	default:
 		pprint("unknown noted arg %#lux\n", arg0);
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 		/* fall through */
 
 	case NDFLT:
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
 		qunlock(&up->debug);
-		pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag!=NDebug);
 	}
 }
 
--- a/sys/src/9/teg2/syscall.c
+++ b/sys/src/9/teg2/syscall.c
@@ -87,13 +87,13 @@
 		cur->r0 = (uintptr)nf->arg0;
 		break;
 	default:
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 		/*FALLTHROUGH*/
 	case NDFLT:
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
-		pexit(up->lastnote.msg, up->lastnote.flag != NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag != NDebug);
 	}
 }
 
--- a/sys/src/9/xen/trap.c
+++ b/sys/src/9/xen/trap.c
@@ -613,16 +613,14 @@
 
 	default:
 		pprint("unknown noted arg 0x%lux\n", arg0);
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 		/* fall through */
 		
 	case NDFLT:
-		if(up->lastnote.flag == NDebug){ 
-			qunlock(&up->debug);
-			pprint("suicide: %s\n", up->lastnote.msg);
-		} else
-			qunlock(&up->debug);
-		pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
+		qunlock(&up->debug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag!=NDebug);
 	}
 }
 
--- a/sys/src/9/zynq/trap.c
+++ b/sys/src/9/zynq/trap.c
@@ -395,13 +395,13 @@
 		break;
 	
 	default:
-		up->lastnote.flag = NDebug;
+		up->lastnote->flag = NDebug;
 	
 	case NDFLT:
 		qunlock(&up->debug);
-		if(up->lastnote.flag == NDebug)
-			pprint("suicide: %s\n", up->lastnote.msg);
-		pexit(up->lastnote.msg, up->lastnote.flag != NDebug);
+		if(up->lastnote->flag == NDebug)
+			pprint("suicide: %s\n", up->lastnote->msg);
+		pexit(up->lastnote->msg, up->lastnote->flag != NDebug);
 	}
 }