shithub: front

Download patch

ref: 42940399b81a57cd6b5618a746042095de75e95d
parent: 487c2dc215f3c0445480d8f2e266c16f3a53f657
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Wed Jul 24 15:55:42 EDT 2024

kernel: use flag and integer to handle affinity

Instead of Proc { Mach *mp; Mach *wired; },
track affinity by an integer representing
the mach number instead.

This simplifies the code as it avoids needing
to compare with MACHP(m->machno).

Wiering a process to a processor is now done
by just assigning affinity and then set a flag
that it should not change.

Call procpriority() when we want to change
priority of a process instead of managing
the fields directly.

--- a/sys/src/9/pc/cputemp.c
+++ b/sys/src/9/pc/cputemp.c
@@ -67,9 +67,8 @@
 static long
 intelcputemprd(Chan *c, void *va, long n, vlong offset)
 {
+	int r, t, i, w;
 	char *a;
-	long i, r, t;
-	Mach *w;
 
 	w = up->wired;
 	a = va;
--- a/sys/src/9/port/portdat.h
+++ b/sys/src/9/port/portdat.h
@@ -755,9 +755,6 @@
 	short	notified;	/* sysnoted is due */
 	int	(*notify)(void*, char*);
 
-	Mach	*wired;
-	Mach	*mp;		/* machine this process last ran on */
-
 	Lock	*lastlock;	/* debugging */
 	Lock	*lastilock;	/* debugging */
 
@@ -767,6 +764,8 @@
 	ulong	priority;	/* priority level */
 	ulong	basepri;	/* base priority level */
 	uchar	fixedpri;	/* priority level doesn't change */
+	uchar	wired;
+	int	affinity;	/* machno this process last ran on */
 	ulong	cpu;		/* cpu average */
 	ulong	lastupdate;
 
--- a/sys/src/9/port/proc.c
+++ b/sys/src/9/port/proc.c
@@ -9,6 +9,10 @@
 #include	"tos.h"
 #include	"ureg.h"
 
+enum {
+	Scaling = 2,
+};
+
 int	schedgain = 30;	/* units in seconds */
 int	nrdy;
 
@@ -28,13 +32,6 @@
 	int	nextindex;
 } procalloc;
 
-enum
-{
-	Q=10,
-	DQ=4,
-	Scaling=2,
-};
-
 Schedq	runq[Nrq];
 ulong	runvec;
 
@@ -212,7 +209,8 @@
 		m->schedticks = m->ticks + HZ/10;
 	m->readied = nil;
 	m->proc = up;
-	up->mach = up->mp = MACHP(m->machno);
+	up->mach = MACHP(m->machno);
+	up->affinity = m->machno;
 	up->state = Running;
 	mmuswitch(up);
 	gotolabel(&up->sched);
@@ -417,9 +415,9 @@
 	 * When the priority changes, we want to give
 	 * every cpu a chance to pick up the load.
 	 */
-	if(p->wired == nil)
+	if(!p->wired)
 	if(pri < 3 || pri != p->priority)
-		p->mp = nil;
+		p->affinity = -1;
 	p->priority = pri;
 
 	if(pri == PriEdf){
@@ -469,7 +467,7 @@
 		splx(s);
 		return;
 	case 0:
-		if(up != p && (p->wired == nil || p->wired == MACHP(m->machno)))
+		if(up != p && (!p->wired || p->affinity == m->machno))
 			m->readied = p;	/* group scheduling */
 		pri = reprioritize(p);
 		break;
@@ -610,7 +608,7 @@
 
 	/* cooperative scheduling until the clock ticks */
 	if((p = m->readied) != nil && p->mach == nil && p->state == Ready
-	&& (p->wired == nil || p->wired == MACHP(m->machno))
+	&& (!p->wired || p->affinity == m->machno)
 	&& runq[Nrq-1].head == nil && runq[Nrq-2].head == nil){
 		skipscheds++;
 		rq = &runq[p->priority];
@@ -633,8 +631,8 @@
 		 */
 		for(rq = &runq[Nrq-1]; rq >= runq; rq--){
 			for(p = rq->head; p != nil; p = p->rnext){
-				if(p->mp == nil || p->mp == MACHP(m->machno)
-				|| (p->wired == nil && i > 0))
+				if(p->affinity < 0 || p->affinity == m->machno
+				|| (!p->wired && i > 0))
 					goto found;
 			}
 		}
@@ -731,8 +729,8 @@
 	p->delaysched = 0;
 
 	/* sched params */
-	p->mp = nil;
-	p->wired = nil;
+	p->wired = 0;
+	p->affinity = -1;
 	procpriority(p, PriNormal, 0);
 	p->cpu = 0;
 	p->lastupdate = MACHP(0)->ticks*Scaling;
@@ -745,33 +743,31 @@
  * wire this proc to a machine
  */
 void
-procwired(Proc *p, int bm)
+procwired(Proc *p, int a)
 {
+	ushort nwired[MAXMACH];
 	Proc *pp;
 	int i;
-	char nwired[MAXMACH];
-	Mach *wm;
 
-	if(bm < 0){
+	if(a < 0){
 		/* pick a machine to wire to */
 		memset(nwired, 0, sizeof(nwired));
-		p->wired = nil;
+		p->wired = 0;
 		for(i=0; (pp = proctab(i)) != nil; i++){
-			wm = pp->wired;
-			if(wm != nil && pp->pid)
-				nwired[wm->machno]++;
+			a = pp->affinity;
+			if(a >= 0 && pp->wired && pp->pid)
+				nwired[a]++;
 		}
-		bm = 0;
+		a = 0;
 		for(i=0; i<conf.nmach; i++)
-			if(nwired[i] < nwired[bm])
-				bm = i;
+			if(nwired[i] < nwired[a])
+				a = i;
 	} else {
 		/* use the virtual machine requested */
-		bm = bm % conf.nmach;
+		a = a % conf.nmach;
 	}
-
-	p->wired = MACHP(bm);
-	p->mp = p->wired;
+	p->affinity = a;
+	p->wired = 1;
 }
 
 void
@@ -783,11 +779,7 @@
 		pri = 0;
 	p->basepri = pri;
 	p->priority = pri;
-	if(fixed){
-		p->fixedpri = 1;
-	} else {
-		p->fixedpri = 0;
-	}
+	p->fixedpri = fixed != 0;
 }
 
 void
@@ -1824,8 +1816,8 @@
 {
 	qlock(&up->debug);
 	kstrdup(&up->user, new);
-	up->basepri = PriNormal;
 	qunlock(&up->debug);
+	procpriority(up, PriNormal, 0);
 }
 
 /*
--- a/sys/src/9/port/sysproc.c
+++ b/sys/src/9/port/sysproc.c
@@ -33,7 +33,6 @@
 	Rgrp *org;
 	Egrp *oeg;
 	ulong pid, flag;
-	Mach *wm;
 	char *devs;
 
 	flag = va_arg(list, ulong);
@@ -229,13 +228,11 @@
 	 *  (i.e. has bad properties) and has to be discarded.
 	 */
 	flushmmu();
-	p->basepri = up->basepri;
-	p->priority = up->basepri;
-	p->fixedpri = up->fixedpri;
-	p->mp = up->mp;
-	wm = up->wired;
-	if(wm != nil)
-		procwired(p, wm->machno);
+
+	procpriority(p, up->basepri, up->fixedpri);
+	if(up->wired)
+		procwired(p, up->affinity);
+
 	ready(p);
 	sched();
 	return pid;
@@ -581,13 +578,6 @@
 		for(i=0; i<=f->maxfd; i++)
 			fdclose(i, CCEXEC);
 	}
-
-	/*
-	 *  '/' processes are higher priority (hack to make /ip more responsive).
-	 */
-	if(devtab[tc->type]->dc == L'/')
-		up->basepri = PriRoot;
-	up->priority = up->basepri;
 
 	poperror();	/* tc */
 	cclose(tc);
--