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.
This commit is contained in:
Dave Collins 2013-10-26 16:12:54 -05:00
parent 1aa65e6863
commit 7d8bb5ab4c
2 changed files with 14 additions and 1 deletions

14
btcd.go
View file

@ -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)

View file

@ -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}"`
}