Skip to content

Commit 3750d83

Browse files
committed
internal/debug: APIs for profiling and tracing
The debug package provides an RPC wrapper for glog settings and the debugging facilities of the Go runtime. They can be triggered through both command line flags and the IPC listener.
1 parent e287b56 commit 3750d83

10 files changed

Lines changed: 502 additions & 107 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package main
2020
import (
2121
"fmt"
2222
"io/ioutil"
23-
_ "net/http/pprof"
2423
"os"
2524
"path/filepath"
2625
"runtime"
@@ -34,6 +33,7 @@ import (
3433
"github.com/ethereum/go-ethereum/cmd/utils"
3534
"github.com/ethereum/go-ethereum/common"
3635
"github.com/ethereum/go-ethereum/eth"
36+
"github.com/ethereum/go-ethereum/internal/debug"
3737
"github.com/ethereum/go-ethereum/logger"
3838
"github.com/ethereum/go-ethereum/logger/glog"
3939
"github.com/ethereum/go-ethereum/metrics"
@@ -326,12 +326,6 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
326326
utils.VMEnableJitFlag,
327327
utils.NetworkIdFlag,
328328
utils.RPCCORSDomainFlag,
329-
utils.VerbosityFlag,
330-
utils.BacktraceAtFlag,
331-
utils.LogVModuleFlag,
332-
utils.LogFileFlag,
333-
utils.PProfEanbledFlag,
334-
utils.PProfPortFlag,
335329
utils.MetricsEnabledFlag,
336330
utils.SolcPathFlag,
337331
utils.GpoMinGasPriceFlag,
@@ -342,23 +336,29 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
342336
utils.GpobaseCorrectionFactorFlag,
343337
utils.ExtraDataFlag,
344338
}
339+
app.Flags = append(app.Flags, debug.Flags...)
340+
345341
app.Before = func(ctx *cli.Context) error {
346342
runtime.GOMAXPROCS(runtime.NumCPU())
343+
if err := debug.Setup(ctx); err != nil {
344+
return err
345+
}
346+
// Start system runtime metrics collection
347+
go metrics.CollectProcessMetrics(3 * time.Second)
347348

348-
utils.SetupLogger(ctx)
349349
utils.SetupNetwork(ctx)
350350
utils.SetupVM(ctx)
351-
if ctx.GlobalBool(utils.PProfEanbledFlag.Name) {
352-
utils.StartPProf(ctx)
353-
}
354351
return nil
355352
}
356-
// Start system runtime metrics collection
357-
go metrics.CollectProcessMetrics(3 * time.Second)
353+
354+
app.After = func(ctx *cli.Context) error {
355+
logger.Flush()
356+
debug.Exit()
357+
return nil
358+
}
358359
}
359360

360361
func main() {
361-
defer logger.Flush()
362362
if err := app.Run(os.Args); err != nil {
363363
fmt.Fprintln(os.Stderr, err)
364364
os.Exit(1)

‎cmd/geth/usage.go‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/codegangsta/cli"
2525
"github.com/ethereum/go-ethereum/cmd/utils"
26+
"github.com/ethereum/go-ethereum/internal/debug"
2627
)
2728

2829
// AppHelpTemplate is the test template for the default, global app help topic.
@@ -147,16 +148,8 @@ var AppHelpFlagGroups = []flagGroup{
147148
},
148149
},
149150
{
150-
Name: "LOGGING AND DEBUGGING",
151-
Flags: []cli.Flag{
152-
utils.VerbosityFlag,
153-
utils.LogVModuleFlag,
154-
utils.BacktraceAtFlag,
155-
utils.LogFileFlag,
156-
utils.PProfEanbledFlag,
157-
utils.PProfPortFlag,
158-
utils.MetricsEnabledFlag,
159-
},
151+
Name: "LOGGING AND DEBUGGING",
152+
Flags: append([]cli.Flag{utils.MetricsEnabledFlag}, debug.Flags...),
160153
},
161154
{
162155
Name: "EXPERIMENTAL",

‎cmd/utils/flags.go‎

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@ package utils
1818

1919
import (
2020
"crypto/ecdsa"
21+
"errors"
2122
"fmt"
2223
"io/ioutil"
23-
"log"
2424
"math"
2525
"math/big"
26-
"net/http"
2726
"os"
2827
"path/filepath"
2928
"runtime"
3029
"strconv"
3130
"strings"
3231

33-
"errors"
34-
3532
"github.com/codegangsta/cli"
3633
"github.com/ethereum/ethash"
3734
"github.com/ethereum/go-ethereum/accounts"
@@ -223,35 +220,6 @@ var (
223220
}
224221

225222
// logging and debug settings
226-
VerbosityFlag = cli.IntFlag{
227-
Name: "verbosity",
228-
Usage: "Logging verbosity: 0-6 (0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=debug detail)",
229-
Value: int(logger.InfoLevel),
230-
}
231-
LogFileFlag = cli.StringFlag{
232-
Name: "logfile",
233-
Usage: "Log output file within the data dir (default = no log file generated)",
234-
Value: "",
235-
}
236-
LogVModuleFlag = cli.GenericFlag{
237-
Name: "vmodule",
238-
Usage: "Per-module verbosity: comma-separated list of <module>=<level>, where <module> is file literal or a glog pattern",
239-
Value: glog.GetVModule(),
240-
}
241-
BacktraceAtFlag = cli.GenericFlag{
242-
Name: "backtrace",
243-
Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
244-
Value: glog.GetTraceLocation(),
245-
}
246-
PProfEanbledFlag = cli.BoolFlag{
247-
Name: "pprof",
248-
Usage: "Enable the profiling server on localhost",
249-
}
250-
PProfPortFlag = cli.IntFlag{
251-
Name: "pprofport",
252-
Usage: "Profile server listening port",
253-
Value: 6060,
254-
}
255223
MetricsEnabledFlag = cli.BoolFlag{
256224
Name: metrics.MetricsEnabledFlag,
257225
Usage: "Enable metrics collection and reporting",
@@ -297,7 +265,7 @@ var (
297265
Value: DirectoryString{common.DefaultIpcPath()},
298266
}
299267
WSEnabledFlag = cli.BoolFlag{
300-
Name: "ws",
268+
Name: "ws",
301269
Usage: "Enable the WS-RPC server",
302270
}
303271
WSListenAddrFlag = cli.StringFlag{
@@ -324,6 +292,7 @@ var (
324292
Name: "exec",
325293
Usage: "Execute JavaScript statement (only in combination with console/attach)",
326294
}
295+
327296
// Network Settings
328297
MaxPeersFlag = cli.IntFlag{
329298
Name: "maxpeers",
@@ -714,19 +683,6 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
714683
return stack
715684
}
716685

717-
// SetupLogger configures glog from the logging-related command line flags.
718-
func SetupLogger(ctx *cli.Context) {
719-
glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
720-
glog.CopyStandardLogTo("INFO")
721-
glog.SetToStderr(true)
722-
if ctx.GlobalIsSet(LogFileFlag.Name) {
723-
logger.New("", ctx.GlobalString(LogFileFlag.Name), ctx.GlobalInt(VerbosityFlag.Name))
724-
}
725-
if ctx.GlobalIsSet(VMDebugFlag.Name) {
726-
vm.Debug = ctx.GlobalBool(VMDebugFlag.Name)
727-
}
728-
}
729-
730686
// SetupNetwork configures the system for either the main net or some test network.
731687
func SetupNetwork(ctx *cli.Context) {
732688
switch {
@@ -746,6 +702,9 @@ func SetupVM(ctx *cli.Context) {
746702
vm.EnableJit = ctx.GlobalBool(VMEnableJitFlag.Name)
747703
vm.ForceJit = ctx.GlobalBool(VMForceJitFlag.Name)
748704
vm.SetJITCacheSize(ctx.GlobalInt(VMJitCacheFlag.Name))
705+
if ctx.GlobalIsSet(VMDebugFlag.Name) {
706+
vm.Debug = ctx.GlobalBool(VMDebugFlag.Name)
707+
}
749708
}
750709

751710
// MakeChain creates a chain manager from set command line flags.
@@ -873,10 +832,3 @@ func StartWS(stack *node.Node, ctx *cli.Context) error {
873832
glog.V(logger.Error).Infof("Unable to start RPC-WS interface, could not find admin API")
874833
return errors.New("Unable to start RPC-WS interface")
875834
}
876-
877-
func StartPProf(ctx *cli.Context) {
878-
address := fmt.Sprintf("localhost:%d", ctx.GlobalInt(PProfPortFlag.Name))
879-
go func() {
880-
log.Println(http.ListenAndServe(address, nil))
881-
}()
882-
}

0 commit comments

Comments
 (0)