MatLogger2
1.0.0
Library for logging of numeric data to HDF5 MAT-files, which is RT-safe and multithreaded.
|
The VariableBuffer class implements a memory buffer for a single logged variable. More...
#include <var_buffer.h>
Classes | |
struct | BufferInfo |
class | QueueImpl |
The QueueImpl class implements the buffering strategy for a single logged variable. More... | |
Public Types | |
enum | Mode { Mode::producer_consumer, Mode::circular_buffer } |
Enum for specifying the type of buffer. More... | |
typedef std::function< void(BufferInfo)> | CallbackType |
Public Member Functions | |
VariableBuffer (std::string name, int dim_rows, int dim_cols, int block_size) | |
Constructor. More... | |
void | set_on_block_available (CallbackType callback) |
Sets a callback that is used to notify that a new block has been pushed into the queue. More... | |
void | set_buffer_mode (VariableBuffer::Mode mode) |
Set whether this buffer should be treated as a (possibly dual threaded) producer-consumer queue, or as a single-threaded circular buffer. More... | |
const std::string & | get_name () const |
std::pair< int, int > | get_dimension () const |
template<typename Derived > | |
bool | add_elem (const Eigen::MatrixBase< Derived > &data) |
Add an element to the buffer. More... | |
bool | read_block (Eigen::MatrixXd &data, int &valid_elements) |
Reads a whole block from the queue, if one is available. More... | |
bool | flush_to_queue () |
Writes current block to the queue. More... | |
~VariableBuffer () | |
Static Public Member Functions | |
static int | NumBlocks () |
The VariableBuffer class implements a memory buffer for a single logged variable.
This is an internal library component, and it is not meant for direct use.
The memory buffer is splitted into a fixed number of blocks, that make up a "pool" of available memory. When a block is full, it is pushed into a lockfree queue, so that it is available for the consumer thread. As soon as the block is consumed, it is returned back to the pool via another lockfree queue.
Apart from the lockfree queues, no other data is shared between add_elem() and read_block(). So, they can be called concurrently without further synchronization.
Because the lockfree queue is of the Single-Producer-Single-Consumer type, a single thread is allowed to call add_elem and read_block, concurrently.
Definition at line 32 of file var_buffer.h.
typedef std::function<void(BufferInfo)> XBot::VariableBuffer::CallbackType |
Definition at line 63 of file var_buffer.h.
|
strong |
Enum for specifying the type of buffer.
Enumerator | |
---|---|
producer_consumer | |
circular_buffer |
Definition at line 40 of file var_buffer.h.
VariableBuffer::VariableBuffer | ( | std::string | name, |
int | dim_rows, | ||
int | dim_cols, | ||
int | block_size | ||
) |
Constructor.
name | Variable name |
dim_rows | Sample rows number |
dim_cols | Sample columns number |
block_size | Number of samples that make up a block |
Definition at line 136 of file var_buffer.cpp.
VariableBuffer::~VariableBuffer | ( | ) |
Definition at line 272 of file var_buffer.cpp.
|
inline |
Add an element to the buffer.
If there is no space inside the current block, this is pushed into the queue by calling flush_to_queue().
Only a single producer thread is allowed to concurrently call this method.
Derived | Data type |
data | Data value |
Definition at line 260 of file var_buffer.h.
bool VariableBuffer::flush_to_queue | ( | ) |
Writes current block to the queue.
If a callback was registered through set_on_block_available(), it is called on success.
Definition at line 196 of file var_buffer.cpp.
std::pair< int, int > VariableBuffer::get_dimension | ( | ) | const |
Definition at line 150 of file var_buffer.cpp.
const std::string & VariableBuffer::get_name | ( | ) | const |
Definition at line 42 of file matlogger2.cpp.
|
static |
Definition at line 261 of file var_buffer.cpp.
bool XBot::VariableBuffer::read_block | ( | Eigen::MatrixXd & | data, |
int & | valid_elements | ||
) |
Reads a whole block from the queue, if one is available.
The block is then returned to the pool.
Only a single consumer thread is allowed to concurrently call this method.
data | Matrix which is filled with the read block (unless the function returns false) |
valid_elements | Number of valid elements contained in the block. This means that only data.leftCols(valid_elements) contains valid data. |
Definition at line 165 of file var_buffer.cpp.
void XBot::VariableBuffer::set_buffer_mode | ( | VariableBuffer::Mode | mode | ) |
Set whether this buffer should be treated as a (possibly dual threaded) producer-consumer queue, or as a single-threaded circular buffer.
By default, the producer_consumer mode is used.
NOTE: only call this method before starting using the logger!!
Definition at line 266 of file var_buffer.cpp.
void VariableBuffer::set_on_block_available | ( | CallbackType | callback | ) |
Sets a callback that is used to notify that a new block has been pushed into the queue.
Definition at line 155 of file var_buffer.cpp.