Skip to content
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

Add --profile-{cpu,mem} flags #1683

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ linters-settings:
disabled: true
nestif:
min-complexity: 17
maintidx:
under: 10
godox:
keywords:
- BUG
Expand All @@ -132,7 +134,7 @@ linters-settings:
cyclop:
max-complexity: 37
gocognit:
min-complexity: 53
min-complexity: 71
gci:
sections:
- standard
Expand Down
65 changes: 65 additions & 0 deletions cmd/crictl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -270,12 +272,47 @@ func main() {
Usage: "Address to which the gRPC tracing collector will send spans to.",
Value: "127.0.0.1:4317",
},
&cli.StringFlag{
Name: "profile-cpu",
Usage: "Write a pprof CPU profile to the provided path.",
},
&cli.StringFlag{
Name: "profile-mem",
Usage: "Write a pprof memory profile to the provided path.",
},
}

var cpuProfile *os.File
defer func() {
if cpuProfile != nil {
pprof.StopCPUProfile()
cpuProfile.Close()
}
}()

app.Before = func(context *cli.Context) (err error) {
var config *common.ServerConfiguration
var exePath string

cpuProfilePath := context.String("profile-cpu")
if cpuProfilePath != "" {
cpuProfilePath, err = filepath.Abs(cpuProfilePath)
if err != nil {
return fmt.Errorf("unable to get absolute memory profile path: %w", err)
}

logrus.Infof("Creating CPU profile in: %s", cpuProfilePath)

cpuProfile, err = os.Create(cpuProfilePath)
if err != nil {
return fmt.Errorf("could not create CPU profile %q: %w", cpuProfilePath, err)
}

if err := pprof.StartCPUProfile(cpuProfile); err != nil {
return fmt.Errorf("could not start CPU profiling in %q: %w", cpuProfilePath, err)
}
}

if exePath, err = os.Executable(); err != nil {
logrus.Fatal(err)
}
Expand Down Expand Up @@ -355,6 +392,34 @@ func main() {

return nil
}

app.After = func(ctx *cli.Context) (err error) {
memProfilePath := ctx.String("profile-mem")
if memProfilePath != "" {
memProfilePath, err = filepath.Abs(memProfilePath)
if err != nil {
return fmt.Errorf("unable to get absolute memory profile path: %w", err)
}

logrus.Infof("Creating memory profile in: %s", memProfilePath)

file, err := os.Create(memProfilePath)
if err != nil {
return fmt.Errorf("could not create memory profile %q: %w", memProfilePath, err)
}
defer file.Close()

// Ensure up to date data
runtime.GC()

if err := pprof.WriteHeapProfile(file); err != nil {
return fmt.Errorf("could not write memory profile in %q: %w", memProfilePath, err)
}
}

return nil
}

// sort all flags
for _, cmd := range app.Commands {
sort.Sort(cli.FlagsByName(cmd.Flags))
Expand Down
Loading