Skip to content
Closed
4 changes: 4 additions & 0 deletions execution/execmodule/exec_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/c2h5oh/datasize"
Expand Down Expand Up @@ -208,6 +209,9 @@ func (c *CacheView) GetAsOf(key []byte, ts uint64) (v []byte, ok bool, err error

type ExecModule struct {
backgroundCtx context.Context

idleAfterCommitBusy atomic.Bool
gcCyclesAtLastCommit uint64
// Snapshots + MDBX
blockReader dbservices.FullBlockReader

Expand Down
1 change: 1 addition & 0 deletions execution/execmodule/forkchoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ func (e *ExecModule) updateForkChoice(ctx context.Context, originalBlockHash, sa
}
commitTimings = append(commitTimings, pruneTimings...)
}
e.startIdleAfterCommit()

e.logTimings("Timings: Forkchoice", commitTimings)
e.emitBlockMetrics(blockHash, e.forkValidator.GetTimings(blockHash), persist, headNum, finishProgressBefore, mergeExtendingFork)
Expand Down
64 changes: 64 additions & 0 deletions execution/execmodule/idle_after_commit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2026 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

package execmodule

import (
"runtime"
"runtime/metrics"

"github.com/erigontech/erigon/diagnostics/mem"
)

const (
minTriggerRatioNum = 45
triggerRatioDen = 64
)

func (e *ExecModule) startIdleAfterCommit() {
if !e.idleAfterCommitBusy.CompareAndSwap(false, true) {
return
}
go func() {
defer e.idleAfterCommitBusy.Store(false)
e.gcIfDue()
mem.RefreshVirtualMemStats(mem.VirtualMemStatsAge)
}()
}

func (e *ExecModule) gcIfDue() {
s := []metrics.Sample{
{Name: "/gc/cycles/total:gc-cycles"},
{Name: "/gc/heap/live:bytes"},
{Name: "/gc/heap/goal:bytes"},
{Name: "/memory/classes/heap/objects:bytes"},
}
metrics.Read(s)
cycles, live, goal, heap := s[0].Value.Uint64(), s[1].Value.Uint64(), s[2].Value.Uint64(), s[3].Value.Uint64()
ranSinceLastCommit := cycles != e.gcCyclesAtLastCommit
e.gcCyclesAtLastCommit = cycles
if gcDue(heap, live, goal, ranSinceLastCommit) {
runtime.GC()
}
}

func gcDue(heap, live, goal uint64, ranSinceLastCommit bool) bool {
if ranSinceLastCommit || goal <= live {
return false
}
runway := goal - live
return heap >= live+runway/2 && heap < live+runway/triggerRatioDen*minTriggerRatioNum
}
37 changes: 37 additions & 0 deletions execution/execmodule/idle_after_commit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2026 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

package execmodule

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestGCDueBetweenHalfRunwayAndTheEarliestPacerTrigger(t *testing.T) {
const gib = uint64(1) << 30
live, goal := 10*gib, 20*gib
half := live + (goal-live)/2
earliestTrigger := live + (goal-live)/64*45

require.False(t, gcDue(half-1, live, goal, false), "less than half the runway used")
require.True(t, gcDue(half, live, goal, false), "half the runway used")
require.True(t, gcDue(earliestTrigger-1, live, goal, false), "just below the earliest pacer trigger")
require.False(t, gcDue(earliestTrigger, live, goal, false), "a natural cycle may already run, and runtime.GC would add a second one")
require.False(t, gcDue(half, live, goal, true), "a cycle finished since the last commit")
require.False(t, gcDue(goal, live, live, false), "goal not above live: the runtime already collects without a break")
}
Loading