diff options
author | Takashi Iwai <tiwai@suse.de> | 2012-05-11 17:50:49 +0200 |
---|---|---|
committer | Takashi Iwai <tiwai@suse.de> | 2012-05-11 19:05:12 +0200 |
commit | 0910c216f78d1097a4ac6dcc83b38809dea94160 (patch) | |
tree | 906b3beea4baea0c31062ff2ebc1714fedc48a62 | |
parent | b2522f9262539fc328b4b9344f8a2f7ef2cb18d5 (diff) | |
download | linux-0910c216f78d1097a4ac6dcc83b38809dea94160.tar.gz linux-0910c216f78d1097a4ac6dcc83b38809dea94160.tar.bz2 linux-0910c216f78d1097a4ac6dcc83b38809dea94160.zip |
ALSA: pcm - Optimize the call of snd_pcm_update_hw_ptr() in read/write loop
In the PCM read/write loop, the driver calls snd_pcm_update_hw_ptr()
at each time at the beginning of the loop. Russell King reported that
this hogs CPU significantly.
The current code assumes that the pointer callback is very fast and
cheap, also not too much fine grained. It's not true in all cases.
When the pointer advances short samples while the read/write copy has
been performed, the driver updates the hw_ptr and gets avail > 0
again. Then it tries to read/write these small chunks. This repeats
until the avail really gets to zero.
For avoiding this situation, a simple workaround is to call
snd_pcm_update_hw_ptr() only once at starting the loop, assuming that
the read/write copy is performed fast enough. If the available count
becomes short, it goes to snd_pcm_wait_avail() anyway, and this
processes right.
Tested-by: Russell King <rmk+kernel@arm.linux.org.uk>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
-rw-r--r-- | sound/core/pcm_lib.c | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c index 4d18941178e6..faedb1481b24 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -1894,6 +1894,7 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, struct snd_pcm_runtime *runtime = substream->runtime; snd_pcm_uframes_t xfer = 0; snd_pcm_uframes_t offset = 0; + snd_pcm_uframes_t avail; int err = 0; if (size == 0) @@ -1917,13 +1918,12 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, } runtime->twake = runtime->control->avail_min ? : 1; + if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) + snd_pcm_update_hw_ptr(substream); + avail = snd_pcm_playback_avail(runtime); while (size > 0) { snd_pcm_uframes_t frames, appl_ptr, appl_ofs; - snd_pcm_uframes_t avail; snd_pcm_uframes_t cont; - if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) - snd_pcm_update_hw_ptr(substream); - avail = snd_pcm_playback_avail(runtime); if (!avail) { if (nonblock) { err = -EAGAIN; @@ -1971,6 +1971,7 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, offset += frames; size -= frames; xfer += frames; + avail -= frames; if (runtime->status->state == SNDRV_PCM_STATE_PREPARED && snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) { err = snd_pcm_start(substream); @@ -2111,6 +2112,7 @@ static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream, struct snd_pcm_runtime *runtime = substream->runtime; snd_pcm_uframes_t xfer = 0; snd_pcm_uframes_t offset = 0; + snd_pcm_uframes_t avail; int err = 0; if (size == 0) @@ -2141,13 +2143,12 @@ static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream, } runtime->twake = runtime->control->avail_min ? : 1; + if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) + snd_pcm_update_hw_ptr(substream); + avail = snd_pcm_capture_avail(runtime); while (size > 0) { snd_pcm_uframes_t frames, appl_ptr, appl_ofs; - snd_pcm_uframes_t avail; snd_pcm_uframes_t cont; - if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) - snd_pcm_update_hw_ptr(substream); - avail = snd_pcm_capture_avail(runtime); if (!avail) { if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) { @@ -2202,6 +2203,7 @@ static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream, offset += frames; size -= frames; xfer += frames; + avail -= frames; } _end_unlock: runtime->twake = 0; |