Skip to content

Commit d1b8f38

Browse files
committed
samples: subsys: zbus: benchmark: use native posix host rtc for ARCH_POSIX
Because posix emulated execution is significantly faster than posix emulated timer and benchmark finishes before even the first tick occurs. Signed-off-by: Alp Sayin <alp.sayin@amd.com>
1 parent 925c865 commit d1b8f38

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

samples/subsys/zbus/benchmark/src/benchmark.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
#include <zephyr/logging/log.h>
1212
#include <zephyr/sys/util_macro.h>
1313
#include <zephyr/zbus/zbus.h>
14+
15+
#if defined(CONFIG_ARCH_POSIX)
16+
#include "native_rtc.h"
17+
#define GET_ARCH_TIME_NS() (native_rtc_gettime_us(RTC_CLOCK_PSEUDOHOSTREALTIME) * NSEC_PER_USEC)
18+
#else
19+
#define GET_ARCH_TIME_NS() (k_cyc_to_ns_near32(sys_clock_cycle_get_32()))
20+
#endif
21+
1422
LOG_MODULE_DECLARE(zbus, CONFIG_ZBUS_LOG_LEVEL);
1523

1624
#define CONSUMER_STACK_SIZE (CONFIG_IDLE_STACK_SIZE + CONFIG_BM_MESSAGE_SIZE)
@@ -185,7 +193,7 @@ static void producer_thread(void)
185193

186194
zbus_chan_finish(&bm_channel);
187195

188-
uint32_t start_ns = k_cyc_to_ns_near32(sys_clock_cycle_get_32());
196+
uint32_t start_ns = GET_ARCH_TIME_NS();
189197

190198
for (uint64_t internal_count = BYTES_TO_BE_SENT / CONFIG_BM_ONE_TO; internal_count > 0;
191199
internal_count -= CONFIG_BM_MESSAGE_SIZE) {
@@ -200,7 +208,7 @@ static void producer_thread(void)
200208
zbus_chan_notify(&bm_channel, K_MSEC(200));
201209
}
202210

203-
uint32_t end_ns = k_cyc_to_ns_near32(sys_clock_cycle_get_32());
211+
uint32_t end_ns = GET_ARCH_TIME_NS();
204212

205213
uint32_t duration = end_ns - start_ns;
206214

@@ -218,4 +226,5 @@ static void producer_thread(void)
218226
printk("\n@%u\n", duration);
219227
}
220228

221-
K_THREAD_DEFINE(producer_thread_id, PRODUCER_STACK_SIZE, producer_thread, NULL, NULL, NULL, 5, 0, 0);
229+
K_THREAD_DEFINE(producer_thread_id, PRODUCER_STACK_SIZE, producer_thread,
230+
NULL, NULL, NULL, 5, 0, 0);

0 commit comments

Comments
 (0)