2022-03-16 09:13:17 +00:00
|
|
|
# Nimbus
|
2024-04-03 15:48:35 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-03-16 09:13:17 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
|
|
|
## Pretty printing, an alternative to `$` for debugging
|
|
|
|
## ----------------------------------------------------
|
|
|
|
##
|
|
|
|
## minimal dependencies, avoiding circular import
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[sequtils, strformat, strutils, tables, times],
|
2022-08-18 20:41:03 +00:00
|
|
|
eth/common,
|
2023-10-25 14:03:09 +00:00
|
|
|
stew/byteutils,
|
2022-08-04 08:04:30 +00:00
|
|
|
../../nimbus/constants
|
2022-03-16 09:13:17 +00:00
|
|
|
|
|
|
|
export
|
|
|
|
sequtils, strformat, strutils
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
func reGroup(q: openArray[int]; itemsPerSegment = 16): seq[seq[int]] =
|
2022-03-16 09:13:17 +00:00
|
|
|
var top = 0
|
|
|
|
while top < q.len:
|
|
|
|
let w = top
|
|
|
|
top = min(w + itemsPerSegment, q.len)
|
|
|
|
result.add q[w ..< top]
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, units pretty printer
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
func ppUs*(elapsed: Duration): string =
|
2022-08-12 15:42:07 +00:00
|
|
|
result = $elapsed.inMicroseconds
|
|
|
|
let ns = elapsed.inNanoseconds mod 1_000 # fraction of a micro second
|
|
|
|
if ns != 0:
|
|
|
|
# to rounded deca milli seconds
|
|
|
|
let du = (ns + 5i64) div 10i64
|
|
|
|
result &= &".{du:02}"
|
|
|
|
result &= "us"
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
func ppMs*(elapsed: Duration): string =
|
2022-04-08 04:54:11 +00:00
|
|
|
result = $elapsed.inMilliseconds
|
2022-08-12 15:42:07 +00:00
|
|
|
let ns = elapsed.inNanoseconds mod 1_000_000 # fraction of a milli second
|
2022-03-16 09:13:17 +00:00
|
|
|
if ns != 0:
|
|
|
|
# to rounded deca milli seconds
|
|
|
|
let dm = (ns + 5_000i64) div 10_000i64
|
|
|
|
result &= &".{dm:02}"
|
|
|
|
result &= "ms"
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
func ppSecs*(elapsed: Duration): string =
|
2022-03-16 09:13:17 +00:00
|
|
|
result = $elapsed.inSeconds
|
2022-08-12 15:42:07 +00:00
|
|
|
let ns = elapsed.inNanoseconds mod 1_000_000_000 # fraction of a second
|
2022-03-16 09:13:17 +00:00
|
|
|
if ns != 0:
|
2022-08-12 15:42:07 +00:00
|
|
|
# round up
|
2022-03-16 09:13:17 +00:00
|
|
|
let ds = (ns + 5_000_000i64) div 10_000_000i64
|
|
|
|
result &= &".{ds:02}"
|
|
|
|
result &= "s"
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
func ppMins*(elapsed: Duration): string =
|
2022-08-12 15:42:07 +00:00
|
|
|
result = $elapsed.inMinutes
|
|
|
|
let ns = elapsed.inNanoseconds mod 60_000_000_000 # fraction of a minute
|
|
|
|
if ns != 0:
|
|
|
|
# round up
|
|
|
|
let dm = (ns + 500_000_000i64) div 1_000_000_000i64
|
|
|
|
result &= &":{dm:02}"
|
|
|
|
result &= "m"
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
func toKMG*[T](s: T): string =
|
|
|
|
func subst(s: var string; tag, new: string): bool =
|
2022-03-16 09:13:17 +00:00
|
|
|
if tag.len < s.len and s[s.len - tag.len ..< s.len] == tag:
|
|
|
|
s = s[0 ..< s.len - tag.len] & new
|
|
|
|
return true
|
|
|
|
result = $s
|
|
|
|
for w in [("000", "K"),("000K","M"),("000M","G"),("000G","T"),
|
|
|
|
("000T","P"),("000P","E"),("000E","Z"),("000Z","Y")]:
|
|
|
|
if not result.subst(w[0],w[1]):
|
|
|
|
return
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
func pp*(elapsed: Duration): string =
|
|
|
|
try:
|
|
|
|
if 0 < times.inMinutes(elapsed):
|
|
|
|
result = elapsed.ppMins
|
|
|
|
elif 0 < times.inSeconds(elapsed):
|
|
|
|
result = elapsed.ppSecs
|
2024-11-01 19:06:26 +00:00
|
|
|
elif 0 < times.inMilliseconds(elapsed):
|
2023-11-24 22:16:21 +00:00
|
|
|
result = elapsed.ppMs
|
2024-11-01 19:06:26 +00:00
|
|
|
elif 0 < times.inMicroseconds(elapsed):
|
2023-11-24 22:16:21 +00:00
|
|
|
result = elapsed.ppUs
|
2023-12-12 17:47:41 +00:00
|
|
|
else:
|
2024-11-01 19:06:26 +00:00
|
|
|
result = $elapsed.inNanoseconds & "ns"
|
2023-11-24 22:16:21 +00:00
|
|
|
except ValueError:
|
|
|
|
result = $elapsed
|
|
|
|
|
2022-03-16 09:13:17 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, pretty printer
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
func pp*(s: string; hex = false): string =
|
2022-03-16 09:13:17 +00:00
|
|
|
if hex:
|
|
|
|
let n = (s.len + 1) div 2
|
|
|
|
(if s.len < 20: s else: s[0 .. 5] & ".." & s[s.len-8 .. s.len-1]) &
|
|
|
|
"[" & (if 0 < n: "#" & $n else: "") & "]"
|
|
|
|
elif s.len <= 30:
|
|
|
|
s
|
|
|
|
else:
|
|
|
|
(if (s.len and 1) == 0: s[0 ..< 8] else: "0" & s[0 ..< 7]) &
|
|
|
|
"..(" & $s.len & ").." & s[s.len-16 ..< s.len]
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
func pp*(q: openArray[int]; itemsPerLine: int; lineSep: string): string =
|
2022-03-16 09:13:17 +00:00
|
|
|
doAssert q == q.reGroup(itemsPerLine).concat
|
|
|
|
q.reGroup(itemsPerLine)
|
|
|
|
.mapIt(it.mapIt(&"0x{it:02x}").join(", "))
|
|
|
|
.join("," & lineSep)
|
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
func pp*(a: Hash32; collapse = true): string =
|
2022-03-16 09:13:17 +00:00
|
|
|
if not collapse:
|
2023-11-08 12:18:32 +00:00
|
|
|
a.data.toHex
|
2024-10-16 06:51:38 +00:00
|
|
|
elif a == ZERO_HASH32:
|
|
|
|
"ZERO_HASH32"
|
2022-09-03 18:15:35 +00:00
|
|
|
elif a == EMPTY_ROOT_HASH:
|
|
|
|
"EMPTY_ROOT_HASH"
|
2022-08-04 08:04:30 +00:00
|
|
|
elif a == EMPTY_UNCLE_HASH:
|
|
|
|
"EMPTY_UNCLE_HASH"
|
|
|
|
elif a == EMPTY_SHA3:
|
|
|
|
"EMPTY_SHA3"
|
2022-03-16 09:13:17 +00:00
|
|
|
else:
|
2024-04-03 15:48:35 +00:00
|
|
|
"£" & a.data.toHex.join[0..6] & ".." & a.data.toHex.join[56..63]
|
2022-03-16 09:13:17 +00:00
|
|
|
|
2024-10-01 21:03:10 +00:00
|
|
|
func pp*(a: openArray[Hash32]; collapse = true): string =
|
2022-03-16 09:13:17 +00:00
|
|
|
"@[" & a.toSeq.mapIt(it.pp).join(" ") & "]"
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
func pp*(q: openArray[int]; itemsPerLine: int; indent: int): string =
|
2022-03-16 09:13:17 +00:00
|
|
|
q.pp(itemsPerLine = itemsPerLine, lineSep = "\n" & " ".repeat(max(1,indent)))
|
|
|
|
|
2023-11-24 22:16:21 +00:00
|
|
|
func pp*(q: openArray[byte]; noHash = false): string =
|
2022-03-16 09:13:17 +00:00
|
|
|
if q.len == 32 and not noHash:
|
|
|
|
var a: array[32,byte]
|
|
|
|
for n in 0..31: a[n] = q[n]
|
2024-09-29 12:37:09 +00:00
|
|
|
Hash32(a).pp
|
2022-03-16 09:13:17 +00:00
|
|
|
else:
|
2023-11-08 12:18:32 +00:00
|
|
|
q.toHex.pp(hex = true)
|
2022-03-16 09:13:17 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Elapsed time pretty printer
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template showElapsed*(noisy: bool; info: string; code: untyped) =
|
|
|
|
block:
|
2023-12-12 17:47:41 +00:00
|
|
|
let seStartTrackling = getTime()
|
2022-08-12 15:42:07 +00:00
|
|
|
block:
|
|
|
|
code
|
2022-03-16 09:13:17 +00:00
|
|
|
if noisy:
|
2023-12-12 17:47:41 +00:00
|
|
|
let elpd {.inject.} = getTime() - seStartTrackling
|
2022-03-16 09:13:17 +00:00
|
|
|
if 0 < times.inSeconds(elpd):
|
|
|
|
echo "*** ", info, &": {elpd.ppSecs:>4}"
|
|
|
|
else:
|
|
|
|
echo "*** ", info, &": {elpd.ppMs:>4}"
|
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
template showElapsed*(
|
|
|
|
noisy: bool;
|
|
|
|
info: string;
|
|
|
|
elapsed: Duration;
|
|
|
|
code: untyped) =
|
|
|
|
block:
|
2023-12-12 17:47:41 +00:00
|
|
|
let seStartTrackling = getTime()
|
2022-08-12 15:42:07 +00:00
|
|
|
block:
|
|
|
|
code
|
|
|
|
block:
|
2023-12-12 17:47:41 +00:00
|
|
|
elapsed = getTime() - seStartTrackling
|
2022-08-12 15:42:07 +00:00
|
|
|
if noisy:
|
|
|
|
let elpd {.inject.} = elapsed
|
|
|
|
if 0 < times.inSeconds(elpd):
|
|
|
|
echo "*** ", info, &": {elpd.ppSecs:>4}"
|
|
|
|
else:
|
|
|
|
echo "*** ", info, &": {elpd.ppMs:>4}"
|
|
|
|
|
2023-12-12 17:47:41 +00:00
|
|
|
template profileSection*(
|
|
|
|
noisy: bool;
|
|
|
|
info: string;
|
|
|
|
state: var (Duration, int);
|
|
|
|
suspend: bool;
|
|
|
|
code: untyped) =
|
|
|
|
block:
|
|
|
|
let psStartProfiling = getTime()
|
|
|
|
block:
|
|
|
|
code
|
|
|
|
if not suspend:
|
|
|
|
let elpd = getTime() - psStartProfiling
|
|
|
|
if noisy:
|
|
|
|
echo "*** ", info, ": ", elpd.pp
|
|
|
|
state[0] += elpd
|
|
|
|
state[1].inc
|
|
|
|
|
|
|
|
template profileSection*(
|
|
|
|
noisy: bool;
|
|
|
|
info: string;
|
|
|
|
state: var (Duration, int);
|
|
|
|
code: untyped) =
|
|
|
|
noisy.profileSection(info, state, suspend=false):
|
|
|
|
code
|
|
|
|
|
2022-03-16 09:13:17 +00:00
|
|
|
template catchException*(info: string; trace: bool; code: untyped) =
|
|
|
|
block:
|
|
|
|
try:
|
|
|
|
code
|
|
|
|
except CatchableError as e:
|
|
|
|
if trace:
|
|
|
|
echo "*** ", info, ": exception ", e.name, "(", e.msg, ")"
|
|
|
|
echo " ", e.getStackTrace.strip.replace("\n","\n ")
|
|
|
|
|
|
|
|
template catchException*(info: string; code: untyped) =
|
|
|
|
catchException(info, false, code)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|