Skip to contentSkip to conversation

Async behavior and -EAGAIN on write?

Alsa-user Paul Adolph 1 participant 2 messages Jun 2, 2008 3 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 11 lines
Quoted 10 lines

For playback I am using the ALSA callback to wake up a thread that then feeds the ALSA ring buffer with a period-worth of samples using snd_pcm_writei(). I've set snd_pcm_sw_params_set_avail_min() to one period. The problem is that most of the time when my thread wakes up and tries to write the data, snd_pcm_writei() fails with -EAGAIN, and I must retry until it works.

Presumably this write failure is because there is not enough space in the ring buffer, but then why did the ALSA callback fire in the first place? As it stands my retries are eating needless amounts of CPU.

Quoted 6 lines

No definitive help. I don't know the internals, but I have a guess. It probably leads the
amount so that on busy systems, underruns don't occur. You could try
setting it to 2 buffer
min and still writing only a single buffer. That way, you have a buffer of slack.

It seems to make no difference what snd_pcm_sw_set_avail_min() value I use. I tried setting it from twice to many times the period size. I still get ALSA callbacks very often. When I check available space at each callback invocation using snd_pcm_avail_update(), I almost always get much less than this setting. Indeed most usually much less than a single period size.

Quoted 3 lines

You might have better luck trying this on the alsa-devel list. The developers live there and they
know the internals. It is possible they will answer.

Good idea. I am cross-posting this email. (Hopefully to no-one's annoyance...)

Quoted 5 lines

I went and looked at the function you are using.
(snip)
Are you using a buffer size compatible with your card. It is possible it is using the closest one and that is less than the buffer size you have defined. Just a thought.

To set buffer sizes, I am using set_buffer/period_time_near() and taking whatever the card gives me - I read them back with the get functions. One weird thing I noticed is that my card give me a period size of 470 frames *always*, regardless of whatever desired buffer and period time I ask for. I don't think this issue is related to the async behavior weirdness though.

Thanks for your help.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 7 lines
Quoted 7 lines

For playback I am using the ALSA callback to wake up a thread that then feeds the ALSA ring buffer with a period-worth of samples using snd_pcm_writei(). I've set snd_pcm_sw_params_set_avail_min() to one period. The problem is that most of the time when my thread wakes up and tries to write the data, snd_pcm_writei() fails with -EAGAIN, and I must retry until it works.

Quoted 7 lines
Quoted 4 lines

Presumably this write failure is because there is not enough space in the ring buffer, but then why did the ALSA callback fire in the first place? As it stands my retries are eating needless amounts of CPU.

What does snd_pcm_avail_update return if you call it before snd_pcm_writei in your callback?

Taking the sleep out of my thread so I respond to the ALSA callback wakeups as often as possible, I found that snd_pcm_avail_update() returns very small values, usually less than 1/2 the period size and sometimes even 0! It seems as if I'm getting callbacks whenever ALSA feels like giving them. This behavior does not seem to change at all even with different settings of snd_pcm_sw_params_set_avail_min()! I must be missing some key thing in all this.

Here are some of the readings. This is with snd_pcm_sw_params_set_avail_min(period_size) and period_size = 470 frames.
avail = 68
avail = 202
avail = 336
avail = 0
avail = 134

Quoted 1 line

What period and buffer sizes do you use?

I am using set_buffer/period_time_near and read back the actual values. I get the following with the settings I'm using.
buffer_size : 7526 frames
period_size : 470 frames

I'm set to 22050 sampling rate, 16-bit signed interleaved samples.

Quoted 3 lines

FYI, you should seriously consider using poll() rather than the async/callback interface. It's more efficient than signals and more compatible with various IO plugins like pulseaudio.

Yes - I'm looking into this for sure. This async mode has caused me lots of problems. My main issue is that I'm almost out of time to spend on this part of the project. :-(

Thanks for your help.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/