restic/cmd/restic/global_debug.go

98 lines
2.5 KiB
Go
Raw Normal View History

2018-04-29 13:42:05 +00:00
// +build debug profile
2017-01-22 18:10:32 +00:00
package main
import (
"fmt"
"net/http"
_ "net/http/pprof"
"os"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository"
"github.com/pkg/profile"
2017-01-22 18:10:32 +00:00
)
var (
2018-04-30 12:18:12 +00:00
listenProfile string
memProfilePath string
cpuProfilePath string
traceProfilePath string
blockProfilePath string
insecure bool
2017-01-22 18:10:32 +00:00
)
func init() {
f := cmdRoot.PersistentFlags()
2018-04-30 12:18:12 +00:00
f.StringVar(&listenProfile, "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 19:55:26 +00:00
f.StringVar(&traceProfilePath, "trace-profile", "", "write trace to `dir`")
2018-04-30 12:18:12 +00:00
f.StringVar(&blockProfilePath, "block-profile", "", "write block profile 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 18:10:32 +00:00
}
func runDebug() error {
2018-04-30 12:18:12 +00:00
if listenProfile != "" {
fmt.Fprintf(os.Stderr, "running profile HTTP server on %v\n", listenProfile)
2017-01-22 18:10:32 +00:00
go func() {
2018-04-30 12:18:12 +00:00
err := http.ListenAndServe(listenProfile, nil)
2017-01-22 18:10:32 +00:00
if err != nil {
2018-04-30 12:18:12 +00:00
fmt.Fprintf(os.Stderr, "profile HTTP server listen failed: %v\n", err)
2017-01-22 18:10:32 +00:00
}
}()
}
2018-04-08 19:55:26 +00:00
profilesEnabled := 0
if memProfilePath != "" {
profilesEnabled++
}
if cpuProfilePath != "" {
profilesEnabled++
}
if traceProfilePath != "" {
profilesEnabled++
}
2018-04-30 12:18:12 +00:00
if blockProfilePath != "" {
profilesEnabled++
}
2018-04-08 19:55:26 +00:00
if profilesEnabled > 1 {
2018-04-30 12:18:12 +00:00
return errors.Fatal("only one profile (memory, CPU, trace, or block) 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 19:55:26 +00:00
} else if traceProfilePath != "" {
prof = profile.Start(profile.Quiet, profile.NoShutdownHook, profile.TraceProfile, profile.ProfilePath(traceProfilePath))
2018-04-30 12:18:12 +00:00
} else if blockProfilePath != "" {
prof = profile.Start(profile.Quiet, profile.NoShutdownHook, profile.BlockProfile, profile.ProfilePath(blockProfilePath))
}
if prof != nil {
AddCleanupHandler(func() error {
prof.Stop()
return nil
})
}
if insecure {
repository.TestUseLowSecurityKDFParameters(fakeTestingTB{})
}
return nil
}