@@ -82,9 +82,6 @@ using node::SnapshotMetadata;
82
82
using node::UndoReadFromDisk;
83
83
using node::UnlinkPrunedFiles;
84
84
85
- #define MICRO 0.000001
86
- #define MILLI 0.001
87
-
88
85
/* * Maximum kilobytes for transactions to store for processing during reorg */
89
86
static const unsigned int MAX_DISCONNECTED_TX_POOL_SIZE = 20000 ;
90
87
/* * Time to wait between writing blocks/block index to disk. */
@@ -1966,14 +1963,14 @@ static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const Ch
1966
1963
}
1967
1964
1968
1965
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 ;
1977
1974
1978
1975
/* * Apply the effects of this block (with given index) on the UTXO set represented by coins.
1979
1976
* Validity checks that depend on the UTXO set are also done; ConnectBlock()
@@ -1987,7 +1984,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
1987
1984
uint256 block_hash{block.GetHash ()};
1988
1985
assert (*pindex->phashBlock == block_hash);
1989
1986
1990
- int64_t nTimeStart = GetTimeMicros () ;
1987
+ const auto time_start{ SteadyClock::now ()} ;
1991
1988
1992
1989
// Check it again in case a previous version let a bad block in
1993
1990
// NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
@@ -2016,7 +2013,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2016
2013
uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256 () : pindex->pprev ->GetBlockHash ();
2017
2014
assert (hashPrevBlock == view.GetBestBlock ());
2018
2015
2019
- nBlocksTotal ++;
2016
+ num_blocks_total ++;
2020
2017
2021
2018
// Special case for the genesis block, skipping connection of its transactions
2022
2019
// (its coinbase is unspendable)
@@ -2057,8 +2054,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2057
2054
}
2058
2055
}
2059
2056
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);
2062
2063
2063
2064
// Do not allow blocks that contain transactions which 'overwrite' older transactions,
2064
2065
// unless those are already completely spent.
@@ -2156,8 +2157,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2156
2157
// Get the script flags for this block
2157
2158
unsigned int flags{GetBlockScriptFlags (*pindex, m_chainman)};
2158
2159
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);
2161
2166
2162
2167
CBlockUndo blockundo;
2163
2168
@@ -2241,8 +2246,13 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2241
2246
}
2242
2247
UpdateCoins (tx, view, i == 0 ? undoDummy : blockundo.vtxundo .back (), pindex->nHeight );
2243
2248
}
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);
2246
2256
2247
2257
CAmount blockReward = nFees + GetBlockSubsidy (pindex->nHeight , m_params.GetConsensus ());
2248
2258
if (block.vtx [0 ]->GetValueOut () > blockReward) {
@@ -2254,8 +2264,13 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2254
2264
LogPrintf (" ERROR: %s: CheckQueue failed\n " , __func__);
2255
2265
return state.Invalid (BlockValidationResult::BLOCK_CONSENSUS, " block-validation-failed" );
2256
2266
}
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);
2259
2274
2260
2275
if (fJustCheck )
2261
2276
return true ;
@@ -2264,8 +2279,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2264
2279
return false ;
2265
2280
}
2266
2281
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);
2269
2288
2270
2289
if (!pindex->IsValid (BLOCK_VALID_SCRIPTS)) {
2271
2290
pindex->RaiseValidity (BLOCK_VALID_SCRIPTS);
@@ -2275,16 +2294,20 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2275
2294
// add this block to the view's block chain
2276
2295
view.SetBestBlock (pindex->GetBlockHash ());
2277
2296
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);
2280
2303
2281
2304
TRACE6 (validation, block_connected,
2282
2305
block_hash.data (),
2283
2306
pindex->nHeight ,
2284
2307
block.vtx .size (),
2285
2308
nInputs,
2286
2309
nSigOpsCost,
2287
- nTime5 - nTimeStart // in microseconds (µs)
2310
+ time_5 - time_start // in microseconds (µs)
2288
2311
);
2289
2312
2290
2313
return true ;
@@ -2591,7 +2614,7 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
2591
2614
return error (" DisconnectTip(): Failed to read block" );
2592
2615
}
2593
2616
// Apply the block atomically to the chain state.
2594
- int64_t nStart = GetTimeMicros () ;
2617
+ const auto time_start{ SteadyClock::now ()} ;
2595
2618
{
2596
2619
CCoinsViewCache view (&CoinsTip ());
2597
2620
assert (view.GetBestBlock () == pindexDelete->GetBlockHash ());
@@ -2600,7 +2623,8 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
2600
2623
bool flushed = view.Flush ();
2601
2624
assert (flushed);
2602
2625
}
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));
2604
2628
2605
2629
{
2606
2630
// 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
2640
2664
return true ;
2641
2665
}
2642
2666
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{} ;
2648
2672
2649
2673
struct PerBlockConnectTrace {
2650
2674
CBlockIndex* pindex = nullptr ;
@@ -2699,7 +2723,7 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
2699
2723
2700
2724
assert (pindexNew->pprev == m_chain.Tip ());
2701
2725
// Read block from disk.
2702
- int64_t nTime1 = GetTimeMicros () ;
2726
+ const auto time_1{ SteadyClock::now ()} ;
2703
2727
std::shared_ptr<const CBlock> pthisBlock;
2704
2728
if (!pblock) {
2705
2729
std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
@@ -2713,9 +2737,13 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
2713
2737
}
2714
2738
const CBlock& blockConnecting = *pthisBlock;
2715
2739
// 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);
2719
2747
{
2720
2748
CCoinsViewCache view (&CoinsTip ());
2721
2749
bool rv = ConnectBlock (blockConnecting, state, pindexNew, view);
@@ -2725,20 +2753,32 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
2725
2753
InvalidBlockFound (pindexNew, state);
2726
2754
return error (" %s: ConnectBlock %s failed, %s" , __func__, pindexNew->GetBlockHash ().ToString (), state.ToString ());
2727
2755
}
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);
2731
2763
bool flushed = view.Flush ();
2732
2764
assert (flushed);
2733
2765
}
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);
2736
2772
// Write the chain state to disk, if necessary.
2737
2773
if (!FlushStateToDisk (state, FlushStateMode::IF_NEEDED)) {
2738
2774
return false ;
2739
2775
}
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);
2742
2782
// Remove conflicting transactions from the mempool.;
2743
2783
if (m_mempool) {
2744
2784
m_mempool->removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
@@ -2748,9 +2788,17 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
2748
2788
m_chain.SetTip (*pindexNew);
2749
2789
UpdateTip (pindexNew);
2750
2790
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);
2754
2802
2755
2803
connectTrace.BlockConnected (pindexNew, std::move (pthisBlock));
2756
2804
return true ;
0 commit comments