Skip to content

Commit 95219ae

Browse files
authored
cmd/utils: move cache sanity check to SetEthConfig (#22510)
Move the cache sanity check to the SetEthConfig function to allow the config file to load.
1 parent 5338ce4 commit 95219ae

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

cmd/geth/main.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ package main
1919

2020
import (
2121
"fmt"
22-
"math"
2322
"os"
24-
godebug "runtime/debug"
2523
"sort"
2624
"strconv"
2725
"strings"
@@ -41,7 +39,6 @@ import (
4139
"github.com/ethereum/go-ethereum/log"
4240
"github.com/ethereum/go-ethereum/metrics"
4341
"github.com/ethereum/go-ethereum/node"
44-
gopsutil "github.com/shirou/gopsutil/mem"
4542
"gopkg.in/urfave/cli.v1"
4643
)
4744

@@ -300,25 +297,6 @@ func prepare(ctx *cli.Context) {
300297
log.Info("Dropping default light client cache", "provided", ctx.GlobalInt(utils.CacheFlag.Name), "updated", 128)
301298
ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(128))
302299
}
303-
// Cap the cache allowance and tune the garbage collector
304-
mem, err := gopsutil.VirtualMemory()
305-
if err == nil {
306-
if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 {
307-
log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024)
308-
mem.Total = 2 * 1024 * 1024 * 1024
309-
}
310-
allowance := int(mem.Total / 1024 / 1024 / 3)
311-
if cache := ctx.GlobalInt(utils.CacheFlag.Name); cache > allowance {
312-
log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance)
313-
ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(allowance))
314-
}
315-
}
316-
// Ensure Go's GC ignores the database cache for trigger percentage
317-
cache := ctx.GlobalInt(utils.CacheFlag.Name)
318-
gogc := math.Max(20, math.Min(100, 100/(float64(cache)/1024)))
319-
320-
log.Debug("Sanitizing Go's GC trigger", "percent", int(gogc))
321-
godebug.SetGCPercent(int(gogc))
322300

323301
// Start metrics export if enabled
324302
utils.SetupMetrics(ctx)

cmd/utils/flags.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222
"fmt"
2323
"io"
2424
"io/ioutil"
25+
"math"
2526
"math/big"
2627
"os"
2728
"path/filepath"
29+
godebug "runtime/debug"
2830
"strconv"
2931
"strings"
3032
"text/tabwriter"
@@ -65,6 +67,7 @@ import (
6567
"github.com/ethereum/go-ethereum/p2p/netutil"
6668
"github.com/ethereum/go-ethereum/params"
6769
pcsclite "github.com/gballet/go-libpcsclite"
70+
gopsutil "github.com/shirou/gopsutil/mem"
6871
"gopkg.in/urfave/cli.v1"
6972
)
7073

@@ -1473,6 +1476,26 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
14731476
setWhitelist(ctx, cfg)
14741477
setLes(ctx, cfg)
14751478

1479+
// Cap the cache allowance and tune the garbage collector
1480+
mem, err := gopsutil.VirtualMemory()
1481+
if err == nil {
1482+
if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 {
1483+
log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024)
1484+
mem.Total = 2 * 1024 * 1024 * 1024
1485+
}
1486+
allowance := int(mem.Total / 1024 / 1024 / 3)
1487+
if cache := ctx.GlobalInt(CacheFlag.Name); cache > allowance {
1488+
log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance)
1489+
ctx.GlobalSet(CacheFlag.Name, strconv.Itoa(allowance))
1490+
}
1491+
}
1492+
// Ensure Go's GC ignores the database cache for trigger percentage
1493+
cache := ctx.GlobalInt(CacheFlag.Name)
1494+
gogc := math.Max(20, math.Min(100, 100/(float64(cache)/1024)))
1495+
1496+
log.Debug("Sanitizing Go's GC trigger", "percent", int(gogc))
1497+
godebug.SetGCPercent(int(gogc))
1498+
14761499
if ctx.GlobalIsSet(SyncModeFlag.Name) {
14771500
cfg.SyncMode = *GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode)
14781501
}

0 commit comments

Comments
 (0)