perf(uORB): skip subscription lookups until a device node is added (#29249)

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é <benjamin@zephyrproject.org>
This commit is contained in:
Benjamin Cabé authored and GitHub committed 2026-10-24 07:06:39 -08:00
1 parent 8c3b934957
commit cd2b157682
8 files changed
+67 -2

No files matched your search

+16
View File
@@ -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) {
+2 -2
View File
@@ -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};
@@ -39,6 +39,7 @@
#include <uORB/Subscription.hpp>
#include <uORB/uORB.h>
#include <uORB/topics/orb_test.h>
#include <uORB/topics/orb_test_medium.h>
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);
}
}
}
@@ -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;
@@ -51,6 +51,7 @@ class Manager;
#include <stdlib.h>
#include <containers/IntrusiveSortedList.hpp>
#include <px4_platform_common/atomic.h>
#include <px4_platform_common/atomic_bitset.h>
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<uORB::DeviceNode *> _node_list;
AtomicBitset<ORB_TOPICS_COUNT> _node_exists[ORB_MULTI_MAX_INSTANCES];
px4::atomic<unsigned> _node_generation{1};
px4_sem_t _lock; /**< lock to protect access to all class members (also for derived classes) */
+7
View File
@@ -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;
+6
View File
@@ -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);
+6
View File
@@ -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};