Skip to content

Commit 843073d

Browse files
fjlobscuren
authored andcommitted
[release/1.4.18] core: print import stats more often
If geth is busy importing 2048 heavy blocks it can take a while before it prints anything. This change ensures that a message gets printed every 8s. (cherry picked from commit e66b158)
1 parent 33a6e52 commit 843073d

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

core/blockchain.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -824,19 +824,16 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
824824
// faster than direct delivery and requires much less mutex
825825
// acquiring.
826826
var (
827-
stats struct{ queued, processed, ignored int }
827+
stats = insertStats{startTime: time.Now()}
828828
events = make([]interface{}, 0, len(chain))
829829
coalescedLogs vm.Logs
830-
tstart = time.Now()
831-
832-
nonceChecked = make([]bool, len(chain))
830+
nonceChecked = make([]bool, len(chain))
833831
)
834832

835833
// Start the parallel nonce verifier.
836834
nonceAbort, nonceResults := verifyNoncesFromBlocks(self.pow, chain)
837835
defer close(nonceAbort)
838836

839-
txcount := 0
840837
for i, block := range chain {
841838
if atomic.LoadInt32(&self.procInterrupt) == 1 {
842839
glog.V(logger.Debug).Infoln("Premature abort during block chain processing")
@@ -931,7 +928,6 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
931928
return i, err
932929
}
933930

934-
txcount += len(block.Transactions())
935931
// write the block to the chain and get the status
936932
status, err := self.WriteBlock(block)
937933
if err != nil {
@@ -966,19 +962,54 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
966962
case SplitStatTy:
967963
events = append(events, ChainSplitEvent{block, logs})
968964
}
965+
969966
stats.processed++
967+
if glog.V(logger.Info) {
968+
stats.report(chain, i)
969+
}
970970
}
971971

972-
if (stats.queued > 0 || stats.processed > 0 || stats.ignored > 0) && bool(glog.V(logger.Info)) {
973-
tend := time.Since(tstart)
974-
start, end := chain[0], chain[len(chain)-1]
975-
glog.Infof("imported %d block(s) (%d queued %d ignored) including %d txs in %v. #%v [%x / %x]\n", stats.processed, stats.queued, stats.ignored, txcount, tend, end.Number(), start.Hash().Bytes()[:4], end.Hash().Bytes()[:4])
976-
}
977972
go self.postChainEvents(events, coalescedLogs)
978973

979974
return 0, nil
980975
}
981976

977+
// insertStats tracks and reports on block insertion.
978+
type insertStats struct {
979+
queued, processed, ignored int
980+
lastIndex int
981+
startTime time.Time
982+
}
983+
984+
const (
985+
statsReportLimit = 1024
986+
statsReportTimeLimit = 8 * time.Second
987+
)
988+
989+
// report prints statistics if some number of blocks have been processed
990+
// or more than a few seconds have passed since the last message.
991+
func (st *insertStats) report(chain []*types.Block, index int) {
992+
limit := statsReportLimit
993+
if index == len(chain)-1 {
994+
limit = 0 // Always print a message for the last block.
995+
}
996+
now := time.Now()
997+
duration := now.Sub(st.startTime)
998+
if duration > statsReportTimeLimit || st.queued > limit || st.processed > limit || st.ignored > limit {
999+
start, end := chain[st.lastIndex], chain[index]
1000+
txcount := countTransactions(chain[st.lastIndex:index])
1001+
glog.Infof("imported %d block(s) (%d queued %d ignored) including %d txs in %v. #%v [%x / %x]\n", st.processed, st.queued, st.ignored, txcount, duration, end.Number(), start.Hash().Bytes()[:4], end.Hash().Bytes()[:4])
1002+
*st = insertStats{startTime: now, lastIndex: index}
1003+
}
1004+
}
1005+
1006+
func countTransactions(chain []*types.Block) (c int) {
1007+
for _, b := range chain {
1008+
c += len(b.Transactions())
1009+
}
1010+
return c
1011+
}
1012+
9821013
// reorgs takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them
9831014
// to be part of the new canonical chain and accumulates potential missing transactions and post an
9841015
// event about them

0 commit comments

Comments
 (0)