Skip to content
class

Profiler

Profiler

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_bool
Whether the profiler is currently recording. Toggled by start and stop.
events_std::vector<OpEvent>
Storage for recorded events; appended under mu_.
mu_mutable std::mutex
Synchronises every public method that touches active_ 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

6
fn

clear

void clear()

Removes 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.

fnconst

events

→std::vector<OpEvent>
int events()

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.

fnconst

is_active

→bool
bool is_active()

Returns whether the profiler is currently recording.

Returns

bool

true 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.

fn

record

void record(OpEvent event)

Appends event to the recorded list if the profiler is active.

Parameters

eventOpEvent
The event to store. Moved into events_.

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.

fn

start

void start()

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.

fn

stop

void stop()

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.