Skip to content

Commit 649458c

Browse files
committed
audio_sal: support memory trace
Use mem_trace utility in media_lib_sal to trace memory usage
1 parent 912d7fb commit 649458c

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

components/audio_sal/audio_mem.c

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,31 @@
3636
#include "esp_efuse.h"
3737
#endif
3838

39-
// #define ENABLE_AUDIO_MEM_TRACE
39+
#ifdef CONFIG_MEDIA_LIB_MEM_AUTO_TRACE
40+
#define ENABLE_AUDIO_MEM_TRACE
41+
#define MALLOC_RAM_FLAG 1
42+
#endif
43+
/**
44+
* @brief Memory trace function is replaced by mem_trace module in `media_lib_sal`
45+
* Users need follow below steps to let trace function work
46+
* 1. After app start `#include "media_lib_adapter.h"` and call `media_lib_add_default_adapter();`
47+
* 2. Automatically enable memory trace function from menuconfig
48+
* `ADF Library Configuration --> Support trace memory automatically`
49+
* Or call `media_lib_start_mem_trace` manually
50+
* 3. Call `media_lib_stop_mem_trace` to stop trace and see trace results
51+
*
52+
* Use weak realization of trace function so that can work without `media_lib_sal`
53+
*/
54+
#ifdef ENABLE_AUDIO_MEM_TRACE
55+
int __attribute__((weak)) media_lib_add_trace_mem(const char *module, void *addr, int size, uint8_t flag)
56+
{
57+
return 0;
58+
}
59+
60+
void __attribute__((weak)) media_lib_remove_trace_mem(void *addr)
61+
{
62+
}
63+
#endif
4064

4165
void *audio_malloc(size_t size)
4266
{
@@ -47,17 +71,17 @@ void *audio_malloc(size_t size)
4771
data = malloc(size);
4872
#endif
4973
#ifdef ENABLE_AUDIO_MEM_TRACE
50-
ESP_LOGI("AUDIO_MEM", "malloc:%p, size:%d, called:0x%08x", data, size, (intptr_t)__builtin_return_address(0) - 2);
74+
media_lib_add_trace_mem(NULL, data, size, 0);
5175
#endif
5276
return data;
5377
}
5478

5579
void audio_free(void *ptr)
5680
{
57-
free(ptr);
5881
#ifdef ENABLE_AUDIO_MEM_TRACE
59-
ESP_LOGI("AUIDO_MEM", "free:%p, called:0x%08x", ptr, (intptr_t)__builtin_return_address(0) - 2);
82+
media_lib_remove_trace_mem(ptr);
6083
#endif
84+
free(ptr);
6185
}
6286

6387
void *audio_calloc(size_t nmemb, size_t size)
@@ -72,38 +96,43 @@ void *audio_calloc(size_t nmemb, size_t size)
7296
data = calloc(nmemb, size);
7397
#endif
7498
#ifdef ENABLE_AUDIO_MEM_TRACE
75-
ESP_LOGI("AUIDO_MEM", "calloc:%p, size:%d, called:0x%08x", data, size, (intptr_t)__builtin_return_address(0) - 2);
99+
media_lib_add_trace_mem(NULL, data, nmemb*size, 0);
76100
#endif
77101
return data;
78102
}
79103

80104
void *audio_realloc(void *ptr, size_t size)
81105
{
82106
void *p = NULL;
107+
#ifdef ENABLE_AUDIO_MEM_TRACE
108+
media_lib_remove_trace_mem(ptr);
109+
#endif
110+
83111
#if CONFIG_SPIRAM_BOOT_INIT
84112
p = heap_caps_realloc(ptr, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
85113
#else
86114
p = heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT);
87115
#endif
88116
#ifdef ENABLE_AUDIO_MEM_TRACE
89-
ESP_LOGI("AUDIO_MEM", "realloc,new:%p, ptr:%p size:%d, called:0x%08x", p, ptr, size, (intptr_t)__builtin_return_address(0) - 2);
117+
media_lib_add_trace_mem(NULL, p, size, 0);
90118
#endif
91119
return p;
92120
}
93121

94122
char *audio_strdup(const char *str)
95123
{
124+
int size = strlen(str) + 1;
96125
#if CONFIG_SPIRAM_BOOT_INIT
97-
char *copy = heap_caps_malloc(strlen(str) + 1, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
126+
char *copy = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
98127
#else
99-
char *copy = malloc(strlen(str) + 1);
128+
char *copy = malloc(size);
100129
#endif
101130
if (copy) {
102131
strcpy(copy, str);
103-
}
104132
#ifdef ENABLE_AUDIO_MEM_TRACE
105-
ESP_LOGI("AUDIO_MEM", "strdup:%p, size:%d, called:0x%08x", copy, strlen(copy), (intptr_t)__builtin_return_address(0) - 2);
133+
media_lib_add_trace_mem(NULL, copy, size, 0);
106134
#endif
135+
}
107136
return copy;
108137
}
109138

@@ -115,9 +144,8 @@ void *audio_calloc_inner(size_t n, size_t size)
115144
#else
116145
data = heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
117146
#endif
118-
119147
#ifdef ENABLE_AUDIO_MEM_TRACE
120-
ESP_LOGI("AUIDO_MEM", "calloc_inner:%p, size:%d, called:0x%08x", data, size, (intptr_t)__builtin_return_address(0) - 2);
148+
media_lib_add_trace_mem(NULL, data, n*size, MALLOC_RAM_FLAG);
121149
#endif
122150
return data;
123151
}
@@ -132,21 +160,19 @@ void audio_mem_print(const char *tag, int line, const char *func)
132160
#endif
133161
}
134162

135-
#if defined (CONFIG_SPIRAM_BOOT_INIT)
163+
136164
bool audio_mem_spiram_is_enabled(void)
137165
{
166+
#if defined (CONFIG_SPIRAM_BOOT_INIT)
138167
return true;
139-
}
140168
#else
141-
bool audio_mem_spiram_is_enabled(void)
142-
{
143169
return false;
144-
}
145170
#endif
171+
}
146172

147-
#if defined(CONFIG_SPIRAM_BOOT_INIT) && (CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY)
148173
bool audio_mem_spiram_stack_is_enabled(void)
149174
{
175+
#if defined(CONFIG_SPIRAM_BOOT_INIT) && (CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY)
150176
bool ret = true;
151177
#if defined(CONFIG_IDF_TARGET_ESP32)
152178
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 4)
@@ -158,12 +184,9 @@ bool audio_mem_spiram_stack_is_enabled(void)
158184
ESP_LOGW("AUIDO_MEM", "Can't support stack on external memory due to ESP32 chip is %d", (int)chip_ver);
159185
ret = false;
160186
}
161-
#endif
187+
#endif // defined(CONFIG_IDF_TARGET_ESP32)
162188
return ret;
163-
}
164-
#else
165-
bool audio_mem_spiram_stack_is_enabled(void)
166-
{
189+
#else // defined(CONFIG_SPIRAM_BOOT_INIT) ...
167190
return false;
168-
}
169191
#endif
192+
}

0 commit comments

Comments
 (0)