From f9cae832e6f56c6abe89b3bf05d1f176c2a7c913 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sat, 1 Dec 2012 23:04:14 +0100 Subject: Parallelize script verification * During block verification (when parallelism is requested), script check actions are stored instead of being executed immediately. * After every processed transactions, its signature actions are pushed to a CScriptCheckQueue, which maintains a queue and some synchronization mechanism. * Two or more threads (if enabled) start processing elements from this queue, * When the block connection code is finished processing transactions, it joins the worker pool until the queue is empty. As cs_main is held the entire time, and all verification must be finished before the block continues processing, this does not reach the best possible performance. It is a less drastic change than some more advanced mechanisms (like doing verification out-of-band entirely, and rolling back blocks when a failure is detected). The -par=N flag controls the number of threads (1-16). 0 means auto, and is the default. --- src/init.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index 557c23fc9..087750cf9 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -84,6 +84,10 @@ void Shutdown(void* parg) fShutdown = true; nTransactionsUpdated++; bitdb.Flush(false); + { + LOCK(cs_main); + ThreadScriptCheckQuit(); + } StopNode(); { LOCK(cs_main); @@ -303,6 +307,7 @@ std::string HelpMessage() " -checklevel= " + _("How thorough the block verification is (0-6, default: 1)") + "\n" + " -loadblock= " + _("Imports blocks from external blk000??.dat file") + "\n" + " -reindex " + _("Rebuild blockchain index from current blk000??.dat files") + "\n" + + " -par=N " + _("Set the number of script verification threads (1-16, 0=auto, default: 0)") + "\n" + "\n" + _("Block creation options:") + "\n" + " -blockminsize= " + _("Set minimum block size in bytes (default: 0)") + "\n" + @@ -484,6 +489,15 @@ bool AppInit2() fDebug = GetBoolArg("-debug"); fBenchmark = GetBoolArg("-benchmark"); + // -par=0 means autodetect, but nScriptCheckThreads==0 means no concurrency + nScriptCheckThreads = GetArg("-par", 0); + if (nScriptCheckThreads == 0) + nScriptCheckThreads = boost::thread::hardware_concurrency(); + if (nScriptCheckThreads <= 1) + nScriptCheckThreads = 0; + else if (nScriptCheckThreads > MAX_SCRIPTCHECK_THREADS) + nScriptCheckThreads = MAX_SCRIPTCHECK_THREADS; + // -debug implies fDebug* if (fDebug) fDebugNet = true; @@ -579,6 +593,12 @@ bool AppInit2() if (fDaemon) fprintf(stdout, "Bitcoin server starting\n"); + if (nScriptCheckThreads) { + printf("Using %u threads for script verification\n", nScriptCheckThreads); + for (int i=0; i