]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Check for CPUs more accurartely when ONLN != CONF.
authorNick Mathewson <nickm@torproject.org>
Tue, 19 Feb 2013 07:31:53 +0000 (02:31 -0500)
committerNick Mathewson <nickm@torproject.org>
Tue, 19 Feb 2013 07:34:36 +0000 (02:34 -0500)
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.

changes/bug8002 [new file with mode: 0644]
src/common/compat.c

diff --git a/changes/bug8002 b/changes/bug8002
new file mode 100644 (file)
index 0000000..d6e2ff2
--- /dev/null
@@ -0,0 +1,5 @@
+  o Minor bugfixes:
+    - When autodetecting the number of CPUs, use the number of available
+      CPUs in preferernce to the number of configured CPUs. Inform the
+      user if this reduces the number of avialable CPUs. Fix for bug 8002.
+      Bugfix on 0.2.3.1-alpha.
index d7ce89479a4b70104587c6b5235b7b5e327d5a4e..e6206530ee40ed7f1276e8d15226c388e89ff839 100644 (file)
@@ -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