aboutsummaryrefslogtreecommitdiffstats
path: root/internal/debug/flags.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2016-01-26 21:39:21 +0800
committerFelix Lange <fjl@twurst.com>2016-01-28 20:36:11 +0800
commit3750d835a1697f6784c727074cc959dda33cdcf3 (patch)
tree6183747f1ce69b6bb1ed256e4dd9a1bba0031d69 /internal/debug/flags.go
parente287b56b69cdf8340f679e9cfd229c959cc45e24 (diff)
downloaddexon-3750d835a1697f6784c727074cc959dda33cdcf3.tar
dexon-3750d835a1697f6784c727074cc959dda33cdcf3.tar.gz
dexon-3750d835a1697f6784c727074cc959dda33cdcf3.tar.bz2
dexon-3750d835a1697f6784c727074cc959dda33cdcf3.tar.lz
dexon-3750d835a1697f6784c727074cc959dda33cdcf3.tar.xz
dexon-3750d835a1697f6784c727074cc959dda33cdcf3.tar.zst
dexon-3750d835a1697f6784c727074cc959dda33cdcf3.zip
internal/debug: APIs for profiling and tracing
The debug package provides an RPC wrapper for glog settings and the debugging facilities of the Go runtime. They can be triggered through both command line flags and the IPC listener.
Diffstat (limited to 'internal/debug/flags.go')
-rw-r--r--internal/debug/flags.go117
1 files changed, 117 insertions, 0 deletions
diff --git a/internal/debug/flags.go b/internal/debug/flags.go
new file mode 100644
index 000000000..22e524cd6
--- /dev/null
+++ b/internal/debug/flags.go
@@ -0,0 +1,117 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package debug
+
+import (
+ "fmt"
+ "net/http"
+ _ "net/http/pprof"
+ "runtime"
+
+ "github.com/codegangsta/cli"
+ "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/logger/glog"
+)
+
+var (
+ verbosityFlag = cli.GenericFlag{
+ Name: "verbosity",
+ Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=detail",
+ Value: glog.GetVerbosity(),
+ }
+ vmoduleFlag = cli.GenericFlag{
+ Name: "vmodule",
+ Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=6,p2p=5)",
+ Value: glog.GetVModule(),
+ }
+ backtraceAtFlag = cli.GenericFlag{
+ Name: "backtrace",
+ Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
+ Value: glog.GetTraceLocation(),
+ }
+ pprofFlag = cli.BoolFlag{
+ Name: "pprof",
+ Usage: "Enable the pprof HTTP server",
+ }
+ pprofPortFlag = cli.IntFlag{
+ Name: "pprofport",
+ Usage: "pprof HTTP server listening port",
+ Value: 6060,
+ }
+ memprofilerateFlag = cli.IntFlag{
+ Name: "memprofilerate",
+ Usage: "Turn on memory profiling with the given rate",
+ }
+ blockprofilerateFlag = cli.IntFlag{
+ Name: "blockprofilerate",
+ Usage: "Turn on block profiling with the given rate",
+ }
+ cpuprofileFlag = cli.StringFlag{
+ Name: "cpuprofile",
+ Usage: "Write CPU profile to the given file",
+ }
+ traceFlag = cli.StringFlag{
+ Name: "trace",
+ Usage: "Write execution trace to the given file",
+ }
+)
+
+// Flags holds all command-line flags required for debugging.
+var Flags = []cli.Flag{
+ verbosityFlag, vmoduleFlag, backtraceAtFlag,
+ pprofFlag, pprofPortFlag,
+ memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
+}
+
+// Setup initializes profiling and logging based on the CLI flags.
+// It should be called as early as possible in the program.
+func Setup(ctx *cli.Context) error {
+ // logging
+ glog.CopyStandardLogTo("INFO")
+ glog.SetToStderr(true)
+
+ // profiling, tracing
+ runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
+ Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name))
+ if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
+ if err := Handler.StartTrace(traceFile); err != nil {
+ return err
+ }
+ }
+ if cpuFile := ctx.GlobalString(cpuprofileFlag.Name); cpuFile != "" {
+ if err := Handler.StartCPUProfile(cpuFile); err != nil {
+ return err
+ }
+ }
+
+ // pprof server
+ if ctx.GlobalBool(pprofFlag.Name) {
+ address := fmt.Sprintf("127.0.0.1:%d", ctx.GlobalInt(pprofPortFlag.Name))
+ go func() {
+ glog.V(logger.Info).Infof("starting pprof server at http://%s/debug/pprof", address)
+ glog.Errorln(http.ListenAndServe(address, nil))
+ }()
+ }
+ return nil
+}
+
+// Exit stops all running profiles, flushing their output to the
+// respective file.
+func Exit() {
+ Handler.StopCPUProfile()
+ Handler.StopTrace()
+}