Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/platform/library/include/platform/lib/heap_usage.h
Original file line number Diff line number Diff line change
@@ -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 <stdbool.h>
#include <stddef.h>

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__ */

15 changes: 15 additions & 0 deletions src/platform/library/lib/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@
#include <malloc.h>
#include <rtos/alloc.h>
#include <sof/lib/mm_heap.h>
#include <platform/lib/heap_usage.h>

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

Expand All @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions tools/testbench/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions tools/testbench/testbench.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Ranjani Sridharan <ranjani.sridharan@linux.intel.com>

#include <sof/audio/module_adapter/module/generic.h>
#include <platform/lib/heap_usage.h>
#include <sof/ipc/driver.h>
#include <sof/ipc/topology.h>
#include <sof/list.h>
Expand All @@ -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:
Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -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);
Expand Down
Loading