Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::thread_local_buffer Class Reference

Thread-local buffer for lock-free metric collection. More...

#include <thread_local_buffer.h>

Collaboration diagram for kcenon::monitoring::thread_local_buffer:
Collaboration graph

Classes

struct  stats
 Get statistics about buffer operations. More...
 

Public Member Functions

 thread_local_buffer (size_t capacity=DEFAULT_CAPACITY, std::shared_ptr< central_collector > collector=nullptr)
 Construct a thread-local buffer.
 
 ~thread_local_buffer ()
 Destructor - flushes any remaining samples.
 
 thread_local_buffer (const thread_local_buffer &)=delete
 
thread_local_bufferoperator= (const thread_local_buffer &)=delete
 
 thread_local_buffer (thread_local_buffer &&)=delete
 
thread_local_bufferoperator= (thread_local_buffer &&)=delete
 
bool record (const metric_sample &sample)
 Record a metric sample (lock-free)
 
bool record_auto_flush (const metric_sample &sample)
 Record a metric sample with automatic flush on overflow.
 
size_t flush ()
 Flush buffered samples to central collector.
 
size_t size () const
 Get current number of buffered samples.
 
bool is_full () const
 Check if buffer is full.
 
size_t capacity () const
 Get buffer capacity.
 
void set_collector (std::shared_ptr< central_collector > collector)
 Set the central collector.
 
const statsget_stats () const
 Get buffer statistics.
 

Static Public Attributes

static constexpr size_t DEFAULT_CAPACITY = 256
 

Private Attributes

std::vector< metric_samplebuffer_
 
size_t capacity_
 
size_t write_index_ {0}
 
std::shared_ptr< central_collectorcollector_
 
stats stats_
 

Detailed Description

Thread-local buffer for lock-free metric collection.

Each thread maintains its own buffer for recording metrics without locks. When the buffer fills up, it flushes to a central collector.

Uses lazy initialization: capacity is reserved at construction but elements are constructed on first use. This avoids wasting memory for short-lived threads that never record any metrics.

Thread Safety:
NOT thread-safe across threads (thread-local use only). Thread-safe within a single thread (no concurrent access).

Definition at line 66 of file thread_local_buffer.h.

Constructor & Destructor Documentation

◆ thread_local_buffer() [1/3]

kcenon::monitoring::thread_local_buffer::thread_local_buffer ( size_t capacity = DEFAULT_CAPACITY,
std::shared_ptr< central_collector > collector = nullptr )
explicit

Construct a thread-local buffer.

Parameters
capacityMaximum number of samples before flush
collectorCentral collector to receive flushed samples

◆ ~thread_local_buffer()

kcenon::monitoring::thread_local_buffer::~thread_local_buffer ( )

Destructor - flushes any remaining samples.

◆ thread_local_buffer() [2/3]

kcenon::monitoring::thread_local_buffer::thread_local_buffer ( const thread_local_buffer & )
delete

◆ thread_local_buffer() [3/3]

kcenon::monitoring::thread_local_buffer::thread_local_buffer ( thread_local_buffer && )
delete

Member Function Documentation

◆ capacity()

size_t kcenon::monitoring::thread_local_buffer::capacity ( ) const
inline

Get buffer capacity.

Returns
Maximum number of samples before flush

Definition at line 137 of file thread_local_buffer.h.

References capacity_.

◆ flush()

size_t kcenon::monitoring::thread_local_buffer::flush ( )

Flush buffered samples to central collector.

Returns
Number of samples flushed
Thread Safety:
Thread-safe (single-threaded access guaranteed by TLS)
Note
Acquires lock in central_collector during flush

Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ get_stats()

const stats & kcenon::monitoring::thread_local_buffer::get_stats ( ) const
inline

Get buffer statistics.

Returns
Statistics about buffer operations

Definition at line 160 of file thread_local_buffer.h.

References stats_.

◆ is_full()

bool kcenon::monitoring::thread_local_buffer::is_full ( ) const
inline

Check if buffer is full.

Returns
true if at capacity, false otherwise

Definition at line 131 of file thread_local_buffer.h.

References capacity_, and write_index_.

Referenced by TEST_F().

Here is the caller graph for this function:

◆ operator=() [1/2]

thread_local_buffer & kcenon::monitoring::thread_local_buffer::operator= ( const thread_local_buffer & )
delete

◆ operator=() [2/2]

thread_local_buffer & kcenon::monitoring::thread_local_buffer::operator= ( thread_local_buffer && )
delete

◆ record()

bool kcenon::monitoring::thread_local_buffer::record ( const metric_sample & sample)

Record a metric sample (lock-free)

Parameters
sampleMetric sample to record
Returns
true if recorded, false if buffer is full (caller should flush)
Thread Safety:
Thread-safe (single-threaded access guaranteed by TLS)
Performance:
O(1) amortized - direct array write after initial growth

Referenced by TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ record_auto_flush()

bool kcenon::monitoring::thread_local_buffer::record_auto_flush ( const metric_sample & sample)

Record a metric sample with automatic flush on overflow.

Parameters
sampleMetric sample to record
Returns
true if recorded successfully (with or without flush)
Thread Safety:
Thread-safe (single-threaded access guaranteed by TLS)
Note
Automatically flushes and retries if buffer is full

Referenced by TEST_F(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ set_collector()

void kcenon::monitoring::thread_local_buffer::set_collector ( std::shared_ptr< central_collector > collector)
inline

Set the central collector.

Parameters
collectorCentral collector to receive flushed samples

Definition at line 143 of file thread_local_buffer.h.

143 {
144 collector_ = collector;
145 }
std::shared_ptr< central_collector > collector_

References collector_.

◆ size()

size_t kcenon::monitoring::thread_local_buffer::size ( ) const
inline

Get current number of buffered samples.

Returns
Number of samples waiting to be flushed

Definition at line 125 of file thread_local_buffer.h.

125{ return write_index_; }

References write_index_.

Referenced by TEST_F(), and TEST_F().

Here is the caller graph for this function:

Member Data Documentation

◆ buffer_

std::vector<metric_sample> kcenon::monitoring::thread_local_buffer::buffer_
private

Definition at line 163 of file thread_local_buffer.h.

◆ capacity_

size_t kcenon::monitoring::thread_local_buffer::capacity_
private

Definition at line 164 of file thread_local_buffer.h.

Referenced by capacity(), and is_full().

◆ collector_

std::shared_ptr<central_collector> kcenon::monitoring::thread_local_buffer::collector_
private

Definition at line 166 of file thread_local_buffer.h.

Referenced by set_collector().

◆ DEFAULT_CAPACITY

size_t kcenon::monitoring::thread_local_buffer::DEFAULT_CAPACITY = 256
staticconstexpr

Definition at line 68 of file thread_local_buffer.h.

◆ stats_

stats kcenon::monitoring::thread_local_buffer::stats_
private

Definition at line 167 of file thread_local_buffer.h.

Referenced by get_stats().

◆ write_index_

size_t kcenon::monitoring::thread_local_buffer::write_index_ {0}
private

Definition at line 165 of file thread_local_buffer.h.

165{0}; // Single-writer, no atomic needed

Referenced by is_full(), and size().


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