Kagome
Polkadot Runtime Engine in C++17
|
#include <message_pool.hpp>
Classes |
Public Types | |
using | RefCount = int16_t |
Public Member Functions | |
MessagePool (std::size_t entry_size_bytes, std::size_t entries_count) | |
std::optional< MessageHandle > | push (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< MessageHandle > | nextFreeSlot () |
performs quick lookup for a free slot More... | |
Private Attributes | |
const std::size_t | entry_size_ |
const std::size_t | entries_count_ |
std::vector< Record > | pool_ |
std::unordered_set< std::size_t > | free_slots_ |
common::spin_lock | mutex_ |
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.
struct kagome::telemetry::MessagePool::Record |
Definition at line 99 of file message_pool.hpp.
Class Members | ||
---|---|---|
vector< uint8_t > | data | |
size_t | data_size | |
size_t | ref_count |
using kagome::telemetry::MessagePool::RefCount = int16_t |
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.
kagome::telemetry::MessagePool::MessagePool | ( | std::size_t | entry_size_bytes, |
std::size_t | entries_count | ||
) |
Contruct the pool
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.
MessagePool::RefCount kagome::telemetry::MessagePool::add_ref | ( | MessageHandle | handle | ) |
Increase reference counter for the specified handle
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.
Definition at line 99 of file message_pool.cpp.
|
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.
handle | - handle to the record |
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
message | - the data, could be disposed immediately after returning |
ref_count | - initial reference counter value for the record |
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.
handle | - handle to the record |
The record would be disposed when counter decreases to zero.
Definition at line 65 of file message_pool.cpp.
|
private |
Definition at line 106 of file message_pool.hpp.
|
private |
Definition at line 105 of file message_pool.hpp.
|
private |
Definition at line 108 of file message_pool.hpp.
|
private |
Definition at line 109 of file message_pool.hpp.
|
private |
Definition at line 107 of file message_pool.hpp.