diff --git a/btcd.go b/btcd.go index dc7e6f03..fe159be9 100644 --- a/btcd.go +++ b/btcd.go @@ -11,6 +11,8 @@ import ( "github.com/conformal/btcscript" "github.com/conformal/seelog" "net" + "net/http" + _ "net/http/pprof" "os" "runtime" ) @@ -108,6 +110,14 @@ func btcdMain() error { loggers = setLogLevel(cfg.DebugLevel) } + // See if we want to enable profiling + if cfg.Profile != "" { + go func() { + log.Errorf("%v", http.ListenAndServe( + net.JoinHostPort("", cfg.Profile), nil)) + }() + } + // Perform upgrades to btcd as new versions require it. err = doUpgrades() if err != nil { diff --git a/config.go b/config.go index 277e96cd..5d045582 100644 --- a/config.go +++ b/config.go @@ -15,6 +15,7 @@ import ( "net" "os" "path/filepath" + "strconv" "strings" "time" ) @@ -62,6 +63,7 @@ type config struct { TestNet3 bool `long:"testnet" description:"Use the test network"` RegressionTest bool `long:"regtest" description:"Use the regression test network"` 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"` DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"` } @@ -282,6 +284,16 @@ func loadConfig() (*config, []string, error) { return nil, nil, err } + // Validate profile port number + profilePort, err := strconv.Atoi(cfg.Profile) + if err != nil || profilePort < 1024 || profilePort > 65535 { + str := "%s: The profile port must be between 1024 and 65535" + err := errors.New(fmt.Sprintf(str, "loadConfig")) + fmt.Fprintln(os.Stderr, err) + parser.WriteHelp(os.Stderr) + return nil, nil, err + } + // Append the network type to the data directory so it is "namespaced" // per network. In addition to the block database, there are other // pieces of data that are saved to disk such as address manager state.