All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alexander E. Patrakov" <patrakov@gmail.com>
To: Clemens Ladisch <clemens@ladisch.de>,
	ALSA Development Mailing List <alsa-devel@alsa-project.org>,
	Takashi Iwai <tiwai@suse.de>,
	David Henningsson <david.henningsson@canonical.com>,
	Takashi Sakamoto <o-takashi@sakamocchi.jp>
Subject: Re: Master Plan on rewinding
Date: Mon, 08 Sep 2014 13:31:18 +0600	[thread overview]
Message-ID: <540D5B46.3020904@gmail.com> (raw)
In-Reply-To: <540CC53B.7080204@ladisch.de>

08.09.2014 02:51, Clemens Ladisch wrote:
> Alexander E. Patrakov wrote:
>> The consensus is that rewind_safeguard is a workaround for an ALSA bug.
>
> I do not agree with this consensus.

OK, disagreements are what this thread is for :)

>
>> This information should come from snd_pcm_rewindable() from the hw
>> plugin. I.e., on a hw device, it should not be equal to
>> snd_pcm_mmap_hw_avail().  [...]
>> the safeguard should be ideally equal to the granularity of hardware
>> pointer updates.
>
> When snd_pcm_rewindable() is called, it uses the last reported hardware
> pointer position, which is the boundary between safe-to-rewrite and
> already-used data.  Subtracting the amount of one pointer update step
> makes it safe for the next pointer update, but it is not known how much
> time will elapse until the hardware does this update.  This time might
> be too small for the software to have any chance, or even zero, but it
> is also possible that there is still enough time to do the rewriting up
> until the reported boundary.  And if the software is too slow, it is
> even possible that two or more pointer updates happen, which would have
> required a larger safeguard.

I disagree with the words "boundary between safe-to-rewrite and 
already-used data". They describe a point that hardware knows, but 
software cannot know. My viewpoint is that, even with a perfect 
scheduler, known-safe-to-rewrite and known-already-used data are not 
separated with a point. See below for an example.

You indeed have a strong argument for keeping the safeguard in 
PulseAudio as a protection against scheduler glitches, but look what 
happens on ymfpci. Having read David's email, I understand that it is 
not the same reason why the safeguard has been originally added.

On ymfpci, interrupts (that don't correspond 1:1 to periods) happen 
every 5 ms, and pointer (which is a hardware register) is updated only 
during interrupts by the hardware.

|---------|---------P----h----p---------|-a-------|---------|

Here each character corresponds to 0.5 ms, "-" means nothing 
interesting, "|", "P" and "p" mark interrupt positions, "h" marks the 
place where the card reads the sound data from (i.e. the true boundary 
between safe-to-rewrite and already-used data). Thus, the interrupt at 
"P" has already happened, but the interrupt at "p" hasn't. "a" is the 
application write pointer.

So, what should alsa-lib return for snd_pcm_avail() and 
snd_pcm_rewind()? The driver only knows that "P" is already used, can 
infer that "p" isn't used yet, and knows nothing about samples in the 
middle.

For snd_pcm_avail(), it can only say that the application may only write 
up to "P" from the left. Everything else would be a possible lie leading 
to not-yet-played data possibly being overwritten.

For snd_pcm_rewindable(), it can only say that the application may try 
to go to "p" from the right. Everything further to the left is at risk 
of being already-played, even with a perfect scheduler and infinitely 
fast CPU.

> In other words: the snd_pcm_mmap_hw_avail() value is possibly ouf of
> date, but due to the real-time nature of hardware pointer updates, it is
> not possible to define a safeguard that would be more correct.  Writing
> the buffer near the DMA pointer is always racy.

Correct. The problem is that, on some cards, the uncertainty here 
(described above) is an order of magnitude bigger than typical scheduler 
glitches, and depends on sound card hardware. I don't want to introduce 
an unnecessarily-big safeguard for all sound cards because of a few bad 
ones.

So it looks like we need both the hardware-independent safeguard in 
PulseAudio (against scheduler glitches) and the hardware-dependent 
safeguard in alsa-lib (against known imprecise reporting by the card).

> The only somewhat reliable way to determine if a rewrite is successful
> is to check _afterwards_ whether the hardware pointer has advanced by
> too much.

PulseAudio does that in addition to the safeguard.

>> [...] On cards where pointer updates happen only on interrupts, the
>> driver should not configure the card in such a way that one period
>> visible to the userspace corresponds to one interrupt. Instead, it
>> should always configure the card for the minimum possible period
>> size, and report only part of the period interrupts to period_elapsed().
>
> Such stupid DMA controllers are typically found on mobile devices, which
> cannot afford extraneous interrupts.

Thanks for the information, I didn't know that, and hereby retract the 
quoted proposal.

Still, to support this type of stupid DMA controllers properly, we need 
a vocabulary of such weirdnesses and an agreed-upon way to communicate 
them from the kernel to userspace. I don't think that I can build such 
vocabulary.

>
>> === On the programmer expectations ===
>>
>> Some people, including at least Andrew Eikum and Clemens Ladisch, at
>> least once in the past expressed the opinion that amounts to "any
>> plugin that does not allow random access is, as far as the ALSA API is
>> concerned, buggy" (quoting http://permalink.gmane.org/gmane.linux.alsa.devel/122159 ),
>> i.e. they are maybe asking for the impossible.
>
> This was merely a description of the current API and its implementation,
> which assume that all devices/plugins have a rewritable ring buffer.
> I am fully aware that this assumption is wrong.

OK. Now I see that in your case it was a misinterpretation from my side 
- sorry for that! But this doesn't cancel the attitude "if a function is 
documented without any caveats, it must always work, usefully" that I 
have seen from others. So I'll put a documentation patch on my TODO list.

-- 
Alexander E. Patrakov

  parent reply	other threads:[~2014-09-08  7:31 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-07 15:16 Master Plan on rewinding Alexander E. Patrakov
2014-09-07 18:38 ` Tanu Kaskinen
2014-09-07 19:05   ` Alexander E. Patrakov
2014-09-07 20:51 ` Clemens Ladisch
2014-09-08  3:06   ` Raymond Yau
2014-09-08  7:31   ` Alexander E. Patrakov [this message]
2014-09-09  8:43     ` Clemens Ladisch
2014-09-09  8:55       ` Alexander E. Patrakov
2014-09-09  9:08         ` David Henningsson
2014-09-09  9:31           ` Alexander E. Patrakov
2014-09-21  2:02             ` Raymond Yau
2014-09-22 13:20               ` Lars-Peter Clausen
2014-09-22 13:36                 ` Alexander E. Patrakov
2014-09-22 13:44                   ` Lars-Peter Clausen
2014-09-23  8:29                   ` Raymond Yau
2014-09-23 10:22                     ` Alexander E. Patrakov
2014-09-09 13:45         ` Clemens Ladisch
2014-09-09 15:55           ` Alexander E. Patrakov
2014-09-09 16:09             ` Takashi Iwai
2014-09-07 23:12 ` David Henningsson
2014-09-09 19:56   ` Pierre-Louis Bossart
2014-09-10  5:38     ` Alexander E. Patrakov
2014-09-08  7:34 ` Lars-Peter Clausen
2014-09-08  7:59 ` David Henningsson
2014-09-08  8:46   ` Alexander E. Patrakov
2014-09-08  9:26     ` David Henningsson
2014-09-08 10:21       ` Alexander E. Patrakov
2014-09-09  8:43         ` Clemens Ladisch
2014-09-11  3:49 ` Raymond Yau
2014-09-11  4:19   ` A. C. Censi
2014-09-13  9:15     ` Raymond Yau
2014-09-11  5:28   ` Alexander E. Patrakov
2014-09-11  6:21     ` Raymond Yau
2014-09-13  8:57     ` Raymond Yau
2014-09-13 10:43       ` Alexander E. Patrakov
2014-09-13 11:33         ` Raymond Yau
2014-09-13 11:36           ` Alexander E. Patrakov
2014-09-13 18:35 ` Alexander E. Patrakov
2014-09-14 11:37   ` Raymond Yau
2014-09-14 12:07     ` Alexander E. Patrakov
2014-09-15  2:43       ` Raymond Yau
2014-09-15  9:19       ` Takashi Iwai
2014-09-15  9:58         ` Alexander E. Patrakov
2014-09-15 10:08           ` Takashi Iwai
2014-09-15 17:01             ` Pierre-Louis Bossart
2014-09-15 17:14               ` Alexander E. Patrakov
2014-09-15 18:08                 ` Takashi Iwai
2014-09-18  1:15                   ` Raymond Yau
2014-09-21  9:22                     ` Alexander E. Patrakov
2014-09-21  9:53                     ` Clemens Ladisch
2014-09-21 10:56                       ` Alexander E. Patrakov
2014-09-22  3:27                       ` Raymond Yau

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=540D5B46.3020904@gmail.com \
    --to=patrakov@gmail.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=clemens@ladisch.de \
    --cc=david.henningsson@canonical.com \
    --cc=o-takashi@sakamocchi.jp \
    --cc=tiwai@suse.de \
    /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.