samples: zbus: fix variable sizes and simplify benchmark sample

The benchmark sample was not suitable for constrained devices.
With u32, the maximum accumulated time was about 4 seconds.
Some devices take more than 4 seconds to finish the benchmark.
This commit fixes the time counting by replacing the variables
and calls to 64 bits and removes all the dynamic allocation
memory on the sample to keep that as simple as possible. It
adjusts the documentation and the tests as well.

Signed-off-by: Rodrigo Peixoto <rodrigopex@gmail.com>
This commit is contained in:
Rodrigo Peixoto 2023-04-03 07:57:53 -03:00 committed by Maureen Helm
parent 938c59c063
commit a24c307baa
5 changed files with 103 additions and 133 deletions

View file

@ -12,13 +12,13 @@ Building and Running
:zephyr-app: samples/subsys/zbus/dyn_channel
:host-os: unix
:board: qemu_cortex_m3
:gen-args: -DCONFIG_BM_MESSAGE_SIZE=1 -DCONFIG_BM_ONE_TO=1 -DCONFIG_BM_ASYNC=0
:gen-args: -DCONFIG_BM_MESSAGE_SIZE=1 -DCONFIG_BM_ONE_TO=1 -DCONFIG_BM_ASYNC=y
:goals: build run
Notice we have the following parameters:
* **CONFIG_BM_MESSAGE_SIZE** the size of the message to be transferred;
* **CONFIG_BM_ONE_TO** number of consumers to send;
* **CONFIG_BM_MESSAGE_SIZE** the size of the message to be transferred (1, 2, 4, 8, 16, 32, 64, 128, or 256);
* **CONFIG_BM_ONE_TO** number of consumers to send (1, 2, 4, or 8);
* **CONFIG_BM_ASYNC** if the execution must be asynchronous or synchronous. Use y to async and n to sync;
Sample Output
@ -27,23 +27,23 @@ The result would be something like:
.. code-block:: console
*** Booting Zephyr OS build zephyr-v3.2.0 ***
I: Benchmark 1 to 8: Dynamic memory, ASYNC transmission and message size 256
*** Booting Zephyr OS build zephyr-v3.3.0 ***
I: Benchmark 1 to 1: Dynamic memory, SYNC transmission and message size 1
I: Bytes sent = 262144, received = 262144
I: Average data rate: 1872457.14B/s
I: Duration: 140ms
I: Average data rate: 0.6MB/s
I: Duration: 4.72020167s
@140
@4072020167
Running the benchmark automatically
===================================
There is a Robot script called ``benchmark_256KB.robot`` which runs all the input combinations as the complete benchmark.
The resulting file, ''zbus_dyn_benchmark_256KB.csv`` is generated in the project root folder. It takes a long time to execute. In the CSV file, we have the following columns:
There is a `Robot framework <https://robotframework.org/>`_ script called ``benchmark_256KB.robot`` which runs all the input combinations as the complete benchmark.
The resulting file, ``zbus_dyn_benchmark_256KB.csv`` is generated in the project root folder. It takes a long time to execute. In the CSV file, we have the following columns:
+------------+---------------------+--------------------------+---------------+-------------+-------------+
| Style | Number of consumers | Message size (bytes) | Duration (ms) | RAM (bytes) | ROM (bytes) |
| Style | Number of consumers | Message size (bytes) | Duration (ns) | RAM (bytes) | ROM (bytes) |
+============+=====================+==========================+===============+=============+=============+
| SYNC/ASYNC | 1,2,4,8 | 1,2,4,8,16,32,64,128,256 | float | int | int |
+------------+---------------------+--------------------------+---------------+-------------+-------------+
@ -52,81 +52,81 @@ The complete benchmark command using Robot framework is:
.. code-block:: console
robot --variable serial_port:/dev/ttyACM0 -d /tmp/benchmark_out benchmark_256KB.robot
robot --variable serial_port:/dev/ttyACM0 --variable board:nrf52833dk_nrf52833 -d /tmp/benchmark_out benchmark_256KB.robot
An example of execution using the ``hifive1_revb`` board would generate a file like this:
An example of execution using the ``nrf52833dk_nrf52833`` board would generate a file like this:
.. code-block::
SYNC,1,1,8534.0,6856,17434
SYNC,1,2,4469.0,6856,17440
SYNC,1,4,2362.3333333333335,6856,17438
SYNC,1,8,1307.6666666666667,6864,17448
SYNC,1,16,768.6666666666666,6872,17478
SYNC,1,32,492.0,6888,17506
SYNC,1,64,391.0,6920,17540
SYNC,1,128,321.0,6984,17600
SYNC,1,256,258.0,7112,17758
SYNC,2,1,4856.666666666667,6880,17490
SYNC,2,2,2464.0,6880,17494
SYNC,2,4,1367.0,6880,17494
SYNC,2,8,778.6666666666666,6888,17504
SYNC,2,16,477.0,6896,17534
SYNC,2,32,321.0,6912,17562
SYNC,2,64,266.0,6944,17592
SYNC,2,128,203.0,7008,17662
SYNC,2,256,188.0,7136,17814
SYNC,4,1,3021.3333333333335,6920,17536
SYNC,4,2,1505.3333333333333,6920,17542
SYNC,4,4,860.0,6920,17542
SYNC,4,8,521.3333333333334,6928,17552
SYNC,4,16,328.0,6936,17582
SYNC,4,32,227.0,6952,17606
SYNC,4,64,180.0,6984,17646
SYNC,4,128,164.0,7048,17710
SYNC,4,256,149.0,7176,17854
SYNC,8,1,2044.3333333333333,7000,17632
SYNC,8,2,1002.6666666666666,7000,17638
SYNC,8,4,586.0,7000,17638
SYNC,8,8,383.0,7008,17648
SYNC,8,16,250.0,7016,17674
SYNC,8,32,203.0,7032,17708
SYNC,8,64,156.0,7064,17742
SYNC,8,128,141.0,7128,17806
SYNC,8,256,133.0,7256,17958
ASYNC,1,1,22187.666666666668,7312,17776
ASYNC,1,2,11424.666666666666,7312,17782
ASYNC,1,4,5823.0,7312,17778
ASYNC,1,8,3071.0,7312,17790
ASYNC,1,16,1625.0,7328,17832
ASYNC,1,32,966.3333333333334,7344,17862
ASYNC,1,64,578.0,7376,17896
ASYNC,1,128,403.6666666666667,7440,17956
ASYNC,1,256,352.0,7568,18126
ASYNC,2,1,18547.333333333332,7792,18030
ASYNC,2,2,9563.0,7792,18034
ASYNC,2,4,4831.0,7792,18030
ASYNC,2,8,2497.3333333333335,7792,18044
ASYNC,2,16,1377.6666666666667,7824,18098
ASYNC,2,32,747.3333333333334,7856,18132
ASYNC,2,64,492.0,7920,18162
ASYNC,2,128,321.0,8048,18232
ASYNC,2,256,239.33333333333334,8304,18408
ASYNC,4,1,16823.0,8744,18474
ASYNC,4,2,8604.333333333334,8744,18480
ASYNC,4,4,4325.666666666667,8744,18472
ASYNC,4,8,2258.0,8744,18490
ASYNC,4,16,1198.3333333333333,8808,18572
ASYNC,4,32,696.0,8872,18610
ASYNC,4,64,430.0,9000,18650
ASYNC,4,128,289.0,9256,18714
ASYNC,4,256,227.0,9768,18906
ASYNC,8,1,15976.666666666666,10648,19366
ASYNC,8,2,7929.666666666667,10648,19372
ASYNC,8,4,4070.6666666666665,10648,19356
ASYNC,8,8,2158.6666666666665,10648,19382
ASYNC,8,16,1119.6666666666667,10776,19506
ASYNC,8,32,619.6666666666666,10904,19566
ASYNC,8,64,391.0,11160,19600
ASYNC,8,128,273.0,11672,19686
ASYNC,8,256,211.0,12696,19934
SYNC,1,1,2312815348.3333335,7286,23752
SYNC,1,2,1172444661.3333333,7287,23760
SYNC,1,4,602284749.6666666,7289,23768
SYNC,1,8,323750814.0,7293,23772
SYNC,1,16,175120035.66666666,7301,23776
SYNC,1,32,103942871.33333333,7317,23776
SYNC,1,64,68318685.0,7349,23776
SYNC,1,128,50567627.333333336,7477,23776
SYNC,1,256,41656494.0,7733,23776
SYNC,2,1,1277842204.3333333,7298,23768
SYNC,2,2,647094726.6666666,7299,23776
SYNC,2,4,329559326.3333333,7301,23784
SYNC,2,8,170979817.66666666,7305,23796
SYNC,2,16,95174153.66666667,7313,23792
SYNC,2,32,55786133.0,7329,23792
SYNC,2,64,36173502.333333336,7361,23792
SYNC,2,128,26326497.666666668,7489,23792
SYNC,2,256,21280924.333333332,7745,23792
SYNC,4,1,745513916.0,7322,23800
SYNC,4,2,374755859.6666667,7323,23808
SYNC,4,4,191497802.66666666,7325,23816
SYNC,4,8,101399739.66666667,7329,23820
SYNC,4,16,54026286.0,7337,23824
SYNC,4,32,31097412.0,7353,23824
SYNC,4,64,19643148.333333332,7385,23824
SYNC,4,128,13936360.333333334,7513,23824
SYNC,4,256,11047363.333333334,7769,23824
SYNC,8,1,477518717.3333333,7370,23864
SYNC,8,2,240773518.66666666,7371,23872
SYNC,8,4,121897379.33333333,7373,23880
SYNC,8,8,64015706.333333336,7377,23884
SYNC,8,16,33681234.0,7385,23888
SYNC,8,32,18880208.333333332,7401,23888
SYNC,8,64,11505127.0,7433,23888
SYNC,8,128,7781982.333333333,7561,23888
SYNC,8,256,5940755.333333333,7817,23888
ASYNC,1,1,9422749837.333334,7962,24108
ASYNC,1,2,4728759765.333333,7963,24116
ASYNC,1,4,2380554199.3333335,7965,24124
ASYNC,1,8,1225118001.6666667,7969,24128
ASYNC,1,16,618764241.6666666,7977,24132
ASYNC,1,32,326253255.3333333,7993,24132
ASYNC,1,64,179473876.66666666,8025,24132
ASYNC,1,128,106170654.33333333,8217,24132
ASYNC,1,256,69386800.33333333,8601,24136
ASYNC,2,1,8347330729.0,8650,24288
ASYNC,2,2,4186747233.3333335,8651,24296
ASYNC,2,4,2092895507.3333333,8653,24304
ASYNC,2,8,1049245198.6666666,8657,24316
ASYNC,2,16,541544596.6666666,8665,24312
ASYNC,2,32,281127929.6666667,8681,24312
ASYNC,2,64,150746663.66666666,8713,24312
ASYNC,2,128,85662842.0,8969,24312
ASYNC,2,256,48909505.0,9481,24320
ASYNC,4,1,7854085286.666667,10026,24652
ASYNC,4,2,3935852050.3333335,10027,24660
ASYNC,4,4,1972869873.0,10029,24668
ASYNC,4,8,979451497.6666666,10033,24672
ASYNC,4,16,499348958.0,10041,24676
ASYNC,4,32,253712972.0,10057,24676
ASYNC,4,64,131022135.33333333,10089,24676
ASYNC,4,128,69610595.66666667,10473,24676
ASYNC,4,256,38706461.666666664,11241,24692
ASYNC,8,1,7590311686.666667,12778,25220
ASYNC,8,2,3800333658.6666665,12779,25228
ASYNC,8,4,1900014241.6666667,12781,25236
ASYNC,8,8,940419515.0,12785,25240
ASYNC,8,16,478739420.6666667,12793,25244
ASYNC,8,32,241465250.66666666,12809,25244
ASYNC,8,64,122701009.0,12841,25244
ASYNC,8,128,63405355.0,13481,25244
ASYNC,8,256,33752441.666666664,14761,25244

View file

@ -1,6 +1,5 @@
CONFIG_LOG=y
CONFIG_LOG_MODE_MINIMAL=y
CONFIG_HEAP_MEM_POOL_SIZE=1024
CONFIG_ASSERT=n
CONFIG_ZBUS=y
CONFIG_ZBUS_LOG_LEVEL_INF=y

View file

@ -13,7 +13,7 @@ tests:
- "I: Benchmark 1 to 8: Dynamic memory, ASYNC transmission and message size 256"
- "I: Bytes sent = 262144, received = 262144"
- "I: Average data rate: (\\d+).(\\d+)MB/s"
- "I: Duration: (\\d+).(\\d+)us"
- "I: Duration: (\\d+).(\\d+)s"
- "@(.*)"
extra_configs:
- CONFIG_BM_ONE_TO=8
@ -34,7 +34,7 @@ tests:
- "I: Benchmark 1 to 8: Dynamic memory, SYNC transmission and message size 256"
- "I: Bytes sent = 262144, received = 262144"
- "I: Average data rate: (\\d+).(\\d+)MB/s"
- "I: Duration: (\\d+).(\\d+)us"
- "I: Duration: (\\d+).(\\d+)s"
- "@(.*)"
extra_configs:
- CONFIG_BM_ONE_TO=8

View file

@ -19,7 +19,7 @@
#elif defined(CONFIG_ARCH_POSIX)
#error "This sample cannot be built for other POSIX arch boards than native_posix"
#else
#define GET_ARCH_TIME_NS() (k_cyc_to_ns_near32(sys_clock_cycle_get_32()))
#define GET_ARCH_TIME_NS() (k_cyc_to_ns_near64(sys_clock_cycle_get_32()))
#endif
LOG_MODULE_DECLARE(zbus, CONFIG_ZBUS_LOG_LEVEL);
@ -27,8 +27,8 @@ 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 */
ZBUS_CHAN_DEFINE(bm_channel, /* Name */
struct bm_msg, /* Message type */
NULL, /* Validator */
NULL, /* User data */
@ -86,18 +86,13 @@ ZBUS_SUBSCRIBER_DEFINE(s16, 4);
#define S_TASK(name) \
void name##_task(void) \
{ \
struct external_data_msg *actual_message_data; \
const struct zbus_channel *chan; \
struct bm_msg msg_received; \
struct bm_msg *msg_received; \
\
while (!zbus_sub_wait(&name, &chan, K_FOREVER)) { \
zbus_chan_claim(chan, K_NO_WAIT); \
\
actual_message_data = zbus_chan_msg(chan); \
__ASSERT_NO_MSG(actual_message_data->reference != NULL); \
\
memcpy(&msg_received, actual_message_data->reference, \
sizeof(struct bm_msg)); \
msg_received = zbus_chan_msg(chan); \
\
zbus_chan_finish(chan); \
\
@ -164,10 +159,10 @@ ZBUS_LISTENER_DEFINE(s16, s_cb);
static void s_cb(const struct zbus_channel *chan)
{
struct bm_msg msg_received;
const struct external_data_msg *actual_message_data = zbus_chan_const_msg(chan);
const struct bm_msg *actual_message_data = zbus_chan_const_msg(chan);
memcpy(&msg_received, actual_message_data->reference, sizeof(struct bm_msg));
/* It only illustrates the message is ready to be consumed */
ARG_UNUSED(actual_message_data);
count += CONFIG_BM_MESSAGE_SIZE;
}
@ -180,40 +175,21 @@ static void producer_thread(void)
CONFIG_BM_ONE_TO, IS_ENABLED(CONFIG_BM_ASYNC) ? "A" : "", CONFIG_BM_MESSAGE_SIZE);
struct bm_msg msg;
struct external_data_msg *actual_message_data;
for (uint64_t i = (CONFIG_BM_MESSAGE_SIZE - 1); i > 0; --i) {
msg.bytes[i] = i;
}
zbus_chan_claim(&bm_channel, K_NO_WAIT);
actual_message_data = zbus_chan_msg(&bm_channel);
actual_message_data->reference = k_malloc(sizeof(struct bm_msg));
__ASSERT_NO_MSG(actual_message_data->reference != NULL);
actual_message_data->size = sizeof(struct bm_msg);
__ASSERT_NO_MSG(actual_message_data->size > 0);
zbus_chan_finish(&bm_channel);
uint32_t start_ns = GET_ARCH_TIME_NS();
uint64_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) {
zbus_chan_claim(&bm_channel, K_NO_WAIT);
actual_message_data = zbus_chan_msg(&bm_channel);
memcpy(actual_message_data->reference, &msg, CONFIG_BM_MESSAGE_SIZE);
zbus_chan_finish(&bm_channel);
zbus_chan_notify(&bm_channel, K_MSEC(200));
zbus_chan_pub(&bm_channel, &msg, K_MSEC(200));
}
uint32_t end_ns = GET_ARCH_TIME_NS();
uint64_t end_ns = GET_ARCH_TIME_NS();
uint32_t duration = end_ns - start_ns;
uint64_t duration = end_ns - start_ns;
if (duration == 0) {
LOG_ERR("Something wrong. Duration is zero!\n");
@ -224,10 +200,10 @@ static void producer_thread(void)
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);
LOG_INF("Duration: %llu.%llus", duration / NSEC_PER_SEC, duration % NSEC_PER_SEC);
printk("\n@%u\n", duration);
printk("\n@%llu\n", duration);
}
K_THREAD_DEFINE(producer_thread_id, PRODUCER_STACK_SIZE, 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);

View file

@ -7,11 +7,6 @@
#include <stddef.h>
#include <stdint.h>
struct external_data_msg {
void *reference;
size_t size;
};
struct bm_msg {
uint8_t bytes[CONFIG_BM_MESSAGE_SIZE];
};