Commit 73d93c03 authored by Nick Mathewson's avatar Nick Mathewson 🤹
Browse files

Autodetect the number of CPUs when possible if NumCPUs==0

This is needed for IOCP, since telling the IOCP backend about all
your CPUs is a good idea.  It'll also come in handy with asn's
multithreaded crypto stuff, and for people who run servers without
reading the manual.
parent c612ddee
Loading
Loading
Loading
Loading

changes/cpudetect

0 → 100644
+3 −0
Original line number Diff line number Diff line
  o Minor features
    - If you set the NumCPUs option to 0, Tor will try to detect how many
      CPUs you have.  This is the new default behavior.
+1 −1
Original line number Diff line number Diff line
@@ -226,7 +226,7 @@ dnl -------------------------------------------------------------------
dnl Check for functions before libevent, since libevent-1.2 apparently
dnl exports strlcpy without defining it in a header.

AC_CHECK_FUNCS(gettimeofday ftime socketpair uname inet_aton strptime getrlimit strlcat strlcpy strtoull getaddrinfo localtime_r gmtime_r memmem strtok_r flock prctl vasprintf)
AC_CHECK_FUNCS(gettimeofday ftime socketpair uname inet_aton strptime getrlimit strlcat strlcpy strtoull getaddrinfo localtime_r gmtime_r memmem strtok_r flock prctl vasprintf sysconf)

using_custom_malloc=no
if test x$enable_openbsd_malloc = xyes ; then
+3 −1
Original line number Diff line number Diff line
@@ -860,7 +860,9 @@ is non-zero):
    characters inclusive, and must contain only the characters [a-zA-Z0-9].

**NumCPUs** __num__::
    How many processes to use at once for decrypting onionskins. (Default: 1)
    How many processes to use at once for decrypting onionskins and other
    parallelizable operations.  If this is set to 0, Tor will try to detect
    how many CPUs you have, defaulting to 1 if it can't tell.  (Default: 0)

**ORPort** __PORT__::
    Advertise this port to listen for connections from Tor clients and servers.
+46 −0
Original line number Diff line number Diff line
@@ -1898,6 +1898,52 @@ spawn_exit(void)
#endif
}

/** Implementation logic for compute_num_cpus(). */
static int
compute_num_cpus_impl(void)
{
#ifdef MS_WINDOWS
  SYSTEM_INFO info;
  memset(&info, 0, sizeof(info));
  GetSystemInfo(&info);
  if (info.dwNumberOfProcessors >= 1 && info.dwNumberOfProcessors < INT_MAX)
    return (int)info.dwNumberOfProcessors;
  else
    return -1;
#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF)
  long cpus = sysconf(_SC_NPROCESSORS_CONF);
  if (cpus >= 1 && cpus < INT_MAX)
    return (int)cpus;
  else
    return -1;
#else
  return -1;
#endif
}

#define MAX_DETECTABLE_CPUS 16

/** Return how many CPUs we are running with.  We assume that nobody is
 * using hot-swappable CPUs, so we don't recompute this after the first
 * time.  Return -1 if we don't know how to tell the number of CPUs on this
 * system.
 */
int
compute_num_cpus(void)
{
  static int num_cpus = -2;
  if (num_cpus == -2) {
    num_cpus = compute_num_cpus_impl();
    tor_assert(num_cpus != -2);
    if (num_cpus > MAX_DETECTABLE_CPUS)
      log_notice(LD_GENERAL, "Wow!  I detected that you have %d CPUs. I "
                 "will not autodetect any more than %d, though.  If you "
                 "want to configure more, set NumCPUs in your torrc",
                 num_cpus, MAX_DETECTABLE_CPUS);
  }
  return num_cpus;
}

/** Set *timeval to the current time of day.  On error, log and terminate.
 * (Same as gettimeofday(timeval,NULL), but never returns -1.)
 */
+2 −0
Original line number Diff line number Diff line
@@ -522,6 +522,8 @@ void spawn_exit(void) ATTR_NORETURN;
#undef TOR_IS_MULTITHREADED
#endif

int compute_num_cpus(void);

/* Because we use threads instead of processes on most platforms (Windows,
 * Linux, etc), we need locking for them.  On platforms with poor thread
 * support or broken gethostbyname_r, these functions are no-ops. */
Loading