From cd2b157682134a660b149f3c3b409487c2d8f7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Cab=C3=A9?= Date: Sat, 24 Oct 2026 17:06:39 +0200 Subject: [PATCH] perf(uORB): skip subscription lookups until a device node is added (#29249) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While a topic has no device node, every Subscription::update(), copy(), updated() and advertised() repeats the device node lookup and fails again. Polling modules touch many optional topics on every loop: in a SITL flight about 8% of all instructions, and about a quarter of each MAVLink thread, were such failed lookups. Device nodes are never removed, so a lookup that failed cannot succeed before a node is added. Count the nodes added in DeviceMaster and only retry the lookup once that count has changed. Protected builds, where user space has no cheap access to it, keep retrying every time. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Benjamin Cabé --- platforms/common/uORB/Subscription.cpp | 16 +++++++++++++++ platforms/common/uORB/Subscription.hpp | 4 ++-- .../common/uORB/test/uORBSubscriptionTest.cpp | 20 +++++++++++++++++++ platforms/common/uORB/uORBDeviceMaster.cpp | 1 + platforms/common/uORB/uORBDeviceMaster.hpp | 9 +++++++++ platforms/common/uORB/uORBManager.cpp | 7 +++++++ platforms/common/uORB/uORBManager.hpp | 6 ++++++ platforms/common/uORB/uORBManagerUsr.cpp | 6 ++++++ 8 files changed, 67 insertions(+), 2 deletions(-) diff --git a/platforms/common/uORB/Subscription.cpp b/platforms/common/uORB/Subscription.cpp index 837feed5c78..188df0264a2 100644 --- a/platforms/common/uORB/Subscription.cpp +++ b/platforms/common/uORB/Subscription.cpp @@ -50,6 +50,14 @@ bool Subscription::subscribe() } if (_orb_id != ORB_ID::INVALID && uORB::Manager::get_instance()) { + // While unsubscribed, _last_generation holds the node generation of the last failed lookup: + // nodes are never removed, so the lookup cannot succeed before a node is added. + const unsigned node_generation = uORB::Manager::orb_node_generation(); + + if (node_generation != 0 && node_generation == _last_generation) { + return false; + } + unsigned initial_generation; void *node = uORB::Manager::orb_add_internal_subscriber(_orb_id, _instance, &initial_generation); @@ -58,11 +66,19 @@ bool Subscription::subscribe() _last_generation = initial_generation; return true; } + + _last_generation = node_generation; } return false; } +unsigned Subscription::get_last_generation() const +{ + // while unsubscribed, _last_generation holds a node generation: report 0 as before + return (_node != nullptr) ? _last_generation : 0; +} + void Subscription::unsubscribe() { if (_node != nullptr) { diff --git a/platforms/common/uORB/Subscription.hpp b/platforms/common/uORB/Subscription.hpp index 596d81cfeb0..b065e6a5300 100644 --- a/platforms/common/uORB/Subscription.hpp +++ b/platforms/common/uORB/Subscription.hpp @@ -117,7 +117,7 @@ public: bool ChangeInstance(uint8_t instance); uint8_t get_instance() const { return _instance; } - unsigned get_last_generation() const { return _last_generation; } + unsigned get_last_generation() const; orb_id_t get_topic() const { return get_orb_meta(_orb_id); } ORB_ID orb_id() const { return _orb_id; } @@ -131,7 +131,7 @@ protected: void *_node{nullptr}; - unsigned _last_generation{0}; /**< last generation the subscriber has seen */ + unsigned _last_generation{0}; /**< last generation the subscriber has seen, or while unsubscribed the node generation of the last failed lookup */ ORB_ID _orb_id{ORB_ID::INVALID}; uint8_t _instance{0}; diff --git a/platforms/common/uORB/test/uORBSubscriptionTest.cpp b/platforms/common/uORB/test/uORBSubscriptionTest.cpp index ec6333d6535..222d5a2e3bc 100644 --- a/platforms/common/uORB/test/uORBSubscriptionTest.cpp +++ b/platforms/common/uORB/test/uORBSubscriptionTest.cpp @@ -39,6 +39,7 @@ #include #include #include +#include namespace uORB { @@ -110,5 +111,24 @@ TEST_F(uORBSubscriptionTest, updateWhenNotSubscribedThenSubscribed) ASSERT_NE(testable.getNodeValue(), nullptr) << "Node value after 'updated' have to be initialized"; } + +TEST_F(uORBSubscriptionTest, subscribeAfterFailedLookupsOnceTopicIsAdvertised) +{ + // orb_test_medium has no device node yet: repeated lookups fail + uORB::Subscription sub{ORB_ID(orb_test_medium)}; + ASSERT_FALSE(sub.advertised()); + ASSERT_FALSE(sub.updated()); + ASSERT_FALSE(sub.valid()); + EXPECT_EQ(sub.get_last_generation(), 0u) << "Generation of an unsubscribed subscription must stay 0"; + + orb_test_medium_s message{}; + orb_advert_t pub = orb_advertise(ORB_ID(orb_test_medium), &message); + ASSERT_NE(pub, nullptr); + + EXPECT_TRUE(sub.advertised()) << "Adding the device node must make the next lookup succeed"; + EXPECT_TRUE(sub.valid()); + + orb_unadvertise(pub); +} } } diff --git a/platforms/common/uORB/uORBDeviceMaster.cpp b/platforms/common/uORB/uORBDeviceMaster.cpp index 8b181d1f8f6..ccb5dde0932 100644 --- a/platforms/common/uORB/uORBDeviceMaster.cpp +++ b/platforms/common/uORB/uORBDeviceMaster.cpp @@ -120,6 +120,7 @@ int uORB::DeviceMaster::advertise(const struct orb_metadata *meta, bool is_adver // add to the node map. _node_list.add(node); _node_exists[node->get_instance()].set((orb_id_size_t)node->id(), true); + _node_generation.fetch_add(1); // only once the node can be found } else { delete node; diff --git a/platforms/common/uORB/uORBDeviceMaster.hpp b/platforms/common/uORB/uORBDeviceMaster.hpp index ccda58f8d37..7f36e04921e 100644 --- a/platforms/common/uORB/uORBDeviceMaster.hpp +++ b/platforms/common/uORB/uORBDeviceMaster.hpp @@ -51,6 +51,7 @@ class Manager; #include #include +#include #include using px4::AtomicBitset; @@ -92,6 +93,13 @@ public: } + /** + * Changes every time a DeviceNode is added. Nodes are never removed, so a node lookup that + * failed cannot succeed before this value changes. Starts at 1 and cannot wrap around, as at most + * ORB_TOPICS_COUNT * ORB_MULTI_MAX_INSTANCES nodes exist. + */ + unsigned nodeGeneration() const { return _node_generation.load(); } + bool deviceNodeExists(ORB_ID id, const uint8_t instance) { if ((id == ORB_ID::INVALID) || (instance > ORB_MULTI_MAX_INSTANCES - 1)) { @@ -141,6 +149,7 @@ private: IntrusiveSortedList _node_list; AtomicBitset _node_exists[ORB_MULTI_MAX_INSTANCES]; + px4::atomic _node_generation{1}; px4_sem_t _lock; /**< lock to protect access to all class members (also for derived classes) */ diff --git a/platforms/common/uORB/uORBManager.cpp b/platforms/common/uORB/uORBManager.cpp index 5fed20ac946..a8445653df5 100644 --- a/platforms/common/uORB/uORBManager.cpp +++ b/platforms/common/uORB/uORBManager.cpp @@ -419,6 +419,13 @@ bool uORB::Manager::orb_device_node_exists(ORB_ID orb_id, uint8_t instance) device_master->deviceNodeExists(orb_id, instance); } +unsigned uORB::Manager::orb_node_generation() +{ + // no device master means no device node yet: 0, so that subscribers retry + const DeviceMaster *device_master = uORB::Manager::get_instance()->_device_master; + return (device_master != nullptr) ? device_master->nodeGeneration() : 0; +} + void *uORB::Manager::orb_add_internal_subscriber(ORB_ID orb_id, uint8_t instance, unsigned *initial_generation) { uORB::DeviceNode *node = nullptr; diff --git a/platforms/common/uORB/uORBManager.hpp b/platforms/common/uORB/uORBManager.hpp index 1568546bcf1..dca962b700f 100644 --- a/platforms/common/uORB/uORBManager.hpp +++ b/platforms/common/uORB/uORBManager.hpp @@ -435,6 +435,12 @@ public: static bool orb_device_node_exists(ORB_ID orb_id, uint8_t instance); + /** + * Changes every time a device node is added, so a failed orb_add_internal_subscriber() cannot + * succeed before it changes. 0 if unknown, e.g. in user space of a protected build. + */ + static unsigned orb_node_generation(); + static void *orb_add_internal_subscriber(ORB_ID orb_id, uint8_t instance, unsigned *initial_generation); static void orb_remove_internal_subscriber(void *node_handle); diff --git a/platforms/common/uORB/uORBManagerUsr.cpp b/platforms/common/uORB/uORBManagerUsr.cpp index 82c88455114..9a51ffac48f 100644 --- a/platforms/common/uORB/uORBManagerUsr.cpp +++ b/platforms/common/uORB/uORBManagerUsr.cpp @@ -270,6 +270,12 @@ bool uORB::Manager::orb_device_node_exists(ORB_ID orb_id, uint8_t instance) return data.ret == PX4_OK ? true : false; } +unsigned uORB::Manager::orb_node_generation() +{ + // the device master lives in the kernel: unknown, so subscribers always retry + return 0; +} + void *uORB::Manager::orb_add_internal_subscriber(ORB_ID orb_id, uint8_t instance, unsigned *initial_generation) { orbiocdevaddsubscriber_t data = {orb_id, instance, initial_generation, nullptr};