-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryTracing.cpp
77 lines (69 loc) · 2.52 KB
/
MemoryTracing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* \brief Small utility for memory tracing
*
* \file MemoryTracing.cpp
* \author Laurent Louf
*/
#include "MemoryTracing.h"
#include "esp_heap_trace.h"
#include "esp_log.h"
#if defined(CONFIG_MEMORY_TRACING_ENABLE) && !defined(CONFIG_HEAP_TRACING)
#error "Heap tracing (CONFIG_HEAP_TRACING) not activated but mandatory for MemoryTracing module"
#endif
#ifdef CONFIG_MEMORY_TRACING_ENABLE_HEAP_TRACES_DUMP
static heap_trace_record_t
trace_record[CONFIG_MEMORY_TRACING_NUMBER_RECORDS]; // This buffer must be in internal RAM
#endif
static const char* TAG = "MemoryTracing";
static uint32_t get_ccount(void) {
uint32_t ccount = xthal_get_ccount() & ~3;
#ifndef CONFIG_FREERTOS_UNICORE
ccount |= xPortGetCoreID();
#endif
return ccount;
}
void main_memory_tracing(void* pvParameters) {
#ifdef CONFIG_MEMORY_TRACING_ENABLE
#ifdef CONFIG_MEMORY_TRACING_ENABLE_HEAP_TRACES_DUMP
// Initialize and launch memory tracing
ESP_ERROR_CHECK(heap_trace_init_standalone(trace_record, CONFIG_MEMORY_TRACING_NUMBER_RECORDS));
ESP_ERROR_CHECK(heap_trace_start(HEAP_TRACE_LEAKS));
#endif
uint16_t i_free_heap_display;
uint32_t ccount;
while (1) {
#ifdef CONFIG_MEMORY_TRACING_ENABLE_HEAP_TRACES_DUMP
for (i_free_heap_display = 0;
i_free_heap_display < CONFIG_MEMORY_TRACING_NB_FREE_HEAP_DISPLAY_BEFORE_TRACE_DUMP;
i_free_heap_display++) {
vTaskDelay(CONFIG_MEMORY_TRACING_INTERVAL_DISPLAY_FREE_HEAP_MS / portTICK_PERIOD_MS);
ccount = get_ccount();
ESP_LOGI(TAG, "Free heap size %d, ccount 0x%08x, core %d", esp_get_free_heap_size(),
ccount & ~3, ccount & 1);
}
heap_trace_dump();
#else
vTaskDelay(CONFIG_MEMORY_TRACING_INTERVAL_DISPLAY_FREE_HEAP_MS / portTICK_PERIOD_MS);
ccount = get_ccount();
ESP_LOGI(TAG, "Free heap size %d, ccount 0x%08x, core %d", esp_get_free_heap_size(),
ccount & ~3, ccount & 1);
#endif
}
#endif
}
bool start_memory_tracing(TaskHandle_t& o_task_handle) {
#ifdef CONFIG_MEMORY_TRACING_ENABLE
TaskHandle_t task_handle = NULL;
if (xTaskCreate(main_memory_tracing, "MEMORY_TRACING", CONFIG_MEMORY_TRACING_TASK_STACK_SIZE,
NULL, tskIDLE_PRIORITY + 2, &task_handle) == pdPASS) {
o_task_handle = task_handle;
return true;
} else {
o_task_handle = NULL;
return false;
}
#else
o_task_handle = NULL;
return false;
#endif
}