Skip to content

Commit

Permalink
Merge pull request bitcoin#1151 from sickpig/fix-virt-cores
Browse files Browse the repository at this point in the history
[PORT] Default to virtual cores and set appropriate thread scheduler
  • Loading branch information
gandrewstone committed Aug 22, 2018
2 parents b4064e1 + 32bdf1c commit 068934f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/init.cpp
Expand Up @@ -395,6 +395,8 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
{
const CChainParams &chainparams = Params();
RenameThread("bitcoin-loadblk");
ScheduleBatchPriority();

// -reindex
if (fReindex)
{
Expand Down
27 changes: 18 additions & 9 deletions src/util.cpp
Expand Up @@ -42,6 +42,7 @@

#include <algorithm>
#include <fcntl.h>
#include <sched.h>
#include <sys/resource.h>
#include <sys/stat.h>

Expand Down Expand Up @@ -89,6 +90,7 @@
#include <openssl/conf.h>
#include <openssl/crypto.h>
#include <openssl/rand.h>
#include <thread>

std::vector<std::string> splitByCommasAndRemoveSpaces(const std::vector<std::string> &args)
{
Expand Down Expand Up @@ -930,15 +932,7 @@ void SetThreadPriority(int nPriority)
#endif // WIN32
}

int GetNumCores()
{
#if BOOST_VERSION >= 105600
return boost::thread::physical_concurrency();
#else // Must fall back to hardware_concurrency, which unfortunately counts virtual cores
return boost::thread::hardware_concurrency();
#endif
}

int GetNumCores() { return std::thread::hardware_concurrency(); }
std::string CopyrightHolders(const std::string &strPrefix)
{
std::string strCopyrightHolders = strPrefix + _(COPYRIGHT_HOLDERS);
Expand Down Expand Up @@ -1039,3 +1033,18 @@ bool wildmatch(string pattern, string test)
test = test.substr(1);
}
}

int ScheduleBatchPriority(void)
{
#ifdef SCHED_BATCH
const static sched_param param{0};
if (int ret = pthread_setschedparam(pthread_self(), SCHED_BATCH, &param))
{
LOGA("Failed to pthread_setschedparam: %s\n", strerror(errno));
return ret;
}
return 0;
#else
return 1;
#endif
}
14 changes: 11 additions & 3 deletions src/util.h
Expand Up @@ -444,9 +444,8 @@ bool SoftSetArg(const std::string &strArg, const std::string &strValue);
bool SoftSetBoolArg(const std::string &strArg, bool fValue);

/**
* Return the number of physical cores available on the current system.
* @note This does not count virtual cores, such as those provided by HyperThreading
* when boost is newer than 1.56.
* Return the number of cores available on the current system.
* @note This does count virtual cores, such as those provided by HyperThreading.
*/
int GetNumCores();

Expand Down Expand Up @@ -492,4 +491,13 @@ the second argument will be matched to this pattern. Returns true iff the string
matches pattern. */
bool wildmatch(std::string pattern, std::string test);

/**
* On platforms that support it, tell the kernel the calling thread is
* CPU-intensive and non-interactive. See SCHED_BATCH in sched(7) for details.
*
* @return The return value of sched_setschedule(), or 1 on systems without
* sched_setchedule().
*/
int ScheduleBatchPriority(void);

#endif // BITCOIN_UTIL_H

0 comments on commit 068934f

Please sign in to comment.