shithub: qk1

Download patch

ref: e43997246b9f7b9bcb762743dd44c08906cc3a33
parent: ca4cc1cb602c098dba51bb9f8e5da6b9a2827a7a
author: Konstantinn Bonnet <qu7uux@gmail.com>
date: Sat Feb 21 09:21:09 EST 2015

fix performance issues with sound; minor cleanups

- write() in SNDDMA_Submit() was blocking cpu use, put it on another proc, use a
  channel to sync; use nbsend and buffered channel to avoid waiting
- main() -> threadmain(); rfork -> proccreate
- remove some unused stuff
- sys_9: put setfcr call before all the other stuff since

--- a/README
+++ b/README
@@ -8,7 +8,7 @@
 	- to install an expansion or compatible mod (zB rogue):
 		. mkdir $home/lib/quake/rogue
 		. cp /n/quakecd/rogue/*.pak $home/lib/quake/rogue
-	- sound works, but is glitchy and reduces FPS quite a bit, esp. high resolutions
+	- sound is a glitchy with higher resolutions (see snd_9.c)
 	- cdaudio, networking: PORTME
 	- high resolutions work up to 4096x4096 (arbitrary limit in r_shared.h)
 		. by default, quake uses a 8MB heap, which will be insufficient for higher
@@ -45,7 +45,6 @@
 	since Con_Printf was often used there, and it outputs to both fd=1 and console,
 	so needs fixing
 	- vid_9.c: shitty, can probably improve performance a bit without screwing code
-	- snd_9.c: maybe writes to /dev/audio should be on another proc?
 	- *printf -> *print was stupid
 
 
@@ -59,17 +58,16 @@
 	- compile with BAN_TEST -> net_dgrm build fails
 	- sprites/particles not scaled correctly on high resolutions
 	- m_windowed 1: can still look around in frozen world when paused
+	- m_windowed 1: mouse can still escape, causing glitchy mouse look
 	- net_9p.c
 	- cd_9.c,net_udp.c,net_dgrm.c: actuallyport
 	- snd_9.c, vid_9.c: lousy
 	- manpages
-	- caps lock == Kctl (← kbdfs, notanissue?)
 	- ungrab mouse when entering console or menus a la games/doom (IN_Grabm())
 	- cleanse *_9.c from linux legacy
 	- linking errors with -T cflag
 	- fix potential fp exceptions in code rather than ignoring them?
 	- sys_9.c: proper P9 use (style)
-	- rewrite everything to use plan9 style? (ocd)
 	- g '/\* FIXME'
 	- port quakeworld and utilities +/- merge common code and mkmany
 	- Sys_Error printing %r systematically is stupid
--- a/in_9.c
+++ b/in_9.c
@@ -22,7 +22,7 @@
 int keyq_head = 0;
 int keyq_tail = 0;
 int mouseactive;
-int kpid = -1, mpid = -1;
+int ktid = -1, mtid = -1;
 
 /* vid_9.c */
 extern int config_notify;
@@ -143,7 +143,7 @@
 	return k;
 }
 
-void kproc (void)
+void kproc (void *)
 {
 	int n, k, fd;
 	char buf[128], kdown[128] = {0}, *s;
@@ -190,7 +190,7 @@
 	close(fd);
 }
 
-void mproc (void)
+void mproc (void *)
 {
 	int b, n, nerr, fd;
 	char buf[1+5*12];
@@ -254,13 +254,13 @@
 void IN_Shutdown (void)
 {
 	IN_Grabm(0);
-	if(kpid != -1){
-		postnote(PNPROC, kpid, "shutdown");
-		kpid = -1;
+	if(ktid != -1){
+		threadkill(ktid);
+		ktid = -1;
 	}
-	if(mpid != -1){
-		postnote(PNPROC, mpid, "shutdown");
-		mpid = -1;
+	if(mtid != -1){
+		threadkill(mtid);
+		mtid = -1;
 	}
 	mouse_avail = 0;
 }
@@ -268,31 +268,23 @@
 void sucks(void *, char *note)
 {
 	if(!strncmp(note, "sys:", 4))
-		IN_Shutdown();	/* FIXME: safe? */
+		IN_Shutdown();
 	noted(NDFLT);
 }
 
 void IN_Init (void)
 {
-	int pid;
-
 	Cvar_RegisterVariable(&m_windowed);
 	Cvar_RegisterVariable(&m_filter);
 	notify(sucks);
-	if((pid = rfork(RFPROC|RFMEM|RFFDG)) == 0){
-		kproc();
-		exits(nil);
-	}
-	kpid = pid;
+	if((ktid = proccreate(kproc, nil, mainstacksize)) < 0)
+		sysfatal("proccreate kproc: %r");
 	if(COM_CheckParm("-nomouse"))
 		return;
 	if(m_windowed.value)
 		IN_Grabm(1);
-	if((pid = rfork(RFPROC|RFMEM|RFFDG)) == 0){
-		mproc();
-		exits(nil);
-	}
-	mpid = pid;
+	if((mtid = proccreate(mproc, nil, mainstacksize)) < 0)
+		sysfatal("proccreate mproc: %r");
 	mouse_x = mouse_y = 0.0;
 	/* FIXME: both kind of do the same thing */
 	//mouse_avail = mouseactive = 1;
--- a/snd_9.c
+++ b/snd_9.c
@@ -1,14 +1,37 @@
 #include <u.h>
 #include <libc.h>
 #include <stdio.h>
+#include <thread.h>
 #include "quakedef.h"
 
 int audio_fd;
 int snd_inited;
 int wpos;
-int tryrates[] = { 11025, 22051, 44100, 8000 };
+int stid = -1;
+enum{
+	Nbuf	= 16
+};
+Channel *schan;
 
 
+void sproc (void *)
+{
+	int n;
+
+	for(;;){
+		if(recv(schan, nil) < 0){
+			Sys_Warn("sproc:recv");
+			break;
+		}
+		if((n = write(audio_fd, shm->buffer, shm->samplebits/8 * shm->samples)) < 0){
+			Sys_Warn("SNDDMA_Submit:write");
+			break;
+		}
+		wpos += n;
+	}
+	stid = -1;
+}
+
 qboolean SNDDMA_Init(void)
 {
 	int i;
@@ -44,7 +67,6 @@
 	shm->samples = 1024;
 	shm->submission_chunk = 1;
 
-	/* FIXME: use Hunk_AllocName? */
 	if((shm->buffer = mallocz(shm->samplebits/8 * shm->samples, 1)) == nil){
 		Sys_Warn("SNDDMA_Init:malloc");
 		close(audio_fd);
@@ -51,6 +73,11 @@
 		return 0;
 	}
 	shm->samplepos = 0;
+	schan = chancreate(sizeof(int), Nbuf);
+	if((stid = proccreate(sproc, nil, mainstacksize)) < 0){
+		Sys_Warn("proccreate sproc");
+		return 0;
+	}
 	snd_inited = 1;
 	return 1;
 }
@@ -68,6 +95,12 @@
 	if(snd_inited){
 		close(audio_fd);
 		free(shm->buffer);
+		if(stid != -1){
+			threadkill(stid);
+			stid = -1;
+		}
+		chanclose(schan);
+		chanfree(schan);
 		snd_inited = 0;
 	}
 }
@@ -74,11 +107,8 @@
 
 void SNDDMA_Submit(void)
 {
-	int n;
-
-	if((n = write(audio_fd, shm->buffer, shm->samplebits/8 * shm->samples)) < 0){
-		Sys_Warn("SNDDMA_Submit:write");
+	if(nbsend(schan, nil) < 0){
+		Sys_Warn("SNDDMA_Submit:send");
 		SNDDMA_Shutdown();
 	}
-	wpos += n;
 }
--- a/sys_9.c
+++ b/sys_9.c
@@ -1,14 +1,13 @@
 #include <u.h>
 #include <libc.h>
 #include <stdio.h>
+#include <thread.h>
 #include "quakedef.h"
 
 qboolean isDedicated;
 int nostdout = 0;
-char *cachedir = "/tmp";
-cvar_t sys_linerefresh = {"sys_linerefresh","0"};	// set for entity display
+mainstacksize = 512*1024;	/* FIXME */
 
-
 void Sys_Printf (char *fmt, ...)
 {
 	char buf[1024];
@@ -158,12 +157,8 @@
 {
 }
 
-void Sys_LineRefresh (void)
+void threadmain (int c, char **v)
 {
-}
-
-void main (int c, char **v)
-{
 	static char basedir[1024];
 	int j;
 	char *home;
@@ -174,6 +169,9 @@
 
 	memset(&parms, 0, sizeof(parms));
 
+	/* ignore fp exceptions (bad), rendering shit assumes they are */
+	setfcr(getfcr() & ~(FPOVFL|FPUNFL|FPINVAL|FPZDIV));	/* FIXME */
+
 	COM_InitArgv(c, v);
 	parms.argc = com_argc;
 	parms.argv = com_argv;
@@ -189,12 +187,8 @@
 		free(home);
 	}else
 		snprintf(basedir, sizeof basedir, "/sys/lib/quake");
-
 	parms.basedir = basedir;
 
-	/* ignore fp exceptions (bad), rendering shit assumes they are */
-	setfcr(getfcr() & ~(FPOVFL|FPUNFL|FPINVAL|FPZDIV));	/* FIXME */
-
 	Host_Init(&parms);
 
 	if(COM_CheckParm("-nostdout"))
@@ -222,9 +216,5 @@
 			oldtime += time;
 
 		Host_Frame(time);
-
-		// graphic debugging aids
-		if (sys_linerefresh.value)
-			Sys_LineRefresh ();
 	}
 }