Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GOGC and GOMAXPROCS configuration items added and defaults set #1899

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ coverage.txt
btcec/coverage.txt
btcutil/coverage.txt
btcutil/psbt/coverage.txt
/.idea/.gitignore
/.idea/btcd.iml
/.idea/modules.xml
/.idea/vcs.xml
31 changes: 30 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (
"os"
"path/filepath"
"runtime"
"runtime/debug"
"sort"
"strconv"
"strings"
"time"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/connmgr"
Expand All @@ -30,7 +32,6 @@ import (
"github.com/btcsuite/btcd/mempool"
"github.com/btcsuite/btcd/peer"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/go-socks/socks"
flags "github.com/jessevdk/go-flags"
)
Expand Down Expand Up @@ -66,6 +67,7 @@ const (
sampleConfigFilename = "sample-btcd.conf"
defaultTxIndex = false
defaultAddrIndex = false
defaultGOGC = 300
)

var (
Expand All @@ -76,6 +78,7 @@ var (
defaultRPCKeyFile = filepath.Join(defaultHomeDir, "rpc.key")
defaultRPCCertFile = filepath.Join(defaultHomeDir, "rpc.cert")
defaultLogDir = filepath.Join(defaultHomeDir, defaultLogDirname)
defaultGOMAXPROCS = runtime.NumCPU() * 3
)

// runServiceCommand is only set to a real function on Windows. It is used
Expand Down Expand Up @@ -119,6 +122,8 @@ type config struct {
DropTxIndex bool `long:"droptxindex" description:"Deletes the hash-based transaction index from the database on start up and then exits."`
ExternalIPs []string `long:"externalip" description:"Add an ip to the list of local addresses we claim to listen on to peers"`
Generate bool `long:"generate" description:"Generate (mine) bitcoins using the CPU"`
GoGC int `long:"gogc" description:"configures the ratio of the garbage collector (GOGC) - higher values will lead to greater memory usage but decrease time spent by Go runtime collecting garbage"`
GoMaxProcs int `long:"gomaxprocs" description:"sets the maximum number of goroutines to allow to run concurrently (GOMAXPROCS). Goroutines are not processor threads, they are better when greater than processor threads"`
FreeTxRelayLimit float64 `long:"limitfreerelay" description:"Limit relay of transactions with no transaction fee to the given amount in thousands of bytes per minute"`
Listeners []string `long:"listen" description:"Add an interface/port to listen for connections (default all interfaces port: 8333, testnet: 18333)"`
LogDir string `long:"logdir" description:"Directory to log output."`
Expand Down Expand Up @@ -439,6 +444,8 @@ func loadConfig() (*config, []string, error) {
Generate: defaultGenerate,
TxIndex: defaultTxIndex,
AddrIndex: defaultAddrIndex,
GoMaxProcs: defaultGOMAXPROCS,
GoGC: defaultGOGC,
}

// Service options which are only added on Windows.
Expand Down Expand Up @@ -1136,6 +1143,28 @@ func loadConfig() (*config, []string, error) {
}
}

// Clamp GOMAXPROCS between 1 and 10x number of CPU threads
if cfg.GoMaxProcs < 1 {
cfg.GoMaxProcs = 1
}
if cfg.GoMaxProcs > runtime.NumCPU()*10 {
cfg.GoMaxProcs = runtime.NumCPU() * 10
}

// Set the configured value on the runtime.
runtime.GOMAXPROCS(cfg.GoMaxProcs)

// Clamp GOGC between 1 and 1000
if cfg.GoGC < 1 {
cfg.GoGC = 1
}
if cfg.GoGC > 1000 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW this can already be set as an env variable:

btcd/btcd.go

Lines 331 to 338 in 718d268

// If GOGC is not explicitly set, override GC percent.
if os.Getenv("GOGC") == "" {
// Block and transaction processing can cause bursty allocations. This
// limits the garbage collector from excessively overallocating during
// bursts. This value was arrived at with the help of profiling live
// usage.
debug.SetGCPercent(10)
}

cfg.GoGC = 1000
}

// Set the configured value on the runtime.
debug.SetGCPercent(cfg.GoGC)

// Warn about missing config file only after all other configuration is
// done. This prevents the warning on help messages and invalid
// options. Note this should go directly before the return.
Expand Down
10 changes: 10 additions & 0 deletions sample-btcd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,13 @@
; be disabled if this option is not specified. The profile information can be
; accessed at http://localhost:<profileport>/debug/pprof once running.
; profile=6061

; This sets the value of GOGC - by default set to 300, which increases memory
; utilization slightly but dramatically improves performance.
; gogc = 300

; GOMAXPROCS value is by default set to the same as the number of CPU threads,
; however, goroutines are much lighter than threads and the default value will
; be set to 3 times the CPU threads from runtime.NumCPU(). Thus 24 would be a
; typical value.
; gomaxprocs = 24