mnemosyne causes cpu usage in pulseaudio

Bug #290691 reported by marmuta
8
Affects Status Importance Assigned to Milestone
The Mnemosyne Project
Fix Released
Undecided
Unassigned
mnemosyne (Ubuntu)
Fix Released
Medium
Scott Kitterman
Intrepid
Fix Released
Medium
Scott Kitterman

Bug Description

Binary package hint: mnemosyne

Ubuntu intrepid 8.10RC
mnemosyne 1.1-0ubuntu1

Immediately after starting mnemosyne pulseaudio silently opens an audio stream. No audio can be heard but pulseaudio still appears to play, using cpu seemingly for nothing. Cpu use goes down right after closing mnemosyne.
Since I have mnemosyne running most of the time (to add the occasional flash card), but never use it for audio, this seems like a waste of resources. It slows down the rest of the system and keeps cpu and audio hardware awake all the time. potentially reducing runtime for notebooks.

I expected mnemosyne to open an audio stream only when actually playing media embedded into flash cards.
Instead it plays silence during the whole runtime of the program, wasting system resources.

Steps to reproduce:
$ sudo apt-get install mnemosyne
$ top -bn3 |grep pulseaudio
 7523 ubuntu 20 0 37320 5000 3820 S 0.0 0.2 12:44.92 pulseaudio
 7523 ubuntu 20 0 37320 5000 3820 S 0.0 0.2 12:44.92 pulseaudio
 7523 ubuntu 20 0 37320 5000 3820 S 0.0 0.2 12:44.92 pulseaudio
$ mnemosyne&
$ top|grep pulseaudio
 7523 ubuntu 20 0 39412 5628 4440 S 7.7 0.3 12:25.15 pulseaudio
 7523 ubuntu 20 0 39412 5628 4440 S 8.3 0.3 12:25.40 pulseaudio
 7523 ubuntu 20 0 39412 5628 4440 S 8.0 0.3 12:25.64 pulseaudio

The silent pulseaudio stream:
$ pactl list >pa2.list
$ mnemosyne&
$ pactl list >pa2.list
$ diff pa1.list pa2.list
82c82
< Latency: 0 usec
---
> Latency: 65833 usec
120a121,133
> *** Sink Input #6 ***
> Name: Simple DirectMedia Layer
> Driver: pulsecore/protocol-native.c
> Owner Module: 5
> Client: 31
> Sink: 0
> Sample Specification: s16le 2ch 22050Hz
> Channel Map: front-left,front-right
> Volume: 0: 100% 1: 100%
> Buffer Latency: 374248 usec
> Sink Latency: 73000 usec
> Resample method: speex-float-1
>
186c199,204
< *** Client #29 ***
---
> *** Client #31 ***
> Name: python
> Driver: pulsecore/protocol-native.c
> Owner Module: 5
>
> *** Client #32 ***

Revision history for this message
marmuta (marmuta) wrote :

This workaround stops it from playing any audio, including sound in flash cards:

$ SDL_AUDIODRIVER=none mnemosyne

It shows an error but runs anyway:
Unable to initialise sound.
Traceback (most recent call last):
  File "/var/lib/python-support/python2.5/mnemosyne/pyqt_ui/sound.py", line 43, in __init__
    pygame.mixer.init()
error: No available audio device

Revision history for this message
Peter Bienstman (peter-bienstman) wrote :

This is a pulseaudio/pygame issue, not a Mnemosyne one. Using e.g. the alsa driver, I don't see this behaviour.

Peter

Revision history for this message
marmuta (marmuta) wrote :

Hi, thanks for answering so quickly :)

When I run it with alsa the cpu usage just shifts to mnemosyne (on a Intrepid RC live cd):

ubuntu@ubuntu:~$ pulseaudio -k
W: ltdl-bind-now.c: Failed to find original dlopen loader.
ubuntu@ubuntu:~$ SDL_AUDIODRIVER=alsa mnemosyne&
[1] 8945
ubuntu@ubuntu:~$ top -bn3 |grep pulseaudio
ubuntu@ubuntu:~$ top -bn3 |grep mnemosyne
 8945 ubuntu 20 0 55220 20m 13m S 7.9 1.0 0:03.62 mnemosyne
 8945 ubuntu 20 0 55220 20m 13m S 6.0 1.0 0:03.80 mnemosyne
 8945 ubuntu 20 0 55220 20m 13m S 6.0 1.0 0:03.98 mnemosyne
ubuntu@ubuntu:~$

Looking at the sources, pygame.mixer.init always opens an SDL mixer stream. I think this is by design, rather than a bug, since games have something to play most of the time. Whereas Mnemosyne sits mostly idle (ok, does so for me ;), only occasionally playing a single sound.

I'm wondering if it could open the mixer just when needed and then shut it down again after playback is done? Or maybe late init the mixer on first playback and close it a minute after the last one?

Revision history for this message
Peter Bienstman (peter-bienstman) wrote : Re: [Bug 290691] Re: mnemosyne causes cpu usage in pulseaudio

But still, opening a mixer stream which is idle should not take all the CPU
time, so I'm inclined to think this is pygame regression, as with previous
pygame versions it worked fine.

I agree that it would be in principle possible to work around this in
Mnemosyne, but the upcoming 2.x codebase no longer uses pygame but phonon, so
I'm not really inclined to put a lot of effort in something that will become
obsolete anyhow...

Peter

On Thursday 30 October 2008 09:21:08 marmuta wrote:
> Hi, thanks for answering so quickly :)
>
> When I run it with alsa the cpu usage just shifts to mnemosyne (on a
> Intrepid RC live cd):
>
> ubuntu@ubuntu:~$ pulseaudio -k
> W: ltdl-bind-now.c: Failed to find original dlopen loader.
> ubuntu@ubuntu:~$ SDL_AUDIODRIVER=alsa mnemosyne&
> [1] 8945
> ubuntu@ubuntu:~$ top -bn3 |grep pulseaudio
> ubuntu@ubuntu:~$ top -bn3 |grep mnemosyne
> 8945 ubuntu 20 0 55220 20m 13m S 7.9 1.0 0:03.62 mnemosyne
> 8945 ubuntu 20 0 55220 20m 13m S 6.0 1.0 0:03.80 mnemosyne
> 8945 ubuntu 20 0 55220 20m 13m S 6.0 1.0 0:03.98 mnemosyne
> ubuntu@ubuntu:~$
>
> Looking at the sources, pygame.mixer.init always opens an SDL mixer
> stream. I think this is by design, rather than a bug, since games have
> something to play most of the time. Whereas Mnemosyne sits mostly idle
> (ok, does so for me ;), only occasionally playing a single sound.
>
> I'm wondering if it could open the mixer just when needed and then shut
> it down again after playback is done? Or maybe late init the mixer on
> first playback and close it a minute after the last one?

--
------------------------------------------------
Peter Bienstman
Ghent University, Dept. of Information Technology
Sint-Pietersnieuwstraat 41, B-9000 Gent, Belgium
tel: +32 9 264 34 46, fax: +32 9 264 35 93
WWW: http://photonics.intec.UGent.be
email: <email address hidden>
------------------------------------------------

Revision history for this message
marmuta (marmuta) wrote :

I agree that it shouldn't eat cpu when not explicitely asked to play. On the other hand, I could imagine this being a design choice for (game) performance reasons. The issue is probably low latency, no need to restart the sound hardware for each bullet.

I had a closer look at the pygame and sdl_mixer. The call sequence goes
pygame.mixer.init() -> Mix_OpenAudio -> SDL_OpenAudio and SDL_PauseAudio(0).
The stream will always start playing, there is no way around it and it has been like this throughout the history of pygame and sdl_mixer.

Here is the latest and very first version of pygame. see autoinit() in /src/mixer.c:
http://www.pygame.org/ftp/pygame-1.8.1release.tar.gz
http://www.pygame.org/ftp/old/pygame-0.1a.tar.gz

And the same for sdl_mixer, see Mix_OpenAudio():
http://www.libsdl.org/cgi/viewvc.cgi/trunk/SDL_mixer/mixer.c?view=markup
http://www.libsdl.org/cgi/viewvc.cgi/trunk/SDL_mixer/mixer.c?revision=2&view=markup

So, there has to be something else going on. Maybe it's a hardware thing. I believe some sound cards have hardware mixers and skip the software resampling. Could be my crappy onboard sound to blame. Or has Intrepids sound stack slowed down?

Whatever the reason for the cpu usage, there shouldn't be a silent audio stream around anyway.
Maybe mnemosyne really should take care of this. It would be super If that happened in 2.0 with phonon, but I'm also willing to contribute a patch for the current version.
Will there be a 2.0 release soon?

Revision history for this message
Peter Bienstman (peter-bienstman) wrote :

On Friday 31 October 2008 00:07:16 marmuta wrote:

> Whatever the reason for the cpu usage, there shouldn't be a silent audio
> stream around anyway. Maybe mnemosyne really should take care of this. It
> would be super If that happened in 2.0 with phonon, but I'm also willing to
> contribute a patch for the current version. Will there be a 2.0 release
> soon?

Not soonish, no. If you are willing to help out with a patch for the 1.x
codebase, that would be great!

Peter

Revision history for this message
marmuta (marmuta) wrote :

Done, here's the patch for mnemosyne 1.1-0ubuntu1.

Revision history for this message
Scott Kitterman (kitterman) wrote :

Based on a direct reply from upstream "It look OK to me. In fact, I'll also add it to the 1.x codebase.", I'm going to package this for SRU for Hardy and Intrepid.

Changed in mnemosyne:
assignee: nobody → kitterman
importance: Undecided → Medium
status: New → In Progress
Revision history for this message
Peter Bienstman (peter-bienstman) wrote :

Thanks a lot for the rapid patch! Feel free to apply it, I will also add it to
the 1.x repository.

Peter

Revision history for this message
Scott Kitterman (kitterman) wrote :

Fix uploaded for Jaunty. It'll be there once the archive is unfrozen.

Changed in mnemosyne:
assignee: nobody → kitterman
importance: Undecided → Medium
status: New → In Progress
status: In Progress → Fix Committed
Revision history for this message
Scott Kitterman (kitterman) wrote :

motu-sru ack'ed via IRC.

Revision history for this message
Martin Pitt (pitti) wrote :

Accepted into intrepid-proposed, please test and give feedback here. Please see https://wiki.ubuntu.com/Testing/EnableProposed for documentation how to enable and use -proposed. Thank you in advance!

Changed in mnemosyne:
status: In Progress → Fix Committed
Revision history for this message
marmuta (marmuta) wrote :

Tested on my beat-up Intrepid install and found no more cpu usage. Sound works when needed and there were no obvious regressions. So, it works there.

I tried to test on the current live cd, but the update had mysteriously vanished from intrepid-proposed. What happened? It's not in the repository anymore.

Revision history for this message
marmuta (marmuta) wrote :

Poof! it's back. Tested on the live cd and found this bug solved with no obvious regressions.

Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package mnemosyne - 1.1.1-0ubuntu1

---------------
mnemosyne (1.1.1-0ubuntu1) jaunty; urgency=low

  * New upstream release
    - Includes fix for (LP: #285793)
  * Change mnemosyne/pyqt_ui/sound.py to moderate CPU use when no sound is
    needed (LP: #290691)
    - In line since package from Debian lacks a patch system

 -- Scott Kitterman <email address hidden> Sun, 02 Nov 2008 23:47:02 -0500

Changed in mnemosyne:
status: Fix Committed → Fix Released
Revision history for this message
Peter Bienstman (peter-bienstman) wrote :

Included in the recently released 1.2 (which incidentally also fixes some other bugs)

Changed in mnemosyne-proj:
status: New → Fix Released
Revision history for this message
Scott Kitterman (kitterman) wrote :

Thanks for the hint. I just uploaded 1.2 to Ubuntu Jaunty (which will become
9.04).

Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package mnemosyne - 1.1-0ubuntu1.1

---------------
mnemosyne (1.1-0ubuntu1.1) intrepid-proposed; urgency=low

  * Change mnemosyne/pyqt_ui/sound.py so pulseaudio isn't activated when not
    needed (LP: #290691)
  * Fix import in mnemosyne/pyqt_ui/mnemosyne so it doesn't crash
    (LP: #285793)

 -- Scott Kitterman <email address hidden> Mon, 03 Nov 2008 00:16:20 -0500

Changed in mnemosyne:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.