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/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; 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);