Skip to content

Commit

Permalink
Add perfetto track counters to accel LocalTransporter (#1634)
Browse files Browse the repository at this point in the history
  • Loading branch information
esseivaju authored Feb 21, 2025
1 parent caeafdb commit 6cd2f98
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
49 changes: 48 additions & 1 deletion src/accel/LocalTransporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@
#include "corecel/io/Logger.hh"
#include "corecel/sys/Device.hh"
#include "corecel/sys/Environment.hh"
#include "corecel/sys/ScopedProfiling.hh"
#include "corecel/sys/ScopedSignalHandler.hh"
#include "corecel/sys/TraceCounter.hh"
#include "corecel/sys/TracingSession.hh"
#include "geocel/GeantUtils.hh"
#include "geocel/g4/Convert.hh"
#include "celeritas/Quantities.hh"
#include "celeritas/ext/GeantSd.hh"
#include "celeritas/ext/GeantUnits.hh"
#include "celeritas/global/ActionSequence.hh"
#include "celeritas/global/Stepper.hh"
#include "celeritas/io/EventWriter.hh"
#include "celeritas/io/RootEventWriter.hh"
#include "celeritas/phys/PDGNumber.hh"
Expand All @@ -58,6 +62,45 @@ bool nonfatal_flush()
return result;
}

//---------------------------------------------------------------------------//
//! Trace the number of active, alive, dead, and queued tracks
class TrackCounters
{
public:
TrackCounters()
{
if (use_profiling())
{
std::string stream_id = std::to_string(get_geant_thread_id());
active_counter_ = std::string("active-" + stream_id);
alive_counter_ = std::string("alive-" + stream_id);
dead_counter_ = std::string("dead-" + stream_id);
queued_counter_ = std::string("queued-" + stream_id);
}
};

void operator()(StepperResult const& track_counts) const
{
trace_counter(active_counter_.c_str(), track_counts.active);
trace_counter(alive_counter_.c_str(), track_counts.alive);
trace_counter(dead_counter_.c_str(),
track_counts.active - track_counts.alive);
trace_counter(queued_counter_.c_str(), track_counts.queued);
}

private:
std::string active_counter_;
std::string alive_counter_;
std::string dead_counter_;
std::string queued_counter_;
};

void trace(StepperResult const& track_counts)
{
static thread_local TrackCounters const trace_;
trace_(track_counts);
}

#define CELER_VALIDATE_OR_KILL_ACTIVE(COND, MSG, STEPPER) \
do \
{ \
Expand Down Expand Up @@ -321,6 +364,7 @@ void LocalTransporter::Flush()
run_accum_.steps += track_counts.active;
run_accum_.primaries += buffer_.size();
run_accum_.lost_primaries += buffer_accum_.lost_primaries;
trace(track_counts);

buffer_.clear();
buffer_accum_ = {};
Expand All @@ -338,7 +382,7 @@ void LocalTransporter::Flush()
track_counts = (*step_)();
run_accum_.steps += track_counts.active;
++step_iters;

trace(track_counts);
CELER_VALIDATE_OR_KILL_ACTIVE(
!interrupted(), << "caught interrupt signal", *step_);
}
Expand Down Expand Up @@ -382,6 +426,9 @@ void LocalTransporter::Finalize()
#endif
}

// Flush any remaining track counters on the worker thread
flush_tracing();

// Reset all data
CELER_LOG_LOCAL(debug) << "Resetting local transporter";
*this = {};
Expand Down
10 changes: 7 additions & 3 deletions src/corecel/sys/TraceCounter.perfetto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <perfetto.h>

#include "corecel/Types.hh"
#include "corecel/sys/ScopedProfiling.hh"

#include "detail/TrackEvent.perfetto.hh"

Expand All @@ -31,9 +32,12 @@ template<class T>
void trace_counter(char const* name, T value)
{
static_assert(std::is_arithmetic_v<T>, "Only support numeric counters");
TRACE_COUNTER(detail::perfetto_track_event_category,
::perfetto::DynamicString{name},
value);
if (use_profiling())
{
TRACE_COUNTER(detail::perfetto_track_event_category,
::perfetto::DynamicString{name},
value);
}
}

//---------------------------------------------------------------------------//
Expand Down
16 changes: 15 additions & 1 deletion src/corecel/sys/TracingSession.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ perfetto::TraceConfig configure_session() noexcept
//---------------------------------------------------------------------------//
} // namespace

//---------------------------------------------------------------------------//
/*!
* Perform the same action a \c TracingSession::flush, however, it does not
* require a session instance. This is useful in geant4 applications, where
* workers do not have access to the session instance.
*/
void flush_tracing() noexcept
{
if (use_profiling())
{
perfetto::TrackEvent::Flush();
}
}

//---------------------------------------------------------------------------//
/*!
* Start a system tracing session.
Expand Down Expand Up @@ -162,7 +176,7 @@ void TracingSession::flush() noexcept
{
if (session_ && started_)
{
perfetto::TrackEvent::Flush();
flush_tracing();
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/corecel/sys/TracingSession.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class TracingSession;

namespace celeritas
{
//---------------------------------------------------------------------------//
// Flush perfetto track events without requiring a TracingSession instance.
void flush_tracing() noexcept;

//---------------------------------------------------------------------------//
/*!
* RAII wrapper for a tracing session.
Expand Down Expand Up @@ -79,7 +83,7 @@ class TracingSession
//---------------------------------------------------------------------------//

#if !CELERITAS_USE_PERFETTO

inline void flush_tracing() noexcept {}
inline TracingSession::TracingSession() noexcept = default;
inline TracingSession::TracingSession(std::string_view) noexcept {}
inline TracingSession::~TracingSession() = default;
Expand Down

0 comments on commit 6cd2f98

Please sign in to comment.