Skip to content
Open
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
21 changes: 21 additions & 0 deletions cmd/buildah/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"runtime"
"runtime/pprof"
"runtime/trace"
"strings"
"syscall"

Expand Down Expand Up @@ -38,6 +39,8 @@ type globalFlags struct {
CPUProfile string
cpuProfileFile *os.File
MemoryProfile string
TraceProfile string
traceProfileFile *os.File
UserShortNameAliasConfPath string
CgroupManager string
}
Expand Down Expand Up @@ -105,6 +108,7 @@ func init() {
rootCmd.PersistentFlags().StringVar(&globalFlagResults.LogLevel, logLevel, "warn", `the log level to be used, one of "trace", "debug", "info", "warn", "error", "fatal", or "panic"`)
rootCmd.PersistentFlags().StringVar(&globalFlagResults.CPUProfile, "cpu-profile", "", "`file` to write CPU profile")
rootCmd.PersistentFlags().StringVar(&globalFlagResults.MemoryProfile, "memory-profile", "", "`file` to write memory profile")
rootCmd.PersistentFlags().StringVar(&globalFlagResults.TraceProfile, "trace-profile", "", "`file` to write trace profile")

if err := rootCmd.PersistentFlags().MarkHidden("cpu-profile"); err != nil {
logrus.Fatalf("unable to mark cpu-profile flag as hidden: %v", err)
Expand All @@ -118,6 +122,9 @@ func init() {
if err := rootCmd.PersistentFlags().MarkHidden("memory-profile"); err != nil {
logrus.Fatalf("unable to mark memory-profile flag as hidden: %v", err)
}
if err := rootCmd.PersistentFlags().MarkHidden("trace-profile"); err != nil {
logrus.Fatalf("unable to mark trace-profile flag as hidden: %v", err)
}
}

func initConfig() {
Expand Down Expand Up @@ -157,6 +164,16 @@ func before(cmd *cobra.Command) error {
}
}

if globalFlagResults.TraceProfile != "" {
globalFlagResults.traceProfileFile, err = os.Create(globalFlagResults.TraceProfile)
if err != nil {
logrus.Fatalf("could not create trace output file %s: %v", globalFlagResults.TraceProfile, err)
}
if err := trace.Start(globalFlagResults.traceProfileFile); err != nil {
logrus.Fatalf("could not start trace: %v", err)
}
}

defaultContainerConfig, err := config.Default()
if err != nil {
return err
Expand Down Expand Up @@ -219,6 +236,10 @@ func after(cmd *cobra.Command) error {
logrus.Fatalf("could not write memory profile %s: %v", globalFlagResults.MemoryProfile, err)
}
}
if globalFlagResults.TraceProfile != "" {
trace.Stop()
globalFlagResults.traceProfileFile.Close()
}
return nil
}

Expand Down
33 changes: 33 additions & 0 deletions tests/profiling.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bats

load helpers

@test "trace-profile flag creates a valid Go trace" {
tmpfile="$TEST_SCRATCH_DIR/buildah-trace.$RANDOM.$RANDOM"
run_buildah info --trace-profile="$tmpfile"
assert "$status" -eq 0 "buildah info with --trace-profile should succeed"
[ -s "$tmpfile" ] || die "trace profile should not be empty"
run go tool trace -pprof=net "$tmpfile"
assert "$status" -eq 0 "go tool trace should succeed"
rm -f "$tmpfile"
}

@test "cpu-profile flag creates a valid CPU profile" {
tmpfile="$TEST_SCRATCH_DIR/buildah-cpu.$RANDOM.$RANDOM"
run_buildah info --cpu-profile="$tmpfile"
assert "$status" -eq 0 "buildah info with --cpu-profile should succeed"
[ -s "$tmpfile" ] || die "CPU profile should not be empty"
run go tool pprof -top "$tmpfile"
assert "$status" -eq 0 "go tool pprof should succeed on CPU profile"
rm -f "$tmpfile"
}

@test "memory-profile flag creates a valid memory profile" {
tmpfile="$TEST_SCRATCH_DIR/buildah-mem.$RANDOM.$RANDOM"
run_buildah info --memory-profile="$tmpfile"
assert "$status" -eq 0 "buildah info with --memory-profile should succeed"
[ -s "$tmpfile" ] || die "memory profile should not be empty"
run go tool pprof -top "$tmpfile"
assert "$status" -eq 0 "go tool pprof should succeed on memory profile"
rm -f "$tmpfile"
}