Skip to content

Commit ddd8ae1

Browse files
committed
vmh_tests: Add set of parametrized tests and test suit
Add enhanced vmh testing. Signed-off-by: Jakub Dabek <jakub.dabek@intel.com>
1 parent f79ae60 commit ddd8ae1

File tree

1 file changed

+262
-0
lines changed

1 file changed

+262
-0
lines changed

zephyr/test/vmh.c

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <errno.h>
99
#include <stdbool.h>
10+
#include <stdlib.h>
1011

1112
#include <adsp_memory_regions.h>
1213
#include <sof/boot_test.h>
@@ -17,8 +18,269 @@
1718

1819
LOG_MODULE_DECLARE(sof_boot_test, CONFIG_SOF_LOG_LEVEL);
1920

21+
/* Test creating and freeing a virtual memory heap */
22+
static void test_vmh_init_and_free_heap(int memory_region_attribute,
23+
struct vmh_heap_config *config,
24+
int core_id,
25+
bool allocating_continuously,
26+
bool expect_success)
27+
{
28+
struct vmh_heap *heap = vmh_init_heap(config, memory_region_attribute,
29+
core_id, allocating_continuously);
30+
if (expect_success) {
31+
zassert_not_null(heap,
32+
"Heap initialization expected to succeed but failed");
33+
}
34+
else
35+
zassert_is_null(heap, "Heap initialization expected to fail but succeeded");
36+
37+
if (heap) {
38+
int ret = vmh_free_heap(heap);
39+
40+
zassert_equal(ret, 0, "Failed to free heap");
41+
}
42+
}
43+
44+
/* Test for vmh_alloc and vmh_free */
45+
static void test_vmh_alloc_free_no_check(struct vmh_heap *heap,
46+
uint32_t alloc_size,
47+
bool expect_success)
48+
{
49+
void *ptr = vmh_alloc(heap, alloc_size);
50+
51+
if (expect_success)
52+
zassert_not_null(ptr, "Allocation expected to succeed but failed");
53+
else
54+
zassert_is_null(ptr, "Allocation expected to fail but succeeded");
55+
56+
if (ptr) {
57+
int ret = vmh_free(heap, ptr);
58+
59+
zassert_equal(ret, 0, "Failed to free allocated memory");
60+
}
61+
}
62+
63+
/* Test function for vmh_alloc and vmh_free with memory read/write */
64+
static void test_vmh_alloc_free_check(struct vmh_heap *heap,
65+
uint32_t alloc_size,
66+
bool expect_success)
67+
{
68+
void *ptr = vmh_alloc(heap, alloc_size);
69+
70+
if (expect_success)
71+
zassert_not_null(ptr, "Allocation expected to succeed but failed");
72+
else {
73+
zassert_is_null(ptr, "Allocation expected to fail but succeeded");
74+
return;
75+
}
76+
77+
if (ptr) {
78+
/* Write test pattern to the allocated memory and verify it */
79+
memset(ptr, 0xAA, alloc_size);
80+
81+
uint8_t start_val = *((uint8_t *)ptr);
82+
83+
zassert_equal(start_val, 0xAA,
84+
"Memory content verification failed at the start");
85+
86+
uint8_t end_val = *((uint8_t *)ptr + alloc_size - 1);
87+
88+
zassert_equal(end_val, 0xAA,
89+
"Memory content verification failed at the end");
90+
91+
if (alloc_size > 2) {
92+
uint32_t middle_pos = alloc_size / 2;
93+
uint8_t middle_val = *((uint8_t *)ptr + middle_pos);
94+
95+
zassert_equal(middle_val, 0xAA,
96+
"Memory content verification failed in the middle");
97+
}
98+
99+
int ret = vmh_free(heap, ptr);
100+
101+
zassert_equal(ret, 0, "Failed to free allocated memory");
102+
}
103+
}
104+
105+
/* Test function for multiple allocations on the same heap with read/write */
106+
static void test_vmh_multiple_allocs(struct vmh_heap *heap, int num_allocs,
107+
uint32_t min_alloc_size,
108+
uint32_t max_alloc_size)
109+
{
110+
void *ptrs[num_allocs];
111+
uint32_t alloc_size;
112+
bool success;
113+
int ret;
114+
115+
/* Perform multiple allocations */
116+
for (int i = 0; i < num_allocs; i++) {
117+
/* Generate a random allocation size between min_alloc_size and max_alloc_size */
118+
alloc_size = min_alloc_size +
119+
k_cycle_get_32() % (max_alloc_size - min_alloc_size + 1);
120+
121+
ptrs[i] = vmh_alloc(heap, alloc_size);
122+
123+
if (!ptrs[i])
124+
LOG_INF("Test allocation failed for size: %d", alloc_size);
125+
126+
zassert_true(ptrs[i] != NULL,
127+
"Allocation of size %u expected to succeed but failed",
128+
alloc_size);
129+
130+
if (ptrs[i] != NULL) {
131+
uint32_t test_value = 0xFFAADD11;
132+
133+
memset(ptrs[i], test_value, alloc_size);
134+
uint32_t *check_ptr = (uint32_t *)ptrs[i];
135+
136+
zassert_equal(check_ptr[j], test_value,
137+
"Memory content verification failed at position %u", j);
138+
}
139+
}
140+
141+
for (int i = 0; i < num_allocs; i++) {
142+
if (ptrs[i]) {
143+
ret = vmh_free(heap, ptrs[i]);
144+
zassert_equal(ret, 0, "Failed to free allocated memory");
145+
}
146+
}
147+
}
148+
149+
/* Test case for multiple allocations */
150+
static void test_vmh_alloc_multiple_times(bool allocating_continuously)
151+
{
152+
struct vmh_heap *heap =
153+
vmh_init_heap(NULL, MEM_REG_ATTR_CORE_HEAP, 0, allocating_continuously);
154+
155+
zassert_not_null(heap, "Heap initialization failed");
156+
157+
/* Test multiple allocations with small sizes */
158+
test_vmh_multiple_allocs(heap, 16, 4, 8);
159+
test_vmh_multiple_allocs(heap, 64, 4, 8);
160+
test_vmh_multiple_allocs(heap, 16, 4, 1024);
161+
test_vmh_multiple_allocs(heap, 64, 4, 1024);
162+
if (allocating_continuously) {
163+
test_vmh_multiple_allocs(heap, 16, 1024, 4096);
164+
test_vmh_multiple_allocs(heap, 16, 4096, 8192);
165+
test_vmh_multiple_allocs(heap, 64, 1024, 4096);
166+
test_vmh_multiple_allocs(heap, 64, 4096, 8192);
167+
}
168+
169+
/* Clean up the heap after testing */
170+
int ret = vmh_free_heap(heap);
171+
172+
zassert_equal(ret, 0, "Failed to free heap after multiple allocations");
173+
}
174+
175+
/* Test case for vmh_alloc and vmh_free */
176+
static void test_vmh_alloc_free(bool allocating_continuously)
177+
{
178+
struct vmh_heap *heap =
179+
vmh_init_heap(NULL, MEM_REG_ATTR_CORE_HEAP, 0, allocating_continuously);
180+
181+
zassert_not_null(heap, "Heap initialization failed");
182+
183+
test_vmh_alloc_free_no_check(heap, 512, true);
184+
test_vmh_alloc_free_no_check(heap, 1024, true);
185+
test_vmh_alloc_free_no_check(heap, sizeof(int), true);
186+
test_vmh_alloc_free_no_check(heap, 0, false);
187+
188+
test_vmh_alloc_free_check(heap, 512, true);
189+
test_vmh_alloc_free_check(heap, 1024, true);
190+
test_vmh_alloc_free_check(heap, sizeof(int), true);
191+
test_vmh_alloc_free_check(heap, 0, false);
192+
193+
int ret = vmh_free_heap(heap);
194+
195+
zassert_equal(ret, 0, "Failed to free heap");
196+
197+
/* Could add tests with configs for heaps*/
198+
}
199+
200+
/* Test case for vmh_alloc and vmh_free with and without config */
201+
static void test_heap_creation(void)
202+
{
203+
test_vmh_init_and_free_heap(MEM_REG_ATTR_CORE_HEAP, NULL, 0, false, true);
204+
205+
/* Try to setup with pre defined heap config */
206+
struct vmh_heap_config config = {0};
207+
208+
config.block_bundles_table[0].block_size = 8;
209+
210+
config.block_bundles_table[0].number_of_blocks = 1024;
211+
212+
config.block_bundles_table[1].block_size = 16;
213+
214+
config.block_bundles_table[1].number_of_blocks = 512;
215+
216+
test_vmh_init_and_free_heap(MEM_REG_ATTR_CORE_HEAP, &config, 0, false, true);
217+
}
218+
219+
/* Test case for alloc/free on configured heap */
220+
static void test_alloc_on_configured_heap(bool allocating_continuously)
221+
{
222+
223+
/* Try to setup with pre defined heap config */
224+
struct vmh_heap_config config = {0};
225+
226+
config.block_bundles_table[0].block_size = 32;
227+
228+
config.block_bundles_table[0].number_of_blocks = 256;
229+
230+
/* Create continuous allocation heap for success test */
231+
struct vmh_heap *heap =
232+
vmh_init_heap(NULL, MEM_REG_ATTR_CORE_HEAP, 0, allocating_continuously);
233+
234+
/* Will succeed on continuous and fail with single block alloc */
235+
test_vmh_alloc_free_check(heap, 512, allocating_continuously);
236+
237+
int ret = vmh_free_heap(heap);
238+
239+
zassert_equal(ret, 0, "Failed to free heap");
240+
}
241+
242+
/* Test cases for initializing heaps on all available regions */
243+
static void test_vmh_init_all_heaps(void)
244+
{
245+
int num_regions = CONFIG_MP_MAX_NUM_CPUS + VIRTUAL_REGION_COUNT;
246+
int i;
247+
const struct sys_mm_drv_region *virtual_memory_region =
248+
sys_mm_drv_query_memory_regions();
249+
250+
/* Test initializing all types of heaps */
251+
for (i = 0; i < num_regions; i++) {
252+
253+
/* Zeroed size symbolizes end of regions table */
254+
if (!virtual_memory_region[i].size)
255+
break;
256+
257+
struct vmh_heap *heap = vmh_init_heap(NULL, virtual_memory_region[i].attr,
258+
i, true);
259+
260+
zassert_not_null(heap, "Heap initialization expected to succeed but failed");
261+
262+
/* Test if it fails when heap already exists */
263+
test_vmh_init_and_free_heap(virtual_memory_region[i].attr, NULL, i, true,
264+
false);
265+
266+
if (heap) {
267+
int ret = vmh_free_heap(heap);
268+
269+
zassert_equal(ret, 0, "Failed to free heap");
270+
}
271+
}
272+
}
20273

21274
ZTEST(sof_boot, virtual_memory_heap)
22275
{
276+
test_heap_creation();
277+
test_vmh_init_all_heaps();
278+
test_alloc_on_configured_heap(true);
279+
test_alloc_on_configured_heap(false);
280+
test_vmh_alloc_free(true);
281+
test_vmh_alloc_free(false);
282+
test_vmh_alloc_multiple_times(true);
283+
test_vmh_alloc_multiple_times(false);
284+
23285
TEST_CHECK_RET(true, "virtual_memory_heap");
24286
}

0 commit comments

Comments
 (0)