Skip to content

Commit

Permalink
Merge pull request bnb-chain#2151 from fjl/debug-api
Browse files Browse the repository at this point in the history
internal/debug: APIs for profiling and tracing
  • Loading branch information
obscuren committed Jan 28, 2016
2 parents ae1a137 + 3750d83 commit 528dcc3
Show file tree
Hide file tree
Showing 12 changed files with 517 additions and 116 deletions.
28 changes: 14 additions & 14 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package main
import (
"fmt"
"io/ioutil"
_ "net/http/pprof"
"os"
"path/filepath"
"runtime"
Expand All @@ -34,6 +33,7 @@ import (
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/internal/debug"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/metrics"
Expand Down Expand Up @@ -326,12 +326,6 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
utils.VMEnableJitFlag,
utils.NetworkIdFlag,
utils.RPCCORSDomainFlag,
utils.VerbosityFlag,
utils.BacktraceAtFlag,
utils.LogVModuleFlag,
utils.LogFileFlag,
utils.PProfEanbledFlag,
utils.PProfPortFlag,
utils.MetricsEnabledFlag,
utils.SolcPathFlag,
utils.GpoMinGasPriceFlag,
Expand All @@ -342,23 +336,29 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
utils.GpobaseCorrectionFactorFlag,
utils.ExtraDataFlag,
}
app.Flags = append(app.Flags, debug.Flags...)

app.Before = func(ctx *cli.Context) error {
runtime.GOMAXPROCS(runtime.NumCPU())
if err := debug.Setup(ctx); err != nil {
return err
}
// Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second)

utils.SetupLogger(ctx)
utils.SetupNetwork(ctx)
utils.SetupVM(ctx)
if ctx.GlobalBool(utils.PProfEanbledFlag.Name) {
utils.StartPProf(ctx)
}
return nil
}
// Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second)

app.After = func(ctx *cli.Context) error {
logger.Flush()
debug.Exit()
return nil
}
}

func main() {
defer logger.Flush()
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
13 changes: 3 additions & 10 deletions cmd/geth/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/codegangsta/cli"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/internal/debug"
)

// AppHelpTemplate is the test template for the default, global app help topic.
Expand Down Expand Up @@ -147,16 +148,8 @@ var AppHelpFlagGroups = []flagGroup{
},
},
{
Name: "LOGGING AND DEBUGGING",
Flags: []cli.Flag{
utils.VerbosityFlag,
utils.LogVModuleFlag,
utils.BacktraceAtFlag,
utils.LogFileFlag,
utils.PProfEanbledFlag,
utils.PProfPortFlag,
utils.MetricsEnabledFlag,
},
Name: "LOGGING AND DEBUGGING",
Flags: append([]cli.Flag{utils.MetricsEnabledFlag}, debug.Flags...),
},
{
Name: "EXPERIMENTAL",
Expand Down
60 changes: 6 additions & 54 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,17 @@ package utils

import (
"crypto/ecdsa"
"errors"
"fmt"
"io/ioutil"
"log"
"math"
"math/big"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"

"errors"

"github.com/codegangsta/cli"
"github.com/ethereum/ethash"
"github.com/ethereum/go-ethereum/accounts"
Expand Down Expand Up @@ -223,35 +220,6 @@ var (
}

// logging and debug settings
VerbosityFlag = cli.IntFlag{
Name: "verbosity",
Usage: "Logging verbosity: 0-6 (0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=debug detail)",
Value: int(logger.InfoLevel),
}
LogFileFlag = cli.StringFlag{
Name: "logfile",
Usage: "Log output file within the data dir (default = no log file generated)",
Value: "",
}
LogVModuleFlag = cli.GenericFlag{
Name: "vmodule",
Usage: "Per-module verbosity: comma-separated list of <module>=<level>, where <module> is file literal or a glog pattern",
Value: glog.GetVModule(),
}
BacktraceAtFlag = cli.GenericFlag{
Name: "backtrace",
Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
Value: glog.GetTraceLocation(),
}
PProfEanbledFlag = cli.BoolFlag{
Name: "pprof",
Usage: "Enable the profiling server on localhost",
}
PProfPortFlag = cli.IntFlag{
Name: "pprofport",
Usage: "Profile server listening port",
Value: 6060,
}
MetricsEnabledFlag = cli.BoolFlag{
Name: metrics.MetricsEnabledFlag,
Usage: "Enable metrics collection and reporting",
Expand Down Expand Up @@ -297,7 +265,7 @@ var (
Value: DirectoryString{common.DefaultIpcPath()},
}
WSEnabledFlag = cli.BoolFlag{
Name: "ws",
Name: "ws",
Usage: "Enable the WS-RPC server",
}
WSListenAddrFlag = cli.StringFlag{
Expand All @@ -324,6 +292,7 @@ var (
Name: "exec",
Usage: "Execute JavaScript statement (only in combination with console/attach)",
}

// Network Settings
MaxPeersFlag = cli.IntFlag{
Name: "maxpeers",
Expand Down Expand Up @@ -714,19 +683,6 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
return stack
}

// SetupLogger configures glog from the logging-related command line flags.
func SetupLogger(ctx *cli.Context) {
glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
glog.CopyStandardLogTo("INFO")
glog.SetToStderr(true)
if ctx.GlobalIsSet(LogFileFlag.Name) {
logger.New("", ctx.GlobalString(LogFileFlag.Name), ctx.GlobalInt(VerbosityFlag.Name))
}
if ctx.GlobalIsSet(VMDebugFlag.Name) {
vm.Debug = ctx.GlobalBool(VMDebugFlag.Name)
}
}

// SetupNetwork configures the system for either the main net or some test network.
func SetupNetwork(ctx *cli.Context) {
switch {
Expand All @@ -746,6 +702,9 @@ func SetupVM(ctx *cli.Context) {
vm.EnableJit = ctx.GlobalBool(VMEnableJitFlag.Name)
vm.ForceJit = ctx.GlobalBool(VMForceJitFlag.Name)
vm.SetJITCacheSize(ctx.GlobalInt(VMJitCacheFlag.Name))
if ctx.GlobalIsSet(VMDebugFlag.Name) {
vm.Debug = ctx.GlobalBool(VMDebugFlag.Name)
}
}

// MakeChain creates a chain manager from set command line flags.
Expand Down Expand Up @@ -873,10 +832,3 @@ func StartWS(stack *node.Node, ctx *cli.Context) error {
glog.V(logger.Error).Infof("Unable to start RPC-WS interface, could not find admin API")
return errors.New("Unable to start RPC-WS interface")
}

func StartPProf(ctx *cli.Context) {
address := fmt.Sprintf("localhost:%d", ctx.GlobalInt(PProfPortFlag.Name))
go func() {
log.Println(http.ListenAndServe(address, nil))
}()
}
Loading

0 comments on commit 528dcc3

Please sign in to comment.