From 39a379ebe2c36a5f94915c76024733d19f4d2a0a Mon Sep 17 00:00:00 2001 From: Matheus Degiovani Date: Thu, 1 Dec 2022 14:56:25 -0300 Subject: [PATCH] dcrwallet: Add --cpuprofile option This generates a full CPU profile output. It also changes the memory profile to not stop after 5 minutes and instead continue until the execution ends. --- config.go | 1 + dcrwallet.go | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 1c1f606f8..959c2b053 100644 --- a/config.go +++ b/config.go @@ -91,6 +91,7 @@ type config struct { NoFileLogging bool `long:"nofilelogging" description:"Disable file logging"` Profile []string `long:"profile" description:"Enable HTTP profiling this interface/port"` MemProfile string `long:"memprofile" description:"Write mem profile to the specified file"` + CPUProfile string `long:"cpuprofile" description:"Write cpu profile to the specified file"` // Wallet options WalletPass string `long:"walletpass" default-mask:"-" description:"Public wallet password; required when created with one"` diff --git a/dcrwallet.go b/dcrwallet.go index fb49059f3..361962d9c 100644 --- a/dcrwallet.go +++ b/dcrwallet.go @@ -128,6 +128,23 @@ func run(ctx context.Context) error { } } + // Write cpu profile if requested. + if cfg.CPUProfile != "" { + if done(ctx) { + return ctx.Err() + } + + f, err := os.Create(cfg.CPUProfile) + if err != nil { + log.Errorf("Unable to create cpu profile: %v", err.Error()) + return err + + } + pprof.StartCPUProfile(f) + defer f.Close() + defer pprof.StopCPUProfile() + } + // Write mem profile if requested. if cfg.MemProfile != "" { if done(ctx) { @@ -136,12 +153,10 @@ func run(ctx context.Context) error { f, err := os.Create(cfg.MemProfile) if err != nil { - log.Errorf("Unable to create cpu profile: %v", err) + log.Errorf("Unable to create mem profile: %v", err) return err } - timer := time.NewTimer(time.Minute * 5) // 5 minutes - go func() { - <-timer.C + defer func() { pprof.WriteHeapProfile(f) f.Close() }()