restic/cmd/restic/global_debug.go

91 lines
2.2 KiB
Go
Raw Normal View History

2017-01-22 19:10:32 +01:00
// +build debug
package main
import (
"fmt"
"net/http"
_ "net/http/pprof"
"os"
2017-07-23 14:21:03 +02:00
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository"
"github.com/pkg/profile"
2017-01-22 19:10:32 +01:00
)
var (
listenMemoryProfile string
memProfilePath string
cpuProfilePath string
2018-04-08 21:55:26 +02:00
traceProfilePath string
insecure bool
2017-01-22 19:10:32 +01:00
)
func init() {
f := cmdRoot.PersistentFlags()
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`")
2018-04-08 21:55:26 +02:00
f.StringVar(&traceProfilePath, "trace-profile", "", "write trace to `dir`")
f.BoolVar(&insecure, "insecure-kdf", false, "use insecure KDF settings")
}
type fakeTestingTB struct{}
func (fakeTestingTB) Logf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg, args...)
2017-01-22 19:10:32 +01:00
}
func runDebug() error {
2017-01-22 19:10:32 +01:00
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)
}
}()
}
2018-04-08 21:55:26 +02:00
profilesEnabled := 0
if memProfilePath != "" {
profilesEnabled++
}
if cpuProfilePath != "" {
profilesEnabled++
}
if traceProfilePath != "" {
profilesEnabled++
}
if profilesEnabled > 1 {
return errors.Fatal("only one profile (memory or CPU) may be activated at the same time")
}
var prof interface {
Stop()
}
if memProfilePath != "" {
prof = profile.Start(profile.Quiet, profile.NoShutdownHook, profile.MemProfile, profile.ProfilePath(memProfilePath))
} else if cpuProfilePath != "" {
prof = profile.Start(profile.Quiet, profile.NoShutdownHook, profile.CPUProfile, profile.ProfilePath(cpuProfilePath))
2018-04-08 21:55:26 +02:00
} else if traceProfilePath != "" {
prof = profile.Start(profile.Quiet, profile.NoShutdownHook, profile.TraceProfile, profile.ProfilePath(traceProfilePath))
}
if prof != nil {
AddCleanupHandler(func() error {
prof.Stop()
return nil
})
}
if insecure {
repository.TestUseLowSecurityKDFParameters(fakeTestingTB{})
}
return nil
}