From be5917a058a904099a2457aaf91a4c0dbbd34a8c Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Fri, 11 Dec 2020 13:54:42 -0500 Subject: [PATCH] add profiler dev option Signed-off-by: Alex Goodman --- cmd/root.go | 28 ++++++++++++++++++++++------ internal/config/config.go | 22 ++++++++++++++-------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 12c169d95..3652bd0cd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,14 +5,9 @@ import ( "fmt" "io/ioutil" "os" + "runtime/pprof" "strings" - "github.com/anchore/syft/syft/distro" - - "github.com/anchore/syft/syft/pkg" - - "github.com/anchore/syft/syft/source" - "github.com/anchore/syft/internal" "github.com/anchore/syft/internal/anchore" "github.com/anchore/syft/internal/bus" @@ -20,8 +15,11 @@ import ( "github.com/anchore/syft/internal/ui" "github.com/anchore/syft/internal/version" "github.com/anchore/syft/syft" + "github.com/anchore/syft/syft/distro" "github.com/anchore/syft/syft/event" + "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/presenter" + "github.com/anchore/syft/syft/source" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" @@ -56,7 +54,25 @@ You can also explicitly specify the scheme to use: } os.Exit(1) } + + if appConfig.Dev.ProfileCPU { + f, err := os.Create("cpu.profile") + if err != nil { + log.Errorf("unable to create CPU profile: %+v", err) + } else { + err := pprof.StartCPUProfile(f) + if err != nil { + log.Errorf("unable to start CPU profile: %+v", err) + } + } + } + err := doRunCmd(cmd, args) + + if appConfig.Dev.ProfileCPU { + pprof.StopCPUProfile() + } + if err != nil { log.Errorf(err.Error()) os.Exit(1) diff --git a/internal/config/config.go b/internal/config/config.go index 991cde1a8..aba34db0a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -17,14 +17,15 @@ import ( // Application is the main syft application configuration. type Application struct { - ConfigPath string `yaml:",omitempty"` // the location where the application config was read from (either from -c or discovered while loading) - PresenterOpt presenter.Option `yaml:"-"` // -o, the native Presenter.Option to use for report formatting - Output string `yaml:"output" mapstructure:"output"` // -o, the Presenter hint string to use for report formatting - ScopeOpt source.Scope `yaml:"-"` // -s, the native source.Scope option to use for how to catalog the container image - Scope string `yaml:"scope" mapstructure:"scope"` // -s, the source.Scope string hint for how to catalog the container image - Quiet bool `yaml:"quiet" mapstructure:"quiet"` // -q, indicates to not show any status output to stderr (ETUI or logging UI) - Log logging `yaml:"log" mapstructure:"log"` // all logging-related options - CliOptions CliOnlyOptions `yaml:"-"` // all options only available through the CLI (not via env vars or config) + ConfigPath string `yaml:",omitempty"` // the location where the application config was read from (either from -c or discovered while loading) + PresenterOpt presenter.Option `yaml:"-"` // -o, the native Presenter.Option to use for report formatting + Output string `yaml:"output" mapstructure:"output"` // -o, the Presenter hint string to use for report formatting + ScopeOpt source.Scope `yaml:"-"` // -s, the native source.Scope option to use for how to catalog the container image + Scope string `yaml:"scope" mapstructure:"scope"` // -s, the source.Scope string hint for how to catalog the container image + Quiet bool `yaml:"quiet" mapstructure:"quiet"` // -q, indicates to not show any status output to stderr (ETUI or logging UI) + Log logging `yaml:"log" mapstructure:"log"` // all logging-related options + CliOptions CliOnlyOptions `yaml:"-"` // all options only available through the CLI (not via env vars or config) + Dev Development `mapstructure:"dev"` CheckForAppUpdate bool `yaml:"check-for-app-update" mapstructure:"check-for-app-update"` // whether to check for an application update on start up or not Anchore anchore `yaml:"anchore" mapstructure:"anchore"` // options for interacting with Anchore Engine/Enterprise } @@ -53,6 +54,10 @@ type anchore struct { Dockerfile string `yaml:"dockerfile" mapstructure:"dockerfile"` // -d , dockerfile to attach for upload } +type Development struct { + ProfileCPU bool `mapstructure:"profile-cpu"` +} + // LoadApplicationConfig populates the given viper object with application configuration discovered on disk func LoadApplicationConfig(v *viper.Viper, cliOpts CliOnlyOptions, wasHostnameSet bool) (*Application, error) { // the user may not have a config, and this is OK, we can use the default config + default cobra cli values instead @@ -216,4 +221,5 @@ func setNonCliDefaultValues(v *viper.Viper) { v.SetDefault("log.file", "") v.SetDefault("log.structured", false) v.SetDefault("check-for-app-update", true) + v.SetDefault("dev.profile-cpu", false) }