Skip to content

Commit 1fd71ca

Browse files
nordic-krchnashif
authored andcommitted
lib: os: mpsc_pbuf: Add optional debug features
Added optional debug prints. Logging cannot be used because mpsc pbuf is used by the logging. Added option to clear packet memory after allocation. Option is enabled in Kconfig. Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
1 parent 3a765f4 commit 1fd71ca

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/os/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ config MPSC_PBUF
6868
storing variable length packets in a circular way and operate directly
6969
on the buffer memory.
7070

71+
if MPSC_PBUF
72+
config MPSC_CLEAR_ALLOCATED
73+
bool "Clear allocated packet"
74+
help
75+
When enabled packet space is zeroed before returning from allocation.
76+
endif
77+
7178
rsource "Kconfig.cbprintf"
7279

7380
endmenu

lib/os/mpsc_pbuf.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55
*/
66
#include <sys/mpsc_pbuf.h>
77

8+
#define MPSC_PBUF_DEBUG 0
9+
10+
#define MPSC_PBUF_DBG(buffer, ...) do { \
11+
if (MPSC_PBUF_DEBUG) { \
12+
printk(__VA_ARGS__); \
13+
if (buffer) { \
14+
mpsc_state_print(buffer); \
15+
} \
16+
} \
17+
} while (0)
18+
19+
static inline void mpsc_state_print(struct mpsc_pbuf_buffer *buffer)
20+
{
21+
if (MPSC_PBUF_DEBUG) {
22+
printk("wr:%d/%d, rd:%d/%d\n",
23+
buffer->wr_idx, buffer->tmp_wr_idx,
24+
buffer->rd_idx, buffer->tmp_rd_idx);
25+
}
26+
}
27+
828
void mpsc_pbuf_init(struct mpsc_pbuf_buffer *buffer,
929
const struct mpsc_pbuf_buffer_config *cfg)
1030
{
@@ -188,6 +208,7 @@ union mpsc_pbuf_generic *mpsc_pbuf_alloc(struct mpsc_pbuf_buffer *buffer,
188208
bool cont;
189209
uint32_t free_wlen;
190210

211+
MPSC_PBUF_DBG(buffer, "alloc %d words, ", (int)wlen);
191212
do {
192213
k_spinlock_key_t key;
193214
bool wrap;
@@ -230,6 +251,13 @@ union mpsc_pbuf_generic *mpsc_pbuf_alloc(struct mpsc_pbuf_buffer *buffer,
230251
}
231252
} while (cont);
232253

254+
MPSC_PBUF_DBG(buffer, "allocated %p ", item);
255+
256+
if (IS_ENABLED(CONFIG_MPSC_CLEAR_ALLOCATED) && item) {
257+
/* During test fill with 0's to simplify message comparison */
258+
memset(item, 0, sizeof(int) * wlen);
259+
}
260+
233261
return item;
234262
}
235263

@@ -243,6 +271,7 @@ void mpsc_pbuf_commit(struct mpsc_pbuf_buffer *buffer,
243271
item->hdr.valid = 1;
244272
buffer->wr_idx = idx_inc(buffer, buffer->wr_idx, wlen);
245273
k_spin_unlock(&buffer->lock, key);
274+
MPSC_PBUF_DBG(buffer, "committed %p ", item);
246275
}
247276

248277
void mpsc_pbuf_put_word_ext(struct mpsc_pbuf_buffer *buffer,
@@ -368,6 +397,9 @@ union mpsc_pbuf_generic *mpsc_pbuf_claim(struct mpsc_pbuf_buffer *buffer)
368397
}
369398
}
370399

400+
if (!cont) {
401+
MPSC_PBUF_DBG(buffer, "claimed: %p ", item);
402+
}
371403
k_spin_unlock(&buffer->lock, key);
372404
} while (cont);
373405

@@ -388,6 +420,7 @@ void mpsc_pbuf_free(struct mpsc_pbuf_buffer *buffer,
388420
} else {
389421
item->skip.len = wlen;
390422
}
423+
MPSC_PBUF_DBG(buffer, "freed: %p ", item);
391424

392425
k_spin_unlock(&buffer->lock, key);
393426
k_sem_give(&buffer->sem);

0 commit comments

Comments
 (0)