From cd7cc0c13ced2eb9803bb7a523ec769a8bd10096 Mon Sep 17 00:00:00 2001 From: georgehao Date: Thu, 20 Mar 2025 15:10:39 +0800 Subject: [PATCH 1/2] optimize gc --- cmd/utils/flags.go | 22 ---------------------- internal/debug/api.go | 5 +++++ internal/web3ext/web3ext.go | 5 +++++ 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 8d8bdf6773fc..9afa4c3c777d 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -28,7 +28,6 @@ import ( "os" "path/filepath" "runtime" - godebug "runtime/debug" "strconv" "strings" "text/tabwriter" @@ -36,7 +35,6 @@ import ( "time" pcsclite "github.com/gballet/go-libpcsclite" - gopsutil "github.com/shirou/gopsutil/mem" "gopkg.in/urfave/cli.v1" "github.com/scroll-tech/go-ethereum/accounts" @@ -1784,26 +1782,6 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { log.Info("Shadow fork peers", "ids", cfg.ShadowForkPeerIDs) } - // Cap the cache allowance and tune the garbage collector - mem, err := gopsutil.VirtualMemory() - if err == nil { - if 32<<(^uintptr(0)>>63) == 32 && mem.Total > 2*1024*1024*1024 { - log.Warn("Lowering memory allowance on 32bit arch", "available", mem.Total/1024/1024, "addressable", 2*1024) - mem.Total = 2 * 1024 * 1024 * 1024 - } - allowance := int(mem.Total / 1024 / 1024 / 3) - if cache := ctx.GlobalInt(CacheFlag.Name); cache > allowance { - log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance) - ctx.GlobalSet(CacheFlag.Name, strconv.Itoa(allowance)) - } - } - // Ensure Go's GC ignores the database cache for trigger percentage - cache := ctx.GlobalInt(CacheFlag.Name) - gogc := math.Max(20, math.Min(100, 100/(float64(cache)/1024))) - - log.Debug("Sanitizing Go's GC trigger", "percent", int(gogc)) - godebug.SetGCPercent(int(gogc)) - if ctx.GlobalIsSet(SyncModeFlag.Name) { cfg.SyncMode = *GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode) } diff --git a/internal/debug/api.go b/internal/debug/api.go index f5bd7e8d2cb8..5bbb65acb5ca 100644 --- a/internal/debug/api.go +++ b/internal/debug/api.go @@ -244,6 +244,11 @@ func (*HandlerT) SetGCPercent(v int) int { return debug.SetGCPercent(v) } +// SetMemoryLimit sets the GOMEMLIMIT for the process. It returns the previous limit. +func (*HandlerT) SetMemoryLimit(limit int64) int64 { + return debug.SetMemoryLimit(limit) +} + func writeProfile(name, file string) error { p := pprof.Lookup(name) log.Info("Writing profile records", "count", p.Count(), "type", name, "dump", file) diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 325510034186..09cd93dbe8af 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -308,6 +308,11 @@ web3._extend({ call: 'debug_setGCPercent', params: 1, }), + new web3._extend.Method({ + name: 'SetMemoryLimit', + call: 'debug_SetMemoryLimit', + params: 1, + }), new web3._extend.Method({ name: 'memStats', call: 'debug_memStats', From 29a9bc1d377bb5b500dd060ea79352497f9f39fc Mon Sep 17 00:00:00 2001 From: georgehao Date: Thu, 20 Mar 2025 17:46:30 +0800 Subject: [PATCH 2/2] fix typo --- internal/web3ext/web3ext.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 09cd93dbe8af..ab0448a5d031 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -309,8 +309,8 @@ web3._extend({ params: 1, }), new web3._extend.Method({ - name: 'SetMemoryLimit', - call: 'debug_SetMemoryLimit', + name: 'setMemoryLimit', + call: 'debug_setMemoryLimit', params: 1, }), new web3._extend.Method({