From: Clemens Ladisch <clemens@ladisch.de>
To: Russell King - ARM Linux <linux@arm.linux.org.uk>
Cc: alsa-devel@alsa-project.org,
Mark Brown <broonie@opensource.wolfsonmicro.com>
Subject: Re: ALSA calling pcm_pointer excessively?
Date: Thu, 10 May 2012 19:05:56 +0200 [thread overview]
Message-ID: <4FABF574.7090804@ladisch.de> (raw)
In-Reply-To: <20120510162930.GA17361@n2100.arm.linux.org.uk>
Russell King - ARM Linux wrote:
> I think what's happening is that snd_pcm_lib_write1() is looping, and
> each time it updates the hardware position, it finds that it can
> transfer 8 or 16 bytes to the buffer. Once it's transferred that,
> it re-updates the hardware position which has now advanced by another
> 8 or 16 bytes. Repeat, and you find that snd_pcm_lib_write1() spends
> a lot of time inefficiently copying the buffer.
>
> It seems that it will only sleep if the hardware pointer stops making
> progress.
I'd guess that most existing hardware is fast enough to transfer
samples and has a big enough granularity (typically 32 byte bursts
for PCI) that the pointer doesn't change in consecutive loop
iterations.
This (untested) patch tries to avoid too many busy looping.
Regards,
Clemens
--- a/sound/core/pcm_lib.c
+++ b/sound/core/pcm_lib.c
@@ -1894,7 +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;
- int err = 0;
+ int busy_loops = 0, err = 0;
if (size == 0)
return 0;
@@ -1919,12 +1919,17 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
runtime->twake = runtime->control->avail_min ? : 1;
while (size > 0) {
snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
- snd_pcm_uframes_t avail;
+ snd_pcm_uframes_t avail, avail_wait_max;
snd_pcm_uframes_t cont;
+
+ if (busy_loops < 5)
+ avail_wait_max = 0;
+ else
+ avail_wait_max = min(runtime->control->avail_min, size);
if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
snd_pcm_update_hw_ptr(substream);
avail = snd_pcm_playback_avail(runtime);
- if (!avail) {
+ if (avail <= avail_wait_max) {
if (nonblock) {
err = -EAGAIN;
goto _end_unlock;
@@ -1934,6 +1939,9 @@ static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
err = wait_for_avail(substream, &avail);
if (err < 0)
goto _end_unlock;
+ busy_loops = 0;
+ } else {
+ busy_loops++;
}
frames = size > avail ? avail : size;
cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
next prev parent reply other threads:[~2012-05-10 17:06 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-10 16:06 ALSA calling pcm_pointer excessively? Russell King - ARM Linux
2012-05-10 16:29 ` Russell King - ARM Linux
2012-05-10 17:05 ` Clemens Ladisch [this message]
2012-05-10 17:13 ` Clemens Ladisch
2012-05-10 18:18 ` Russell King - ARM Linux
2012-05-10 18:43 ` Clemens Ladisch
2012-05-10 23:09 ` Russell King - ARM Linux
2012-05-10 17:47 ` Jassi Brar
2012-05-11 13:31 ` Takashi Iwai
2012-05-11 14:02 ` Russell King - ARM Linux
2012-05-11 14:17 ` Takashi Iwai
2012-05-11 15:04 ` Russell King - ARM Linux
2012-05-11 16:31 ` Takashi Iwai
2012-05-11 16:38 ` Russell King - ARM Linux
2012-05-11 17:07 ` Takashi Iwai
2012-05-10 16:34 ` Trent Piepho
2012-05-10 16:42 ` Russell King - ARM Linux
2012-05-10 17:00 ` Jassi Brar
2012-05-10 17:04 ` Trent Piepho
2012-05-10 17:04 ` Russell King - ARM Linux
2012-05-10 17:18 ` Jassi Brar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4FABF574.7090804@ladisch.de \
--to=clemens@ladisch.de \
--cc=alsa-devel@alsa-project.org \
--cc=broonie@opensource.wolfsonmicro.com \
--cc=linux@arm.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.