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