Skip to content

Commit

Permalink
google_aec: Don't allocate giant blobs on the heap
Browse files Browse the repository at this point in the history
The operation of the AEC component uses a single buffer as an internal
heap.  This is very large, over half the available SRAM at component
creation time on MTL.  That's just a poor fit for the heap.  It would
be trivial to create a fragmentation scenario by creating/destroying
components (which happens under user control all the way out in Linux
userspace!) where AEC can't initialize and microphone input breaks.

Longer term we can look at moving this usage back to the heap by
integrating the component's internal allocations with the SOF/Zephyr
heap (which is quite performant), allowing it to make fine-grained
allocations which will work more robustly.

Signed-off-by: Andy Ross <andyross@google.com>
  • Loading branch information
andyross authored and cujomalainey committed Jun 21, 2024
1 parent 41a4892 commit 0eb34db
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/audio/google/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ config COMP_GOOGLE_RTC_AUDIO_REFERENCE_CHANNEL_MAX
retrieved at runtime, but channels higher than this number
are ignored

config COMP_GOOGLE_RTC_AUDIO_PROCESSING_MEMORY_BUFFER_SIZE_BYTES
config COMP_GOOGLE_RTC_AUDIO_PROCESSING_MEMORY_BUFFER_SIZE_KB
depends on COMP_GOOGLE_RTC_AUDIO_PROCESSING
int "Memory buffer size for Google Real Time Communication Audio processing"
default 200000
default 200
help
Sets the size of the memory buffer for the Google real-time
communication audio processing.
Expand Down
29 changes: 14 additions & 15 deletions src/audio/google/google_rtc_audio_processing.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ DECLARE_SOF_RT_UUID("google-rtc-audio-processing", google_rtc_audio_processing_u
DECLARE_TR_CTX(google_rtc_audio_processing_tr, SOF_UUID(google_rtc_audio_processing_uuid),
LOG_LEVEL_INFO);

#if !(defined(__ZEPHYR__) && defined(CONFIG_XTENSA))
/* Zephyr provides uncached memory for static variables on SMP, but we
* are single-core component and know we can safely use the cache for
* AEC work. XTOS SOF is cached by default, so stub the Zephyr API.
*/
#define arch_xtensa_cached_ptr(p) (p)
#endif

static __aligned(PLATFORM_DCACHE_ALIGN)
uint8_t aec_mem_blob[CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING_MEMORY_BUFFER_SIZE_KB * 1024];

struct google_rtc_audio_processing_comp_data {
#if CONFIG_IPC_MAJOR_4
struct sof_ipc4_aec_config config;
Expand Down Expand Up @@ -80,7 +91,6 @@ struct google_rtc_audio_processing_comp_data {
int num_capture_channels;
GoogleRtcAudioProcessingState *state;

uint8_t *memory_buffer;
struct comp_data_blob_handler *tuning_handler;
bool reconfigure;
int aec_reference_source;
Expand Down Expand Up @@ -413,18 +423,9 @@ static int google_rtc_audio_processing_init(struct processing_module *mod)
cd->num_frames = CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING_SAMPLE_RATE_HZ /
GOOGLE_RTC_AUDIO_PROCESSING_FREQENCY_TO_PERIOD_FRAMES;

if (CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING_MEMORY_BUFFER_SIZE_BYTES > 0) {
cd->memory_buffer = rballoc(0, SOF_MEM_CAPS_RAM,
CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING_MEMORY_BUFFER_SIZE_BYTES *
sizeof(cd->memory_buffer[0]));
if (!cd->memory_buffer) {
comp_err(dev, "google_rtc_audio_processing_init: failed to allocate memory buffer");
ret = -ENOMEM;
goto fail;
}

GoogleRtcAudioProcessingAttachMemoryBuffer(cd->memory_buffer, CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING_MEMORY_BUFFER_SIZE_BYTES);
}
/* Giant blob of scratch memory. */
GoogleRtcAudioProcessingAttachMemoryBuffer(arch_xtensa_cached_ptr(&aec_mem_blob[0]),
sizeof(aec_mem_blob));

cd->state = GoogleRtcAudioProcessingCreateWithConfig(CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING_SAMPLE_RATE_HZ,
cd->num_capture_channels,
Expand Down Expand Up @@ -535,7 +536,6 @@ static int google_rtc_audio_processing_init(struct processing_module *mod)
GoogleRtcAudioProcessingFree(cd->state);
}
GoogleRtcAudioProcessingDetachMemoryBuffer();
rfree(cd->memory_buffer);
#if CONFIG_IPC_MAJOR_4
rfree(cd->process_buffer);
#else
Expand All @@ -561,7 +561,6 @@ static int google_rtc_audio_processing_free(struct processing_module *mod)
#endif
rfree(cd->aec_reference_buffer);
GoogleRtcAudioProcessingDetachMemoryBuffer();
rfree(cd->memory_buffer);
#if CONFIG_IPC_MAJOR_4
rfree(cd->process_buffer);
#else
Expand Down

0 comments on commit 0eb34db

Please sign in to comment.