alsa: Fix excessive I/O causing higher CPU usage SDL-1.2
authorRyan C. Gordon <icculus@icculus.org>
Sun, 22 Mar 2020 22:03:38 -0400
branchSDL-1.2
changeset 136659c0fc6e28c5e
parent 13529 9341324b4c61
alsa: Fix excessive I/O causing higher CPU usage

"On GCW Zero jz4770 platform, I saw higher than usual CPU usage when
running a more recent kernel (4.xx series versus 3.xx). Upon
investigation, it was found that the ALSA pcm file was not blocking
as it should. This resulted in ~30-50,000 system calls a second that
were unnecesary.

After adjusting the order in which SDL requests its pcm blocking mode,
the number of syscalls a second has dropped to a much smaller figure,
< 1,000/sec if I recall correctly. CPU usage also dropped by ~5%."

(This patch was written by Daniel Silsby.)

Fixes Bugzilla #4941.
src/audio/alsa/SDL_alsa_audio.c
     1.1 --- a/src/audio/alsa/SDL_alsa_audio.c	Mon Nov 18 21:58:36 2019 +0000
     1.2 +++ b/src/audio/alsa/SDL_alsa_audio.c	Sun Mar 22 22:03:38 2020 -0400
     1.3 @@ -496,6 +496,10 @@
     1.4  		return(-1);
     1.5  	}
     1.6  
     1.7 +	/* Switch to blocking mode for playback */
     1.8 +	/* Note: this must happen before hw/sw params are set. */
     1.9 +	SDL_NAME(snd_pcm_nonblock)(pcm_handle, 0);
    1.10 +
    1.11  	/* Figure out what the hardware is capable of */
    1.12  	snd_pcm_hw_params_alloca(&hwparams);
    1.13  	status = SDL_NAME(snd_pcm_hw_params_any)(pcm_handle, hwparams);
    1.14 @@ -628,9 +632,6 @@
    1.15  	}
    1.16  	SDL_memset(mixbuf, spec->silence, spec->size);
    1.17  
    1.18 -	/* Switch to blocking mode for playback */
    1.19 -	SDL_NAME(snd_pcm_nonblock)(pcm_handle, 0);
    1.20 -
    1.21  	/* We're ready to rock and roll. :-) */
    1.22  	return(0);
    1.23  }