|
| 1 | +//go:build profiling |
| 2 | + |
| 3 | +// See @cmd/image-builder/memprofile_stub.go for API documentation shared with the non-profiling build. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "os" |
| 11 | + "runtime" |
| 12 | + "runtime/pprof" |
| 13 | + |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +// Go default allocation sampling rate (see runtime.MemProfileRate). |
| 18 | +const defaultMemProfileRate = 512 * 1024 |
| 19 | + |
| 20 | +var ( |
| 21 | + memProfilePath string |
| 22 | + memProfileGoroutinePath string |
| 23 | + memProfileRateOpt int = -1 |
| 24 | +) |
| 25 | + |
| 26 | +func memProfileResetForProcessStart() { |
| 27 | + runtime.MemProfileRate = 0 |
| 28 | +} |
| 29 | + |
| 30 | +func registerMemProfileFlags(root *cobra.Command) { |
| 31 | + f := root.PersistentFlags() |
| 32 | + f.StringVar(&memProfilePath, "memprofile", "", "write a heap memory profile in pprof format when the program exits (use with go tool pprof)") |
| 33 | + f.StringVar(&memProfileGoroutinePath, "memprofile-goroutine", "", "write a goroutine profile in pprof format on exit (optional; use with go tool pprof)") |
| 34 | + 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)") |
| 35 | +} |
| 36 | + |
| 37 | +func memProfilePersistentPreRun(_ *cobra.Command, _ []string) { |
| 38 | + runtime.MemProfileRate = 0 |
| 39 | + if memProfilePath != "" { |
| 40 | + if memProfileRateOpt >= 0 { |
| 41 | + runtime.MemProfileRate = memProfileRateOpt |
| 42 | + } else { |
| 43 | + runtime.MemProfileRate = defaultMemProfileRate |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func memProfileFlush() error { |
| 49 | + var errs []error |
| 50 | + if memProfilePath != "" { |
| 51 | + if err := writeHeapProfile(memProfilePath); err != nil { |
| 52 | + errs = append(errs, err) |
| 53 | + } |
| 54 | + } |
| 55 | + if memProfileGoroutinePath != "" { |
| 56 | + if err := writeGoroutineProfile(memProfileGoroutinePath); err != nil { |
| 57 | + errs = append(errs, err) |
| 58 | + } |
| 59 | + } |
| 60 | + return errors.Join(errs...) |
| 61 | +} |
| 62 | + |
| 63 | +func writeHeapProfile(path string) error { |
| 64 | + f, err := os.Create(path) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("memprofile: create heap profile %q: %w", path, err) |
| 67 | + } |
| 68 | + defer f.Close() |
| 69 | + if err := pprof.WriteHeapProfile(f); err != nil { |
| 70 | + return fmt.Errorf("memprofile: write heap profile %q: %w", path, err) |
| 71 | + } |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func writeGoroutineProfile(path string) error { |
| 76 | + f, err := os.Create(path) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("memprofile: create goroutine profile %q: %w", path, err) |
| 79 | + } |
| 80 | + defer f.Close() |
| 81 | + prof := pprof.Lookup("goroutine") |
| 82 | + if prof == nil { |
| 83 | + return fmt.Errorf("memprofile: goroutine profile unavailable") |
| 84 | + } |
| 85 | + if err := prof.WriteTo(f, 0); err != nil { |
| 86 | + return fmt.Errorf("memprofile: write goroutine profile %q: %w", path, err) |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
0 commit comments