From e345a43f375e4ed036d3a35f7bdd0199a9b5cd90 Mon Sep 17 00:00:00 2001 From: Seppo Ingalsuo Date: Wed, 1 Oct 2025 11:16:15 +0300 Subject: [PATCH 1/2] Tools: Testbench: Follow and print heap consumption This patch adds to memory dynamic allocation functions to sum number of bytes allocated with each allocation function. The change impacts only the library platform code that the sof-testbech4 uses. The sof-testbench4 run will print a summary of allocations sizes in simulation end. The possible freed memory or reallocation with smaller size is not taken into account. Also the SOF framework for test pipelines will impact the number. To know exactly the size of consumed heap, should subtract from reported numbers the sizes of minimal component test run such as template (plus the size of it's component data). Signed-off-by: Seppo Ingalsuo --- .../library/include/platform/lib/heap_usage.h | 21 ++++++++++++++++++ src/platform/library/lib/alloc.c | 15 +++++++++++++ tools/testbench/testbench.c | 22 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/platform/library/include/platform/lib/heap_usage.h diff --git a/src/platform/library/include/platform/lib/heap_usage.h b/src/platform/library/include/platform/lib/heap_usage.h new file mode 100644 index 000000000000..4e5e5ebf2629 --- /dev/null +++ b/src/platform/library/include/platform/lib/heap_usage.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2025 Intel Corporation. + */ + +#ifndef __SOF_LIB_HEAP_USAGE_H__ +#define __SOF_LIB_HEAP_USAGE_H__ + +#include +#include + +struct platform_library_heap_usage { + bool enable; + size_t rmalloc_size; + size_t rzalloc_size; + size_t rballoc_align_size; + size_t rbrealloc_align_size; +}; + +#endif /* __SOF_LIB_HEAP_USAGE_H__ */ + diff --git a/src/platform/library/lib/alloc.c b/src/platform/library/lib/alloc.c index ec7667b351be..0eaa08d5c602 100644 --- a/src/platform/library/lib/alloc.c +++ b/src/platform/library/lib/alloc.c @@ -13,16 +13,25 @@ #include #include #include +#include + +struct platform_library_heap_usage sof_platform_library_heap_usage = {0}; /* testbench mem alloc definition */ void *rmalloc(uint32_t flags, size_t bytes) { + if (sof_platform_library_heap_usage.enable) + sof_platform_library_heap_usage.rmalloc_size += bytes; + return malloc(bytes); } void *rzalloc(uint32_t flags, size_t bytes) { + if (sof_platform_library_heap_usage.enable) + sof_platform_library_heap_usage.rzalloc_size += bytes; + return calloc(bytes, 1); } @@ -34,12 +43,18 @@ void rfree(void *ptr) void *rballoc_align(uint32_t flags, size_t bytes, uint32_t alignment) { + if (sof_platform_library_heap_usage.enable) + sof_platform_library_heap_usage.rballoc_align_size += bytes; + return malloc(bytes); } void *rbrealloc_align(void *ptr, uint32_t flags, size_t bytes, size_t old_bytes, uint32_t alignment) { + if (sof_platform_library_heap_usage.enable && bytes > old_bytes) + sof_platform_library_heap_usage.rballoc_align_size += bytes; + return realloc(ptr, bytes); } diff --git a/tools/testbench/testbench.c b/tools/testbench/testbench.c index 45c5c8843494..525defe215d2 100644 --- a/tools/testbench/testbench.c +++ b/tools/testbench/testbench.c @@ -6,6 +6,7 @@ // Ranjani Sridharan #include +#include #include #include #include @@ -24,6 +25,8 @@ #define TESTBENCH_NCH 2 +extern struct platform_library_heap_usage sof_platform_library_heap_usage; + /* * Parse output filenames from user input * This function takes in the output filenames as an input in the format: @@ -299,6 +302,22 @@ static void test_pipeline_stats(struct testbench_prm *tp, long long delta_t) printf("Total execution time: %lld us, %.2f x realtime\n", delta_t, (float)frames_out / tp->fs_out * 1000000 / delta_t); + if (sof_platform_library_heap_usage.enable) { + printf("Size of rmalloc() allocations: %zu B\n", + sof_platform_library_heap_usage.rmalloc_size); + printf("Size of rzalloc() allocations: %zu B\n", + sof_platform_library_heap_usage.rzalloc_size); + printf("Size of rballoc_align() allocations: %zu B\n", + sof_platform_library_heap_usage.rballoc_align_size); + printf("Size of rbrealloc_align() allocations: %zu B\n", + sof_platform_library_heap_usage.rbrealloc_align_size); + printf("Total size of allocation from heap: %zu B\n", + sof_platform_library_heap_usage.rmalloc_size + + sof_platform_library_heap_usage.rzalloc_size + + sof_platform_library_heap_usage.rballoc_align_size + + sof_platform_library_heap_usage.rbrealloc_align_size); + } + printf("\n"); } @@ -333,6 +352,9 @@ static int pipline_test(struct testbench_prm *tp) break; } + /* Start follow heap usage */ + sof_platform_library_heap_usage.enable = true; + err = tb_set_up_all_pipelines(tp); if (err < 0) { fprintf(stderr, "error: pipelines set up %d failed %d\n", dp_count, err); From 5354ccc43953f1dd870cf7fb6c480eb8d962150b Mon Sep 17 00:00:00 2001 From: Seppo Ingalsuo Date: Wed, 1 Oct 2025 19:28:51 +0300 Subject: [PATCH 2/2] Tools: Testbench: Use calloc() instead of rzalloc() file file comp This change avoids the heap usage of file component to impact the reported heap consumption numbers. The allocations of file component do not need to be done via the SOF framework. Signed-off-by: Seppo Ingalsuo --- tools/testbench/file.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/testbench/file.c b/tools/testbench/file.c index 282dc509cce0..fb8ea3ff3883 100644 --- a/tools/testbench/file.c +++ b/tools/testbench/file.c @@ -534,7 +534,7 @@ static int file_init_set_dai_data(struct processing_module *mod) struct dai_data *dd; struct copier_data *ccd = module_get_private_data(mod); - dd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*dd)); + dd = calloc(1, sizeof(*dd)); if (!dd) return -ENOMEM; @@ -559,7 +559,7 @@ static int file_init_set_dai_data(struct processing_module *mod) struct dai_data *dd; struct comp_dev *dev = mod->dev; - dd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*dd)); + dd = calloc(1, sizeof(*dd)); if (!dd) return -ENOMEM; @@ -601,14 +601,14 @@ static int file_init(struct processing_module *mod) tb_debug_print("file_init()\n"); - ccd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*ccd)); + ccd = calloc(1, sizeof(*ccd)); if (!ccd) return -ENOMEM; mod_data->private = ccd; /* File component data is placed to copier's ipcgtw_data */ - cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*cd)); + cd = calloc(1, sizeof(*cd)); if (!cd) { free(ccd); return -ENOMEM;