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"
|
2017-01-23 16:52:26 +00:00
|
|
|
|
|
|
|
"github.com/pkg/profile"
|
2017-01-22 18:10:32 +00:00
|
|
|
)
|
|
|
|
|
2024-04-05 21:16:15 +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()
|
2024-04-05 21:16:15 +00:00
|
|
|
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")
|
2017-03-25 17:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type fakeTestingTB struct{}
|
|
|
|
|
|
|
|
func (fakeTestingTB) Logf(msg string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, msg, args...)
|
2017-01-22 18:10:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 16:52:26 +00:00
|
|
|
func runDebug() error {
|
2024-04-05 21:16:15 +00:00
|
|
|
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() {
|
2024-04-05 21:16:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-01-23 16:52:26 +00:00
|
|
|
|
2018-04-08 19:55:26 +00:00
|
|
|
profilesEnabled := 0
|
2024-04-05 21:16:15 +00:00
|
|
|
if profileOpts.memPath != "" {
|
2018-04-08 19:55:26 +00:00
|
|
|
profilesEnabled++
|
|
|
|
}
|
2024-04-05 21:16:15 +00:00
|
|
|
if profileOpts.cpuPath != "" {
|
2018-04-08 19:55:26 +00:00
|
|
|
profilesEnabled++
|
|
|
|
}
|
2024-04-05 21:16:15 +00:00
|
|
|
if profileOpts.tracePath != "" {
|
2018-04-08 19:55:26 +00:00
|
|
|
profilesEnabled++
|
|
|
|
}
|
2024-04-05 21:16:15 +00:00
|
|
|
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")
|
2017-01-23 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 21:16:15 +00:00
|
|
|
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))
|
2017-08-29 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 21:16:15 +00:00
|
|
|
if profileOpts.insecure {
|
2017-03-25 17:00:42 +00:00
|
|
|
repository.TestUseLowSecurityKDFParameters(fakeTestingTB{})
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:52:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-04-05 21:16:15 +00:00
|
|
|
|
|
|
|
func stopDebug() {
|
|
|
|
if prof != nil {
|
|
|
|
prof.Stop()
|
|
|
|
}
|
|
|
|
}
|