From d705f740ee1369df9e0686c076a1b2d1ed26f54a Mon Sep 17 00:00:00 2001 From: Evgeniy Paltsev Date: Tue, 24 Jan 2023 19:31:41 +0000 Subject: [PATCH] samples/subsys/zbus/benchmark: fix counter synchronization issue Current implementation has obvious synchronization issue with global counter variable which is updated (RMW) in multiple threads without any locking. Replace regular variable by atomic type. As the maximum possible value is much less than INT32_MAX it's OK to replace the original uint64_t by (32-bit) atomic_t. Signed-off-by: Eugeniy Paltsev Signed-off-by: Evgeniy Paltsev --- samples/subsys/zbus/benchmark/src/benchmark.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/samples/subsys/zbus/benchmark/src/benchmark.c b/samples/subsys/zbus/benchmark/src/benchmark.c index a322225bb1a..dfa2be2d306 100644 --- a/samples/subsys/zbus/benchmark/src/benchmark.c +++ b/samples/subsys/zbus/benchmark/src/benchmark.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #if defined(CONFIG_ARCH_POSIX) @@ -52,7 +53,7 @@ ZBUS_CHAN_DEFINE(bm_channel, /* Name */ ); #define BYTES_TO_BE_SENT (256LLU * 1024LLU) -static uint64_t count; +static atomic_t count; #if (CONFIG_BM_ASYNC == 1) ZBUS_SUBSCRIBER_DEFINE(s1, 4); @@ -98,7 +99,7 @@ ZBUS_SUBSCRIBER_DEFINE(s16, 4); \ zbus_chan_finish(chan); \ \ - count += CONFIG_BM_MESSAGE_SIZE; \ + atomic_add(&count, CONFIG_BM_MESSAGE_SIZE); \ } \ } \ \ @@ -219,7 +220,7 @@ static void producer_thread(void) uint64_t i = ((BYTES_TO_BE_SENT * NSEC_PER_SEC) / MB(1)) / duration; uint64_t f = ((BYTES_TO_BE_SENT * NSEC_PER_SEC * 100) / MB(1) / duration) % 100; - LOG_INF("Bytes sent = %lld, received = %llu", BYTES_TO_BE_SENT, count); + LOG_INF("Bytes sent = %lld, received = %lu", BYTES_TO_BE_SENT, atomic_get(&count)); LOG_INF("Average data rate: %llu.%lluMB/s", i, f); LOG_INF("Duration: %u.%uus", duration / NSEC_PER_USEC, duration % NSEC_PER_USEC);