[A5.0.10] - High CPU usage after al_attach_mixer_to_voice
Renato Riolino


I'm hunting a very high CPU usage on my game and I found out that the CPU increases after calling al_attach_mixer_to_voice().

I'm doing the following to initialize audio:

1al_init(); 2if (!al_is_audio_installed()) 3 if (!al_install_audio()) 4 return; 5al_init_acodec_addon(); 6 7av_Voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); 8if (av_Voice == NULL) 9 return; 10am_Mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); 11if (am_Mixer == NULL) 12 return; 13 14al_attach_mixer_to_voice(am_Mixer, av_Voice); 15al_set_default_mixer(am_Mixer); 16al_reserve_samples(32);

Before al_create_voice(), pulseaudio and my game are using less than 1% of CPU.
After al_create_voice(), pulseaudio is using 10% of CPU and my game still 1%.
When al_attach_mixer_to_voice() is called, pulseaudio start using 35% and my game 20%.

Those CPU percentages was checked using top while my game was on a sleep() after calling each function.

There is no threads running, just my main.c with this code.

My system is a Ubuntu Linux, kernel 3.8.0-35-generic 64 bits.

2lspci | grep Audio 300:1b.0 Audio device: Intel Corporation 82801H (ICH8 Family) HD Audio Controller (rev 03) 4 5cat /proc/asound/cards 6 0 [Intel ]: HDA-Intel - HDA Intel 7 HDA Intel at 0xfebf8000 irq 48 8 9alsactl -v 10alsactl version 1.0.25 11 12pulseaudio --version 13pulseaudio 3.0 14 15Allegro is 5.0.10


Could you provide a self-contained example? It'll be easier to nail down the problem that way.

EDIT: Nvm, I can reproduce it with just the code you gave... kind of odd, I bet we're spin-locking somewhere. I'll see if it's easy to find/fix.

EDIT2: It doesn't appear to be obvious... gdb shows 4 threads:

  • the main thread is sleeping in al_rest()

  • thread 2 is waiting for events from the window

  • thread 3 appears to be polling inside pulse audio, and I suspect this is responsible for the CPU usage

  • thread 4 is waiting on a condition variable inside the voice thread

So one'd have to find out why thread 3 exists and what it is doing... the backtrace is not helpful:

#0  0x00007ffff73dca43 in __GI___poll (fds=<optimized out>, nfds=<optimized out>, timeout=<optimized out>) at ../sysdeps/unix/sysv/linux/poll.c:87
#1  0x00007ffff5e61b6f in ?? () from /usr/lib/x86_64-linux-gnu/libpulse.so.0
#2  0x00007ffff5e530b5 in pa_mainloop_poll () from /usr/lib/x86_64-linux-gnu/libpulse.so.0
#3  0x00007ffff5e53719 in pa_mainloop_iterate () from /usr/lib/x86_64-linux-gnu/libpulse.so.0
#4  0x00007ffff5e537d0 in pa_mainloop_run () from /usr/lib/x86_64-linux-gnu/libpulse.so.0
#5  0x00007ffff5e61b1f in ?? () from /usr/lib/x86_64-linux-gnu/libpulse.so.0
#6  0x00007ffff4a2c543 in ?? () from /usr/lib/x86_64-linux-gnu/pulseaudio/libpulsecommon-2.1.so
#7  0x00007ffff6de2e9a in start_thread (arg=0x7fffee3d7700) at pthread_create.c:308
#8  0x00007ffff73e83fd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
#9  0x0000000000000000 in ?? ()


Further experimentation has shown that it might just be because the voice is started out in the playing state... however, I'm having trouble getting the voice/mixer playing functions to affect the CPU usage in a positive way (notable, calling al_set_mixer_playing(mixer, false) causes the CPU to spike to 200%!)

Thread #613903. Printed from Allegro.cc