Merge branch 'feature/add_option_to_disable_heap_debug' into 'master'

heap: add option to enable/disable heap trace function

See merge request sdk/ESP8266_RTOS_SDK!1180
This commit is contained in:
Dong Heng
2020-02-20 10:30:35 +08:00
9 changed files with 170 additions and 5 deletions

View File

@ -1,10 +1,16 @@
menu "Heap memory"
config HEAP_DISABLE_IRAM
bool "Disable IRAM as heap memory"
default n
help
Disable IRAM as heap memory, and heap memory only use DRAM.
config HEAP_DISABLE_IRAM
bool "Disable IRAM as heap memory"
default n
help
Disable IRAM as heap memory, and heap memory only use DRAM.
config HEAP_TRACING
bool "Enables heap tracing API"
default n
help
Enables heap tracing API.
endmenu

View File

@ -51,6 +51,7 @@ typedef struct mem_blk {
/**
* Second type memory block.
*/
#ifdef CONFIG_HEAP_TRACING
typedef struct mem_blk2 {
struct mem_blk2 *prev; ///< Point to previous memory block
struct mem_blk2 *next; ///< Point to next memory block
@ -58,6 +59,9 @@ typedef struct mem_blk2 {
const char *file; ///< Which "file" allocate the memory block
size_t line; ///< Which "line" allocate the memory block
} mem2_blk_t;
#else
typedef mem_blk_t mem2_blk_t;
#endif
/**
* User region information.

View File

@ -14,12 +14,15 @@
#pragma once
#include "sdkconfig.h"
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef CONFIG_HEAP_TRACING
typedef enum {
HEAP_TRACE_NONE = 0,
@ -80,6 +83,8 @@ esp_err_t heap_trace_resume(void);
*/
void heap_trace_dump(void);
#endif /* CONFIG_HEAP_TRACING */
#ifdef __cplusplus
}
#endif

View File

@ -14,6 +14,7 @@
#pragma once
#include "sdkconfig.h"
#include <stdbool.h>
#ifdef __cplusplus
@ -21,7 +22,12 @@ extern "C" {
#endif
#define MEM_BLK_TAG 0x80000000 ///< Mark the memory block used
#ifdef CONFIG_HEAP_TRACING
#define MEM_BLK_TRACE 0x80000000 ///< Mark the memory block traced
#else
#define MEM_BLK_TRACE 0x00000000 ///< Mark the memory block traced
#endif
#define _mem_blk_get_ptr(_mem_blk, _offset, _mask) \
((mem_blk_t *)((((uint32_t *)(_mem_blk))[_offset]) & (~_mask)))
@ -47,12 +53,14 @@ static inline size_t mem_blk_head_size(bool trace)
static inline void mem_blk_set_traced(mem2_blk_t *mem_blk, const char *file, size_t line)
{
#ifdef CONFIG_HEAP_TRACING
uint32_t *val = (uint32_t *)mem_blk + 1;
*val |= MEM_BLK_TRACE;
mem_blk->file = file;
mem_blk->line = line | MEM_BLK_TRACE;
#endif
}
static inline void mem_blk_set_untraced(mem2_blk_t *mem_blk)
@ -154,10 +162,12 @@ static inline size_t ptr_size(void *ptr)
return size;
}
#ifdef CONFIG_HEAP_TRACING
static inline size_t mem2_blk_line(mem2_blk_t *mem2_blk)
{
return mem2_blk->line & ~MEM_BLK_TRACE;
}
#endif
#ifdef __cplusplus
}

View File

@ -32,7 +32,9 @@ extern heap_region_t g_heap_region[];
static const char *TAG = "heap_init";
size_t g_heap_region_num;
#ifdef CONFIG_HEAP_TRACING
int __g_heap_trace_mode = HEAP_TRACE_NONE;
#endif
/**
* @brief Initialize regions of memory to the collection of heaps at runtime.
@ -116,7 +118,9 @@ void IRAM_ATTR *_heap_caps_malloc(size_t size, uint32_t caps, const char *file,
_heap_caps_lock(num);
#ifdef CONFIG_HEAP_TRACING
trace = __g_heap_trace_mode == HEAP_TRACE_LEAKS;
#endif
mem_blk_size = ptr2memblk_size(size, trace);

View File

@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_HEAP_TRACING
#include <string.h>
#include "esp_heap_caps.h"
@ -146,3 +150,5 @@ void heap_trace_dump(void)
_heap_caps_unlock(num);
}
}
#endif /* CONFIG_HEAP_TRACING */

View File

@ -0,0 +1,6 @@
set(COMPONENT_SRCDIRS ".")
set(COMPONENT_ADD_INCLUDEDIRS ".")
set(COMPONENT_REQUIRES unity heap esp8266)
register_component()

View File

@ -0,0 +1,5 @@
#
#Component Makefile
#
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive

View File

@ -0,0 +1,119 @@
// Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD
//
// 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 <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/param.h>
#include <unity.h>
#include "esp_attr.h"
#include "esp8266/eagle_soc.h"
#include "esp_heap_caps.h"
#define MIXED_HEAP_LIST_NOTES 64
#define TIME_US_REG 0x3ff20c00
static inline uint32_t get_us(void)
{
return REG_READ(TIME_US_REG);
}
static void* IRAM_ATTR test_alloc_time_in_us(size_t n, uint32_t *time)
{
void *p;
uint32_t t;
t = get_us();
p = heap_caps_malloc(n, MALLOC_CAP_8BIT);
if (!p)
return NULL;
*time += get_us() - t;
return p;
}
static void IRAM_ATTR test_free_time_in_us(void *p, uint32_t *time)
{
uint32_t t;
t = get_us();
heap_caps_free(p);
*time += get_us() - t;
}
static void IRAM_ATTR test_heap_effectivity(uint32_t *time, uint32_t *count)
{
int cnt = 0;
uint32_t alloc_us = 0, free_us = 0;
for (; cnt < INT32_MAX; cnt++) {
void *p = test_alloc_time_in_us(cnt + sizeof(void *) + 1, &alloc_us);
if (!p)
break;
test_free_time_in_us(p, &free_us);
}
time[0] = alloc_us;
time[1] = free_us;
count[0] = cnt;
}
static void test_heap_init(uint32_t *pbuf)
{
for (int i = 0; i < MIXED_HEAP_LIST_NOTES; i++) {
pbuf[i] = (uint32_t)heap_caps_malloc(1, MALLOC_CAP_8BIT);
assert(pbuf[i]);
}
for (int i = 0; i < MIXED_HEAP_LIST_NOTES; i += 2) {
heap_caps_free((void *)pbuf[i]);
pbuf[i] = 0;
}
}
static void test_heap_deinit(uint32_t *pbuf)
{
for (int i = 0; i < MIXED_HEAP_LIST_NOTES; i++) {
if (pbuf[i]) {
heap_caps_free((void *)pbuf[i]);
pbuf[i] = 0;
}
}
}
TEST_CASE("Test Heap alloc/free effectivity", "[Heap]")
{
uint32_t time_in_us[2], count;
uint32_t buf[MIXED_HEAP_LIST_NOTES];
test_heap_init(buf);
test_heap_effectivity(time_in_us, &count);
printf("Each alloc costs time %u us, each free costs time %u us\n", time_in_us[0] / count, time_in_us[1] / count);
test_heap_deinit(buf);
}
// Enable Heap Trace : Each alloc costs time 27 us, each free costs time 5 us
// Disable Heap Trace : Each alloc costs time 18 us, each free costs time 4 us