restic/cmd/restic/global_debug.go

99 lines
2.6 KiB
Go
Raw Normal View History

2022-03-28 20:23:47 +00:00
//go:build debug || profile
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
)
type ProfileOptions struct {
listen string
memPath string
cpuPath string
tracePath string
blockPath string
insecure bool
}
var profileOpts ProfileOptions
var prof interface {
Stop()
}
2017-01-22 18:10:32 +00:00
func init() {
f := cmdRoot.PersistentFlags()
f.StringVar(&profileOpts.listen, "listen-profile", "", "listen on this `address:port` for memory profiling")
f.StringVar(&profileOpts.memPath, "mem-profile", "", "write memory profile to `dir`")
f.StringVar(&profileOpts.cpuPath, "cpu-profile", "", "write cpu profile to `dir`")
f.StringVar(&profileOpts.tracePath, "trace-profile", "", "write trace to `dir`")
f.StringVar(&profileOpts.blockPath, "block-profile", "", "write block profile to `dir`")
f.BoolVar(&profileOpts.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 {
if profileOpts.listen != "" {
fmt.Fprintf(os.Stderr, "running profile HTTP server on %v\n", profileOpts.listen)
2017-01-22 18:10:32 +00:00
go func() {
err := http.ListenAndServe(profileOpts.listen, 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 profileOpts.memPath != "" {
2018-04-08 19:55:26 +00:00
profilesEnabled++
}
if profileOpts.cpuPath != "" {
2018-04-08 19:55:26 +00:00
profilesEnabled++
}
if profileOpts.tracePath != "" {
2018-04-08 19:55:26 +00:00
profilesEnabled++
}
if profileOpts.blockPath != "" {
2018-04-30 12:18:12 +00:00
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")
}
if profileOpts.memPath != "" {
prof = profile.Start(profile.Quiet, profile.NoShutdownHook, profile.MemProfile, profile.ProfilePath(profileOpts.memPath))
} else if profileOpts.cpuPath != "" {
prof = profile.Start(profile.Quiet, profile.NoShutdownHook, profile.CPUProfile, profile.ProfilePath(profileOpts.cpuPath))
} else if profileOpts.tracePath != "" {
prof = profile.Start(profile.Quiet, profile.NoShutdownHook, profile.TraceProfile, profile.ProfilePath(profileOpts.tracePath))
} else if profileOpts.blockPath != "" {
prof = profile.Start(profile.Quiet, profile.NoShutdownHook, profile.BlockProfile, profile.ProfilePath(profileOpts.blockPath))
}
if profileOpts.insecure {
repository.TestUseLowSecurityKDFParameters(fakeTestingTB{})
}
return nil
}
func stopDebug() {
if prof != nil {
prof.Stop()
}
}