From 4fd2b4cd0f843b95a7bae8b0063f31798c266786 Mon Sep 17 00:00:00 2001 From: Frank Date: Tue, 12 Dec 2023 00:43:48 +0100 Subject: [PATCH] found a way to increase TCP stacksize (for AudioReactive UI) we can use `-D CONFIG_ASYNC_TCP_TASK_STACK_SIZE=xxxx` to increase the stack size of AsyncWebserver - default is 8196. This should give us a bit of headroom for new UI items. We still need PR #94 for more savings. --- platformio.ini | 6 ++++++ usermods/audioreactive/audio_reactive.h | 11 +++++++++++ wled00/const.h | 6 +++++- wled00/data/index.js | 2 +- wled00/wled.cpp | 22 ++++++++++++++++++++-- wled00/wled.h | 2 +- wled00/wled_server.cpp | 4 ++-- 7 files changed, 46 insertions(+), 7 deletions(-) diff --git a/platformio.ini b/platformio.ini index 15de61d313..ef73c2cdb9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -294,6 +294,7 @@ build_flags = -g -DARDUINO_ARCH_ESP32 #-DCONFIG_LITTLEFS_FOR_IDF_3_2 -D CONFIG_ASYNC_TCP_USE_WDT=0 + -D CONFIG_ASYNC_TCP_TASK_STACK_SIZE=9472 ;; WLEDMM increase stack by 1.25Kb, as audioreactive needs bigger SETTINGS_STACK_BUF_SIZE #use LITTLEFS library by lorol in ESP32 core 1.x.x instead of built-in in 2.x.x -D LOROL_LITTLEFS ; -DARDUINO_USB_CDC_ON_BOOT=0 ;; this flag is mandatory for "classic ESP32" when building with arduino-esp32 >=2.0.3 @@ -334,6 +335,7 @@ build_flagsV4 = -g -DARDUINO_ARCH_ESP32 -DESP32 -DCONFIG_LITTLEFS_FOR_IDF_3_2 -DLFS_THREADSAFE -D CONFIG_ASYNC_TCP_USE_WDT=0 + -D CONFIG_ASYNC_TCP_TASK_STACK_SIZE=9472 ;; WLEDMM increase stack by 1.25Kb, as audioreactive needs bigger SETTINGS_STACK_BUF_SIZE ; -DARDUINO_USB_CDC_ON_BOOT=0 ;; mandatory for "classic ESP32" when builing with arduino-esp32 >=2.0.3 ;;; V4.4.x libraries (without LOROL_LITTLEFS; with newer NeoPixelBus) lib_depsV4 = @@ -357,6 +359,7 @@ build_flags = -g -DARDUINO_ARCH_ESP32 -DESP32 #-DCONFIG_LITTLEFS_FOR_IDF_3_2 -D CONFIG_ASYNC_TCP_USE_WDT=0 + -D CONFIG_ASYNC_TCP_TASK_STACK_SIZE=9472 ;; WLEDMM increase stack by 1.25Kb, as audioreactive needs bigger SETTINGS_STACK_BUF_SIZE -DARDUINO_USB_CDC_ON_BOOT=0 ;; this flag is mandatory for "classic ESP32" when building with arduino-esp32 >=2.0.3 default_partitions = tools/WLED_ESP32_4MB_1MB_FS.csv lib_deps = @@ -375,6 +378,7 @@ build_flags = -g -DCONFIG_IDF_TARGET_ESP32S2=1 -DCONFIG_LITTLEFS_FOR_IDF_3_2 -DLFS_THREADSAFE ;; WLEDMM -D CONFIG_ASYNC_TCP_USE_WDT=0 + -D CONFIG_ASYNC_TCP_TASK_STACK_SIZE=9216 ;; WLEDMM increase stack by 1Kb, as audioreactive needs bigger SETTINGS_STACK_BUF_SIZE -DARDUINO_USB_MSC_ON_BOOT=0 -DARDUINO_USB_DFU_ON_BOOT=0 -DCO -DARDUINO_USB_MODE=0 ;; this flag is mandatory for ESP32-S2 ! @@ -397,6 +401,7 @@ build_flags = -g -DCONFIG_IDF_TARGET_ESP32C3=1 -DCONFIG_LITTLEFS_FOR_IDF_3_2 -DLFS_THREADSAFE ;; WLEDMM -D CONFIG_ASYNC_TCP_USE_WDT=0 + -D CONFIG_ASYNC_TCP_TASK_STACK_SIZE=9472 ;; WLEDMM increase stack by 1.25Kb, as audioreactive needs bigger SETTINGS_STACK_BUF_SIZE -DCO -DARDUINO_USB_MODE=1 ;; this flag is mandatory for ESP32-C3 ;; please make sure that the following flags are properly set (to 0 or 1) by your board.json, or included in your custom platformio_override.ini entry: @@ -419,6 +424,7 @@ build_flags = -g -DCONFIG_IDF_TARGET_ESP32S3=1 -DCONFIG_LITTLEFS_FOR_IDF_3_2 -DLFS_THREADSAFE ;; WLEDMM -D CONFIG_ASYNC_TCP_USE_WDT=0 + -D CONFIG_ASYNC_TCP_TASK_STACK_SIZE=9472 ;; WLEDMM increase stack by 1.25Kb, as audioreactive needs bigger SETTINGS_STACK_BUF_SIZE -DARDUINO_USB_MSC_ON_BOOT=0 -DARDUINO_DFU_ON_BOOT=0 -DCO ;; please make sure that the following flags are properly set (to 0 or 1) by your board.json, or included in your custom platformio_override.ini entry: diff --git a/usermods/audioreactive/audio_reactive.h b/usermods/audioreactive/audio_reactive.h index e4bf2a76c2..c97ccb0ff2 100644 --- a/usermods/audioreactive/audio_reactive.h +++ b/usermods/audioreactive/audio_reactive.h @@ -91,6 +91,17 @@ #define PLOT_FLUSH() #endif +// sanity checks +#ifdef ARDUINO_ARCH_ESP32 + // we need more space in for oappend() stack buffer -> SETTINGS_STACK_BUF_SIZE and CONFIG_ASYNC_TCP_TASK_STACK_SIZE + #if SETTINGS_STACK_BUF_SIZE < 3904 // 3904 is required for WLEDMM-0.14.0-b28 + #warning please increase SETTINGS_STACK_BUF_SIZE >= 3904 + #endif + #if (CONFIG_ASYNC_TCP_TASK_STACK_SIZE - SETTINGS_STACK_BUF_SIZE) < 4352 // at least 4096+256 words of free task stack is needed by async_tcp alone + #error remaining async_tcp stack will be too low - please increase CONFIG_ASYNC_TCP_TASK_STACK_SIZE + #endif +#endif + // audiosync constants #define AUDIOSYNC_NONE 0x00 // UDP sound sync off #define AUDIOSYNC_SEND 0x01 // UDP sound sync - send mode diff --git a/wled00/const.h b/wled00/const.h index b06c4aa84a..a4e0b23039 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -382,7 +382,11 @@ #ifdef ESP8266 #define SETTINGS_STACK_BUF_SIZE 2048 #else -#define SETTINGS_STACK_BUF_SIZE 3834 // WLEDMM added 696+32 bytes of margin (was 3096) for audioreactive UI + #if !defined(USERMOD_AUDIOREACTIVE) + #define SETTINGS_STACK_BUF_SIZE 3834 // WLEDMM added 696+32 bytes of margin (was 3096) + #else + #define SETTINGS_STACK_BUF_SIZE 3904 // WLEDMM more buffer for audioreactive UI (add '-D CONFIG_ASYNC_TCP_TASK_STACK_SIZE=9216' to your build_flags) + #endif #endif #ifdef WLED_USE_ETHERNET diff --git a/wled00/data/index.js b/wled00/data/index.js index 8ecf2369be..d4109554fb 100644 --- a/wled00/data/index.js +++ b/wled00/data/index.js @@ -697,7 +697,7 @@ ${inforow("Filesystem",i.fs.u + "/" + i.fs.t + " kB (" +Math.round(i.fs.u*100/i. ${theap>0?inforow("Heap ☾",((i.totalheap-i.freeheap)/1000).toFixed(0)+"/"+theap.toFixed(0)+" kB"," ("+Math.round((i.totalheap-i.freeheap)/(10*theap))+"%)"):""} ${i.minfreeheap?inforow("Max used heap ☾",((i.totalheap-i.minfreeheap)/1000).toFixed(1)+" kB"," ("+Math.round((i.totalheap-i.minfreeheap)/(10*theap))+"%)"):""} ${inforow("Free heap",heap," kB")} -${i.freestack?inforow("Free stack ☾",i.freestack," kB"):""} +${i.freestack?inforow("Free stack ☾",(i.freestack/1024).toFixed(3)," kB"):""} ${inforow("Flash Size ☾",flashsize," kB")} ${i.tpram?inforow("PSRAM ☾",(i.tpram/1024).toFixed(1)," kB"):""} ${i.psram?((i.tpram-i.psram)>16383?inforow("Used PSRAM ☾",((i.tpram-i.psram)/1024).toFixed(1)," kB"):inforow("Used PSRAM ☾",(i.tpram-i.psram)," B")):""} diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 2fd8811701..196ff7abd0 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -64,6 +64,22 @@ // WLEDMM end +#if defined(ARDUINO_ARCH_ESP32) && (defined(WLED_DEBUG) || defined(WLED_DEBUG_HEAP)) +// WLEDMM stack debug tool - find async_tcp task, and queries it's free stack +static int wledmm_get_tcp_stacksize(void) { + static TaskHandle_t tcp_taskHandle = NULL; // to store the task handle for later calls + char * tcp_taskname = pcTaskGetTaskName(tcp_taskHandle); // ask for name of the known task (to make sure we are still looking at the right one) + + if ((tcp_taskHandle == NULL) || (tcp_taskname == NULL) || (strncmp(tcp_taskname, "async_tcp", 9) != 0)) { + tcp_taskHandle = xTaskGetHandle("async_tcp"); // need to look for the task by name. FreeRTOS docs say this is very slow, so we store the result for next time + //DEBUG_PRINT(F("async_tcp task ")); DEBUG_PRINTLN( (tcp_taskHandle != NULL) ? F("found") : F("not found")); + } + + if (tcp_taskHandle != NULL) return uxTaskGetStackHighWaterMark(tcp_taskHandle); // got it !! + else return -1; +} +#endif + /* * Main WLED class implementation. Mostly initialization and connection logic */ @@ -347,6 +363,8 @@ void WLED::loop() if (millis() - debugTime > 4999 ) { // WLEDMM: Special case for debugging heap faster DEBUG_PRINT(F("*** Free heap: ")); DEBUG_PRINT(heap_caps_get_free_size(0x1800)); DEBUG_PRINT(F("\tLargest free block: ")); DEBUG_PRINT(heap_caps_get_largest_free_block(0x1800)); + DEBUG_PRINT(F(" *** \t\tArduino min free stack: ")); DEBUG_PRINT(uxTaskGetStackHighWaterMark(NULL)); + DEBUG_PRINT(F(" TCP min free stack: ")); DEBUG_PRINT(wledmm_get_tcp_stacksize()); DEBUG_PRINTLN(F(" ***")); debugTime = millis(); } @@ -531,7 +549,7 @@ void WLED::setup() #endif DEBUG_PRINT(F("heap ")); DEBUG_PRINTLN(ESP.getFreeHeap()); #ifdef ARDUINO_ARCH_ESP32 - #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) // unfortunately not availeable in older framework versions + #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) // unfortunately not available in older framework versions DEBUG_PRINT(F("\nArduino max stack ")); DEBUG_PRINTLN(getArduinoLoopTaskStackSize()); #endif DEBUG_PRINTF("%s min free stack %d\n", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL)); //WLEDMM @@ -568,7 +586,7 @@ void WLED::setup() //DEBUG_PRINT(F("LEDs inited. heap usage ~")); //DEBUG_PRINTLN(heapPreAlloc - ESP.getFreeHeap()); - USER_FLUSH(); // WLEDMM flush buffer now, before anything time-critial is started. + USER_FLUSH(); // WLEDMM flush buffer now, before anything time-critical is started. pinManager.manageDebugTXPin(); diff --git a/wled00/wled.h b/wled00/wled.h index 032a3a4f97..38f472df60 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2312050 +#define VERSION 2312120 //WLEDMM + Moustachauve/Wled-Native // You can define custom product info from build flags. diff --git a/wled00/wled_server.cpp b/wled00/wled_server.cpp index 2a50cf3702..202b07d961 100644 --- a/wled00/wled_server.cpp +++ b/wled00/wled_server.cpp @@ -575,8 +575,8 @@ void serveSettingsJS(AsyncWebServerRequest* request) #ifdef ARDUINO_ARCH_ESP32 DEBUG_PRINT(F("ServeSettingsJS: ")); - DEBUG_PRINTF("%s min free stack %d\n", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL)); //WLEDMM - DEBUG_PRINTF(PSTR(" bytes.\tString buffer usage: %4d of %d bytes\n"), strlen(buf)+1, SETTINGS_STACK_BUF_SIZE+37); + DEBUG_PRINTF("%s min free stack %d", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL)); //WLEDMM + DEBUG_PRINTF(PSTR(" bytes.\t\tString buffer usage: %4d of %d bytes\n"), strlen(buf)+1, SETTINGS_STACK_BUF_SIZE+37); #endif AsyncWebServerResponse *response;