diff --git a/DEFAULT_CONFIG.json5 b/DEFAULT_CONFIG.json5 index f174a5cd88..19939cd1eb 100644 --- a/DEFAULT_CONFIG.json5 +++ b/DEFAULT_CONFIG.json5 @@ -543,10 +543,20 @@ /// NOTE: shared memory can be used only if zenoh is compiled with "shared-memory" feature, otherwise /// settings in this section have no effect. shared_memory: { + /// Whether shared memory is enabled or not. + /// If set to `true`, the SHM buffer optimization support will be announced to other parties. (default `true`). + /// This option doesn't make SHM buffer optimization mandatory, the real support depends on other party setting. /// A probing procedure for shared memory is performed upon session opening. To enable zenoh to operate /// over shared memory (and to not fallback on network mode), shared memory needs to be enabled also on the /// subscriber side. By doing so, the probing procedure will succeed and shared memory will operate as expected. enabled: true, + /// SHM resources initialization mode (default "lazy"). + /// - "lazy": SHM subsystem internals will be initialized lazily upon the first SHM buffer + /// allocation or reception. This setting provides better startup time and optimizes resource usage, + /// but produces extra latency at the first SHM buffer interaction. + /// - "init": SHM subsystem internals will be initialized upon Session opening. This setting sacrifices + /// startup time, but guarantees no latency impact when first SHM buffer is processed. + mode: "lazy", }, auth: { /// The configuration of authentication. diff --git a/commons/zenoh-config/src/defaults.rs b/commons/zenoh-config/src/defaults.rs index 4eda0d051d..e07dc8cc54 100644 --- a/commons/zenoh-config/src/defaults.rs +++ b/commons/zenoh-config/src/defaults.rs @@ -307,7 +307,10 @@ impl Default for LinkRxConf { #[allow(clippy::derivable_impls)] impl Default for ShmConf { fn default() -> Self { - Self { enabled: true } + Self { + enabled: true, + mode: ShmInitMode::default(), + } } } diff --git a/commons/zenoh-config/src/lib.rs b/commons/zenoh-config/src/lib.rs index 34e6cba3c4..0092cee437 100644 --- a/commons/zenoh-config/src/lib.rs +++ b/commons/zenoh-config/src/lib.rs @@ -548,9 +548,19 @@ validated_struct::validator! { pub shared_memory: ShmConf { /// Whether shared memory is enabled or not. - /// If set to `true`, the SHM buffer optimization support will be announced to other parties. (default `false`). + /// If set to `true`, the SHM buffer optimization support will be announced to other parties. (default `true`). /// This option doesn't make SHM buffer optimization mandatory, the real support depends on other party setting + /// A probing procedure for shared memory is performed upon session opening. To enable zenoh to operate + /// over shared memory (and to not fallback on network mode), shared memory needs to be enabled also on the + /// subscriber side. By doing so, the probing procedure will succeed and shared memory will operate as expected. enabled: bool, + /// SHM resources initialization mode (default "lazy"). + /// - "lazy": SHM subsystem internals will be initialized lazily upon the first SHM buffer + /// allocation or reception. This setting provides better startup time and optimizes resource usage, + /// but produces extra latency at the first SHM buffer interaction. + /// - "init": SHM subsystem internals will be initialized upon Session opening. This setting sacrifices + /// startup time, but guarantees no latency impact when first SHM buffer is processed. + mode: ShmInitMode, }, pub auth: #[derive(Default)] AuthConf { @@ -634,6 +644,14 @@ pub enum QueueAllocMode { Lazy, } +#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ShmInitMode { + Init, + #[default] + Lazy, +} + impl Default for PermissionsConf { fn default() -> Self { PermissionsConf { diff --git a/commons/zenoh-shm/src/init.rs b/commons/zenoh-shm/src/init.rs new file mode 100644 index 0000000000..50d89a5dfb --- /dev/null +++ b/commons/zenoh-shm/src/init.rs @@ -0,0 +1,29 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +use crate::{ + api::client_storage::GLOBAL_CLIENT_STORAGE, + cleanup::CLEANUP, + metadata::{storage::GLOBAL_METADATA_STORAGE, subscription::GLOBAL_METADATA_SUBSCRIPTION}, + watchdog::{confirmator::GLOBAL_CONFIRMATOR, validator::GLOBAL_VALIDATOR}, +}; + +pub fn init() { + CLEANUP.init(); + GLOBAL_CLIENT_STORAGE.init(); + GLOBAL_METADATA_STORAGE.init(); + GLOBAL_METADATA_SUBSCRIPTION.init(); + GLOBAL_CONFIRMATOR.init(); + GLOBAL_VALIDATOR.init(); +} diff --git a/commons/zenoh-shm/src/lib.rs b/commons/zenoh-shm/src/lib.rs index 37cdf75df6..b2ef6989a9 100644 --- a/commons/zenoh-shm/src/lib.rs +++ b/commons/zenoh-shm/src/lib.rs @@ -54,6 +54,7 @@ macro_rules! tested_crate_module { pub mod api; mod cleanup; pub mod header; +pub mod init; pub mod metadata; pub mod posix_shm; pub mod reader; diff --git a/zenoh/src/net/runtime/mod.rs b/zenoh/src/net/runtime/mod.rs index 4d299428e5..b9907b86e6 100644 --- a/zenoh/src/net/runtime/mod.rs +++ b/zenoh/src/net/runtime/mod.rs @@ -172,6 +172,9 @@ impl RuntimeBuilder { .unwrap_or_else(|| load_plugins(&config)); // Admin space creation flag let start_admin_space = *config.adminspace.enabled(); + // SHM lazy init flag + #[cfg(feature = "shared-memory")] + let shm_init_mode = *config.transport.shared_memory.mode(); let config = Notifier::new(crate::config::Config(config)); let runtime = Runtime { @@ -231,6 +234,12 @@ impl RuntimeBuilder { } }); + #[cfg(feature = "shared-memory")] + match shm_init_mode { + zenoh_config::ShmInitMode::Init => zenoh_shm::init::init(), + zenoh_config::ShmInitMode::Lazy => {} + }; + Ok(runtime) } } diff --git a/zenoh/tests/shm.rs b/zenoh/tests/shm.rs index 50194e7662..d0078123ef 100644 --- a/zenoh/tests/shm.rs +++ b/zenoh/tests/shm.rs @@ -195,6 +195,20 @@ async fn test_session_pubsub(peer01: &Session, peer02: &Session, reliability: Re } } +#[test] +fn zenoh_shm_startup_init() { + // Open the sessions + let mut config = zenoh::Config::default(); + config + .transport + .shared_memory + .set_mode(zenoh_config::ShmInitMode::Init) + .unwrap(); + tokio::runtime::Runtime::new().unwrap().block_on(async { + let _session = ztimeout!(zenoh::open(config)).unwrap(); + }); +} + #[test] fn zenoh_shm_unicast() { tokio::runtime::Runtime::new().unwrap().block_on(async {