code: plan9front

Download patch

ref: 692919521c15531cc0bed3006fd958b164a746ab
parent: 007d42e741b780aeecefd66776b1733c3dd53522
author: Sigrid <ftrvxmtrx@gmail.com>
date: Tue Feb 23 10:54:09 EST 2021

vmx: reduce cpu load by eliminating nop-loop

Sacrifice some of the sub-millisecond timer precision in favor of less
cpu load when the timer is about to be kicked a bit early.  Result is
visible *especially* when the guest idling.

Timer proc *still* has to send to the channel (in order to kick PIT
and RTC logic), which takes time, and compensates a bit for possibly
early runs.

--- a/sys/src/cmd/vmx/vmx.c
+++ b/sys/src/cmd/vmx/vmx.c
@@ -423,12 +423,13 @@
 static void
 sleeperproc(void *)
 {
-	vlong then, now;
+	vlong then, now, dt, adj;
 
 	timerid = threadid();
 	timerevent = nanosec() + SleeperPoll;
 	unlock(&timerlock);
 	threadsetname("sleeper");
+	adj = 0;
 	for(;;){
 		lock(&timerlock);
 		then = timerevent;
@@ -435,12 +436,15 @@
 		now = nanosec();
 		if(then <= now) timerevent = now + SleeperPoll;
 		unlock(&timerlock);
-		if(then - now >= MinSleep){
-			sleep((then - now) / MSEC);
-			continue;
+		if(then > now){
+			dt = then - now;
+			if(dt+adj >= MinSleep){
+				sleep((dt + adj) / MSEC);
+				continue;
+			}
+			adj = dt;
 		}
-		while(nanosec() < then)
-			;
+
 		sendul(sleepch, 0);
 	}
 }