29#include "ipc/session/standalone/shm/arena_lend/ipc_shm_message.capnp.h"
35#include "ipc/shm/arena_lend/owner_shm_pool_listener.hpp"
46#include <flow/util/basic_blob.hpp>
47#include <flow/util/util.hpp>
48#include <flow/error/error.hpp>
49#include <boost/utility.hpp>
50#include <boost/unordered/unordered_map.hpp>
51#include <boost/unordered/unordered_set.hpp>
161 private flow::log::Log_context,
162 public std::enable_shared_from_this<Shm_session>,
163 private boost::noncopyable
213 using Blob = flow::util::Blob_sans_log_context;
407 static std::shared_ptr<Shm_session>
create(flow::log::Logger* logger,
Shm_channel&& shm_channel,
409 flow::async::Task_asio_err&& shm_channel_error_handler);
425 bool lend_arena(
const std::shared_ptr<Arena>& arena);
606 size_t per_arena_stats_sz_limit_or_0 = 0,
size_t* n_arenas_omitted =
nullptr);
659#ifdef IPC_DOXYGEN_ONLY
808 flow::async::Task_asio_err&& shm_channel_error_handler);
989 boost::unordered_map<std::shared_ptr<Arena>,
1054 using flow::error::Runtime_error;
1055 using flow::util::default_init_at;
1057 Thread_lcl_obj_db_admin<Arena>::this_thread_piggy_scan();
1061 FLOW_LOG_TRACE(
"Disconnected, so ignoring object lending attempt.");
1068 auto const *
const disposer = std::get_deleter<Owner_obj_disposer_and_mdt<Arena>>(object);
1081 throw Runtime_error{
"lend_object() was given handle (shared_ptr) without disposer of the expected type; "
1082 "did you get it from Ipc_arena::construct() as required?"};
1086 const auto object_ptr =
object.get();
1089 const auto& shm_arena = *disposer->m_shm_arena;
1090 const auto collection_id = shm_arena.get_id();
1091 const auto use_ct_idx = disposer->m_use_ct_idx;
1092 const auto lend_tracker_pool_id = disposer->m_lend_tracker_pool_id;
1098 default_init_at(object_handle);
1100 object_handle->m_collection_id = collection_id;
1101 object_handle->m_lend_tracker_pool_id = lend_tracker_pool_id;
1102 object_handle->m_use_ct_idx = use_ct_idx;
1103 Owner_shm_pool_repository::from_address(object_ptr,
1104 object_handle->m_pool_id, object_handle->m_pool_offset);
1106 if (flow::util::this_thread_unique_token() == disposer->m_cting_thread_token)
1112 Thread_lcl_obj_db_admin<Arena>
1113 ::this_thread_obj_db()->lending_obj(collection_id, use_ct_idx);
1117 Thread_lcl_obj_db_client
1118 ::this_thread_obj_db()->lending_obj(shm_arena, lend_tracker_pool_id, use_ct_idx);
1129 using Borrower_shm_pool_collection_repository
1133 Thread_lcl_obj_db_admin<Arena>::this_thread_piggy_scan();
1135 const size_t blob_size = blob.size();
1138 FLOW_LOG_WARNING(
"Blob size [" << blob_size <<
"] does not match expected "
1139 "size [" <<
sizeof(
Shm_object_handle) <<
"]. This indicates a bug on someone's part; "
1140 "either the blob was somehow improperly formed owner-side, or the user has supplied something "
1141 "unequal to what was formed there. borrow_object() returning null as advertised.");
1149 memcpy(&object_handle, blob.const_data(),
sizeof(object_handle));
1152 = Borrower_shm_pool_collection_repository::to_address_safe(object_handle.
m_pool_id,
1168 FLOW_LOG_WARNING(
"Borrowed blob encodes pool ID [" << object_handle.
m_pool_id <<
"], but this pool ID "
1169 "is no longer borrowed or has never been borrowed; treating the blob as invalid. "
1170 "borrow_object() returning null as advertised for invalid SHM-handle blobs.");
1178 return construct_with_borrower_obj_disposer<T, Shm_session>
1179 (
static_cast<T*
>(obj_addr),
1188 shared_from_this());
Singleton repository for borrowed SHM-pool data.
The Owner_shm_pool_listener impl object that we register within a particular Ipc_arena (Ipc_arena::ad...
Shm_pool_listener_impl(Shm_session &owner, collection_id_t collection_id)
Constructor.
Shm_session & m_owner
See ctor.
void notify_created_shm_pool(const std::shared_ptr< Shm_pool > &shm_pool) override
Implements super-class API (see inner class doc header for recap).
void notify_initial_shm_pools(const std::set< std::shared_ptr< Shm_pool > > &shm_pools) override
Implements super-class API (notification called upon initial registration in lend_arena() in a synchr...
const collection_id_t m_collection_id
See ctor.
void notify_removed_shm_pool(const std::shared_ptr< Shm_pool > &shm_pool) override
Implements super-class API.
In SHM-jemalloc, a context between this process and a single (typically other) opposing process w/r/t...
const util::process_id_t m_remote_process_id
The other end's process ID, cached from m_shm_channel; used for registering borrowed items.
~Shm_session()
Constructors/destructor.
boost::unordered_map< std::shared_ptr< Arena >, std::unique_ptr< Shm_pool_listener_impl > > m_shm_pool_listener_map
Maps an arena to its SHM-pool listener; this is used for receiving changes in the SHM pools.
void info_dump(Info_dump *target_info_dump)
Fills-out the stats/info contents of the given stat::Shm_session_info_dump: a printable bundling of s...
static std::vector< Shm_pool_info > borrowed_shm_pool_live_info()
For informational/stats-adjacent purposes, returns information on currently open (and memory-mapped) ...
Blob lend_object(const Handle< T > &object)
Registers an in-SHM object for lending to the borrower on the other end of the session; and returns a...
Shm_pool::size_t pool_offset_t
Short-hand for pool offset type.
Borrower_pool_stats m_borrower_pool_stats
Stats about borrower SHM-pool (and containing SHM-arena) borrowing (opening) and unborrowing (closing...
void lend_shm_pools(collection_id_t collection_id, const std::set< std::shared_ptr< Shm_pool > > &shm_pools)
Equivalent to lend_shm_pool(collection_id, P) for each P in shm_pools.
void send_response(const Shm_struc_channel::Msg_in *original_message, util::String_view operation)
Helper that sends ack to having received a thing that the opposing side sent via its send_sync_reques...
bool send_sync_request(Shm_struc_channel::Msg_out &message, util::String_view operation)
Helper that sends message over internal-use m_shm_channel and returns on ack (or failure to send).
Shm_pool::pool_id_t pool_id_t
Short-hand for pool ID type.
bool lend_arena(const std::shared_ptr< Arena > &arena)
Registers an arena for lending to the borrower process (usually a separate process) on the other end ...
void receive_arena(collection_id_t collection_id, Shared_name &&pool_name_base)
Borrows an arena shared by the lender: in thread W a/k/a m_serial_task_loop handle LendArena message.
Handle< T > borrow_object(const Blob &serialized_object) const
Yields a handle to the cross-process-GCed in-SHM object that was earlier S->lend_object()ed based on ...
static const Borrower_pool_lookup_global_stats & borrower_pool_lookup_global_stats()
Returns the process-wide borrower-side stat-set Borrower_pool_lookup_global_stats,...
void set_disconnected(const Error_code &err_code)
Helper that permanently sets m_connected true => false, so that lend_arena(), lend_object(),...
boost::unordered_map< collection_id_t, boost::unordered_set< pool_id_t > > m_borrower_pool_id_map
Per-collection set of borrowed pool IDs; used for cleanup in dtor.
void borrower_pool_stats_reset()
Resets borrower_pool_stats() – the per-*this-Shm_session view only; does not affect the process-wide ...
std::atomic< bool > m_connected
Whether the session is connected; true to start; when set to false m_shm_channel_error_handler is inv...
const Borrower_pool_stats & borrower_pool_stats() const
Returns the borrower-side stat-set Borrower_pool_stats limited to *this Shm_session – i....
bool lend_shm_pool(collection_id_t collection_id, const std::shared_ptr< Shm_pool > &shm_pool)
Registers a SHM pool for lending and sends messages to the borrower about it; returns on acknowledgme...
void remove_lender_shm_pool(collection_id_t collection_id, const std::shared_ptr< Shm_pool > &shm_pool)
As Shm_pool_listener_impl::notify_created_shm_pool() (by Ipc_arena) => lend_shm_pool(),...
static std::shared_ptr< Shm_session > create(flow::log::Logger *logger, Shm_channel &&shm_channel, const transport::struc::Session_token &session_token_non_nil, flow::async::Task_asio_err &&shm_channel_error_handler)
Creates an instance of this class; the resulting Shm_arena object is immediately ready to receive inc...
ipc::session::shm::arena_lend::Borrower_pool_stats_list Borrower_pool_stats_list
Alias for a (per-arena) stats-list type, as returned by borrower_pool_stats_process_wide().
flow::async::Task_asio_err m_shm_channel_error_handler
Callback executed when a channel error code is emitted when using m_shm_channel.
static const Borrower_pool_stats & borrower_pool_stats_process_wide(Borrower_pool_stats_list *per_arena_stats=nullptr, size_t per_arena_stats_sz_limit_or_0=0, size_t *n_arenas_omitted=nullptr)
Returns the process-wide borrower-side stat-set Borrower_pool_stats – the running totals across all S...
void receive_shm_pool(collection_id_t collection_id, pool_id_t shm_pool_id, pool_offset_t pool_size)
Borrows a SHM pool from the lender: in thread W a/k/a m_serial_task_loop handle LendPool message.
ipc::shm::arena_lend::collection_id_t collection_id_t
Short-hand for pool-collection (a/k/a arena) ID type.
static void global_stats_reset()
Resets borrower_pool_lookup_global_stats() and borrower_pool_stats_process_wide().
ipc::shm::arena_lend::Shm_pool Shm_pool
Alias for a SHM pool.
std::optional< Shm_struc_channel > m_shm_channel
The channel used for transmitting SHM-pool messages.
flow::util::Blob_sans_log_context Blob
Alias for a light-weight blob used to encode SHM-handles emitted by lend_object() and consumed by bor...
Arena::Handle< T > Handle
First-class handle to an outer object as returned by borrow_object(); equals Arena::Handle a/k/a shm:...
flow::async::Single_thread_task_loop m_serial_task_loop
Task engine to process tasks in a serial manner (a/k/a thread W).
Shm_session(flow::log::Logger *logger, Shm_channel &&shm_channel, const transport::struc::Session_token &session_token_non_nil, flow::async::Task_asio_err &&shm_channel_error_handler)
Constructor (private due to shared_ptr factory pattern).
util::process_id_t get_remote_process_id() const
Returns the remote process ID.
When borrowing STL-compliant in-SHM data structures, use this type as the Arena template parameter to...
Singleton repository for SHM-pool data pertaining to all SHM-arenas owned by this process.
(Internal-use) Thread_lcl_obj_db_admin<A>::this_thread_obj_db() is the current thread's module that t...
(Internal-use) Where a Thread_lcl_obj_db_admin cannot handle lend-object and dispose-object tracking,...
Represents/manages a segregated collection of in-SHM (shared memory) objects and buffers allocated us...
Obj_handle< Pointed_type > Handle
First-class handle – a shared_ptr of some kind by definition – to objects construct<>()ed by this are...
Stateless allocator usable with STL-compliant containers to store (or merely read) them directly in S...
A Channel with a blobs pipe only (no handles pipe) that uses a Unix domain socket connection as the u...
Owning and wrapping a pre-connected transport::Channel peer (an endpoint of an established channel ov...
typename Sync_io_obj::Msg_out Msg_out
Encapsulation of any out-message payload sent or meant to be sent via send() (et al) by a *this of th...
typename Sync_io_obj::Msg_in Msg_in
Encapsulation of any in-message instance received by a *this of this type.
String-wrapping abstraction representing a name uniquely distinguishing a kernel-persistent entity fr...
Support for SHM-backed ipc::session sessions and session-servers with the SHM-jemalloc (ipc::shm::are...
std::vector< std::unique_ptr< ipc::shm::arena_lend::stat::Borrower_pool_stats > > Borrower_pool_stats_list
Canonical short-hand for a list of per-arena stat::Borrower_pool_stats, as produced by the borrower-s...
uint32_t pool_id_t
Pool ID type.
Obj_handle< T > construct_with_borrower_obj_disposer(T *addr, pool_id_t lend_tracker_pool_id, use_ct_idx_t use_ct_idx, owner_id_t owner_id, collection_id_t collection_id, std::shared_ptr< const Shm_session > &&shm_session_to_keep_alive)
(Roughly) the borrower-side counterpart of Owner_obj_disposer_and_mdt, this returns a borrower-side h...
uint32_t use_ct_idx_t
Index into a given Lend_tracker_pool's bulk storage of arena-construct()ed (outer a/k/a first-class) ...
uint32_t collection_id_t
Identifier type for a shared memory pool collection.
util::Shared_name Shared_name
Short-hand for util::Shared_name; used in particular for SHM pool names at least.
boost::uuids::uuid Session_token
A type used by struc::Channel for internal safety/security/auth needs.
::pid_t process_id_t
Syntactic-sugary type for POSIX process ID (integer).
flow::util::String_view String_view
Short-hand for Flow's String_view.
flow::Error_code Error_code
Short-hand for flow::Error_code which is very common.
A serialized object handle: a compact representation of an in-SHM object's location and lending metad...
pool_offset_t m_pool_offset
The offset within the pool where this object resides.
pool_id_t m_pool_id
Pool identifier. Reminder: this is globally unique until reboot.
ipc::shm::arena_lend::detail::use_ct_idx_t m_use_ct_idx
Index of the use-count slot within the above Lend_tracker_pool.
pool_id_t m_lend_tracker_pool_id
Pool ID of the Lend_tracker_pool holding the use-count slot for this object. Same namespace as m_pool...
collection_id_t m_collection_id
The collection ID of the arena in which this object was created. Reminder: it is unique only given a ...
Internal-use shared_ptr disposer object, attached to every cross-process-lendable object handle retur...
Printable (with knobs in contained util::stat::Info_dump_format) bundling of stats/info relevant to (...
Analogous subject matter to Owner_pool_lookup_global_stats but borrower-side.
Loosely similar subject matter to Owner_pool_stats but regarding the borrower-side view of SHM-pools,...
This info-struct contains basic properties of a SHM-pool (in a SHM-arena).