Add profiling with pprof (#754)

This commit is contained in:
Frank Mueller 2018-03-23 14:58:40 +01:00 committed by Igor Mandrigin
parent 70d153bb60
commit 9bb732d981
2 changed files with 49 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import (
"github.com/status-im/status-go/geth/params" "github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/metrics" "github.com/status-im/status-go/metrics"
nodemetrics "github.com/status-im/status-go/metrics/node" nodemetrics "github.com/status-im/status-go/metrics/node"
"github.com/status-im/status-go/profiling"
) )
var ( var (
@ -41,6 +42,8 @@ var (
ipcEnabled = flag.Bool("ipc", false, "Enable IPC RPC endpoint") ipcEnabled = flag.Bool("ipc", false, "Enable IPC RPC endpoint")
cliEnabled = flag.Bool("cli", false, "Enable debugging CLI server") cliEnabled = flag.Bool("cli", false, "Enable debugging CLI server")
cliPort = flag.String("cliport", debug.CLIPort, "CLI server's listening port") cliPort = flag.String("cliport", debug.CLIPort, "CLI server's listening port")
pprofEnabled = flag.Bool("pprof", false, "Enable runtime profiling via pprof")
pprofPort = flag.Int("pprofport", 52525, "Port for runtime profiling via pprof")
logLevel = flag.String("log", "INFO", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`) logLevel = flag.String("log", "INFO", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`)
logFile = flag.String("logfile", "", "Path to the log file") logFile = flag.String("logfile", "", "Path to the log file")
version = flag.Bool("version", false, "Print version") version = flag.Bool("version", false, "Print version")
@ -127,6 +130,7 @@ func main() {
// handle interrupt signals // handle interrupt signals
interruptCh := haltOnInterruptSignal(backend.NodeManager()) interruptCh := haltOnInterruptSignal(backend.NodeManager())
// Check if debugging CLI connection shall be enabled. // Check if debugging CLI connection shall be enabled.
if *cliEnabled { if *cliEnabled {
err := startDebug(backend) err := startDebug(backend)
@ -136,6 +140,11 @@ func main() {
} }
} }
// Check if profiling shall be enabled.
if *pprofEnabled {
profiling.NewProfiler(*pprofPort).Go()
}
// Run stats server. // Run stats server.
if *statsEnabled { if *statsEnabled {
go startCollectingStats(interruptCh, backend.NodeManager()) go startCollectingStats(interruptCh, backend.NodeManager())

40
profiling/profiler.go Normal file
View File

@ -0,0 +1,40 @@
package profiling
import (
"fmt"
"net/http"
hpprof "net/http/pprof"
"github.com/ethereum/go-ethereum/log"
)
// Profiler runs and controls a HTTP pprof interface.
type Profiler struct {
server *http.Server
}
// NewProfiler creates an instance of the profiler with
// the given port.
func NewProfiler(port int) *Profiler {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", hpprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", hpprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", hpprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", hpprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", hpprof.Trace)
p := Profiler{
server: &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
},
}
return &p
}
// Go starts the HTTP pprof in the background.
func (p *Profiler) Go() {
go func() {
log.Info("debug server stopped", "err", p.server.ListenAndServe())
}()
log.Info("debug server started")
}