Skip to content

Commit

Permalink
configurable shm size
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowhatter committed Aug 21, 2024
1 parent 785eab0 commit 57fb755
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
35 changes: 35 additions & 0 deletions rmw_zenoh_cpp/src/detail/zenoh_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ static const std::unordered_map<ConfigurableEntity,
};

static const char * router_check_attempts_envar = "ZENOH_ROUTER_CHECK_ATTEMPTS";
#ifdef RMW_ZENOH_BUILD_WITH_SHARED_MEMORY
static const char * zenoh_shm_alloc_size_envar = "ZENOH_SHM_ALLOC_SIZE";
static const size_t zenoh_shm_alloc_size_default = 1024*1024;
#endif

rmw_ret_t _get_z_config(
const char * envar_name,
Expand Down Expand Up @@ -128,4 +132,35 @@ std::optional<uint64_t> zenoh_router_check_attempts()
// If unset, check indefinitely.
return default_value;
}


#ifdef RMW_ZENOH_BUILD_WITH_SHARED_MEMORY
///=============================================================================
size_t zenoh_shm_alloc_size() {
const char * envar_value;

if (NULL != rcutils_get_env(zenoh_shm_alloc_size_envar, &envar_value)) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp", "Envar %s cannot be read. Report this bug.",
zenoh_shm_alloc_size_envar);
return zenoh_shm_alloc_size_default;
}

// If the environment variable contains a value, handle it accordingly.
if (envar_value[0] != '\0') {
const auto read_value = std::strtoull(envar_value, nullptr, 10);
if (read_value > 0) {
if (read_value > std::numeric_limits<size_t>::max()) {
RMW_ZENOH_LOG_ERROR_NAMED(
"rmw_zenoh_cpp", "Envar %s: value is too large!",
zenoh_shm_alloc_size_envar);
} else {
return read_value;
}
}
}

return zenoh_shm_alloc_size_default;
}
#endif
} // namespace rmw_zenoh_cpp
11 changes: 11 additions & 0 deletions rmw_zenoh_cpp/src/detail/zenoh_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ rmw_ret_t get_z_config(const ConfigurableEntity & entity, z_owned_config_t * con
/// @return The number of times to try connecting to a zenoh router and
/// std::nullopt if establishing a connection to a router is not required.
std::optional<uint64_t> zenoh_router_check_attempts();

#ifdef RMW_ZENOH_BUILD_WITH_SHARED_MEMORY
///=============================================================================
/// Get the amount of shared memory to be pre-allocated for Zenoh SHM operation
/// based on the environment variable ZENOH_SHM_ALLOC_SIZE.
/// @details The behavior is as follows:
/// - If not set or <= 0, the default value of 1MB is returned.
/// - Else value of environemnt variable is returned.
/// @return The amount of shared memory to be pre-allocated for Zenoh SHM operation
size_t zenoh_shm_alloc_size();
#endif
} // namespace rmw_zenoh_cpp

#endif // DETAIL__ZENOH_CONFIG_HPP_
7 changes: 1 addition & 6 deletions rmw_zenoh_cpp/src/rmw_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@


extern "C" {
#ifdef RMW_ZENOH_BUILD_WITH_SHARED_MEMORY
// Megabytes of SHM to reserve.
// TODO(clalancette): Make this configurable, or get it from the configuration
#define SHM_BUFFER_SIZE_MB 10
#endif

namespace
{
Expand Down Expand Up @@ -232,7 +227,7 @@ rmw_ret_t rmw_init(const rmw_init_options_t *options, rmw_context_t *context) {
// TODO(yuyuan): determine the default alignment
z_alloc_alignment_t alignment = {5};
z_owned_memory_layout_t layout;
z_memory_layout_new(&layout, SHM_BUFFER_SIZE_MB * 1024 * 1024, alignment);
z_memory_layout_new(&layout, rmw_zenoh_cpp::zenoh_shm_alloc_size(), alignment);

z_owned_shm_provider_t provider;
if (z_posix_shm_provider_new(&provider, z_loan(layout))) {
Expand Down

0 comments on commit 57fb755

Please sign in to comment.