tests: benchmarks: sys_kernel: add k_malloc() test

Useful to evaluate malloc performance changes.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
This commit is contained in:
Nicolas Pitre 2025-03-14 16:27:06 -04:00 committed by Benjamin Cabé
commit ea7a969204
4 changed files with 69 additions and 2 deletions

View file

@ -15,3 +15,9 @@ CONFIG_HW_STACK_PROTECTION=n
# Can only run under 1 CPU
CONFIG_MP_MAX_NUM_CPUS=1
# for the k_malloc() test
CONFIG_KERNEL_MEM_POOL=y
CONFIG_HEAP_MEM_POOL_SIZE=8192
CONFIG_SYS_HEAP_VALIDATE=n
CONFIG_SYS_HEAP_RUNTIME_STATS=n

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2025 BayLibre SAS
* *
* SPDX-License-Identifier: Apache-2.0
*/
#include "syskernel.h"
static void *malloc_array[NUMBER_OF_LOOPS];
static void do_malloc(int loops)
{
int i;
for (i = 0; i < loops; i++) {
malloc_array[i] = k_malloc(1);
}
}
static void do_free(int loops)
{
int i;
for (i = 0; i < loops; i++) {
k_free(malloc_array[i]);
}
}
int malloc_test(void)
{
uint32_t t;
int return_value = 0;
fprintf(output_file, sz_test_case_fmt,
"malloc #1");
fprintf(output_file, sz_description,
"\n\tk_malloc");
printf(sz_test_start_fmt);
t = BENCH_START();
do_malloc(number_of_loops);
t = TIME_STAMP_DELTA_GET(t);
return_value += check_result(number_of_loops, t);
fprintf(output_file, sz_test_case_fmt,
"malloc #2");
fprintf(output_file, sz_description,
"\n\tk_free");
printf(sz_test_start_fmt);
t = BENCH_START();
do_free(number_of_loops);
t = TIME_STAMP_DELTA_GET(t);
return_value += check_result(number_of_loops, t);
return return_value;
}

View file

@ -161,11 +161,12 @@ int main(void)
test_result += lifo_test();
test_result += fifo_test();
test_result += stack_test();
test_result += malloc_test();
test_result += mem_slab_test();
if (test_result) {
/* sema/lifo/fifo/stack/mem_slab account for 14 tests in total */
if (test_result == 14) {
/* sema/lifo/fifo/stack/malloc/mem_slab account for 16 tests in total */
if (test_result == 16) {
fprintf(output_file, sz_module_result_fmt,
sz_success);
} else {

View file

@ -56,6 +56,7 @@ int sema_test(void);
int lifo_test(void);
int fifo_test(void);
int stack_test(void);
int malloc_test(void);
int mem_slab_test(void);
void begin_test(void);