From 3fde92b120abdf7796020da07e79505bdc851151 Mon Sep 17 00:00:00 2001 From: gmega Date: Thu, 20 Feb 2025 18:13:54 -0300 Subject: [PATCH 1/7] feat: enable chronos profiling --- .gitmodules | 5 ++++- codex/conf.nim | 17 +++++++++++++++++ config.nims | 2 ++ tests/config.nims | 3 +++ vendor/nim-chronos | 2 +- vendor/nim-chroprof | 1 + 6 files changed, 28 insertions(+), 2 deletions(-) create mode 160000 vendor/nim-chroprof diff --git a/.gitmodules b/.gitmodules index ece88749..d7382f6c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -77,7 +77,7 @@ path = vendor/nim-chronos url = https://github.com/status-im/nim-chronos.git ignore = untracked - branch = master + branch = feature/profiler-v4 [submodule "vendor/nim-faststreams"] path = vendor/nim-faststreams url = https://github.com/status-im/nim-faststreams.git @@ -221,3 +221,6 @@ [submodule "vendor/nph"] path = vendor/nph url = https://github.com/arnetheduck/nph.git +[submodule "vendor/nim-chroprof"] + path = vendor/nim-chroprof + url = git@github.com:codex-storage/nim-chroprof.git diff --git a/codex/conf.nim b/codex/conf.nim index 2a859efb..65d2c328 100644 --- a/codex/conf.nim +++ b/codex/conf.nim @@ -20,6 +20,7 @@ import std/strutils import std/typetraits import pkg/chronos +import pkg/chronos/config import pkg/chronicles/helpers import pkg/chronicles/topics_registry import pkg/confutils/defs @@ -35,6 +36,10 @@ import pkg/ethers import pkg/questionable import pkg/questionable/results +when chronosProfiling: + import pkg/chroprof + import pkg/chroprof/collector + import ./codextypes import ./discovery import ./logutils @@ -144,6 +149,12 @@ type name: "data-dir" .}: OutDir + profilerMaxMetrics* {. + desc: "Maximum number of metrics to export to Prometheus.", + defaultValue: 100, + name: "profiler-max-metrics" + .}: int + listenAddrs* {. desc: "Multi Addresses to listen on", defaultValue: @@ -777,11 +788,17 @@ proc setupLogging*(conf: CodexConf) = quit QuitFailure proc setupMetrics*(config: CodexConf) = + when chronosProfiling: + enableProfiling() + if config.metricsEnabled: let metricsAddress = config.metricsAddress notice "Starting metrics HTTP server", url = "http://" & $metricsAddress & ":" & $config.metricsPort & "/metrics" try: + when chronosProfiling: + enableProfilerMetrics(k = config.profilerMaxMetrics) + startMetricsHttpServer($metricsAddress, config.metricsPort) except CatchableError as exc: raiseAssert exc.msg diff --git a/config.nims b/config.nims index 05a31fff..c5a4a744 100644 --- a/config.nims +++ b/config.nims @@ -149,6 +149,8 @@ switch("define", "nimOldCaseObjects") # Enable compat mode for Chronos V4 switch("define", "chronosHandleException") +switch("define", "chronosProfiling") + # begin Nimble config (version 1) when system.fileExists("nimble.paths"): include "nimble.paths" diff --git a/tests/config.nims b/tests/config.nims index 8ae00806..81a89944 100644 --- a/tests/config.nims +++ b/tests/config.nims @@ -12,3 +12,6 @@ when not defined(chronicles_log_level): "chronicles_sinks:textlines[dynamic]" # allow logs to be filtered at runtime --"import": "logging" # ensure that logging is ignored at runtime + +--undef: + chronosProfiling diff --git a/vendor/nim-chronos b/vendor/nim-chronos index c04576d8..f85a3b43 160000 --- a/vendor/nim-chronos +++ b/vendor/nim-chronos @@ -1 +1 @@ -Subproject commit c04576d829b8a0a1b12baaa8bc92037501b3a4a0 +Subproject commit f85a3b43c2606d8966bb1eb6e36de10c7b4aab94 diff --git a/vendor/nim-chroprof b/vendor/nim-chroprof new file mode 160000 index 00000000..95da0353 --- /dev/null +++ b/vendor/nim-chroprof @@ -0,0 +1 @@ +Subproject commit 95da0353d276f688926926d796439b24874ccf44 From 98e7c179dd48b717034847fbe4622bd63f187a09 Mon Sep 17 00:00:00 2001 From: gmega Date: Mon, 17 Mar 2025 14:42:51 -0300 Subject: [PATCH 2/7] fix: fix submodule origins --- .gitmodules | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index d7382f6c..60f485db 100644 --- a/.gitmodules +++ b/.gitmodules @@ -75,7 +75,7 @@ branch = master [submodule "vendor/nim-chronos"] path = vendor/nim-chronos - url = https://github.com/status-im/nim-chronos.git + url = https://github.com/codex-storage/nim-chronos.git ignore = untracked branch = feature/profiler-v4 [submodule "vendor/nim-faststreams"] @@ -223,4 +223,4 @@ url = https://github.com/arnetheduck/nph.git [submodule "vendor/nim-chroprof"] path = vendor/nim-chroprof - url = git@github.com:codex-storage/nim-chroprof.git + url = https://github.com/codex-storage/nim-chroprof.git From 02c86f4178b5ac4c1303841920f4a13cd3a630e1 Mon Sep 17 00:00:00 2001 From: ThatBen Date: Tue, 6 May 2025 10:27:21 +0200 Subject: [PATCH 3/7] attempt to fix --- .github/workflows/docker-dist-tests.yml | 2 +- build.nims | 4 ++-- vendor/asynctest | 2 +- vendor/codex-contracts-eth | 2 +- vendor/constantine | 2 +- vendor/nim-codex-dht | 2 +- vendor/nim-datastore | 2 +- vendor/nim-ethers | 2 +- vendor/nim-leveldbstatic | 2 +- vendor/nim-libp2p | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/docker-dist-tests.yml b/.github/workflows/docker-dist-tests.yml index c6c133f2..fbf22795 100644 --- a/.github/workflows/docker-dist-tests.yml +++ b/.github/workflows/docker-dist-tests.yml @@ -45,7 +45,7 @@ jobs: uses: ./.github/workflows/docker-reusable.yml needs: get-contracts-hash with: - nimflags: '-d:disableMarchNative -d:codex_enable_api_debug_peers=true -d:codex_enable_proof_failures=true -d:codex_enable_log_counter=true -d:verify_circuit=true' + nimflags: '-d:disableMarchNative -d:codex_enable_api_debug_peers=true -d:codex_enable_proof_failures=true -d:codex_enable_log_counter=true -d:verify_circuit=true -d:chronosProfiling=true' nat_ip_auto: true tag_latest: ${{ github.ref_name == github.event.repository.default_branch || startsWith(github.ref, 'refs/tags/') }} tag_suffix: dist-tests diff --git a/build.nims b/build.nims index 88660321..aea13ba7 100644 --- a/build.nims +++ b/build.nims @@ -31,7 +31,7 @@ proc test(name: string, srcDir = "tests/", params = "", lang = "c") = task codex, "build codex binary": buildBinary "codex", - params = "-d:chronicles_runtime_filtering -d:chronicles_log_level=TRACE" + params = "-d:chronicles_runtime_filtering -d:chronicles_log_level=TRACE -d:chronosProfiling=true" task toolsCirdl, "build tools/cirdl binary": buildBinary "tools/cirdl/cirdl" @@ -49,7 +49,7 @@ task testIntegration, "Run integration tests": test "testIntegration" # use params to enable logging from the integration test executable # test "testIntegration", params = "-d:chronicles_sinks=textlines[notimestamps,stdout],textlines[dynamic] " & - # "-d:chronicles_enabled_topics:integration:TRACE" + # "-d:chronicles_enabled_topics:integration:TRACE" task build, "build codex binary": codexTask() diff --git a/vendor/asynctest b/vendor/asynctest index 5154c0d7..73c08f77 160000 --- a/vendor/asynctest +++ b/vendor/asynctest @@ -1 +1 @@ -Subproject commit 5154c0d79dd8bb086ab418cc659e923330ac24f2 +Subproject commit 73c08f77afc5cc2a5628d00f915b97bf72f70c9b diff --git a/vendor/codex-contracts-eth b/vendor/codex-contracts-eth index c00152e6..0bf13851 160000 --- a/vendor/codex-contracts-eth +++ b/vendor/codex-contracts-eth @@ -1 +1 @@ -Subproject commit c00152e6213a3ad4e6760a670213bfae22b0aabf +Subproject commit 0bf138512b7c1c3b8d77c48376e47f702e47106c diff --git a/vendor/constantine b/vendor/constantine index bc3845aa..8d6a6a38 160000 --- a/vendor/constantine +++ b/vendor/constantine @@ -1 +1 @@ -Subproject commit bc3845aa492b52f7fef047503b1592e830d1a774 +Subproject commit 8d6a6a38b90fb8ee3ec2230839773e69aab36d80 diff --git a/vendor/nim-codex-dht b/vendor/nim-codex-dht index 4bd3a39e..f6eef1ac 160000 --- a/vendor/nim-codex-dht +++ b/vendor/nim-codex-dht @@ -1 +1 @@ -Subproject commit 4bd3a39e0030f8ee269ef217344b6b59ec2be6dc +Subproject commit f6eef1ac95c70053b2518f1e3909c909ed8701a6 diff --git a/vendor/nim-datastore b/vendor/nim-datastore index d67860ad..5778e373 160000 --- a/vendor/nim-datastore +++ b/vendor/nim-datastore @@ -1 +1 @@ -Subproject commit d67860add63fd23cdacde1d3da8f4739c2660c2d +Subproject commit 5778e373fa97286f389e0aef61f1e8f30a934dab diff --git a/vendor/nim-ethers b/vendor/nim-ethers index b505ef1a..bbced467 160000 --- a/vendor/nim-ethers +++ b/vendor/nim-ethers @@ -1 +1 @@ -Subproject commit b505ef1ab889be8161bb1efb4908e3dfde5bc1c9 +Subproject commit bbced4673316763c6ef931b4d0a08069cde2474c diff --git a/vendor/nim-leveldbstatic b/vendor/nim-leveldbstatic index 4da61d23..378ef63e 160000 --- a/vendor/nim-leveldbstatic +++ b/vendor/nim-leveldbstatic @@ -1 +1 @@ -Subproject commit 4da61d231a5e73c5daf85eb23f146242b90b144f +Subproject commit 378ef63e261e3b5834a3567404edc3ce838498b3 diff --git a/vendor/nim-libp2p b/vendor/nim-libp2p index 036e110a..c08d8073 160000 --- a/vendor/nim-libp2p +++ b/vendor/nim-libp2p @@ -1 +1 @@ -Subproject commit 036e110a6080fba1a1662c58cfd8c21f9a548021 +Subproject commit c08d80734989b028b3d1705f2188d783a343aac0 From 545f337a5b53a9eafaf55a3c0fc70b11b5bb9294 Mon Sep 17 00:00:00 2001 From: ThatBen Date: Wed, 7 May 2025 16:43:00 +0200 Subject: [PATCH 4/7] enables freeze guard --- codex/conf.nim | 2 +- vendor/nim-chroprof | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codex/conf.nim b/codex/conf.nim index 310c6313..7f853772 100644 --- a/codex/conf.nim +++ b/codex/conf.nim @@ -794,7 +794,7 @@ proc setupLogging*(conf: CodexConf) = proc setupMetrics*(config: CodexConf) = when chronosProfiling: - enableProfiling() + enableProfiling(200.milliseconds) if config.metricsEnabled: let metricsAddress = config.metricsAddress diff --git a/vendor/nim-chroprof b/vendor/nim-chroprof index 95da0353..cd4a3adf 160000 --- a/vendor/nim-chroprof +++ b/vendor/nim-chroprof @@ -1 +1 @@ -Subproject commit 95da0353d276f688926926d796439b24874ccf44 +Subproject commit cd4a3adfb170585af8430f7ba324c3478d7084f8 From 62ac70f15f36c96cb78e8591146d7d4931045668 Mon Sep 17 00:00:00 2001 From: ThatBen Date: Wed, 7 May 2025 17:00:41 +0200 Subject: [PATCH 5/7] attempt to set maxexec threshold --- codex/conf.nim | 4 ++-- vendor/nim-chroprof | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/codex/conf.nim b/codex/conf.nim index 7f853772..047b70b2 100644 --- a/codex/conf.nim +++ b/codex/conf.nim @@ -794,7 +794,7 @@ proc setupLogging*(conf: CodexConf) = proc setupMetrics*(config: CodexConf) = when chronosProfiling: - enableProfiling(200.milliseconds) + enableProfiling(milliseconds(200)) if config.metricsEnabled: let metricsAddress = config.metricsAddress @@ -802,7 +802,7 @@ proc setupMetrics*(config: CodexConf) = url = "http://" & $metricsAddress & ":" & $config.metricsPort & "/metrics" try: when chronosProfiling: - enableProfilerMetrics(k = config.profilerMaxMetrics) + enableProfilerMetrics(k = config.profilerMaxMetrics, 200.milliseconds) startMetricsHttpServer($metricsAddress, config.metricsPort) except CatchableError as exc: diff --git a/vendor/nim-chroprof b/vendor/nim-chroprof index cd4a3adf..f12bded5 160000 --- a/vendor/nim-chroprof +++ b/vendor/nim-chroprof @@ -1 +1 @@ -Subproject commit cd4a3adfb170585af8430f7ba324c3478d7084f8 +Subproject commit f12bded51e2a60513b414a93642b44f1156a964e From 49bb3d347a04095fb6163680cfabd0d057879723 Mon Sep 17 00:00:00 2001 From: ThatBen Date: Wed, 7 May 2025 18:34:35 +0200 Subject: [PATCH 6/7] Adds config option to set exec time threshold --- codex/conf.nim | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/codex/conf.nim b/codex/conf.nim index 047b70b2..0fcc025b 100644 --- a/codex/conf.nim +++ b/codex/conf.nim @@ -288,6 +288,14 @@ type desc: "Logs to file", defaultValue: string.none, name: "log-file", hidden .}: Option[string] + execTimeThreshold* {. + desc: "Threshold for execution time", + defaultValue: 200.milliseconds, + defaultValueDesc: "0.2s", + name: "exec-time-threshold", + abbr: "et" + .}: Duration + case cmd* {.defaultValue: noCmd, command.}: StartUpCmd of persistence: ethProvider* {. @@ -794,7 +802,8 @@ proc setupLogging*(conf: CodexConf) = proc setupMetrics*(config: CodexConf) = when chronosProfiling: - enableProfiling(milliseconds(200)) + notice "Enabling profiling without metrics", execTimeThreshold = config.execTimeThreshold + enableProfiling(config.execTimeThreshold) if config.metricsEnabled: let metricsAddress = config.metricsAddress @@ -802,7 +811,8 @@ proc setupMetrics*(config: CodexConf) = url = "http://" & $metricsAddress & ":" & $config.metricsPort & "/metrics" try: when chronosProfiling: - enableProfilerMetrics(k = config.profilerMaxMetrics, 200.milliseconds) + notice "Enabling profiling with metrics", execTimeThreshold = config.execTimeThreshold + enableProfilerMetrics(k = config.profilerMaxMetrics, config.execTimeThreshold) startMetricsHttpServer($metricsAddress, config.metricsPort) except CatchableError as exc: From 56e2d4a82fa9b3c6e9b912d3b395ab6fec5847f5 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 15 May 2025 10:21:49 +0200 Subject: [PATCH 7/7] Workaround: duration parsing does not support units smaller than 1 second --- codex/conf.nim | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/codex/conf.nim b/codex/conf.nim index 0fcc025b..c9e3fb05 100644 --- a/codex/conf.nim +++ b/codex/conf.nim @@ -290,11 +290,11 @@ type execTimeThreshold* {. desc: "Threshold for execution time", - defaultValue: 200.milliseconds, - defaultValueDesc: "0.2s", + defaultValue: 200, + defaultValueDesc: "200 milliseconds", name: "exec-time-threshold", abbr: "et" - .}: Duration + .}: int case cmd* {.defaultValue: noCmd, command.}: StartUpCmd of persistence: @@ -801,9 +801,10 @@ proc setupLogging*(conf: CodexConf) = quit QuitFailure proc setupMetrics*(config: CodexConf) = + let threshold = config.execTimeThreshold.milliseconds when chronosProfiling: - notice "Enabling profiling without metrics", execTimeThreshold = config.execTimeThreshold - enableProfiling(config.execTimeThreshold) + notice "Enabling profiling without metrics", execTimeThreshold = threshold + enableProfiling(threshold) if config.metricsEnabled: let metricsAddress = config.metricsAddress @@ -811,8 +812,8 @@ proc setupMetrics*(config: CodexConf) = url = "http://" & $metricsAddress & ":" & $config.metricsPort & "/metrics" try: when chronosProfiling: - notice "Enabling profiling with metrics", execTimeThreshold = config.execTimeThreshold - enableProfilerMetrics(k = config.profilerMaxMetrics, config.execTimeThreshold) + notice "Enabling profiling with metrics", execTimeThreshold = threshold + enableProfilerMetrics(k = config.profilerMaxMetrics, threshold) startMetricsHttpServer($metricsAddress, config.metricsPort) except CatchableError as exc: