diff --git a/codex.nim b/codex.nim index c190592e..efaf9289 100644 --- a/codex.nim +++ b/codex.nim @@ -19,13 +19,11 @@ import pkg/confutils/toml/std/uri as confTomlUri import pkg/toml_serialization import pkg/libp2p -when defined(chronosFuturesInstrumentation): - import ./codex/utils/asyncprofiler - import ./codex/conf import ./codex/codex import ./codex/units import ./codex/utils/keyutils +import ./codex/utils/asyncprofiler export codex, conf, libp2p, chronos, chronicles @@ -57,6 +55,11 @@ when isMainModule: config.setupLogging() config.setupMetrics() + # TODO this should not be here, but currently can't have it in setupMetrics + # or we get a circular import. + when chronosFuturesInstrumentation: + AsyncProfilerInfo.initDefault(k = config.profilerMaxMetrics) + case config.cmd: of StartUpCommand.noCommand: @@ -109,18 +112,6 @@ when isMainModule: notice "Stopping Codex" - when defined(chronosFuturesInstrumentation): - let metrics = getFutureSummaryMetrics() - for (k,v) in metrics.pairs(): - let count = v.count - if count > 0: - echo "" - echo "metric: ", $k - echo "count: ", count - echo "avg execTime:\t", v.totalExecTime div count, "\ttotal: ", v.totalExecTime - echo "avg wallTime:\t", v.totalWallTime div count, "\ttotal: ", v.totalWallTime - echo "avg runTime:\t", v.totalRunTime div count, "\ttotal: ", v.totalRunTime - try: setControlCHook(controlCHandler) except Exception as exc: # TODO Exception diff --git a/codex/conf.nim b/codex/conf.nim index 2c0e75d5..61502f81 100644 --- a/codex/conf.nim +++ b/codex/conf.nim @@ -106,6 +106,11 @@ type abbr: "d" name: "data-dir" }: OutDir + profilerMaxMetrics* {. + desc: "Maximum number of metrics to export to Prometheus." + defaultValue: 100 + name: "profiler-max-metrics" }: int + case cmd* {. command defaultValue: noCommand }: StartUpCommand diff --git a/codex/utils/asyncprofiler/metricscollector.nim b/codex/utils/asyncprofiler/metricscollector.nim index 7f1773e6..edc0142b 100644 --- a/codex/utils/asyncprofiler/metricscollector.nim +++ b/codex/utils/asyncprofiler/metricscollector.nim @@ -181,12 +181,15 @@ when defined(metrics): resetMetric(chronos_largest_exec_time_total) resetMetric(chronos_largest_exec_time_max) - var asyncProfilerInfo* {.global.}: AsyncProfilerInfo = AsyncProfilerInfo.newCollector( + var asyncProfilerInfo* {.global.}: AsyncProfilerInfo + + proc initDefault*(AsyncProfilerInfo: typedesc, k: int) = + asyncProfilerInfo = AsyncProfilerInfo.newCollector( perfSampler = getFutureSummaryMetrics, - k = 10, + k = k, # We want to collect metrics every 5 seconds. sampleInterval = initDuration(seconds = 5), clock = proc (): Time = getTime(), ) - setChangeCallback(proc (): void = asyncProfilerInfo.collect()) + setChangeCallback(proc (): void = asyncProfilerInfo.collect()) diff --git a/tests/codex/blockexchange/engine/testengine.nim b/tests/codex/blockexchange/engine/testengine.nim index 45505f04..22791e77 100644 --- a/tests/codex/blockexchange/engine/testengine.nim +++ b/tests/codex/blockexchange/engine/testengine.nim @@ -16,9 +16,8 @@ import pkg/codex/chunker import pkg/codex/discovery import pkg/codex/blocktype import pkg/codex/utils/asyncheapqueue +import pkg/codex/utils/asyncprofiler -when defined(chronosFuturesInstrumentation): - import pkg/codex/utils/asyncprofiler import ../../helpers import ../../examples