Offload TSC -> time conversion to server.

This commit is contained in:
Bartosz Taudul 2017-09-26 00:13:24 +02:00
parent 27e1952cc5
commit 11a790a18f
4 changed files with 20 additions and 12 deletions

View File

@ -86,7 +86,7 @@ int64_t Profiler::GetTime()
{ {
#if defined _MSC_VER || defined __CYGWIN__ #if defined _MSC_VER || defined __CYGWIN__
unsigned int ui; unsigned int ui;
return int64_t( __rdtscp( &ui ) * s_instance->m_timerMul ); return int64_t( __rdtscp( &ui ) );
#else #else
return std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count(); return std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
#endif #endif
@ -151,6 +151,7 @@ void Profiler::Worker()
#else #else
welcome.lz4 = 1; welcome.lz4 = 1;
#endif #endif
welcome.timerMul = m_timerMul;
welcome.timeBegin = m_timeBegin; welcome.timeBegin = m_timeBegin;
welcome.delay = m_delay; welcome.delay = m_delay;
@ -282,6 +283,8 @@ void Profiler::CalibrateTimer()
const auto dr = r1 - r0; const auto dr = r1 - r0;
m_timerMul = double( dt ) / double( dr ); m_timerMul = double( dt ) / double( dr );
#else
m_timerMul = 1.;
#endif #endif
} }

View File

@ -26,6 +26,7 @@ enum ServerQuery : uint8_t
struct WelcomeMessage struct WelcomeMessage
{ {
uint8_t lz4; uint8_t lz4;
double timerMul;
uint64_t timeBegin; uint64_t timeBegin;
uint64_t delay; uint64_t delay;
}; };

View File

@ -84,8 +84,9 @@ void View::Worker()
WelcomeMessage welcome; WelcomeMessage welcome;
if( !m_sock.Read( &welcome, sizeof( welcome ), &tv, ShouldExit ) ) goto close; if( !m_sock.Read( &welcome, sizeof( welcome ), &tv, ShouldExit ) ) goto close;
lz4 = welcome.lz4; lz4 = welcome.lz4;
m_frames.push_back( welcome.timeBegin ); m_timerMul = welcome.timerMul;
m_delay = welcome.delay; m_frames.push_back( welcome.timeBegin * m_timerMul );
m_delay = welcome.delay * m_timerMul;
} }
m_hasData.store( true, std::memory_order_release ); m_hasData.store( true, std::memory_order_release );
@ -232,7 +233,7 @@ void View::ProcessZoneBegin( uint64_t id, const QueueZoneBegin& ev )
CheckString( ev.filename ); CheckString( ev.filename );
CheckString( ev.function ); CheckString( ev.function );
CheckThreadString( ev.thread ); CheckThreadString( ev.thread );
zone->start = ev.time; zone->start = ev.time * m_timerMul;
zone->color = ev.color; zone->color = ev.color;
SourceLocation srcloc { ev.filename, ev.function, ev.line }; SourceLocation srcloc { ev.filename, ev.function, ev.line };
@ -260,8 +261,8 @@ void View::ProcessZoneBegin( uint64_t id, const QueueZoneBegin& ev )
} }
else else
{ {
assert( ev.time <= it->second.time ); zone->end = it->second.time * m_timerMul;
zone->end = it->second.time; assert( zone->start <= zone->end );
NewZone( zone, ev.thread ); NewZone( zone, ev.thread );
lock.unlock(); lock.unlock();
m_pendingEndZone.erase( it ); m_pendingEndZone.erase( it );
@ -279,8 +280,9 @@ void View::ProcessZoneEnd( uint64_t id, const QueueZoneEnd& ev )
{ {
auto zone = it->second; auto zone = it->second;
std::unique_lock<std::mutex> lock( m_lock ); std::unique_lock<std::mutex> lock( m_lock );
assert( ev.time >= zone->start ); assert( zone->end == -1 );
zone->end = ev.time; zone->end = ev.time * m_timerMul;
assert( zone->end >= zone->start );
UpdateZone( zone ); UpdateZone( zone );
lock.unlock(); lock.unlock();
m_openZones.erase( it ); m_openZones.erase( it );
@ -291,16 +293,17 @@ void View::ProcessFrameMark( uint64_t id )
{ {
assert( !m_frames.empty() ); assert( !m_frames.empty() );
const auto lastframe = m_frames.back(); const auto lastframe = m_frames.back();
if( lastframe < id ) const auto time = id * m_timerMul;
if( lastframe < time )
{ {
std::unique_lock<std::mutex> lock( m_lock ); std::unique_lock<std::mutex> lock( m_lock );
m_frames.push_back( id ); m_frames.push_back( time );
} }
else else
{ {
auto it = std::lower_bound( m_frames.begin(), m_frames.end(), id ); auto it = std::lower_bound( m_frames.begin(), m_frames.end(), time );
std::unique_lock<std::mutex> lock( m_lock ); std::unique_lock<std::mutex> lock( m_lock );
m_frames.insert( it, id ); m_frames.insert( it, time );
} }
} }

View File

@ -118,6 +118,7 @@ private:
int64_t m_zvEnd; int64_t m_zvEnd;
uint64_t m_delay; uint64_t m_delay;
double m_timerMul;
}; };
} }