From eaf6716a8570d0680f21d68cebc8a5f4ad5d85d6 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Sun, 22 Nov 2026 09:25:48 -0700 Subject: [PATCH] fix(rtps): the QEMU restart case no longer runs the device out of internal RAM The QEMU matrix has failed on main since #909: its restart case tears the participant down and builds a new one while a dozen peers from the earlier cases are still announcing. The new participant discovers them all at once: the proxies (small allocations, internal RAM first) and lwIP's receive / reassembly buffers (internal-only by default) took the byte-addressable internal RAM a task stack needs, so the firmware's publish timer could not create its task (std::thread -> "pthread: Failed to create task!" -> abort with exceptions off). The device rebooted, QEMU's emulated chip cannot boot again from a software reset, and every later case failed with it. From the device log: endpoints used ~20 KB of internal RAM at first boot but ~84 KB after the restart (internal free 139 KB vs 75 KB, most of the latter IRAM, which stacks cannot use). #909's own CI run passed: the case was at the edge of the budget. - sdkconfig.defaults: CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP=y (lwIP from PSRAM first) and CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=65536 (twice the default kept for allocations that need internal RAM: FreeRTOS stacks, DMA). - The memory markers and the status line also report dram= / dram_min= / dram_largest= (internal AND 8-bit: what a stack comes from), since `internal` includes IRAM and hid the shortage. Appended, so the memory suite's parser is unaffected. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU --- components/rtps/interop/qemu/main/rtps_qemu.cpp | 17 +++++++++++++---- components/rtps/interop/qemu/sdkconfig.defaults | 11 +++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/components/rtps/interop/qemu/main/rtps_qemu.cpp b/components/rtps/interop/qemu/main/rtps_qemu.cpp index d3e712d11c..6bd4839852 100644 --- a/components/rtps/interop/qemu/main/rtps_qemu.cpp +++ b/components/rtps/interop/qemu/main/rtps_qemu.cpp @@ -56,14 +56,20 @@ void log_mem_sizes(espp::Logger &logger) { sizeof(rtps::StatelessReader), sizeof(espp::RtpsParticipant)); } +// `internal` counts the ESP32's IRAM as well, which a task stack or any byte +// access cannot use; `dram` (internal AND byte-addressable) is what a new +// task's stack actually comes from, so it is the number that runs out first. +constexpr uint32_t kDramCaps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT; void log_mem_heap(espp::Logger &logger, const char *stage) { logger.info("RTPSTEST mem heap {} internal={} internal_min={} internal_largest={} psram={} " - "psram_largest={}", + "psram_largest={} dram={} dram_min={} dram_largest={}", stage, heap_caps_get_free_size(MALLOC_CAP_INTERNAL), heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL), heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL), heap_caps_get_free_size(MALLOC_CAP_SPIRAM), - heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM)); + heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM), + heap_caps_get_free_size(kDramCaps), heap_caps_get_minimum_free_size(kDramCaps), + heap_caps_get_largest_free_block(kDramCaps)); } // The restart case calls /qemu_add_two_ints with this magic request to ask the // device to recreate its participant (see the service handler in run_participant). @@ -392,7 +398,8 @@ static void run_participant(espp::Logger &logger, const std::string &interface_a sample_stacks(); logger.info("RTPSTEST status up={} tx={} rx={} be_rx={} big_rx={} pub_matches={} " "sub_matches={} heap={} min_heap={} int={} int_min={} int_largest={} psram={} " - "stack_min={} stack_min_task={} stack_min_worker={}", + "stack_min={} stack_min_task={} stack_min_worker={} dram={} dram_min={} " + "dram_largest={}", up, tx_count.load(), rx_count.load(), be_rx_count.load(), big_rx_count.load(), pub_matches.load(), sub_matches.load(), heap_caps_get_free_size(MALLOC_CAP_DEFAULT), heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT), @@ -400,6 +407,8 @@ static void run_participant(espp::Logger &logger, const std::string &interface_a heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL), heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL), heap_caps_get_free_size(MALLOC_CAP_SPIRAM), stack_min == UINT32_MAX ? 0 : stack_min, - stack_min_task, stack_min_worker == UINT32_MAX ? 0 : stack_min_worker); + stack_min_task, stack_min_worker == UINT32_MAX ? 0 : stack_min_worker, + heap_caps_get_free_size(kDramCaps), heap_caps_get_minimum_free_size(kDramCaps), + heap_caps_get_largest_free_block(kDramCaps)); } } diff --git a/components/rtps/interop/qemu/sdkconfig.defaults b/components/rtps/interop/qemu/sdkconfig.defaults index a49bfea789..5f45a705df 100644 --- a/components/rtps/interop/qemu/sdkconfig.defaults +++ b/components/rtps/interop/qemu/sdkconfig.defaults @@ -15,6 +15,17 @@ CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" CONFIG_SPIRAM=y CONFIG_SPIRAM_USE_MALLOC=y CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=4096 +# Keep internal RAM for what must live there. The restart case tears the +# participant down and builds a new one while a dozen peers from the earlier +# cases are still announcing: the new participant discovers them all at once, +# and the proxies (small allocations: internal first, see ALWAYSINTERNAL) plus +# lwIP's receive / reassembly buffers (internal-only by default) took the data +# RAM a task stack needs, so creating the publish timer's task failed and the +# device aborted ("pthread: Failed to create task!"; every later case then +# failed with it). lwIP now allocates from PSRAM first, and twice the default +# is reserved for allocations that need internal RAM (FreeRTOS stacks, DMA). +CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP=y +CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=65536 CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096 CONFIG_ESP_MAIN_TASK_STACK_SIZE=12288