Collect ctx message can let the lock be very heavy, let's avoid it

This commit is contained in:
q 2026-01-08 00:35:43 +08:00
parent 957a3602d9
commit 7162125b1f

View file

@ -155,23 +155,29 @@ func (stat *generateStats) finishContract(account common.Hash, done uint64) {
// report prints the cumulative progress statistic smartly. // report prints the cumulative progress statistic smartly.
func (stat *generateStats) report() { func (stat *generateStats) report() {
stat.lock.RLock() stat.lock.RLock()
defer stat.lock.RUnlock() accounts := stat.accounts
slots := stat.slots
start := stat.start
head := stat.head
slotsHead := stat.slotsHead
slotsStart := stat.slotsStart
stat.lock.RUnlock()
ctx := []interface{}{ ctx := []interface{}{
"accounts", stat.accounts, "accounts", accounts,
"slots", stat.slots, "slots", slots,
"elapsed", common.PrettyDuration(time.Since(stat.start)), "elapsed", common.PrettyDuration(time.Since(start)),
} }
if stat.accounts > 0 { if accounts > 0 {
// If there's progress on the account trie, estimate the time to finish crawling it // If there's progress on the account trie, estimate the time to finish crawling it
if done := binary.BigEndian.Uint64(stat.head[:8]) / stat.accounts; done > 0 { if done := binary.BigEndian.Uint64(head[:8]) / accounts; done > 0 {
var ( var (
left = (math.MaxUint64 - binary.BigEndian.Uint64(stat.head[:8])) / stat.accounts left = (math.MaxUint64 - binary.BigEndian.Uint64(head[:8])) / accounts
eta = common.CalculateETA(done, left, time.Since(stat.start)) eta = common.CalculateETA(done, left, time.Since(start))
) )
// If there are large contract crawls in progress, estimate their finish time // If there are large contract crawls in progress, estimate their finish time
for acc, head := range stat.slotsHead { for acc, head := range slotsHead {
start := stat.slotsStart[acc] start := slotsStart[acc]
if done := binary.BigEndian.Uint64(head[:8]); done > 0 { if done := binary.BigEndian.Uint64(head[:8]); done > 0 {
left := math.MaxUint64 - binary.BigEndian.Uint64(head[:8]) left := math.MaxUint64 - binary.BigEndian.Uint64(head[:8])
@ -192,14 +198,17 @@ func (stat *generateStats) report() {
// reportDone prints the last log when the whole generation is finished. // reportDone prints the last log when the whole generation is finished.
func (stat *generateStats) reportDone() { func (stat *generateStats) reportDone() {
stat.lock.RLock() stat.lock.RLock()
defer stat.lock.RUnlock() accounts := stat.accounts
slots := stat.slots
start := stat.start
stat.lock.RUnlock()
var ctx []interface{} var ctx []interface{}
ctx = append(ctx, []interface{}{"accounts", stat.accounts}...) ctx = append(ctx, []interface{}{"accounts", accounts}...)
if stat.slots != 0 { if slots != 0 {
ctx = append(ctx, []interface{}{"slots", stat.slots}...) ctx = append(ctx, []interface{}{"slots", slots}...)
} }
ctx = append(ctx, []interface{}{"elapsed", common.PrettyDuration(time.Since(stat.start))}...) ctx = append(ctx, []interface{}{"elapsed", common.PrettyDuration(time.Since(start))}...)
log.Info("Iterated snapshot", ctx...) log.Info("Iterated snapshot", ctx...)
} }