mirror of
https://github.com/wolfpld/tracy
synced 2025-04-29 12:23:53 +00:00
RingBuffer has const size, so use template.
This eliminates division.
This commit is contained in:
parent
caa1b1a792
commit
28aae73f74
@ -1,17 +1,17 @@
|
|||||||
namespace tracy
|
namespace tracy
|
||||||
{
|
{
|
||||||
|
|
||||||
|
template<size_t Size>
|
||||||
class RingBuffer
|
class RingBuffer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RingBuffer( uint32_t size, int fd )
|
RingBuffer( int fd )
|
||||||
: m_size( size )
|
: m_fd( fd )
|
||||||
, m_fd( fd )
|
|
||||||
{
|
{
|
||||||
const auto pageSize = uint32_t( getpagesize() );
|
const auto pageSize = uint32_t( getpagesize() );
|
||||||
assert( size >= pageSize );
|
assert( Size >= pageSize );
|
||||||
assert( __builtin_popcount( size ) == 1 );
|
assert( __builtin_popcount( Size ) == 1 );
|
||||||
m_mapSize = size + pageSize;
|
m_mapSize = Size + pageSize;
|
||||||
m_mapAddr = mmap( nullptr, m_mapSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
|
m_mapAddr = mmap( nullptr, m_mapSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
|
||||||
if( !m_mapAddr )
|
if( !m_mapAddr )
|
||||||
{
|
{
|
||||||
@ -63,14 +63,14 @@ public:
|
|||||||
|
|
||||||
void Read( void* dst, uint64_t offset, uint64_t cnt )
|
void Read( void* dst, uint64_t offset, uint64_t cnt )
|
||||||
{
|
{
|
||||||
auto src = ( m_metadata->data_tail + offset ) % m_size;
|
auto src = ( m_metadata->data_tail + offset ) % Size;
|
||||||
if( src + cnt <= m_size )
|
if( src + cnt <= Size )
|
||||||
{
|
{
|
||||||
memcpy( dst, m_buffer + src, cnt );
|
memcpy( dst, m_buffer + src, cnt );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const auto s0 = m_size - src;
|
const auto s0 = Size - src;
|
||||||
memcpy( dst, m_buffer + src, s0 );
|
memcpy( dst, m_buffer + src, s0 );
|
||||||
memcpy( (char*)dst + s0, m_buffer, cnt - s0 );
|
memcpy( (char*)dst + s0, m_buffer, cnt - s0 );
|
||||||
}
|
}
|
||||||
@ -106,7 +106,6 @@ private:
|
|||||||
std::atomic_store_explicit( (volatile std::atomic<uint64_t>*)&m_metadata->data_tail, tail, std::memory_order_release );
|
std::atomic_store_explicit( (volatile std::atomic<uint64_t>*)&m_metadata->data_tail, tail, std::memory_order_release );
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t m_size;
|
|
||||||
size_t m_mapSize;
|
size_t m_mapSize;
|
||||||
void* m_mapAddr;
|
void* m_mapAddr;
|
||||||
|
|
||||||
|
@ -620,7 +620,9 @@ static const char TracePipe[] = "trace_pipe";
|
|||||||
static std::atomic<bool> traceActive { false };
|
static std::atomic<bool> traceActive { false };
|
||||||
static Thread* s_threadSampling = nullptr;
|
static Thread* s_threadSampling = nullptr;
|
||||||
static int s_numCpus = 0;
|
static int s_numCpus = 0;
|
||||||
static RingBuffer* s_ring = nullptr;
|
|
||||||
|
static constexpr size_t RingBufSize = 64*1024;
|
||||||
|
static RingBuffer<RingBufSize>* s_ring = nullptr;
|
||||||
|
|
||||||
static int perf_event_open( struct perf_event_attr* hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags )
|
static int perf_event_open( struct perf_event_attr* hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags )
|
||||||
{
|
{
|
||||||
@ -636,7 +638,7 @@ static void SetupSampling( int64_t& samplingPeriod )
|
|||||||
samplingPeriod = 100*1000;
|
samplingPeriod = 100*1000;
|
||||||
|
|
||||||
s_numCpus = (int)std::thread::hardware_concurrency();
|
s_numCpus = (int)std::thread::hardware_concurrency();
|
||||||
s_ring = (RingBuffer*)tracy_malloc( sizeof( RingBuffer ) * s_numCpus );
|
s_ring = (RingBuffer<RingBufSize>*)tracy_malloc( sizeof( RingBuffer<RingBufSize> ) * s_numCpus );
|
||||||
|
|
||||||
perf_event_attr pe = {};
|
perf_event_attr pe = {};
|
||||||
|
|
||||||
@ -661,11 +663,11 @@ static void SetupSampling( int64_t& samplingPeriod )
|
|||||||
const int fd = perf_event_open( &pe, -1, i, -1, 0 );
|
const int fd = perf_event_open( &pe, -1, i, -1, 0 );
|
||||||
if( fd == -1 )
|
if( fd == -1 )
|
||||||
{
|
{
|
||||||
for( int j=0; j<i; j++ ) s_ring[j].~RingBuffer();
|
for( int j=0; j<i; j++ ) s_ring[j].~RingBuffer<RingBufSize>();
|
||||||
tracy_free( s_ring );
|
tracy_free( s_ring );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
new( s_ring+i ) RingBuffer( 64*1024, fd );
|
new( s_ring+i ) RingBuffer<RingBufSize>( fd );
|
||||||
}
|
}
|
||||||
|
|
||||||
s_threadSampling = (Thread*)tracy_malloc( sizeof( Thread ) );
|
s_threadSampling = (Thread*)tracy_malloc( sizeof( Thread ) );
|
||||||
@ -680,7 +682,7 @@ static void SetupSampling( int64_t& samplingPeriod )
|
|||||||
{
|
{
|
||||||
if( !s_ring[i].CheckTscCaps() )
|
if( !s_ring[i].CheckTscCaps() )
|
||||||
{
|
{
|
||||||
for( int j=0; j<s_numCpus; j++ ) s_ring[j].~RingBuffer();
|
for( int j=0; j<s_numCpus; j++ ) s_ring[j].~RingBuffer<RingBufSize>();
|
||||||
tracy_free( s_ring );
|
tracy_free( s_ring );
|
||||||
const char* err = "Tracy Profiler: sampling is disabled due to non-native scheduler clock. Are you running under a VM?";
|
const char* err = "Tracy Profiler: sampling is disabled due to non-native scheduler clock. Are you running under a VM?";
|
||||||
Profiler::MessageAppInfo( err, strlen( err ) );
|
Profiler::MessageAppInfo( err, strlen( err ) );
|
||||||
@ -760,7 +762,7 @@ static void SetupSampling( int64_t& samplingPeriod )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for( int i=0; i<s_numCpus; i++ ) s_ring[i].~RingBuffer();
|
for( int i=0; i<s_numCpus; i++ ) s_ring[i].~RingBuffer<RingBufSize>();
|
||||||
tracy_free( s_ring );
|
tracy_free( s_ring );
|
||||||
}, nullptr );
|
}, nullptr );
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user