From 7d8bb5ab4c16210c6fe2b74d226853412662af82 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sat, 26 Oct 2013 16:12:54 -0500 Subject: [PATCH] Add --cpuprofile option. This commit provides a new --cpuprofile flag that can be used to specify a file path to write CPU profile data into. The resulting profile can then be consumed by the 'go tool pprof' command. --- btcd.go | 14 +++++++++++++- config.go | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/btcd.go b/btcd.go index b854f2f2..e6bb53ff 100644 --- a/btcd.go +++ b/btcd.go @@ -10,6 +10,7 @@ import ( _ "net/http/pprof" "os" "runtime" + "runtime/pprof" ) var ( @@ -44,7 +45,7 @@ func btcdMain() error { // Show version at startup. log.Infof("Version %s", version()) - // See if we want to enable profiling. + // Enable http profiling server if requested. if cfg.Profile != "" { go func() { listenAddr := net.JoinHostPort("", cfg.Profile) @@ -56,6 +57,17 @@ func btcdMain() error { }() } + // Write cpu profile if requested. + if cfg.CpuProfile != "" { + f, err := os.Create(cfg.CpuProfile) + if err != nil { + log.Errorf("Unable to create cpu profile: %v", err) + return err + } + pprof.StartCPUProfile(f) + defer pprof.StopCPUProfile() + } + // Perform upgrades to btcd as new versions require it. if err := doUpgrades(); err != nil { log.Errorf("%v", err) diff --git a/config.go b/config.go index 7d018450..35e6b552 100644 --- a/config.go +++ b/config.go @@ -61,6 +61,7 @@ type config struct { DisableCheckpoints bool `long:"nocheckpoints" description:"Disable built-in checkpoints. Don't do this unless you know what you're doing."` DbType string `long:"dbtype" description:"Database backend to use for the Block Chain"` Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"` + CpuProfile string `long:"cpuprofile" description:"Write CPU profile to the specified file"` DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"` }