timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
tim::data_storage::ring_buffer< Tp > Struct Template Reference

Ring buffer wrapper around tim::base::ring_buffer for data of type Tp. If the data object size is larger than the page size (typically 4KB), behavior is undefined. During initialization, one requests a minimum number of objects and the buffer will support that number of object + the remainder of the page, e.g. if a page is 1000 bytes, the object is 1 byte, and the buffer is requested to support 1500 objects, then an allocation supporting 2000 objects (i.e. 2 pages) will be created. More...

#include "timemory/storage/ring_buffer.hpp"

+ Collaboration diagram for tim::data_storage::ring_buffer< Tp >:

Public Types

using base_type = base::ring_buffer
 

Public Member Functions

 ring_buffer ()=default
 
 ~ring_buffer ()=default
 
 ring_buffer (bool _use_mmap)
 
 ring_buffer (size_t _size)
 
 ring_buffer (size_t _size, bool _use_mmap)
 
 ring_buffer (const ring_buffer &)
 
 ring_buffer (ring_buffer &&) noexcept=default
 
ring_bufferoperator= (const ring_buffer &)
 
ring_bufferoperator= (ring_buffer &&) noexcept=default
 
bool is_initialized () const
 Returns whether the buffer has been allocated. More...
 
size_t capacity () const
 Get the total number of Tp instances supported. More...
 
void init (size_t _size)
 Creates new ring buffer. More...
 
void destroy ()
 Destroy ring buffer. More...
 
size_t data_size () const
 Write data to buffer. More...
 
Tp * write (Tp *in)
 Write data to buffer. Return pointer to location of write. More...
 
Tp * read (Tp *out) const
 Read data from buffer. Return pointer to location of read. More...
 
Tp * request ()
 Get an uninitialized address at tail of buffer. More...
 
Tp * retrieve ()
 Read data from head of buffer. More...
 
size_t count () const
 Returns number of Tp instances currently held by the buffer. More...
 
size_t free () const
 Returns how many Tp instances are availiable in the buffer. More...
 
bool is_empty () const
 Returns if the buffer is empty. More...
 
bool is_full () const
 Returns if the buffer is full. More...
 
size_t rewind (size_t n) const
 Rewinds the read pointer. More...
 
template<typename... Args>
auto emplace (Args &&... args)
 
std::string as_string () const
 
bool get_use_mmap () const
 query whether using mmap More...
 
void set_use_mmap (bool)
 explicitly configure to use mmap if avail More...
 

Private Member Functions

template<typename Tp >
std::pair< size_t, Tp * > write (Tp *in, std::enable_if_t< std::is_class< Tp >::value, int >=0)
 Write class-type data to buffer (uses placement new). More...
 
template<typename Tp >
std::pair< size_t, Tp * > write (Tp *in, std::enable_if_t<!std::is_class< Tp >::value, int >=0)
 Write non-class-type data to buffer (uses memcpy). More...
 
void * request (size_t n)
 Request a pointer to an allocation for at least. More...
 
template<typename Tp >
std::pair< size_t, Tp * > read (Tp *out, std::enable_if_t< std::is_class< Tp >::value, int >=0) const
 Read class-type data from buffer (uses placement new). More...
 
template<typename Tp >
std::pair< size_t, Tp * > read (Tp *out, std::enable_if_t<!std::is_class< Tp >::value, int >=0) const
 Read non-class-type data from buffer (uses memcpy). More...
 
void * retrieve (size_t n)
 Retrieve a pointer to the head allocation of at least. More...
 

Friends

std::ostream & operator<< (std::ostream &os, const ring_buffer &obj)
 

Detailed Description

template<typename Tp>
struct tim::data_storage::ring_buffer< Tp >

Ring buffer wrapper around tim::base::ring_buffer for data of type Tp. If the data object size is larger than the page size (typically 4KB), behavior is undefined. During initialization, one requests a minimum number of objects and the buffer will support that number of object + the remainder of the page, e.g. if a page is 1000 bytes, the object is 1 byte, and the buffer is requested to support 1500 objects, then an allocation supporting 2000 objects (i.e. 2 pages) will be created.

Definition at line 360 of file ring_buffer.hpp.

Member Typedef Documentation

◆ base_type

Definition at line 362 of file ring_buffer.hpp.

Constructor & Destructor Documentation

◆ ring_buffer() [1/6]

template<typename Tp >
tim::data_storage::ring_buffer< Tp >::ring_buffer ( )
default

◆ ~ring_buffer()

template<typename Tp >
tim::data_storage::ring_buffer< Tp >::~ring_buffer ( )
default

◆ ring_buffer() [2/6]

template<typename Tp >
tim::data_storage::ring_buffer< Tp >::ring_buffer ( bool  _use_mmap)
inlineexplicit

Definition at line 367 of file ring_buffer.hpp.

368 : base_type{ _use_mmap }
369 {}

◆ ring_buffer() [3/6]

template<typename Tp >
tim::data_storage::ring_buffer< Tp >::ring_buffer ( size_t  _size)
inlineexplicit

Definition at line 371 of file ring_buffer.hpp.

372 : base_type{ _size * sizeof(Tp) }
373 {}

◆ ring_buffer() [4/6]

template<typename Tp >
tim::data_storage::ring_buffer< Tp >::ring_buffer ( size_t  _size,
bool  _use_mmap 
)
inline

Definition at line 375 of file ring_buffer.hpp.

376 : base_type{ _size * sizeof(Tp), _use_mmap }
377 {}

◆ ring_buffer() [5/6]

template<typename Tp >
tim::data_storage::ring_buffer< Tp >::ring_buffer ( const ring_buffer< Tp > &  rhs)

Definition at line 471 of file ring_buffer.hpp.

472: base_type{ rhs }
473{
474 for(const auto& itr : rhs.m_copy)
475 itr.second(*this, itr.first);
476}

◆ ring_buffer() [6/6]

template<typename Tp >
tim::data_storage::ring_buffer< Tp >::ring_buffer ( ring_buffer< Tp > &&  )
defaultnoexcept

Member Function Documentation

◆ as_string()

template<typename Tp >
std::string tim::data_storage::ring_buffer< Tp >::as_string ( ) const
inline

Definition at line 437 of file ring_buffer.hpp.

438 {
439 std::ostringstream ss{};
440 size_t _w = std::log10(base_type::capacity()) + 1;
441 ss << std::boolalpha << std::right << "data size: " << std::setw(_w)
442 << data_size() << " B, is_initialized: " << std::setw(5) << is_initialized()
443 << ", is_empty: " << std::setw(5) << is_empty()
444 << ", is_full: " << std::setw(5) << is_full()
445 << ", capacity: " << std::setw(_w) << capacity()
446 << ", count: " << std::setw(_w) << count() << ", free: " << std::setw(_w)
447 << free() << ", raw capacity: " << std::setw(_w) << base_type::capacity()
448 << " B, raw count: " << std::setw(_w) << base_type::count()
449 << " B, raw free: " << std::setw(_w) << base_type::free()
450 << " B, pointer: " << std::setw(15) << base_type::m_ptr
451 << ", raw read count: " << std::setw(_w) << base_type::m_read_count
452 << ", raw write count: " << std::setw(_w) << base_type::m_write_count;
453 return ss.str();
454 }
size_t free() const
Returns how many bytes are availiable in the buffer.
size_t capacity() const
Get the total number of bytes supported.
Definition: ring_buffer.hpp:71
size_t count() const
Returns number of bytes currently held by the buffer.
size_t capacity() const
Get the total number of Tp instances supported.
size_t count() const
Returns number of Tp instances currently held by the buffer.
bool is_initialized() const
Returns whether the buffer has been allocated.
bool is_full() const
Returns if the buffer is full.
size_t free() const
Returns how many Tp instances are availiable in the buffer.
size_t data_size() const
Write data to buffer.
bool is_empty() const
Returns if the buffer is empty.

References tim::base::ring_buffer::capacity(), tim::data_storage::ring_buffer< Tp >::capacity(), tim::base::ring_buffer::count(), tim::data_storage::ring_buffer< Tp >::count(), tim::data_storage::ring_buffer< Tp >::data_size(), tim::base::ring_buffer::free(), tim::data_storage::ring_buffer< Tp >::free(), tim::data_storage::ring_buffer< Tp >::is_empty(), tim::data_storage::ring_buffer< Tp >::is_full(), and tim::data_storage::ring_buffer< Tp >::is_initialized().

◆ capacity()

template<typename Tp >
size_t tim::data_storage::ring_buffer< Tp >::capacity ( ) const
inline

Get the total number of Tp instances supported.

Definition at line 389 of file ring_buffer.hpp.

389{ return (base_type::capacity()) / sizeof(Tp); }

References tim::base::ring_buffer::capacity().

Referenced by tim::data_storage::ring_buffer< Tp >::as_string().

◆ count()

template<typename Tp >
size_t tim::data_storage::ring_buffer< Tp >::count ( ) const
inline

Returns number of Tp instances currently held by the buffer.

Definition at line 413 of file ring_buffer.hpp.

413{ return (base_type::count()) / sizeof(Tp); }

References tim::base::ring_buffer::count().

Referenced by tim::data_storage::ring_buffer< Tp >::as_string().

◆ data_size()

template<typename Tp >
size_t tim::data_storage::ring_buffer< Tp >::data_size ( ) const
inline

Write data to buffer.

Definition at line 398 of file ring_buffer.hpp.

398{ return sizeof(Tp); }

Referenced by tim::data_storage::ring_buffer< Tp >::as_string().

◆ destroy()

template<typename Tp >
void tim::data_storage::ring_buffer< Tp >::destroy ( )
inline

Destroy ring buffer.

Definition at line 395 of file ring_buffer.hpp.

void destroy()
Destroy ring buffer.

References tim::base::ring_buffer::destroy().

◆ emplace()

template<typename Tp >
template<typename... Args>
auto tim::data_storage::ring_buffer< Tp >::emplace ( Args &&...  args)
inline

Definition at line 428 of file ring_buffer.hpp.

429 {
430 Tp _obj{ std::forward<Args>(args)... };
431 return write(&_obj);
432 }
Tp * write(Tp *in)
Write data to buffer. Return pointer to location of write.

References tim::data_storage::ring_buffer< Tp >::write().

◆ free()

template<typename Tp >
size_t tim::data_storage::ring_buffer< Tp >::free ( ) const
inline

Returns how many Tp instances are availiable in the buffer.

Definition at line 416 of file ring_buffer.hpp.

416{ return (base_type::free()) / sizeof(Tp); }

References tim::base::ring_buffer::free().

Referenced by tim::data_storage::ring_buffer< Tp >::as_string().

◆ get_use_mmap()

template<typename Tp >
bool tim::base::ring_buffer::get_use_mmap ( ) const
inline

query whether using mmap

Definition at line 134 of file ring_buffer.hpp.

134{ return m_use_mmap; }

◆ init()

template<typename Tp >
void tim::data_storage::ring_buffer< Tp >::init ( size_t  _size)
inline

Creates new ring buffer.

Definition at line 392 of file ring_buffer.hpp.

392{ base_type::init(_size * sizeof(Tp)); }
void init(size_t size)
Creates new ring buffer.
Definition: ring_buffer.cpp:84

References tim::base::ring_buffer::init().

◆ is_empty()

template<typename Tp >
bool tim::data_storage::ring_buffer< Tp >::is_empty ( ) const
inline

Returns if the buffer is empty.

Definition at line 419 of file ring_buffer.hpp.

419{ return base_type::is_empty(); }
bool is_empty() const
Returns if the buffer is empty.

References tim::base::ring_buffer::is_empty().

Referenced by tim::data_storage::ring_buffer< Tp >::as_string().

◆ is_full()

template<typename Tp >
bool tim::data_storage::ring_buffer< Tp >::is_full ( ) const
inline

Returns if the buffer is full.

Definition at line 422 of file ring_buffer.hpp.

422{ return (base_type::free() < sizeof(Tp)); }

References tim::base::ring_buffer::free().

Referenced by tim::data_storage::ring_buffer< Tp >::as_string().

◆ is_initialized()

template<typename Tp >
bool tim::data_storage::ring_buffer< Tp >::is_initialized ( ) const
inline

Returns whether the buffer has been allocated.

Definition at line 386 of file ring_buffer.hpp.

386{ return base_type::is_initialized(); }
bool is_initialized() const
Returns whether the buffer has been allocated.
Definition: ring_buffer.hpp:68

References tim::base::ring_buffer::is_initialized().

Referenced by tim::data_storage::ring_buffer< Tp >::as_string().

◆ operator=() [1/2]

template<typename Tp >
ring_buffer< Tp > & tim::data_storage::ring_buffer< Tp >::operator= ( const ring_buffer< Tp > &  rhs)

Definition at line 480 of file ring_buffer.hpp.

481{
482 if(this == &rhs)
483 return *this;
484
486 for(const auto& itr : rhs.m_copy)
487 itr.second(*this, itr.first);
488
489 return *this;
490}
ring_buffer & operator=(const ring_buffer &)
Definition: ring_buffer.cpp:71

◆ operator=() [2/2]

template<typename Tp >
ring_buffer & tim::data_storage::ring_buffer< Tp >::operator= ( ring_buffer< Tp > &&  )
defaultnoexcept

◆ read()

template<typename Tp >
Tp * tim::data_storage::ring_buffer< Tp >::read ( Tp *  out) const
inline

Read data from buffer. Return pointer to location of read.

Definition at line 404 of file ring_buffer.hpp.

404{ return remove_copy(base_type::read<Tp>(out).second); }

◆ request()

template<typename Tp >
Tp * tim::data_storage::ring_buffer< Tp >::request ( )
inline

Get an uninitialized address at tail of buffer.

Definition at line 407 of file ring_buffer.hpp.

407{ return add_copy(base_type::request<Tp>()); }

◆ retrieve()

template<typename Tp >
Tp * tim::data_storage::ring_buffer< Tp >::retrieve ( )
inline

Read data from head of buffer.

Definition at line 410 of file ring_buffer.hpp.

410{ return remove_copy(base_type::retrieve<Tp>()); }

◆ rewind()

template<typename Tp >
size_t tim::data_storage::ring_buffer< Tp >::rewind ( size_t  n) const
inline

Rewinds the read pointer.

Definition at line 425 of file ring_buffer.hpp.

425{ return base_type::rewind(n); }
size_t rewind(size_t n) const
Rewind the read position n bytes.

References tim::base::ring_buffer::rewind().

◆ set_use_mmap()

template<typename Tp >
void tim::base::ring_buffer::set_use_mmap ( bool  _v)

explicitly configure to use mmap if avail

Definition at line 131 of file ring_buffer.cpp.

206{
207 if(m_init)
208 throw std::runtime_error("tim::base::ring_buffer::set_use_mmap(bool) cannot be "
209 "called after initialization");
210 m_use_mmap = _v;
211 m_use_mmap_explicit = true;
212}

◆ write()

template<typename Tp >
Tp * tim::data_storage::ring_buffer< Tp >::write ( Tp *  in)
inline

Write data to buffer. Return pointer to location of write.

Definition at line 401 of file ring_buffer.hpp.

401{ return add_copy(base_type::write<Tp>(in).second); }

Referenced by tim::data_storage::ring_buffer< Tp >::emplace().

Friends And Related Function Documentation

◆ operator<<

template<typename Tp >
std::ostream & operator<< ( std::ostream &  os,
const ring_buffer< Tp > &  obj 
)
friend

Definition at line 456 of file ring_buffer.hpp.

457 {
458 return os << obj.as_string();
459 }
const std::string std::ostream * os

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