From 23f63251f5be7139d6e08bd76f63d5c8817d5ab0 Mon Sep 17 00:00:00 2001 From: gmega Date: Thu, 9 Nov 2023 14:35:50 -0300 Subject: [PATCH] add assertions to try to track threading errors --- codex/utils/asyncprofiler/metricscollector.nim | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/codex/utils/asyncprofiler/metricscollector.nim b/codex/utils/asyncprofiler/metricscollector.nim index edc0142b..d8862c87 100644 --- a/codex/utils/asyncprofiler/metricscollector.nim +++ b/codex/utils/asyncprofiler/metricscollector.nim @@ -16,6 +16,7 @@ when defined(metrics): init: bool lastSample: Time collections*: uint + threadId: int PerfSampler = proc (): MetricsSummary {.raises: [].} @@ -70,6 +71,9 @@ when defined(metrics): "the largest chronos_single_exec_time_max of all procs", ) + let moduleInitThread = getThreadId() + echo "MODULE INIT THREAD: ", getThreadId() + proc newCollector*( AsyncProfilerInfo: typedesc, perfSampler: PerfSampler, @@ -83,6 +87,7 @@ when defined(metrics): sampleInterval: sampleInterval, init: true, lastSample: low(Time), + threadId: getThreadId(), ) proc collectSlowestProcs( @@ -140,6 +145,10 @@ when defined(metrics): chronos_largest_exec_time_max.set(largestMaxExecTime.nanoseconds) proc collect*(self: AsyncProfilerInfo, force: bool = false): void = + if getThreadId() != self.threadId: + raise (ref Defect)(msg: "AsyncProfilerInfo.collect() called from a different thread" & + " than the one it was initialized with.") + let now = self.clock() if not force and (now - self.lastSample < self.sampleInterval): @@ -184,6 +193,13 @@ when defined(metrics): var asyncProfilerInfo* {.global.}: AsyncProfilerInfo proc initDefault*(AsyncProfilerInfo: typedesc, k: int) = + + echo "INIT DEFAULT THREAD: ", getThreadId() + + if moduleInitThread != getThreadId(): + raise (ref Defect)(msg: "AsyncProfilerInfo.initDefault() called from a different thread" & + " than the one it was initialized with.") + asyncProfilerInfo = AsyncProfilerInfo.newCollector( perfSampler = getFutureSummaryMetrics, k = k,