From 9012be93adee795599286c1b21e9888be0d6bb83 Mon Sep 17 00:00:00 2001 From: Andrea Bernasconi Date: Wed, 21 Oct 2026 16:23:53 +0200 Subject: [PATCH] perf(uxrce_dds_client): poll transport fd with uORB to cut idle wakeups (#29099) * perf(uxrce_dds_client): poll transport fd with uORB to cut idle wakeups The main loop polled uORB with a 1 ms timeout, so it woke up ~1000 times per second even with no topic updates and no agent traffic. On NuttX, add the transport fd to the px4_poll() set (reserved as an extra trailing slot in SendTopicsSubs::fds). The loop now sleeps until a subscribed topic updates or the agent sends data, with a 10 ms timeout kept only as a fallback for housekeeping. On POSIX, px4_poll() only handles uORB fds, so the previous behavior is kept: a short 1 ms timeout, dropped to 0 when the transport has pending data. * fix(uxrce_dds_client): skip transport fd in poll after POLLERR/POLLHUP poll() always reports POLLERR/POLLHUP, even when only POLLIN is requested. On NuttX a disconnected USB CDC link keeps reporting POLLHUP, so with the transport fd in the poll set px4_poll() returned immediately on every iteration and the loop spun at full CPU until checkConnectivity dropped the session. When the transport slot reports an error, leave it out of the next poll and wait on uORB with the short timeout instead. This bounds the loop rate while the error persists and recovers on its own once it clears. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Andrea Bernasconi --------- Signed-off-by: Andrea Bernasconi --- src/modules/uxrce_dds_client/dds_topics.h.em | 4 +- .../uxrce_dds_client/uxrce_dds_client.cpp | 44 ++++++++++++++++--- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/modules/uxrce_dds_client/dds_topics.h.em b/src/modules/uxrce_dds_client/dds_topics.h.em index 8d7b7046248..a5ff7b8e46e 100644 --- a/src/modules/uxrce_dds_client/dds_topics.h.em +++ b/src/modules/uxrce_dds_client/dds_topics.h.em @@ -92,7 +92,9 @@ struct SendTopicsSubs { @[ end for]@ }; - px4_pollfd_struct_t fds[@(len(publications))] {}; + // One entry per publication, plus a trailing slot the client uses for the + // transport fd, so it can wait on uORB updates and incoming agent data together. + px4_pollfd_struct_t fds[@(len(publications)) + 1] {}; uint32_t num_payload_sent{}; diff --git a/src/modules/uxrce_dds_client/uxrce_dds_client.cpp b/src/modules/uxrce_dds_client/uxrce_dds_client.cpp index 532c75b5d66..2864bbdb5ba 100644 --- a/src/modules/uxrce_dds_client/uxrce_dds_client.cpp +++ b/src/modules/uxrce_dds_client/uxrce_dds_client.cpp @@ -670,17 +670,40 @@ void UxrceddsClient::run() hrt_abstime last_sync_session = 0; int poll_error_counter = 0; +#if defined(__PX4_NUTTX) + bool transport_poll_error = false; +#endif resetConnectivityCounters(); while (!should_exit() && _connected) { perf_begin(_loop_perf); perf_count(_loop_interval_perf); - // Keep loop latency low without busy-looping: - // - Use a short uORB poll timeout (instead of 10ms) - // - If transport has pending data, don't block in uORB poll at all. - int orb_poll_timeout_ms = 1; + // The last fds slot is reserved for the transport fd (see SendTopicsSubs) + const unsigned num_orb_fds = (sizeof(_subs->fds) / sizeof(_subs->fds[0])) - 1; + unsigned num_poll_fds = num_orb_fds; + int poll_timeout_ms = 1; +#if defined(__PX4_NUTTX) + + // On NuttX the transport fd is a regular file descriptor: poll it together with the + // uORB subscriptions so the loop only wakes up on topic updates or incoming agent data, + // instead of spinning on a 1ms timeout. The timeout is only a fallback for housekeeping. + // If the transport fd reported an error on the previous iteration (e.g. POLLHUP on a + // disconnected USB link), skip it once and wait on uORB with the short timeout instead, + // so a persistent error can't make poll() return immediately and spin the loop. + if (_fd >= 0 && !transport_poll_error) { + _subs->fds[num_orb_fds].fd = _fd; + _subs->fds[num_orb_fds].events = POLLIN; + _subs->fds[num_orb_fds].revents = 0; + num_poll_fds = num_orb_fds + 1; + poll_timeout_ms = 10; + } + +#else + + // px4_poll() on POSIX only handles uORB fds: keep a short timeout and + // don't block in uORB poll at all if the transport has pending data. if (_fd >= 0) { struct pollfd transport_pollfd {}; transport_pollfd.fd = _fd; @@ -688,12 +711,19 @@ void UxrceddsClient::run() const int transport_poll = poll(&transport_pollfd, 1, 0); if (transport_poll > 0) { - orb_poll_timeout_ms = 0; + poll_timeout_ms = 0; } } - /* Wait for topic updates */ - int poll = px4_poll(_subs->fds, (sizeof(_subs->fds) / sizeof(_subs->fds[0])), orb_poll_timeout_ms); +#endif + + /* Wait for topic updates (and incoming agent data on NuttX) */ + int poll = px4_poll(_subs->fds, num_poll_fds, poll_timeout_ms); + +#if defined(__PX4_NUTTX) + transport_poll_error = (num_poll_fds > num_orb_fds) + && (_subs->fds[num_orb_fds].revents & (POLLERR | POLLHUP | POLLNVAL)); +#endif /* Handle the poll results */ if (poll > 0) {