shithub: riscv

Download patch

ref: 853f0e33fe744d816d66276491c34f9bccd4e6e3
parent: de27182a8ebe759bebfb7b123dffbb0f29eea922
author: Sigrid <ftrvxmtrx@gmail.com>
date: Fri Jul 31 11:48:54 EDT 2020

vmx(1): use cycles() instead of nsec() when possible

this provides better timing and reduced number of syscalls (~2.7M old
vs ~35K new in a test)

--- a/sys/src/cmd/vmx/fns.h
+++ b/sys/src/cmd/vmx/fns.h
@@ -52,3 +52,4 @@
 int x86access(int, uintptr, int, uvlong*, int, int, TLB *);
 u32int io(int, u16int, u32int, int);
 int x86step(void);
+uvlong nanosec(void);
--- a/sys/src/cmd/vmx/io.c
+++ b/sys/src/cmd/vmx/io.c
@@ -31,7 +31,7 @@
 	vlong t;
 	
 	if(rtcnext != -1){
-		t = nsec();
+		t = nanosec();
 		if(t >= rtcnext){
 			cmos[0xc] |= 0x40;
 			rtcnext = -1;
@@ -58,7 +58,7 @@
 	default: d = 4 + (cmos[0xa] & 0xf);
 	}
 	b = (1000000000ULL << d) / 1048576;
-	t = nsec();
+	t = nanosec();
 	rtcnext = t + b - t % b;
 	settimer(rtcnext);
 }
@@ -464,7 +464,7 @@
 
 	for(i = 0; i < 3; i++){
 		p = &pit[i];
-		nt = nsec();
+		nt = nanosec();
 		t = nt - p->lastnsec;
 		p->lastnsec = nt;
 		switch(p->mode){
@@ -533,7 +533,7 @@
 		if(p->access != 3 || hi){
 			p->count = p->reload;
 			p->state = 1;
-			p->lastnsec = nsec();
+			p->lastnsec = nanosec();
 			settimer(p->lastnsec + p->count * PERIOD);
 		}else
 			p->state = 0;
@@ -543,7 +543,7 @@
 		if(p->state == 0 && (p->access != 3 || hi)){
 			p->count = p->reload;
 			p->state = 1;
-			p->lastnsec = nsec();
+			p->lastnsec = nanosec();
 			pitadvance();
 		}
 		break;
@@ -621,7 +621,7 @@
 			pit[n].reload = 0;
 			pit[n].readstate = pit[n].access == 1 ? READHI : READLO;
 			pit[n].writestate = pit[n].access == 1 ? READHI : READLO;
-			pit[n].lastnsec = nsec();
+			pit[n].lastnsec = nanosec();
 			switch(pit[n].mode){
 			case 0:
 				pitout(n, 0);
--- a/sys/src/cmd/vmx/mkfile
+++ b/sys/src/cmd/vmx/mkfile
@@ -15,6 +15,7 @@
 	vesa.$O \
 	9p.$O \
 	x86.$O \
+	nanosec.$O \
 
 </sys/src/cmd/mkone
 
--- a/sys/src/cmd/vmx/vga.c
+++ b/sys/src/cmd/vmx/vga.c
@@ -358,7 +358,7 @@
 			n = read(fd, buf, sizeof(buf)-1);
 			if(n <= 0)
 				sysfatal("read /dev/kbd: %r");
-			kbwatchdog = nsec();
+			kbwatchdog = nanosec();
 			buf[n-1] = 0;
 			buf[n] = 0;
 		}
@@ -431,7 +431,7 @@
 				clicked = m.buttons & 1;
 				break;
 			}
-			if(kbwatchdog != 0 && nsec() - kbwatchdog > 1000ULL*1000*1000)
+			if(kbwatchdog != 0 && nanosec() - kbwatchdog > 1000ULL*1000*1000)
 				mousegrab = 0;
 			gotm = 1;
 			if(!ptinrect(m.xy, grabout)){
@@ -503,7 +503,7 @@
 		p += 160;
 	}
 	cp = (vga.crtc[14] << 8 | vga.crtc[15]);
-	if(cp >= sa && cp < sa + 80*25 && (vga.crtc[10] & 0x20) == 0 && nsec() / 500000000 % 2 == 0){
+	if(cp >= sa && cp < sa + 80*25 && (vga.crtc[10] & 0x20) == 0 && nanosec() / 500000000 % 2 == 0){
 		buf[0] = cp437[tfb[cp*2]];
 		attr = tfb[cp*2+1];
 		r.min = Pt((cp - sa) % 80 * 8, (cp - sa) / 80 * 16);
--- a/sys/src/cmd/vmx/virtio.c
+++ b/sys/src/cmd/vmx/virtio.c
@@ -518,7 +518,7 @@
 		if((v->net.flags & VNETHEADER) != 0){
 			txbuf[0] = len  >> 8;
 			txbuf[1] = len;
-			ns = nsec();
+			ns = nanosec();
 			txbuf[2] = ns >> 56;
 			txbuf[3] = ns >> 48;
 			txbuf[4] = ns >> 40;
--- a/sys/src/cmd/vmx/vmx.c
+++ b/sys/src/cmd/vmx/vmx.c
@@ -430,13 +430,13 @@
 	vlong then, now;
 
 	timerid = threadid();
-	timerevent = nsec() + SleeperPoll;
+	timerevent = nanosec() + SleeperPoll;
 	unlock(&timerlock);
 	threadsetname("sleeper");
 	for(;;){
 		lock(&timerlock);
 		then = timerevent;
-		now = nsec();
+		now = nanosec();
 		if(then <= now) timerevent = now + SleeperPoll;
 		unlock(&timerlock);
 		if(then - now >= MinSleep){
@@ -443,7 +443,7 @@
 			sleep((then - now) / MSEC);
 			continue;
 		}
-		while(nsec() < then)
+		while(nanosec() < then)
 			;
 		sendul(sleepch, 0);
 	}