diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/init.cpp | 4 | ||||
-rw-r--r-- | src/main.cpp | 5 | ||||
-rw-r--r-- | src/qt/scicon.cpp | 6 | ||||
-rw-r--r-- | src/scheduler.cpp | 46 | ||||
-rw-r--r-- | src/scheduler.h | 15 | ||||
-rw-r--r-- | src/support/pagelocker.h | 1 | ||||
-rw-r--r-- | src/test/scheduler_tests.cpp | 27 | ||||
-rw-r--r-- | src/wallet/wallet.cpp | 2 | ||||
-rw-r--r-- | src/wallet/wallet.h | 2 | ||||
-rw-r--r-- | src/wallet/walletdb.cpp | 3 |
11 files changed, 84 insertions, 29 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 93ad26acf8..c0aba3eae6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,7 +14,7 @@ $(LIBLEVELDB): $(LIBMEMENV) $(LIBLEVELDB) $(LIBMEMENV): @echo "Building LevelDB ..." && $(MAKE) -C $(@D) $(@F) CXX="$(CXX)" \ CC="$(CC)" PLATFORM=$(TARGET_OS) AR="$(AR)" $(LEVELDB_TARGET_FLAGS) \ - OPT="$(CXXFLAGS) $(CPPFLAGS)" + OPT="$(CXXFLAGS) $(CPPFLAGS) -D__STDC_LIMIT_MACROS" endif BITCOIN_CONFIG_INCLUDES=-I$(builddir)/config diff --git a/src/init.cpp b/src/init.cpp index 3c3b3fb231..490ed6f54c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -333,7 +333,7 @@ std::string HelpMessage(HelpMessageMode mode) strUsage += HelpMessageOpt("-salvagewallet", _("Attempt to recover private keys from a corrupt wallet.dat") + " " + _("on startup")); strUsage += HelpMessageOpt("-sendfreetransactions", strprintf(_("Send transactions as zero-fee transactions if possible (default: %u)"), 0)); strUsage += HelpMessageOpt("-spendzeroconfchange", strprintf(_("Spend unconfirmed change when sending transactions (default: %u)"), 1)); - strUsage += HelpMessageOpt("-txconfirmtarget=<n>", strprintf(_("If paytxfee is not set, include enough fee so transactions begin confirmation on average within n blocks (default: %u)"), 1)); + strUsage += HelpMessageOpt("-txconfirmtarget=<n>", strprintf(_("If paytxfee is not set, include enough fee so transactions begin confirmation on average within n blocks (default: %u)"), DEFAULT_TX_CONFIRM_TARGET)); strUsage += HelpMessageOpt("-maxtxfee=<amt>", strprintf(_("Maximum total fees to use in a single wallet transaction; setting this too low may abort large transactions (default: %s)"), FormatMoney(maxTxFee))); strUsage += HelpMessageOpt("-upgradewallet", _("Upgrade wallet to latest format") + " " + _("on startup")); @@ -834,7 +834,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) mapArgs["-maxtxfee"], ::minRelayTxFee.ToString())); } } - nTxConfirmTarget = GetArg("-txconfirmtarget", 1); + nTxConfirmTarget = GetArg("-txconfirmtarget", DEFAULT_TX_CONFIRM_TARGET); bSpendZeroConfChange = GetBoolArg("-spendzeroconfchange", true); fSendFreeTransactions = GetBoolArg("-sendfreetransactions", false); diff --git a/src/main.cpp b/src/main.cpp index 462b74b937..d3956fafda 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1885,6 +1885,7 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) { try { if (fPruneMode && fCheckForPruning) { FindFilesToPrune(setFilesToPrune); + fCheckForPruning = false; if (!setFilesToPrune.empty()) { fFlushForPrune = true; if (!fHavePruned) { @@ -1941,10 +1942,8 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) { } } // Finally remove any pruned files - if (fFlushForPrune) { + if (fFlushForPrune) UnlinkPrunedFiles(setFilesToPrune); - fCheckForPruning = false; - } nLastWrite = nNow; } // Flush best chain related state. This can only be done if the blocks / block index write was also done. diff --git a/src/qt/scicon.cpp b/src/qt/scicon.cpp index b2f2399b24..c493b5569e 100644 --- a/src/qt/scicon.cpp +++ b/src/qt/scicon.cpp @@ -11,7 +11,9 @@ #include <QPalette> #include <QPixmap> -static void MakeSingleColorImage(QImage& img, const QColor& colorbase) +namespace { + +void MakeSingleColorImage(QImage& img, const QColor& colorbase) { img = img.convertToFormat(QImage::Format_ARGB32); for (int x = img.width(); x--; ) @@ -24,6 +26,8 @@ static void MakeSingleColorImage(QImage& img, const QColor& colorbase) } } +} + QImage SingleColorImage(const QString& filename, const QColor& colorbase) { QImage img(filename); diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 8b55888ae8..4f1d8be7c0 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -8,7 +8,7 @@ #include <boost/bind.hpp> #include <utility> -CScheduler::CScheduler() : nThreadsServicingQueue(0) +CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false) { } @@ -29,32 +29,37 @@ void CScheduler::serviceQueue() { boost::unique_lock<boost::mutex> lock(newTaskMutex); ++nThreadsServicingQueue; + stopRequested = false; + stopWhenEmpty = false; // newTaskMutex is locked throughout this loop EXCEPT // when the thread is waiting or when the user's function // is called. - while (1) { + while (!shouldStop()) { try { - while (taskQueue.empty()) { + while (!shouldStop() && taskQueue.empty()) { // Wait until there is something to do. newTaskScheduled.wait(lock); } -// Wait until either there is a new task, or until -// the time of the first item on the queue: + + // Wait until either there is a new task, or until + // the time of the first item on the queue: // wait_until needs boost 1.50 or later; older versions have timed_wait: #if BOOST_VERSION < 105000 - while (!taskQueue.empty() && newTaskScheduled.timed_wait(lock, toPosixTime(taskQueue.begin()->first))) { + while (!shouldStop() && !taskQueue.empty() && + newTaskScheduled.timed_wait(lock, toPosixTime(taskQueue.begin()->first))) { // Keep waiting until timeout } #else - while (!taskQueue.empty() && newTaskScheduled.wait_until(lock, taskQueue.begin()->first) != boost::cv_status::timeout) { + while (!shouldStop() && !taskQueue.empty() && + newTaskScheduled.wait_until(lock, taskQueue.begin()->first) != boost::cv_status::timeout) { // Keep waiting until timeout } #endif // If there are multiple threads, the queue can empty while we're waiting (another // thread may service the task we were waiting on). - if (taskQueue.empty()) + if (shouldStop() || taskQueue.empty()) continue; Function f = taskQueue.begin()->second; @@ -70,6 +75,19 @@ void CScheduler::serviceQueue() throw; } } + --nThreadsServicingQueue; +} + +void CScheduler::stop(bool drain) +{ + { + boost::unique_lock<boost::mutex> lock(newTaskMutex); + if (drain) + stopWhenEmpty = true; + else + stopRequested = true; + } + newTaskScheduled.notify_all(); } void CScheduler::schedule(CScheduler::Function f, boost::chrono::system_clock::time_point t) @@ -96,3 +114,15 @@ void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaSeconds) { scheduleFromNow(boost::bind(&Repeat, this, f, deltaSeconds), deltaSeconds); } + +size_t CScheduler::getQueueInfo(boost::chrono::system_clock::time_point &first, + boost::chrono::system_clock::time_point &last) const +{ + boost::unique_lock<boost::mutex> lock(newTaskMutex); + size_t result = taskQueue.size(); + if (!taskQueue.empty()) { + first = taskQueue.begin()->first; + last = taskQueue.rbegin()->first; + } + return result; +} diff --git a/src/scheduler.h b/src/scheduler.h index bb383ab9f9..436659e58b 100644 --- a/src/scheduler.h +++ b/src/scheduler.h @@ -60,11 +60,24 @@ public: // and interrupted using boost::interrupt_thread void serviceQueue(); + // Tell any threads running serviceQueue to stop as soon as they're + // done servicing whatever task they're currently servicing (drain=false) + // or when there is no work left to be done (drain=true) + void stop(bool drain=false); + + // Returns number of tasks waiting to be serviced, + // and first and last task times + size_t getQueueInfo(boost::chrono::system_clock::time_point &first, + boost::chrono::system_clock::time_point &last) const; + private: std::multimap<boost::chrono::system_clock::time_point, Function> taskQueue; boost::condition_variable newTaskScheduled; - boost::mutex newTaskMutex; + mutable boost::mutex newTaskMutex; int nThreadsServicingQueue; + bool stopRequested; + bool stopWhenEmpty; + bool shouldStop() { return stopRequested || (stopWhenEmpty && taskQueue.empty()); } }; #endif diff --git a/src/support/pagelocker.h b/src/support/pagelocker.h index 3fd793072f..88b95cce73 100644 --- a/src/support/pagelocker.h +++ b/src/support/pagelocker.h @@ -37,7 +37,6 @@ public: ~LockedPageManagerBase() { - assert(this->GetLockedPageCount() == 0); } diff --git a/src/test/scheduler_tests.cpp b/src/test/scheduler_tests.cpp index a26d0afaed..cb1a427db0 100644 --- a/src/test/scheduler_tests.cpp +++ b/src/test/scheduler_tests.cpp @@ -42,6 +42,8 @@ static void MicroSleep(uint64_t n) BOOST_AUTO_TEST_CASE(manythreads) { + seed_insecure_rand(false); + // Stress test: hundreds of microsecond-scheduled tasks, // serviced by 10 threads. // @@ -54,10 +56,6 @@ BOOST_AUTO_TEST_CASE(manythreads) // counters should sum to the number of initial tasks performed. CScheduler microTasks; - boost::thread_group microThreads; - for (int i = 0; i < 5; i++) - microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, µTasks)); - boost::mutex counterMutex[10]; int counter[10] = { 0 }; boost::random::mt19937 rng(insecure_rand()); @@ -67,6 +65,9 @@ BOOST_AUTO_TEST_CASE(manythreads) boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now(); boost::chrono::system_clock::time_point now = start; + boost::chrono::system_clock::time_point first, last; + size_t nTasks = microTasks.getQueueInfo(first, last); + BOOST_CHECK(nTasks == 0); for (int i = 0; i < 100; i++) { boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng)); @@ -77,9 +78,19 @@ BOOST_AUTO_TEST_CASE(manythreads) randomDelta(rng), tReschedule); microTasks.schedule(f, t); } + nTasks = microTasks.getQueueInfo(first, last); + BOOST_CHECK(nTasks == 100); + BOOST_CHECK(first < last); + BOOST_CHECK(last > now); + + // As soon as these are created they will start running and servicing the queue + boost::thread_group microThreads; + for (int i = 0; i < 5; i++) + microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, µTasks)); MicroSleep(600); now = boost::chrono::system_clock::now(); + // More threads and more tasks: for (int i = 0; i < 5; i++) microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, µTasks)); @@ -93,11 +104,9 @@ BOOST_AUTO_TEST_CASE(manythreads) microTasks.schedule(f, t); } - // All 2,000 tasks should be finished within 2 milliseconds. Sleep a bit longer. - MicroSleep(2100); - - microThreads.interrupt_all(); - microThreads.join_all(); + // Drain the task queue then exit threads + microTasks.stop(true); + microThreads.join_all(); // ... wait until all the threads are done int counterSum = 0; for (int i = 0; i < 10; i++) { diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 0afdcd0011..3396a3a188 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -30,7 +30,7 @@ using namespace std; */ CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE); CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE; -unsigned int nTxConfirmTarget = 1; +unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET; bool bSpendZeroConfChange = true; bool fSendFreeTransactions = false; bool fPayAtLeastCustomFee = true; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 1c900b0315..b0da92cfd1 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -44,6 +44,8 @@ static const CAmount DEFAULT_TRANSACTION_FEE = 0; static const CAmount nHighTransactionFeeWarning = 0.01 * COIN; //! -maxtxfee default static const CAmount DEFAULT_TRANSACTION_MAXFEE = 0.1 * COIN; +//! -txconfirmtarget default +static const unsigned int DEFAULT_TX_CONFIRM_TARGET = 2; //! -maxtxfee will warn if called with a higher fee than this amount (in satoshis) static const CAmount nHighTransactionMaxFeeWarning = 100 * nHighTransactionFeeWarning; //! Largest (in bytes) free transaction we're willing to create diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index de56a2d1af..13eaf95aa6 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -915,7 +915,7 @@ bool CWalletDB::Recover(CDBEnv& dbenv, const std::string& filename, bool fOnlyKe } std::vector<CDBEnv::KeyValPair> salvagedData; - bool allOK = dbenv.Salvage(newFilename, true, salvagedData); + bool fSuccess = dbenv.Salvage(newFilename, true, salvagedData); if (salvagedData.empty()) { LogPrintf("Salvage(aggressive) found no records in %s.\n", newFilename); @@ -923,7 +923,6 @@ bool CWalletDB::Recover(CDBEnv& dbenv, const std::string& filename, bool fOnlyKe } LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size()); - bool fSuccess = allOK; boost::scoped_ptr<Db> pdbCopy(new Db(dbenv.dbenv, 0)); int ret = pdbCopy->open(NULL, // Txn pointer filename.c_str(), // Filename |