From d348db4a0d3b865f7b5bf7bfd4c3621de96c041a Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Sat, 4 Sep 2021 07:14:13 -0700 Subject: [PATCH] tinygo: add a flag for creating cpu profiles --- main.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/main.go b/main.go index 188a0100..a20c5f9b 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "path/filepath" "regexp" "runtime" + "runtime/pprof" "strconv" "strings" "sync/atomic" @@ -1089,6 +1090,7 @@ func main() { ldflags := flag.String("ldflags", "", "Go link tool compatible ldflags") wasmAbi := flag.String("wasm-abi", "", "WebAssembly ABI conventions: js (no i64 params) or generic") llvmFeatures := flag.String("llvm-features", "", "comma separated LLVM features to enable") + cpuprofile := flag.String("cpuprofile", "", "cpuprofile output") var flagJSON, flagDeps, flagTest *bool if command == "help" || command == "list" { @@ -1173,6 +1175,20 @@ func main() { os.Exit(1) } + if *cpuprofile != "" { + f, err := os.Create(*cpuprofile) + if err != nil { + fmt.Fprintln(os.Stderr, "could not create CPU profile: ", err) + os.Exit(1) + } + defer f.Close() + if err := pprof.StartCPUProfile(f); err != nil { + fmt.Fprintln(os.Stderr, "could not start CPU profile: ", err) + os.Exit(1) + } + defer pprof.StopCPUProfile() + } + switch command { case "build": if outpath == "" {