util: pprof: Improve pprof for ease of use

This makes it slightly cleaner.
This commit is contained in:
James Shubin
2025-01-31 00:00:05 -05:00
parent 95f353c6a4
commit 7331d3a7ee
2 changed files with 16 additions and 13 deletions

20
main.go
View File

@@ -77,16 +77,6 @@ func main() {
return // for safety return // for safety
} }
// Run profiling if it's activated.
// TODO: Should we pass a logger into this?
ctx, cancel := context.WithCancel(context.Background())
if err := pprof.Run(ctx); err != nil {
fmt.Println(err)
os.Exit(1)
//return // redundant
}
defer cancel()
cliUtil.LogSetup(debug) cliUtil.LogSetup(debug)
data := &cliUtil.Data{ data := &cliUtil.Data{
Program: program, Program: program,
@@ -102,6 +92,16 @@ func main() {
Args: os.Args, Args: os.Args,
} }
// Run profiling if it's activated.
// TODO: Should we pass a logger into this?
ctx, cancel := context.WithCancel(context.Background())
if err := pprof.Run(ctx); err != nil {
fmt.Println(err)
os.Exit(1)
//return // redundant
}
defer cancel()
name := "" name := ""
if len(data.Args) > 1 { if len(data.Args) > 1 {
name = data.Args[1] name = data.Args[1]

View File

@@ -46,7 +46,7 @@ import (
// var, it returns nil. If this is not able to start logging, then it errors. If // var, it returns nil. If this is not able to start logging, then it errors. If
// it starts logging, this waits for an exit signal in a goroutine and returns // it starts logging, this waits for an exit signal in a goroutine and returns
// nil. The magic env name is MGMT_PPROF_PATH. Example usage: // nil. The magic env name is MGMT_PPROF_PATH. Example usage:
// MGMT_PPROF_PATH="~/pprof/out.pprof" ./mgmt run lang examples/lang/hello.mcl // MGMT_PPROF_PATH=~/pprof/"out.pprof" ./mgmt run lang examples/lang/hello0.mcl
// go tool pprof -no_browser -http :10000 ~/pprof/out.pprof // go tool pprof -no_browser -http :10000 ~/pprof/out.pprof
func Run(ctx context.Context) error { func Run(ctx context.Context) error {
s := os.Getenv("MGMT_PPROF_PATH") s := os.Getenv("MGMT_PPROF_PATH")
@@ -55,10 +55,12 @@ func Run(ctx context.Context) error {
log.Printf(format, v...) // XXX: use parent logger when available log.Printf(format, v...) // XXX: use parent logger when available
} }
if s == "" || !strings.HasPrefix(s, "/") { if s == "" {
return nil // not activated return nil // not activated
} }
logf("pprof logging to: %s", s) if !strings.HasPrefix(s, "/") {
return fmt.Errorf("pprof path is not absolute")
}
f, err := os.Create(s) f, err := os.Create(s)
if err != nil { if err != nil {
@@ -67,6 +69,7 @@ func Run(ctx context.Context) error {
if err := pprof.StartCPUProfile(f); err != nil { if err := pprof.StartCPUProfile(f); err != nil {
return fmt.Errorf("could not start CPU profile: %v", err) return fmt.Errorf("could not start CPU profile: %v", err)
} }
logf("pprof logging to: %s", s)
signals := make(chan os.Signal, 1+1) // 1 * ^C + 1 * SIGTERM signals := make(chan os.Signal, 1+1) // 1 * ^C + 1 * SIGTERM
signal.Notify(signals, os.Interrupt) // catch ^C signal.Notify(signals, os.Interrupt) // catch ^C