Kyron Runtime Component Architecture#

Kyron Runtime Component Architecture
status: draft
security: YES
safety: QM
version: 1
Kyron Runtime
status: valid
security: YES
safety: QM
bazel_target: @@//src/kyron:libkyron
bazel_type: rust_library
version: 1
coverage: lines 74% · branches 67% · files 52/66

Overview#

The Kyron runtime is a Rust async/await runtime. It is built from one or more execution engines. Each engine has its own pool of async workers, which use a work-stealing scheduler, and optional dedicated workers that keep a task on its thread. The runtime also provides a safety worker, an I/O event loop, TCP/UDP networking, timers and sleep, channels, select!, iceoryx2-based IPC, and the #[kyron::main] attribute macro.

Static Architecture#

Kyron Runtime Static View
status: valid
security: YES
safety: QM

The generated diagram above shows the interface operations of the executor core. The diagram below shows the parts of the component, one box per row of the table below, and their main dependencies. Dashed arrows are wake-ups through a task’s waker.

The runtime is structured into the following parts. Only the executor core is covered by component requirements so far, so only the executor core parts are linked to requirements. The other parts get their links when their requirements are added.

Part

Responsibility

Runtime (src/runtime)

RuntimeBuilder and Runtime: build a runtime from one or more execution engines, enter it with block_on and spawn tasks into a selected engine.

Execution engine (src/scheduler/execution_engine.rs)

ExecutionEngineBuilder and ExecutionEngine: own a fixed pool of async workers, the dedicated workers, and the optional safety worker of one engine.

Scheduler (src/scheduler/scheduler_mt.rs, src/scheduler/task)

Task representation, per-worker queues, work stealing between async workers, and routing of dedicated tasks to the queue of their dedicated worker.

Workers (src/scheduler/workers)

Async workers, dedicated workers and the safety worker. Each one is an OS thread that polls the tasks assigned to it.

I/O driver (src/io, src/mio)

Registers OS file descriptors (sockets, iceoryx2 event fds) with the OS poller (POSIX poll(2) on Linux and QNX) and wakes the tasks waiting for their readiness. A parked async worker waits in the poller, and busy async workers check it without waiting every 32 tasks, see I/O Wait and Wake (comp_arc_dyn__kyron_runtime__io_wait_wake). kyron::mio also exposes the non-blocking OS sockets that kyron::net is built on.

Networking (src/net)

TcpListener, TcpStream and UdpSocket: non-blocking sockets on top of the I/O driver.

IPC (src/ipc/iceoryx2)

Async iceoryx2 event Listener. Its file descriptor is registered with the I/O driver.

Time (src/time, src/futures/sleep.rs)

Clock, Instant, sleep and the timer wheel. Expired timers are processed when a worker wakes up from parking and by busy workers every 32 tasks. A parked worker usually uses the next timer expiry as its wait timeout.

Channels and select! (src/channels, src/macros)

Single-producer single-consumer and broadcast channels, and the select! macro.

Safety API and safety worker (src/safety.rs, src/scheduler/workers/safety_worker.rs)

kyron::safety::spawn*: tasks whose error result can be handled by the safety worker. The conditions are shown in Safety Worker Error Handling (comp_arc_dyn__kyron_runtime__safety_worker).

Futures and common types (src/futures, src/common)

ReusableBoxFuture and its pool (taken by spawn_from_reusable), yield_now, sleep, and shared types such as UniqueWorkerId.

Attribute macro (crate kyron-macros)

#[kyron::main]: builds a runtime from its arguments and runs the annotated function in it.

All parts use the queues, synchronisation and threading primitives of Kyron Foundation (comp__kyron_foundation).

Dynamic Architecture#

The three sequences below show how a task runs, how a task waits for I/O, and how the safety worker takes over the error handling of a safety task.

Task Life Cycle
status: valid
security: YES
safety: QM

A task spawned on async worker A is stolen and run by async worker B.

Every async worker takes the tasks of its own queue in FIFO order. Every 16th task it moves a batch of tasks from the global queue into its own queue. A worker with an empty queue steals only while fewer than half of the async workers are searching; otherwise it parks. The limit on searching workers is rounded up and not strictly enforced: concurrent workers can exceed it briefly.

I/O Wait and Wake
status: valid
security: YES
safety: QM
version: 1

A task reads from a socket that has no data yet. The same sequence applies to UdpSocket and to the iceoryx2 event Listener.

The wait timeout is the next timer expiry, or none when another parked worker already waits for that expiry. Busy async workers also call the poller with a zero timeout every 32 tasks, so readiness is seen without a parked worker. A task spawned while a worker waits in the OS poller wakes that worker through the internal waker of the I/O driver.

Safety Worker Error Handling
status: valid
security: YES
safety: QM
version: 1

A parent task spawns a safety task, and the safety task fails. Calling the safety API from a dedicated worker is a non-recoverable error. Without a safety worker (enable_safety_worker not called) the safety API falls back to the regular path and raises no error; an application that needs the safety worker calls kyron::safety::ensure_safety_enabled() first. The current release is QM, so an integrator must not rely on this sequence for a safety goal (see Kyron Safety Plan).

A non-recoverable error logs the error and panics (not_recoverable_error!).

Interfaces#

Kyron Runtime API
status: valid
security: NO
safety: QM
language: rust
version: 1

The public Rust API of the runtime for building engines and runtimes, entering the runtime and spawning tasks.

kyron::runtime::RuntimeBuilder::new
status: valid
security: NO
safety: QM

Create a runtime builder without engines. Defined in src/kyron/src/runtime/runtime_impl.rs as RuntimeBuilder::new; the contract is documented in its rustdoc.

kyron::runtime::RuntimeBuilder::with_engine
status: valid
security: NO
safety: QM

Add an execution engine to the runtime. Defined in src/kyron/src/runtime/runtime_impl.rs as RuntimeBuilder::with_engine; the contract is documented in its rustdoc.

kyron::runtime::RuntimeBuilder::build
status: valid
security: NO
safety: QM

Build the runtime from its execution engines. Defined in src/kyron/src/runtime/runtime_impl.rs as RuntimeBuilder::build; the contract is documented in its rustdoc.

kyron::runtime::ExecutionEngineBuilder::new
status: valid
security: NO
safety: QM

Create an execution engine configuration. Defined in src/kyron/src/scheduler/execution_engine.rs as ExecutionEngineBuilder::new; the contract is documented in its rustdoc.

kyron::runtime::ExecutionEngineBuilder::workers
status: valid
security: NO
safety: QM

Set the number of async workers. Defined in src/kyron/src/scheduler/execution_engine.rs as ExecutionEngineBuilder::workers; the contract is documented in its rustdoc.

kyron::runtime::ExecutionEngineBuilder::task_queue_size
status: valid
security: NO
safety: QM

Set the size of the task queue of each async worker. Defined in src/kyron/src/scheduler/execution_engine.rs as ExecutionEngineBuilder::task_queue_size; the contract is documented in its rustdoc.

kyron::runtime::ExecutionEngineBuilder::thread_priority
status: valid
security: NO
safety: QM

Set the OS priority of the async workers. Defined in src/kyron/src/scheduler/execution_engine.rs as ExecutionEngineBuilder::thread_priority; the contract is documented in its rustdoc.

kyron::runtime::ExecutionEngineBuilder::thread_affinity
status: valid
security: NO
safety: QM

Set the CPU affinity of the async workers. Defined in src/kyron/src/scheduler/execution_engine.rs as ExecutionEngineBuilder::thread_affinity; the contract is documented in its rustdoc.

kyron::runtime::ExecutionEngineBuilder::thread_scheduler
status: valid
security: NO
safety: QM

Set the OS scheduler type of the async workers. Defined in src/kyron/src/scheduler/execution_engine.rs as ExecutionEngineBuilder::thread_scheduler; the contract is documented in its rustdoc.

kyron::runtime::ExecutionEngineBuilder::thread_stack_size
status: valid
security: NO
safety: QM

Set the stack size of the async workers. Defined in src/kyron/src/scheduler/execution_engine.rs as ExecutionEngineBuilder::thread_stack_size; the contract is documented in its rustdoc.

kyron::runtime::ExecutionEngineBuilder::enable_safety_worker
status: valid
security: NO
safety: QM

Enable the safety worker of the engine. Defined in src/kyron/src/scheduler/execution_engine.rs as ExecutionEngineBuilder::enable_safety_worker; the contract is documented in its rustdoc.

kyron::runtime::ExecutionEngineBuilder::safety_worker_task_queue_size
status: valid
security: NO
safety: QM

Set the task queue size of the safety worker. Defined in src/kyron/src/scheduler/execution_engine.rs as ExecutionEngineBuilder::safety_worker_task_queue_size; the contract is documented in its rustdoc.

kyron::runtime::ExecutionEngineBuilder::with_dedicated_worker
status: valid
security: NO
safety: QM

Add a dedicated worker to the engine. Defined in src/kyron/src/scheduler/execution_engine.rs as ExecutionEngineBuilder::with_dedicated_worker; the contract is documented in its rustdoc.

kyron::runtime::Runtime::block_on
status: valid
security: NO
safety: QM

Run a future to completion on the default engine, blocking the current thread. Defined in src/kyron/src/runtime/runtime_impl.rs as Runtime::block_on; the contract is documented in its rustdoc.

kyron::runtime::Runtime::block_on_engine
status: valid
security: NO
safety: QM

Run a future to completion on a specified engine, blocking the current thread. Defined in src/kyron/src/runtime/runtime_impl.rs as Runtime::block_on_engine; the contract is documented in its rustdoc.

kyron::runtime::Runtime::spawn
status: valid
security: NO
safety: QM

Start a future asynchronously on the default engine. Defined in src/kyron/src/runtime/runtime_impl.rs as Runtime::spawn; the contract is documented in its rustdoc.

kyron::runtime::Runtime::spawn_in_engine
status: valid
security: NO
safety: QM

Start a future asynchronously on a specified engine. Defined in src/kyron/src/runtime/runtime_impl.rs as Runtime::spawn_in_engine; the contract is documented in its rustdoc.

kyron::runtime::Runtime::shutdown
status: valid
security: NO
safety: QM

Stop all engines and their worker threads. Defined in src/kyron/src/runtime/runtime_impl.rs as Runtime::shutdown; the contract is documented in its rustdoc.

kyron::spawn
status: valid
security: NO
safety: QM
source_code_link: src/kyron/src/lib.rs:79
version: 1

Spawn a task on the async workers. Defined in src/kyron/src/lib.rs as spawn; the contract is documented in its rustdoc.

kyron::spawn_from_boxed
status: valid
security: NO
safety: QM
source_code_link: src/kyron/src/lib.rs:95
version: 1

Spawn an already boxed task on the async workers. Defined in src/kyron/src/lib.rs as spawn_from_boxed; the contract is documented in its rustdoc.

kyron::spawn_from_reusable
status: valid
security: NO
safety: QM
source_code_link: src/kyron/src/lib.rs:110
version: 1

Spawn a reusable task on the async workers. Defined in src/kyron/src/lib.rs as spawn_from_reusable; the contract is documented in its rustdoc.

kyron::spawn_on_dedicated
status: valid
security: NO
safety: QM
source_code_link: src/kyron/src/lib.rs:125
version: 1

Spawn a task on a dedicated worker. Defined in src/kyron/src/lib.rs as spawn_on_dedicated; the contract is documented in its rustdoc.

kyron::spawn_from_boxed_on_dedicated
status: valid
security: NO
safety: QM
source_code_link: src/kyron/src/lib.rs:139
version: 1

Spawn an already boxed task on a dedicated worker. Defined in src/kyron/src/lib.rs as spawn_from_boxed_on_dedicated; the contract is documented in its rustdoc.

kyron::spawn_from_reusable_on_dedicated
status: valid
security: NO
safety: QM
source_code_link: src/kyron/src/lib.rs:154
version: 1

Spawn a reusable task on a dedicated worker. Defined in src/kyron/src/lib.rs as spawn_from_reusable_on_dedicated; the contract is documented in its rustdoc.

kyron::JoinHandle (Future)
status: valid
security: NO
safety: QM

Await the completion and the result of a spawned task. Defined in src/kyron/src/scheduler/join_handle.rs as Future for JoinHandle::poll; the contract is documented in its rustdoc.

kyron::JoinHandle::abort
status: valid
security: NO
safety: QM

Abort a spawned task cooperatively. Defined in src/kyron/src/scheduler/join_handle.rs as JoinHandle::abort; the contract is documented in its rustdoc.

kyron::futures::yield_now::yield_now
status: valid
security: NO
safety: QM

Give control back to the worker so that other queued tasks run first. Defined in src/kyron/src/futures/yield_now.rs as yield_now; the contract is documented in its rustdoc.

Further Interfaces#

The following public interfaces have no component requirements yet. They are described at the interface level only. Their operations and code links are added together with their requirements. The contract of each type and function is documented in its rustdoc.

Kyron Networking API
status: valid
security: YES
safety: QM
language: rust
version: 1

kyron::net: TcpListener, TcpStream and UdpSocket. They expose the application to network input. Kyron provides no authentication or encryption (see Kyron Security Plan).

Kyron OS I/O Layer API
status: valid
security: YES
safety: QM
language: rust
version: 1

kyron::mio: the non-blocking OS sockets (TcpListenerBridge, TcpStreamBridge, UdpSocketBridge), Poll, Registry and raw file descriptor support that the I/O driver and kyron::net are built on. Like kyron::net, it exposes the application to network input.

Kyron Futures and Common Types
status: valid
security: NO
safety: QM
language: rust
version: 1

kyron::futures::reusable_box_future (ReusableBoxFuture and its pool, taken by spawn_from_reusable) and kyron::common::types (UniqueWorkerId, TaskId, FutureBox).

Kyron I/O API
status: valid
security: YES
safety: QM
language: rust
version: 1

kyron::io: the async read and write traits, ReadBuf, ReadinessState and BridgedFd, which makes any pollable file descriptor usable from a task.

Kyron IPC API
status: valid
security: YES
safety: QM
language: rust
version: 1

kyron::ipc::iceoryx2: the async iceoryx2 event listener (AsyncListener, with wait_one and wait_all) and the EventBuilderAsyncExt trait that creates it. The module also re-exports the iceoryx2 and iceoryx2_cal crates, so their whole API is reachable through it. It is built only with the iceoryx2-ipc feature (on by default; the Bazel flag //src/flags:enable_ipc).

Kyron Time API
status: valid
security: NO
safety: QM
language: rust
version: 1

kyron::time::clock (Clock, Instant) and kyron::futures::sleep (sleep).

Kyron Channels API
status: valid
security: NO
safety: QM
language: rust
version: 1

kyron::channels: the spsc and spmc_broadcast channels, and the kyron::select! macro.

Kyron Safety API
status: valid
security: NO
safety: QM
language: rust
version: 1

kyron::safety: spawn, spawn_from_boxed, spawn_from_reusable and their _on_dedicated variants, for futures that return SafetyResult. The safety worker itself is enabled by ExecutionEngineBuilder::enable_safety_worker of Kyron Runtime API (real_arc_int__kyron_runtime__runtime_api).

Kyron Main Macro
status: valid
security: NO
safety: QM
language: rust
version: 1

#[kyron::main]: an attribute macro that builds a runtime from its arguments and runs the annotated async function in it.

Requirements Linked to Component Architecture#

ID

Title

Fulfils Back

comp_req__kyron_runtime__async_task_exec

Cooperative Async Task Execution

comp_arc_dyn__kyron_runtime__task_lifecycle; comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__broadcast_channel

Broadcast Channel

comp_req__kyron_runtime__dedicated_affinity

Dedicated Worker Affinity

comp_req__kyron_runtime__dedicated_locality

Dedicated Worker Task Locality

comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__dedicated_unknown_id

Spawn on Unregistered Dedicated Worker

comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__dedicated_worker

Dedicated Workers

comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__fifo_task_queue

FIFO Task Queues

comp_arc_dyn__kyron_runtime__task_lifecycle; comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__fixed_worker_pool

Fixed Async Worker Pool

comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__multi_engine

Multiple Isolated Execution Engines

comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__no_task_prio

No Task Priorities

comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__reusable_spawn

Spawn From Reusable Futures

comp_req__kyron_runtime__safety_api

Safety API Availability

comp_req__kyron_runtime__safety_err_handling

Safety Worker Error Handling

comp_req__kyron_runtime__sleep

Async Sleep

comp_req__kyron_runtime__spsc_channel

Single-Producer Single-Consumer Channel

comp_req__kyron_runtime__task_queue_overflow

Task Queue Overflow

comp_arc_dyn__kyron_runtime__task_lifecycle; comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__task_queue_size

Task Queue Size

comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__tcp

TCP Networking

comp_req__kyron_runtime__thread_params

Worker Thread Parameters

comp_req__kyron_runtime__udp

UDP Networking

comp_req__kyron_runtime__worker_affinity

Async Worker Affinity

comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__worker_prio

Uniform Async Worker Priority

comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api

comp_req__kyron_runtime__yield_guideline

Yielding Guideline

comp_req__kyron_runtime__yield_now

Yield Operation

comp_arc_sta__kyron_runtime__static_view; real_arc_int__kyron_runtime__runtime_api