Skip to content

Commit d0a2e25

Browse files
committed
samples: bench: Create benchmark sample
This runs various sync primitives in a basic benchmark. This is useful to compare performance between native threads and work-queue based async. Initial results suggest that, unexpectedly, there is additional overhead with async. Signed-off-by: David Brown <[email protected]>
1 parent 53c8b9c commit d0a2e25

File tree

9 files changed

+696
-0
lines changed

9 files changed

+696
-0
lines changed

samples/bench/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(bench)
7+
8+
rust_cargo_application()

samples/bench/Cargo.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
[package]
5+
# This must be rustapp for now.
6+
name = "rustapp"
7+
version = "0.1.0"
8+
edition = "2021"
9+
description = "A sample hello world application in Rust"
10+
license = "Apache-2.0 or MIT"
11+
12+
[lib]
13+
crate-type = ["staticlib"]
14+
15+
[dependencies]
16+
zephyr = "0.1.0"
17+
18+
# Dependencies that are used by build.rs.
19+
[build-dependencies]
20+
zephyr-build = "0.1.0"
21+
22+
[profile.release]
23+
debug-assertions = true
24+
overflow-checks = true
25+
debug = true

samples/bench/Kconfig

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
mainmenu "Rust Dining Philosphers"
5+
6+
source "Kconfig.zephyr"
7+
8+
choice
9+
prompt "Select Synchronization implementation"
10+
default SYNC_CHANNEL
11+
12+
config SYNC_SYS_SEMAPHORE
13+
bool "Use sys::Semaphore to synchronize forks"
14+
help
15+
Use to have the dining philosophers sample use sys::Semaphore, with one per fork, to
16+
synchronize.
17+
18+
config SYNC_SYS_DYNAMIC_SEMAPHORE
19+
bool "Use a dynamic sys::Semaphore to synchronize forks"
20+
help
21+
Use to have the dining philosophers sample use sys::Semaphore, with one per fork, to
22+
synchronize. The Semaphores will be dynamically allocated.
23+
24+
config SYNC_SYS_MUTEX
25+
bool "Use sys::Semaphore to synchronize forks"
26+
help
27+
Use to have the dining philosophers sample use sys::Mutex, with one per fork, to
28+
synchronize.
29+
30+
config SYNC_CONDVAR
31+
bool "Use sync::Condvar and sync::Mutex to synchronize forks"
32+
help
33+
Use to have the dining philosophers sample use a single data structure, protected
34+
by a sync::Mutex and coordinated with a sync::Condvar, to synchronize.
35+
36+
config SYNC_CHANNEL
37+
bool "Use sync::channel to synchronize forks"
38+
help
39+
Use to have the dining philosophers sample use a worker thread, communicating via
40+
channels to synchronize.
41+
42+
endchoice
43+
44+
if SYNC_CHANNEL
45+
config USE_BOUNDED_CHANNELS
46+
bool "Should channel sync use bounded channels?"
47+
default y
48+
help
49+
If set, the channel-based communication will use bounded channels with bounds calculated
50+
to not ever block.
51+
endif
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# This board doesn't have a serial console, so use RTT.
5+
CONFIG_UART_CONSOLE=n
6+
CONFIG_RTT_CONSOLE=y
7+
CONFIG_USE_SEGGER_RTT=y

samples/bench/boards/rpi_pico.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# This board doesn't have a serial console, so use RTT.
5+
CONFIG_UART_CONSOLE=n
6+
CONFIG_RTT_CONSOLE=y
7+
CONFIG_USE_SEGGER_RTT=y

samples/bench/build.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2023 Linaro LTD
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// This crate needs access to kconfig variables. This is an example of how to do that. The
5+
// zephyr-build must be a build dependency.
6+
7+
fn main() {
8+
zephyr_build::export_bool_kconfig();
9+
}

samples/bench/prj.conf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_RUST=y
5+
CONFIG_RUST_ALLOC=y
6+
CONFIG_MAIN_STACK_SIZE=8192
7+
8+
CONFIG_POLL=y
9+
10+
# CONFIG_USERSPACE=y
11+
12+
# Some debugging
13+
CONFIG_THREAD_MONITOR=y
14+
CONFIG_THREAD_ANALYZER=y
15+
CONFIG_THREAD_ANALYZER_USE_PRINTK=y
16+
CONFIG_THREAD_ANALYZER_AUTO=n
17+
# CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=15

samples/bench/sample.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
sample:
2+
description: Philosphers, in Rust
3+
name: philosophers rust
4+
common:
5+
harness: console
6+
harness_config:
7+
type: one_line
8+
regex:
9+
# Match the statistics, and make sure that each philosopher has at least 10 (two digits)
10+
# meals.
11+
- "c:\\[\\d{2,}, \\d{2,}, \\d{2,}, \\d{2,}, \\d{2,}, \\d{2,}\\]"
12+
tags: rust
13+
filter: CONFIG_RUST_SUPPORTED
14+
tests:
15+
sample.rust.philosopher.semaphore:
16+
tags: introduction
17+
min_ram: 32
18+
extra_configs:
19+
- CONFIG_SYNC_SYS_SEMAPHORE=y
20+
sample.rust.philosopher.dynsemaphore:
21+
tags: introduction
22+
min_ram: 32
23+
extra_configs:
24+
- CONFIG_SYNC_SYS_DYNAMIC_SEMAPHORE=y
25+
sample.rust.philosopher.sysmutex:
26+
tags: introduction
27+
min_ram: 32
28+
extra_configs:
29+
- CONFIG_SYNC_SYS_MUTEX=y
30+
sample.rust.philosopher.condvar:
31+
tags: introduction
32+
min_ram: 32
33+
extra_configs:
34+
- CONFIG_SYNC_CONDVAR=y
35+
sample.rust.philosopher.channel_bounded:
36+
tags: introduction
37+
min_ram: 32
38+
extra_configs:
39+
- CONFIG_SYNC_CHANNEL=y
40+
- CONFIG_USE_BOUNDED_CHANNELS=y
41+
sample.rust.philosopher.channel_unbounded:
42+
tags: introduction
43+
min_ram: 32
44+
extra_configs:
45+
- CONFIG_SYNC_CHANNEL=y
46+
- CONFIG_USE_BOUNDED_CHANNELS=n

0 commit comments

Comments
 (0)