Aaronwu

Memory Leak in Audio Path

Discussion created by Aaronwu Employee on Sep 7, 2011
Latest reply on Oct 12, 2011 by Aaronwu

I'm running 2010R1 kernel and distro (I  pulled the latest from git about 1500PDT yesterday) on a custom board  based on a BF527-EZKIT (v2) and I've noticed a memory leak somewhere in  the audio path. I'm using the BF527C's "internal" codec (SSM2602)  connected over TWI/I2S on SPORT0.

I first noticed the leak when running a customer application based on  Linphone/mediastream. Every time a handle to the soundcard was closed  after data was written, I was losing memory. If I never closed the  handle to the soundcard, I could write data all day long with no memory  loss. I could also open and close the handle to the sound card all day  long as long as I never wrote any data to it.

I wrote a simple script to play a WAV file via aplay and check the  memory after each play. You can see that I'm losing 132 bytes each time I  play the WAV file (which is 12,524 bytes)

This leak does not exist under 2009R1 with a BF537 and AD1980B codec.

Since  ALSA is the same version as it was in the 2009R1 distro  (1.0.18) and  the alsa-utils are the same, I'm presuming that the memory  leak lies  somewhere in the either the SPORT driver, the SSM2602  driver, the DMA driver or the TWI  driver.

Here's the script and its output:

 

root:/test> ls -al
drwxr-xr-x    2 root     root             0 Feb 28 01:03 .
drwxr-xr-x   15 root     root             0 Feb 28 01:03 ..
-rw-r--r--    1 root     root         12524 Feb 28 01:02 d.wav
-rwxr-xr-x    1 root     root            68 Feb 28 01:03 loop.sh
root:/test> cat loop.sh

#!/bin/sh

 

while true
do
         aplay d.wav
         sleep 1
         free
         sleep 1
done

 

root:/test> free
              total         used         free       shared      buffers
Mem:         61952        10416        51536            0            0
-/+ buffers:              10416        51536


root:/test> ./loop.sh
Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
              total         used         free       shared      buffers
Mem:         61952        10792        51160            0            0
-/+ buffers:              10792        51160
Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
              total         used         free       shared      buffers
Mem:         61952        10924        51028            0            0
-/+ buffers:              10924        51028
Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
              total         used         free       shared      buffers
Mem:         61952        11056        50896            0            0
-/+ buffers:              11056        50896
Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
              total         used         free       shared      buffers
Mem:         61952        11188        50764            0            0
-/+ buffers:              11188        50764
Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
              total         used         free       shared      buffers
Mem:         61952        11320        50632            0            0
-/+ buffers:              11320        50632
Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
              total         used         free       shared      buffers
Mem:         61952        11452        50500            0            0
-/+ buffers:              11452        50500

 


Fast forward five minutes

 


Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz,  Mono
              total         used         free       shared      buffers
Mem:         61952        28088        33864            0            0
-/+ buffers:              28088        33864


Fast forward another 9 minutes or so

 


Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz,  Mono
              total         used         free       shared      buffers
Mem:         61952        49468        12484            0            0
-/+ buffers:              49468        12484
Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
              total         used         free       shared      buffers
Mem:         61952        49600        12352            0            0
-/+ buffers:              49600        12352
Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
              total         used         free       shared      buffers
Mem:         61952        49732        12220            0            0
-/+ buffers:              49732        12220


Fast forward another 5 minutes or so

 


 

Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz,  Mono
              total         used         free       shared      buffers
Mem:         61952        56048         5904            0            0
-/+ buffers:              56048         5904
Playing WAVE 'd.wav' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono
snd_pcm_update_hw_ptr0: 6 callbacks suppressed
Allocation of length 36864 from process 1955 (aplay) failed
DMA per-cpu:
CPU    0: hi:    0, btch:   1 usd:   0
active_anon:0 inactive_anon:0 isolated_anon:0
  active_file:303 inactive_file:11154 isolated_file:0
  unevictable:1335 dirty:0 writeback:0 unstable:0
  free:1033 slab_reclaimable:226 slab_unreclaimable:321
  mapped:0 shmem:0 pagetables:0 bounce:0
DMA free:4132kB min:4096kB low:5120kB high:6144kB active_anon:0kB  inactive_anon:0kB active_file:1212kB inactive_file:44616kB  unevictable:5340kB isolated(anon):0kB isolated(file):0kB preso
lowmem_reserve[]: 0 0 0
DMA: 9*4kB 4*8kB 2*16kB 2*32kB 0*64kB 1*128kB 1*256kB 1*512kB 1*1024kB  1*2048kB 0*4096kB 0*8192kB 0*16384kB 0*32768kB = 4132kB
1407 total pagecache pages
ALSA lib  /home/cameron/src/blackfin/uclinux-dist/lib/alsa-lib/alsa-lib-1.0.18/src/pcm/pcm_mmap.c:422:(snd_pcm_mmap)  malloc failed: Cannot allocate memory
aplay: set_params:965: Unable to install hw params:
ACCESS:  RW_INTERLEAVED
FORMAT:  S16_LE
SUBFORMAT:  STD
SAMPLE_BITS: 16
FRAME_BITS: 16
CHANNELS: 1
RATE: 8000
PERIOD_TIME: (21333 21334)
PERIOD_SIZE: (170 171)
PERIOD_BYTES: (340 342)
PERIODS: (15 17)
BUFFER_TIME: 341250
BUFFER_SIZE: 2730
BUFFER_BYTES: 5460
TICK_TIME: 0
             total         used         free       shared      buffers
Mem:         61952        56184         5768            0            0
-/+ buffers:              56184         5768

Outcomes