From 668a36a652ab4be040fe90b1a5967f36faace5a1 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 22 Jan 2017 19:10:32 +0100 Subject: [PATCH] Add option for debug pprof service --- src/cmds/restic/global_debug.go | 31 +++++++++++++++++++++++++++++++ src/cmds/restic/global_release.go | 6 ++++++ src/cmds/restic/main.go | 6 ++++++ 3 files changed, 43 insertions(+) create mode 100644 src/cmds/restic/global_debug.go create mode 100644 src/cmds/restic/global_release.go diff --git a/src/cmds/restic/global_debug.go b/src/cmds/restic/global_debug.go new file mode 100644 index 000000000..347e9e2c6 --- /dev/null +++ b/src/cmds/restic/global_debug.go @@ -0,0 +1,31 @@ +// +build debug + +package main + +import ( + "fmt" + "net/http" + _ "net/http/pprof" + "os" +) + +var ( + listenMemoryProfile string +) + +func init() { + f := cmdRoot.PersistentFlags() + f.StringVar(&listenMemoryProfile, "listen-profile", "", "listen on this `address:port` for memory profiling") +} + +func runDebug() { + if listenMemoryProfile != "" { + fmt.Fprintf(os.Stderr, "running memory profile HTTP server on %v\n", listenMemoryProfile) + go func() { + err := http.ListenAndServe(listenMemoryProfile, nil) + if err != nil { + fmt.Fprintf(os.Stderr, "memory profile listen failed: %v\n", err) + } + }() + } +} diff --git a/src/cmds/restic/global_release.go b/src/cmds/restic/global_release.go new file mode 100644 index 000000000..88b639074 --- /dev/null +++ b/src/cmds/restic/global_release.go @@ -0,0 +1,6 @@ +// +build !debug + +package main + +// runDebug is a noop without the debug tag. +func runDebug() {} diff --git a/src/cmds/restic/main.go b/src/cmds/restic/main.go index fed711b9a..6e5a0b746 100644 --- a/src/cmds/restic/main.go +++ b/src/cmds/restic/main.go @@ -22,6 +22,12 @@ directories in an encrypted repository stored on different backends. `, SilenceErrors: true, SilenceUsage: true, + + // run the debug functions for all subcommands (if build tag "debug" is + // enabled) + PersistentPreRun: func(*cobra.Command, []string) { + runDebug() + }, } func init() {