Skip to content

fix(rtps): QEMU restart case internal-RAM headroom (received frames to PSRAM) + docs - #923

Merged
finger563 merged 5 commits into
mainfrom
fix/rtps-restart-headroom
Nov 23, 2026
Merged

finger563 merged 5 commits into
mainfrom
fix/rtps-restart-headroom

Conversation

@finger563

@finger563 finger563 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #922. After the in-process restart, the QEMU restart case passed with almost no internal RAM to spare: 18 KB free after its endpoints, with a low point of 10 KB. Some runs aborted creating a task or a lock.

Cause

The QEMU Ethernet driver (esp_eth_mac_openeth.c) malloc()s a 1522-byte buffer for every received frame. lwIP frees it only when the application reads the datagram, and the firmware lets each socket queue up to 64 (CONFIG_LWIP_UDP_RECVMBOX_SIZE). That size is below CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL (4096), so the frames sat in internal RAM.

A participant recreated next to a dozen live ROS 2 peers gets an answer from every peer to each endpoint it creates. Measured per endpoint, that cost 8-12 KB of internal RAM after the restart, against about 1 KB on first boot. The task count stayed flat. Discovery uses fixed pools, and the engine's own payload buffers were ruled out: moving them to PSRAM changed nothing.

Change

  • QEMU firmware: CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=1024, below a frame, so received frames go to PSRAM while small allocations (FreeRTOS objects, small engine state) stay internal. The engine is unchanged.
  • QEMU firmware logging: a RTPSTEST dram <endpoint> free= min= largest= tasks= line after each endpoint, on first boot and after the restart, so a regression shows in the CI device log. The memory suite's stage names are not reused.
  • Docs:
    • QEMU board README, docs page and QEMU guide: how the emulated MAC holds received frames (DMA ring, per-frame heap buffer, lwIP queues, reassembly) and what to configure.
    • RTPS README and docs: an "Internal RAM on a busy domain" section covering symptoms, cause, settings and where to measure.
    • The RESERVE_INTERNAL comment from fix(rtps): the QEMU restart case no longer runs the device out of internal RAM #922 no longer blames discovery proxies.

Result (QEMU matrix device log)

Free internal RAM (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT), after the restart:

before (#922) this PR
after the endpoints 17987 67759
low point 10420 58364
per endpoint 8-12 KB ~1 KB

Matrix 45/0, soak 10/0, memory suite passing.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

… first 3 s

Diagnostic for the restart case's thin internal-RAM headroom: a RTPSTEST dram
marker (byte-addressable internal free / min / largest + task count) after
each endpoint is created, every 100 ms for 3 s after the endpoints exist, and
the task names at both ends of that window, on every participant (the first
and the one after the restart), so their costs can be compared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
@github-actions

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

rtps memory footprint (QEMU, esp32)

Deltas in parentheses are against main @ 555cdd3 (run 45059047023): a bigger size or a smaller free heap than the baseline's is a cost of this change.

librtps.a in the image (bytes, idf.py size-components)

embedded host host_large
flash code 162,092 (+20) 163,454 (+24) 163,870 (+24)
flash data 1,272 (+0) 1,272 (+0) 1,272 (+0)
DRAM (.data + .bss) 126 (+0) 126 (+0) 126 (+0)
IRAM 0 (+0) 0 (+0) 0 (+0)

engine objects, sizeof (bytes; the Domain is allocated from the heap at start())

object embedded host host_large
rtps::Domain (holds every pool below) 99,208 (+0) 569,968 (+0) 3,156,208 (+0)
rtps::Participant 34,304 (+0) 42,736 (+0) 52,720 (+0)
rtps::StatefulWriter 2,528 (+0) 2,960 (+0) 6,416 (+0)
rtps::StatelessWriter 1,872 (+0) 2,256 (+0) 3,792 (+0)
rtps::StatefulReader 1,968 (+0) 2,008 (+0) 2,104 (+0)
rtps::StatelessReader 1,960 (+0) 2,000 (+0) 2,096 (+0)
espp::RtpsParticipant 608 (+0) 608 (+0) 608 (+0)

embedded: heap by stage (free bytes; a cost is a negative delta)

stage internal delta internal low-water mark largest internal block PSRAM delta
boot 220,279 (+0) 219,552 (+1,384) 131,072 4,191,440 (+0)
started 160,171 (+16) -60,108 159,576 (+36) 73,728 4,090,196 (-12) -101,244
endpoints 139,375 (-120) -20,796 138,000 (-120) 69,632 4,090,196 (-12) +0

embedded: heap after each host-driven step (free bytes; deltas against the previous row; the pools are preallocated, so a peer costs little heap - the low-water mark shows the transients)

step internal delta internal low-water mark largest internal block PSRAM delta
baseline 133,259 (+28) 132,172 (+1,468) 69,632 4,090,216 (+0)
+1 remote participant (no match) 133,135 (+40) -124 131,112 (+5,104) 69,632 4,090,216 (+0) +0
+1 more remote participant (no match) 133,127 (+32) -8 131,104 (+5,284) 69,632 4,090,208 (-8) -8
+1 remote participant with a matched publisher 133,099 (+40) -28 130,948 (+7,728) 69,632 4,090,208 (-8) +0
after a service call 133,091 (+56) -8 130,948 (+32,708) 69,632 4,090,208 (-8) +0
after an action goal 131,987 (+0) -1,104 130,320 (+46,652) 69,632 4,090,216 (+0) +8
after one 30 KB sample 131,987 (+8) +0 129,960 (+54,028) 69,632 4,060,004 (-12) -30,212
all peers gone 131,979 (+0) -8 129,960 (+54,028) 69,632 4,059,996 (-20) -8

host: heap by stage (free bytes; a cost is a negative delta)

stage internal delta internal low-water mark largest internal block PSRAM delta
boot 219,047 (+0) 218,320 (+1,384) 131,072 4,191,440 (+0)
started 130,575 (+16) -88,472 129,980 (+36) 69,632 3,617,108 (-12) -574,332
endpoints 109,779 (-136) -20,796 108,404 (-136) 69,632 3,617,108 (-12) +0

host_large: the participant did not start on this target - the profile does not fit (panicked while the engine was being built - internal RAM exhausted building the Domain's mutexes (the first error logged: [RtpsTransport/E][3.418]: Failed to create valid UDP socket for port 7401)); its sizeof is in the table above, the boot heap below

host_large: heap by stage (free bytes; a cost is a negative delta)

stage internal delta internal low-water mark largest internal block PSRAM delta
boot 219,047 (+0) 218,320 (+0) 131,072 4,191,440 (+0)
started n/a
endpoints n/a

The QEMU memory suite on 276547d (run); the full numbers are the run's rtps-memory artifact.

finger563 and others added 4 commits November 22, 2026 14:30
…TERNAL 512)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
…SRAM)

The QEMU restart case showed a participant recreated next to a dozen live ROS 2
peers spending 8-12 KB of internal RAM per endpoint (about 1 KB on an empty
domain) until creating the action server's FreeRTOS lock failed and the device
aborted. The cost is the engine's payload bytes - history-cache samples, the
messages it builds and the datagrams it parses: a few hundred bytes each, below
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL, so malloc() put them in internal RAM, and
the new endpoints match every peer at once. Lowering ALWAYSINTERNAL to 512 in the
QEMU firmware brought the per-endpoint cost after the restart back to ~1 KB and
the post-restart low point from 10 KB to 60 KB, confirming it.

PayloadBuffer::bytes and PacketInfo::payload now use rtps::PayloadAllocator,
which allocates from PSRAM first (heap_caps_malloc_prefer, internal RAM as the
fallback) when CONFIG_RTPS_PAYLOAD_PREFER_PSRAM is set - default on whenever
PSRAM is enabled - and is plain operator new elsewhere (host, no PSRAM, option
off). Applications get it without changing a global heap setting. The QEMU
firmware is back on the default ALWAYSINTERNAL to exercise the engine fix and
keeps a per-endpoint internal-RAM marker (the timeline and task-name dumps are
removed). README + docs: the option, and a section on internal RAM on a busy
domain (RESERVE_INTERNAL, lwIP in PSRAM, which free-size figure to watch).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
…rt the payload allocator

The payload allocator did not move the restart case's internal-RAM cost (after
the restart the endpoints still left 8.7 KB, matrix 11/33): the engine's
payloads were not it. The cost is the openeth driver's receive buffers: it
mallocs a full frame (~1.5 KB) per packet and lwIP holds it until the engine
reads the datagram, up to LWIP_UDP_RECVMBOX_SIZE (64) per socket. Below the
4096 ALWAYSINTERNAL threshold they landed in internal RAM, and a participant
recreated next to a dozen live peers gets all of their answers while it
creates its endpoints. The ALWAYSINTERNAL=512 experiment moved exactly these.

QEMU firmware: ALWAYSINTERNAL 1024, below a frame, so smaller allocations
(FreeRTOS objects, small engine state) stay internal. PayloadAllocator and
RTPS_PAYLOAD_PREFER_PSRAM are removed (the engine is unchanged). The README's
busy-domain section now names the driver's frames and lwIP's queue depth as
the cost and what to configure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
…derations

QEMU board (README, its docs page, the QEMU guide): how the emulated MAC's
driver holds received frames - the DMA receive ring (4 x 1600 B internal by
default; a burst longer than it is dropped, RX frame dropped (0x14)) and one
malloc()ed 1522-byte buffer per frame until the application reads it, queued
up to LWIP_UDP_RECVMBOX_SIZE per socket - why that lands in internal RAM with
PSRAM (below ALWAYSINTERNAL), how it fails (a task or lock that cannot be
created), what to configure, and that IP reassembly is off by default.

RTPS (README and docs page): the busy-domain section now explains the
mechanism - four sockets plus dedicated ports, every live peer answering each
new endpoint at once, worst when joining or recreating on a running domain -
with the symptoms, the options, where to measure, and the per-endpoint
RTPSTEST dram markers the QEMU firmware logs. The QEMU firmware's
RESERVE_INTERNAL comment no longer blames the proxies (discovery uses fixed
pools; it was the queued frames).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
@finger563 finger563 changed the title fix(rtps): more internal-RAM headroom for the QEMU restart case (in progress) fix(rtps): QEMU restart case internal-RAM headroom (received frames to PSRAM) + docs Nov 23, 2026
@finger563
finger563 marked this pull request as ready for review November 23, 2026 00:55
@finger563
finger563 merged commit 454a5b5 into main Nov 23, 2026
34 checks passed
@finger563
finger563 deleted the fix/rtps-restart-headroom branch November 23, 2026 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant