Skip to content

Commit ca058bd

Browse files
authored
SHM lazy init is now configured in config.json (#1756)
* SHM lazy init is now configured in config.json * format fix * review fixes * fix typo
1 parent e4ea6f0 commit ca058bd

File tree

7 files changed

+86
-2
lines changed

7 files changed

+86
-2
lines changed

DEFAULT_CONFIG.json5

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,20 @@
543543
/// NOTE: shared memory can be used only if zenoh is compiled with "shared-memory" feature, otherwise
544544
/// settings in this section have no effect.
545545
shared_memory: {
546+
/// Whether shared memory is enabled or not.
547+
/// If set to `true`, the SHM buffer optimization support will be announced to other parties. (default `true`).
548+
/// This option doesn't make SHM buffer optimization mandatory, the real support depends on other party setting.
546549
/// A probing procedure for shared memory is performed upon session opening. To enable zenoh to operate
547550
/// over shared memory (and to not fallback on network mode), shared memory needs to be enabled also on the
548551
/// subscriber side. By doing so, the probing procedure will succeed and shared memory will operate as expected.
549552
enabled: true,
553+
/// SHM resources initialization mode (default "lazy").
554+
/// - "lazy": SHM subsystem internals will be initialized lazily upon the first SHM buffer
555+
/// allocation or reception. This setting provides better startup time and optimizes resource usage,
556+
/// but produces extra latency at the first SHM buffer interaction.
557+
/// - "init": SHM subsystem internals will be initialized upon Session opening. This setting sacrifices
558+
/// startup time, but guarantees no latency impact when first SHM buffer is processed.
559+
mode: "lazy",
550560
},
551561
auth: {
552562
/// The configuration of authentication.

commons/zenoh-config/src/defaults.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,10 @@ impl Default for LinkRxConf {
307307
#[allow(clippy::derivable_impls)]
308308
impl Default for ShmConf {
309309
fn default() -> Self {
310-
Self { enabled: true }
310+
Self {
311+
enabled: true,
312+
mode: ShmInitMode::default(),
313+
}
311314
}
312315
}
313316

commons/zenoh-config/src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,19 @@ validated_struct::validator! {
548548
pub shared_memory:
549549
ShmConf {
550550
/// Whether shared memory is enabled or not.
551-
/// If set to `true`, the SHM buffer optimization support will be announced to other parties. (default `false`).
551+
/// If set to `true`, the SHM buffer optimization support will be announced to other parties. (default `true`).
552552
/// This option doesn't make SHM buffer optimization mandatory, the real support depends on other party setting
553+
/// A probing procedure for shared memory is performed upon session opening. To enable zenoh to operate
554+
/// over shared memory (and to not fallback on network mode), shared memory needs to be enabled also on the
555+
/// subscriber side. By doing so, the probing procedure will succeed and shared memory will operate as expected.
553556
enabled: bool,
557+
/// SHM resources initialization mode (default "lazy").
558+
/// - "lazy": SHM subsystem internals will be initialized lazily upon the first SHM buffer
559+
/// allocation or reception. This setting provides better startup time and optimizes resource usage,
560+
/// but produces extra latency at the first SHM buffer interaction.
561+
/// - "init": SHM subsystem internals will be initialized upon Session opening. This setting sacrifices
562+
/// startup time, but guarantees no latency impact when first SHM buffer is processed.
563+
mode: ShmInitMode,
554564
},
555565
pub auth: #[derive(Default)]
556566
AuthConf {
@@ -634,6 +644,14 @@ pub enum QueueAllocMode {
634644
Lazy,
635645
}
636646

647+
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
648+
#[serde(rename_all = "snake_case")]
649+
pub enum ShmInitMode {
650+
Init,
651+
#[default]
652+
Lazy,
653+
}
654+
637655
impl Default for PermissionsConf {
638656
fn default() -> Self {
639657
PermissionsConf {

commons/zenoh-shm/src/init.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Copyright (c) 2024 ZettaScale Technology
3+
//
4+
// This program and the accompanying materials are made available under the
5+
// terms of the Eclipse Public License 2.0 which is available at
6+
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
//
9+
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
//
11+
// Contributors:
12+
// ZettaScale Zenoh Team, <[email protected]>
13+
//
14+
15+
use crate::{
16+
api::client_storage::GLOBAL_CLIENT_STORAGE,
17+
cleanup::CLEANUP,
18+
metadata::{storage::GLOBAL_METADATA_STORAGE, subscription::GLOBAL_METADATA_SUBSCRIPTION},
19+
watchdog::{confirmator::GLOBAL_CONFIRMATOR, validator::GLOBAL_VALIDATOR},
20+
};
21+
22+
pub fn init() {
23+
CLEANUP.init();
24+
GLOBAL_CLIENT_STORAGE.init();
25+
GLOBAL_METADATA_STORAGE.init();
26+
GLOBAL_METADATA_SUBSCRIPTION.init();
27+
GLOBAL_CONFIRMATOR.init();
28+
GLOBAL_VALIDATOR.init();
29+
}

commons/zenoh-shm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ macro_rules! tested_crate_module {
5454
pub mod api;
5555
mod cleanup;
5656
pub mod header;
57+
pub mod init;
5758
pub mod metadata;
5859
pub mod posix_shm;
5960
pub mod reader;

zenoh/src/net/runtime/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ impl RuntimeBuilder {
172172
.unwrap_or_else(|| load_plugins(&config));
173173
// Admin space creation flag
174174
let start_admin_space = *config.adminspace.enabled();
175+
// SHM lazy init flag
176+
#[cfg(feature = "shared-memory")]
177+
let shm_init_mode = *config.transport.shared_memory.mode();
175178

176179
let config = Notifier::new(crate::config::Config(config));
177180
let runtime = Runtime {
@@ -231,6 +234,12 @@ impl RuntimeBuilder {
231234
}
232235
});
233236

237+
#[cfg(feature = "shared-memory")]
238+
match shm_init_mode {
239+
zenoh_config::ShmInitMode::Init => zenoh_shm::init::init(),
240+
zenoh_config::ShmInitMode::Lazy => {}
241+
};
242+
234243
Ok(runtime)
235244
}
236245
}

zenoh/tests/shm.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,20 @@ async fn test_session_pubsub(peer01: &Session, peer02: &Session, reliability: Re
195195
}
196196
}
197197

198+
#[test]
199+
fn zenoh_shm_startup_init() {
200+
// Open the sessions
201+
let mut config = zenoh::Config::default();
202+
config
203+
.transport
204+
.shared_memory
205+
.set_mode(zenoh_config::ShmInitMode::Init)
206+
.unwrap();
207+
tokio::runtime::Runtime::new().unwrap().block_on(async {
208+
let _session = ztimeout!(zenoh::open(config)).unwrap();
209+
});
210+
}
211+
198212
#[test]
199213
fn zenoh_shm_unicast() {
200214
tokio::runtime::Runtime::new().unwrap().block_on(async {

0 commit comments

Comments
 (0)