Skip to content

Commit

Permalink
cmd/geth, mobile: add memsize to pprof server (#16532)
Browse files Browse the repository at this point in the history
* cmd/geth, mobile: add memsize to pprof server

This is a temporary change, to be reverted before the next release.

* cmd/geth: fix variable name
  • Loading branch information
fjl authored and karalabe committed Apr 23, 2018
1 parent 9586f2a commit e7067be
Show file tree
Hide file tree
Showing 13 changed files with 834 additions and 10 deletions.
2 changes: 2 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ func geth(ctx *cli.Context) error {
// it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
// miner.
func startNode(ctx *cli.Context, stack *node.Node) {
debug.Memsize.Add("node", stack)

// Start up the node itself
utils.StartNode(stack)

Expand Down
27 changes: 17 additions & 10 deletions internal/debug/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ import (
"github.com/ethereum/go-ethereum/log/term"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/metrics/exp"
"github.com/fjl/memsize/memsizeui"
colorable "github.com/mattn/go-colorable"
"gopkg.in/urfave/cli.v1"
)

var Memsize memsizeui.Handler

var (
verbosityFlag = cli.IntFlag{
Name: "verbosity",
Expand Down Expand Up @@ -129,21 +132,25 @@ func Setup(ctx *cli.Context) error {

// pprof server
if ctx.GlobalBool(pprofFlag.Name) {
// Hook go-metrics into expvar on any /debug/metrics request, load all vars
// from the registry into expvar, and execute regular expvar handler.
exp.Exp(metrics.DefaultRegistry)

address := fmt.Sprintf("%s:%d", ctx.GlobalString(pprofAddrFlag.Name), ctx.GlobalInt(pprofPortFlag.Name))
go func() {
log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address))
if err := http.ListenAndServe(address, nil); err != nil {
log.Error("Failure in running pprof server", "err", err)
}
}()
StartPProf(address)
}
return nil
}

func StartPProf(address string) {
// Hook go-metrics into expvar on any /debug/metrics request, load all vars
// from the registry into expvar, and execute regular expvar handler.
exp.Exp(metrics.DefaultRegistry)
http.Handle("/memsize/", http.StripPrefix("/memsize", &Memsize))
log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address))
go func() {
if err := http.ListenAndServe(address, nil); err != nil {
log.Error("Failure in running pprof server", "err", err)
}
}()
}

// Exit stops all running profiles, flushing their output to the
// respective file.
func Exit() {
Expand Down
11 changes: 11 additions & 0 deletions mobile/geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/ethstats"
"github.com/ethereum/go-ethereum/internal/debug"
"github.com/ethereum/go-ethereum/les"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
Expand Down Expand Up @@ -72,6 +73,9 @@ type NodeConfig struct {

// WhisperEnabled specifies whether the node should run the Whisper protocol.
WhisperEnabled bool

// Listening address of pprof server.
PprofAddress string
}

// defaultNodeConfig contains the default node configuration values to use if all
Expand Down Expand Up @@ -107,6 +111,11 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
if config.BootstrapNodes == nil || config.BootstrapNodes.Size() == 0 {
config.BootstrapNodes = defaultNodeConfig.BootstrapNodes
}

if config.PprofAddress != "" {
debug.StartPProf(config.PprofAddress)
}

// Create the empty networking stack
nodeConf := &node.Config{
Name: clientIdentifier,
Expand All @@ -127,6 +136,8 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
return nil, err
}

debug.Memsize.Add("node", rawStack)

var genesis *core.Genesis
if config.EthereumGenesis != "" {
// Parse the user supplied genesis spec if not mainnet
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/fjl/memsize/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 119 additions & 0 deletions vendor/github.com/fjl/memsize/bitmap.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/fjl/memsize/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e7067be

Please sign in to comment.