Skip to content

Use os_sync_wait_on_address for SHM condition semaphores on macOS - #6632

Open
mini-1235 wants to merge 3 commits into
eProsima:masterfrom
mini-1235:fix/macos-shm-condition-wait-on-address
Open

mini-1235 wants to merge 3 commits into
eProsima:masterfrom
mini-1235:fix/macos-shm-condition-wait-on-address

Conversation

@mini-1235

Copy link
Copy Markdown
Contributor

Description

On macOS, Boost.Interprocess cannot use process-shared unnamed POSIX semaphores (sem_init returns ENOSYS), so boost::interprocess::interprocess_semaphore falls back to its spin emulation. A waiting thread spins, yields, and then sleeps with thread_sleep_tick() between polls. The SHM transport waits on these semaphores inside RobustInterprocessCondition, so every SHM notification on macOS is delivered with a delay of up to a scheduler tick. The listener threads also keep waking while idle (the CPU cost reported in #2237). This is still the case in the latest Boost.

This PR adds RobustInterprocessSemaphore, used only on macOS in place of interprocess_semaphore in RobustInterprocessCondition::SemaphoreNode:

  • It is a 32-bit atomic counter in shared memory, the same layout as Boost's spin_semaphore.
  • On macOS 14.4+ it blocks in the kernel with os_sync_wait_on_address(..., OS_SYNC_WAIT_ON_ADDRESS_SHARED), and post() wakes a waiter with os_sync_wake_by_address_any. On older macOS it falls back to Boost's spin_wait, which is the current behaviour.
  • Each kernel wait is capped at 1 ms. Processes running an older Fast DDS post through Boost's spin_semaphore, which only increments the counter and never wakes the waiter. With the cap, mixed-version setups are never slower than today.

Other platforms are unchanged.

Results on macOS (Apple Silicon, macOS 27.2, Fast DDS 3.6.2 with and without this change, built from source with identical flags). 10 kHz, 192 B messages, KeepLast 1000, 8 s, SHM transport, 8 runs per build interleaved before/after:

before after
average latency (median) 3.3 ms (3.2–7.1) 0.3 ms (0.2–0.5)
inter-arrival gaps > 1 ms 1,160–1,247 167–264
subscriber CPU at 10 kHz 19–21% 26–30% (wake per message instead of batched polling)
idle subscriber CPU, 4 runs 1.0–1.1 s / 20 s 0.42–0.43 s / 20 s

Mixed versions: an old publisher with a new subscriber gives 100% delivery at 0.8 ms average latency. A new publisher with an old subscriber gives 100% at 3.3 ms, the same as today. For reference, the same benchmark on Linux, where this change compiles to nothing, gives 0.0 ms and almost no gaps.

@Mergifyio backport 3.2.x

Contributor Checklist

  • Commit messages follow the project guidelines.
  • The code follows the style guidelines of this project. Written to match the surrounding code (uncrustify was not run locally).
  • NO: Tests that thoroughly check the new feature have been added/Regression tests checking the bug and its fix have been added; the added tests pass locally. This is a latency/CPU fix with no functional change, so there is nothing to assert in a unit test. The existing SharedMemTests exercise the new code path and pass on macOS with this change.
  • Any new/modified methods have been properly documented using Doxygen.
  • N/A Any new configuration API has an equivalent XML API (with the corresponding XSD extension)
  • Changes are backport compatible: they do NOT break ABI nor change library core behavior. The new class is internal, and the shared-memory layout is unchanged.
  • Changes are API compatible.
  • N/A New feature has been added to the versions.md file (if applicable).
  • N/A New feature has been documented/Current behavior is correctly described in the documentation.
  • Applicable backports have been included in the description. The patch applies cleanly to 3.2.x.

Reviewer Checklist

  • The PR has a milestone assigned.
  • The title and description correctly express the PR's purpose.
  • Check contributor checklist is correct.
  • If this is a critical bug fix, backports to the critical-only supported branches have been requested.
  • Check CI results: changes do not issue any warning.
  • Check CI results: failing tests are unrelated with the changes.

Boost.Interprocess has no process-shared unnamed semaphores on macOS, so
interprocess_semaphore falls back to a spin emulation that sleeps between
polls. Every SHM notification on macOS was delivered with up to a
scheduler tick of delay (about 3 ms average), and idle listener threads
kept waking.

Add RobustInterprocessSemaphore, used by RobustInterprocessCondition on
macOS only. It keeps the 32-bit counter layout of Boost's spin_semaphore,
blocks with os_sync_wait_on_address(OS_SYNC_WAIT_ON_ADDRESS_SHARED) on
macOS 14.4+, and falls back to Boost's spin_wait on older systems. Each
kernel wait is capped at 1 ms so posts from processes using the old
spin_semaphore, which don't wake waiters, are still seen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
@mini-1235

Copy link
Copy Markdown
Contributor Author

I also believe the long-term fix should be made upstream in Boost itself, so I have opened an issue to track it: boostorg/interprocess#285

Compare size and alignment of the whole semaphore with
boost::interprocess::interprocess_semaphore, instead of only comparing
std::atomic<uint32_t> with uint32_t.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
@MiguelCompany MiguelCompany added this to the v3.6.4.0 milestone Nov 16, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
@idesign0

Copy link
Copy Markdown

Tested this PR on top of the two Fast DDS versions used by ROS 2 on macOS. Results below, for reference for the 3.2.x backport and a possible 2.14.x one.

Setup: M2, macOS 27.0.1, ROS 2 from the Homebrew tap idesign0/ros2.

  • Kilted: v3.2.5 ± this PR.
  • Jazzy: 2.14.x ± this PR. The patch applies cleanly to 2.14.x.
  • Build: each variant built in Release with the same config.h/config.hpp as the installed library (HAVE_SECURITY 0), and swapped in under the same ROS 2 install via DYLD_LIBRARY_PATH.
  • Test: rmw_fastrtps_cpp with default transports, two processes, std_msgs/Float64.
Kilted 3.2.5 Kilted 3.2.5 + #6632 Jazzy 2.14.7 Jazzy 2.14.7 + #6632
2 kHz, KEEP_LAST 1: received 1074–1333 Hz 1814–1984 Hz 1100–1135 Hz 1906–2158 Hz
2 kHz, KEEP_LAST 1: latency p50 2.3–2.5 ms ~100 µs 3.3 ms 0.60–0.67 ms
2 kHz, KEEP_LAST 100: latency p50 2.81 ms 35 µs 2.86 ms 36 µs
2 kHz: arrival gap p50 / p99 2 µs / 5.7 ms 500 / 557–691 µs 2 µs / 5.7 ms 498–499 / 631–634 µs
20 kHz: received, p50 96.7 %, 3.1 ms 100 %, 31 µs 99.9 %, 3.1 ms 100 %, 32 µs
50 kHz: received 94.0 % 90.8–99.6 % 86.9 % 91.4–96.8 %
100 kHz: received 1.3 % 1.9–2.3 % (4 runs) 2.1 % 1.3–76 % (4 runs, bimodal)
150 kHz: received — 0.2–0.3 % — 0.2 %
Idle subscriber CPU (20 s) 1.15 s 0.38 s 1.13 s 0.32 s

Mixed versions behave as described in the PR description, on both versions:

Publisher → subscriber Kilted p50 Jazzy p50
old → new 593 µs 584 µs
new → old 2.8 ms 2.8 ms

Two observations:

  • KEEP_LAST 1 on 2.14.x: with both sides patched, the median is ~0.6 ms on 2.14.x versus ~0.1 ms on 3.2.5, and p99 is ~1.1–1.2 ms on both. That matches the 1 ms cap, so some wake-ups seem to be picked up only by the timeout. This happens with KEEP_LAST 1; with KEEP_LAST 100 the median is 35–36 µs on both.
  • Overload: around 100 kHz both versions hit a cliff, with or without the patch. 3.2.5 delivered ~2 % in every run; 2.14.x delivered ~2 % in two of four runs and 59–76 % in the other two. At 150 kHz both deliver ~0.2 %. So the overload collapse seems unrelated to this change.

Humble (2.6.x) needs a small context change to apply (no BoostAtExitRegistry.hpp include there). I haven't tested it yet.

Scripts and full results: https://github.com/idesign0/ros2-macos-rmw-bench

idesign0 pushed a commit to idesign0/id_Fast-DDS that referenced this pull request Nov 20, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
(cherry picked from commit 4a85ed4)

Carried in id_Fast-DDS until eProsima#6632 is merged and backported
(fixes eProsima#6639).

Tested on macOS 27.0.1 (M2) against the ros2-kilted keg (Homebrew tap
idesign0/ros2): built v3.2.5 + eProsima#6632 as libfastdds 3.2.5 Release with the
keg's config.hpp (HAVE_SECURITY 0) and swapped it in via DYLD_LIBRARY_PATH.
Unpatched build: 2 kHz Float64 KEEP_LAST 1 received 1074-1333 Hz at
2.3-2.5 ms p50, KEEP_LAST 100 p50 2.81 ms with 5.7 ms bursts. With eProsima#6632:
1814-1984 Hz at ~100 us p50, KEEP_LAST 100 p50 35 us with steady 0.5 ms
arrival gaps; idle listener CPU 1.15 s -> 0.38 s per 20 s.
idesign0 pushed a commit to idesign0/id_Fast-DDS that referenced this pull request Nov 20, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
(cherry picked from commit 4a85ed4)

Carried in id_Fast-DDS until eProsima#6632 is merged and backported
(fixes eProsima#6639).

Tested on macOS 27.0.1 (M2) against the ros2-jazzy keg (Homebrew tap
idesign0/ros2): built this branch + eProsima#6632 as libfastrtps 2.14.7 Release with
the keg's config.h (HAVE_SECURITY 0, FASTDDS_STATISTICS) and swapped it in via
DYLD_LIBRARY_PATH. Unpatched build: 2 kHz Float64 KEEP_LAST 1 received
1100-1135 Hz at 3.3 ms p50, KEEP_LAST 100 p50 2.86 ms with 5.7 ms bursts.
With eProsima#6632: 1906-2158 Hz at 0.60-0.67 ms p50, KEEP_LAST 100 p50 36 us with
steady 0.5 ms arrival gaps; idle listener CPU 1.13 s -> 0.32 s per 20 s.
idesign0 pushed a commit to idesign0/id_Fast-DDS that referenced this pull request Nov 22, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
(cherry picked from commit 4a85ed4)

Carried in id_Fast-DDS until eProsima#6632 is merged and backported
(fixes eProsima#6639).

Tested on macOS 27.0.1 (M2) against the ros2-jazzy keg (Homebrew tap
idesign0/ros2): built this branch + eProsima#6632 as libfastrtps 2.14.7 Release with
the keg's config.h (HAVE_SECURITY 0, FASTDDS_STATISTICS) and swapped it in via
DYLD_LIBRARY_PATH. Unpatched build: 2 kHz Float64 KEEP_LAST 1 received
1100-1135 Hz at 3.3 ms p50, KEEP_LAST 100 p50 2.86 ms with 5.7 ms bursts.
With eProsima#6632: 1906-2158 Hz at 0.60-0.67 ms p50, KEEP_LAST 100 p50 36 us with
steady 0.5 ms arrival gaps; idle listener CPU 1.13 s -> 0.32 s per 20 s.
idesign0 pushed a commit to idesign0/id_Fast-DDS that referenced this pull request Nov 22, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
(cherry picked from commit 4a85ed4)

Carried in id_Fast-DDS until eProsima#6632 is merged and backported
(fixes eProsima#6639).

Tested on macOS 27.0.1 (M2) against the ros2-kilted keg (Homebrew tap
idesign0/ros2): built v3.2.5 + eProsima#6632 as libfastdds 3.2.5 Release with the
keg's config.hpp (HAVE_SECURITY 0) and swapped it in via DYLD_LIBRARY_PATH.
Unpatched build: 2 kHz Float64 KEEP_LAST 1 received 1074-1333 Hz at
2.3-2.5 ms p50, KEEP_LAST 100 p50 2.81 ms with 5.7 ms bursts. With eProsima#6632:
1814-1984 Hz at ~100 us p50, KEEP_LAST 100 p50 35 us with steady 0.5 ms
arrival gaps; idle listener CPU 1.15 s -> 0.38 s per 20 s.
idesign0 pushed a commit to idesign0/id_Fast-DDS that referenced this pull request Nov 29, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
(cherry picked from commit 4a85ed4)

Carried in id_Fast-DDS until eProsima#6632 is merged and backported
(fixes eProsima#6639).

Tested on macOS 27.0.1 (M2) against the ros2-jazzy keg (Homebrew tap
idesign0/ros2): built this branch + eProsima#6632 as libfastrtps 2.14.7 Release with
the keg's config.h (HAVE_SECURITY 0, FASTDDS_STATISTICS) and swapped it in via
DYLD_LIBRARY_PATH. Unpatched build: 2 kHz Float64 KEEP_LAST 1 received
1100-1135 Hz at 3.3 ms p50, KEEP_LAST 100 p50 2.86 ms with 5.7 ms bursts.
With eProsima#6632: 1906-2158 Hz at 0.60-0.67 ms p50, KEEP_LAST 100 p50 36 us with
steady 0.5 ms arrival gaps; idle listener CPU 1.13 s -> 0.32 s per 20 s.
idesign0 pushed a commit to idesign0/id_Fast-DDS that referenced this pull request Nov 29, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
(cherry picked from commit 4a85ed4)

Carried in id_Fast-DDS until eProsima#6632 is merged and backported
(fixes eProsima#6639).

Tested on macOS 27.0.1 (M2) against the ros2-kilted keg (Homebrew tap
idesign0/ros2): built v3.2.5 + eProsima#6632 as libfastdds 3.2.5 Release with the
keg's config.hpp (HAVE_SECURITY 0) and swapped it in via DYLD_LIBRARY_PATH.
Unpatched build: 2 kHz Float64 KEEP_LAST 1 received 1074-1333 Hz at
2.3-2.5 ms p50, KEEP_LAST 100 p50 2.81 ms with 5.7 ms bursts. With eProsima#6632:
1814-1984 Hz at ~100 us p50, KEEP_LAST 100 p50 35 us with steady 0.5 ms
arrival gaps; idle listener CPU 1.15 s -> 0.38 s per 20 s.
idesign0 pushed a commit to idesign0/id_Fast-DDS that referenced this pull request Dec 4, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Maurice <mauricepurnawan@gmail.com>
(cherry picked from commit 4a85ed4)

Carried in id_Fast-DDS until eProsima#6632 is merged and backported
(fixes eProsima#6639).

Tested on macOS 27.0.1 (M2) against the ros2-kilted keg (Homebrew tap
idesign0/ros2): built v3.2.5 + eProsima#6632 as libfastdds 3.2.5 Release with the
keg's config.hpp (HAVE_SECURITY 0) and swapped it in via DYLD_LIBRARY_PATH.
Unpatched build: 2 kHz Float64 KEEP_LAST 1 received 1074-1333 Hz at
2.3-2.5 ms p50, KEEP_LAST 100 p50 2.81 ms with 5.7 ms bursts. With eProsima#6632:
1814-1984 Hz at ~100 us p50, KEEP_LAST 100 p50 35 us with steady 0.5 ms
arrival gaps; idle listener CPU 1.15 s -> 0.38 s per 20 s.

(cherry picked from commit 956924d)

This branch has not been deployed

No deployments
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.

[macOS] SHM transport wakes readers on a ~5.7 ms polling tick: Boost.Interprocess falls back to spin_semaphore on Apple

3 participants