2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-12 22:02:23 +00:00

Add options for creating a memory or CPU profile

This commit is contained in:
Alexander Neumann 2017-01-23 17:52:26 +01:00
parent 84255f4f4f
commit 47bd9cdf2f
3 changed files with 39 additions and 4 deletions

View File

@ -7,18 +7,29 @@ import (
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"restic/errors"
"github.com/pkg/profile"
) )
var ( var (
listenMemoryProfile string listenMemoryProfile string
memProfilePath string
cpuProfilePath string
prof interface {
Stop()
}
) )
func init() { func init() {
f := cmdRoot.PersistentFlags() f := cmdRoot.PersistentFlags()
f.StringVar(&listenMemoryProfile, "listen-profile", "", "listen on this `address:port` for memory profiling") f.StringVar(&listenMemoryProfile, "listen-profile", "", "listen on this `address:port` for memory profiling")
f.StringVar(&memProfilePath, "mem-profile", "", "write memory profile to `dir`")
f.StringVar(&cpuProfilePath, "cpu-profile", "", "write cpu profile to `dir`")
} }
func runDebug() { func runDebug() error {
if listenMemoryProfile != "" { if listenMemoryProfile != "" {
fmt.Fprintf(os.Stderr, "running memory profile HTTP server on %v\n", listenMemoryProfile) fmt.Fprintf(os.Stderr, "running memory profile HTTP server on %v\n", listenMemoryProfile)
go func() { go func() {
@ -28,4 +39,22 @@ func runDebug() {
} }
}() }()
} }
if memProfilePath != "" && cpuProfilePath != "" {
return errors.Fatal("only one profile (memory or CPU) may be activated at the same time")
}
if memProfilePath != "" {
prof = profile.Start(profile.Quiet, profile.MemProfile, profile.ProfilePath(memProfilePath))
} else if memProfilePath != "" {
prof = profile.Start(profile.Quiet, profile.CPUProfile, profile.ProfilePath(memProfilePath))
}
return nil
}
func shutdownDebug() {
if prof != nil {
prof.Stop()
}
} }

View File

@ -3,4 +3,7 @@
package main package main
// runDebug is a noop without the debug tag. // runDebug is a noop without the debug tag.
func runDebug() {} func runDebug() error { return nil }
// shutdownDebug is a noop without the debug tag.
func shutdownDebug() {}

View File

@ -25,8 +25,11 @@ directories in an encrypted repository stored on different backends.
// run the debug functions for all subcommands (if build tag "debug" is // run the debug functions for all subcommands (if build tag "debug" is
// enabled) // enabled)
PersistentPreRun: func(*cobra.Command, []string) { PersistentPreRunE: func(*cobra.Command, []string) error {
runDebug() return runDebug()
},
PersistentPostRun: func(*cobra.Command, []string) {
shutdownDebug()
}, },
} }