diff --git a/cmd/kaspaminer/config.go b/cmd/kaspaminer/config.go index effb607d9..20293319d 100644 --- a/cmd/kaspaminer/config.go +++ b/cmd/kaspaminer/config.go @@ -2,11 +2,13 @@ package main import ( "fmt" - "github.com/kaspanet/kaspad/config" "os" "path/filepath" + "strconv" "strings" + "github.com/kaspanet/kaspad/config" + "github.com/kaspanet/kaspad/util" "github.com/pkg/errors" @@ -37,6 +39,7 @@ type configFlags struct { Verbose bool `long:"verbose" short:"v" description:"Enable logging of RPC requests"` NumberOfBlocks uint64 `short:"n" long:"numblocks" description:"Number of blocks to mine. If omitted, will mine until the process is interrupted."` BlockDelay uint64 `long:"block-delay" description:"Delay for block submission (in milliseconds). This is used only for testing purposes."` + Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"` config.NetworkFlags } @@ -78,6 +81,13 @@ func parseConfig() (*configFlags, error) { return nil, errors.New("--rpccert should be omitted if --notls is used") } + if cfg.Profile != "" { + profilePort, err := strconv.Atoi(cfg.Profile) + if err != nil || profilePort < 1024 || profilePort > 65535 { + return nil, errors.New("The profile port must be between 1024 and 65535") + } + } + initLog(defaultLogFile, defaultErrLogFile) return cfg, nil diff --git a/cmd/kaspaminer/main.go b/cmd/kaspaminer/main.go index 97f150386..44fc65ef9 100644 --- a/cmd/kaspaminer/main.go +++ b/cmd/kaspaminer/main.go @@ -2,11 +2,16 @@ package main import ( "fmt" - "github.com/kaspanet/kaspad/version" + "net" + "net/http" "os" + "github.com/kaspanet/kaspad/version" + "github.com/pkg/errors" + _ "net/http/pprof" + "github.com/kaspanet/kaspad/signal" "github.com/kaspanet/kaspad/util/panics" ) @@ -28,6 +33,17 @@ func main() { enableRPCLogging() } + // Enable http profiling server if requested. + if cfg.Profile != "" { + spawn(func() { + listenAddr := net.JoinHostPort("", cfg.Profile) + log.Infof("Profile server listening on %s", listenAddr) + profileRedirect := http.RedirectHandler("/debug/pprof", http.StatusSeeOther) + http.Handle("/", profileRedirect) + log.Errorf("%s", http.ListenAndServe(listenAddr, nil)) + }) + } + client, err := connectToServer(cfg) if err != nil { panic(errors.Wrap(err, "Error connecting to the RPC server"))