Skip to content

gc optimize #1151

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

Open
wants to merge 2 commits into
base: develop
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
22 changes: 0 additions & 22 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ import (
"os"
"path/filepath"
"runtime"
godebug "runtime/debug"
"strconv"
"strings"
"text/tabwriter"
"text/template"
"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"
Expand Down Expand Up @@ -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)))
Copy link
Member

@colinlyguo colinlyguo Mar 20, 2025

Choose a reason for hiding this comment

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

What about just removing this dynamic setting? and keep other logic. e.g. the Lowering memory allowance on 32bit arch logic.


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)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/debug/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading