diff options
author | Nick Mathewson <nickm@torproject.org> | 2013-02-19 02:31:53 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2013-02-19 02:34:36 -0500 |
commit | 22804c0391f0549356959e1b474c43bfcba1b8cb (patch) | |
tree | 76c4cb6afebf9c2520a7ed11d0a9953d07843fc9 /src/common/compat.c | |
parent | 59fc77e29b17840bf403d9ee9245846ad807b12d (diff) | |
download | tor-22804c0391f0549356959e1b474c43bfcba1b8cb.tar.gz tor-22804c0391f0549356959e1b474c43bfcba1b8cb.zip |
Check for CPUs more accurartely when ONLN != CONF.
There are two ways to use sysconf to ask about the number of
CPUs. When we're on a VM, we would sometimes get it wrong by asking
for the number of total CPUs (say, 64) when we should have been asking
for the number of CPUs online (say, 1 or 2).
Fix for bug 8002.
Diffstat (limited to 'src/common/compat.c')
-rw-r--r-- | src/common/compat.c | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index d7ce89479a..e6206530ee 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -2265,8 +2265,33 @@ compute_num_cpus_impl(void) return (int)info.dwNumberOfProcessors; else return -1; -#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF) - long cpus = sysconf(_SC_NPROCESSORS_CONF); +#elif defined(HAVE_SYSCONF) +#ifdef _SC_NPROCESSORS_CONF + long cpus_conf = sysconf(_SC_NPROCESSORS_CONF); +#else + long cpus_conf = -1; +#endif +#ifdef _SC_NPROCESSORS_ONLN + long cpus_onln = sysconf(_SC_NPROCESSORS_ONLN); +#else + long cpus_onln = -1; +#endif + long cpus = -1; + + if (cpus_conf > 0 && cpus_onln < 0) { + cpus = cpus_conf; + } else if (cpus_onln > 0 && cpus_conf < 0) { + cpus = cpus_onln; + } else if (cpus_onln > 0 && cpus_conf > 0) { + if (cpus_onln < cpus_conf) { + log_notice(LD_GENERAL, "I think we have %ld CPUS, but only %ld of them " + "are available. Telling Tor to only use %ld. You can over" + "ride this with the NumCPUs option", + cpus_conf, cpus_onln, cpus_onln); + } + cpus = cpus_onln; + } + if (cpus >= 1 && cpus < INT_MAX) return (int)cpus; else |