add assertions to try to track threading errors

This commit is contained in:
gmega 2023-11-09 14:35:50 -03:00
parent 3c6ef3019f
commit 23f63251f5
No known key found for this signature in database
GPG Key ID: FFD8DAF00660270F
1 changed files with 16 additions and 0 deletions

View File

@ -16,6 +16,7 @@ when defined(metrics):
init: bool init: bool
lastSample: Time lastSample: Time
collections*: uint collections*: uint
threadId: int
PerfSampler = proc (): MetricsSummary {.raises: [].} PerfSampler = proc (): MetricsSummary {.raises: [].}
@ -70,6 +71,9 @@ when defined(metrics):
"the largest chronos_single_exec_time_max of all procs", "the largest chronos_single_exec_time_max of all procs",
) )
let moduleInitThread = getThreadId()
echo "MODULE INIT THREAD: ", getThreadId()
proc newCollector*( proc newCollector*(
AsyncProfilerInfo: typedesc, AsyncProfilerInfo: typedesc,
perfSampler: PerfSampler, perfSampler: PerfSampler,
@ -83,6 +87,7 @@ when defined(metrics):
sampleInterval: sampleInterval, sampleInterval: sampleInterval,
init: true, init: true,
lastSample: low(Time), lastSample: low(Time),
threadId: getThreadId(),
) )
proc collectSlowestProcs( proc collectSlowestProcs(
@ -140,6 +145,10 @@ when defined(metrics):
chronos_largest_exec_time_max.set(largestMaxExecTime.nanoseconds) chronos_largest_exec_time_max.set(largestMaxExecTime.nanoseconds)
proc collect*(self: AsyncProfilerInfo, force: bool = false): void = 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() let now = self.clock()
if not force and (now - self.lastSample < self.sampleInterval): if not force and (now - self.lastSample < self.sampleInterval):
@ -184,6 +193,13 @@ when defined(metrics):
var asyncProfilerInfo* {.global.}: AsyncProfilerInfo var asyncProfilerInfo* {.global.}: AsyncProfilerInfo
proc initDefault*(AsyncProfilerInfo: typedesc, k: int) = 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( asyncProfilerInfo = AsyncProfilerInfo.newCollector(
perfSampler = getFutureSummaryMetrics, perfSampler = getFutureSummaryMetrics,
k = k, k = k,