From 17eb382b66ffbff36f987db334c01e2b6fcfd99f Mon Sep 17 00:00:00 2001 From: Nicolas Liaudat Date: Wed, 18 Jan 2023 22:57:11 +0100 Subject: [PATCH] Heap tracing (#1861) * 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 --- .../ClassFlowCNNGeneral.cpp | 21 +++++++++++- code/include/defines.h | 11 +++++++ code/main/main.cpp | 32 +++++++++++++++++++ code/platformio.ini | 4 +++ ...config.esp32cam-dev-task-analysis.defaults | 17 +++++++++- 5 files changed, 83 insertions(+), 2 deletions(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowCNNGeneral.cpp b/code/components/jomjol_flowcontroll/ClassFlowCNNGeneral.cpp index 07e68b08..f06c4fca 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowCNNGeneral.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowCNNGeneral.cpp @@ -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 + #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) @@ -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; @@ -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; } diff --git a/code/include/defines.h b/code/include/defines.h index 28211c44..c5a13d1d 100644 --- a/code/include/defines.h +++ b/code/include/defines.h @@ -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 diff --git a/code/main/main.cpp b/code/main/main.cpp index dedd89cf..5d945be0 100644 --- a/code/main/main.cpp +++ b/code/main/main.cpp @@ -62,6 +62,13 @@ #endif #endif +//#ifdef CONFIG_HEAP_TRACING_STANDALONE +#if defined HEAP_TRACING_MAIN_WIFI || defined HEAP_TRACING_MAIN_START + #include + #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; @@ -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 @@ -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); @@ -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 ================="); @@ -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; diff --git a/code/platformio.ini b/code/platformio.ini index 2a6356ae..ba172fe2 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -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 diff --git a/code/sdkconfig.esp32cam-dev-task-analysis.defaults b/code/sdkconfig.esp32cam-dev-task-analysis.defaults index 6fc0ed51..da1afdc4 100644 --- a/code/sdkconfig.esp32cam-dev-task-analysis.defaults +++ b/code/sdkconfig.esp32cam-dev-task-analysis.defaults @@ -1,3 +1,18 @@ CONFIG_FREERTOS_USE_TRACE_FACILITY=1 CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS=y -CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID=y \ No newline at end of file +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