Skip to content

Commit

Permalink
Merge pull request #251 from depot/cpu-profiler
Browse files Browse the repository at this point in the history
Add conditional CPU profiler
  • Loading branch information
jacobwgillespie authored Feb 26, 2024
2 parents 387f676 + e11f385 commit 45d2bdd
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cmd/depot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"path"
"runtime/pprof"
"strings"
"syscall"

Expand All @@ -29,6 +30,21 @@ func main() {
helpers.DisableOTEL()
}

cpuProfile := os.Getenv("DEPOT_CPU_PROFILE")
var cpuProfileFile *os.File
if cpuProfile != "" {
var err error
cpuProfileFile, err = os.Create(cpuProfile)
if err != nil {
log.Fatal(err)
}

err = pprof.StartCPUProfile(cpuProfileFile)
if err != nil {
log.Fatal(err)
}
}

binary := os.Args[0]
if strings.HasSuffix(binary, "-buildx") {
cmd, subcmd := parseCmdSubcmd()
Expand All @@ -44,6 +60,12 @@ func main() {
}

code := runMain()

if cpuProfile != "" {
pprof.StopCPUProfile()
cpuProfileFile.Close()
}

os.Exit(code)
}

Expand Down

0 comments on commit 45d2bdd

Please sign in to comment.