From ce6483e3283e5a096207fc38113053fb8d05bd73 Mon Sep 17 00:00:00 2001 From: benbierens Date: Tue, 11 Jul 2023 08:43:15 +0200 Subject: [PATCH] trying with future locations --- docker/asyncfutures2.nim | 1 + docker/asyncloop.nim | 19 +++++++++++------ docker/codex.Dockerfile | 1 + docker/srcloc.nim | 46 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 docker/srcloc.nim diff --git a/docker/asyncfutures2.nim b/docker/asyncfutures2.nim index 7e57a2a9..e026f977 100644 --- a/docker/asyncfutures2.nim +++ b/docker/asyncfutures2.nim @@ -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 diff --git a/docker/asyncloop.nim b/docker/asyncloop.nim index 4308f8c2..957a266c 100644 --- a/docker/asyncloop.nim +++ b/docker/asyncloop.nim @@ -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) {. diff --git a/docker/codex.Dockerfile b/docker/codex.Dockerfile index 89bb1057..28848770 100644 --- a/docker/codex.Dockerfile +++ b/docker/codex.Dockerfile @@ -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 diff --git a/docker/srcloc.nim b/docker/srcloc.nim new file mode 100644 index 00000000..321c9e7f --- /dev/null +++ b/docker/srcloc.nim @@ -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)