flags.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. package debug
  17. import (
  18. "fmt"
  19. "io"
  20. "net/http"
  21. _ "net/http/pprof"
  22. "os"
  23. "runtime"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/metrics"
  26. "github.com/ethereum/go-ethereum/metrics/exp"
  27. "github.com/fjl/memsize/memsizeui"
  28. colorable "github.com/mattn/go-colorable"
  29. "github.com/mattn/go-isatty"
  30. "gopkg.in/urfave/cli.v1"
  31. )
  32. var Memsize memsizeui.Handler
  33. var (
  34. verbosityFlag = cli.IntFlag{
  35. Name: "verbosity",
  36. Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
  37. Value: 3,
  38. }
  39. vmoduleFlag = cli.StringFlag{
  40. Name: "vmodule",
  41. Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
  42. Value: "",
  43. }
  44. backtraceAtFlag = cli.StringFlag{
  45. Name: "backtrace",
  46. Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
  47. Value: "",
  48. }
  49. debugFlag = cli.BoolFlag{
  50. Name: "debug",
  51. Usage: "Prepends log messages with call-site location (file and line number)",
  52. }
  53. pprofFlag = cli.BoolFlag{
  54. Name: "pprof",
  55. Usage: "Enable the pprof HTTP server",
  56. }
  57. pprofPortFlag = cli.IntFlag{
  58. Name: "pprof.port",
  59. Usage: "pprof HTTP server listening port",
  60. Value: 6060,
  61. }
  62. pprofAddrFlag = cli.StringFlag{
  63. Name: "pprof.addr",
  64. Usage: "pprof HTTP server listening interface",
  65. Value: "127.0.0.1",
  66. }
  67. memprofilerateFlag = cli.IntFlag{
  68. Name: "pprof.memprofilerate",
  69. Usage: "Turn on memory profiling with the given rate",
  70. Value: runtime.MemProfileRate,
  71. }
  72. blockprofilerateFlag = cli.IntFlag{
  73. Name: "pprof.blockprofilerate",
  74. Usage: "Turn on block profiling with the given rate",
  75. }
  76. cpuprofileFlag = cli.StringFlag{
  77. Name: "pprof.cpuprofile",
  78. Usage: "Write CPU profile to the given file",
  79. }
  80. traceFlag = cli.StringFlag{
  81. Name: "trace",
  82. Usage: "Write execution trace to the given file",
  83. }
  84. // (Deprecated April 2020)
  85. legacyPprofPortFlag = cli.IntFlag{
  86. Name: "pprofport",
  87. Usage: "pprof HTTP server listening port (deprecated, use --pprof.port)",
  88. Value: 6060,
  89. }
  90. legacyPprofAddrFlag = cli.StringFlag{
  91. Name: "pprofaddr",
  92. Usage: "pprof HTTP server listening interface (deprecated, use --pprof.addr)",
  93. Value: "127.0.0.1",
  94. }
  95. legacyMemprofilerateFlag = cli.IntFlag{
  96. Name: "memprofilerate",
  97. Usage: "Turn on memory profiling with the given rate (deprecated, use --pprof.memprofilerate)",
  98. Value: runtime.MemProfileRate,
  99. }
  100. legacyBlockprofilerateFlag = cli.IntFlag{
  101. Name: "blockprofilerate",
  102. Usage: "Turn on block profiling with the given rate (deprecated, use --pprof.blockprofilerate)",
  103. }
  104. legacyCpuprofileFlag = cli.StringFlag{
  105. Name: "cpuprofile",
  106. Usage: "Write CPU profile to the given file (deprecated, use --pprof.cpuprofile)",
  107. }
  108. )
  109. // Flags holds all command-line flags required for debugging.
  110. var Flags = []cli.Flag{
  111. verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag,
  112. pprofFlag, pprofAddrFlag, pprofPortFlag, memprofilerateFlag,
  113. blockprofilerateFlag, cpuprofileFlag, traceFlag,
  114. }
  115. var DeprecatedFlags = []cli.Flag{
  116. legacyPprofPortFlag, legacyPprofAddrFlag, legacyMemprofilerateFlag,
  117. legacyBlockprofilerateFlag, legacyCpuprofileFlag,
  118. }
  119. var (
  120. ostream log.Handler
  121. glogger *log.GlogHandler
  122. )
  123. func init() {
  124. usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
  125. output := io.Writer(os.Stderr)
  126. if usecolor {
  127. output = colorable.NewColorableStderr()
  128. }
  129. ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
  130. glogger = log.NewGlogHandler(ostream)
  131. }
  132. // Setup initializes profiling and logging based on the CLI flags.
  133. // It should be called as early as possible in the program.
  134. func Setup(ctx *cli.Context) error {
  135. // logging
  136. log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
  137. glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
  138. glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
  139. glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))
  140. log.Root().SetHandler(glogger)
  141. // profiling, tracing
  142. if ctx.GlobalIsSet(legacyMemprofilerateFlag.Name) {
  143. runtime.MemProfileRate = ctx.GlobalInt(legacyMemprofilerateFlag.Name)
  144. log.Warn("The flag --memprofilerate is deprecated and will be removed in the future, please use --pprof.memprofilerate")
  145. }
  146. runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
  147. if ctx.GlobalIsSet(legacyBlockprofilerateFlag.Name) {
  148. Handler.SetBlockProfileRate(ctx.GlobalInt(legacyBlockprofilerateFlag.Name))
  149. log.Warn("The flag --blockprofilerate is deprecated and will be removed in the future, please use --pprof.blockprofilerate")
  150. }
  151. Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name))
  152. if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
  153. if err := Handler.StartGoTrace(traceFile); err != nil {
  154. return err
  155. }
  156. }
  157. if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" {
  158. if err := Handler.StartCPUProfile(cpuFile); err != nil {
  159. return err
  160. }
  161. }
  162. if cpuFile := ctx.GlobalString(legacyCpuprofileFlag.Name); cpuFile != "" {
  163. log.Warn("The flag --cpuprofile is deprecated and will be removed in the future, please use --pprof.cpuprofile")
  164. if err := Handler.StartCPUProfile(cpuFile); err != nil {
  165. return err
  166. }
  167. }
  168. // pprof server
  169. if ctx.GlobalBool(pprofFlag.Name) {
  170. listenHost := ctx.GlobalString(pprofAddrFlag.Name)
  171. if ctx.GlobalIsSet(legacyPprofAddrFlag.Name) && !ctx.GlobalIsSet(pprofAddrFlag.Name) {
  172. listenHost = ctx.GlobalString(legacyPprofAddrFlag.Name)
  173. log.Warn("The flag --pprofaddr is deprecated and will be removed in the future, please use --pprof.addr")
  174. }
  175. port := ctx.GlobalInt(pprofPortFlag.Name)
  176. if ctx.GlobalIsSet(legacyPprofPortFlag.Name) && !ctx.GlobalIsSet(pprofPortFlag.Name) {
  177. port = ctx.GlobalInt(legacyPprofPortFlag.Name)
  178. log.Warn("The flag --pprofport is deprecated and will be removed in the future, please use --pprof.port")
  179. }
  180. address := fmt.Sprintf("%s:%d", listenHost, port)
  181. StartPProf(address)
  182. }
  183. return nil
  184. }
  185. func StartPProf(address string) {
  186. // Hook go-metrics into expvar on any /debug/metrics request, load all vars
  187. // from the registry into expvar, and execute regular expvar handler.
  188. exp.Exp(metrics.DefaultRegistry)
  189. http.Handle("/memsize/", http.StripPrefix("/memsize", &Memsize))
  190. log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address))
  191. go func() {
  192. if err := http.ListenAndServe(address, nil); err != nil {
  193. log.Error("Failure in running pprof server", "err", err)
  194. }
  195. }()
  196. }
  197. // Exit stops all running profiles, flushing their output to the
  198. // respective file.
  199. func Exit() {
  200. Handler.StopCPUProfile()
  201. Handler.StopGoTrace()
  202. }