@@ -82,9 +82,6 @@ using node::SnapshotMetadata;
8282using node::UndoReadFromDisk;
8383using node::UnlinkPrunedFiles;
8484
85- #define MICRO 0.000001
86- #define MILLI 0.001
87-
8885/* * Maximum kilobytes for transactions to store for processing during reorg */
8986static const unsigned int MAX_DISCONNECTED_TX_POOL_SIZE = 20000 ;
9087/* * Time to wait between writing blocks/block index to disk. */
@@ -1966,14 +1963,14 @@ static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const Ch
19661963}
19671964
19681965
1969- static int64_t nTimeCheck = 0 ;
1970- static int64_t nTimeForks = 0 ;
1971- static int64_t nTimeConnect = 0 ;
1972- static int64_t nTimeVerify = 0 ;
1973- static int64_t nTimeUndo = 0 ;
1974- static int64_t nTimeIndex = 0 ;
1975- static int64_t nTimeTotal = 0 ;
1976- static int64_t nBlocksTotal = 0 ;
1966+ static SteadyClock::duration time_check{} ;
1967+ static SteadyClock::duration time_forks{} ;
1968+ static SteadyClock::duration time_connect{} ;
1969+ static SteadyClock::duration time_verify{} ;
1970+ static SteadyClock::duration time_undo{} ;
1971+ static SteadyClock::duration time_index{} ;
1972+ static SteadyClock::duration time_total{} ;
1973+ static int64_t num_blocks_total = 0 ;
19771974
19781975/* * Apply the effects of this block (with given index) on the UTXO set represented by coins.
19791976 * Validity checks that depend on the UTXO set are also done; ConnectBlock()
@@ -1987,7 +1984,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
19871984 uint256 block_hash{block.GetHash ()};
19881985 assert (*pindex->phashBlock == block_hash);
19891986
1990- int64_t nTimeStart = GetTimeMicros () ;
1987+ const auto time_start{ SteadyClock::now ()} ;
19911988
19921989 // Check it again in case a previous version let a bad block in
19931990 // NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
@@ -2016,7 +2013,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
20162013 uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256 () : pindex->pprev ->GetBlockHash ();
20172014 assert (hashPrevBlock == view.GetBestBlock ());
20182015
2019- nBlocksTotal ++;
2016+ num_blocks_total ++;
20202017
20212018 // Special case for the genesis block, skipping connection of its transactions
20222019 // (its coinbase is unspendable)
@@ -2057,8 +2054,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
20572054 }
20582055 }
20592056
2060- int64_t nTime1 = GetTimeMicros (); nTimeCheck += nTime1 - nTimeStart;
2061- LogPrint (BCLog::BENCH, " - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n " , MILLI * (nTime1 - nTimeStart), nTimeCheck * MICRO, nTimeCheck * MILLI / nBlocksTotal);
2057+ const auto time_1{SteadyClock::now ()};
2058+ time_check += time_1 - time_start;
2059+ LogPrint (BCLog::BENCH, " - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2060+ Ticks<MillisecondsDouble>(time_1 - time_start),
2061+ Ticks<SecondsDouble>(time_check),
2062+ Ticks<MillisecondsDouble>(time_check) / num_blocks_total);
20622063
20632064 // Do not allow blocks that contain transactions which 'overwrite' older transactions,
20642065 // unless those are already completely spent.
@@ -2156,8 +2157,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
21562157 // Get the script flags for this block
21572158 unsigned int flags{GetBlockScriptFlags (*pindex, m_chainman)};
21582159
2159- int64_t nTime2 = GetTimeMicros (); nTimeForks += nTime2 - nTime1;
2160- LogPrint (BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n " , MILLI * (nTime2 - nTime1), nTimeForks * MICRO, nTimeForks * MILLI / nBlocksTotal);
2160+ const auto time_2{SteadyClock::now ()};
2161+ time_forks += time_2 - time_1;
2162+ LogPrint (BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2163+ Ticks<MillisecondsDouble>(time_2 - time_1),
2164+ Ticks<SecondsDouble>(time_forks),
2165+ Ticks<MillisecondsDouble>(time_forks) / num_blocks_total);
21612166
21622167 CBlockUndo blockundo;
21632168
@@ -2241,8 +2246,13 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
22412246 }
22422247 UpdateCoins (tx, view, i == 0 ? undoDummy : blockundo.vtxundo .back (), pindex->nHeight );
22432248 }
2244- int64_t nTime3 = GetTimeMicros (); nTimeConnect += nTime3 - nTime2;
2245- LogPrint (BCLog::BENCH, " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n " , (unsigned )block.vtx .size (), MILLI * (nTime3 - nTime2), MILLI * (nTime3 - nTime2) / block.vtx .size (), nInputs <= 1 ? 0 : MILLI * (nTime3 - nTime2) / (nInputs-1 ), nTimeConnect * MICRO, nTimeConnect * MILLI / nBlocksTotal);
2249+ const auto time_3{SteadyClock::now ()};
2250+ time_connect += time_3 - time_2;
2251+ LogPrint (BCLog::BENCH, " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n " , (unsigned )block.vtx .size (),
2252+ Ticks<MillisecondsDouble>(time_3 - time_2), Ticks<MillisecondsDouble>(time_3 - time_2) / block.vtx .size (),
2253+ nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_3 - time_2) / (nInputs - 1 ),
2254+ Ticks<SecondsDouble>(time_connect),
2255+ Ticks<MillisecondsDouble>(time_connect) / num_blocks_total);
22462256
22472257 CAmount blockReward = nFees + GetBlockSubsidy (pindex->nHeight , m_params.GetConsensus ());
22482258 if (block.vtx [0 ]->GetValueOut () > blockReward) {
@@ -2254,8 +2264,13 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
22542264 LogPrintf (" ERROR: %s: CheckQueue failed\n " , __func__);
22552265 return state.Invalid (BlockValidationResult::BLOCK_CONSENSUS, " block-validation-failed" );
22562266 }
2257- int64_t nTime4 = GetTimeMicros (); nTimeVerify += nTime4 - nTime2;
2258- LogPrint (BCLog::BENCH, " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n " , nInputs - 1 , MILLI * (nTime4 - nTime2), nInputs <= 1 ? 0 : MILLI * (nTime4 - nTime2) / (nInputs-1 ), nTimeVerify * MICRO, nTimeVerify * MILLI / nBlocksTotal);
2267+ const auto time_4{SteadyClock::now ()};
2268+ time_verify += time_4 - time_2;
2269+ LogPrint (BCLog::BENCH, " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n " , nInputs - 1 ,
2270+ Ticks<MillisecondsDouble>(time_4 - time_2),
2271+ nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_4 - time_2) / (nInputs - 1 ),
2272+ Ticks<SecondsDouble>(time_verify),
2273+ Ticks<MillisecondsDouble>(time_verify) / num_blocks_total);
22592274
22602275 if (fJustCheck )
22612276 return true ;
@@ -2264,8 +2279,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
22642279 return false ;
22652280 }
22662281
2267- int64_t nTime5 = GetTimeMicros (); nTimeUndo += nTime5 - nTime4;
2268- LogPrint (BCLog::BENCH, " - Write undo data: %.2fms [%.2fs (%.2fms/blk)]\n " , MILLI * (nTime5 - nTime4), nTimeUndo * MICRO, nTimeUndo * MILLI / nBlocksTotal);
2282+ const auto time_5{SteadyClock::now ()};
2283+ time_undo += time_5 - time_4;
2284+ LogPrint (BCLog::BENCH, " - Write undo data: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2285+ Ticks<MillisecondsDouble>(time_5 - time_4),
2286+ Ticks<SecondsDouble>(time_undo),
2287+ Ticks<MillisecondsDouble>(time_undo) / num_blocks_total);
22692288
22702289 if (!pindex->IsValid (BLOCK_VALID_SCRIPTS)) {
22712290 pindex->RaiseValidity (BLOCK_VALID_SCRIPTS);
@@ -2275,16 +2294,20 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
22752294 // add this block to the view's block chain
22762295 view.SetBestBlock (pindex->GetBlockHash ());
22772296
2278- int64_t nTime6 = GetTimeMicros (); nTimeIndex += nTime6 - nTime5;
2279- LogPrint (BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n " , MILLI * (nTime6 - nTime5), nTimeIndex * MICRO, nTimeIndex * MILLI / nBlocksTotal);
2297+ const auto time_6{SteadyClock::now ()};
2298+ time_index += time_6 - time_5;
2299+ LogPrint (BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2300+ Ticks<MillisecondsDouble>(time_6 - time_5),
2301+ Ticks<SecondsDouble>(time_index),
2302+ Ticks<MillisecondsDouble>(time_index) / num_blocks_total);
22802303
22812304 TRACE6 (validation, block_connected,
22822305 block_hash.data (),
22832306 pindex->nHeight ,
22842307 block.vtx .size (),
22852308 nInputs,
22862309 nSigOpsCost,
2287- nTime5 - nTimeStart // in microseconds (µs)
2310+ time_5 - time_start // in microseconds (µs)
22882311 );
22892312
22902313 return true ;
@@ -2591,7 +2614,7 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
25912614 return error (" DisconnectTip(): Failed to read block" );
25922615 }
25932616 // Apply the block atomically to the chain state.
2594- int64_t nStart = GetTimeMicros () ;
2617+ const auto time_start{ SteadyClock::now ()} ;
25952618 {
25962619 CCoinsViewCache view (&CoinsTip ());
25972620 assert (view.GetBestBlock () == pindexDelete->GetBlockHash ());
@@ -2600,7 +2623,8 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
26002623 bool flushed = view.Flush ();
26012624 assert (flushed);
26022625 }
2603- LogPrint (BCLog::BENCH, " - Disconnect block: %.2fms\n " , (GetTimeMicros () - nStart) * MILLI);
2626+ LogPrint (BCLog::BENCH, " - Disconnect block: %.2fms\n " ,
2627+ Ticks<MillisecondsDouble>(SteadyClock::now () - time_start));
26042628
26052629 {
26062630 // Prune locks that began at or after the tip should be moved backward so they get a chance to reorg
@@ -2640,11 +2664,11 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
26402664 return true ;
26412665}
26422666
2643- static int64_t nTimeReadFromDiskTotal = 0 ;
2644- static int64_t nTimeConnectTotal = 0 ;
2645- static int64_t nTimeFlush = 0 ;
2646- static int64_t nTimeChainState = 0 ;
2647- static int64_t nTimePostConnect = 0 ;
2667+ static SteadyClock::duration time_read_from_disk_total{} ;
2668+ static SteadyClock::duration time_connect_total{} ;
2669+ static SteadyClock::duration time_flush{} ;
2670+ static SteadyClock::duration time_chainstate{} ;
2671+ static SteadyClock::duration time_post_connect{} ;
26482672
26492673struct PerBlockConnectTrace {
26502674 CBlockIndex* pindex = nullptr ;
@@ -2699,7 +2723,7 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
26992723
27002724 assert (pindexNew->pprev == m_chain.Tip ());
27012725 // Read block from disk.
2702- int64_t nTime1 = GetTimeMicros () ;
2726+ const auto time_1{ SteadyClock::now ()} ;
27032727 std::shared_ptr<const CBlock> pthisBlock;
27042728 if (!pblock) {
27052729 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
@@ -2713,9 +2737,13 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
27132737 }
27142738 const CBlock& blockConnecting = *pthisBlock;
27152739 // Apply the block atomically to the chain state.
2716- int64_t nTime2 = GetTimeMicros (); nTimeReadFromDiskTotal += nTime2 - nTime1;
2717- int64_t nTime3;
2718- LogPrint (BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime2 - nTime1) * MILLI, nTimeReadFromDiskTotal * MICRO, nTimeReadFromDiskTotal * MILLI / nBlocksTotal);
2740+ const auto time_2{SteadyClock::now ()};
2741+ time_read_from_disk_total += time_2 - time_1;
2742+ SteadyClock::time_point time_3;
2743+ LogPrint (BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2744+ Ticks<MillisecondsDouble>(time_2 - time_1),
2745+ Ticks<SecondsDouble>(time_read_from_disk_total),
2746+ Ticks<MillisecondsDouble>(time_read_from_disk_total) / num_blocks_total);
27192747 {
27202748 CCoinsViewCache view (&CoinsTip ());
27212749 bool rv = ConnectBlock (blockConnecting, state, pindexNew, view);
@@ -2725,20 +2753,32 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
27252753 InvalidBlockFound (pindexNew, state);
27262754 return error (" %s: ConnectBlock %s failed, %s" , __func__, pindexNew->GetBlockHash ().ToString (), state.ToString ());
27272755 }
2728- nTime3 = GetTimeMicros (); nTimeConnectTotal += nTime3 - nTime2;
2729- assert (nBlocksTotal > 0 );
2730- LogPrint (BCLog::BENCH, " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime3 - nTime2) * MILLI, nTimeConnectTotal * MICRO, nTimeConnectTotal * MILLI / nBlocksTotal);
2756+ time_3 = SteadyClock::now ();
2757+ time_connect_total += time_3 - time_2;
2758+ assert (num_blocks_total > 0 );
2759+ LogPrint (BCLog::BENCH, " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2760+ Ticks<MillisecondsDouble>(time_3 - time_2),
2761+ Ticks<SecondsDouble>(time_connect_total),
2762+ Ticks<MillisecondsDouble>(time_connect_total) / num_blocks_total);
27312763 bool flushed = view.Flush ();
27322764 assert (flushed);
27332765 }
2734- int64_t nTime4 = GetTimeMicros (); nTimeFlush += nTime4 - nTime3;
2735- LogPrint (BCLog::BENCH, " - Flush: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime4 - nTime3) * MILLI, nTimeFlush * MICRO, nTimeFlush * MILLI / nBlocksTotal);
2766+ const auto time_4{SteadyClock::now ()};
2767+ time_flush += time_4 - time_3;
2768+ LogPrint (BCLog::BENCH, " - Flush: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2769+ Ticks<MillisecondsDouble>(time_4 - time_3),
2770+ Ticks<SecondsDouble>(time_flush),
2771+ Ticks<MillisecondsDouble>(time_flush) / num_blocks_total);
27362772 // Write the chain state to disk, if necessary.
27372773 if (!FlushStateToDisk (state, FlushStateMode::IF_NEEDED)) {
27382774 return false ;
27392775 }
2740- int64_t nTime5 = GetTimeMicros (); nTimeChainState += nTime5 - nTime4;
2741- LogPrint (BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, nTimeChainState * MILLI / nBlocksTotal);
2776+ const auto time_5{SteadyClock::now ()};
2777+ time_chainstate += time_5 - time_4;
2778+ LogPrint (BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2779+ Ticks<MillisecondsDouble>(time_5 - time_4),
2780+ Ticks<SecondsDouble>(time_chainstate),
2781+ Ticks<MillisecondsDouble>(time_chainstate) / num_blocks_total);
27422782 // Remove conflicting transactions from the mempool.;
27432783 if (m_mempool) {
27442784 m_mempool->removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
@@ -2748,9 +2788,17 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
27482788 m_chain.SetTip (*pindexNew);
27492789 UpdateTip (pindexNew);
27502790
2751- int64_t nTime6 = GetTimeMicros (); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
2752- LogPrint (BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO, nTimePostConnect * MILLI / nBlocksTotal);
2753- LogPrint (BCLog::BENCH, " - Connect block: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime6 - nTime1) * MILLI, nTimeTotal * MICRO, nTimeTotal * MILLI / nBlocksTotal);
2791+ const auto time_6{SteadyClock::now ()};
2792+ time_post_connect += time_6 - time_5;
2793+ time_total += time_6 - time_1;
2794+ LogPrint (BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2795+ Ticks<MillisecondsDouble>(time_6 - time_5),
2796+ Ticks<SecondsDouble>(time_post_connect),
2797+ Ticks<MillisecondsDouble>(time_post_connect) / num_blocks_total);
2798+ LogPrint (BCLog::BENCH, " - Connect block: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2799+ Ticks<MillisecondsDouble>(time_6 - time_1),
2800+ Ticks<SecondsDouble>(time_total),
2801+ Ticks<MillisecondsDouble>(time_total) / num_blocks_total);
27542802
27552803 connectTrace.BlockConnected (pindexNew, std::move (pthisBlock));
27562804 return true ;
0 commit comments