Parallelize background jobs.

This commit is contained in:
Bartosz Taudul 2020-03-18 02:02:37 +01:00
parent a48e804e96
commit 1f4dbd1b2e

View File

@ -1668,16 +1668,16 @@ Worker::Worker( FileRead& f, EventType::Type eventMask, bool bgTasks )
m_backgroundDone.store( false, std::memory_order_relaxed ); m_backgroundDone.store( false, std::memory_order_relaxed );
#ifndef TRACY_NO_STATISTICS #ifndef TRACY_NO_STATISTICS
m_threadBackground = std::thread( [this, reconstructMemAllocPlot, eventMask] { m_threadBackground = std::thread( [this, reconstructMemAllocPlot, eventMask] {
std::vector<std::thread> jobs;
if( !m_data.ctxSwitch.empty() ) if( !m_data.ctxSwitch.empty() )
{ {
ReconstructContextSwitchUsage(); jobs.emplace_back( std::thread( [this] { ReconstructContextSwitchUsage(); } ) );
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
} }
if( reconstructMemAllocPlot ) if( reconstructMemAllocPlot )
{ {
ReconstructMemAllocPlot(); jobs.emplace_back( std::thread( [this] { ReconstructMemAllocPlot(); } ) );
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
} }
std::function<void(Vector<short_ptr<ZoneEvent>>&, uint16_t)> ProcessTimeline; std::function<void(Vector<short_ptr<ZoneEvent>>&, uint16_t)> ProcessTimeline;
@ -1693,6 +1693,7 @@ Worker::Worker( FileRead& f, EventType::Type eventMask, bool bgTasks )
} }
}; };
jobs.emplace_back( std::thread( [this, ProcessTimeline] {
for( auto& t : m_data.threads ) for( auto& t : m_data.threads )
{ {
if( m_shutdown.load( std::memory_order_relaxed ) ) return; if( m_shutdown.load( std::memory_order_relaxed ) ) return;
@ -1702,23 +1703,11 @@ Worker::Worker( FileRead& f, EventType::Type eventMask, bool bgTasks )
ProcessTimeline( t->timeline, m_data.localThreadCompress.DecompressMustRaw( t->id ) ); ProcessTimeline( t->timeline, m_data.localThreadCompress.DecompressMustRaw( t->id ) );
} }
} }
for( auto& v : m_data.sourceLocationZones ) } ) );
{
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
auto& zones = v.second.zones;
#ifdef NO_PARALLEL_SORT
pdqsort_branchless( zones.begin(), zones.end(), []( const auto& lhs, const auto& rhs ) { return lhs.Zone()->Start() < rhs.Zone()->Start(); } );
#else
std::sort( std::execution::par_unseq, zones.begin(), zones.end(), []( const auto& lhs, const auto& rhs ) { return lhs.Zone()->Start() < rhs.Zone()->Start(); } );
#endif
}
{
std::lock_guard<std::shared_mutex> lock( m_data.lock );
m_data.sourceLocationZonesReady = true;
}
if( eventMask & EventType::Samples ) if( eventMask & EventType::Samples )
{ {
jobs.emplace_back( std::thread( [this] {
unordered_flat_map<uint32_t, uint32_t> counts; unordered_flat_map<uint32_t, uint32_t> counts;
uint32_t total = 0; uint32_t total = 0;
for( auto& t : m_data.threads ) total += t->samples.size(); for( auto& t : m_data.threads ) total += t->samples.size();
@ -1743,12 +1732,11 @@ Worker::Worker( FileRead& f, EventType::Type eventMask, bool bgTasks )
} }
for( auto& v : counts ) UpdateSampleStatistics( v.first, v.second, false ); for( auto& v : counts ) UpdateSampleStatistics( v.first, v.second, false );
} }
{
std::lock_guard<std::shared_mutex> lock( m_data.lock ); std::lock_guard<std::shared_mutex> lock( m_data.lock );
m_data.callstackSamplesReady = true; m_data.callstackSamplesReady = true;
} } ) );
jobs.emplace_back( std::thread( [this] {
uint32_t gcnt = 0; uint32_t gcnt = 0;
for( auto& t : m_data.threads ) for( auto& t : m_data.threads )
{ {
@ -1837,10 +1825,27 @@ Worker::Worker( FileRead& f, EventType::Type eventMask, bool bgTasks )
while( idx-- > 0 ); while( idx-- > 0 );
} }
} }
std::lock_guard<std::shared_mutex> lock( m_data.lock ); std::lock_guard<std::shared_mutex> lock( m_data.lock );
m_data.ghostZonesReady = true; m_data.ghostZonesReady = true;
m_data.ghostCnt = gcnt; m_data.ghostCnt = gcnt;
} ) );
}
for( auto& job : jobs ) job.join();
for( auto& v : m_data.sourceLocationZones )
{
if( m_shutdown.load( std::memory_order_relaxed ) ) return;
auto& zones = v.second.zones;
#ifdef NO_PARALLEL_SORT
pdqsort_branchless( zones.begin(), zones.end(), []( const auto& lhs, const auto& rhs ) { return lhs.Zone()->Start() < rhs.Zone()->Start(); } );
#else
std::sort( std::execution::par_unseq, zones.begin(), zones.end(), []( const auto& lhs, const auto& rhs ) { return lhs.Zone()->Start() < rhs.Zone()->Start(); } );
#endif
}
{
std::lock_guard<std::shared_mutex> lock( m_data.lock );
m_data.sourceLocationZonesReady = true;
} }
m_backgroundDone.store( true, std::memory_order_relaxed ); m_backgroundDone.store( true, std::memory_order_relaxed );