flags.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "net/http"
  20. _ "net/http/pprof"
  21. "runtime"
  22. "github.com/codegangsta/cli"
  23. "github.com/ethereum/go-ethereum/logger"
  24. "github.com/ethereum/go-ethereum/logger/glog"
  25. )
  26. var (
  27. verbosityFlag = cli.GenericFlag{
  28. Name: "verbosity",
  29. Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=detail",
  30. Value: glog.GetVerbosity(),
  31. }
  32. vmoduleFlag = cli.GenericFlag{
  33. Name: "vmodule",
  34. Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=6,p2p=5)",
  35. Value: glog.GetVModule(),
  36. }
  37. backtraceAtFlag = cli.GenericFlag{
  38. Name: "backtrace",
  39. Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
  40. Value: glog.GetTraceLocation(),
  41. }
  42. pprofFlag = cli.BoolFlag{
  43. Name: "pprof",
  44. Usage: "Enable the pprof HTTP server",
  45. }
  46. pprofPortFlag = cli.IntFlag{
  47. Name: "pprofport",
  48. Usage: "pprof HTTP server listening port",
  49. Value: 6060,
  50. }
  51. memprofilerateFlag = cli.IntFlag{
  52. Name: "memprofilerate",
  53. Usage: "Turn on memory profiling with the given rate",
  54. Value: runtime.MemProfileRate,
  55. }
  56. blockprofilerateFlag = cli.IntFlag{
  57. Name: "blockprofilerate",
  58. Usage: "Turn on block profiling with the given rate",
  59. }
  60. cpuprofileFlag = cli.StringFlag{
  61. Name: "cpuprofile",
  62. Usage: "Write CPU profile to the given file",
  63. }
  64. traceFlag = cli.StringFlag{
  65. Name: "trace",
  66. Usage: "Write execution trace to the given file",
  67. }
  68. )
  69. // Flags holds all command-line flags required for debugging.
  70. var Flags = []cli.Flag{
  71. verbosityFlag, vmoduleFlag, backtraceAtFlag,
  72. pprofFlag, pprofPortFlag,
  73. memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
  74. }
  75. // Setup initializes profiling and logging based on the CLI flags.
  76. // It should be called as early as possible in the program.
  77. func Setup(ctx *cli.Context) error {
  78. // logging
  79. glog.CopyStandardLogTo("INFO")
  80. glog.SetToStderr(true)
  81. // profiling, tracing
  82. runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
  83. Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name))
  84. if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
  85. if err := Handler.StartTrace(traceFile); err != nil {
  86. return err
  87. }
  88. }
  89. if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" {
  90. if err := Handler.StartCPUProfile(cpuFile); err != nil {
  91. return err
  92. }
  93. }
  94. // pprof server
  95. if ctx.GlobalBool(pprofFlag.Name) {
  96. address := fmt.Sprintf("127.0.0.1:%d", ctx.GlobalInt(pprofPortFlag.Name))
  97. go func() {
  98. glog.V(logger.Info).Infof("starting pprof server at http://%s/debug/pprof", address)
  99. glog.Errorln(http.ListenAndServe(address, nil))
  100. }()
  101. }
  102. return nil
  103. }
  104. // Exit stops all running profiles, flushing their output to the
  105. // respective file.
  106. func Exit() {
  107. Handler.StopCPUProfile()
  108. Handler.StopTrace()
  109. }