shithub: riscv

Download patch

ref: 01b4c2a63dac5a244d20d3d06b44c4072227f19a
parent: bd3429304cdded196990683d4ee77cd36eac3b9c
author: cinap_lenrek <[email protected]>
date: Wed Sep 7 20:44:38 EDT 2016

kernel: always do unsigned subtractions for m->ticks delta for updatecpu() and rebalance(), handle ticks wrap arround in hzsched()

--- a/sys/src/9/port/proc.c
+++ b/sys/src/9/port/proc.c
@@ -201,7 +201,7 @@
 
 	/* unless preempted, get to run for at least 100ms */
 	if(anyhigher()
-	|| (!up->fixedpri && m->ticks > m->schedticks && anyready())){
+	|| (!up->fixedpri && (long)(m->ticks - m->schedticks) > 0 && anyready())){
 		m->readied = nil;	/* avoid cooperative scheduling */
 		up->delaysched++;
 	}
@@ -271,8 +271,7 @@
 void
 updatecpu(Proc *p)
 {
-	int n, t, ocpu;
-	int D = schedgain*HZ*Scaling;
+	ulong t, ocpu, n, D;
 
 	if(p->edf != nil)
 		return;
@@ -279,10 +278,11 @@
 
 	t = MACHP(0)->ticks*Scaling + Scaling/2;
 	n = t - p->lastupdate;
-	p->lastupdate = t;
-
 	if(n == 0)
 		return;
+	p->lastupdate = t;
+
+	D = schedgain*HZ*Scaling;
 	if(n > D)
 		n = D;
 
@@ -294,8 +294,7 @@
 		t = (t*(D-n))/D;
 		p->cpu = 1000 - t;
 	}
-
-//iprint("pid %d %s for %d cpu %d -> %d\n", p->pid,p==up?"active":"inactive",n, ocpu,p->cpu);
+//iprint("pid %lud %s for %lud cpu %lud -> %lud\n", p->pid,p==up?"active":"inactive",n, ocpu,p->cpu);
 }
 
 /*
@@ -463,9 +462,10 @@
 static void
 rebalance(void)
 {
-	int pri, npri, t, x;
+	int pri, npri, x;
 	Schedq *rq;
 	Proc *p;
+	ulong t;
 
 	t = m->ticks;
 	if(t - balancetime < HZ)