2016-01-26 07:39:21 -06:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-23 12:31:13 -06:00
|
|
|
"io"
|
2016-01-26 07:39:21 -06:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2017-02-22 06:10:07 -06:00
|
|
|
"os"
|
2016-01-26 07:39:21 -06:00
|
|
|
"runtime"
|
|
|
|
|
2017-02-22 06:10:07 -06:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-02-23 03:56:08 -06:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
|
|
|
"github.com/ethereum/go-ethereum/metrics/exp"
|
2018-04-23 08:20:39 -05:00
|
|
|
"github.com/fjl/memsize/memsizeui"
|
2017-02-23 12:31:13 -06:00
|
|
|
colorable "github.com/mattn/go-colorable"
|
2018-09-29 09:15:39 -05:00
|
|
|
"github.com/mattn/go-isatty"
|
2016-06-09 04:44:42 -05:00
|
|
|
"gopkg.in/urfave/cli.v1"
|
2016-01-26 07:39:21 -06:00
|
|
|
)
|
|
|
|
|
2018-04-23 08:20:39 -05:00
|
|
|
var Memsize memsizeui.Handler
|
|
|
|
|
2016-01-26 07:39:21 -06:00
|
|
|
var (
|
2017-02-22 06:10:07 -06:00
|
|
|
verbosityFlag = cli.IntFlag{
|
2016-01-26 07:39:21 -06:00
|
|
|
Name: "verbosity",
|
2017-02-23 03:44:16 -06:00
|
|
|
Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
|
2017-02-22 06:10:07 -06:00
|
|
|
Value: 3,
|
2016-01-26 07:39:21 -06:00
|
|
|
}
|
2017-02-22 06:10:07 -06:00
|
|
|
vmoduleFlag = cli.StringFlag{
|
2016-01-26 07:39:21 -06:00
|
|
|
Name: "vmodule",
|
2017-02-23 03:44:16 -06:00
|
|
|
Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
|
2017-02-22 06:10:07 -06:00
|
|
|
Value: "",
|
2016-01-26 07:39:21 -06:00
|
|
|
}
|
2017-02-22 06:10:07 -06:00
|
|
|
backtraceAtFlag = cli.StringFlag{
|
2016-01-26 07:39:21 -06:00
|
|
|
Name: "backtrace",
|
|
|
|
Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
|
2017-02-22 06:10:07 -06:00
|
|
|
Value: "",
|
2016-01-26 07:39:21 -06:00
|
|
|
}
|
2017-02-23 03:44:16 -06:00
|
|
|
debugFlag = cli.BoolFlag{
|
|
|
|
Name: "debug",
|
|
|
|
Usage: "Prepends log messages with call-site location (file and line number)",
|
|
|
|
}
|
2016-01-26 07:39:21 -06:00
|
|
|
pprofFlag = cli.BoolFlag{
|
|
|
|
Name: "pprof",
|
|
|
|
Usage: "Enable the pprof HTTP server",
|
|
|
|
}
|
|
|
|
pprofPortFlag = cli.IntFlag{
|
2020-05-05 03:19:17 -05:00
|
|
|
Name: "pprof.port",
|
2016-01-26 07:39:21 -06:00
|
|
|
Usage: "pprof HTTP server listening port",
|
|
|
|
Value: 6060,
|
|
|
|
}
|
2016-10-14 02:22:31 -05:00
|
|
|
pprofAddrFlag = cli.StringFlag{
|
2020-05-05 03:19:17 -05:00
|
|
|
Name: "pprof.addr",
|
2016-10-14 02:22:31 -05:00
|
|
|
Usage: "pprof HTTP server listening interface",
|
|
|
|
Value: "127.0.0.1",
|
|
|
|
}
|
2016-01-26 07:39:21 -06:00
|
|
|
memprofilerateFlag = cli.IntFlag{
|
2020-05-05 03:19:17 -05:00
|
|
|
Name: "pprof.memprofilerate",
|
2016-01-26 07:39:21 -06:00
|
|
|
Usage: "Turn on memory profiling with the given rate",
|
2016-03-11 17:36:15 -06:00
|
|
|
Value: runtime.MemProfileRate,
|
2016-01-26 07:39:21 -06:00
|
|
|
}
|
|
|
|
blockprofilerateFlag = cli.IntFlag{
|
2020-05-05 03:19:17 -05:00
|
|
|
Name: "pprof.blockprofilerate",
|
2016-01-26 07:39:21 -06:00
|
|
|
Usage: "Turn on block profiling with the given rate",
|
|
|
|
}
|
|
|
|
cpuprofileFlag = cli.StringFlag{
|
2020-05-05 03:19:17 -05:00
|
|
|
Name: "pprof.cpuprofile",
|
2016-01-26 07:39:21 -06:00
|
|
|
Usage: "Write CPU profile to the given file",
|
|
|
|
}
|
|
|
|
traceFlag = cli.StringFlag{
|
|
|
|
Name: "trace",
|
|
|
|
Usage: "Write execution trace to the given file",
|
|
|
|
}
|
2020-05-05 03:19:17 -05:00
|
|
|
// (Deprecated April 2020)
|
|
|
|
legacyPprofPortFlag = cli.IntFlag{
|
|
|
|
Name: "pprofport",
|
|
|
|
Usage: "pprof HTTP server listening port (deprecated, use --pprof.port)",
|
|
|
|
Value: 6060,
|
|
|
|
}
|
|
|
|
legacyPprofAddrFlag = cli.StringFlag{
|
|
|
|
Name: "pprofaddr",
|
|
|
|
Usage: "pprof HTTP server listening interface (deprecated, use --pprof.addr)",
|
|
|
|
Value: "127.0.0.1",
|
|
|
|
}
|
|
|
|
legacyMemprofilerateFlag = cli.IntFlag{
|
|
|
|
Name: "memprofilerate",
|
|
|
|
Usage: "Turn on memory profiling with the given rate (deprecated, use --pprof.memprofilerate)",
|
|
|
|
Value: runtime.MemProfileRate,
|
|
|
|
}
|
|
|
|
legacyBlockprofilerateFlag = cli.IntFlag{
|
|
|
|
Name: "blockprofilerate",
|
|
|
|
Usage: "Turn on block profiling with the given rate (deprecated, use --pprof.blockprofilerate)",
|
|
|
|
}
|
|
|
|
legacyCpuprofileFlag = cli.StringFlag{
|
|
|
|
Name: "cpuprofile",
|
|
|
|
Usage: "Write CPU profile to the given file (deprecated, use --pprof.cpuprofile)",
|
|
|
|
}
|
2016-01-26 07:39:21 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Flags holds all command-line flags required for debugging.
|
|
|
|
var Flags = []cli.Flag{
|
2017-02-23 03:44:16 -06:00
|
|
|
verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag,
|
2020-05-05 03:19:17 -05:00
|
|
|
pprofFlag, pprofAddrFlag, pprofPortFlag, memprofilerateFlag,
|
|
|
|
blockprofilerateFlag, cpuprofileFlag, traceFlag,
|
|
|
|
}
|
|
|
|
|
|
|
|
var DeprecatedFlags = []cli.Flag{
|
|
|
|
legacyPprofPortFlag, legacyPprofAddrFlag, legacyMemprofilerateFlag,
|
|
|
|
legacyBlockprofilerateFlag, legacyCpuprofileFlag,
|
2016-01-26 07:39:21 -06:00
|
|
|
}
|
|
|
|
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 02:59:04 -05:00
|
|
|
var (
|
|
|
|
ostream log.Handler
|
|
|
|
glogger *log.GlogHandler
|
|
|
|
)
|
2017-02-23 12:31:13 -06:00
|
|
|
|
|
|
|
func init() {
|
2018-09-29 09:15:39 -05:00
|
|
|
usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
|
2017-02-23 12:31:13 -06:00
|
|
|
output := io.Writer(os.Stderr)
|
|
|
|
if usecolor {
|
|
|
|
output = colorable.NewColorableStderr()
|
|
|
|
}
|
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature
* cmd, dashboard, internal, log: requested changes
* dashboard, vendor: gofmt, govendor, use vendored file watcher
* dashboard, log: gofmt -s -w, goimports
* dashboard, log: gosimple
2018-07-11 02:59:04 -05:00
|
|
|
ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
|
|
|
|
glogger = log.NewGlogHandler(ostream)
|
2017-02-23 12:31:13 -06:00
|
|
|
}
|
2017-02-22 06:10:07 -06:00
|
|
|
|
2016-01-26 07:39:21 -06:00
|
|
|
// Setup initializes profiling and logging based on the CLI flags.
|
|
|
|
// It should be called as early as possible in the program.
|
2020-01-21 06:57:33 -06:00
|
|
|
func Setup(ctx *cli.Context) error {
|
2016-01-26 07:39:21 -06:00
|
|
|
// logging
|
2017-02-23 03:44:16 -06:00
|
|
|
log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
|
2017-02-22 06:10:07 -06:00
|
|
|
glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
|
|
|
|
glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
|
|
|
|
glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))
|
|
|
|
log.Root().SetHandler(glogger)
|
2016-01-26 07:39:21 -06:00
|
|
|
|
|
|
|
// profiling, tracing
|
2020-05-05 03:19:17 -05:00
|
|
|
if ctx.GlobalIsSet(legacyMemprofilerateFlag.Name) {
|
|
|
|
runtime.MemProfileRate = ctx.GlobalInt(legacyMemprofilerateFlag.Name)
|
|
|
|
log.Warn("The flag --memprofilerate is deprecated and will be removed in the future, please use --pprof.memprofilerate")
|
|
|
|
}
|
2016-01-26 07:39:21 -06:00
|
|
|
runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
|
2020-05-05 03:19:17 -05:00
|
|
|
|
|
|
|
if ctx.GlobalIsSet(legacyBlockprofilerateFlag.Name) {
|
|
|
|
Handler.SetBlockProfileRate(ctx.GlobalInt(legacyBlockprofilerateFlag.Name))
|
|
|
|
log.Warn("The flag --blockprofilerate is deprecated and will be removed in the future, please use --pprof.blockprofilerate")
|
|
|
|
}
|
2016-01-26 07:39:21 -06:00
|
|
|
Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name))
|
2020-05-05 03:19:17 -05:00
|
|
|
|
2016-01-26 07:39:21 -06:00
|
|
|
if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
|
2016-05-06 04:15:05 -05:00
|
|
|
if err := Handler.StartGoTrace(traceFile); err != nil {
|
2016-01-26 07:39:21 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 03:19:17 -05:00
|
|
|
|
2016-01-26 07:39:21 -06:00
|
|
|
if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" {
|
|
|
|
if err := Handler.StartCPUProfile(cpuFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 03:19:17 -05:00
|
|
|
if cpuFile := ctx.GlobalString(legacyCpuprofileFlag.Name); cpuFile != "" {
|
|
|
|
log.Warn("The flag --cpuprofile is deprecated and will be removed in the future, please use --pprof.cpuprofile")
|
|
|
|
if err := Handler.StartCPUProfile(cpuFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 07:39:21 -06:00
|
|
|
|
|
|
|
// pprof server
|
|
|
|
if ctx.GlobalBool(pprofFlag.Name) {
|
2020-05-05 03:19:17 -05:00
|
|
|
listenHost := ctx.GlobalString(pprofAddrFlag.Name)
|
|
|
|
if ctx.GlobalIsSet(legacyPprofAddrFlag.Name) && !ctx.GlobalIsSet(pprofAddrFlag.Name) {
|
|
|
|
listenHost = ctx.GlobalString(legacyPprofAddrFlag.Name)
|
|
|
|
log.Warn("The flag --pprofaddr is deprecated and will be removed in the future, please use --pprof.addr")
|
|
|
|
}
|
|
|
|
|
|
|
|
port := ctx.GlobalInt(pprofPortFlag.Name)
|
|
|
|
if ctx.GlobalIsSet(legacyPprofPortFlag.Name) && !ctx.GlobalIsSet(pprofPortFlag.Name) {
|
|
|
|
port = ctx.GlobalInt(legacyPprofPortFlag.Name)
|
|
|
|
log.Warn("The flag --pprofport is deprecated and will be removed in the future, please use --pprof.port")
|
|
|
|
}
|
|
|
|
|
|
|
|
address := fmt.Sprintf("%s:%d", listenHost, port)
|
2018-04-23 08:20:39 -05:00
|
|
|
StartPProf(address)
|
2016-01-26 07:39:21 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:20:39 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-01-26 07:39:21 -06:00
|
|
|
// Exit stops all running profiles, flushing their output to the
|
|
|
|
// respective file.
|
|
|
|
func Exit() {
|
|
|
|
Handler.StopCPUProfile()
|
2016-05-06 04:15:05 -05:00
|
|
|
Handler.StopGoTrace()
|
2016-01-26 07:39:21 -06:00
|
|
|
}
|