-
Notifications
You must be signed in to change notification settings - Fork 28
cmd: introduce profiling options #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lzap
wants to merge
2
commits into
osbuild:main
Choose a base branch
from
lzap:pprof-opts1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| //go:build profiling | ||
|
|
||
| // See @cmd/image-builder/memprofile_stub.go for API documentation shared with the non-profiling build. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
| "runtime/pprof" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // Go default allocation sampling rate (see runtime.MemProfileRate). | ||
| const defaultMemProfileRate = 512 * 1024 | ||
|
|
||
| var ( | ||
| memProfilePath string | ||
| memProfileGoroutinePath string | ||
| memProfileRateOpt int = -1 | ||
| ) | ||
|
|
||
| func memProfileResetForProcessStart() { | ||
| runtime.MemProfileRate = 0 | ||
| } | ||
|
|
||
| func registerMemProfileFlags(root *cobra.Command) { | ||
| f := root.PersistentFlags() | ||
| f.StringVar(&memProfilePath, "memprofile", "", "write a heap memory profile in pprof format when the program exits (use with go tool pprof)") | ||
| f.StringVar(&memProfileGoroutinePath, "memprofile-goroutine", "", "write a goroutine profile in pprof format on exit (optional; use with go tool pprof)") | ||
| f.IntVar(&memProfileRateOpt, "memprofile-rate", -1, "when --memprofile is set, sets runtime.MemProfileRate for allocation sampling (-1 uses Go's default 524288; 0 samples only live heap at exit with minimal runtime overhead)") | ||
| } | ||
|
|
||
| func memProfilePersistentPreRun(_ *cobra.Command, _ []string) { | ||
| runtime.MemProfileRate = 0 | ||
| if memProfilePath != "" { | ||
| if memProfileRateOpt >= 0 { | ||
| runtime.MemProfileRate = memProfileRateOpt | ||
| } else { | ||
| runtime.MemProfileRate = defaultMemProfileRate | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func memProfileFlush() error { | ||
| var errs []error | ||
| if memProfilePath != "" { | ||
| if err := writeHeapProfile(memProfilePath); err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| } | ||
| if memProfileGoroutinePath != "" { | ||
| if err := writeGoroutineProfile(memProfileGoroutinePath); err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| } | ||
| return errors.Join(errs...) | ||
| } | ||
|
|
||
| func writeHeapProfile(path string) error { | ||
| f, err := os.Create(path) | ||
| if err != nil { | ||
| return fmt.Errorf("memprofile: create heap profile %q: %w", path, err) | ||
| } | ||
| defer f.Close() | ||
| if err := pprof.WriteHeapProfile(f); err != nil { | ||
| return fmt.Errorf("memprofile: write heap profile %q: %w", path, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func writeGoroutineProfile(path string) error { | ||
| f, err := os.Create(path) | ||
| if err != nil { | ||
| return fmt.Errorf("memprofile: create goroutine profile %q: %w", path, err) | ||
| } | ||
| defer f.Close() | ||
| prof := pprof.Lookup("goroutine") | ||
| if prof == nil { | ||
| return fmt.Errorf("memprofile: goroutine profile unavailable") | ||
| } | ||
| if err := prof.WriteTo(f, 0); err != nil { | ||
| return fmt.Errorf("memprofile: write goroutine profile %q: %w", path, err) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| //go:build !profiling | ||
|
|
||
| // Memory profiling hooks: this file is the no-op implementation when the binary is built | ||
| // without -tags profiling. See memprofile.go for the profiling implementation. | ||
|
|
||
| package main | ||
|
|
||
| import "github.com/spf13/cobra" | ||
|
|
||
| // memProfileResetForProcessStart is called at the start of run() before cobra runs; it keeps | ||
| // default allocation sampling behavior. With profiling, it forces sampling off until flags apply. | ||
| func memProfileResetForProcessStart() {} | ||
|
|
||
| // registerMemProfileFlags would attach --memprofile* flags to the root command; no flags are | ||
| // registered without the profiling build tag. | ||
| func registerMemProfileFlags(_ *cobra.Command) {} | ||
|
|
||
| // memProfilePersistentPreRun would set runtime.MemProfileRate from --memprofile* flags; it does | ||
| // nothing without the profiling build tag. | ||
| func memProfilePersistentPreRun(_ *cobra.Command, _ []string) {} | ||
|
|
||
| // memProfileFlush would write heap and/or goroutine profiles on exit; it always returns nil here. | ||
| func memProfileFlush() error { return nil } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //go:build profiling | ||
|
|
||
| package main_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| main "github.com/osbuild/image-builder-cli/cmd/image-builder" | ||
| testrepos "github.com/osbuild/images/test/data/repositories" | ||
| ) | ||
|
|
||
| func TestMemProfileWritesHeapAndGoroutineFiles(t *testing.T) { | ||
| restore := main.MockNewRepoRegistry(testrepos.New) | ||
| defer restore() | ||
|
|
||
| dir := t.TempDir() | ||
| heapPath := filepath.Join(dir, "heap.pprof") | ||
| gPath := filepath.Join(dir, "goroutine.pprof") | ||
|
|
||
| restore = main.MockOsArgs([]string{"list", "--format=json", "--memprofile", heapPath, "--memprofile-goroutine", gPath}) | ||
| defer restore() | ||
|
|
||
| var fakeStdout bytes.Buffer | ||
| restore = main.MockOsStdout(&fakeStdout) | ||
| defer restore() | ||
|
|
||
| err := main.Run() | ||
| require.NoError(t, err) | ||
|
|
||
| st, err := os.Stat(heapPath) | ||
| require.NoError(t, err) | ||
| require.Greater(t, st.Size(), int64(0)) | ||
|
|
||
| stg, err := os.Stat(gPath) | ||
| require.NoError(t, err) | ||
| require.Greater(t, stg.Size(), int64(0)) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit hard to read. It looks like the rate is always 0 unless
memProfilePathis used, somemProfileGoroutinePathdoesn't matter. I'd unconditionally set it to 0 first, then override that with theif memProfilePath != "" {block.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah thanks, rebased.