trying with future locations

This commit is contained in:
benbierens 2023-07-11 08:43:15 +02:00
parent 0f4cca13a0
commit ce6483e328
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
4 changed files with 60 additions and 7 deletions

View File

@ -315,6 +315,7 @@ proc addCallback*(future: FutureBase, cb: CallbackFunc, udata: pointer = nil) =
callSoon(cb, udata)
else:
var acb = newAsyncCallback(cb, udata)
acb.location = future.location
future.callbacks.add acb

View File

@ -20,6 +20,9 @@ import ./timer
when defined(chronosDurationThreshold):
import std/monotimes
import srcloc
import pkg/metrics
declareGauge(chronosCallbackDuration, "chronos callback duration")
export Port, SocketFlag
export timer
@ -198,7 +201,7 @@ type
function*: CallbackFunc
udata*: pointer
when defined(chronosDurationThreshold):
location*: string # ptr SrcLoc
location*: array[2, ptr SrcLoc]
AsyncError* = object of CatchableError
## Generic async exception
@ -223,10 +226,10 @@ type
const chronosDurationThreshold {.intdefine.} = 0
template newAsyncCallback*(cbFunc: CallbackFunc, udataPtr: pointer = nil): AsyncCallback =
when defined(chronosDurationThreshold):
AsyncCallback(function: cbFunc, udata: udataPtr, location: getStackTrace())
else:
AsyncCallback(function: cbFunc, udata: udataPtr)
# when defined(chronosDurationThreshold):
# AsyncCallback(function: cbFunc, udata: udataPtr, location: getStackTrace())
# else:
AsyncCallback(function: cbFunc, udata: udataPtr)
when defined(chronosDurationThreshold):
type
@ -334,6 +337,7 @@ template processCallbacks(loop: untyped) =
durationNs = getMonoTime().ticks - startTime
durationUs = durationNs div 1000
chronosCallbackDuration.set(durationUs.int64)
if durationUs > chronosDurationThreshold:
# callable.location*: array[2, ptr SrcLoc]
@ -341,8 +345,9 @@ template processCallbacks(loop: untyped) =
# file*: cstring
# line*: int
let loc = callable.location
var trace = "trace: " & loc
var trace = "trace: "
if not(isNil(callable.location[0])): trace = trace & "[0]=" & $callable.location[0]
if not(isNil(callable.location[1])): trace = trace & "[1]=" & $callable.location[1]
invokeChronosDurationThresholdBreachedHandler(trace, durationUs)
proc raiseAsDefect*(exc: ref Exception, msg: string) {.

View File

@ -23,6 +23,7 @@ RUN make clean
RUN make -j ${MAKE_PARALLEL} update
COPY docker/asyncfutures2.nim ./vendor/nim-chronos/chronos
COPY docker/asyncloop.nim ./vendor/nim-chronos/chronos
COPY docker/srcloc.nim ./vendor/nim-chronos/chronos
RUN make -j ${MAKE_PARALLEL}
# Create

46
docker/srcloc.nim Normal file
View File

@ -0,0 +1,46 @@
#
# Chronos source location utilities
# (c) Copyright 2018-Present
# Status Research & Development GmbH
#
# Licensed under either of
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import stew/base10
type
SrcLoc* = object
procedure*: cstring
file*: cstring
line*: int
trace*: cstring
proc `$`*(loc: ptr SrcLoc): string =
var res = $loc.file
res.add("(")
res.add(Base10.toString(uint64(loc.line)))
res.add(")")
res.add(" ")
if len(loc.procedure) == 0:
res.add("[unspecified]")
else:
res.add($loc.procedure)
res.add("[")
res.add($loc.trace)
res.add("]")
res
proc srcLocImpl(procedure: static string,
file: static string, line: static int): ptr SrcLoc =
var loc {.global.} = SrcLoc(
file: cstring(file), line: line, procedure: procedure, trace: "a"
)
return addr(loc)
template getSrcLocation*(procedure: static string = ""): ptr SrcLoc =
srcLocImpl(procedure,
instantiationInfo(-2).filename, instantiationInfo(-2).line)