Kagome
Polkadot Runtime Engine in C++17
kagome::telemetry::MessagePool Class Reference

#include <message_pool.hpp>

Collaboration diagram for kagome::telemetry::MessagePool:

Classes

struct  Record
 

Public Types

using RefCount = int16_t
 

Public Member Functions

 MessagePool (std::size_t entry_size_bytes, std::size_t entries_count)
 
std::optional< MessageHandlepush (const std::string &message, int16_t ref_count)
 
RefCount add_ref (MessageHandle handle)
 
RefCount release (MessageHandle handle)
 
boost::asio::mutable_buffer operator[] (MessageHandle handle) const
 
std::size_t capacity () const
 

Private Member Functions

std::optional< MessageHandlenextFreeSlot ()
 performs quick lookup for a free slot More...
 

Private Attributes

const std::size_t entry_size_
 
const std::size_t entries_count_
 
std::vector< Recordpool_
 
std::unordered_set< std::size_t > free_slots_
 
common::spin_lock mutex_
 

Detailed Description

Message pool used to deduplicate and hold the data till async write operations are in progress.

The pool is designed to be extremely fast against data-copy operations. That is why all the storage buffers are pre-allocated during the construction. All the copy operations are performed in a fastest manner via memcpy.

Access operations to shared data where data race is possible are synchronized via spin locks. Locking is performed for a smallest possible set of lines of code. Spin locks let us avoid expensive context switches and performance degradation.

Definition at line 36 of file message_pool.hpp.


Class Documentation

struct kagome::telemetry::MessagePool::Record

Definition at line 99 of file message_pool.hpp.

Collaboration diagram for kagome::telemetry::MessagePool::Record:
Class Members
vector< uint8_t > data
size_t data_size
size_t ref_count

Member Typedef Documentation

Type for reference external reference counters. Normally cannot be negative. That sized and signed type is chosen to easily catch illegal cases (negative and too hign numbers -> will more likely result in an overflow).

Definition at line 45 of file message_pool.hpp.

Constructor & Destructor Documentation

kagome::telemetry::MessagePool::MessagePool ( std::size_t  entry_size_bytes,
std::size_t  entries_count 
)

Contruct the pool

Parameters
entry_size_bytes- max size of a single record
entries_count- max amount of records to hold at the same time

Definition at line 13 of file message_pool.cpp.

Member Function Documentation

MessagePool::RefCount kagome::telemetry::MessagePool::add_ref ( MessageHandle  handle)

Increase reference counter for the specified handle

Parameters
handle- handle to the record

Would be useful in case when many verbosity level would be developed.

Definition at line 52 of file message_pool.cpp.

std::size_t kagome::telemetry::MessagePool::capacity ( ) const

Reports a number of records the pool was initialized for.

Returns
pool capacity

Definition at line 99 of file message_pool.cpp.

std::optional< MessageHandle > kagome::telemetry::MessagePool::nextFreeSlot ( )
private

performs quick lookup for a free slot

Definition at line 103 of file message_pool.cpp.

boost::asio::mutable_buffer kagome::telemetry::MessagePool::operator[] ( MessageHandle  handle) const

Access the record by the handle.

Parameters
handle- handle to the record
Returns
- boost::asio::buffer with the contents ready to be passed to boost::asio::async_write call

Definition at line 81 of file message_pool.cpp.

std::optional< MessageHandle > kagome::telemetry::MessagePool::push ( const std::string &  message,
int16_t  ref_count 
)

Put a message to the pool

Parameters
message- the data, could be disposed immediately after returning
ref_count- initial reference counter value for the record
Returns
- handle to the record or std::nullopt when the pool is full

Note: the record will be freed from the pool as soon as an handle-owner calls the release method for the handle ref_count-many times

Definition at line 27 of file message_pool.cpp.

MessagePool::RefCount kagome::telemetry::MessagePool::release ( MessageHandle  handle)

Decrement reference counter for the handled record.

Parameters
handle- handle to the record

The record would be disposed when counter decreases to zero.

Definition at line 65 of file message_pool.cpp.

Member Data Documentation

const std::size_t kagome::telemetry::MessagePool::entries_count_
private

Definition at line 106 of file message_pool.hpp.

const std::size_t kagome::telemetry::MessagePool::entry_size_
private

Definition at line 105 of file message_pool.hpp.

std::unordered_set<std::size_t> kagome::telemetry::MessagePool::free_slots_
private

Definition at line 108 of file message_pool.hpp.

common::spin_lock kagome::telemetry::MessagePool::mutex_
private

Definition at line 109 of file message_pool.hpp.

std::vector<Record> kagome::telemetry::MessagePool::pool_
private

Definition at line 107 of file message_pool.hpp.


The documentation for this class was generated from the following files: