Skip to content

Commit 3b10caf

Browse files
rodrigopexnashif
authored andcommitted
zbus: channel msg subscriber pool isolation
Currently, zbus uses a single global `net_buf`pool to publish messages to msg_subscribers. It would be good to have a way to separate the pools for channels related to critical parts of the systems to avoid publication failure on these particular channels. These channels will not use the global pool. They can set an isolated pool by calling the `zbus_chan_set_msg_sub_pool.` Signed-off-by: Rodrigo Peixoto <[email protected]>
1 parent da70246 commit 3b10caf

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

include/zephyr/zbus/zbus.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ struct zbus_channel_data {
5656
*/
5757
sys_slist_t observers;
5858
#endif /* CONFIG_ZBUS_RUNTIME_OBSERVERS */
59+
60+
#if defined(CONFIG_ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION) || defined(__DOXYGEN__)
61+
/** Net buf pool for message subscribers. It can be either the global or a separated one.
62+
*/
63+
struct net_buf_pool *msg_subscriber_pool;
64+
#endif /* ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION */
5965
};
6066

6167
/**
@@ -347,6 +353,9 @@ struct zbus_channel_observation {
347353
.user_data = _user_data, \
348354
.validator = _validator, \
349355
.data = &_CONCAT(_zbus_chan_data_, _name), \
356+
IF_ENABLED(ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION, ( \
357+
.msg_subscriber_pool = &_zbus_msg_subscribers_pool, \
358+
)) \
350359
}; \
351360
/* Extern declaration of observers */ \
352361
ZBUS_OBS_DECLARE(_observers); \
@@ -684,6 +693,25 @@ static inline void *zbus_chan_user_data(const struct zbus_channel *chan)
684693
return chan->user_data;
685694
}
686695

696+
#if defined(CONFIG_ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION) || defined(__DOXYGEN__)
697+
698+
/**
699+
* @brief Set the channel's msg subscriber `net_buf` pool.
700+
*
701+
* @param chan The channel's reference.
702+
* @param pool The reference to the `net_buf` memory pool.
703+
*/
704+
static inline void zbus_chan_set_msg_sub_pool(const struct zbus_channel *chan,
705+
struct net_buf_pool *pool)
706+
{
707+
__ASSERT(chan != NULL, "chan is required");
708+
__ASSERT(pool != NULL, "pool is required");
709+
710+
chan->data->msg_subscriber_pool = pool;
711+
}
712+
713+
#endif /* ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION */
714+
687715
#if defined(CONFIG_ZBUS_RUNTIME_OBSERVERS) || defined(__DOXYGEN__)
688716

689717
/**

subsys/zbus/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ config ZBUS_MSG_SUBSCRIBER_BUF_ALLOC_STATIC
3737

3838
endchoice
3939

40+
config ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION
41+
default n
42+
bool "Use isolated pools instead of only using the global pool."
43+
4044
config ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_SIZE
4145
default 16
4246
int "The count of net_buf available to be used simutaneously."

subsys/zbus/zbus.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ static struct k_spinlock obs_slock;
2525

2626
NET_BUF_POOL_HEAP_DEFINE(_zbus_msg_subscribers_pool, CONFIG_ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_SIZE,
2727
sizeof(struct zbus_channel *), NULL);
28+
2829
BUILD_ASSERT(K_HEAP_MEM_POOL_SIZE > 0, "MSG_SUBSCRIBER feature requires heap memory pool.");
2930

3031
static inline struct net_buf *_zbus_create_net_buf(struct net_buf_pool *pool, size_t size,
3132
k_timeout_t timeout)
3233
{
33-
return net_buf_alloc_len(&_zbus_msg_subscribers_pool, size, timeout);
34+
return net_buf_alloc_len(pool, size, timeout);
3435
}
3536

3637
#else
@@ -47,7 +48,7 @@ static inline struct net_buf *_zbus_create_net_buf(struct net_buf_pool *pool, si
4748
"CONFIG_ZBUS_MSG_SUBSCRIBER_NET_BUF_STATIC_DATA_SIZE must be greater or equal to "
4849
"%d",
4950
(int)size);
50-
return net_buf_alloc(&_zbus_msg_subscribers_pool, timeout);
51+
return net_buf_alloc(pool, timeout);
5152
}
5253
#endif /* CONFIG_ZBUS_MSG_SUBSCRIBER_BUF_ALLOC_DYNAMIC */
5354

@@ -129,8 +130,11 @@ static inline int _zbus_vded_exec(const struct zbus_channel *chan, k_timepoint_t
129130
struct zbus_channel_observation_mask *observation_mask;
130131

131132
#if defined(CONFIG_ZBUS_MSG_SUBSCRIBER)
132-
buf = _zbus_create_net_buf(&_zbus_msg_subscribers_pool, zbus_chan_msg_size(chan),
133-
sys_timepoint_timeout(end_time));
133+
struct net_buf_pool *pool =
134+
COND_CODE_1(CONFIG_ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_ISOLATION,
135+
(chan->data->msg_subscriber_pool), (&_zbus_msg_subscribers_pool));
136+
137+
buf = _zbus_create_net_buf(pool, zbus_chan_msg_size(chan), sys_timepoint_timeout(end_time));
134138

135139
_ZBUS_ASSERT(buf != NULL, "net_buf zbus_msg_subscribers_pool is "
136140
"unavailable or heap is full");
@@ -179,7 +183,6 @@ static inline int _zbus_vded_exec(const struct zbus_channel *chan, k_timepoint_t
179183
struct zbus_observer_node *obs_nd, *tmp;
180184

181185
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&chan->data->observers, obs_nd, tmp, node) {
182-
183186
const struct zbus_observer *obs = obs_nd->obs;
184187

185188
if (!obs->data->enabled) {

0 commit comments

Comments
 (0)