diff --git a/chroprof/api.nim b/chroprof/api.nim index f332c4e..2707360 100644 --- a/chroprof/api.nim +++ b/chroprof/api.nim @@ -11,22 +11,18 @@ proc getMetrics*(): MetricsTotals = ## current thread. result = profilerInstance.metrics -template enableProfiling*(callback: EventCallback, maxExecThreshold: Duration) = +template enableProfiling*(callback: EventCallback, threshold: Duration) = ## Enables profiling for the the event loop running in the current thread. ## The client may optionally supply a callback to be notified of `Future` ## events. - attachMonitoring( - proc(e: Event) {.nimcall, gcsafe, raises: [].} = - profilerInstance.processEvent(e) - callback(e) - , - maxExecThreshold = maxExecThreshold - ) + profilerInstance.maxExecThreshold = threshold + proc onProfilerEvent(e: Event) {.nimcall, gcsafe, raises: [].} = + profilerInstance.processEvent(e) + callback(e) + attachMonitoring(onProfilerEvent) -template enableProfiling*(maxExecThreshold: Duration) = - attachMonitoring( - proc(e: Event) {.nimcall, gcsafe, raises: [].} = - profilerInstance.processEvent(e) - , - maxExecThreshold = maxExecThreshold - ) +template enableProfiling*(threshold: Duration) = + profilerInstance.maxExecThreshold = threshold + proc onProfilerEvent(e: Event) {.nimcall, gcsafe, raises: [].} = + profilerInstance.processEvent(e) + attachMonitoring(onProfilerEvent) diff --git a/chroprof/collector.nim b/chroprof/collector.nim index bf172ab..b77be2d 100644 --- a/chroprof/collector.nim +++ b/chroprof/collector.nim @@ -173,7 +173,7 @@ when defined(metrics): var asyncProfilerInfo* {.global.}: ChronosProfilerInfo - proc enableProfilerMetrics*(k: int, maxExecThreshold: Duration) = + proc enableProfilerMetrics*(k: int, maxExecThreshold: timer.Duration) = assert threadId() == moduleInitThread, "You cannot call enableProfilerMetrics() from a thread other than" & " the one that initialized the metricscolletor module." @@ -194,5 +194,5 @@ when defined(metrics): if e.newState == ExtendedFutureState.Completed: asyncProfilerInfo.collect() , - maxExecThreshold = maxExecThreshold + maxExecThreshold ) diff --git a/chroprof/profiler.nim b/chroprof/profiler.nim index e056d56..94e2743 100644 --- a/chroprof/profiler.nim +++ b/chroprof/profiler.nim @@ -70,6 +70,7 @@ type MetricsTotals* = Table[FutureType, AggregateMetrics] ProfilerState* = object + maxExecThreshold*: Duration callStack: seq[uint] partials: Table[uint, PartialMetrics] metrics*: MetricsTotals @@ -142,9 +143,9 @@ proc aggregatePartial( self.metrics.withValue(metrics.futureType, aggMetrics): let execTime = metrics.partialExecTime - metrics.partialChildrenExecOverlap - if execTime > threshold: + if execTime > self.maxExecThreshold: raiseAssert("Execution time of future is too high (" & - $metrics.futureType & ")" & $execTime) + $metrics.futureType & ") time: " & $execTime & " threshold: " & $self.maxExecThreshold) aggMetrics.callCount.inc() aggMetrics.execTime += execTime