tracy/client/TracyRingBuffer.hpp

124 lines
3.0 KiB
C++
Raw Normal View History

2020-08-12 12:01:02 +00:00
namespace tracy
{
template<size_t Size>
2020-08-12 12:01:02 +00:00
class RingBuffer
{
public:
2021-05-18 23:25:13 +00:00
RingBuffer( int fd, int id )
: m_id( id )
, m_fd( fd )
2020-08-12 12:01:02 +00:00
{
const auto pageSize = uint32_t( getpagesize() );
assert( Size >= pageSize );
assert( __builtin_popcount( Size ) == 1 );
m_mapSize = Size + pageSize;
2020-08-15 11:40:36 +00:00
auto mapAddr = mmap( nullptr, m_mapSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
if( !mapAddr )
2020-08-12 12:01:02 +00:00
{
m_fd = 0;
2021-06-02 22:53:49 +00:00
m_metadata = nullptr;
2020-08-12 12:01:02 +00:00
close( fd );
return;
}
2020-08-15 11:40:36 +00:00
m_metadata = (perf_event_mmap_page*)mapAddr;
2020-08-12 12:01:02 +00:00
assert( m_metadata->data_offset == pageSize );
2020-08-15 11:40:36 +00:00
m_buffer = ((char*)mapAddr) + pageSize;
m_tail = m_metadata->data_tail;
2020-08-12 12:01:02 +00:00
}
~RingBuffer()
{
2020-08-15 11:40:36 +00:00
if( m_metadata ) munmap( m_metadata, m_mapSize );
2020-08-12 12:01:02 +00:00
if( m_fd ) close( m_fd );
}
RingBuffer( const RingBuffer& ) = delete;
RingBuffer& operator=( const RingBuffer& ) = delete;
RingBuffer( RingBuffer&& other )
{
memcpy( (char*)&other, (char*)this, sizeof( RingBuffer ) );
2020-08-15 11:40:36 +00:00
m_metadata = nullptr;
2020-08-12 12:01:02 +00:00
m_fd = 0;
}
RingBuffer& operator=( RingBuffer&& other )
{
memcpy( (char*)&other, (char*)this, sizeof( RingBuffer ) );
2020-08-15 11:40:36 +00:00
m_metadata = nullptr;
2020-08-12 12:01:02 +00:00
m_fd = 0;
return *this;
}
2020-08-15 11:40:36 +00:00
bool IsValid() const { return m_metadata != nullptr; }
2021-05-18 23:25:13 +00:00
int GetId() const { return m_id; }
2020-08-12 12:01:02 +00:00
void Enable()
{
ioctl( m_fd, PERF_EVENT_IOC_ENABLE, 0 );
}
bool HasData() const
{
const auto head = LoadHead();
return head > m_tail;
2020-08-12 12:01:02 +00:00
}
void Read( void* dst, uint64_t offset, uint64_t cnt )
{
auto src = ( m_tail + offset ) % Size;
if( src + cnt <= Size )
2020-08-12 12:01:02 +00:00
{
memcpy( dst, m_buffer + src, cnt );
}
else
{
const auto s0 = Size - src;
2020-08-12 12:01:02 +00:00
memcpy( dst, m_buffer + src, s0 );
memcpy( (char*)dst + s0, m_buffer, cnt - s0 );
}
}
void Advance( uint64_t cnt )
{
m_tail += cnt;
StoreTail();
2020-08-12 12:01:02 +00:00
}
bool CheckTscCaps() const
{
return m_metadata->cap_user_time_zero;
}
int64_t ConvertTimeToTsc( int64_t timestamp ) const
{
if( !m_metadata->cap_user_time_zero ) return 0;
const auto time = timestamp - m_metadata->time_zero;
const auto quot = time / m_metadata->time_mult;
const auto rem = time % m_metadata->time_mult;
return ( quot << m_metadata->time_shift ) + ( rem << m_metadata->time_shift ) / m_metadata->time_mult;
}
2020-08-12 12:01:02 +00:00
private:
uint64_t LoadHead() const
{
return std::atomic_load_explicit( (const volatile std::atomic<uint64_t>*)&m_metadata->data_head, std::memory_order_acquire );
}
void StoreTail()
2020-08-12 12:01:02 +00:00
{
std::atomic_store_explicit( (volatile std::atomic<uint64_t>*)&m_metadata->data_tail, m_tail, std::memory_order_release );
2020-08-12 12:01:02 +00:00
}
uint64_t m_tail;
2020-08-12 12:01:02 +00:00
char* m_buffer;
int m_id;
perf_event_mmap_page* m_metadata;
2020-08-12 12:01:02 +00:00
2020-08-15 11:40:36 +00:00
size_t m_mapSize;
2020-08-12 12:01:02 +00:00
int m_fd;
};
}