Skip to content

Commit

Permalink
SHM lazy init is now configured in config.json (eclipse-zenoh#1756)
Browse files Browse the repository at this point in the history
* SHM lazy init is now configured in config.json

* format fix

* review fixes

* fix typo
  • Loading branch information
yellowhatter authored Feb 5, 2025
1 parent e4ea6f0 commit ca058bd
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 2 deletions.
10 changes: 10 additions & 0 deletions DEFAULT_CONFIG.json5
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion commons/zenoh-config/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}

Expand Down
20 changes: 19 additions & 1 deletion commons/zenoh-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions commons/zenoh-shm/src/init.rs
Original file line number Diff line number Diff line change
@@ -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, <zenoh@zettascale.tech>
//

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();
}
1 change: 1 addition & 0 deletions commons/zenoh-shm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions zenoh/src/net/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down
14 changes: 14 additions & 0 deletions zenoh/tests/shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ca058bd

Please sign in to comment.