2024-02-29 17:55:45 -03:00
|
|
|
import chronos/futures
|
|
|
|
|
import ./[profiler, events]
|
|
|
|
|
|
2024-02-29 20:54:00 -03:00
|
|
|
export Event, ExtendedFutureState, ProfilerState, MetricsTotals,
|
|
|
|
|
AggregateMetrics, FutureType, execTimeWithChildren
|
|
|
|
|
|
|
|
|
|
type EventCallback* = proc (e: Event) {.nimcall, gcsafe, raises: [].}
|
2024-02-29 17:55:45 -03:00
|
|
|
|
|
|
|
|
var profilerInstance {.threadvar.}: ProfilerState
|
|
|
|
|
|
|
|
|
|
proc getMetrics*(): MetricsTotals =
|
|
|
|
|
## Returns the `MetricsTotals` for the event loop running in the
|
|
|
|
|
## current thread.
|
|
|
|
|
result = profilerInstance.metrics
|
|
|
|
|
|
|
|
|
|
proc enableEventCallbacks*(callback: EventCallback): void =
|
|
|
|
|
onBaseFutureEvent = handleBaseFutureEvent
|
|
|
|
|
onAsyncFutureEvent = handleAsyncFutureEvent
|
|
|
|
|
handleFutureEvent = callback
|
|
|
|
|
|
2024-02-29 20:54:00 -03:00
|
|
|
proc enableProfiling*(clientCallback: EventCallback = nil) =
|
2024-02-29 17:55:45 -03:00
|
|
|
## Enables profiling for the the event loop running in the current thread.
|
|
|
|
|
handleFutureEvent = proc (e: Event) {.nimcall.} =
|
|
|
|
|
profilerInstance.processEvent(e)
|
2024-02-29 20:54:00 -03:00
|
|
|
if not isNil(clientCallback): clientCallback(e)
|