Skip to content

Commit 4e541d9

Browse files
committed
Add --profile-{cpu,mem} flags
The flags allow debugging `crictl` with respect to CPU and memory consumption. Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
1 parent 4dc7922 commit 4e541d9

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

.golangci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ linters-settings:
122122
disabled: true
123123
nestif:
124124
min-complexity: 17
125+
maintidx:
126+
under: 10
125127
godox:
126128
keywords:
127129
- BUG
@@ -132,7 +134,7 @@ linters-settings:
132134
cyclop:
133135
max-complexity: 37
134136
gocognit:
135-
min-complexity: 53
137+
min-complexity: 63
136138
gci:
137139
sections:
138140
- standard

cmd/crictl/main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"os"
2424
"runtime"
25+
"runtime/pprof"
2526
"slices"
2627
"sort"
2728
"strings"
@@ -270,12 +271,34 @@ func main() {
270271
Usage: "Address to which the gRPC tracing collector will send spans to.",
271272
Value: "127.0.0.1:4317",
272273
},
274+
&cli.StringFlag{
275+
Name: "profile-cpu",
276+
Usage: "Write a pprof CPU profile to the provided path.",
277+
},
278+
&cli.StringFlag{
279+
Name: "profile-mem",
280+
Usage: "Write a pprof memory profile to the provided path.",
281+
},
273282
}
274283

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

288+
cpuProfilePath := context.String("profile-cpu")
289+
if cpuProfilePath != "" {
290+
logrus.Infof("Creating CPU profile in: %s", cpuProfilePath)
291+
292+
file, err := os.Create(cpuProfilePath)
293+
if err != nil {
294+
return fmt.Errorf("could not create CPU profile: %w", err)
295+
}
296+
297+
if err := pprof.StartCPUProfile(file); err != nil {
298+
return fmt.Errorf("could not start CPU profiling: %w", err)
299+
}
300+
}
301+
279302
if exePath, err = os.Executable(); err != nil {
280303
logrus.Fatal(err)
281304
}
@@ -355,6 +378,29 @@ func main() {
355378

356379
return nil
357380
}
381+
382+
app.After = func(ctx *cli.Context) error {
383+
memProfilePath := ctx.String("profile-mem")
384+
if memProfilePath != "" {
385+
logrus.Infof("Creating memory profile in: %s", memProfilePath)
386+
387+
file, err := os.Create(memProfilePath)
388+
if err != nil {
389+
return fmt.Errorf("could not create memory profile: %w", err)
390+
}
391+
defer file.Close()
392+
393+
// Ensure up to date data
394+
runtime.GC()
395+
396+
if err := pprof.WriteHeapProfile(file); err != nil {
397+
return fmt.Errorf("could not write memory profile: %w", err)
398+
}
399+
}
400+
401+
return nil
402+
}
403+
358404
// sort all flags
359405
for _, cmd := range app.Commands {
360406
sort.Sort(cli.FlagsByName(cmd.Flags))
@@ -373,4 +419,7 @@ func main() {
373419
logrus.Errorf("Unable to shutdown tracer provider: %v", err)
374420
}
375421
}
422+
423+
// noop when profiling is not enabled
424+
pprof.StopCPUProfile()
376425
}

0 commit comments

Comments
 (0)