github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/internal/debug/flags.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package debug 18 19 import ( 20 "fmt" 21 "io" 22 "net/http" 23 _ "net/http/pprof" 24 "os" 25 "runtime" 26 27 "github.com/kisexp/xdchain/log" 28 "github.com/kisexp/xdchain/metrics" 29 "github.com/kisexp/xdchain/metrics/exp" 30 "github.com/fjl/memsize/memsizeui" 31 "github.com/mattn/go-colorable" 32 "github.com/mattn/go-isatty" 33 "gopkg.in/urfave/cli.v1" 34 ) 35 36 var Memsize memsizeui.Handler 37 38 var ( 39 verbosityFlag = cli.IntFlag{ 40 Name: "verbosity", 41 Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail", 42 Value: 3, 43 } 44 vmoduleFlag = cli.StringFlag{ 45 Name: "vmodule", 46 Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)", 47 Value: "", 48 } 49 backtraceAtFlag = cli.StringFlag{ 50 Name: "backtrace", 51 Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")", 52 Value: "", 53 } 54 debugFlag = cli.BoolFlag{ 55 Name: "debug", 56 Usage: "Prepends log messages with call-site location (file and line number)", 57 } 58 pprofFlag = cli.BoolFlag{ 59 Name: "pprof", 60 Usage: "Enable the pprof HTTP server", 61 } 62 pprofPortFlag = cli.IntFlag{ 63 Name: "pprof.port", 64 Usage: "pprof HTTP server listening port", 65 Value: 6060, 66 } 67 pprofAddrFlag = cli.StringFlag{ 68 Name: "pprof.addr", 69 Usage: "pprof HTTP server listening interface", 70 Value: "127.0.0.1", 71 } 72 memprofilerateFlag = cli.IntFlag{ 73 Name: "pprof.memprofilerate", 74 Usage: "Turn on memory profiling with the given rate", 75 Value: runtime.MemProfileRate, 76 } 77 blockprofilerateFlag = cli.IntFlag{ 78 Name: "pprof.blockprofilerate", 79 Usage: "Turn on block profiling with the given rate", 80 } 81 cpuprofileFlag = cli.StringFlag{ 82 Name: "pprof.cpuprofile", 83 Usage: "Write CPU profile to the given file", 84 } 85 traceFlag = cli.StringFlag{ 86 Name: "trace", 87 Usage: "Write execution trace to the given file", 88 } 89 // (Deprecated April 2020) 90 legacyPprofPortFlag = cli.IntFlag{ 91 Name: "pprofport", 92 Usage: "pprof HTTP server listening port (deprecated, use --pprof.port)", 93 Value: 6060, 94 } 95 legacyPprofAddrFlag = cli.StringFlag{ 96 Name: "pprofaddr", 97 Usage: "pprof HTTP server listening interface (deprecated, use --pprof.addr)", 98 Value: "127.0.0.1", 99 } 100 legacyMemprofilerateFlag = cli.IntFlag{ 101 Name: "memprofilerate", 102 Usage: "Turn on memory profiling with the given rate (deprecated, use --pprof.memprofilerate)", 103 Value: runtime.MemProfileRate, 104 } 105 legacyBlockprofilerateFlag = cli.IntFlag{ 106 Name: "blockprofilerate", 107 Usage: "Turn on block profiling with the given rate (deprecated, use --pprof.blockprofilerate)", 108 } 109 legacyCpuprofileFlag = cli.StringFlag{ 110 Name: "cpuprofile", 111 Usage: "Write CPU profile to the given file (deprecated, use --pprof.cpuprofile)", 112 } 113 ) 114 115 // Flags holds all command-line flags required for debugging. 116 var Flags = []cli.Flag{ 117 verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag, 118 pprofFlag, pprofAddrFlag, pprofPortFlag, memprofilerateFlag, 119 blockprofilerateFlag, cpuprofileFlag, traceFlag, 120 } 121 122 var DeprecatedFlags = []cli.Flag{ 123 legacyPprofPortFlag, legacyPprofAddrFlag, legacyMemprofilerateFlag, 124 legacyBlockprofilerateFlag, legacyCpuprofileFlag, 125 } 126 127 var ( 128 ostream log.Handler 129 glogger *log.GlogHandler 130 ) 131 132 func init() { 133 usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb" 134 output := io.Writer(os.Stderr) 135 if usecolor { 136 output = colorable.NewColorableStderr() 137 } 138 ostream = log.StreamHandler(output, log.TerminalFormat(usecolor)) 139 glogger = log.NewGlogHandler(ostream) 140 } 141 142 // Setup initializes profiling and logging based on the CLI flags. 143 // It should be called as early as possible in the program. 144 func Setup(ctx *cli.Context) error { 145 // logging 146 log.PrintOrigins(ctx.GlobalBool(debugFlag.Name)) 147 glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name))) 148 glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name)) 149 glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name)) 150 log.Root().SetHandler(glogger) 151 152 // profiling, tracing 153 if ctx.GlobalIsSet(legacyMemprofilerateFlag.Name) { 154 runtime.MemProfileRate = ctx.GlobalInt(legacyMemprofilerateFlag.Name) 155 log.Warn("The flag --memprofilerate is deprecated and will be removed in the future, please use --pprof.memprofilerate") 156 } 157 runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name) 158 159 if ctx.GlobalIsSet(legacyBlockprofilerateFlag.Name) { 160 Handler.SetBlockProfileRate(ctx.GlobalInt(legacyBlockprofilerateFlag.Name)) 161 log.Warn("The flag --blockprofilerate is deprecated and will be removed in the future, please use --pprof.blockprofilerate") 162 } 163 Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name)) 164 165 if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" { 166 if err := Handler.StartGoTrace(traceFile); err != nil { 167 return err 168 } 169 } 170 171 if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" { 172 if err := Handler.StartCPUProfile(cpuFile); err != nil { 173 return err 174 } 175 } 176 if cpuFile := ctx.GlobalString(legacyCpuprofileFlag.Name); cpuFile != "" { 177 log.Warn("The flag --cpuprofile is deprecated and will be removed in the future, please use --pprof.cpuprofile") 178 if err := Handler.StartCPUProfile(cpuFile); err != nil { 179 return err 180 } 181 } 182 183 // pprof server 184 if ctx.GlobalBool(pprofFlag.Name) { 185 listenHost := ctx.GlobalString(pprofAddrFlag.Name) 186 if ctx.GlobalIsSet(legacyPprofAddrFlag.Name) && !ctx.GlobalIsSet(pprofAddrFlag.Name) { 187 listenHost = ctx.GlobalString(legacyPprofAddrFlag.Name) 188 log.Warn("The flag --pprofaddr is deprecated and will be removed in the future, please use --pprof.addr") 189 } 190 191 port := ctx.GlobalInt(pprofPortFlag.Name) 192 if ctx.GlobalIsSet(legacyPprofPortFlag.Name) && !ctx.GlobalIsSet(pprofPortFlag.Name) { 193 port = ctx.GlobalInt(legacyPprofPortFlag.Name) 194 log.Warn("The flag --pprofport is deprecated and will be removed in the future, please use --pprof.port") 195 } 196 197 address := fmt.Sprintf("%s:%d", listenHost, port) 198 // This context value ("metrics.addr") represents the utils.MetricsHTTPFlag.Name. 199 // It cannot be imported because it will cause a cyclical dependency. 200 StartPProf(address, !ctx.GlobalIsSet("metrics.addr")) 201 } 202 return nil 203 } 204 205 func StartPProf(address string, withMetrics bool) { 206 // Hook go-metrics into expvar on any /debug/metrics request, load all vars 207 // from the registry into expvar, and execute regular expvar handler. 208 if withMetrics { 209 exp.Exp(metrics.DefaultRegistry) 210 } 211 http.Handle("/memsize/", http.StripPrefix("/memsize", &Memsize)) 212 log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address)) 213 go func() { 214 if err := http.ListenAndServe(address, nil); err != nil { 215 log.Error("Failure in running pprof server", "err", err) 216 } 217 }() 218 } 219 220 // Exit stops all running profiles, flushing their output to the 221 // respective file. 222 func Exit() { 223 Handler.StopCPUProfile() 224 Handler.StopGoTrace() 225 }