From 10ef9cc0429394f09726749c8f262e1b739e095c Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Tue, 6 Aug 2024 14:51:08 +0900 Subject: [PATCH] main: add flag to write execution traces Execution traces are part of the go runtime tooling and is useful to check what is slowing down the ibd. For example, a slow ibd because of slow block downloads from peers won't show up on cpu profiling but will on a trace. --- btcd.go | 13 +++++++++++++ config.go | 1 + 2 files changed, 14 insertions(+) diff --git a/btcd.go b/btcd.go index c7f292cbc9..1d9a1e5f6b 100644 --- a/btcd.go +++ b/btcd.go @@ -14,6 +14,7 @@ import ( "runtime" "runtime/debug" "runtime/pprof" + "runtime/trace" "github.com/btcsuite/btcd/blockchain/indexers" "github.com/btcsuite/btcd/database" @@ -100,6 +101,18 @@ func btcdMain(serverChan chan<- *server) error { defer runtime.GC() } + // Write execution trace if requested. + if cfg.TraceProfile != "" { + f, err := os.Create(cfg.TraceProfile) + if err != nil { + btcdLog.Errorf("Unable to create execution trace: %v", err) + return err + } + trace.Start(f) + defer f.Close() + defer trace.Stop() + } + // Perform upgrades to btcd as new versions require it. if err := doUpgrades(); err != nil { btcdLog.Errorf("%v", err) diff --git a/config.go b/config.go index 9bbce7f69a..2d2e9c98a6 100644 --- a/config.go +++ b/config.go @@ -114,6 +114,7 @@ type config struct { ConnectPeers []string `long:"connect" description:"Connect only to the specified peers at startup"` CPUProfile string `long:"cpuprofile" description:"Write CPU profile to the specified file"` MemoryProfile string `long:"memprofile" description:"Write memory profile to the specified file"` + TraceProfile string `long:"traceprofile" description:"Write execution trace to the specified file"` DataDir string `short:"b" long:"datadir" description:"Directory to store data"` DbType string `long:"dbtype" description:"Database backend to use for the Block Chain"` DebugLevel string `short:"d" long:"debuglevel" description:"Logging level for all subsystems {trace, debug, info, warn, error, critical} -- You may also specify =,=,... to set the log level for individual subsystems -- Use show to list available subsystems"`