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
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2017-01-23 16:52:26 +00:00
|
|
|
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`")
|
2017-03-25 17:00:42 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-23 16:52:26 +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
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-01-23 16:52:26 +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")
|
2017-01-23 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 04:54:01 +00:00
|
|
|
var prof interface {
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:52:26 +00:00
|
|
|
if memProfilePath != "" {
|
2017-08-29 04:54:01 +00:00
|
|
|
prof = profile.Start(profile.Quiet, profile.NoShutdownHook, profile.MemProfile, profile.ProfilePath(memProfilePath))
|
2017-05-17 15:07:33 +00:00
|
|
|
} else if cpuProfilePath != "" {
|
2017-08-29 04:54:01 +00:00
|
|
|
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))
|
2017-08-29 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if prof != nil {
|
2022-08-26 21:04:59 +00:00
|
|
|
AddCleanupHandler(func(code int) (int, error) {
|
2017-08-29 04:54:01 +00:00
|
|
|
prof.Stop()
|
2022-08-26 21:04:59 +00:00
|
|
|
return code, nil
|
2017-08-29 04:54:01 +00:00
|
|
|
})
|
2017-01-23 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 17:00:42 +00:00
|
|
|
if insecure {
|
|
|
|
repository.TestUseLowSecurityKDFParameters(fakeTestingTB{})
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:52:26 +00:00
|
|
|
return nil
|
|
|
|
}
|