Thread-safe event collector for op profiling.
Holds a vector of OpEvent records and a single mutex
protecting both the active flag and the event list. Multiple
OpScope instances on different threads may concurrently
call record — the mutex serialises insertions.
See Also
OpScope — RAII recorder that feeds events into this sink.
set_current_profiler — thread-local registration.
Attributes
active_boolstart and stop.events_std::vector<OpEvent>mu_.mu_mutable std::mutexactive_ or events_.Notes
Typical lifecycle::
Profiler prof; set_current_profiler(&prof); prof.start(); // ... execute ops ... prof.stop(); auto events = prof.events(); set_current_profiler(nullptr);
The Python wrapper lucid.profiler.Profiler runs this exact
sequence inside __enter__ / __exit__.
Methods
6Removes all stored events without changing the active state.
Notes
Resets events_.size() to zero; the active/stopped state of
the profiler is left untouched, so a running profiler keeps
collecting new events afterwards.
Returns a copy of the recorded event list.
Returns
std::vector<OpEvent>Snapshot of all events recorded so far, copied under mu_. Caller can iterate without holding the lock.
Notes
Returns by value to decouple iteration from concurrent writers;
the cost is one OpEvent vector copy per call.
Returns whether the profiler is currently recording.
Returns
booltrue between start and stop, false otherwise.
Notes
Read without acquiring the mutex — the result is a snapshot and
may be stale by the time the caller acts on it. The internal
record() call re-checks under the lock to avoid races.
Appends event to the recorded list if the profiler is active.
Parameters
eventOpEventevents_.Notes
Thread-safe. Called by OpScope on destruction.
Re-checks is_active under mu_ so events that race
with stop are discarded rather than appended.
Enables event collection.
Notes
After this call, every OpScope destructor whose
sink_ points at this profiler will append its event to
events_. Calling start on an already-active
profiler is idempotent.
Disables event collection without clearing recorded events.
Notes
Events already in events_ are preserved — call
clear to drop them. In-flight OpScope
destructors may still record their event if they raced with the
stop() write; record re-checks active_ under the
mutex to discard such latecomers.