From 15191b7ede02f4c052a2a61c55c8ef7a95d8e134 Mon Sep 17 00:00:00 2001 From: Jonathan Moody <103143855+moodyjon@users.noreply.github.com> Date: Fri, 3 Jun 2022 15:08:09 -0400 Subject: [PATCH] [lbry] runtime: Add --memprofile option * Add --memprofile option. Add memprofile to sample config. * Add --memprofile to doc.go. --- config.go | 1 + doc.go | 1 + lbcd.go | 19 +++++++++++++++++++ sample-lbcd.conf | 3 +++ 4 files changed, 24 insertions(+) diff --git a/config.go b/config.go index fe77a818..aee85c3f 100644 --- a/config.go +++ b/config.go @@ -117,6 +117,7 @@ type config struct { ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"` ConnectPeers []string `long:"connect" description:"Connect only to the specified peers at startup"` CPUProfile string `long:"cpuprofile" description:"Write CPU profile to the specified file"` + MemProfile string `long:"memprofile" description:"Write memory profile to the specified file"` DataDir string `short:"b" long:"datadir" description:"Directory to store data"` DbType string `long:"dbtype" description:"Database backend to use for the Block Chain"` DebugLevel string `short:"d" long:"debuglevel" description:"Logging level for all subsystems {trace, debug, info, warn, error, critical} -- You may also specify =,=,... to set the log level for individual subsystems -- Use show to list available subsystems"` diff --git a/doc.go b/doc.go index ee4a9405..84a96ea8 100644 --- a/doc.go +++ b/doc.go @@ -78,6 +78,7 @@ Application Options: memory (default: 100) --maxpeers= Max number of inbound and outbound peers (default: 125) + --memprofile= Write memory profile to the specified file --miningaddr= Add the specified payment address to the list of addresses to use for generated blocks -- At least one address is required if the generate option is diff --git a/lbcd.go b/lbcd.go index b9606653..3a7860b1 100644 --- a/lbcd.go +++ b/lbcd.go @@ -92,6 +92,25 @@ func btcdMain(serverChan chan<- *server) error { defer pprof.StopCPUProfile() } + // Write memory profile if requested. + if cfg.MemProfile != "" { + f, err := os.Create(cfg.MemProfile + ".heap") + if err != nil { + btcdLog.Errorf("Unable to create mem profile: %v", err) + return err + } + defer f.Close() + defer pprof.Lookup("heap").WriteTo(f, 0) + + f, err = os.Create(cfg.MemProfile + ".allocs") + if err != nil { + btcdLog.Errorf("Unable to create mem profile: %v", err) + return err + } + defer f.Close() + defer pprof.Lookup("allocs").WriteTo(f, 0) + } + // Perform upgrades to btcd as new versions require it. if err := doUpgrades(); err != nil { btcdLog.Errorf("%v", err) diff --git a/sample-lbcd.conf b/sample-lbcd.conf index 2a84c797..f2c0b8c7 100644 --- a/sample-lbcd.conf +++ b/sample-lbcd.conf @@ -376,6 +376,9 @@ ; Write CPU profile to the specified file. ; cpuprofile= +; Write memory profile to the specified file. +; memprofile= + ; The port used to listen for HTTP profile requests. The profile server will ; be disabled if this option is not specified. The profile information can be ; accessed at http://localhost:/debug/pprof once running.