perf(thornode): console log writer looks up the working directory once

🔎 Visual review of b20a294c: explainer, review flags and prompts to ask an AI about the change

Summary

thornode logs through the SDK's zerolog console logger, with the caller added by cmd/thornode (CallerWithSkipFrameCount(3)). For every line that has a caller, zerolog's default console caller formatter calls os.Getwd() (which stats . and $PWD) and then filepath.Rel to shorten the path. Release builds use -trimpath, so the caller is a module path such as gitlab.com/thorchain/thornode/v3/x/…. Rel can't relate that to the working directory, so the path never gets shortened, and the syscall is wasted on every line. Block execution logs a lot (every swap, every EndBlock manager), and the console writer runs on the block thread.

The start command now writes the SDK's logger through the same zerolog.ConsoleWriter with a caller formatter that looks the working directory up once. Everything else is kept as is:

  • zerolog.Logger.Output keeps the logger's level, hooks (timestamp), stack setting and context.
  • The writer gets the same NoColor, TimeFormat (time.Kitchen) and output (cmd.OutOrStdout()) that server.CreateSDKLogger / cosmossdk.io/log.NewLogger use.
  • The formatter is zerolog's own, with the os.Getwd call moved out.
  • JSON output (--log_format json) and module-filtered levels (e.g. consensus:info,*:error) go through SDK writers this can't rebuild, so they keep the SDK logger unchanged.

Risk: low. Log output only; no state. The output is byte-identical to the SDK logger's; see Tests.

What changes

  • cmd/thornode/console_log.go (new): consoleLogOutput plus consoleFormatCaller, which is zerolog's consoleDefaultFormatCaller with the working directory cached, and consoleColorize, a copy of zerolog's unexported colorize.
  • cmd/thornode/commands.go: addModuleInitFlags' PreRunE calls it before wrapping the logger in thorlog.SdkLogWrapper.

Evidence

Benchmark (BenchmarkConsoleLogCaller, one error line with a -trimpath-style caller, 5 runs):

ns/op B/op allocs/op
SDK console writer 13,013–15,166 3,001 65
cached cwd 9,398–10,525 2,520 61

That is about −32% per log line.

Mainnet replay (scripts/perf/mainnet-replay.sh, 1,999 blocks 28124001..28125999 through the CometBFT handshake, which checks every block's app hash against mainnet). This change was combined with !5283 (merged) and a scheduled-heights txout window on the v3.20.3-based replay lab stack, 3 interleaved rounds per side. Summed CPU profiles show this change's part:

before after
os.Getwd 4.06 s not in profile
zerolog.ConsoleWriter.Write 20.2 s 17.4 s (−14%)

That is about 0.8% of FinalizeBlock CPU (536.8 s).

scripts/perf/guard.sh (unit tests, then unfiltered regtest vs develop): PASS. All 2,481 regression output files are identical (state, events, gas_used).

Tests

cmd/thornode/console_log_test.go:

  • TestConsoleLogOutputMatchesSDK: the logger is built with server.CreateSDKLogger and with consoleLogOutput, with NO_COLOR set and unset, for default, info, debug with log_no_color, and error with plain format. The same lines are logged through both: callers inside the working directory (shortened), a -trimpath module path, error fields, a module field, and no caller. Their output must be byte-identical. If a later SDK or zerolog upgrade changes the console writer, this test fails.
  • TestConsoleLogOutputKeepsTrace: with --trace the error stack is still logged.
  • TestConsoleLogOutputKeepsOtherWriters: JSON output and module-filtered levels return the SDK logger unchanged.

How it was found

This came from round 8 of the mainnet replay profiling (rounds 4–7: !5274 (merged) to !5281 (merged)). Round 7 (!5281 (merged)) cut the error-log volume. This removes the per-line syscall that remained.

🤖 Generated with Claude Code

Edited by Huginn Ai

Merge request reports

Loading
Loading