From fb36cdb0f416217a8e7bf388f55634ba500d061c Mon Sep 17 00:00:00 2001 From: t-horikawa Date: Fri, 15 Mar 2024 11:34:43 +0900 Subject: [PATCH] the size of allocated shared memory is now adjusted according to the number of client threads --- include/tateyama/status/resource/bridge.h | 10 ++++++++-- .../endpoint/ipc/bootstrap/server_wires_impl.h | 11 ++++++++--- src/tateyama/status/resource/bridge.cpp | 10 ++++++++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/include/tateyama/status/resource/bridge.h b/include/tateyama/status/resource/bridge.h index 18485858..b09b1949 100644 --- a/include/tateyama/status/resource/bridge.h +++ b/include/tateyama/status/resource/bridge.h @@ -57,8 +57,6 @@ class bridge : public framework::resource { }; public: - static constexpr std::size_t shm_size = 16384; - static constexpr id_type tag = framework::resource_id_status; static constexpr std::string_view file_prefix = "tsurugidb-"; // NOLINT @@ -155,6 +153,14 @@ class bridge : public framework::resource { std::unique_ptr database_info_{}; void set_digest(const std::string& path_string); + + static constexpr std::size_t initial_size = 640; // obtained by experiment + static constexpr std::size_t per_size = 8; // obtained by experiment + std::size_t shm_size(std::size_t n) { + std::size_t size = initial_size + (n * per_size); // exact size + size += initial_size / 2; // a little bit of leeway + return ((size / 4096) + 1) * 4096; // round up to the page size + } }; } // namespace tateyama::status_info::resource diff --git a/src/tateyama/endpoint/ipc/bootstrap/server_wires_impl.h b/src/tateyama/endpoint/ipc/bootstrap/server_wires_impl.h index 17662284..847e86c4 100644 --- a/src/tateyama/endpoint/ipc/bootstrap/server_wires_impl.h +++ b/src/tateyama/endpoint/ipc/bootstrap/server_wires_impl.h @@ -592,8 +592,6 @@ inline void server_wire_container_impl::resultset_wire_container_impl::write_com class connection_container { - static constexpr std::size_t request_queue_size = (1<<15); // 32K bytes (tentative) NOLINT - public: explicit connection_container(std::string_view name, std::size_t n) : name_(name) { boost::interprocess::shared_memory_object::remove(name_.c_str()); @@ -602,7 +600,7 @@ class connection_container unrestricted_permissions.set_unrestricted(); managed_shared_memory_ = - std::make_unique(boost::interprocess::create_only, name_.c_str(), request_queue_size, nullptr, unrestricted_permissions); + std::make_unique(boost::interprocess::create_only, name_.c_str(), request_queue_size(n), nullptr, unrestricted_permissions); managed_shared_memory_->destroy(tateyama::common::wire::connection_queue::name); connection_queue_ = managed_shared_memory_->construct(tateyama::common::wire::connection_queue::name)(n, managed_shared_memory_->get_segment_manager()); } @@ -643,6 +641,13 @@ class connection_container std::unique_ptr managed_shared_memory_{}; tateyama::common::wire::connection_queue* connection_queue_; + static constexpr std::size_t initial_size = 720; // obtained by experiment + static constexpr std::size_t per_size = 112; // obtained by experiment + std::size_t request_queue_size(std::size_t n) { + std::size_t size = initial_size + (n * per_size); // exact size + size += initial_size / 2; // a little bit of leeway + return ((size / 4096) + 1) * 4096; // round up to the page size + } }; }; // namespace tateyama::common::wire diff --git a/src/tateyama/status/resource/bridge.cpp b/src/tateyama/status/resource/bridge.cpp index b41b394b..03bb6284 100644 --- a/src/tateyama/status/resource/bridge.cpp +++ b/src/tateyama/status/resource/bridge.cpp @@ -35,7 +35,8 @@ bool bridge::setup(environment& env) { const auto& conf = env.configuration(); set_digest(conf->get_canonical_path().string()); - auto database_name_opt = conf->get_section("ipc_endpoint")->get("database_name"); + auto* ipc_section = conf->get_section("ipc_endpoint"); + auto database_name_opt = ipc_section->get("database_name"); if (!database_name_opt) { LOG(ERROR) << "cannot find database_name at the section in the configuration"; return false; @@ -45,13 +46,18 @@ bool bridge::setup(environment& env) { dbinfo->name(name); database_info_ = std::move(dbinfo); + auto threads_opt = ipc_section->get("threads"); + if (!threads_opt) { + LOG(ERROR) << "cannot find thread_pool_size at the section in the configuration"; + return false; + } std::string status_file_name{file_prefix}; status_file_name += digest_; status_file_name += ".stat"; boost::interprocess::shared_memory_object::remove(status_file_name.c_str()); shm_remover_ = std::make_unique(status_file_name); try { - segment_ = std::make_unique(boost::interprocess::create_only, status_file_name.c_str(), shm_size); + segment_ = std::make_unique(boost::interprocess::create_only, status_file_name.c_str(), shm_size(threads_opt.value())); resource_status_memory_ = std::make_unique(*segment_); resource_status_memory_->set_pid(); resource_status_memory_->set_database_name(name);