Skip to content

Commit d70bc52

Browse files
committed
Rework block processing benchmark code
* Replace -benchmark (and the related fBenchmark) with a regular debug option, -debug=bench. * Increase coverage and granularity of individual block processing steps. * Add cummulative times.
1 parent 3955c39 commit d70bc52

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

src/init.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ std::string HelpMessage(HelpMessageMode mode)
285285
strUsage += "\n" + _("Debugging/Testing options:") + "\n";
286286
if (GetBoolArg("-help-debug", false))
287287
{
288-
strUsage += " -benchmark " + _("Show benchmark information (default: 0)") + "\n";
289288
strUsage += " -checkpoints " + _("Only accept block chain matching built-in checkpoints (default: 1)") + "\n";
290289
strUsage += " -dblogsize=<n> " + _("Flush database activity from memory pool to disk log every <n> megabytes (default: 100)") + "\n";
291290
strUsage += " -disablesafemode " + _("Disable safemode, override a real safe mode event (default: 0)") + "\n";
@@ -298,7 +297,7 @@ std::string HelpMessage(HelpMessageMode mode)
298297
strUsage += " -debug=<category> " + _("Output debugging information (default: 0, supplying <category> is optional)") + "\n";
299298
strUsage += " " + _("If <category> is not supplied, output all debugging information.") + "\n";
300299
strUsage += " " + _("<category> can be:");
301-
strUsage += " addrman, alert, coindb, db, lock, rand, rpc, selectcoins, mempool, net"; // Don't translate these and qt below
300+
strUsage += " addrman, alert, bench, coindb, db, lock, rand, rpc, selectcoins, mempool, net"; // Don't translate these and qt below
302301
if (mode == HMM_BITCOIN_QT)
303302
strUsage += ", qt";
304303
strUsage += ".\n";
@@ -599,7 +598,9 @@ bool AppInit2(boost::thread_group& threadGroup)
599598
if (GetBoolArg("-tor", false))
600599
return InitError(_("Error: Unsupported argument -tor found, use -onion."));
601600

602-
fBenchmark = GetBoolArg("-benchmark", false);
601+
if (GetBoolArg("-benchmark", false))
602+
InitWarning(_("Warning: Unsupported argument -benchmark ignored, use -debug=bench."));
603+
603604
// Checkmempool defaults to true in regtest mode
604605
mempool.setSanityCheck(GetBoolArg("-checkmempool", Params().DefaultCheckMemPool()));
605606
Checkpoints::fEnabled = GetBoolArg("-checkpoints", true);

src/main.cpp

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ CConditionVariable cvBlockChange;
4545
int nScriptCheckThreads = 0;
4646
bool fImporting = false;
4747
bool fReindex = false;
48-
bool fBenchmark = false;
4948
bool fTxIndex = false;
5049
bool fIsBareMultisigStd = true;
5150
unsigned int nCoinCacheSize = 5000;
@@ -1680,6 +1679,12 @@ void ThreadScriptCheck() {
16801679
scriptcheckqueue.Thread();
16811680
}
16821681

1682+
static int64_t nTimeVerify = 0;
1683+
static int64_t nTimeConnect = 0;
1684+
static int64_t nTimeIndex = 0;
1685+
static int64_t nTimeCallbacks = 0;
1686+
static int64_t nTimeTotal = 0;
1687+
16831688
bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck)
16841689
{
16851690
AssertLockHeld(cs_main);
@@ -1735,7 +1740,7 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
17351740

17361741
CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
17371742

1738-
int64_t nStart = GetTimeMicros();
1743+
int64_t nTimeStart = GetTimeMicros();
17391744
int64_t nFees = 0;
17401745
int nInputs = 0;
17411746
unsigned int nSigOps = 0;
@@ -1785,9 +1790,8 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
17851790
vPos.push_back(std::make_pair(tx.GetHash(), pos));
17861791
pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
17871792
}
1788-
int64_t nTime = GetTimeMicros() - nStart;
1789-
if (fBenchmark)
1790-
LogPrintf("- Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin)\n", (unsigned)block.vtx.size(), 0.001 * nTime, 0.001 * nTime / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * nTime / (nInputs-1));
1793+
int64_t nTime1 = GetTimeMicros(); nTimeConnect += nTime1 - nTimeStart;
1794+
LogPrint("bench", " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime1 - nTimeStart), 0.001 * (nTime1 - nTimeStart) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime1 - nTimeStart) / (nInputs-1), nTimeConnect * 0.000001);
17911795

17921796
if (block.vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
17931797
return state.DoS(100,
@@ -1797,9 +1801,8 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
17971801

17981802
if (!control.Wait())
17991803
return state.DoS(100, false);
1800-
int64_t nTime2 = GetTimeMicros() - nStart;
1801-
if (fBenchmark)
1802-
LogPrintf("- Verify %u txins: %.2fms (%.3fms/txin)\n", nInputs - 1, 0.001 * nTime2, nInputs <= 1 ? 0 : 0.001 * nTime2 / (nInputs-1));
1804+
int64_t nTime2 = GetTimeMicros(); nTimeVerify += nTime2 - nTimeStart;
1805+
LogPrint("bench", " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs]\n", nInputs - 1, 0.001 * (nTime2 - nTimeStart), nInputs <= 1 ? 0 : 0.001 * (nTime2 - nTimeStart) / (nInputs-1), nTimeVerify * 0.000001);
18031806

18041807
if (fJustCheck)
18051808
return true;
@@ -1840,6 +1843,9 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
18401843
ret = view.SetBestBlock(pindex->GetBlockHash());
18411844
assert(ret);
18421845

1846+
int64_t nTime3 = GetTimeMicros(); nTimeIndex += nTime3 - nTime2;
1847+
LogPrint("bench", " - Index writing: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeIndex * 0.000001);
1848+
18431849
// Watch for transactions paying to me
18441850
BOOST_FOREACH(const CTransaction& tx, block.vtx)
18451851
g_signals.SyncTransaction(tx, &block);
@@ -1849,6 +1855,9 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
18491855
g_signals.UpdatedTransaction(hashPrevBestCoinBase);
18501856
hashPrevBestCoinBase = block.vtx[0].GetHash();
18511857

1858+
int64_t nTime4 = GetTimeMicros(); nTimeCallbacks += nTime4 - nTime3;
1859+
LogPrint("bench", " - Callbacks: %.2fms [%.2fs]\n", 0.001 * (nTime4 - nTime3), nTimeCallbacks * 0.000001);
1860+
18521861
return true;
18531862
}
18541863

@@ -1928,8 +1937,7 @@ bool static DisconnectTip(CValidationState &state) {
19281937
return error("DisconnectTip() : DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
19291938
assert(view.Flush());
19301939
}
1931-
if (fBenchmark)
1932-
LogPrintf("- Disconnect: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
1940+
LogPrint("bench", "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
19331941
// Write the chain state to disk, if necessary.
19341942
if (!WriteChainState(state))
19351943
return false;
@@ -1953,16 +1961,25 @@ bool static DisconnectTip(CValidationState &state) {
19531961
return true;
19541962
}
19551963

1964+
static int64_t nTimeReadFromDisk = 0;
1965+
static int64_t nTimeConnectTotal = 0;
1966+
static int64_t nTimeFlush = 0;
1967+
static int64_t nTimeChainState = 0;
1968+
static int64_t nTimePostConnect = 0;
1969+
19561970
// Connect a new block to chainActive.
19571971
bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew) {
19581972
assert(pindexNew->pprev == chainActive.Tip());
19591973
mempool.check(pcoinsTip);
19601974
// Read block from disk.
1975+
int64_t nTime1 = GetTimeMicros();
19611976
CBlock block;
19621977
if (!ReadBlockFromDisk(block, pindexNew))
19631978
return state.Abort(_("Failed to read block"));
19641979
// Apply the block atomically to the chain state.
1965-
int64_t nStart = GetTimeMicros();
1980+
int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
1981+
int64_t nTime3;
1982+
LogPrint("bench", " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * 0.001, nTimeReadFromDisk * 0.000001);
19661983
{
19671984
CCoinsViewCache view(*pcoinsTip, true);
19681985
CInv inv(MSG_BLOCK, pindexNew->GetBlockHash());
@@ -1972,13 +1989,17 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew) {
19721989
return error("ConnectTip() : ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
19731990
}
19741991
mapBlockSource.erase(inv.hash);
1992+
nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
1993+
LogPrint("bench", " - Connect total: %.2fms [%.2fs]\n", (nTime3 - nTime2) * 0.001, nTimeConnectTotal * 0.000001);
19751994
assert(view.Flush());
19761995
}
1977-
if (fBenchmark)
1978-
LogPrintf("- Connect: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
1996+
int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
1997+
LogPrint("bench", " - Flush: %.2fms [%.2fs]\n", (nTime4 - nTime3) * 0.001, nTimeFlush * 0.000001);
19791998
// Write the chain state to disk, if necessary.
19801999
if (!WriteChainState(state))
19812000
return false;
2001+
int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
2002+
LogPrint("bench", " - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
19822003
// Remove conflicting transactions from the mempool.
19832004
list<CTransaction> txConflicted;
19842005
mempool.removeForBlock(block.vtx, pindexNew->nHeight, txConflicted);
@@ -1994,6 +2015,9 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew) {
19942015
BOOST_FOREACH(const CTransaction &tx, block.vtx) {
19952016
SyncWithWallets(tx, &block);
19962017
}
2018+
int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
2019+
LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
2020+
LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001);
19972021
return true;
19982022
}
19992023

src/main.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ extern CWaitableCriticalSection csBestBlock;
9191
extern CConditionVariable cvBlockChange;
9292
extern bool fImporting;
9393
extern bool fReindex;
94-
extern bool fBenchmark;
9594
extern int nScriptCheckThreads;
9695
extern bool fTxIndex;
9796
extern bool fIsBareMultisigStd;

0 commit comments

Comments
 (0)