github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/internal/debug/flags.go (about)

     1  package debug
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	_ "net/http/pprof"
     8  	"os"
     9  	"runtime"
    10  
    11  	colorable "github.com/mattn/go-colorable"
    12  	"github.com/neatlab/neatio/chain/log"
    13  	"github.com/neatlab/neatio/chain/log/term"
    14  	"github.com/neatlab/neatio/utilities/metrics"
    15  	"github.com/neatlab/neatio/utilities/metrics/exp"
    16  	"gopkg.in/urfave/cli.v1"
    17  )
    18  
    19  var (
    20  	verbosityFlag = cli.IntFlag{
    21  		Name:  "verbosity",
    22  		Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
    23  		Value: 3,
    24  	}
    25  	vmoduleFlag = cli.StringFlag{
    26  		Name:  "vmodule",
    27  		Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
    28  		Value: "",
    29  	}
    30  	backtraceAtFlag = cli.StringFlag{
    31  		Name:  "backtrace",
    32  		Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
    33  		Value: "",
    34  	}
    35  	debugFlag = cli.BoolFlag{
    36  		Name:  "debug",
    37  		Usage: "Prepends log messages with call-site location (file and line number)",
    38  	}
    39  	pprofFlag = cli.BoolFlag{
    40  		Name:  "pprof",
    41  		Usage: "Enable the pprof HTTP server",
    42  	}
    43  	pprofPortFlag = cli.IntFlag{
    44  		Name:  "pprofport",
    45  		Usage: "pprof HTTP server listening port",
    46  		Value: 6060,
    47  	}
    48  	pprofAddrFlag = cli.StringFlag{
    49  		Name:  "pprofaddr",
    50  		Usage: "pprof HTTP server listening interface",
    51  		Value: "127.0.0.1",
    52  	}
    53  	memprofilerateFlag = cli.IntFlag{
    54  		Name:  "memprofilerate",
    55  		Usage: "Turn on memory profiling with the given rate",
    56  		Value: runtime.MemProfileRate,
    57  	}
    58  	blockprofilerateFlag = cli.IntFlag{
    59  		Name:  "blockprofilerate",
    60  		Usage: "Turn on block profiling with the given rate",
    61  	}
    62  	cpuprofileFlag = cli.StringFlag{
    63  		Name:  "cpuprofile",
    64  		Usage: "Write CPU profile to the given file",
    65  	}
    66  	traceFlag = cli.StringFlag{
    67  		Name:  "trace",
    68  		Usage: "Write execution trace to the given file",
    69  	}
    70  )
    71  
    72  var Flags = []cli.Flag{
    73  	verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag,
    74  	pprofFlag, pprofAddrFlag, pprofPortFlag,
    75  	memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
    76  }
    77  
    78  var (
    79  	ostream log.Handler
    80  	glogger *log.GlogHandler
    81  )
    82  
    83  func init() {
    84  	usecolor := term.IsTty(os.Stderr.Fd()) && os.Getenv("TERM") != "dumb"
    85  	output := io.Writer(os.Stderr)
    86  	if usecolor {
    87  		output = colorable.NewColorableStderr()
    88  	}
    89  	ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
    90  	glogger = log.NewGlogHandler(ostream)
    91  }
    92  
    93  func Setup(ctx *cli.Context) error {
    94  	runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
    95  	Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name))
    96  	if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
    97  		if err := Handler.StartGoTrace(traceFile); err != nil {
    98  			return err
    99  		}
   100  	}
   101  	if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" {
   102  		if err := Handler.StartCPUProfile(cpuFile); err != nil {
   103  			return err
   104  		}
   105  	}
   106  
   107  	if ctx.GlobalBool(pprofFlag.Name) {
   108  		exp.Exp(metrics.DefaultRegistry)
   109  
   110  		address := fmt.Sprintf("%s:%d", ctx.GlobalString(pprofAddrFlag.Name), ctx.GlobalInt(pprofPortFlag.Name))
   111  		go func() {
   112  			log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address))
   113  			if err := http.ListenAndServe(address, nil); err != nil {
   114  				log.Error("Failure in running pprof server", "err", err)
   115  			}
   116  		}()
   117  	}
   118  	return nil
   119  }
   120  
   121  func Exit() {
   122  	Handler.StopCPUProfile()
   123  	Handler.StopGoTrace()
   124  }