Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Zbus Benchmark Sample is smashing the stack and is too fast #53563

Merged
merged 4 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions samples/subsys/zbus/benchmark/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ tests:
regex:
- "I: Benchmark 1 to 8: Dynamic memory, ASYNC transmission and message size 256"
- "I: Bytes sent = 262144, received = 262144"
- "I: Average data rate: (.*)B/s"
- "I: Duration: (.*)ms"
- "I: Average data rate: (\\d+).(\\d+)MB/s"
- "I: Duration: (\\d+).(\\d+)us"
- "@(.*)"
extra_configs:
- CONFIG_BM_ONE_TO=8
- CONFIG_BM_MESSAGE_SIZE=256
- CONFIG_BM_ASYNC=y
platform_exclude: qemu_x86 qemu_x86_64 qemu_riscv32_smp native_posix native_posix_64
qemu_riscv32_smp qemu_cortex_a53_smp qemu_riscv64_smp qemu_leon3 qemu_xtensa qemu_cortex_a53
qemu_riscv32 qemu_malta qemu_malta_be qemu_arc_hs6x qemu_riscv64 m2gl025_miv hifive_unleashed
sample.zbus.benchmark_sync:
tags: zbus
min_ram: 16
Expand All @@ -33,13 +30,10 @@ tests:
regex:
- "I: Benchmark 1 to 8: Dynamic memory, SYNC transmission and message size 256"
- "I: Bytes sent = 262144, received = 262144"
- "I: Average data rate: (.*)B/s"
- "I: Duration: (.*)ms"
- "I: Average data rate: (\\d+).(\\d+)MB/s"
- "I: Duration: (\\d+).(\\d+)us"
- "@(.*)"
extra_configs:
- CONFIG_BM_ONE_TO=8
- CONFIG_BM_MESSAGE_SIZE=256
- CONFIG_BM_ASYNC=n
platform_exclude: qemu_x86 qemu_x86_64 qemu_riscv32_smp native_posix native_posix_64
qemu_riscv32_smp qemu_cortex_a53_smp qemu_riscv64_smp qemu_leon3 qemu_xtensa qemu_cortex_a53
qemu_riscv32 m2gl025_miv m2gl025_miv
32 changes: 23 additions & 9 deletions samples/subsys/zbus/benchmark/src/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@
#include <zephyr/logging/log.h>
#include <zephyr/sys/util_macro.h>
#include <zephyr/zbus/zbus.h>

#if defined(CONFIG_ARCH_POSIX)
#include "native_rtc.h"
#define GET_ARCH_TIME_NS() (native_rtc_gettime_us(RTC_CLOCK_PSEUDOHOSTREALTIME) * NSEC_PER_USEC)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cfriedt Could you please give us your opinion about line 17? Is this a good practice for native_posix?

#else
#define GET_ARCH_TIME_NS() (k_cyc_to_ns_near32(sys_clock_cycle_get_32()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was seeing issues on the ARC QEMU platform where the tick count runs at 100Hz; does this API provide a higher resolution value for that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems so

-- west build: running target run_qemu
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: arcem
*** Booting Zephyr OS build zephyr-v3.2.0-3158-g7bb6557ba710 ***
I: Benchmark 1 to 8: Dynamic memory, SYNC transmission and message size 256
I: Bytes sent = 262144, received = 262144
I: Average data rate: 2.11MB/s
I: Duration: 118061.400us

@118061400
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: archs
*** Booting Zephyr OS build zephyr-v3.2.0-3158-g7bb6557ba710 ***
I: Benchmark 1 to 8: Dynamic memory, SYNC transmission and message size 256
I: Bytes sent = 262144, received = 262144
I: Average data rate: 2.12MB/s
I: Duration: 117709.400us

@117709400
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: hs6x
*** Booting Zephyr OS build zephyr-v3.2.0-3158-g7bb6557ba710 ***
I: Benchmark 1 to 8: Dynamic memory, SYNC transmission and message size 256
I: Bytes sent = 262144, received = 262144
I: Average data rate: 3.12MB/s
I: Duration: 80011.600us

@80011600

#endif

LOG_MODULE_DECLARE(zbus, CONFIG_ZBUS_LOG_LEVEL);

#define CONSUMER_STACK_SIZE (CONFIG_IDLE_STACK_SIZE + CONFIG_BM_MESSAGE_SIZE)
#define PRODUCER_STACK_SIZE (CONFIG_MAIN_STACK_SIZE + CONFIG_BM_MESSAGE_SIZE)

ZBUS_CHAN_DEFINE(bm_channel, /* Name */
struct external_data_msg, /* Message type */

Expand Down Expand Up @@ -91,8 +102,7 @@ ZBUS_SUBSCRIBER_DEFINE(s16, 4);
} \
} \
\
K_THREAD_DEFINE(name##_id, CONFIG_BM_MESSAGE_SIZE + 196, name##_task, NULL, NULL, NULL, 3, \
0, 0);
K_THREAD_DEFINE(name##_id, CONSUMER_STACK_SIZE, name##_task, NULL, NULL, NULL, 3, 0, 0);

S_TASK(s1)
#if (CONFIG_BM_ONE_TO >= 2LLU)
Expand Down Expand Up @@ -183,7 +193,7 @@ static void producer_thread(void)

zbus_chan_finish(&bm_channel);

uint32_t start = k_uptime_get_32();
uint32_t start_ns = GET_ARCH_TIME_NS();

for (uint64_t internal_count = BYTES_TO_BE_SENT / CONFIG_BM_ONE_TO; internal_count > 0;
internal_count -= CONFIG_BM_MESSAGE_SIZE) {
Expand All @@ -197,20 +207,24 @@ static void producer_thread(void)

zbus_chan_notify(&bm_channel, K_MSEC(200));
}
uint32_t duration = (k_uptime_get_32() - start);

uint32_t end_ns = GET_ARCH_TIME_NS();

uint32_t duration = end_ns - start_ns;

if (duration == 0) {
LOG_ERR("Something wrong. Duration is zero!\n");
k_oops();
}
uint64_t i = (BYTES_TO_BE_SENT * 1000) / duration;
uint64_t f = ((BYTES_TO_BE_SENT * 100000) / duration) % 100;
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("Average data rate: %llu.%lluB/s", i, f);
LOG_INF("Duration: %ums", duration);
LOG_INF("Average data rate: %llu.%lluMB/s", i, f);
LOG_INF("Duration: %u.%uus", duration / NSEC_PER_USEC, duration % NSEC_PER_USEC);

printk("\n@%u\n", duration);
}

K_THREAD_DEFINE(producer_thread_id, 1024, producer_thread, NULL, NULL, NULL, 5, 0, 0);
K_THREAD_DEFINE(producer_thread_id, PRODUCER_STACK_SIZE, producer_thread,
NULL, NULL, NULL, 5, 0, 0);