Skip to content

Commit 87e1b37

Browse files
committed
CFSTORE - Fix test failures due to fragmentation
In the config store create test in test case #5 the amount of available memory is determined by fully allocating the heap. This is done multiple times to determine if there is a memory leak. This causes problems when even slight fragmentation occurs in the heap, since the size that can be allocated is decreased slightly, which the test flags as a memory leak. This patch makes memory leak detection more robust by using metrics provided by mbed_stats_heap_get. These metrics are an exact measurement of memory allocated is not changed by fragmentation. This allows the memory leak test to report correct values regardless of fragmentation.
1 parent a37ccd1 commit 87e1b37

File tree

1 file changed

+14
-15
lines changed
  • features/storage/FEATURE_STORAGE/TESTS/cfstore/create

1 file changed

+14
-15
lines changed

features/storage/FEATURE_STORAGE/TESTS/cfstore/create/create.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "mbed.h"
28+
#include "mbed_stats.h"
2829
#include "cfstore_config.h"
2930
#include "cfstore_debug.h"
3031
#include "cfstore_test.h"
@@ -507,11 +508,10 @@ control_t cfstore_create_test_04_end(const size_t call_count)
507508
*
508509
* Create enough KV's to consume the whole of available memory
509510
*/
510-
int32_t cfstore_create_test_05_core(const size_t call_count, uint32_t* bytes_stored_ex)
511+
int32_t cfstore_create_test_05_core(const size_t call_count)
511512
{
512513
int32_t ret = ARM_DRIVER_ERROR;
513514
uint32_t i = 0;
514-
uint32_t bytes_stored = 0;
515515
const uint32_t max_num_kvs_create = 200;
516516
const size_t kv_name_tag_len = 3;
517517
const size_t kv_name_min_len = 10;
@@ -535,9 +535,6 @@ int32_t cfstore_create_test_05_core(const size_t call_count, uint32_t* bytes_sto
535535
memset(value_buf, 0, max_value_buf_size);
536536
snprintf(kv_name_tag_buf, kv_name_tag_len+1, "%0d", (int) i);
537537
ret = cfstore_create_kv_create(kv_name_min_len, kv_name_tag_buf, value_buf, kv_value_min_len/64 * (i+1));
538-
bytes_stored += kv_name_min_len + i + strlen(kv_name_tag_buf); /* kv_name */
539-
bytes_stored += kv_value_min_len/64 * (i+1); /* kv value blob */
540-
bytes_stored += 8; /* kv overhead */
541538
if(ret == ARM_CFSTORE_DRIVER_ERROR_OUT_OF_MEMORY){
542539
CFSTORE_ERRLOG("Out of memory on %d-th KV, trying to allocate memory totalling %d.\n", (int) i, (int) bytes_stored);
543540
break;
@@ -551,9 +548,6 @@ int32_t cfstore_create_test_05_core(const size_t call_count, uint32_t* bytes_sto
551548
free(value_buf);
552549
CFSTORE_TEST_UTEST_MESSAGE(cfstore_create_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: Uninitialize() call failed.\n", __func__);
553550
TEST_ASSERT_MESSAGE(drv->Uninitialize() >= ARM_DRIVER_OK, cfstore_create_utest_msg_g);
554-
if(bytes_stored_ex){
555-
*bytes_stored_ex = bytes_stored;
556-
}
557551
return ret;
558552
}
559553

@@ -576,22 +570,25 @@ control_t cfstore_create_test_05(const size_t call_count)
576570
{
577571
uint32_t i = 0;
578572
int32_t ret = ARM_DRIVER_ERROR;
579-
uint32_t bytes_stored = 0;
580-
uint32_t bytes_stored_prev = 0;
581573
const uint32_t max_loops = 50;
574+
mbed_stats_heap_t stats_before;
575+
mbed_stats_heap_t stats_after;
576+
577+
mbed_stats_heap_get(&stats_before);
582578

583579
CFSTORE_FENTRYLOG("%s:entered\n", __func__);
584580
for(i = 0; i < max_loops; i++) {
585-
ret = cfstore_create_test_05_core(call_count, &bytes_stored);
581+
ret = cfstore_create_test_05_core(call_count);
586582
CFSTORE_TEST_UTEST_MESSAGE(cfstore_create_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: cfstore_create_test_05_core() failed (ret = %d.\n", __func__, (int) ret);
587583
TEST_ASSERT_MESSAGE(ret >= ARM_DRIVER_OK, cfstore_create_utest_msg_g);
588584

585+
mbed_stats_heap_get(&stats_after);
589586
if(i > 1) {
590-
CFSTORE_TEST_UTEST_MESSAGE(cfstore_create_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: memory leak: stored %d bytes on loop %d, but %d bytes on loop %d .\n", __func__, (int) bytes_stored, (int) i, (int) bytes_stored_prev, (int) i-1);
591-
TEST_ASSERT_MESSAGE(bytes_stored == bytes_stored_prev, cfstore_create_utest_msg_g);
592-
587+
CFSTORE_TEST_UTEST_MESSAGE(cfstore_create_utest_msg_g, CFSTORE_UTEST_MSG_BUF_SIZE, "%s:Error: memory leak: stored %d bytes on loop %d, but %d bytes on loop %d .\n", __func__, (int) stats_after.current_size, (int) i, (int) stats_before.current_size, (int) i-1);
588+
TEST_ASSERT_MESSAGE(stats_after.current_size == stats_before.current_size, cfstore_create_utest_msg_g);
589+
TEST_ASSERT(stats_after.alloc_fail_cnt > stats_before.alloc_fail_cnt);
593590
}
594-
bytes_stored_prev = bytes_stored;
591+
stats_before = stats_after;
595592
}
596593
return CaseNext;
597594
}
@@ -828,7 +825,9 @@ Case cases[] = {
828825
Case("CREATE_test_03_end", cfstore_create_test_03_end),
829826
Case("CREATE_test_04_start", cfstore_utest_default_start),
830827
Case("CREATE_test_04_end", cfstore_create_test_04_end),
828+
#if defined(MBED_HEAP_STATS_ENABLED) && MBED_HEAP_STATS_ENABLED && !defined(__ICCARM__)
831829
Case("CREATE_test_05", cfstore_create_test_05),
830+
#endif
832831
Case("CREATE_test_06_start", cfstore_utest_default_start),
833832
Case("CREATE_test_06_end", cfstore_create_test_06_end),
834833
Case("CREATE_test_07_start", cfstore_utest_default_start),

0 commit comments

Comments
 (0)