MySQL 26.10.0
Source Code Documentation
hm::Stat_Buffer< T > Class Template Reference

Sample data buffer. More...

#include <health_monitor_stat_buffer.h>

Inheritance diagram for hm::Stat_Buffer< T >:
[legend]

Public Member Functions

 Stat_Buffer ()=default
 
 Stat_Buffer (bool no_lock, PSI_memory_key memory_key)
 
 Stat_Buffer (PSI_rwlock_key rwlock_key, PSI_memory_key memory_key)
 
virtual ~Stat_Buffer ()
 
 Stat_Buffer (const Stat_Buffer &)=default
 
Stat_Buffer & operator= (const Stat_Buffer &)=default
 
 Stat_Buffer (Stat_Buffer &&) noexcept=default
 
Stat_Buffer & operator= (Stat_Buffer &&) noexcept=default
 
int read (T &record, unsigned int row_num)
 Read an element of the stat buffer. More...
 
int read_last (T &record)
 Read the last element of the stat buffer. More...
 
int lock_read ()
 Acquire a read lock for a shared buffer. More...
 
void unlock ()
 Unlock a read or write lock for a shared buffer. More...
 
int read_no_lock (T &record, unsigned int row_num)
 Read an element from the circular buffer by logical row index. More...
 
int read_impl (T &record, size_t row_num)
 
int write (const T &record)
 Write one element into the circular buffer at the current head position. More...
 
int write_impl (const T &record)
 
void resize (size_t new_size)
 Resize the stat buffer, preserve the most recent data. More...
 
void resize_impl (size_t new_size)
 
void reset ()
 Reset the state of the buffer. More...
 
void reset_no_lock ()
 Reset the buffer. More...
 
void reset_impl ()
 
bool map_logical_to_row_no_lock (uint64_t logical_idx, int &row_out) const
 Map a global logical index to the current ring-buffer row. More...
 
bool map_logical_to_row (uint64_t logical_idx, int &row_out) const
 
bool map_logical_to_row_impl (uint64_t logical_idx, int &row_out) const
 
mysql_rwlock_t * get_rwlock ()
 
size_t max_size () const
 
size_t index () const
 
size_t count () const
 
size_t total_written () const
 
bool full () const
 
bool wrapped () const
 

Protected Attributes

std::unique_ptr< std::vector< T, Allocator > > m_stats {nullptr}
 
size_t m_max_size {0}
 
size_t m_index {0}
 
size_t m_count {0}
 
size_t m_total_written {0}
 
bool m_full {false}
 
bool m_wrapped {false}
 
bool m_no_lock {false}
 
PSI_rwlock_key m_rwlock_key {0}
 
mysql_rwlock_t m_rwlock
 
PSI_memory_key m_memory_key {0}
 

Private Types

using Allocator = Component_malloc_allocator< T >
 

Detailed Description

template<typename T>
class hm::Stat_Buffer< T >

Sample data buffer.

Member Typedef Documentation

◆ Allocator

template<typename T >
using hm::Stat_Buffer< T >::Allocator = Component_malloc_allocator<T>
private

Constructor & Destructor Documentation

◆ Stat_Buffer() [1/5]

template<typename T >
hm::Stat_Buffer< T >::Stat_Buffer ( )
default

◆ Stat_Buffer() [2/5]

template<typename T >
hm::Stat_Buffer< T >::Stat_Buffer ( bool  no_lock,
PSI_memory_key  memory_key 
)
inline

◆ Stat_Buffer() [3/5]

template<typename T >
hm::Stat_Buffer< T >::Stat_Buffer ( PSI_rwlock_key  rwlock_key,
PSI_memory_key  memory_key 
)
inline

◆ ~Stat_Buffer()

template<typename T >
virtual hm::Stat_Buffer< T >::~Stat_Buffer ( )
inlinevirtual

◆ Stat_Buffer() [4/5]

template<typename T >
hm::Stat_Buffer< T >::Stat_Buffer ( const Stat_Buffer< T > &  )
default

◆ Stat_Buffer() [5/5]

template<typename T >
hm::Stat_Buffer< T >::Stat_Buffer ( Stat_Buffer< T > &&  )
defaultnoexcept

Member Function Documentation

◆ count()

template<typename T >
size_t hm::Stat_Buffer< T >::count ( ) const
inline

◆ full()

template<typename T >
bool hm::Stat_Buffer< T >::full ( ) const
inline

◆ get_rwlock()

template<typename T >
mysql_rwlock_t * hm::Stat_Buffer< T >::get_rwlock ( )
inline

◆ index()

template<typename T >
size_t hm::Stat_Buffer< T >::index ( ) const
inline

◆ lock_read()

template<typename T >
int hm::Stat_Buffer< T >::lock_read ( )
inline

Acquire a read lock for a shared buffer.

Returns
0 success
1 fail to set lock

◆ map_logical_to_row()

template<typename T >
bool hm::Stat_Buffer< T >::map_logical_to_row ( uint64_t  logical_idx,
int &  row_out 
) const
inline

◆ map_logical_to_row_impl()

template<typename T >
bool hm::Stat_Buffer< T >::map_logical_to_row_impl ( uint64_t  logical_idx,
int &  row_out 
) const
inline

◆ map_logical_to_row_no_lock()

template<typename T >
bool hm::Stat_Buffer< T >::map_logical_to_row_no_lock ( uint64_t  logical_idx,
int &  row_out 
) const
inline

Map a global logical index to the current ring-buffer row.

This helper translates a monotonically increasing logical sample index (e.g. a global “next_metric”) into a window-relative row index [0..count-1] for the current contents of the buffer.

A logical index is considered “available” if it refers to an element that is still retained in the buffer (not yet overwritten.) If the logical index is older than the oldest existing element or newer than the most recent one, the mapping fails and the function returns false.

Thread safety:

  • Acquires the buffer’s read lock internally (if the buffer is shared) to provide a coherent snapshot of count() and total_written().
Parameters
[in]logical_idxGlobal monotonically increasing sample index.
[out]row_outOn success, the row index in the range [0, count()) that corresponds to logical_idx.
Returns
true If logical_idx maps to a currently available row.
false If logical_idx is too old (overwritten) or not yet written.
Note
This function does not perform I/O. Use the returned row_out with read(record, row_out) to retrieve the element.

◆ max_size()

template<typename T >
size_t hm::Stat_Buffer< T >::max_size ( ) const
inline

◆ operator=() [1/2]

template<typename T >
Stat_Buffer & hm::Stat_Buffer< T >::operator= ( const Stat_Buffer< T > &  )
default

◆ operator=() [2/2]

template<typename T >
Stat_Buffer & hm::Stat_Buffer< T >::operator= ( Stat_Buffer< T > &&  )
defaultnoexcept

◆ read()

template<typename T >
int hm::Stat_Buffer< T >::read ( T &  record,
unsigned int  row_num 
)
inline

Read an element of the stat buffer.

Parameters
[in,out]recordElement from the buffer to be returned
[in]row_numRow number, zero-based
Returns
0 success, 1 failure

◆ read_impl()

template<typename T >
int hm::Stat_Buffer< T >::read_impl ( T &  record,
size_t  row_num 
)
inline

◆ read_last()

template<typename T >
int hm::Stat_Buffer< T >::read_last ( T &  record)
inline

Read the last element of the stat buffer.

Parameters
[in,out]recordElement from the buffer to be returned
Returns
0 success, 1 failure

◆ read_no_lock()

template<typename T >
int hm::Stat_Buffer< T >::read_no_lock ( T &  record,
unsigned int  row_num 
)
inline

Read an element from the circular buffer by logical row index.

The buffer holds up to m_count valid elements. This function accepts a row non-negative row_num range [0, m_count].

Thread safety:

  • If this buffer is shared (m_no_lock == false), the caller should hold a read lock or use read()/read_last() which manage locking.
  • This method itself does not acquire locks.
Parameters
[out]recordOn success, set to element at requested logical row.
[in]row_numZero-based logical row index [0, m_count].
Returns
0 on success.
1 if the buffer is empty (no elements to read).
Note
Logical indexing: When the buffer has wrapped, the oldest element begins at m_index; otherwise it begins at 0.

◆ reset()

template<typename T >
void hm::Stat_Buffer< T >::reset ( )
inline

Reset the state of the buffer.

Existing elements will be overwritten.

◆ reset_impl()

template<typename T >
void hm::Stat_Buffer< T >::reset_impl ( )
inline

◆ reset_no_lock()

template<typename T >
void hm::Stat_Buffer< T >::reset_no_lock ( )
inline

Reset the buffer.

Assumes an active read_lock() if the buffer is shared.

◆ resize()

template<typename T >
void hm::Stat_Buffer< T >::resize ( size_t  new_size)
inline

Resize the stat buffer, preserve the most recent data.

Note that m_stats is preallocated to m_max_size elements, so m_stats->size() is always the maximum size.

Parameters
[in]new_sizeDesired size of the buffer, can be either larger or smaller than the current size

◆ resize_impl()

template<typename T >
void hm::Stat_Buffer< T >::resize_impl ( size_t  new_size)
inline

◆ total_written()

template<typename T >
size_t hm::Stat_Buffer< T >::total_written ( ) const
inline

◆ unlock()

template<typename T >
void hm::Stat_Buffer< T >::unlock ( )
inline

Unlock a read or write lock for a shared buffer.

◆ wrapped()

template<typename T >
bool hm::Stat_Buffer< T >::wrapped ( ) const
inline

◆ write()

template<typename T >
int hm::Stat_Buffer< T >::write ( const T &  record)
inline

Write one element into the circular buffer at the current head position.

If the buffer is not yet full, the element is appended logically and the valid element count (m_count) increases. The head index (m_index) advances modulo the buffer size.

Once the buffer reaches capacity:

  • Subsequent writes overwrite the oldest element
  • m_full remains true
  • m_wrapped is set when the first overwrite occurs and stays true

Thread safety:

  • This method does not acquire locks. For shared buffers (m_no_lock == false), call write() which manages locking, or ensure the caller holds an exclusive lock.
Parameters
[in]recordElement to write.
Returns
0 on success.
1 if the buffer has zero capacity (size() == 0).
Note
Uses logical indexing consistent with read_impl(): When not wrapped, the oldest element is at index 0. After wrapping, the oldest begins at m_index.

◆ write_impl()

template<typename T >
int hm::Stat_Buffer< T >::write_impl ( const T &  record)
inline

Member Data Documentation

◆ m_count

template<typename T >
size_t hm::Stat_Buffer< T >::m_count {0}
protected

◆ m_full

template<typename T >
bool hm::Stat_Buffer< T >::m_full {false}
protected

◆ m_index

template<typename T >
size_t hm::Stat_Buffer< T >::m_index {0}
protected

◆ m_max_size

template<typename T >
size_t hm::Stat_Buffer< T >::m_max_size {0}
protected

◆ m_memory_key

template<typename T >
PSI_memory_key hm::Stat_Buffer< T >::m_memory_key {0}
protected

◆ m_no_lock

template<typename T >
bool hm::Stat_Buffer< T >::m_no_lock {false}
protected

◆ m_rwlock

template<typename T >
mysql_rwlock_t hm::Stat_Buffer< T >::m_rwlock
mutableprotected

◆ m_rwlock_key

template<typename T >
PSI_rwlock_key hm::Stat_Buffer< T >::m_rwlock_key {0}
protected

◆ m_stats

template<typename T >
std::unique_ptr<std::vector<T, Allocator> > hm::Stat_Buffer< T >::m_stats {nullptr}
protected

◆ m_total_written

template<typename T >
size_t hm::Stat_Buffer< T >::m_total_written {0}
protected

◆ m_wrapped

template<typename T >
bool hm::Stat_Buffer< T >::m_wrapped {false}
protected

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