From 61af6399c8dcc2ed91694a99b86c6635b7b47775 Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Fri, 9 Jul 2021 21:35:00 -0700 Subject: [PATCH] cli: fixed profiling flags (#1181) --- Makefile | 16 +++++++++------ cli/app.go | 11 +++-------- cli/profile.go | 43 ++++++++++++++++++++++++++--------------- cli/profile_disabled.go | 9 ++++++++- tools/tools.mk | 1 + 5 files changed, 49 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index a467c115c..abf2e0f3f 100644 --- a/Makefile +++ b/Makefile @@ -45,13 +45,17 @@ endif -include ./Makefile.local.mk install: html-ui - go install $(KOPIA_BUILD_FLAGS) -tags embedhtml + go install $(KOPIA_BUILD_FLAGS) -tags $(KOPIA_BUILD_TAGS) + +install-profiling: KOPIA_BUILD_TAGS=embedhtml,profiling +install-profiling: html-ui + go install $(KOPIA_BUILD_FLAGS) -tags $(KOPIA_BUILD_TAGS) install-noui: go install $(KOPIA_BUILD_FLAGS) install-race: - go install -race $(KOPIA_BUILD_FLAGS) -tags embedhtml + go install -race $(KOPIA_BUILD_FLAGS) -tags $(KOPIA_BUILD_TAGS) lint: $(linter) $(linter) --deadline $(LINTER_DEADLINE) run $(linter_flags) @@ -106,8 +110,8 @@ htmlui/build/index.html: html-ui # on macOS build and sign AMD64, ARM64 and Universal binary and *.tar.gz files for them dist/kopia_darwin_universal/kopia dist/kopia_darwin_amd64/kopia dist/kopia_darwin_arm6/kopia: htmlui/build/index.html $(all_go_sources) - GOARCH=arm64 go build $(KOPIA_BUILD_FLAGS) -o dist/kopia_darwin_arm64/kopia -tags embedhtml - GOARCH=amd64 go build $(KOPIA_BUILD_FLAGS) -o dist/kopia_darwin_amd64/kopia -tags embedhtml + GOARCH=arm64 go build $(KOPIA_BUILD_FLAGS) -o dist/kopia_darwin_arm64/kopia -tags $(KOPIA_BUILD_TAGS) + GOARCH=amd64 go build $(KOPIA_BUILD_FLAGS) -o dist/kopia_darwin_amd64/kopia -tags $(KOPIA_BUILD_TAGS) mkdir -p dist/kopia_darwin_universal lipo -create -output dist/kopia_darwin_universal/kopia dist/kopia_darwin_arm64/kopia dist/kopia_darwin_amd64/kopia ifneq ($(MACOS_SIGNING_IDENTITY),) @@ -121,7 +125,7 @@ endif # on Windows build and sign AMD64 and *.zip file dist/kopia_windows_amd64/kopia.exe: htmlui/build/index.html $(all_go_sources) - GOOS=windows GOARCH=amd64 go build $(KOPIA_BUILD_FLAGS) -o dist/kopia_windows_amd64/kopia.exe -tags embedhtml + GOOS=windows GOARCH=amd64 go build $(KOPIA_BUILD_FLAGS) -o dist/kopia_windows_amd64/kopia.exe -tags $(KOPIA_BUILD_TAGS) ifneq ($(WINDOWS_SIGN_TOOL),) tools/.tools/signtool.exe sign //sha1 $(WINDOWS_CERT_SHA1) //fd sha256 //tr "http://timestamp.digicert.com" //v dist/kopia_windows_amd64/kopia.exe endif @@ -136,7 +140,7 @@ dist/kopia_linux_amd64/kopia dist/kopia_linux_arm64/kopia dist/kopia_linux_arm_6 ifeq ($(GOARCH),amd64) $(MAKE) goreleaser else - go build $(KOPIA_BUILD_FLAGS) -o $(kopia_ui_embedded_exe) -tags embedhtml + go build $(KOPIA_BUILD_FLAGS) -o $(kopia_ui_embedded_exe) -tags $(KOPIA_BUILD_TAGS) endif # builds kopia CLI binary that will be later used as a server for kopia-ui. diff --git a/cli/app.go b/cli/app.go index 1245751b3..fc16a261d 100644 --- a/cli/app.go +++ b/cli/app.go @@ -7,8 +7,6 @@ import ( "io" "net/http" "os" - "runtime/debug" - "strings" "time" "github.com/alecthomas/kingpin" @@ -37,11 +35,6 @@ type textOutput struct { } func (o *textOutput) setup(svc appServices) { - s := string(debug.Stack()) - if strings.Contains(s, "cliProgress") { - fmt.Printf("setting up %s with %v\n", debug.Stack(), svc.stdout()) - } - o.svc = svc } @@ -108,6 +101,7 @@ type advancedAppServices interface { type App struct { // global flags enableAutomaticMaintenance bool + pf profileFlags mt memoryTracker progress *cliProgress initialUpdateCheckDelay time.Duration @@ -221,6 +215,7 @@ func (c *App) setup(app *kingpin.Application) { ).Bool() c.mt.setup(app) + c.pf.setup(app) c.progress.setup(c, app) c.blob.setup(c, app) @@ -390,7 +385,7 @@ func (c *App) maybeRepositoryAction(act func(ctx context.Context, rep repo.Repos return func(kpc *kingpin.ParseContext) error { ctx := c.rootContext() - if err := withProfiling(func() error { + if err := c.pf.withProfiling(func() error { c.mt.startMemoryTracking(ctx) defer c.mt.finishMemoryTracking(ctx) diff --git a/cli/profile.go b/cli/profile.go index 650ecbac0..084c51965 100644 --- a/cli/profile.go +++ b/cli/profile.go @@ -2,30 +2,41 @@ package cli -import "github.com/pkg/profile" - -var ( - profileDir = app.Flag("profile-dir", "Write profile to the specified directory").Hidden().String() - profileCPU = app.Flag("profile-cpu", "Enable CPU profiling").Hidden().Bool() - profileMemory = app.Flag("profile-memory", "Enable memory profiling").Hidden().Int() - profileBlocking = app.Flag("profile-blocking", "Enable block profiling").Hidden().Bool() - profileMutex = app.Flag("profile-mutex", "Enable mutex profiling").Hidden().Bool() +import ( + "github.com/alecthomas/kingpin" + "github.com/pkg/profile" ) +type profileFlags struct { + profileDir string + profileCPU bool + profileMemory int + profileBlocking bool + profileMutex bool +} + +func (c *profileFlags) setup(app *kingpin.Application) { + app.Flag("profile-dir", "Write profile to the specified directory").Hidden().StringVar(&c.profileDir) + app.Flag("profile-cpu", "Enable CPU profiling").Hidden().BoolVar(&c.profileCPU) + app.Flag("profile-memory", "Enable memory profiling").Hidden().IntVar(&c.profileMemory) + app.Flag("profile-blocking", "Enable block profiling").Hidden().BoolVar(&c.profileBlocking) + app.Flag("profile-mutex", "Enable mutex profiling").Hidden().BoolVar(&c.profileMutex) +} + // withProfiling runs the given callback with profiling enabled, configured according to command line flags. -func withProfiling(callback func() error) error { - if *profileDir != "" { - pp := profile.ProfilePath(*profileDir) - if *profileMemory > 0 { - defer profile.Start(pp, profile.MemProfileRate(*profileMemory)).Stop() +func (c *profileFlags) withProfiling(callback func() error) error { + if c.profileDir != "" { + pp := profile.ProfilePath(c.profileDir) + if c.profileMemory > 0 { + defer profile.Start(pp, profile.MemProfileRate(c.profileMemory)).Stop() } - if *profileCPU { + if c.profileCPU { defer profile.Start(pp, profile.CPUProfile).Stop() } - if *profileBlocking { + if c.profileBlocking { defer profile.Start(pp, profile.BlockProfile).Stop() } - if *profileMutex { + if c.profileMutex { defer profile.Start(pp, profile.MutexProfile).Stop() } } diff --git a/cli/profile_disabled.go b/cli/profile_disabled.go index 361b68c6a..6df55e6ff 100644 --- a/cli/profile_disabled.go +++ b/cli/profile_disabled.go @@ -2,7 +2,14 @@ package cli +import "github.com/alecthomas/kingpin" + +type profileFlags struct{} + +func (c *profileFlags) setup(app *kingpin.Application) { +} + // withProfiling runs the given callback with profiling enabled, configured according to command line flags. -func withProfiling(callback func() error) error { +func (c *profileFlags) withProfiling(callback func() error) error { return callback() } diff --git a/tools/tools.mk b/tools/tools.mk index 1623fea23..c778b0ef0 100644 --- a/tools/tools.mk +++ b/tools/tools.mk @@ -277,6 +277,7 @@ export KOPIA_VERSION_NO_PREFIX=$(KOPIA_VERSION:v%=%) export REACT_APP_SHORT_VERSION_INFO:=$(KOPIA_VERSION) export REACT_APP_FULL_VERSION_INFO:=$(KOPIA_VERSION) built on $(date_full) $(hostname) +KOPIA_BUILD_TAGS=embedhtml KOPIA_BUILD_FLAGS=-ldflags "-s -w -X github.com/kopia/kopia/repo.BuildVersion=$(KOPIA_VERSION_NO_PREFIX) -X github.com/kopia/kopia/repo.BuildInfo=$(shell git rev-parse HEAD) -X github.com/kopia/kopia/repo.BuildGitHubRepo=$(GITHUB_REPOSITORY)" clean-tools: