@@ -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. */
@@ -1965,13 +1962,13 @@ static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const Ch
1965
1962
}
1966
1963
1967
1964
1968
- static int64_t time_check = 0 ;
1969
- static int64_t time_forks = 0 ;
1970
- static int64_t time_connect = 0 ;
1971
- static int64_t time_verify = 0 ;
1972
- static int64_t time_undo = 0 ;
1973
- static int64_t time_index = 0 ;
1974
- static int64_t time_total = 0 ;
1965
+ static SteadyClock::duration time_check{} ;
1966
+ static SteadyClock::duration time_forks{} ;
1967
+ static SteadyClock::duration time_connect{} ;
1968
+ static SteadyClock::duration time_verify{} ;
1969
+ static SteadyClock::duration time_undo{} ;
1970
+ static SteadyClock::duration time_index{} ;
1971
+ static SteadyClock::duration time_total{} ;
1975
1972
static int64_t num_blocks_total = 0 ;
1976
1973
1977
1974
/* * Apply the effects of this block (with given index) on the UTXO set represented by coins.
@@ -1986,7 +1983,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
1986
1983
uint256 block_hash{block.GetHash ()};
1987
1984
assert (*pindex->phashBlock == block_hash);
1988
1985
1989
- int64_t time_start = GetTimeMicros () ;
1986
+ const auto time_start{ SteadyClock::now ()} ;
1990
1987
1991
1988
// Check it again in case a previous version let a bad block in
1992
1989
// NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
@@ -2056,9 +2053,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2056
2053
}
2057
2054
}
2058
2055
2059
- int64_t time_1 = GetTimeMicros () ;
2056
+ const auto time_1{ SteadyClock::now ()} ;
2060
2057
time_check += time_1 - time_start;
2061
- LogPrint (BCLog::BENCH, " - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n " , MILLI * (time_1 - time_start), time_check * MICRO, time_check * MILLI / num_blocks_total);
2058
+ LogPrint (BCLog::BENCH, " - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2059
+ Ticks<MillisecondsDouble>(time_1 - time_start),
2060
+ Ticks<SecondsDouble>(time_check),
2061
+ Ticks<MillisecondsDouble>(time_check) / num_blocks_total);
2062
2062
2063
2063
// Do not allow blocks that contain transactions which 'overwrite' older transactions,
2064
2064
// unless those are already completely spent.
@@ -2156,9 +2156,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2156
2156
// Get the script flags for this block
2157
2157
unsigned int flags{GetBlockScriptFlags (*pindex, m_chainman)};
2158
2158
2159
- int64_t time_2 = GetTimeMicros () ;
2159
+ const auto time_2{ SteadyClock::now ()} ;
2160
2160
time_forks += time_2 - time_1;
2161
- LogPrint (BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n " , MILLI * (time_2 - time_1), time_forks * MICRO, time_forks * MILLI / num_blocks_total);
2161
+ LogPrint (BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2162
+ Ticks<MillisecondsDouble>(time_2 - time_1),
2163
+ Ticks<SecondsDouble>(time_forks),
2164
+ Ticks<MillisecondsDouble>(time_forks) / num_blocks_total);
2162
2165
2163
2166
CBlockUndo blockundo;
2164
2167
@@ -2242,9 +2245,13 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2242
2245
}
2243
2246
UpdateCoins (tx, view, i == 0 ? undoDummy : blockundo.vtxundo .back (), pindex->nHeight );
2244
2247
}
2245
- int64_t time_3 = GetTimeMicros () ;
2248
+ const auto time_3{ SteadyClock::now ()} ;
2246
2249
time_connect += time_3 - time_2;
2247
- LogPrint (BCLog::BENCH, " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n " , (unsigned )block.vtx .size (), MILLI * (time_3 - time_2), MILLI * (time_3 - time_2) / block.vtx .size (), nInputs <= 1 ? 0 : MILLI * (time_3 - time_2) / (nInputs-1 ), time_connect * MICRO, time_connect * MILLI / num_blocks_total);
2250
+ LogPrint (BCLog::BENCH, " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n " , (unsigned )block.vtx .size (),
2251
+ Ticks<MillisecondsDouble>(time_3 - time_2), Ticks<MillisecondsDouble>(time_3 - time_2) / block.vtx .size (),
2252
+ nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_3 - time_2) / (nInputs - 1 ),
2253
+ Ticks<SecondsDouble>(time_connect),
2254
+ Ticks<MillisecondsDouble>(time_connect) / num_blocks_total);
2248
2255
2249
2256
CAmount blockReward = nFees + GetBlockSubsidy (pindex->nHeight , m_params.GetConsensus ());
2250
2257
if (block.vtx [0 ]->GetValueOut () > blockReward) {
@@ -2256,9 +2263,13 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2256
2263
LogPrintf (" ERROR: %s: CheckQueue failed\n " , __func__);
2257
2264
return state.Invalid (BlockValidationResult::BLOCK_CONSENSUS, " block-validation-failed" );
2258
2265
}
2259
- int64_t time_4 = GetTimeMicros () ;
2266
+ const auto time_4{ SteadyClock::now ()} ;
2260
2267
time_verify += time_4 - time_2;
2261
- LogPrint (BCLog::BENCH, " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n " , nInputs - 1 , MILLI * (time_4 - time_2), nInputs <= 1 ? 0 : MILLI * (time_4 - time_2) / (nInputs-1 ), time_verify * MICRO, time_verify * MILLI / num_blocks_total);
2268
+ LogPrint (BCLog::BENCH, " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n " , nInputs - 1 ,
2269
+ Ticks<MillisecondsDouble>(time_4 - time_2),
2270
+ nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_4 - time_2) / (nInputs - 1 ),
2271
+ Ticks<SecondsDouble>(time_verify),
2272
+ Ticks<MillisecondsDouble>(time_verify) / num_blocks_total);
2262
2273
2263
2274
if (fJustCheck )
2264
2275
return true ;
@@ -2267,9 +2278,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2267
2278
return false ;
2268
2279
}
2269
2280
2270
- int64_t time_5 = GetTimeMicros () ;
2281
+ const auto time_5{ SteadyClock::now ()} ;
2271
2282
time_undo += time_5 - time_4;
2272
- LogPrint (BCLog::BENCH, " - Write undo data: %.2fms [%.2fs (%.2fms/blk)]\n " , MILLI * (time_5 - time_4), time_undo * MICRO, time_undo * MILLI / num_blocks_total);
2283
+ LogPrint (BCLog::BENCH, " - Write undo data: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2284
+ Ticks<MillisecondsDouble>(time_5 - time_4),
2285
+ Ticks<SecondsDouble>(time_undo),
2286
+ Ticks<MillisecondsDouble>(time_undo) / num_blocks_total);
2273
2287
2274
2288
if (!pindex->IsValid (BLOCK_VALID_SCRIPTS)) {
2275
2289
pindex->RaiseValidity (BLOCK_VALID_SCRIPTS);
@@ -2279,9 +2293,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
2279
2293
// add this block to the view's block chain
2280
2294
view.SetBestBlock (pindex->GetBlockHash ());
2281
2295
2282
- int64_t time_6 = GetTimeMicros () ;
2296
+ const auto time_6{ SteadyClock::now ()} ;
2283
2297
time_index += time_6 - time_5;
2284
- LogPrint (BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n " , MILLI * (time_6 - time_5), time_index * MICRO, time_index * MILLI / num_blocks_total);
2298
+ LogPrint (BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2299
+ Ticks<MillisecondsDouble>(time_6 - time_5),
2300
+ Ticks<SecondsDouble>(time_index),
2301
+ Ticks<MillisecondsDouble>(time_index) / num_blocks_total);
2285
2302
2286
2303
TRACE6 (validation, block_connected,
2287
2304
block_hash.data (),
@@ -2596,7 +2613,7 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
2596
2613
return error (" DisconnectTip(): Failed to read block" );
2597
2614
}
2598
2615
// Apply the block atomically to the chain state.
2599
- int64_t time_start = GetTimeMicros () ;
2616
+ const auto time_start{ SteadyClock::now ()} ;
2600
2617
{
2601
2618
CCoinsViewCache view (&CoinsTip ());
2602
2619
assert (view.GetBestBlock () == pindexDelete->GetBlockHash ());
@@ -2605,7 +2622,8 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
2605
2622
bool flushed = view.Flush ();
2606
2623
assert (flushed);
2607
2624
}
2608
- LogPrint (BCLog::BENCH, " - Disconnect block: %.2fms\n " , (GetTimeMicros () - time_start) * MILLI);
2625
+ LogPrint (BCLog::BENCH, " - Disconnect block: %.2fms\n " ,
2626
+ Ticks<MillisecondsDouble>(SteadyClock::now () - time_start));
2609
2627
2610
2628
{
2611
2629
// Prune locks that began at or after the tip should be moved backward so they get a chance to reorg
@@ -2645,11 +2663,11 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
2645
2663
return true ;
2646
2664
}
2647
2665
2648
- static int64_t time_read_from_disk_total = 0 ;
2649
- static int64_t time_connect_total = 0 ;
2650
- static int64_t time_flush = 0 ;
2651
- static int64_t time_chainstate = 0 ;
2652
- static int64_t time_post_connect = 0 ;
2666
+ static SteadyClock::duration time_read_from_disk_total{} ;
2667
+ static SteadyClock::duration time_connect_total{} ;
2668
+ static SteadyClock::duration time_flush{} ;
2669
+ static SteadyClock::duration time_chainstate{} ;
2670
+ static SteadyClock::duration time_post_connect{} ;
2653
2671
2654
2672
struct PerBlockConnectTrace {
2655
2673
CBlockIndex* pindex = nullptr ;
@@ -2704,7 +2722,7 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
2704
2722
2705
2723
assert (pindexNew->pprev == m_chain.Tip ());
2706
2724
// Read block from disk.
2707
- int64_t time_1 = GetTimeMicros () ;
2725
+ const auto time_1{ SteadyClock::now ()} ;
2708
2726
std::shared_ptr<const CBlock> pthisBlock;
2709
2727
if (!pblock) {
2710
2728
std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
@@ -2718,10 +2736,13 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
2718
2736
}
2719
2737
const CBlock& blockConnecting = *pthisBlock;
2720
2738
// Apply the block atomically to the chain state.
2721
- int64_t time_2 = GetTimeMicros () ;
2739
+ const auto time_2{ SteadyClock::now ()} ;
2722
2740
time_read_from_disk_total += time_2 - time_1;
2723
- int64_t time_3;
2724
- LogPrint (BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs (%.2fms/blk)]\n " , (time_2 - time_1) * MILLI, time_read_from_disk_total * MICRO, time_read_from_disk_total * MILLI / num_blocks_total);
2741
+ SteadyClock::time_point time_3;
2742
+ LogPrint (BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2743
+ Ticks<MillisecondsDouble>(time_2 - time_1),
2744
+ Ticks<SecondsDouble>(time_read_from_disk_total),
2745
+ Ticks<MillisecondsDouble>(time_read_from_disk_total) / num_blocks_total);
2725
2746
{
2726
2747
CCoinsViewCache view (&CoinsTip ());
2727
2748
bool rv = ConnectBlock (blockConnecting, state, pindexNew, view);
@@ -2731,23 +2752,32 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
2731
2752
InvalidBlockFound (pindexNew, state);
2732
2753
return error (" %s: ConnectBlock %s failed, %s" , __func__, pindexNew->GetBlockHash ().ToString (), state.ToString ());
2733
2754
}
2734
- time_3 = GetTimeMicros ();
2755
+ time_3 = SteadyClock::now ();
2735
2756
time_connect_total += time_3 - time_2;
2736
2757
assert (num_blocks_total > 0 );
2737
- LogPrint (BCLog::BENCH, " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n " , (time_3 - time_2) * MILLI, time_connect_total * MICRO, time_connect_total * MILLI / num_blocks_total);
2758
+ LogPrint (BCLog::BENCH, " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2759
+ Ticks<MillisecondsDouble>(time_3 - time_2),
2760
+ Ticks<SecondsDouble>(time_connect_total),
2761
+ Ticks<MillisecondsDouble>(time_connect_total) / num_blocks_total);
2738
2762
bool flushed = view.Flush ();
2739
2763
assert (flushed);
2740
2764
}
2741
- int64_t time_4 = GetTimeMicros () ;
2765
+ const auto time_4{ SteadyClock::now ()} ;
2742
2766
time_flush += time_4 - time_3;
2743
- LogPrint (BCLog::BENCH, " - Flush: %.2fms [%.2fs (%.2fms/blk)]\n " , (time_4 - time_3) * MILLI, time_flush * MICRO, time_flush * MILLI / num_blocks_total);
2767
+ LogPrint (BCLog::BENCH, " - Flush: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2768
+ Ticks<MillisecondsDouble>(time_4 - time_3),
2769
+ Ticks<SecondsDouble>(time_flush),
2770
+ Ticks<MillisecondsDouble>(time_flush) / num_blocks_total);
2744
2771
// Write the chain state to disk, if necessary.
2745
2772
if (!FlushStateToDisk (state, FlushStateMode::IF_NEEDED)) {
2746
2773
return false ;
2747
2774
}
2748
- int64_t time_5 = GetTimeMicros () ;
2775
+ const auto time_5{ SteadyClock::now ()} ;
2749
2776
time_chainstate += time_5 - time_4;
2750
- LogPrint (BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n " , (time_5 - time_4) * MILLI, time_chainstate * MICRO, time_chainstate * MILLI / num_blocks_total);
2777
+ LogPrint (BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2778
+ Ticks<MillisecondsDouble>(time_5 - time_4),
2779
+ Ticks<SecondsDouble>(time_chainstate),
2780
+ Ticks<MillisecondsDouble>(time_chainstate) / num_blocks_total);
2751
2781
// Remove conflicting transactions from the mempool.;
2752
2782
if (m_mempool) {
2753
2783
m_mempool->removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
@@ -2757,11 +2787,17 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
2757
2787
m_chain.SetTip (*pindexNew);
2758
2788
UpdateTip (pindexNew);
2759
2789
2760
- int64_t time_6 = GetTimeMicros () ;
2790
+ const auto time_6{ SteadyClock::now ()} ;
2761
2791
time_post_connect += time_6 - time_5;
2762
2792
time_total += time_6 - time_1;
2763
- LogPrint (BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n " , (time_6 - time_5) * MILLI, time_post_connect * MICRO, time_post_connect * MILLI / num_blocks_total);
2764
- LogPrint (BCLog::BENCH, " - Connect block: %.2fms [%.2fs (%.2fms/blk)]\n " , (time_6 - time_1) * MILLI, time_total * MICRO, time_total * MILLI / num_blocks_total);
2793
+ LogPrint (BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2794
+ Ticks<MillisecondsDouble>(time_6 - time_5),
2795
+ Ticks<SecondsDouble>(time_post_connect),
2796
+ Ticks<MillisecondsDouble>(time_post_connect) / num_blocks_total);
2797
+ LogPrint (BCLog::BENCH, " - Connect block: %.2fms [%.2fs (%.2fms/blk)]\n " ,
2798
+ Ticks<MillisecondsDouble>(time_6 - time_1),
2799
+ Ticks<SecondsDouble>(time_total),
2800
+ Ticks<MillisecondsDouble>(time_total) / num_blocks_total);
2765
2801
2766
2802
connectTrace.BlockConnected (pindexNew, std::move (pthisBlock));
2767
2803
return true ;
0 commit comments