Skip to content

Malloc heap info #2442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 23, 2016
Merged
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
207 changes: 207 additions & 0 deletions TESTS/mbed_drivers/stats/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*
* Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity/unity.h"
#include "utest/utest.h"
#include "mbed_stats.h"
#include <stdlib.h>
#include <stdio.h>

#if !defined(MBED_HEAP_STATS_ENABLED) || !MBED_HEAP_STATS_ENABLED || defined(__ICCARM__)
#error [NOT_SUPPORTED] test not supported
#endif

using namespace utest::v1;

#define ALLOCATION_SIZE_DEFAULT 564
#define ALLOCATION_SIZE_SMALL 124
#define ALLOCATION_SIZE_LARGE 790
#define ALLOCATION_SIZE_FAIL (1024 * 1024 *1024)

typedef void* (*malloc_cb_t) (uint32_t size);

static void* thunk_malloc(uint32_t size);
static void* thunk_calloc_1(uint32_t size);
static void* thunk_calloc_4(uint32_t size);
static void* thunk_realloc(uint32_t size);

malloc_cb_t malloc_thunk_array[] = {
thunk_malloc,
thunk_calloc_1,
thunk_calloc_4,
thunk_realloc,
};

void test_case_malloc_free_size()
{
printf("Initial print to setup stdio buffers\n");
mbed_stats_heap_t stats_start;
mbed_stats_heap_t stats_current;
void *data;

mbed_stats_heap_get(&stats_start);

for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) {

// Allocate memory and assert size change
data = malloc_thunk_array[i](ALLOCATION_SIZE_DEFAULT);
TEST_ASSERT(data != NULL);
mbed_stats_heap_get(&stats_current);
uint32_t increase = stats_current.current_size - stats_start.current_size;
TEST_ASSERT_EQUAL_UINT32(ALLOCATION_SIZE_DEFAULT, increase);
TEST_ASSERT_EQUAL_UINT32(stats_start.total_size + ALLOCATION_SIZE_DEFAULT * (i + 1), stats_current.total_size);
TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt + 1, stats_current.alloc_cnt);
TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);

// Free memory and assert back to starting size
free(data);
mbed_stats_heap_get(&stats_current);
TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt);
TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);
}
}

void test_case_allocate_zero()
{
mbed_stats_heap_t stats_start;
mbed_stats_heap_t stats_current;
void *data;

mbed_stats_heap_get(&stats_start);

for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) {

// Allocate memory and assert size change
data = malloc_thunk_array[i](0);
// Return can be NULL
mbed_stats_heap_get(&stats_current);
TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
TEST_ASSERT_EQUAL_UINT32(stats_start.total_size, stats_current.total_size);
TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);

// Free memory and assert back to starting size
free(data);
mbed_stats_heap_get(&stats_current);
TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt);
TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);
}
}

void test_case_allocate_fail()
{
mbed_stats_heap_t stats_start;
mbed_stats_heap_t stats_current;
void *data;

mbed_stats_heap_get(&stats_start);

for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) {

// Trigger a failure by trying to allocate a buffer that won't fit
data = malloc_thunk_array[i](ALLOCATION_SIZE_FAIL);
TEST_ASSERT(data == NULL);
mbed_stats_heap_get(&stats_current);
TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
TEST_ASSERT_EQUAL_UINT32(stats_start.total_size, stats_current.total_size);
TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt);
TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt + i + 1, stats_current.alloc_fail_cnt);
}
}

static void* thunk_malloc(uint32_t size)
{
printf("Malloc thunk\n");
return malloc(size);
}

static void* thunk_calloc_1(uint32_t size)
{
printf("Calloc thunk 1 byte\n");
return calloc(size / 1, 1);
}

static void* thunk_calloc_4(uint32_t size)
{
printf("Calloc thunk 4 bytes\n");
return calloc(size / 4, 4);
}


static void* thunk_realloc(uint32_t size)
{
printf("Realloc thunk\n");
return realloc(NULL, size);
}

void test_case_realloc_size()
{
mbed_stats_heap_t stats_start;
mbed_stats_heap_t stats_current;
uint32_t increase;
void *data;

mbed_stats_heap_get(&stats_start);

// Allocate memory and assert size change
data = realloc(NULL, ALLOCATION_SIZE_DEFAULT);
TEST_ASSERT(data != NULL);
mbed_stats_heap_get(&stats_current);
increase = stats_current.current_size - stats_start.current_size;
TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_DEFAULT);

// Decrease size and assert size change
data = realloc(data, ALLOCATION_SIZE_SMALL);
TEST_ASSERT(data != NULL);
mbed_stats_heap_get(&stats_current);
increase = stats_current.current_size - stats_start.current_size;
TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_SMALL);

// Increase size and assert size change
data = realloc(data, ALLOCATION_SIZE_LARGE);
TEST_ASSERT(data != NULL);
mbed_stats_heap_get(&stats_current);
increase = stats_current.current_size - stats_start.current_size;
TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_LARGE);

// Free memory and assert back to starting size
free(data);
mbed_stats_heap_get(&stats_current);
TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
}

Case cases[] = {
Case("malloc and free size", test_case_malloc_free_size),
Case("allocate size zero", test_case_allocate_zero),
Case("allocation failure", test_case_allocate_fail),
Case("realloc size", test_case_realloc_size),
};

utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(20, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}

Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);

int main()
{
Harness::run(specification);
}
4 changes: 4 additions & 0 deletions features/frameworks/utest/source/utest_greentea_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "greentea-client/test_env.h"
#include "utest/utest_stack_trace.h"
#include "utest/utest_serial.h"
#include "mbed_stats.h"

using namespace utest::v1;

Expand Down Expand Up @@ -105,7 +106,10 @@ utest::v1::status_t utest::v1::greentea_test_setup_handler(const size_t number_o
void utest::v1::greentea_test_teardown_handler(const size_t passed, const size_t failed, const failure_t failure)
{
UTEST_LOG_FUNCTION();
mbed_stats_heap_t heap_stats;
verbose_test_teardown_handler(passed, failed, failure);
mbed_stats_heap_get(&heap_stats);
greentea_send_kv("max_heap_usage",heap_stats.max_size);
greentea_send_kv(TEST_ENV_TESTCASE_SUMMARY, passed, failed);
int result = !(failed || (failure.reason && !(failure.reason & REASON_IGNORE)));
GREENTEA_TESTSUITE_RESULT(result);
Expand Down
5 changes: 3 additions & 2 deletions features/frameworks/utest/source/utest_harness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,12 @@ void Harness::raise_failure(const failure_reason_t reason)
if (test_cases == NULL) return;

utest::v1::status_t fail_status = STATUS_ABORT;
if (handlers.test_failure) handlers.test_failure(failure_t(reason, location));
if (handlers.case_failure) fail_status = handlers.case_failure(case_current, failure_t(reason, location));

{
UTEST_ENTER_CRITICAL_SECTION;

if (handlers.test_failure) handlers.test_failure(failure_t(reason, location));
if (handlers.case_failure) fail_status = handlers.case_failure(case_current, failure_t(reason, location));
if (fail_status != STATUS_IGNORE) case_failed++;

if ((fail_status == STATUS_ABORT) && case_timeout_handle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,11 @@ int32_t cfstore_test_delete_all(void)
CFSTORE_ERRLOG("%s:Error: failed to delete key_name=%s, len=%d\r\n", __func__, key_name, (int) len);
return ret;
}
CFSTORE_HANDLE_SWAP(prev, next);
ret = drv->Close(next);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this change related to this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @bogdanm, this change is required to pass testing. The changes I made in this PR caused a config store issue to surface. This is a workaround for that issue.

if(ret < ARM_DRIVER_OK){
CFSTORE_ERRLOG("%s:Error: failed to close key_name=%s, len=%d\r\n", __func__, key_name, (int) len);
return ret;
}
}
if(ret == ARM_CFSTORE_DRIVER_ERROR_KEY_NOT_FOUND) {
/* as expected, no more keys have been found by the Find()*/
Expand Down
40 changes: 40 additions & 0 deletions hal/api/mbed_stats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* mbed Microcontroller Library
* Copyright (c) 2016-2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_STATS_H
#define MBED_STATS_H

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
uint32_t current_size; /**< Bytes allocated currently. */
uint32_t max_size; /**< Max bytes allocated at a given time. */
uint32_t total_size; /**< Cumulative sum of bytes ever allocated. */
uint32_t alloc_cnt; /**< Current number of allocations. */
uint32_t alloc_fail_cnt; /**< Number of failed allocations. */
} mbed_stats_heap_t;

/**
* Fill the passed in structure with heap stats.
*/
void mbed_stats_heap_get(mbed_stats_heap_t *stats);

#ifdef __cplusplus
}
#endif

#endif
Loading