Skip to content

Commit

Permalink
Heap tracing (#1861)
Browse files Browse the repository at this point in the history
* Update sdkconfig.esp32cam-dev-task-analysis.defaults

* Update defines.h

* Update platformio.ini

* Update main.cpp

* Update defines.h

* Update ClassFlowCNNGeneral.cpp

* Update platformio.ini
  • Loading branch information
nliaudat authored Jan 18, 2023
1 parent 0df54d1 commit 17eb382
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
21 changes: 20 additions & 1 deletion code/components/jomjol_flowcontroll/ClassFlowCNNGeneral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

static const char* TAG = "CNN";

//#define DEBUG_DETAIL_ON
//#ifdef CONFIG_HEAP_TRACING_STANDALONE
#ifdef HEAP_TRACING_CLASS_FLOW_CNN_GENERAL_DO_ALING_AND_CUT
#include <esp_heap_trace.h>
#define NUM_RECORDS 300
static heap_trace_record_t trace_record[NUM_RECORDS]; // This buffer must be in internal RAM
#endif


ClassFlowCNNGeneral::ClassFlowCNNGeneral(ClassFlowAlignment *_flowalign, t_CNNType _cnntype) : ClassFlowImage(NULL, TAG)
Expand Down Expand Up @@ -462,6 +467,14 @@ string ClassFlowCNNGeneral::getHTMLSingleStep(string host)

bool ClassFlowCNNGeneral::doFlow(string time)
{

#ifdef HEAP_TRACING_CLASS_FLOW_CNN_GENERAL_DO_ALING_AND_CUT
//register a buffer to record the memory trace
ESP_ERROR_CHECK( heap_trace_init_standalone(trace_record, NUM_RECORDS) );
// start tracing
ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) );
#endif

if (disabled)
return true;

Expand All @@ -474,6 +487,12 @@ bool ClassFlowCNNGeneral::doFlow(string time)
doNeuralNetwork(time);

RemoveOldLogs();

#ifdef HEAP_TRACING_CLASS_FLOW_CNN_GENERAL_DO_ALING_AND_CUT
ESP_ERROR_CHECK( heap_trace_stop() );
heap_trace_dump();
#endif

return true;
}

Expand Down
11 changes: 11 additions & 0 deletions code/include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@
// server_tflite.cpp
//#define TASK_ANALYSIS_ON


//Memory leak tracing
//https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/heap_debug.html#heap-information
//need CONFIG_HEAP_TRACING_STANDALONE=y or #define CONFIG_HEAP_TRACING_STANDALONE
//all setup is predifined in [env:esp32cam-dev-task-analysis]
//#define HEAP_TRACING_MAIN_WIFI || HEAP_TRACING_MAIN_START //enable heap tracing per function in main.cpp
//all defines in [env:esp32cam-dev-task-analysis]
//#define HEAP_TRACING_MAIN_WIFI
//#define HEAP_TRACING_MAIN_START
//#define HEAP_TRACING_CLASS_FLOW_CNN_GENERAL_DO_ALING_AND_CUT

/* Uncomment this to keep the logfile open for appending.
* If commented out, the logfile gets opened/closed for each log measage (old behaviour) */
// ClassLogFile
Expand Down
32 changes: 32 additions & 0 deletions code/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
#endif
#endif

//#ifdef CONFIG_HEAP_TRACING_STANDALONE
#if defined HEAP_TRACING_MAIN_WIFI || defined HEAP_TRACING_MAIN_START
#include <esp_heap_trace.h>
#define NUM_RECORDS 300
static heap_trace_record_t trace_record[NUM_RECORDS]; // This buffer must be in internal RAM
#endif

extern const char* GIT_TAG;
extern const char* GIT_REV;
extern const char* GIT_BRANCH;
Expand Down Expand Up @@ -162,6 +169,13 @@ void task_MainInitError_blink(void *pvParameter)

extern "C" void app_main(void)
{

//#ifdef CONFIG_HEAP_TRACING_STANDALONE
#if defined HEAP_TRACING_MAIN_WIFI || defined HEAP_TRACING_MAIN_START
//register a buffer to record the memory trace
ESP_ERROR_CHECK( heap_trace_init_standalone(trace_record, NUM_RECORDS) );
#endif

TickType_t xDelay;

#ifdef DISABLE_BROWNOUT_DETECTOR
Expand Down Expand Up @@ -224,6 +238,10 @@ extern "C" void app_main(void)
CheckStartAPMode(); // if no wlan.ini and/or config.ini --> AP ist startet and this function does not exit anymore until reboot
#endif

#ifdef HEAP_TRACING_MAIN_WIFI
ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) );
#endif

char *ssid = NULL, *passwd = NULL, *hostname = NULL, *ip = NULL, *gateway = NULL, *netmask = NULL, *dns = NULL; int rssithreashold = 0;
LoadWlanFromFile(WLAN_CONFIG_FILE, ssid, passwd, hostname, ip, gateway, netmask, dns, rssithreashold);

Expand Down Expand Up @@ -256,6 +274,15 @@ extern "C" void app_main(void)
xDelay = 2000 / portTICK_PERIOD_MS;
ESP_LOGD(TAG, "main: sleep for: %ldms", (long) xDelay);
vTaskDelay( xDelay );

#ifdef HEAP_TRACING_MAIN_WIFI
ESP_ERROR_CHECK( heap_trace_stop() );
heap_trace_dump();
#endif

#ifdef HEAP_TRACING_MAIN_START
ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) );
#endif

LogFile.WriteToFile(ESP_LOG_INFO, TAG, "=================================================");
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "================== Main Started =================");
Expand All @@ -268,6 +295,11 @@ extern "C" void app_main(void)

std::string zw = getCurrentTimeString("%Y%m%d-%H%M%S");
ESP_LOGD(TAG, "time %s", zw.c_str());

#ifdef HEAP_TRACING_MAIN_START
ESP_ERROR_CHECK( heap_trace_stop() );
heap_trace_dump();
#endif

/* Check if PSRAM can be initalized */
esp_err_t ret;
Expand Down
4 changes: 4 additions & 0 deletions code/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ extends = env:esp32cam-dev, esp32cam-debug
build_flags =
;-D DEBUG_DETAIL_ON ; if esp32cam-debug not in extends
-D TASK_ANALYSIS_ON
;please use only one HEAP tracing at time.
-D HEAP_TRACING_MAIN_WIFI
;-D HEAP_TRACING_MAIN_START
;-D HEAP_TRACING_CLASS_FLOW_CNN_GENERAL_DO_ALING_AND_CUT


; Overwrite espcam build_flags to not include ENABLE_SOFTAP
Expand Down
17 changes: 16 additions & 1 deletion code/sdkconfig.esp32cam-dev-task-analysis.defaults
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
CONFIG_FREERTOS_USE_TRACE_FACILITY=1
CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS=y
CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID=y
CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID=y

CONFIG_HEAP_TRACING_STANDALONE=y
CONFIG_HEAP_POISONING_LIGHT=y
CONFIG_HEAP_TASK_TRACKING=y


# General options for additional checks
CONFIG_HEAP_POISONING_COMPREHENSIVE=y
CONFIG_COMPILER_WARN_WRITE_STRINGS=y
CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y
CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK=y
CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y
CONFIG_COMPILER_STACK_CHECK=y

CONFIG_ESP_TASK_WDT=n

0 comments on commit 17eb382

Please sign in to comment.