Skip to content

Commit fabf1cd

Browse files
author
MacroFake
committed
Use steady clock for bench logging
1 parent faed342 commit fabf1cd

File tree

4 files changed

+92
-51
lines changed

4 files changed

+92
-51
lines changed

src/node/miner.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void BlockAssembler::resetBlock()
105105

106106
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
107107
{
108-
int64_t time_start = GetTimeMicros();
108+
const auto time_start{SteadyClock::now()};
109109

110110
resetBlock();
111111

@@ -143,7 +143,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
143143
addPackageTxs(*m_mempool, nPackagesSelected, nDescendantsUpdated);
144144
}
145145

146-
int64_t time_1 = GetTimeMicros();
146+
const auto time_1{SteadyClock::now()};
147147

148148
m_last_block_num_txs = nBlockTx;
149149
m_last_block_weight = nBlockWeight;
@@ -173,9 +173,12 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
173173
if (!TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev, GetAdjustedTime, false, false)) {
174174
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
175175
}
176-
int64_t time_2 = GetTimeMicros();
176+
const auto time_2{SteadyClock::now()};
177177

178-
LogPrint(BCLog::BENCH, "CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n", 0.001 * (time_1 - time_start), nPackagesSelected, nDescendantsUpdated, 0.001 * (time_2 - time_1), 0.001 * (time_2 - time_start));
178+
LogPrint(BCLog::BENCH, "CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n",
179+
Ticks<MillisecondsDouble>(time_1 - time_start), nPackagesSelected, nDescendantsUpdated,
180+
Ticks<MillisecondsDouble>(time_2 - time_1),
181+
Ticks<MillisecondsDouble>(time_2 - time_start));
179182

180183
return std::move(pblocktemplate);
181184
}

src/policy/fees.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -997,17 +997,18 @@ bool CBlockPolicyEstimator::Read(AutoFile& filein)
997997
return true;
998998
}
999999

1000-
void CBlockPolicyEstimator::FlushUnconfirmed() {
1001-
int64_t startclear = GetTimeMicros();
1000+
void CBlockPolicyEstimator::FlushUnconfirmed()
1001+
{
1002+
const auto startclear{SteadyClock::now()};
10021003
LOCK(m_cs_fee_estimator);
10031004
size_t num_entries = mapMemPoolTxs.size();
10041005
// Remove every entry in mapMemPoolTxs
10051006
while (!mapMemPoolTxs.empty()) {
10061007
auto mi = mapMemPoolTxs.begin();
10071008
_removeTx(mi->first, false); // this calls erase() on mapMemPoolTxs
10081009
}
1009-
int64_t endclear = GetTimeMicros();
1010-
LogPrint(BCLog::ESTIMATEFEE, "Recorded %u unconfirmed txs from mempool in %gs\n", num_entries, (endclear - startclear)*0.000001);
1010+
const auto endclear{SteadyClock::now()};
1011+
LogPrint(BCLog::ESTIMATEFEE, "Recorded %u unconfirmed txs from mempool in %gs\n", num_entries, Ticks<SecondsDouble>(endclear - startclear));
10111012
}
10121013

10131014
FeeFilterRounder::FeeFilterRounder(const CFeeRate& minIncrementalFee)

src/util/time.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.cou
5757

5858
using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>;
5959
using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>;
60+
using MillisecondsDouble = std::chrono::duration<double, std::chrono::milliseconds::period>;
6061

6162
/**
6263
* DEPRECATED

src/validation.cpp

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ using node::SnapshotMetadata;
8282
using node::UndoReadFromDisk;
8383
using 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 */
8986
static const unsigned int MAX_DISCONNECTED_TX_POOL_SIZE = 20000;
9087
/** Time to wait between writing blocks/block index to disk. */
@@ -1965,13 +1962,13 @@ static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const Ch
19651962
}
19661963

19671964

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{};
19751972
static int64_t num_blocks_total = 0;
19761973

19771974
/** 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,
19861983
uint256 block_hash{block.GetHash()};
19871984
assert(*pindex->phashBlock == block_hash);
19881985

1989-
int64_t time_start = GetTimeMicros();
1986+
const auto time_start{SteadyClock::now()};
19901987

19911988
// Check it again in case a previous version let a bad block in
19921989
// NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
@@ -2056,9 +2053,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
20562053
}
20572054
}
20582055

2059-
int64_t time_1 = GetTimeMicros();
2056+
const auto time_1{SteadyClock::now()};
20602057
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);
20622062

20632063
// Do not allow blocks that contain transactions which 'overwrite' older transactions,
20642064
// unless those are already completely spent.
@@ -2156,9 +2156,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
21562156
// Get the script flags for this block
21572157
unsigned int flags{GetBlockScriptFlags(*pindex, m_chainman)};
21582158

2159-
int64_t time_2 = GetTimeMicros();
2159+
const auto time_2{SteadyClock::now()};
21602160
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);
21622165

21632166
CBlockUndo blockundo;
21642167

@@ -2242,9 +2245,13 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
22422245
}
22432246
UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
22442247
}
2245-
int64_t time_3 = GetTimeMicros();
2248+
const auto time_3{SteadyClock::now()};
22462249
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);
22482255

22492256
CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, m_params.GetConsensus());
22502257
if (block.vtx[0]->GetValueOut() > blockReward) {
@@ -2256,9 +2263,13 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
22562263
LogPrintf("ERROR: %s: CheckQueue failed\n", __func__);
22572264
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "block-validation-failed");
22582265
}
2259-
int64_t time_4 = GetTimeMicros();
2266+
const auto time_4{SteadyClock::now()};
22602267
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);
22622273

22632274
if (fJustCheck)
22642275
return true;
@@ -2267,9 +2278,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
22672278
return false;
22682279
}
22692280

2270-
int64_t time_5 = GetTimeMicros();
2281+
const auto time_5{SteadyClock::now()};
22712282
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);
22732287

22742288
if (!pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
22752289
pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
@@ -2279,9 +2293,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
22792293
// add this block to the view's block chain
22802294
view.SetBestBlock(pindex->GetBlockHash());
22812295

2282-
int64_t time_6 = GetTimeMicros();
2296+
const auto time_6{SteadyClock::now()};
22832297
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);
22852302

22862303
TRACE6(validation, block_connected,
22872304
block_hash.data(),
@@ -2596,7 +2613,7 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
25962613
return error("DisconnectTip(): Failed to read block");
25972614
}
25982615
// Apply the block atomically to the chain state.
2599-
int64_t time_start = GetTimeMicros();
2616+
const auto time_start{SteadyClock::now()};
26002617
{
26012618
CCoinsViewCache view(&CoinsTip());
26022619
assert(view.GetBestBlock() == pindexDelete->GetBlockHash());
@@ -2605,7 +2622,8 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
26052622
bool flushed = view.Flush();
26062623
assert(flushed);
26072624
}
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));
26092627

26102628
{
26112629
// 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
26452663
return true;
26462664
}
26472665

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{};
26532671

26542672
struct PerBlockConnectTrace {
26552673
CBlockIndex* pindex = nullptr;
@@ -2704,7 +2722,7 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
27042722

27052723
assert(pindexNew->pprev == m_chain.Tip());
27062724
// Read block from disk.
2707-
int64_t time_1 = GetTimeMicros();
2725+
const auto time_1{SteadyClock::now()};
27082726
std::shared_ptr<const CBlock> pthisBlock;
27092727
if (!pblock) {
27102728
std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
@@ -2718,10 +2736,13 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
27182736
}
27192737
const CBlock& blockConnecting = *pthisBlock;
27202738
// Apply the block atomically to the chain state.
2721-
int64_t time_2 = GetTimeMicros();
2739+
const auto time_2{SteadyClock::now()};
27222740
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);
27252746
{
27262747
CCoinsViewCache view(&CoinsTip());
27272748
bool rv = ConnectBlock(blockConnecting, state, pindexNew, view);
@@ -2731,23 +2752,32 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
27312752
InvalidBlockFound(pindexNew, state);
27322753
return error("%s: ConnectBlock %s failed, %s", __func__, pindexNew->GetBlockHash().ToString(), state.ToString());
27332754
}
2734-
time_3 = GetTimeMicros();
2755+
time_3 = SteadyClock::now();
27352756
time_connect_total += time_3 - time_2;
27362757
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);
27382762
bool flushed = view.Flush();
27392763
assert(flushed);
27402764
}
2741-
int64_t time_4 = GetTimeMicros();
2765+
const auto time_4{SteadyClock::now()};
27422766
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);
27442771
// Write the chain state to disk, if necessary.
27452772
if (!FlushStateToDisk(state, FlushStateMode::IF_NEEDED)) {
27462773
return false;
27472774
}
2748-
int64_t time_5 = GetTimeMicros();
2775+
const auto time_5{SteadyClock::now()};
27492776
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);
27512781
// Remove conflicting transactions from the mempool.;
27522782
if (m_mempool) {
27532783
m_mempool->removeForBlock(blockConnecting.vtx, pindexNew->nHeight);
@@ -2757,11 +2787,17 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
27572787
m_chain.SetTip(*pindexNew);
27582788
UpdateTip(pindexNew);
27592789

2760-
int64_t time_6 = GetTimeMicros();
2790+
const auto time_6{SteadyClock::now()};
27612791
time_post_connect += time_6 - time_5;
27622792
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);
27652801

27662802
connectTrace.BlockConnected(pindexNew, std::move(pthisBlock));
27672803
return true;

0 commit comments

Comments
 (0)